shape_base.py 32 KB

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