ó 2ÄÈ[c@`s=ddlmZmZmZddlZddlZddljjZ ddlm Z m Z m Z m Z mZmZmZddlmZmZddlmZddljZddlmZddlmZmZdd lmZd d d d ddddddddddgZd„Zde fd„ƒYZ!e!de"ƒZ#e!de$ƒZ%de#_'de%_'de fd„ƒYZ(de(fd„ƒYZ)e)ƒZ*d e(fd!„ƒYZ+e+ƒZ,de fd"„ƒYZ-de fd#„ƒYZ.d$e fd%„ƒYZ/e/d&e$ƒZ0e/d&e"ƒZ1e"d'„Z2d(d)„Z3d*„Z4dS(+i(tdivisiontabsolute_importtprint_functionN(tasarrayt ScalarTypetarraytalltruetcumprodtarangetndim(tfind_common_typet issubdtypei(t function_base(tdiff(travel_multi_indext unravel_index(t as_stridedRRtmgridtogridtr_tc_ts_t index_exptix_t ndenumeratetndindext fill_diagonalt diag_indicestdiag_indices_fromcG`sèg}t|ƒ}xÉt|ƒD]»\}}t|ƒ}|jdkrUtdƒ‚n|jdkry|jtjƒ}nt |j tj ƒr |j ƒ\}n|j d||jfd||dƒ}|j|ƒqWt|ƒS(s5 Construct an open mesh from multiple sequences. This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions. Using `ix_` one can quickly construct index arrays that will index the cross product. ``a[np.ix_([1,3],[2,5])]`` returns the array ``[[a[1,2] a[1,5]], [a[3,2] a[3,5]]]``. Parameters ---------- args : 1-D sequences Each sequence should be of integer or boolean type. Boolean sequences will be interpreted as boolean masks for the corresponding dimension (equivalent to passing in ``np.nonzero(boolean_sequence)``). Returns ------- out : tuple of ndarrays N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh. See Also -------- ogrid, mgrid, meshgrid Examples -------- >>> a = np.arange(10).reshape(2, 5) >>> a array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) >>> ixgrid = np.ix_([0, 1], [2, 4]) >>> ixgrid (array([[0], [1]]), array([[2, 4]])) >>> ixgrid[0].shape, ixgrid[1].shape ((2, 1), (1, 2)) >>> a[ixgrid] array([[2, 4], [7, 9]]) >>> ixgrid = np.ix_([True, True], [2, 4]) >>> a[ixgrid] array([[2, 4], [7, 9]]) >>> ixgrid = np.ix_([True, True], [False, False, True, False, True]) >>> a[ixgrid] array([[2, 4], [7, 9]]) is!Cross index must be 1 dimensionali(i(i(tlent enumerateRR t ValueErrortsizetastypet_nxtintpR tdtypetbool_tnonzerotreshapetappendttuple(targstouttndtktnew((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRs9  -tnd_gridcB`s,eZdZed„Zd„Zd„ZRS(s Construct a multi-dimensional "meshgrid". ``grid = nd_grid()`` creates an instance which will return a mesh-grid when indexed. The dimension and number of the output arrays are equal to the number of indexing dimensions. If the step length is not a complex number, then the stop is not inclusive. However, if the step length is a **complex number** (e.g. 5j), then the integer part of its magnitude is interpreted as specifying the number of points to create between the start and stop values, where the stop value **is inclusive**. If instantiated with an argument of ``sparse=True``, the mesh-grid is open (or not fleshed out) so that only one-dimension of each returned argument is greater than 1. Parameters ---------- sparse : bool, optional Whether the grid is sparse or not. Default is False. Notes ----- Two instances of `nd_grid` are made available in the NumPy namespace, `mgrid` and `ogrid`:: mgrid = nd_grid(sparse=False) ogrid = nd_grid(sparse=True) Users should use these pre-defined instances instead of using `nd_grid` directly. Examples -------- >>> mgrid = np.lib.index_tricks.nd_grid() >>> mgrid[0:5,0:5] array([[[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]], [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]]) >>> mgrid[-1:1:5j] array([-1. , -0.5, 0. , 0.5, 1. ]) >>> ogrid = np.lib.index_tricks.nd_grid(sparse=True) >>> ogrid[0:5,0:5] [array([[0], [1], [2], [3], [4]]), array([[0, 1, 2, 3, 4]])] cC`s ||_dS(N(tsparse(tselfR0((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyt__init__Ÿsc C`s‘y´g}t}xûtt|ƒƒD]ç}||j}||j}|dkrWd}n|dkrld}nt|tƒr|jtt |ƒƒƒt }n/|jtt j ||j ||dƒƒƒt|t ƒst|t ƒst||j t ƒr"t }q"q"W|jr]gt||ft|ƒƒD]!\}}tj|d|ƒ^q3} ntj||ƒ} xÆtt|ƒƒD]²}||j}||j}|dkr·d}n|dkrÌd}nt|tƒrtt |ƒƒ}|dkr||j |t |dƒ}qn| |||| |}tddƒ| |<| |t| ƒ| |tlength((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyt __getitem__¢sp       )  D       %          cC`sdS(Ni((R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyt__len__ßs(t__name__t __module__t__doc__tFalseR2RLRM(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR/bs;  =R0tAxisConcatenatorcB`sSeZdZeejƒZeejƒZde ddd„Z d„Z d„Z RS(sv Translates slice objects to concatenation along an axis. For detailed documentation on usage, see `r_`. iiiÿÿÿÿcC`s(||_||_||_||_dS(N(taxistmatrixttrans1dtndmin(R1RSRTRVRU((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR2ñs   c C`s¡t|tƒr=tjƒj}tj||j|jƒ}|St|t ƒsX|f}n|j }|j }|j }|j }g}g} g} g} xft|ƒD]X\} } t}t| tƒr£| j}| j}| j}|dkròd}n|dkrd}nt|tƒrCtt|ƒƒ}tj||d|ƒ}ntj|||ƒ}|dkrÀt|dtd|ƒ}|dkr |jd|ƒ}q qÀnt| tƒrÀ| dkrÍtdƒ‚n| dkrñt}| d k}q¡nd | kr„| j d ƒ}yUg|d D]}t|ƒ^q\}}t!|ƒd kr]t|d ƒ}nw¡Wq„t"k r€td ƒ‚q„Xnyt| ƒ}w¡WqÀtt#fk r¼td ƒ‚qÀXnt$| ƒt%krt| d|ƒ}| j&t!|ƒƒt}| j&|j'ƒn°t(| ƒ}t| dtdtd|ƒ}|dkrÀ||krÀ||}|}|dkr||d7}nt)t*|ƒƒ}|| |||||!}|j+|ƒ}n|j&|ƒ| r¡t|tj,ƒr¡| j&|j'ƒq¡q¡Wt-| | ƒ}|dk rCx(| D]} || j.|ƒ|| R7R9R3R:R tlinspaceR"RRtswapaxesRtTruetsplitRt ExceptionRDttypeRR(R$R tlistR4t transposetndarrayR R!t concatenatetmakemattT(R1REtframetmymatRURVRTRStobjstscalarst arraytypest scalartypesR-titemtscalarR5R6R>R tnewobjtcoltvectxt item_ndimtk2tk1tdefaxestaxest final_dtypetrestoldndim((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRL÷s¤                  )         cC`sdS(Ni((R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRMXs( RNRORPt staticmethodR"RnRaRTRoRQR2RLRM(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRRçs  atRClasscB`seZdZd„ZRS(sí Translates slice objects to concatenation along the first axis. This is a simple way to build up arrays quickly. There are two use cases. 1. If the index expression contains comma separated arrays, then stack them along their first axis. 2. If the index expression contains slice notation or scalars then create a 1-D array with a range indicated by the slice notation. If slice notation is used, the syntax ``start:stop:step`` is equivalent to ``np.arange(start, stop, step)`` inside of the brackets. However, if ``step`` is an imaginary number (i.e. 100j) then its integer portion is interpreted as a number-of-points desired and the start and stop are inclusive. In other words ``start:stop:stepj`` is interpreted as ``np.linspace(start, stop, step, endpoint=1)`` inside of the brackets. After expansion of slice notation, all comma separated sequences are concatenated together. Optional character strings placed as the first element of the index expression can be used to change the output. The strings 'r' or 'c' result in matrix output. If the result is 1-D and 'r' is specified a 1 x N (row) matrix is produced. If the result is 1-D and 'c' is specified, then a N x 1 (column) matrix is produced. If the result is 2-D then both provide the same matrix result. A string integer specifies which axis to stack multiple comma separated arrays along. A string of two comma-separated integers allows indication of the minimum number of dimensions to force each entry into as the second integer (the axis to concatenate along is still the first integer). A string with three comma-separated integers allows specification of the axis to concatenate along, the minimum number of dimensions to force the entries to, and which axis should contain the start of the arrays which are less than the specified number of dimensions. In other words the third integer allows you to specify where the 1's should be placed in the shape of the arrays that have their shapes upgraded. By default, they are placed in the front of the shape tuple. The third argument allows you to specify where the start of the array should be instead. Thus, a third argument of '0' would place the 1's at the end of the array shape. Negative integers specify where in the new shape tuple the last dimension of upgraded arrays should be placed, so the default is '-1'. Parameters ---------- Not a function, so takes no parameters Returns ------- A concatenated ndarray or matrix. See Also -------- concatenate : Join a sequence of arrays along an existing axis. c_ : Translates slice objects to concatenation along the second axis. Examples -------- >>> np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])] array([1, 2, 3, 0, 0, 4, 5, 6]) >>> np.r_[-1:1:6j, [0]*3, 5, 6] array([-1. , -0.6, -0.2, 0.2, 0.6, 1. , 0. , 0. , 0. , 5. , 6. ]) String integers specify the axis to concatenate along or the minimum number of dimensions to force entries into. >>> a = np.array([[0, 1, 2], [3, 4, 5]]) >>> np.r_['-1', a, a] # concatenate along last axis array([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5]]) >>> np.r_['0,2', [1,2,3], [4,5,6]] # concatenate along first axis, dim>=2 array([[1, 2, 3], [4, 5, 6]]) >>> np.r_['0,2,0', [1,2,3], [4,5,6]] array([[1], [2], [3], [4], [5], [6]]) >>> np.r_['1,2,0', [1,2,3], [4,5,6]] array([[1, 4], [2, 5], [3, 6]]) Using 'r' or 'c' as a first string argument creates a matrix. >>> np.r_['r',[1,2,3], [4,5,6]] matrix([[1, 2, 3, 4, 5, 6]]) cC`stj|dƒdS(Ni(RRR2(R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR2¾s(RNRORPR2(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR†_s]tCClasscB`seZdZd„ZRS(sï Translates slice objects to concatenation along the second axis. This is short-hand for ``np.r_['-1,2,0', index expression]``, which is useful because of its common occurrence. In particular, arrays will be stacked along their last axis after being upgraded to at least 2-D with 1's post-pended to the shape (column vectors made out of 1-D arrays). See Also -------- column_stack : Stack 1-D arrays as columns into a 2-D array. r_ : For more detailed documentation. Examples -------- >>> np.c_[np.array([1,2,3]), np.array([4,5,6])] array([[1, 4], [2, 5], [3, 6]]) >>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])] array([[1, 2, 3, 0, 0, 4, 5, 6]]) cC`s tj|dddddƒdS(NiÿÿÿÿRViRUi(RRR2(R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR2Üs(RNRORPR2(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR‡ÃscB`s/eZdZd„Zd„Zd„ZeZRS(s¥ Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values. Parameters ---------- arr : ndarray Input array. See Also -------- ndindex, flatiter Examples -------- >>> a = np.array([[1, 2], [3, 4]]) >>> for index, x in np.ndenumerate(a): ... print(index, x) (0, 0) 1 (0, 1) 2 (1, 0) 3 (1, 1) 4 cC`st|ƒj|_dS(N(Rtflattiter(R1tarr((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR2üscC`s|jjt|jƒfS(s Standard iterator method, returns the index tuple and array value. Returns ------- coords : tuple of ints The indices of the current iteration. val : scalar The array element of the current iteration. (R‰tcoordstnext(R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyt__next__ÿs cC`s|S(N((R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyt__iter__ s(RNRORPR2RRŽRŒ(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRás    cB`s8eZdZd„Zd„Zd„Zd„ZeZRS(sU An N-dimensional iterator object to index arrays. Given the shape of an array, an `ndindex` instance iterates over the N-dimensional index of the array. At each iteration a tuple of indices is returned, the last dimension is iterated over first. Parameters ---------- `*args` : ints The size of each dimension of the array. See Also -------- ndenumerate, flatiter Examples -------- >>> for index in np.ndindex(3, 2, 1): ... print(index) (0, 0, 0) (0, 1, 0) (1, 0, 0) (1, 1, 0) (2, 0, 0) (2, 1, 0) cG`s„t|ƒdkr2t|dtƒr2|d}nttjdƒd|dtj|ƒƒ}tj|dddgdd ƒ|_dS( Niitshapetstridestflagst multi_indext zerosize_oktordertC( RR8R)RR"tzerost zeros_liketnditert_it(R1RR|((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR21s % cC`s|S(N((R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRŽ9scC`st|ƒdS(s„ Increment the multi-dimensional index by one. This method is for backward compatibility only: do not use. N(RŒ(R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pytndincr<scC`st|jƒ|jjS(sù Standard iterator method, updates the index and returns the index tuple. Returns ------- val : tuple of ints Returns a tuple containing the indices of the current iteration. (RŒR™R’(R1((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRDs (RNRORPR2RŽRšRRŒ(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRs     tIndexExpressioncB`s eZdZd„Zd„ZRS(sô A nicer way to build up index tuples for arrays. .. note:: Use one of the two predefined instances `index_exp` or `s_` rather than directly using `IndexExpression`. For any index combination, including slicing and axis insertion, ``a[indices]`` is the same as ``a[np.index_exp[indices]]`` for any array `a`. However, ``np.index_exp[indices]`` can be used anywhere in Python code and returns a tuple of slice objects that can be used in the construction of complex index expressions. Parameters ---------- maketuple : bool If True, always returns a tuple. See Also -------- index_exp : Predefined instance that always returns a tuple: `index_exp = IndexExpression(maketuple=True)`. s_ : Predefined instance without tuple conversion: `s_ = IndexExpression(maketuple=False)`. Notes ----- You can do all this with `slice()` plus a few special objects, but there's a lot to remember and this version is simpler because it uses the standard array indexing syntax. Examples -------- >>> np.s_[2::2] slice(2, None, 2) >>> np.index_exp[2::2] (slice(2, None, 2),) >>> np.array([0, 1, 2, 3, 4])[np.s_[2::2]] array([2, 4]) cC`s ||_dS(N(t maketuple(R1Rœ((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR2scC`s(|jr t|tƒ r |fS|SdS(N(RœR8R)(R1Rw((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRLs(RNRORPR2RL(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyR›as* RœcC`sÉ|jdkrtdƒ‚nd}|jdkrh|jdd}|s¯|jd|jd}q¯nGtt|jƒdkƒs’tdƒ‚ndt|jd ƒjƒ}||jd||…= 2``, the diagonal is the list of locations with indices ``a[i, ..., i]`` all identical. This function modifies the input array in-place, it does not return a value. Parameters ---------- a : array, at least 2-D. Array whose diagonal is to be filled, it gets modified in-place. val : scalar Value to be written on the diagonal, its type must be compatible with that of the array a. wrap : bool For tall matrices in NumPy version up to 1.6.2, the diagonal "wrapped" after N columns. You can have this behavior with this option. This affects only tall matrices. See also -------- diag_indices, diag_indices_from Notes ----- .. versionadded:: 1.4.0 This functionality can be obtained via `diag_indices`, but internally this version uses a much faster implementation that never constructs the indices and uses simple slicing. Examples -------- >>> a = np.zeros((3, 3), int) >>> np.fill_diagonal(a, 5) >>> a array([[5, 0, 0], [0, 5, 0], [0, 0, 5]]) The same function can operate on a 4-D array: >>> a = np.zeros((3, 3, 3, 3), int) >>> np.fill_diagonal(a, 4) We only show a few blocks for clarity: >>> a[0, 0] array([[4, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> a[1, 1] array([[0, 0, 0], [0, 4, 0], [0, 0, 0]]) >>> a[2, 2] array([[0, 0, 0], [0, 0, 0], [0, 0, 4]]) The wrap option affects only tall matrices: >>> # tall matrices no wrap >>> a = np.zeros((5, 3),int) >>> fill_diagonal(a, 4) >>> a array([[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [0, 0, 0]]) >>> # tall matrices wrap >>> a = np.zeros((5, 3),int) >>> fill_diagonal(a, 4, wrap=True) >>> a array([[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [4, 0, 0]]) >>> # wide matrices >>> a = np.zeros((3, 5),int) >>> fill_diagonal(a, 4, wrap=True) >>> a array([[4, 0, 0, 0, 0], [0, 4, 0, 0, 0], [0, 0, 4, 0, 0]]) isarray must be at least 2-diis/All dimensions of input must be of equal lengthiÿÿÿÿN( R RR7RRR RtsumRˆ(tatvaltwraptendR5((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRŸs]icC`st|ƒ}|f|S(s( Return the indices to access the main diagonal of an array. This returns a tuple of indices that can be used to access the main diagonal of an array `a` with ``a.ndim >= 2`` dimensions and shape (n, n, ..., n). For ``a.ndim = 2`` this is the usual diagonal, for ``a.ndim > 2`` this is the set of indices to access ``a[i, i, ..., i]`` for ``i = [0..n-1]``. Parameters ---------- n : int The size, along each dimension, of the arrays for which the returned indices can be used. ndim : int, optional The number of dimensions. See also -------- diag_indices_from Notes ----- .. versionadded:: 1.4.0 Examples -------- Create a set of indices to access the diagonal of a (4, 4) array: >>> di = np.diag_indices(4) >>> di (array([0, 1, 2, 3]), array([0, 1, 2, 3])) >>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) >>> a[di] = 100 >>> a array([[100, 1, 2, 3], [ 4, 100, 6, 7], [ 8, 9, 100, 11], [ 12, 13, 14, 100]]) Now, we create indices to manipulate a 3-D array: >>> d3 = np.diag_indices(2, 3) >>> d3 (array([0, 1]), array([0, 1]), array([0, 1])) And use it to set the diagonal of an array of zeros to 1: >>> a = np.zeros((2, 2, 2), dtype=int) >>> a[d3] = 1 >>> a array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]]) (R(tnR tidx((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRs@ cC`s_|jdkstdƒ‚ntt|jƒdkƒsHtdƒ‚nt|jd|jƒS(s Return the indices to access the main diagonal of an n-dimensional array. See `diag_indices` for full details. Parameters ---------- arr : array, at least 2-D See Also -------- diag_indices Notes ----- .. versionadded:: 1.4.0 is input array must be at least 2-dis/All dimensions of input must be of equal length(R RRR RR(RŠ((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyRUs (5t __future__RRRR^R<tnumpy.core.numerictcoretnumericR"RRRRRRR tnumpy.core.numerictypesR R tR tnumpy.matrixlibRaR tnumpy.core.multiarrayRRtnumpy.lib.stride_tricksRt__all__RtobjectR/RQRRgRR7RPRRR†RR‡RRRR›RRRRR(((s5/tmp/pip-build-fiC0ax/numpy/numpy/lib/index_tricks.pyts@  4  H€  xb  2N5 r D