K iGddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZmZdd lmZd Zd Zd ZdZdZdZddZGddeZy)N)Iterable) Printable)Tuple)diff)S)_sympify NDimArray)DenseNDimArrayImmutableDenseNDimArraySparseNDimArraycddlm}t|tr|St||tt t fr t|S|S)Nr MatrixBase)sympy.matricesr isinstancer listtuplerr )ars `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/tensor/array/arrayop.py_arrayfyrs7)!Y!j$u56&q)) Hc ddlm}m}t|dk(rtj St|dk(rt |dSddlmddlm }ddlm ddl m tfd|Dr||St|d kDrtt|d|dg|d d Stt |\}}t!|t"rt!|t"s||zSt!||rt!||rt|}|j$j'D cic]3\}}|j$j'D]\} } ||z| z|| z5} } }}} || |j(|j(zSt+|D cgc]} t+|D]} | | z }} } t-||j(|j(zScc} } }}wcc} } w) a Tensor product among scalars or array-like objects. The equivalent operator for array expressions is ``ArrayTensorProduct``, which can be used to keep the expression unevaluated. Examples ======== >>> from sympy.tensor.array import tensorproduct, Array >>> from sympy.abc import x, y, z, t >>> A = Array([[1, 2], [3, 4]]) >>> B = Array([x, y]) >>> tensorproduct(A, B) [[[x, y], [2*x, 2*y]], [[3*x, 3*y], [4*x, 4*y]]] >>> tensorproduct(A, x) [[x, 2*x], [3*x, 4*x]] >>> tensorproduct(A, B, B) [[[[x**2, x*y], [x*y, y**2]], [[2*x**2, 2*x*y], [2*x*y, 2*y**2]]], [[[3*x**2, 3*x*y], [3*x*y, 3*y**2]], [[4*x**2, 4*x*y], [4*x*y, 4*y**2]]]] Applying this function on two matrices will result in a rank 4 array. >>> from sympy import Matrix, eye >>> m = Matrix([[x, y], [z, t]]) >>> p = tensorproduct(eye(3), m) >>> p [[[[x, y], [z, t]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[x, y], [z, t]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[x, y], [z, t]]]] See Also ======== sympy.tensor.array.expressions.array_expressions.ArrayTensorProduct r)rImmutableSparseNDimArray_CodegenArrayAbstract)ArrayTensorProduct _ArrayExpr MatrixSymbolc3<K|]}t|fywN)r).0argr#r!rs r z tensorproduct..Fs! ^RU:cJ(=|L M ^sN)sympy.tensor.arrayrrlenrOner0sympy.tensor.array.expressions.array_expressionsrrr!"sympy.matrices.expressions.matexprr#any tensorproductmaprr _sparse_arrayitemsshapeFlattenr )argsrrrrblpk1v1k2v2 new_arrayij product_listr#r!rs @@@rr0r0sFM 4yA~uu  4yA~Q  VSK? ^Y] ^^!4(( 4y1}]47DG<HtABxHH x DAq a #:a+Cs !_%*Q*H V56__5J5J5Lss62r[\[j[j[p[p[rsQWQSUWRURZB&sZs s' 177QWW3DEE!(@AWQZ@AaC@C@L@ "<1771B CC tAs 8G Gc t|}t}|D]s}t|ts t d|j |d}|D]?}||vr t d||j |k7r t d|j |Au|j}t|j Dcgc] \}}||vs |}}}dg|zd} t|D].}| ||z dz <| t|j ||z dz z} 0t|D cgc]2}||vr,t|j |D cgc] } || z c} 4} }} g} |D]Y}g} t|j |dD]&| jtfd|D(| j| [|| || fScc}}wcc} wcc} }w)Nz1collections of contraction/diagonal axes expectedrz"dimension specified more than oncezBcannot contract or diagonalize between axes of different dimensionrc3.K|] }|zywr%)r&ig cum_shapejss rr(z-_util_contraction_diagonal..sD2IbMB.Ds) rsetrr ValueErrorr4addrank enumeraterangeintappendsum)arraycontraction_or_diagonal_axes taken_dims axes_groupdimdrJr>remaining_shape_cumulr?remaining_indices summed_deltaslidxrErFs @@r_util_contraction_diagonalr[Zs UOEJ2  *h/PQ Qkk*Q-( AJ !EFFekk!n$ !eff NN1     ::F>#G G G G cddlm}ddlm}ddlm}ddlm}t ||||fr ||g|St|g|\}}}}g} tj|D]j} t| } tj} tj|D]'} |j| t| z}| ||z } )| j| lt|dk(rt| dk(sJ| dSt!|| |S)a Contraction of an array-like object on the specified axes. The equivalent operator for array expressions is ``ArrayContraction``, which can be used to keep the expression unevaluated. Examples ======== >>> from sympy import Array, tensorcontraction >>> from sympy import Matrix, eye >>> tensorcontraction(eye(3), (0, 1)) 3 >>> A = Array(range(18), (3, 2, 3)) >>> A [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]]] >>> tensorcontraction(A, (0, 2)) [21, 30] Matrix multiplication may be emulated with a proper combination of ``tensorcontraction`` and ``tensorproduct`` >>> from sympy import tensorproduct >>> from sympy.abc import a,b,c,d,e,f,g,h >>> m1 = Matrix([[a, b], [c, d]]) >>> m2 = Matrix([[e, f], [g, h]]) >>> p = tensorproduct(m1, m2) >>> p [[[[a*e, a*f], [a*g, a*h]], [[b*e, b*f], [b*g, b*h]]], [[[c*e, c*f], [c*g, c*h]], [[d*e, d*f], [d*g, d*h]]]] >>> tensorcontraction(p, (1, 2)) [[a*e + b*g, a*f + b*h], [c*e + d*g, c*f + d*h]] >>> m1*m2 Matrix([ [a*e + b*g, a*f + b*h], [c*e + d*g, c*f + d*h]]) See Also ======== sympy.tensor.array.expressions.array_expressions.ArrayContraction r)_array_contractionrr r"r)r-r]rr!r.r#rr[ itertoolsproductrOrZero_get_tuple_indexrNr+type)rPcontraction_axesr]rr!r#rXrVrYcontracted_arrayicontribindex_base_positionisum sum_to_indexidxs rtensorcontractionrjsVTVK?%*&;\JK!%;*:;;?YZ_?sbr?s>> from sympy import Array, tensordiagonal >>> from sympy import Matrix, eye >>> tensordiagonal(eye(3), (0, 1)) [1, 1, 1] >>> from sympy.abc import a,b,c,d >>> m1 = Matrix([[a, b], [c, d]]) >>> tensordiagonal(m1, [0, 1]) [a, d] In case of higher dimensional arrays, the diagonalized out dimensions are appended removed and appended as a single dimension at the end: >>> A = Array(range(18), (3, 2, 3)) >>> A [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]]] >>> tensordiagonal(A, (0, 2)) [[0, 7, 14], [3, 10, 17]] >>> from sympy import permutedims >>> tensordiagonal(A, (0, 2)) == permutedims(Array([A[0, :, 0], A[1, :, 1], A[2, :, 2]]), [1, 0]) True See Also ======== sympy.tensor.array.expressions.array_expressions.ArrayDiagonal c38K|]}t|dkyw)rN)r+)r&r>s rr(z!tensordiagonal..s .13q6Q; .sz%need at least two axes to diagonalizerr r) ArrayDiagonal_array_diagonalr")r/rHr-r!rrmrnr.r#r _validater[r+r^r_rOrarNrbreshape)rP diagonal_axesr!rrmrnr#rXrVdiagonal_deltasdiagonalized_arrayr>diagonal_shapererfrgrhris rtensordiagonalrusX\ . ..@AAKV_?%*&;\JKu5}55ME2M2A[\aArdqAr>E o&56c!f6N6%%'89(!(m%--? $L(()c ddlm}ddlm}t|t f}t ||r)t|}|D]}|jrtdt ||r,t |t r|j}n t|}t ||rt ||rnt|}tt|D cic]?\}}|j|jj!D]\}} |||zz| A} }}}} n@t|D cgc]*}t|D cgc]} | j|c} ,} }} t#|| |j$|j$zS|j|St'|}t ||r>> from sympy import derive_by_array >>> from sympy.abc import x, y, z, t >>> from sympy import cos >>> derive_by_array(cos(x*t), x) -t*sin(t*x) >>> derive_by_array(cos(x*t), [x, y, z, t]) [-t*sin(t*x), 0, 0, -x*sin(t*x)] >>> derive_by_array([x, y**2*z], [[x, y], [z, t]]) [[[1, 0], [0, 2*y*z]], [[0, y**2], [0, 0]]] rrr zcannot derive by this array)rrr*rrr rr _diff_wrtrH as_immutabler+rKr5rr2r3rbr4r) exprdxrr array_typesr>r8xkvr=ys rderive_by_arrayr*s8*2Z3K"k" $R ( @A;; !>?? @$ $ dI &$$&D*40D b+ &$0Y)272;)?MM!%A)-1)C)C)I)I)KM!%A2Xq[MXM MJQQSUA?AaffQi?U U4:iDJJ)>? ?99R= ~ b+ &*'"++NQDIIaL+NPRPXPXY Y"Bb> !M@U,Os%AG2 G?.G: G?1H:G?c ddlm}ddlm}ddlm}ddlm}ddlm}ddlm } ddlm } | j|||| |}t||||fr |||St|ts t|}dd lm} t|| s| t#|}|j$|j'k7r t)d |} ||j*} t||r\t-||j.j1Dcic]&\}}t3||j5||(c}}| S||j*Dcgc] }t7|c}}d gt9|z}t;t=j>|D]\}}| |}||||<t-||| Scc}}wcc}w) a? Permutes the indices of an array. Parameter specifies the permutation of the indices. The equivalent operator for array expressions is ``PermuteDims``, which can be used to keep the expression unevaluated. Examples ======== >>> from sympy.abc import x, y, z, t >>> from sympy import sin >>> from sympy import Array, permutedims >>> a = Array([[x, y, z], [t, sin(x), 0]]) >>> a [[x, y, z], [t, sin(x), 0]] >>> permutedims(a, (1, 0)) [[x, t], [y, sin(x)], [z, 0]] If the array is of second order, ``transpose`` can be used: >>> from sympy import transpose >>> transpose(a) [[x, t], [y, sin(x)], [z, 0]] Examples on higher dimensions: >>> b = Array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) >>> permutedims(b, (2, 1, 0)) [[[1, 5], [3, 7]], [[2, 6], [4, 8]]] >>> permutedims(b, (1, 2, 0)) [[[1, 5], [2, 6]], [[3, 7], [4, 8]]] An alternative way to specify the same permutations as in the previous lines involves passing the *old* and *new* indices, either as a list or as a string: >>> permutedims(b, index_order_old="cba", index_order_new="abc") [[[1, 5], [3, 7]], [[2, 6], [4, 8]]] >>> permutedims(b, index_order_old="cab", index_order_new="abc") [[[1, 5], [2, 6]], [[3, 7], [4, 8]]] ``Permutation`` objects are also allowed: >>> from sympy.combinatorics import Permutation >>> permutedims(b, Permutation([1, 2, 0])) [[[1, 5], [2, 6]], [[3, 7], [4, 8]]] See Also ======== sympy.tensor.array.expressions.array_expressions.PermuteDims rr r r) _permute_dimsr") PermuteDims)get_rank) Permutationzwrong permutation sizeN) r*rr-r!rrr.r#sympy.tensor.array.expressionsrr_get_permutation_from_argumentsrr r sympy.combinatoricsrrsizerJrHr4rbr2r3rrarLr+rKr^r_)rypermindex_order_oldindex_order_newrr!rrr#rrriperm new_shaper}r~r> indices_spanr=rits r permutedimsrjsp3KVN?:I  6 6t_o_ghl_m nD$%:LIJT4(( dI &&t,/ dK (4:& yyDIIK122EETZZ I$(tDz'+'9'9'?'?'AC#q!!d&;&;A&>!?@!CCDMO O4::6aq67Ls4y II--|<=3 #JAw !  4:i ++C7s +F= Gc.eZdZdZdZdZdZdZdZy)r5aO Flatten an iterable object to a list in a lazy-evaluation way. Notes ===== This class is an iterator with which the memory cost can be economised. Optimisation has been considered to ameliorate the performance for some specific data types like DenseNDimArray and SparseNDimArray. Examples ======== >>> from sympy.tensor.array.arrayop import Flatten >>> from sympy.tensor.array import Array >>> A = Array(range(6)).reshape(2, 3) >>> Flatten(A) Flatten([[0, 1, 2], [3, 4, 5]]) >>> [i for i in Flatten(A)] [0, 1, 2, 3, 4, 5] cddlm}ddlm}t |t |fs t dt |tr||}||_d|_ y)Nrrr zData type not yet supported) sympy.matrices.matrixbaserr*r rrNotImplementedErrorr_iter_idx)selfiterablerr s r__init__zFlatten.__init__sE80(Xz$:;%&CD D h % *H  rc|Sr%rCrs r__iter__zFlatten.__iter__s rcddlm}t|j|jkDrt |jt r$|jj|j}nt |jtrI|j|jjvr$|jj|j}nd}n|t |j|r|j|j}nLt|jdrt|j}n |j|j}nt|xjdz c_|S)Nrr__next__r) rrr+rrrr _arrayrr2hasattrnext StopIteration)rrresults rrzFlatten.__next__s8 tzz?TYY &$**n5**4995DJJ899 8 88!ZZ55dii@FFDJJ 3DII.Z0djj)DII.  Q  rc"|jSr%)rrs rrz Flatten.next s}}rcpt|jdz|j|jzdzS)N())rb__name___printr)rprinters r _sympystrzFlatten._sympystrs.Dz""S(7>>$**+EEKKrN) r __module__ __qualname____doc__rrrrrrCrrr5r5s!* 8Lrr5)NNN)r^collections.abcrsympy.core._print_helpersrsympy.core.containersrsympy.core.functionrsympy.core.singletonrsympy.core.sympifyrsympy.tensor.array.ndim_arrayr #sympy.tensor.array.dense_ndim_arrayr r $sympy.tensor.array.sparse_ndim_arrayrrr0r[rjrurrr5rCrrrsb$/'$"'3W@ >DB-D`J:ZPMf="@],@FLiFLr