K i\4`dZddlmZddlmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZmZmZmZmZmZmZmZdd lmZdd lm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&dZ'Gdde Z(dZ)dZ*dZ+dZ,ee,ee*fZ-eedee-Z.dZ/dZ0dZ1dZ2dZ3y) z'Implementation of the Kronecker product)reduce)prod)Mulsympify)adjoint) ShapeError) MatrixExpr) transpose)Identity) MatrixBase)canon condition distributedo_oneexhaustflattentypedunpack) bottom_up)sift)MatAdd)MatMul)MatPowcn|s tdt|dk(r|dSt|jS)aT The Kronecker product of two or more arguments. This computes the explicit Kronecker product for subclasses of ``MatrixBase`` i.e. explicit matrices. Otherwise, a symbolic ``KroneckerProduct`` object is returned. Examples ======== For ``MatrixSymbol`` arguments a ``KroneckerProduct`` object is returned. Elements of this matrix can be obtained by indexing, or for MatrixSymbols with known dimension the explicit matrix can be obtained with ``.as_explicit()`` >>> from sympy import kronecker_product, MatrixSymbol >>> A = MatrixSymbol('A', 2, 2) >>> B = MatrixSymbol('B', 2, 2) >>> kronecker_product(A) A >>> kronecker_product(A, B) KroneckerProduct(A, B) >>> kronecker_product(A, B)[0, 1] A[0, 0]*B[0, 1] >>> kronecker_product(A, B).as_explicit() Matrix([ [A[0, 0]*B[0, 0], A[0, 0]*B[0, 1], A[0, 1]*B[0, 0], A[0, 1]*B[0, 1]], [A[0, 0]*B[1, 0], A[0, 0]*B[1, 1], A[0, 1]*B[1, 0], A[0, 1]*B[1, 1]], [A[1, 0]*B[0, 0], A[1, 0]*B[0, 1], A[1, 1]*B[0, 0], A[1, 1]*B[0, 1]], [A[1, 0]*B[1, 0], A[1, 0]*B[1, 1], A[1, 1]*B[1, 0], A[1, 1]*B[1, 1]]]) For explicit matrices the Kronecker product is returned as a Matrix >>> from sympy import Matrix, kronecker_product >>> sigma_x = Matrix([ ... [0, 1], ... [1, 0]]) ... >>> Isigma_y = Matrix([ ... [0, 1], ... [-1, 0]]) ... >>> kronecker_product(sigma_x, Isigma_y) Matrix([ [ 0, 0, 0, 1], [ 0, 0, -1, 0], [ 0, 1, 0, 0], [-1, 0, 0, 0]]) See Also ======== KroneckerProduct z$Empty Kronecker product is undefinedrr) TypeErrorlenKroneckerProductdoit)matricess j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/expressions/kronecker.pykronecker_productr"s>p >?? 8}{*//11ceZdZdZdZddfd ZedZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZxZS)ra The Kronecker product of two or more arguments. The Kronecker product is a non-commutative product of matrices. Given two matrices of dimension (m, n) and (s, t) it produces a matrix of dimension (m s, n t). This is a symbolic object that simply stores its argument without evaluating it. To actually compute the product, use the function ``kronecker_product()`` or call the ``.doit()`` or ``.as_explicit()`` methods. >>> from sympy import KroneckerProduct, MatrixSymbol >>> A = MatrixSymbol('A', 5, 5) >>> B = MatrixSymbol('B', 5, 5) >>> isinstance(KroneckerProduct(A, B), KroneckerProduct) True T)checkc ttt|}td|Dr?t t d|D}td|Dr|j S|S|rt|t|$|g|S)Nc34K|]}|jywN) is_Identity.0as r! z+KroneckerProduct.__new__..ms+q}}+c34K|]}|jywr()rowsr*s r!r-z+KroneckerProduct.__new__..ns515r.c3<K|]}t|tywr( isinstancer r*s r!r-z+KroneckerProduct.__new__..os;:a,;) listmaprallr r as_explicitvalidatesuper__new__)clsr%argsret __class__s r!r;zKroneckerProduct.__new__ksuC&' +d+ +45556C;d;;((  dOws*T**r#c|jdj\}}|jddD] }||jz}||jz}"||fS)Nrr)r=shaper0cols)selfr0rBmats r!rAzKroneckerProduct.shapexsZYYq\'' d99QR= C CHH D CHH D d|r#c d}t|jD]>}t||j\}}t||j\}}||||fz}@|SNr)reversedr=divmodr0rB)rCijkwargsresultrDmns r!_entryzKroneckerProduct._entrys`DII& C!SXX&DAq!SXX&DAq c!Q$i F  r#cptttt|jj Sr()rr5r6rr=rrCs r! _eval_adjointzKroneckerProduct._eval_adjoints&c'499&=!>?DDFFr#ct|jDcgc]}|jc}jScc}wr()rr= conjugater)rCr,s r!_eval_conjugatez KroneckerProduct._eval_conjugates.!CA!++-!CDIIKK!Cs=cptttt|jj Sr()rr5r6r r=rrQs r!_eval_transposez KroneckerProduct._eval_transposes&c)TYY&?!@AFFHHr#cdddlm}t|jDcgc] }|| c}Scc}w)Nr)trace)rYrr=)rCrYr,s r! _eval_tracezKroneckerProduct._eval_traces& tyy1!U1X1221s-cddlm}m}td|jDs||S|j }t |jDcgc]}||||j z zc}Scc}w)Nr)det Determinantc34K|]}|jywr( is_squarer*s r!r-z5KroneckerProduct._eval_determinant..s211;;2r.) determinantr\r]r7r=r0r)rCr\r]rMr,s r!_eval_determinantz"KroneckerProduct._eval_determinantsY12 22t$ $ II;ASVah';<<;s A/c t|jDcgc]}|jc}Scc}w#t$rddlm}||cYSwxYw)Nr)Inverse)rr=inverser"sympy.matrices.expressions.inverserd)rCr,rds r! _eval_inversezKroneckerProduct._eval_inversesF !#499%Eaaiik%EF F%E ! B4=  !s5055AAct|txrx|j|jk(xr]t|jt|jk(xr0t dt |j|jDS)aDetermine whether two matrices have the same Kronecker product structure Examples ======== >>> from sympy import KroneckerProduct, MatrixSymbol, symbols >>> m, n = symbols(r'm, n', integer=True) >>> A = MatrixSymbol('A', m, m) >>> B = MatrixSymbol('B', n, n) >>> C = MatrixSymbol('C', m, m) >>> D = MatrixSymbol('D', n, n) >>> KroneckerProduct(A, B).structurally_equal(KroneckerProduct(C, D)) True >>> KroneckerProduct(A, B).structurally_equal(KroneckerProduct(D, C)) False >>> KroneckerProduct(A, B).structurally_equal(C) False c3TK|] \}}|j|jk("ywr(rAr+r,bs r!r-z6KroneckerProduct.structurally_equal..s!Tv1177*T&()r3rrArr=r7ziprCothers r!structurally_equalz#KroneckerProduct.structurally_equalsn(5"23UJJ%++-U Nc%**o5UTTYY 9STT Vr#ct|txrx|j|jk(xr]t |j t |j k(xr0t dt|j |j DS)aqDetermine whether two matrices have the appropriate structure to bring matrix multiplication inside the KroneckerProdut Examples ======== >>> from sympy import KroneckerProduct, MatrixSymbol, symbols >>> m, n = symbols(r'm, n', integer=True) >>> A = MatrixSymbol('A', m, n) >>> B = MatrixSymbol('B', n, m) >>> KroneckerProduct(A, B).has_matching_shape(KroneckerProduct(B, A)) True >>> KroneckerProduct(A, B).has_matching_shape(KroneckerProduct(A, B)) False >>> KroneckerProduct(A, B).has_matching_shape(A) False c3TK|] \}}|j|jk("ywr()rBr0rks r!r-z6KroneckerProduct.has_matching_shape..s!RVa!&&(Rrm)r3rrBr0rr=r7rnros r!has_matching_shapez#KroneckerProduct.has_matching_shapesn"5"23SII+S Nc%**o5SRs499ejj7QRR Tr#c xttttt tt i|Sr()rr rrrr)rChintss r!_eval_expand_kroneckerproductz.KroneckerProduct._eval_expand_kroneckerproducts/]uU$4jAQSY6Z#[\]^bcddr#c|j|rC|jt|j|jDcgc] \}}||z c}}S||zScc}}wr()rqr?rnr=rCrpr,rls r!_kronecker_addzKroneckerProduct._kronecker_addsS  " "5 )!4>>DIIuzz8R#Sfq!AE#ST T%< $TA c|j|rC|jt|j|jDcgc] \}}||z c}}S||zScc}}wr()rtr?rnr=rys r!_kronecker_mulzKroneckerProduct._kronecker_mulsS  " "5 )!4>>c$))UZZ6P#QFQAaC#QR R%< $Rr{c |jdd}|r*|jDcgc]}|jdi|}}n |j}tt |Scc}w)NdeepT)getr=r canonicalizer)rCrvrargr=s r!rzKroneckerProduct.doitsYyy& 15;#HCHH%u%;D;99D,d344td|Ds tdy)Nc34K|]}|jywr() is_Matrix)r+rs r!r-zvalidate..s-s}}-r.z Mix of Matrix and Scalar symbols)r7r)r=s r!r9r9s! -- -:;; .r#cg}g}|jD]J}|j\}}|j||jt j |Lt |}|dk7r |t |zS|SrF)r=args_cncextendappendr _from_argsr)kronc_partnc_partrcncs r!extract_commutativersw FGyy+ 2 as~~b)*+ &\F {&000 Kr#c td|Dstdt|z|d}t|ddD]}|j}|j }t |D]Y}||||zz}t |dz D]"}|j||||z|zdzz}$|dk(r|}Ij|}[}t|dj} t|| r|S| |S) aCompute the Kronecker product of a sequence of SymPy Matrices. This is the standard Kronecker product of matrices [1]. Parameters ========== matrices : tuple of MatrixBase instances The matrices to take the Kronecker product of. Returns ======= matrix : MatrixBase The Kronecker product matrix. Examples ======== >>> from sympy import Matrix >>> from sympy.matrices.expressions.kronecker import ( ... matrix_kronecker_product) >>> m1 = Matrix([[1,2],[3,4]]) >>> m2 = Matrix([[1,0],[0,1]]) >>> matrix_kronecker_product(m1, m2) Matrix([ [1, 0, 2, 0], [0, 1, 0, 2], [3, 0, 4, 0], [0, 3, 0, 4]]) >>> matrix_kronecker_product(m2, m1) Matrix([ [1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4]]) References ========== .. [1] https://en.wikipedia.org/wiki/Kronecker_product c3<K|]}t|tywr(r2r+rMs r!r-z+matrix_kronecker_product..-s;Qz!Z(;r4z&Sequence of Matrices expected, got: %sNrrc|jSr()_class_priority)Ms r!z*matrix_kronecker_product..Is a.?.?r#)key) r7rreprrGr0rBrangerow_joincol_joinmaxr?r3) r matrix_expansionrDr0rBrIstartrJnext MatrixClasss r!matrix_kronecker_productrsZ ;(; ; 4tH~ E   |" & xxxxt ,A$S4[0E4!8_ $S4!a%88  Av}}U+ , % (h$?@JJK"K0+,,r#cbtd|jDs|St|jS)Nc3<K|]}t|tywr(r2rs r!r-z-explicit_kronecker_product..Rs.cs0QWW0r.r)r3rtupler=exprs r!_kronecker_dims_keyras%$()0dii000r#ct|jt}|jdd}|s|S|j Dcgc]}t d|}}|st |St ||zScc}w)Nrc$|j|Sr()rz)rys r!rz#kronecker_mat_add..ns!1!1!!4r#)rr=rpopvaluesrr)rr=nonkronsgroupkronss r!kronecker_mat_addrhs{  . /Dxxd#H  ++- )4e < )E ) u~u~((  )sA.c:|j\}}d}|t|dz krk|||dz\}}t|tr9t|tr)|j |||<|j |dzn|dz }|t|dz krk|t |zS)Nrr)as_coeff_matricesrr3rr}rr)rfactorr rIABs r!kronecker_mat_mulrws--/FH A c(ma !A#1 a) *z!=M/N**1-HQK LL1  FA c(ma  &(# ##r#c  t|jtrdtd|jjDr>t|jjDcgc]}t ||j c}S|Scc}w)Nc34K|]}|jywr(r_r*s r!r-z$kronecker_mat_pow..s6[qq{{6[r.)r3baserr7r=rexp)rr,s r!kronecker_mat_powrsZ$))-.36[DIINN6[3[tyy~~!N!&DHH"5!NOO "OsBcd}tttt|ttt t ttti}||}t|dd}||S|S)a-Combine KronekeckerProduct with expression. If possible write operations on KroneckerProducts of compatible shapes as a single KroneckerProduct. Examples ======== >>> from sympy.matrices.expressions import combine_kronecker >>> from sympy import MatrixSymbol, KroneckerProduct, symbols >>> m, n = symbols(r'm, n', integer=True) >>> A = MatrixSymbol('A', m, n) >>> B = MatrixSymbol('B', n, m) >>> combine_kronecker(KroneckerProduct(A, B)*KroneckerProduct(B, A)) KroneckerProduct(A*B, B*A) >>> combine_kronecker(KroneckerProduct(A, B)+KroneckerProduct(B.T, A.T)) KroneckerProduct(A + B.T, B + A.T) >>> C = MatrixSymbol('C', n, n) >>> D = MatrixSymbol('D', m, m) >>> combine_kronecker(KroneckerProduct(C, D)**m) KroneckerProduct(C**m, D**m) cPt|txr|jtSr()r3r hasrrs r!haskronz"combine_kronecker..haskrons$ +J9I0JJr#rN) rrrrrrrrrrgetattr)rrrulerLrs r!combine_kroneckerrsu.K ')GU & & & (.)*+ , -D $ZF 664 (D v  r#N)4r functoolsrmathr sympy.corerrsympy.functionsrsympy.matrices.exceptionsr"sympy.matrices.expressions.matexprr $sympy.matrices.expressions.transposer "sympy.matrices.expressions.specialr sympy.matrices.matrixbaser sympy.strategiesr rrrrrrrsympy.strategies.traversersympy.utilitiesrmataddrmatmulrmatpowrr"rr9rrrrulesrrrrrrrr#r!rs-##09:70KKK/ =2@R5zR5j< M-`0  #    y!J!'12  ) $ $r#