bgrzPddlZddlZddlZddlZddlZddlmZddlm cm Z ddlm Z m Z ddlmZddlmZddlmZddlmZmZdd lmZmZdd lmZejejd Zgd ZdZeedZ GddZ!Gdde!Z"e"Z#Gdde!Z$e$Z%GddZ&Gdde&Z'e'Z(Gdde&Z)e)Z*ed GddZ+ed GddZ,Gd d!Z-e-d"#Z.e-d$#Z/d*d%Z0ee0d+d&Z1ed d,d'Z2d(Z3ee3d)Z4dS)-N) set_module) ScalarTypearray) issubdtype)diff)ravel_multi_index unravel_index) overrideslinspace) as_stridednumpy)module)r r mgridogridr_c_s_ index_expix_ ndenumeratendindex fill_diagonal diag_indicesdiag_indices_fromc|SN)argss M/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/lib/index_tricks.py_ix__dispatcherr"s Kc>g}t|}t|D]\}}t|tjs>t j|}|jdkr|tj }|j dkrtdt|j tjr|\}|d|z|jfzd||z dz zz}||t%|S)a5 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]]) rrz!Cross index must be 1 dimensional)r)len enumerate isinstance_nxndarraynpasarraysizeastypeintpndim ValueErrorrdtypebool_nonzeroreshapeappendtuple)r outndknews r!rr"st C TBD//  3#s{++ +*S//Cx1}}jj** 8q==@AA A ci + + !;;==DCkk$q&CH;.r!tAv>?? 3 ::r#c eZdZdZddZdZdS)nd_grida 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`, approximately defined as:: mgrid = nd_grid(sparse=False) ogrid = nd_grid(sparse=True) Users should use these pre-defined instances instead of using `nd_grid` directly. Fc||_dSrsparse)selfr?s r!__init__znd_grid.__init__s  r#c  g}dg}tt|D]}||j}||j}||j}|d}|d}t |t jtfr2t|}| t|n=| ttj ||z |dzz ||||gz }t j|}|jr,dt!||ft|zD} nt j||} t%|D]\}} | j}| j}|d}|d}t |t jtfr?tt|}|dkr| j|z t'|dz z }| ||z|z| |<|jr|t jgt|z} tt|D]B}t+dd| |<| |t-| | |<t j| |<C| S#t.t0f$r|j}|j}|j}|d}t |t jtfryt|} t| x}} |dkr|j|z t'|dz z }t j||| }t jd| d||z|zcYSt j|||cYSwxYw)Nrrg?c@g|]\}}tj||S)r1)r(arange).0_x_ts r! z'nd_grid.__getitem__..sAAAA "bj2...AAAr#rD)ranger%stepstartstopr'r(complexfloatingcomplexabsr5intmathceil result_typer?zipindicesr&floatnewaxisslicer6 IndexError TypeErrorrE)r@keyr,num_listr9rKrLrMtypnnkkslobj step_floatlengths r! __getitem__znd_grid.__getitem__s`< 5DsH3s88__ 0 01v{A 1v{=E<DdS%8'$BCCEt99DKKD ****KKDIte|S&ABBCCEEEUD$///8,C{ ,AA$'sfSYY.>$?$?AAA[s++"3 + +2w=E<DdS%8'$BCCCs4yy>>Dqyy "%5??BAtE)1{ + c$ii/s4yy))++A$T400E!HqE%,,/BqE"{E!HHII& 5 5 58D8DIE}$!4g >?? 5 YY #J/v199HUNE$q&MM9DoeT:>>z!VQc:::4?%GGGGz%t44444 5sI!I$$CM(MMNF__name__ __module__ __qualname____doc__rArdrr#r!r<r<msB  D=5=5=5=5=5r#r<c"eZdZdZfdZxZS) MGridClassaM An instance which returns a dense multi-dimensional "meshgrid". An instance which returns a dense (or fleshed out) mesh-grid when indexed, so that each returned argument has the same shape. The dimensions 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**. Returns ------- mesh-grid `ndarrays` all of the same dimensions See Also -------- ogrid : like `mgrid` but returns open (not fleshed out) mesh grids meshgrid: return coordinate matrices from coordinate vectors r_ : array concatenator :ref:`how-to-partition` Examples -------- >>> np.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]]]) >>> np.mgrid[-1:1:5j] array([-1. , -0.5, 0. , 0.5, 1. ]) cLtddS)NFr>superrAr@ __class__s r!rAzMGridClass.__init__s$ &&&&&r#rgrhrirjrA __classcell__rqs@r!rlrlsC))V'''''''''r#rlc"eZdZdZfdZxZS) OGridClassa An instance which returns an open multi-dimensional "meshgrid". An instance which returns an open (i.e. not fleshed out) mesh-grid when indexed, so that only one dimension of each returned array is greater than 1. 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**. Returns ------- mesh-grid `ndarrays` with only one dimension not equal to 1 See Also -------- mgrid : like `ogrid` but returns dense (or fleshed out) mesh grids meshgrid: return coordinate matrices from coordinate vectors r_ : array concatenator :ref:`how-to-partition` Examples -------- >>> from numpy import ogrid >>> ogrid[-1:1:5j] array([-1. , -0.5, 0. , 0.5, 1. ]) >>> ogrid[0:5,0:5] [array([[0], [1], [2], [3], [4]]), array([[0, 1, 2, 3, 4]])] cLtddS)NTr>rnrps r!rAzOGridClass.__init__/s$ %%%%%r#rrrts@r!rvrvsC&&P&&&&&&&&&r#rvcfeZdZdZeejZeejZ d dZ dZ dZ d S) AxisConcatenatorzv Translates slice objects to concatenation along an axis. For detailed documentation on usage, see `r_`. rFrc>||_||_||_||_dSr)axismatrixtrans1dndmin)r@r|r}rr~s r!rAzAxisConcatenator.__init__@s"    r#ct|tr:tjj}t j||j|j}|St|ts|f}|j }|j |j }|j }g}g}t|D]\} } d} t| tr| j} | j} | j}| d} | d} t| t&jt*fr/t-t/| }t1| ||}nt'j| || }dkr.t5|d}|dkr|d|}nt| tr| dkrt9d| dvr d }| d k}d | vr| d } d |dd D\}t=|dkrt-|d }i#t>$r(}t9d | |d}~wwxYw t-| }#t8tBf$r}t9d|d}~wwxYwtE| tFvrd } | }ntIj%| }t5| dd }|dkrl|krf|z }|}|dkr||dzz }tMtO}|d|||dz|||z}|(|}|)|| r|)| |)|j*t=|dkrt'j+|fd|D}|,t||}|r+|j%}|-|}|dkr |r|j.}|S)NFrr)num)copyrrzz+special directives must be the first entry.)rcTr,c,g|]}t|Sr)rQ)rFxs r!rIz0AxisConcatenator.__getitem__..xs&?&?&?!s1vv&?&?&?r#rzunknown special directive {!r}zunknown special directive)rsubokrc 8g|]}t|ddS)FT)rrrr1)r)rFobj final_dtypers r!rIz0AxisConcatenator.__getitem__..sIKKK>A#E %[:::KKKr#)r|)/r'strsys _getframef_back matrixlibbmat f_globalsf_localsr6r~rr}r|r&rYrKrLrMr(rNrOrQrPr rErswapaxesr0splitr% Exceptionformatr[typerr*r/listrJ transposer5r1rT concatenatemakematT)r@r\framemymatr~r}r|objsresult_type_objsr9itemscalarrKrLrMr,newobjcolvece item_ndimk2k1defaxesaxesresoldndimrrs @@r!rdzAxisConcatenator.__getitem__Fsf c3   MOO*EN3HHEL#u%% &C, y ~~> 6> 6GAtF$&&6 4y y=E<DdS%8'$BCC;s4yy>>D%eTt<<>> np.r_[np.array([1,2,3]), 0, 0, np.array([4,5,6])] array([1, 2, 3, ..., 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]]) c<t|ddSrryrArs r!rAzRClass.__init__s!!$*****r#NrgrhrirjrArr#r!rrs0\\|+++++r#rceZdZdZdZdS)CClassa 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, ..., 4, 5, 6]]) cBt|ddddS)Nrzrr)rr~rrs r!rAzCClass.__init__2s%!!$!Q!?????r#Nrrr#r!rrs20@@@@@r#rc$eZdZdZdZdZdZdS)ra 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 cBtj|j|_dSr)r*r+flatiter)r@arrs r!rAzndenumerate.__init__UsJsOO( r#cB|jjt|jfS)a 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. )rcoordsnextrs r!__next__zndenumerate.__next__Xsydi00r#c|Srrrs r!__iter__zndenumerate.__iter__f r#N)rgrhrirjrArrrr#r!rr9sK4))) 1 1 1r#rc*eZdZdZdZdZdZdZdS)ra 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 ---------- shape : ints, or a single tuple of ints The size of each dimension of the array can be passed as individual parameters or as the elements of a tuple. See Also -------- ndenumerate, flatiter Examples -------- Dimensions as individual arguments >>> 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) Same dimensions - but in a tuple ``(3, 2, 1)`` >>> 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) ct|dkr#t|dtr|d}tt jd|t j|}t j|ddgd|_dS)Nrr)shapestrides multi_index zerosize_okC)flagsorder) r%r'r6rr(zeros zeros_likenditer_it)r@rrs r!rAzndindex.__init__s u::??z%(E::?!HE sy||5"~e44 6 6 6:a }'E$')))r#c|Srrrs r!rzndindex.__iter__rr#c\tjdtdt|dS)a5 Increment the multi-dimensional index by one. This method is for backward compatibility only: do not use. .. deprecated:: 1.20.0 This method has been advised against since numpy 1.8.0, but only started emitting DeprecationWarning as of this version. z=`ndindex.ndincr()` is deprecated, use `next(ndindex)` insteadr) stacklevelN)warningswarnDeprecationWarningrrs r!ndincrzndindex.ndincrs6  K 1 . . . . T r#cBt|j|jjS)z 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. )rrrrs r!rzndindex.__next__s TXx##r#N)rgrhrirjrArrrrr#r!rrjs[))V))) $ $ $ $ $r#rceZdZdZdZdZdS)IndexExpressiona 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]) c||_dSr maketuple)r@rs r!rAzIndexExpression.__init__s "r#cD|jrt|ts|fS|Sr)rr'r6)r@rs r!rdzIndexExpression.__getitem__s) > *T5"9"9 7NKr#Nrfrr#r!rrs=))V###r#rTrFc|fSrr)avalwraps r!_fill_diagonal_dispatcherr s 4Kr#c|jdkrtdd}|jdkr.|jddz}|s|jd|jdz}notjt |jdkstddtj|jddz}||jd||<dS)a Fill the main diagonal of the given array of any dimensionality. For an array `a` with ``a.ndim >= 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 or array_like Value(s) to write on the diagonal. If `val` is scalar, the value is written along the diagonal. If array-like, the flattened `val` is written along the diagonal, repeating if necessary to fill all diagonal entries. 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) >>> np.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) >>> np.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) >>> np.fill_diagonal(a, 4, wrap=True) >>> a array([[4, 0, 0, 0, 0], [0, 4, 0, 0, 0], [0, 0, 4, 0, 0]]) The anti-diagonal can be filled by reversing the order of elements using either `numpy.flipud` or `numpy.fliplr`. >>> a = np.zeros((3, 3), int); >>> np.fill_diagonal(np.fliplr(a), [1,2,3]) # Horizontal flip >>> a array([[0, 0, 1], [0, 2, 0], [3, 0, 0]]) >>> np.fill_diagonal(np.flipud(a), [1,2,3]) # Vertical flip >>> a array([[0, 0, 3], [0, 2, 0], [1, 0, 0]]) Note that the order in which the diagonal is filled varies depending on the flip function. rzarray must be at least 2-dNrr/All dimensions of input must be of equal lengthrz) r/r0rr*allr cumprodsumr)rrrendrKs r!rrsb vzz5666 Cv{{wqzA~ *'!*qwqz)Cvd17mmq()) PNOO OBJqwss|,,11333AF9C9r#c6tj|}|f|zS)a( 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*rE)nr/idxs r!rrsB )A,,C 6D=r#c|fSrrrs r!_diag_indices_fromrs 6Mr#c|jdkstdtjt |jdkstdt |jd|jS)aC 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 Examples -------- Create a 4 by 4 array. >>> 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]]) Get the indices of the diagonal elements. >>> di = np.diag_indices_from(a) >>> di (array([0, 1, 2, 3]), array([0, 1, 2, 3])) >>> a[di] array([ 0, 5, 10, 15]) This is simply syntactic sugar for diag_indices. >>> np.diag_indices(a.shape[0]) (array([0, 1, 2, 3]), array([0, 1, 2, 3])) rz input array must be at least 2-drr)r/r0r*rr rrrs r!rrsh^ 8q==;<<< 6$sy//Q& ' 'LJKKK  ! ch / //r#rre)r)5 functoolsrrRrrr*_utilsrnumpy.core.numericcorenumericr(rrnumpy.core.numerictypesrnumpy.matrixlibr function_baser numpy.core.multiarrayr r numpy.corer r numpy.lib.stride_tricksrpartialarray_function_dispatch__all__r"rr<rlrrvrryrrrrrrrrrrrrrrrr#r!rsE  00000000......######BBBBBBBB********......,)+ %g777   ))GG*)GTc5c5c5c5c5c5c5c5L-'-'-'-'-'-'-'-'`  *&*&*&*&*&*&*&*&Z  vvvvvvvvz`+`+`+`+`+ `+`+`+F VXX@@@@@ @@@: VXX G--------` GT$T$T$T$T$T$T$T$D33333333l Od + + + _u%%%233BBB43BJ GAAAAH+,,5050-,505050r#