shape_base.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack',
  2. 'stack', 'vstack']
  3. import functools
  4. import itertools
  5. import operator
  6. import warnings
  7. from . import numeric as _nx
  8. from . import overrides
  9. from .multiarray import array, asanyarray, normalize_axis_index
  10. from . import fromnumeric as _from_nx
  11. array_function_dispatch = functools.partial(
  12. overrides.array_function_dispatch, module='numpy')
  13. def _atleast_1d_dispatcher(*arys):
  14. return arys
  15. @array_function_dispatch(_atleast_1d_dispatcher)
  16. def atleast_1d(*arys):
  17. """
  18. Convert inputs to arrays with at least one dimension.
  19. Scalar inputs are converted to 1-dimensional arrays, whilst
  20. higher-dimensional inputs are preserved.
  21. Parameters
  22. ----------
  23. arys1, arys2, ... : array_like
  24. One or more input arrays.
  25. Returns
  26. -------
  27. ret : ndarray
  28. An array, or list of arrays, each with ``a.ndim >= 1``.
  29. Copies are made only if necessary.
  30. See Also
  31. --------
  32. atleast_2d, atleast_3d
  33. Examples
  34. --------
  35. >>> np.atleast_1d(1.0)
  36. array([1.])
  37. >>> x = np.arange(9.0).reshape(3,3)
  38. >>> np.atleast_1d(x)
  39. array([[0., 1., 2.],
  40. [3., 4., 5.],
  41. [6., 7., 8.]])
  42. >>> np.atleast_1d(x) is x
  43. True
  44. >>> np.atleast_1d(1, [3, 4])
  45. [array([1]), array([3, 4])]
  46. """
  47. res = []
  48. for ary in arys:
  49. ary = asanyarray(ary)
  50. if ary.ndim == 0:
  51. result = ary.reshape(1)
  52. else:
  53. result = ary
  54. res.append(result)
  55. if len(res) == 1:
  56. return res[0]
  57. else:
  58. return res
  59. def _atleast_2d_dispatcher(*arys):
  60. return arys
  61. @array_function_dispatch(_atleast_2d_dispatcher)
  62. def atleast_2d(*arys):
  63. """
  64. View inputs as arrays with at least two dimensions.
  65. Parameters
  66. ----------
  67. arys1, arys2, ... : array_like
  68. One or more array-like sequences. Non-array inputs are converted
  69. to arrays. Arrays that already have two or more dimensions are
  70. preserved.
  71. Returns
  72. -------
  73. res, res2, ... : ndarray
  74. An array, or list of arrays, each with ``a.ndim >= 2``.
  75. Copies are avoided where possible, and views with two or more
  76. dimensions are returned.
  77. See Also
  78. --------
  79. atleast_1d, atleast_3d
  80. Examples
  81. --------
  82. >>> np.atleast_2d(3.0)
  83. array([[3.]])
  84. >>> x = np.arange(3.0)
  85. >>> np.atleast_2d(x)
  86. array([[0., 1., 2.]])
  87. >>> np.atleast_2d(x).base is x
  88. True
  89. >>> np.atleast_2d(1, [1, 2], [[1, 2]])
  90. [array([[1]]), array([[1, 2]]), array([[1, 2]])]
  91. """
  92. res = []
  93. for ary in arys:
  94. ary = asanyarray(ary)
  95. if ary.ndim == 0:
  96. result = ary.reshape(1, 1)
  97. elif ary.ndim == 1:
  98. result = ary[_nx.newaxis, :]
  99. else:
  100. result = ary
  101. res.append(result)
  102. if len(res) == 1:
  103. return res[0]
  104. else:
  105. return res
  106. def _atleast_3d_dispatcher(*arys):
  107. return arys
  108. @array_function_dispatch(_atleast_3d_dispatcher)
  109. def atleast_3d(*arys):
  110. """
  111. View inputs as arrays with at least three dimensions.
  112. Parameters
  113. ----------
  114. arys1, arys2, ... : array_like
  115. One or more array-like sequences. Non-array inputs are converted to
  116. arrays. Arrays that already have three or more dimensions are
  117. preserved.
  118. Returns
  119. -------
  120. res1, res2, ... : ndarray
  121. An array, or list of arrays, each with ``a.ndim >= 3``. Copies are
  122. avoided where possible, and views with three or more dimensions are
  123. returned. For example, a 1-D array of shape ``(N,)`` becomes a view
  124. of shape ``(1, N, 1)``, and a 2-D array of shape ``(M, N)`` becomes a
  125. view of shape ``(M, N, 1)``.
  126. See Also
  127. --------
  128. atleast_1d, atleast_2d
  129. Examples
  130. --------
  131. >>> np.atleast_3d(3.0)
  132. array([[[3.]]])
  133. >>> x = np.arange(3.0)
  134. >>> np.atleast_3d(x).shape
  135. (1, 3, 1)
  136. >>> x = np.arange(12.0).reshape(4,3)
  137. >>> np.atleast_3d(x).shape
  138. (4, 3, 1)
  139. >>> np.atleast_3d(x).base is x.base # x is a reshape, so not base itself
  140. True
  141. >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):
  142. ... print(arr, arr.shape) # doctest: +SKIP
  143. ...
  144. [[[1]
  145. [2]]] (1, 2, 1)
  146. [[[1]
  147. [2]]] (1, 2, 1)
  148. [[[1 2]]] (1, 1, 2)
  149. """
  150. res = []
  151. for ary in arys:
  152. ary = asanyarray(ary)
  153. if ary.ndim == 0:
  154. result = ary.reshape(1, 1, 1)
  155. elif ary.ndim == 1:
  156. result = ary[_nx.newaxis, :, _nx.newaxis]
  157. elif ary.ndim == 2:
  158. result = ary[:, :, _nx.newaxis]
  159. else:
  160. result = ary
  161. res.append(result)
  162. if len(res) == 1:
  163. return res[0]
  164. else:
  165. return res
  166. def _arrays_for_stack_dispatcher(arrays, stacklevel=4):
  167. if not hasattr(arrays, '__getitem__') and hasattr(arrays, '__iter__'):
  168. warnings.warn('arrays to stack must be passed as a "sequence" type '
  169. 'such as list or tuple. Support for non-sequence '
  170. 'iterables such as generators is deprecated as of '
  171. 'NumPy 1.16 and will raise an error in the future.',
  172. FutureWarning, stacklevel=stacklevel)
  173. return ()
  174. return arrays
  175. def _vhstack_dispatcher(tup, *,
  176. dtype=None, casting=None):
  177. return _arrays_for_stack_dispatcher(tup)
  178. @array_function_dispatch(_vhstack_dispatcher)
  179. def vstack(tup, *, dtype=None, casting="same_kind"):
  180. """
  181. Stack arrays in sequence vertically (row wise).
  182. This is equivalent to concatenation along the first axis after 1-D arrays
  183. of shape `(N,)` have been reshaped to `(1,N)`. Rebuilds arrays divided by
  184. `vsplit`.
  185. This function makes most sense for arrays with up to 3 dimensions. For
  186. instance, for pixel-data with a height (first axis), width (second axis),
  187. and r/g/b channels (third axis). The functions `concatenate`, `stack` and
  188. `block` provide more general stacking and concatenation operations.
  189. ``np.row_stack`` is an alias for `vstack`. They are the same function.
  190. Parameters
  191. ----------
  192. tup : sequence of ndarrays
  193. The arrays must have the same shape along all but the first axis.
  194. 1-D arrays must have the same length.
  195. dtype : str or dtype
  196. If provided, the destination array will have this dtype. Cannot be
  197. provided together with `out`.
  198. .. versionadded:: 1.24
  199. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
  200. Controls what kind of data casting may occur. Defaults to 'same_kind'.
  201. .. versionadded:: 1.24
  202. Returns
  203. -------
  204. stacked : ndarray
  205. The array formed by stacking the given arrays, will be at least 2-D.
  206. See Also
  207. --------
  208. concatenate : Join a sequence of arrays along an existing axis.
  209. stack : Join a sequence of arrays along a new axis.
  210. block : Assemble an nd-array from nested lists of blocks.
  211. hstack : Stack arrays in sequence horizontally (column wise).
  212. dstack : Stack arrays in sequence depth wise (along third axis).
  213. column_stack : Stack 1-D arrays as columns into a 2-D array.
  214. vsplit : Split an array into multiple sub-arrays vertically (row-wise).
  215. Examples
  216. --------
  217. >>> a = np.array([1, 2, 3])
  218. >>> b = np.array([4, 5, 6])
  219. >>> np.vstack((a,b))
  220. array([[1, 2, 3],
  221. [4, 5, 6]])
  222. >>> a = np.array([[1], [2], [3]])
  223. >>> b = np.array([[4], [5], [6]])
  224. >>> np.vstack((a,b))
  225. array([[1],
  226. [2],
  227. [3],
  228. [4],
  229. [5],
  230. [6]])
  231. """
  232. if not overrides.ARRAY_FUNCTION_ENABLED:
  233. # raise warning if necessary
  234. _arrays_for_stack_dispatcher(tup, stacklevel=2)
  235. arrs = atleast_2d(*tup)
  236. if not isinstance(arrs, list):
  237. arrs = [arrs]
  238. return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
  239. @array_function_dispatch(_vhstack_dispatcher)
  240. def hstack(tup, *, dtype=None, casting="same_kind"):
  241. """
  242. Stack arrays in sequence horizontally (column wise).
  243. This is equivalent to concatenation along the second axis, except for 1-D
  244. arrays where it concatenates along the first axis. Rebuilds arrays divided
  245. by `hsplit`.
  246. This function makes most sense for arrays with up to 3 dimensions. For
  247. instance, for pixel-data with a height (first axis), width (second axis),
  248. and r/g/b channels (third axis). The functions `concatenate`, `stack` and
  249. `block` provide more general stacking and concatenation operations.
  250. Parameters
  251. ----------
  252. tup : sequence of ndarrays
  253. The arrays must have the same shape along all but the second axis,
  254. except 1-D arrays which can be any length.
  255. dtype : str or dtype
  256. If provided, the destination array will have this dtype. Cannot be
  257. provided together with `out`.
  258. .. versionadded:: 1.24
  259. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
  260. Controls what kind of data casting may occur. Defaults to 'same_kind'.
  261. .. versionadded:: 1.24
  262. Returns
  263. -------
  264. stacked : ndarray
  265. The array formed by stacking the given arrays.
  266. See Also
  267. --------
  268. concatenate : Join a sequence of arrays along an existing axis.
  269. stack : Join a sequence of arrays along a new axis.
  270. block : Assemble an nd-array from nested lists of blocks.
  271. vstack : Stack arrays in sequence vertically (row wise).
  272. dstack : Stack arrays in sequence depth wise (along third axis).
  273. column_stack : Stack 1-D arrays as columns into a 2-D array.
  274. hsplit : Split an array into multiple sub-arrays horizontally (column-wise).
  275. Examples
  276. --------
  277. >>> a = np.array((1,2,3))
  278. >>> b = np.array((4,5,6))
  279. >>> np.hstack((a,b))
  280. array([1, 2, 3, 4, 5, 6])
  281. >>> a = np.array([[1],[2],[3]])
  282. >>> b = np.array([[4],[5],[6]])
  283. >>> np.hstack((a,b))
  284. array([[1, 4],
  285. [2, 5],
  286. [3, 6]])
  287. """
  288. if not overrides.ARRAY_FUNCTION_ENABLED:
  289. # raise warning if necessary
  290. _arrays_for_stack_dispatcher(tup, stacklevel=2)
  291. arrs = atleast_1d(*tup)
  292. if not isinstance(arrs, list):
  293. arrs = [arrs]
  294. # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
  295. if arrs and arrs[0].ndim == 1:
  296. return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
  297. else:
  298. return _nx.concatenate(arrs, 1, dtype=dtype, casting=casting)
  299. def _stack_dispatcher(arrays, axis=None, out=None, *,
  300. dtype=None, casting=None):
  301. arrays = _arrays_for_stack_dispatcher(arrays, stacklevel=6)
  302. if out is not None:
  303. # optimize for the typical case where only arrays is provided
  304. arrays = list(arrays)
  305. arrays.append(out)
  306. return arrays
  307. @array_function_dispatch(_stack_dispatcher)
  308. def stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind"):
  309. """
  310. Join a sequence of arrays along a new axis.
  311. The ``axis`` parameter specifies the index of the new axis in the
  312. dimensions of the result. For example, if ``axis=0`` it will be the first
  313. dimension and if ``axis=-1`` it will be the last dimension.
  314. .. versionadded:: 1.10.0
  315. Parameters
  316. ----------
  317. arrays : sequence of array_like
  318. Each array must have the same shape.
  319. axis : int, optional
  320. The axis in the result array along which the input arrays are stacked.
  321. out : ndarray, optional
  322. If provided, the destination to place the result. The shape must be
  323. correct, matching that of what stack would have returned if no
  324. out argument were specified.
  325. dtype : str or dtype
  326. If provided, the destination array will have this dtype. Cannot be
  327. provided together with `out`.
  328. .. versionadded:: 1.24
  329. casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
  330. Controls what kind of data casting may occur. Defaults to 'same_kind'.
  331. .. versionadded:: 1.24
  332. Returns
  333. -------
  334. stacked : ndarray
  335. The stacked array has one more dimension than the input arrays.
  336. See Also
  337. --------
  338. concatenate : Join a sequence of arrays along an existing axis.
  339. block : Assemble an nd-array from nested lists of blocks.
  340. split : Split array into a list of multiple sub-arrays of equal size.
  341. Examples
  342. --------
  343. >>> arrays = [np.random.randn(3, 4) for _ in range(10)]
  344. >>> np.stack(arrays, axis=0).shape
  345. (10, 3, 4)
  346. >>> np.stack(arrays, axis=1).shape
  347. (3, 10, 4)
  348. >>> np.stack(arrays, axis=2).shape
  349. (3, 4, 10)
  350. >>> a = np.array([1, 2, 3])
  351. >>> b = np.array([4, 5, 6])
  352. >>> np.stack((a, b))
  353. array([[1, 2, 3],
  354. [4, 5, 6]])
  355. >>> np.stack((a, b), axis=-1)
  356. array([[1, 4],
  357. [2, 5],
  358. [3, 6]])
  359. """
  360. if not overrides.ARRAY_FUNCTION_ENABLED:
  361. # raise warning if necessary
  362. _arrays_for_stack_dispatcher(arrays, stacklevel=2)
  363. arrays = [asanyarray(arr) for arr in arrays]
  364. if not arrays:
  365. raise ValueError('need at least one array to stack')
  366. shapes = {arr.shape for arr in arrays}
  367. if len(shapes) != 1:
  368. raise ValueError('all input arrays must have the same shape')
  369. result_ndim = arrays[0].ndim + 1
  370. axis = normalize_axis_index(axis, result_ndim)
  371. sl = (slice(None),) * axis + (_nx.newaxis,)
  372. expanded_arrays = [arr[sl] for arr in arrays]
  373. return _nx.concatenate(expanded_arrays, axis=axis, out=out,
  374. dtype=dtype, casting=casting)
  375. # Internal functions to eliminate the overhead of repeated dispatch in one of
  376. # the two possible paths inside np.block.
  377. # Use getattr to protect against __array_function__ being disabled.
  378. _size = getattr(_from_nx.size, '__wrapped__', _from_nx.size)
  379. _ndim = getattr(_from_nx.ndim, '__wrapped__', _from_nx.ndim)
  380. _concatenate = getattr(_from_nx.concatenate,
  381. '__wrapped__', _from_nx.concatenate)
  382. def _block_format_index(index):
  383. """
  384. Convert a list of indices ``[0, 1, 2]`` into ``"arrays[0][1][2]"``.
  385. """
  386. idx_str = ''.join('[{}]'.format(i) for i in index if i is not None)
  387. return 'arrays' + idx_str
  388. def _block_check_depths_match(arrays, parent_index=[]):
  389. """
  390. Recursive function checking that the depths of nested lists in `arrays`
  391. all match. Mismatch raises a ValueError as described in the block
  392. docstring below.
  393. The entire index (rather than just the depth) needs to be calculated
  394. for each innermost list, in case an error needs to be raised, so that
  395. the index of the offending list can be printed as part of the error.
  396. Parameters
  397. ----------
  398. arrays : nested list of arrays
  399. The arrays to check
  400. parent_index : list of int
  401. The full index of `arrays` within the nested lists passed to
  402. `_block_check_depths_match` at the top of the recursion.
  403. Returns
  404. -------
  405. first_index : list of int
  406. The full index of an element from the bottom of the nesting in
  407. `arrays`. If any element at the bottom is an empty list, this will
  408. refer to it, and the last index along the empty axis will be None.
  409. max_arr_ndim : int
  410. The maximum of the ndims of the arrays nested in `arrays`.
  411. final_size: int
  412. The number of elements in the final array. This is used the motivate
  413. the choice of algorithm used using benchmarking wisdom.
  414. """
  415. if type(arrays) is tuple:
  416. # not strictly necessary, but saves us from:
  417. # - more than one way to do things - no point treating tuples like
  418. # lists
  419. # - horribly confusing behaviour that results when tuples are
  420. # treated like ndarray
  421. raise TypeError(
  422. '{} is a tuple. '
  423. 'Only lists can be used to arrange blocks, and np.block does '
  424. 'not allow implicit conversion from tuple to ndarray.'.format(
  425. _block_format_index(parent_index)
  426. )
  427. )
  428. elif type(arrays) is list and len(arrays) > 0:
  429. idxs_ndims = (_block_check_depths_match(arr, parent_index + [i])
  430. for i, arr in enumerate(arrays))
  431. first_index, max_arr_ndim, final_size = next(idxs_ndims)
  432. for index, ndim, size in idxs_ndims:
  433. final_size += size
  434. if ndim > max_arr_ndim:
  435. max_arr_ndim = ndim
  436. if len(index) != len(first_index):
  437. raise ValueError(
  438. "List depths are mismatched. First element was at depth "
  439. "{}, but there is an element at depth {} ({})".format(
  440. len(first_index),
  441. len(index),
  442. _block_format_index(index)
  443. )
  444. )
  445. # propagate our flag that indicates an empty list at the bottom
  446. if index[-1] is None:
  447. first_index = index
  448. return first_index, max_arr_ndim, final_size
  449. elif type(arrays) is list and len(arrays) == 0:
  450. # We've 'bottomed out' on an empty list
  451. return parent_index + [None], 0, 0
  452. else:
  453. # We've 'bottomed out' - arrays is either a scalar or an array
  454. size = _size(arrays)
  455. return parent_index, _ndim(arrays), size
  456. def _atleast_nd(a, ndim):
  457. # Ensures `a` has at least `ndim` dimensions by prepending
  458. # ones to `a.shape` as necessary
  459. return array(a, ndmin=ndim, copy=False, subok=True)
  460. def _accumulate(values):
  461. return list(itertools.accumulate(values))
  462. def _concatenate_shapes(shapes, axis):
  463. """Given array shapes, return the resulting shape and slices prefixes.
  464. These help in nested concatenation.
  465. Returns
  466. -------
  467. shape: tuple of int
  468. This tuple satisfies::
  469. shape, _ = _concatenate_shapes([arr.shape for shape in arrs], axis)
  470. shape == concatenate(arrs, axis).shape
  471. slice_prefixes: tuple of (slice(start, end), )
  472. For a list of arrays being concatenated, this returns the slice
  473. in the larger array at axis that needs to be sliced into.
  474. For example, the following holds::
  475. ret = concatenate([a, b, c], axis)
  476. _, (sl_a, sl_b, sl_c) = concatenate_slices([a, b, c], axis)
  477. ret[(slice(None),) * axis + sl_a] == a
  478. ret[(slice(None),) * axis + sl_b] == b
  479. ret[(slice(None),) * axis + sl_c] == c
  480. These are called slice prefixes since they are used in the recursive
  481. blocking algorithm to compute the left-most slices during the
  482. recursion. Therefore, they must be prepended to rest of the slice
  483. that was computed deeper in the recursion.
  484. These are returned as tuples to ensure that they can quickly be added
  485. to existing slice tuple without creating a new tuple every time.
  486. """
  487. # Cache a result that will be reused.
  488. shape_at_axis = [shape[axis] for shape in shapes]
  489. # Take a shape, any shape
  490. first_shape = shapes[0]
  491. first_shape_pre = first_shape[:axis]
  492. first_shape_post = first_shape[axis+1:]
  493. if any(shape[:axis] != first_shape_pre or
  494. shape[axis+1:] != first_shape_post for shape in shapes):
  495. raise ValueError(
  496. 'Mismatched array shapes in block along axis {}.'.format(axis))
  497. shape = (first_shape_pre + (sum(shape_at_axis),) + first_shape[axis+1:])
  498. offsets_at_axis = _accumulate(shape_at_axis)
  499. slice_prefixes = [(slice(start, end),)
  500. for start, end in zip([0] + offsets_at_axis,
  501. offsets_at_axis)]
  502. return shape, slice_prefixes
  503. def _block_info_recursion(arrays, max_depth, result_ndim, depth=0):
  504. """
  505. Returns the shape of the final array, along with a list
  506. of slices and a list of arrays that can be used for assignment inside the
  507. new array
  508. Parameters
  509. ----------
  510. arrays : nested list of arrays
  511. The arrays to check
  512. max_depth : list of int
  513. The number of nested lists
  514. result_ndim : int
  515. The number of dimensions in thefinal array.
  516. Returns
  517. -------
  518. shape : tuple of int
  519. The shape that the final array will take on.
  520. slices: list of tuple of slices
  521. The slices into the full array required for assignment. These are
  522. required to be prepended with ``(Ellipsis, )`` to obtain to correct
  523. final index.
  524. arrays: list of ndarray
  525. The data to assign to each slice of the full array
  526. """
  527. if depth < max_depth:
  528. shapes, slices, arrays = zip(
  529. *[_block_info_recursion(arr, max_depth, result_ndim, depth+1)
  530. for arr in arrays])
  531. axis = result_ndim - max_depth + depth
  532. shape, slice_prefixes = _concatenate_shapes(shapes, axis)
  533. # Prepend the slice prefix and flatten the slices
  534. slices = [slice_prefix + the_slice
  535. for slice_prefix, inner_slices in zip(slice_prefixes, slices)
  536. for the_slice in inner_slices]
  537. # Flatten the array list
  538. arrays = functools.reduce(operator.add, arrays)
  539. return shape, slices, arrays
  540. else:
  541. # We've 'bottomed out' - arrays is either a scalar or an array
  542. # type(arrays) is not list
  543. # Return the slice and the array inside a list to be consistent with
  544. # the recursive case.
  545. arr = _atleast_nd(arrays, result_ndim)
  546. return arr.shape, [()], [arr]
  547. def _block(arrays, max_depth, result_ndim, depth=0):
  548. """
  549. Internal implementation of block based on repeated concatenation.
  550. `arrays` is the argument passed to
  551. block. `max_depth` is the depth of nested lists within `arrays` and
  552. `result_ndim` is the greatest of the dimensions of the arrays in
  553. `arrays` and the depth of the lists in `arrays` (see block docstring
  554. for details).
  555. """
  556. if depth < max_depth:
  557. arrs = [_block(arr, max_depth, result_ndim, depth+1)
  558. for arr in arrays]
  559. return _concatenate(arrs, axis=-(max_depth-depth))
  560. else:
  561. # We've 'bottomed out' - arrays is either a scalar or an array
  562. # type(arrays) is not list
  563. return _atleast_nd(arrays, result_ndim)
  564. def _block_dispatcher(arrays):
  565. # Use type(...) is list to match the behavior of np.block(), which special
  566. # cases list specifically rather than allowing for generic iterables or
  567. # tuple. Also, we know that list.__array_function__ will never exist.
  568. if type(arrays) is list:
  569. for subarrays in arrays:
  570. yield from _block_dispatcher(subarrays)
  571. else:
  572. yield arrays
  573. @array_function_dispatch(_block_dispatcher)
  574. def block(arrays):
  575. """
  576. Assemble an nd-array from nested lists of blocks.
  577. Blocks in the innermost lists are concatenated (see `concatenate`) along
  578. the last dimension (-1), then these are concatenated along the
  579. second-last dimension (-2), and so on until the outermost list is reached.
  580. Blocks can be of any dimension, but will not be broadcasted using the normal
  581. rules. Instead, leading axes of size 1 are inserted, to make ``block.ndim``
  582. the same for all blocks. This is primarily useful for working with scalars,
  583. and means that code like ``np.block([v, 1])`` is valid, where
  584. ``v.ndim == 1``.
  585. When the nested list is two levels deep, this allows block matrices to be
  586. constructed from their components.
  587. .. versionadded:: 1.13.0
  588. Parameters
  589. ----------
  590. arrays : nested list of array_like or scalars (but not tuples)
  591. If passed a single ndarray or scalar (a nested list of depth 0), this
  592. is returned unmodified (and not copied).
  593. Elements shapes must match along the appropriate axes (without
  594. broadcasting), but leading 1s will be prepended to the shape as
  595. necessary to make the dimensions match.
  596. Returns
  597. -------
  598. block_array : ndarray
  599. The array assembled from the given blocks.
  600. The dimensionality of the output is equal to the greatest of:
  601. * the dimensionality of all the inputs
  602. * the depth to which the input list is nested
  603. Raises
  604. ------
  605. ValueError
  606. * If list depths are mismatched - for instance, ``[[a, b], c]`` is
  607. illegal, and should be spelt ``[[a, b], [c]]``
  608. * If lists are empty - for instance, ``[[a, b], []]``
  609. See Also
  610. --------
  611. concatenate : Join a sequence of arrays along an existing axis.
  612. stack : Join a sequence of arrays along a new axis.
  613. vstack : Stack arrays in sequence vertically (row wise).
  614. hstack : Stack arrays in sequence horizontally (column wise).
  615. dstack : Stack arrays in sequence depth wise (along third axis).
  616. column_stack : Stack 1-D arrays as columns into a 2-D array.
  617. vsplit : Split an array into multiple sub-arrays vertically (row-wise).
  618. Notes
  619. -----
  620. When called with only scalars, ``np.block`` is equivalent to an ndarray
  621. call. So ``np.block([[1, 2], [3, 4]])`` is equivalent to
  622. ``np.array([[1, 2], [3, 4]])``.
  623. This function does not enforce that the blocks lie on a fixed grid.
  624. ``np.block([[a, b], [c, d]])`` is not restricted to arrays of the form::
  625. AAAbb
  626. AAAbb
  627. cccDD
  628. But is also allowed to produce, for some ``a, b, c, d``::
  629. AAAbb
  630. AAAbb
  631. cDDDD
  632. Since concatenation happens along the last axis first, `block` is _not_
  633. capable of producing the following directly::
  634. AAAbb
  635. cccbb
  636. cccDD
  637. Matlab's "square bracket stacking", ``[A, B, ...; p, q, ...]``, is
  638. equivalent to ``np.block([[A, B, ...], [p, q, ...]])``.
  639. Examples
  640. --------
  641. The most common use of this function is to build a block matrix
  642. >>> A = np.eye(2) * 2
  643. >>> B = np.eye(3) * 3
  644. >>> np.block([
  645. ... [A, np.zeros((2, 3))],
  646. ... [np.ones((3, 2)), B ]
  647. ... ])
  648. array([[2., 0., 0., 0., 0.],
  649. [0., 2., 0., 0., 0.],
  650. [1., 1., 3., 0., 0.],
  651. [1., 1., 0., 3., 0.],
  652. [1., 1., 0., 0., 3.]])
  653. With a list of depth 1, `block` can be used as `hstack`
  654. >>> np.block([1, 2, 3]) # hstack([1, 2, 3])
  655. array([1, 2, 3])
  656. >>> a = np.array([1, 2, 3])
  657. >>> b = np.array([4, 5, 6])
  658. >>> np.block([a, b, 10]) # hstack([a, b, 10])
  659. array([ 1, 2, 3, 4, 5, 6, 10])
  660. >>> A = np.ones((2, 2), int)
  661. >>> B = 2 * A
  662. >>> np.block([A, B]) # hstack([A, B])
  663. array([[1, 1, 2, 2],
  664. [1, 1, 2, 2]])
  665. With a list of depth 2, `block` can be used in place of `vstack`:
  666. >>> a = np.array([1, 2, 3])
  667. >>> b = np.array([4, 5, 6])
  668. >>> np.block([[a], [b]]) # vstack([a, b])
  669. array([[1, 2, 3],
  670. [4, 5, 6]])
  671. >>> A = np.ones((2, 2), int)
  672. >>> B = 2 * A
  673. >>> np.block([[A], [B]]) # vstack([A, B])
  674. array([[1, 1],
  675. [1, 1],
  676. [2, 2],
  677. [2, 2]])
  678. It can also be used in places of `atleast_1d` and `atleast_2d`
  679. >>> a = np.array(0)
  680. >>> b = np.array([1])
  681. >>> np.block([a]) # atleast_1d(a)
  682. array([0])
  683. >>> np.block([b]) # atleast_1d(b)
  684. array([1])
  685. >>> np.block([[a]]) # atleast_2d(a)
  686. array([[0]])
  687. >>> np.block([[b]]) # atleast_2d(b)
  688. array([[1]])
  689. """
  690. arrays, list_ndim, result_ndim, final_size = _block_setup(arrays)
  691. # It was found through benchmarking that making an array of final size
  692. # around 256x256 was faster by straight concatenation on a
  693. # i7-7700HQ processor and dual channel ram 2400MHz.
  694. # It didn't seem to matter heavily on the dtype used.
  695. #
  696. # A 2D array using repeated concatenation requires 2 copies of the array.
  697. #
  698. # The fastest algorithm will depend on the ratio of CPU power to memory
  699. # speed.
  700. # One can monitor the results of the benchmark
  701. # https://pv.github.io/numpy-bench/#bench_shape_base.Block2D.time_block2d
  702. # to tune this parameter until a C version of the `_block_info_recursion`
  703. # algorithm is implemented which would likely be faster than the python
  704. # version.
  705. if list_ndim * final_size > (2 * 512 * 512):
  706. return _block_slicing(arrays, list_ndim, result_ndim)
  707. else:
  708. return _block_concatenate(arrays, list_ndim, result_ndim)
  709. # These helper functions are mostly used for testing.
  710. # They allow us to write tests that directly call `_block_slicing`
  711. # or `_block_concatenate` without blocking large arrays to force the wisdom
  712. # to trigger the desired path.
  713. def _block_setup(arrays):
  714. """
  715. Returns
  716. (`arrays`, list_ndim, result_ndim, final_size)
  717. """
  718. bottom_index, arr_ndim, final_size = _block_check_depths_match(arrays)
  719. list_ndim = len(bottom_index)
  720. if bottom_index and bottom_index[-1] is None:
  721. raise ValueError(
  722. 'List at {} cannot be empty'.format(
  723. _block_format_index(bottom_index)
  724. )
  725. )
  726. result_ndim = max(arr_ndim, list_ndim)
  727. return arrays, list_ndim, result_ndim, final_size
  728. def _block_slicing(arrays, list_ndim, result_ndim):
  729. shape, slices, arrays = _block_info_recursion(
  730. arrays, list_ndim, result_ndim)
  731. dtype = _nx.result_type(*[arr.dtype for arr in arrays])
  732. # Test preferring F only in the case that all input arrays are F
  733. F_order = all(arr.flags['F_CONTIGUOUS'] for arr in arrays)
  734. C_order = all(arr.flags['C_CONTIGUOUS'] for arr in arrays)
  735. order = 'F' if F_order and not C_order else 'C'
  736. result = _nx.empty(shape=shape, dtype=dtype, order=order)
  737. # Note: In a c implementation, the function
  738. # PyArray_CreateMultiSortedStridePerm could be used for more advanced
  739. # guessing of the desired order.
  740. for the_slice, arr in zip(slices, arrays):
  741. result[(Ellipsis,) + the_slice] = arr
  742. return result
  743. def _block_concatenate(arrays, list_ndim, result_ndim):
  744. result = _block(arrays, list_ndim, result_ndim)
  745. if list_ndim == 0:
  746. # Catch an edge case where _block returns a view because
  747. # `arrays` is a single numpy array and not a list of numpy arrays.
  748. # This might copy scalars or lists twice, but this isn't a likely
  749. # usecase for those interested in performance
  750. result = result.copy()
  751. return result