σ 2ΔΘ[c@`sΡddlmZmZmZdddddddgZd d lmZd d lmZm Z m Z d d l m Z d „Z d„Zd„Zd„Zd„Zddd„Zgd„Zd„Zd„ZdS(i(tdivisiontabsolute_importtprint_functiont atleast_1dt atleast_2dt atleast_3dtblockthstacktstacktvstacki(tnumeric(tarrayt asanyarraytnewaxis(tnormalize_axis_indexcG`syg}xN|D]F}t|ƒ}|jdkr@|jdƒ}n|}|j|ƒq Wt|ƒdkrq|dS|SdS(s% Convert inputs to arrays with at least one dimension. Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. Parameters ---------- arys1, arys2, ... : array_like One or more input arrays. Returns ------- ret : ndarray An array, or list of arrays, each with ``a.ndim >= 1``. Copies are made only if necessary. See Also -------- atleast_2d, atleast_3d Examples -------- >>> np.atleast_1d(1.0) array([ 1.]) >>> x = np.arange(9.0).reshape(3,3) >>> np.atleast_1d(x) array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) >>> np.atleast_1d(x) is x True >>> np.atleast_1d(1, [3, 4]) [array([1]), array([3, 4])] iiN(R tndimtreshapetappendtlen(tarystrestarytresult((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyR s'  cG`s€g}xy|D]q}t|ƒ}|jdkrC|jddƒ}n.|jdkrk|tdd…f}n|}|j|ƒq Wt|ƒdkrœ|dS|SdS(s` View inputs as arrays with at least two dimensions. Parameters ---------- arys1, arys2, ... : array_like One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have two or more dimensions are preserved. Returns ------- res, res2, ... : ndarray An array, or list of arrays, each with ``a.ndim >= 2``. Copies are avoided where possible, and views with two or more dimensions are returned. See Also -------- atleast_1d, atleast_3d Examples -------- >>> np.atleast_2d(3.0) array([[ 3.]]) >>> x = np.arange(3.0) >>> np.atleast_2d(x) array([[ 0., 1., 2.]]) >>> np.atleast_2d(x).base is x True >>> np.atleast_2d(1, [1, 2], [[1, 2]]) [array([[1]]), array([[1, 2]]), array([[1, 2]])] iiN(R RRR RR(RRRR((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyR?s%  cG`sΫg}x°|D]¨}t|ƒ}|jdkrF|jdddƒ}nb|jdkrq|tdd…tf}n7|jdkr’|dd…dd…tf}n|}|j|ƒq Wt|ƒdkrΣ|dS|SdS(sσ View inputs as arrays with at least three dimensions. Parameters ---------- arys1, arys2, ... : array_like One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have three or more dimensions are preserved. Returns ------- res1, res2, ... : ndarray An array, or list of arrays, each with ``a.ndim >= 3``. Copies are avoided where possible, and views with three or more dimensions are returned. For example, a 1-D array of shape ``(N,)`` becomes a view of shape ``(1, N, 1)``, and a 2-D array of shape ``(M, N)`` becomes a view of shape ``(M, N, 1)``. See Also -------- atleast_1d, atleast_2d Examples -------- >>> np.atleast_3d(3.0) array([[[ 3.]]]) >>> x = np.arange(3.0) >>> np.atleast_3d(x).shape (1, 3, 1) >>> x = np.arange(12.0).reshape(4,3) >>> np.atleast_3d(x).shape (4, 3, 1) >>> np.atleast_3d(x).base is x.base # x is a reshape, so not base itself True >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]): ... print(arr, arr.shape) ... [[[1] [2]]] (1, 2, 1) [[[1] [2]]] (1, 2, 1) [[[1 2]]] (1, 1, 2) iiNi(R RRR RR(RRRR((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyRss1  "cC`s)tjg|D]}t|ƒ^q dƒS(sC Stack arrays in sequence vertically (row wise). This is equivalent to concatenation along the first axis after 1-D arrays of shape `(N,)` have been reshaped to `(1,N)`. Rebuilds arrays divided by `vsplit`. This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions `concatenate`, `stack` and `block` provide more general stacking and concatenation operations. Parameters ---------- tup : sequence of ndarrays The arrays must have the same shape along all but the first axis. 1-D arrays must have the same length. Returns ------- stacked : ndarray The array formed by stacking the given arrays, will be at least 2-D. See Also -------- stack : Join a sequence of arrays along a new axis. hstack : Stack arrays in sequence horizontally (column wise). dstack : Stack arrays in sequence depth wise (along third dimension). concatenate : Join a sequence of arrays along an existing axis. vsplit : Split array into a list of multiple sub-arrays vertically. block : Assemble arrays from blocks. Examples -------- >>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.vstack((a,b)) array([[1, 2, 3], [2, 3, 4]]) >>> a = np.array([[1], [2], [3]]) >>> b = np.array([[2], [3], [4]]) >>> np.vstack((a,b)) array([[1], [2], [3], [2], [3], [4]]) i(t_nxt concatenateR(ttupt_m((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyR Άs4cC`s\g|D]}t|ƒ^q}|rH|djdkrHtj|dƒStj|dƒSdS(sΧ Stack arrays in sequence horizontally (column wise). This is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis. Rebuilds arrays divided by `hsplit`. This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions `concatenate`, `stack` and `block` provide more general stacking and concatenation operations. Parameters ---------- tup : sequence of ndarrays The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length. Returns ------- stacked : ndarray The array formed by stacking the given arrays. See Also -------- stack : Join a sequence of arrays along a new axis. vstack : Stack arrays in sequence vertically (row wise). dstack : Stack arrays in sequence depth wise (along third axis). concatenate : Join a sequence of arrays along an existing axis. hsplit : Split array along second axis. block : Assemble arrays from blocks. Examples -------- >>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.hstack((a,b)) array([1, 2, 3, 2, 3, 4]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[2],[3],[4]]) >>> np.hstack((a,b)) array([[1, 2], [2, 3], [3, 4]]) iiN(RRRR(RRtarrs((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyRμs/cC`sήg|D]}t|ƒ^q}|s4tdƒ‚ntd„|Dƒƒ}t|ƒdkrktdƒ‚n|djd}t||ƒ}tdƒf|tj f}g|D]}||^q―}tj |d|d|ƒS( sϋ Join a sequence of arrays along a new axis. The `axis` parameter specifies the index of the new axis in the dimensions of the result. For example, if ``axis=0`` it will be the first dimension and if ``axis=-1`` it will be the last dimension. .. versionadded:: 1.10.0 Parameters ---------- arrays : sequence of array_like Each array must have the same shape. axis : int, optional The axis in the result array along which the input arrays are stacked. out : ndarray, optional If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified. Returns ------- stacked : ndarray The stacked array has one more dimension than the input arrays. See Also -------- concatenate : Join a sequence of arrays along an existing axis. split : Split array into a list of multiple sub-arrays of equal size. block : Assemble arrays from blocks. Examples -------- >>> arrays = [np.random.randn(3, 4) for _ in range(10)] >>> np.stack(arrays, axis=0).shape (10, 3, 4) >>> np.stack(arrays, axis=1).shape (3, 10, 4) >>> np.stack(arrays, axis=2).shape (3, 4, 10) >>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.stack((a, b)) array([[1, 2, 3], [2, 3, 4]]) >>> np.stack((a, b), axis=-1) array([[1, 2], [2, 3], [3, 4]]) s need at least one array to stackcs`s|]}|jVqdS(N(tshape(t.0tarr((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pys _sis)all input arrays must have the same shapeitaxistoutN( R t ValueErrortsetRRRtslicetNoneRR R(tarraysRR Rtshapest result_ndimtsltexpanded_arrays((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyR#s8c`sUd„}t|ƒtkr9tdj|ˆƒƒƒ‚nt|ƒtkr t|ƒdkr ‡fd†t|ƒDƒ}t|ƒ\}}xq|D]i\}}||kr³|}nt|ƒt|ƒkr’tdjt|ƒt|ƒ||ƒƒƒ‚q’q’W||fSt|ƒtkr>t|ƒdkr>ˆdgdfSˆt j |ƒfSdS(s~ Recursive function checking that the depths of nested lists in `arrays` all match. Mismatch raises a ValueError as described in the block docstring below. The entire index (rather than just the depth) needs to be calculated for each innermost list, in case an error needs to be raised, so that the index of the offending list can be printed as part of the error. The parameter `parent_index` is the full index of `arrays` within the nested lists passed to _block_check_depths_match at the top of the recursion. The return value is a pair. The first item returned is the full index of an element (specifically the first element) from the bottom of the nesting in `arrays`. An empty list at the bottom of the nesting is represented by a `None` index. The second item is the maximum of the ndims of the arrays nested in `arrays`. cS`s!djd„|Dƒƒ}d|S(Ntcs`s*|] }|dk rdj|ƒVqdS(s[{}]N(R$tformat(Rti((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pys €sR%(tjoin(tindextidx_str((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyt format_indexss{} is a tuple. Only lists can be used to arrange blocks, and np.block does not allow implicit conversion from tuple to ndarray.ic3`s+|]!\}}t|ˆ|gƒVqdS(N(t_block_check_depths_match(RR,R(t parent_index(s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pys sscList depths are mismatched. First element was at depth {}, but there is an element at depth {} ({})N( ttypettuplet TypeErrorR+tlistRt enumeratetnextR!R$RR(R%R2R0t idxs_ndimst first_indext max_arr_ndimR.R((R2s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyR1ks, $      $c`s@d„‰d‡‡‡‡fd†‰zˆ|ƒSWdd‰XdS(s6 Internal implementation of block. `arrays` is the argument passed to block. `max_depth` is the depth of nested lists within `arrays` and `result_ndim` is the greatest of the dimensions of the arrays in `arrays` and the depth of the lists in `arrays` (see block docstring for details). cS`st|d|dtdtƒS(Ntndmintcopytsubok(R tFalsetTrue(taR((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyt atleast_nd±sic`s||ˆkrkt|ƒdkr-tdƒ‚ng|D]}ˆ||dƒ^q4}tj|dˆ| ƒSˆ|ˆƒSdS(NisLists cannot be emptyiR(RR!RR(R%tdepthRR(RBtblock_recursiont max_depthR'(s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyRDΆs  &N(R$(R%RER'((RBRDRER's4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyt_block©s   cC`s7t|ƒ\}}t|ƒ}t||t||ƒƒS(s9 Assemble an nd-array from nested lists of blocks. Blocks in the innermost lists are concatenated (see `concatenate`) along the last dimension (-1), then these are concatenated along the second-last dimension (-2), and so on until the outermost list is reached. Blocks can be of any dimension, but will not be broadcasted using the normal rules. Instead, leading axes of size 1 are inserted, to make ``block.ndim`` the same for all blocks. This is primarily useful for working with scalars, and means that code like ``np.block([v, 1])`` is valid, where ``v.ndim == 1``. When the nested list is two levels deep, this allows block matrices to be constructed from their components. .. versionadded:: 1.13.0 Parameters ---------- arrays : nested list of array_like or scalars (but not tuples) If passed a single ndarray or scalar (a nested list of depth 0), this is returned unmodified (and not copied). Elements shapes must match along the appropriate axes (without broadcasting), but leading 1s will be prepended to the shape as necessary to make the dimensions match. Returns ------- block_array : ndarray The array assembled from the given blocks. The dimensionality of the output is equal to the greatest of: * the dimensionality of all the inputs * the depth to which the input list is nested Raises ------ ValueError * If list depths are mismatched - for instance, ``[[a, b], c]`` is illegal, and should be spelt ``[[a, b], [c]]`` * If lists are empty - for instance, ``[[a, b], []]`` See Also -------- concatenate : Join a sequence of arrays together. stack : Stack arrays in sequence along a new dimension. hstack : Stack arrays in sequence horizontally (column wise). vstack : Stack arrays in sequence vertically (row wise). dstack : Stack arrays in sequence depth wise (along third dimension). vsplit : Split array into a list of multiple sub-arrays vertically. Notes ----- When called with only scalars, ``np.block`` is equivalent to an ndarray call. So ``np.block([[1, 2], [3, 4]])`` is equivalent to ``np.array([[1, 2], [3, 4]])``. This function does not enforce that the blocks lie on a fixed grid. ``np.block([[a, b], [c, d]])`` is not restricted to arrays of the form:: AAAbb AAAbb cccDD But is also allowed to produce, for some ``a, b, c, d``:: AAAbb AAAbb cDDDD Since concatenation happens along the last axis first, `block` is _not_ capable of producing the following directly:: AAAbb cccbb cccDD Matlab's "square bracket stacking", ``[A, B, ...; p, q, ...]``, is equivalent to ``np.block([[A, B, ...], [p, q, ...]])``. Examples -------- The most common use of this function is to build a block matrix >>> A = np.eye(2) * 2 >>> B = np.eye(3) * 3 >>> np.block([ ... [A, np.zeros((2, 3))], ... [np.ones((3, 2)), B ] ... ]) array([[ 2., 0., 0., 0., 0.], [ 0., 2., 0., 0., 0.], [ 1., 1., 3., 0., 0.], [ 1., 1., 0., 3., 0.], [ 1., 1., 0., 0., 3.]]) With a list of depth 1, `block` can be used as `hstack` >>> np.block([1, 2, 3]) # hstack([1, 2, 3]) array([1, 2, 3]) >>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.block([a, b, 10]) # hstack([a, b, 10]) array([1, 2, 3, 2, 3, 4, 10]) >>> A = np.ones((2, 2), int) >>> B = 2 * A >>> np.block([A, B]) # hstack([A, B]) array([[1, 1, 2, 2], [1, 1, 2, 2]]) With a list of depth 2, `block` can be used in place of `vstack`: >>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.block([[a], [b]]) # vstack([a, b]) array([[1, 2, 3], [2, 3, 4]]) >>> A = np.ones((2, 2), int) >>> B = 2 * A >>> np.block([[A], [B]]) # vstack([A, B]) array([[1, 1], [1, 1], [2, 2], [2, 2]]) It can also be used in places of `atleast_1d` and `atleast_2d` >>> a = np.array(0) >>> b = np.array([1]) >>> np.block([a]) # atleast_1d(a) array([0]) >>> np.block([b]) # atleast_1d(b) array([1]) >>> np.block([[a]]) # atleast_2d(a) array([[0]]) >>> np.block([[b]]) # atleast_2d(b) array([[1]]) (R1RRFtmax(R%t bottom_indextarr_ndimt list_ndim((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyRΚs” N(t __future__RRRt__all__R*R RR R R t multiarrayRRRRR RR$RR1RFR(((s4/tmp/pip-build-fiC0ax/numpy/numpy/core/shape_base.pyts  4 4 C 6 7H > !