
    \h\4                     `   S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJrJrJrJrJrJr  SSKJr  SSKJ r   SSK!J"r"  SSK#J$r$  SSK%J&r&  S r' " S S\5      r(S r)S r*S r+S r,\\,\\*4r-\" \" S \" \-6 5      5      r.S r/S r0S r1S r2S r3g) z'Implementation of the Kronecker product    )reduce)prod)Mulsympify)adjoint)
ShapeError)
MatrixExpr)	transpose)Identity)
MatrixBase)canon	condition
distributedo_oneexhaustflattentypedunpack)	bottom_up)sift   )MatAdd)MatMul)MatPowc                  z    U (       d  [        S5      e[        U 5      S:X  a  U S   $ [        U 6 R                  5       $ )a  
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 undefinedr   r   )	TypeErrorlenKroneckerProductdoit)matricess    \/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/expressions/kronecker.pykronecker_productr"      s>    p >??
8}{*//11    c                      ^  \ rS rSrSrSrSS.U 4S jjr\S 5       rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rSrU =r$ )r   V   aZ  
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                ,  > [        [        [        U5      5      n[        S U 5       5      (       aD  [	        [        S U 5       5      5      n[        S U 5       5      (       a  UR                  5       $ U$ U(       a  [        U6   [        TU ]$  " U /UQ76 $ )Nc              3   8   #    U  H  oR                   v   M     g 7fN)is_Identity.0as     r!   	<genexpr>+KroneckerProduct.__new__.<locals>.<genexpr>m   s     +d}}d   c              3   8   #    U  H  oR                   v   M     g 7fr)   )rowsr+   s     r!   r.   r/   n   s     51r0   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   
isinstancer   r+   s     r!   r.   r/   o   s     ;d:a,,d   )
listmapr   allr   r   as_explicitvalidatesuper__new__)clsr&   argsret	__class__s       r!   r=   KroneckerProduct.__new__k   s{    C&'+d+++45556C;d;;;((
dOws*T**r#   c                     U R                   S   R                  u  pU R                   SS   H  nXR                  -  nX#R                  -  nM!     X4$ )Nr   r   )r?   shaper2   cols)selfr2   rE   mats       r!   rD   KroneckerProduct.shapex   sM    YYq\''
99QR=CHHDHHD ! |r#   c                     Sn[        U R                  5       H9  n[        XR                  5      u  p[        X%R                  5      u  p'XEXg4   -  nM;     U$ Nr   )reversedr?   divmodr2   rE   )rF   ijkwargsresultrG   mns           r!   _entryKroneckerProduct._entry   sO    DII&C!XX&DA!XX&DA!$iF ' r#   c                 p    [        [        [        [        U R                  5      5      6 R                  5       $ r)   )r   r7   r8   r   r?   r   rF   s    r!   _eval_adjointKroneckerProduct._eval_adjoint   s&    c'499&=!>?DDFFr#   c                     [        U R                   Vs/ s H  oR                  5       PM     sn6 R                  5       $ s  snf r)   )r   r?   	conjugater   )rF   r-   s     r!   _eval_conjugate KroneckerProduct._eval_conjugate   s0    !CA++-!CDIIKK!Cs   ?c                 p    [        [        [        [        U R                  5      5      6 R                  5       $ r)   )r   r7   r8   r
   r?   r   rV   s    r!   _eval_transpose KroneckerProduct._eval_transpose   s&    c)TYY&?!@AFFHHr#   c                 h    SSK J n  [        U R                   Vs/ s H
  o!" U5      PM     sn6 $ s  snf )Nr   )trace)ra   r   r?   )rF   ra   r-   s      r!   _eval_traceKroneckerProduct._eval_trace   s*     tyy1y!U1Xy1221s   /c                     SSK JnJn  [        S U R                   5       5      (       d  U" U 5      $ U R
                  n[        U R                   Vs/ s H  oA" U5      X4R
                  -  -  PM     sn6 $ s  snf )Nr   )detDeterminantc              3   8   #    U  H  oR                   v   M     g 7fr)   	is_squarer+   s     r!   r.   5KroneckerProduct._eval_determinant.<locals>.<genexpr>   s     2	1;;	r0   )determinantre   rf   r9   r?   r2   r   )rF   re   rf   rQ   r-   s        r!   _eval_determinant"KroneckerProduct._eval_determinant   s^    12		222t$$II;ASVah';<<;s    A5c                      [        U R                   Vs/ s H  oR                  5       PM     sn6 $ s  snf ! [         a    SSKJn  U" U 5      s $ f = f)Nr   )Inverse)r   r?   inverser   "sympy.matrices.expressions.inversero   )rF   r-   ro   s      r!   _eval_inverseKroneckerProduct._eval_inverse   sH    	!#499%E9aiik9%EFF%E 	!B4= 	!s   7 27 7 AAc                 4   [        U[        5      =(       a    U R                  UR                  :H  =(       ab    [        U R                  5      [        UR                  5      :H  =(       a0    [        S [        U R                  UR                  5       5       5      $ )a  Determine 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
c              3   X   #    U  H   u  pUR                   UR                   :H  v   M"     g 7fr)   rD   r,   r-   bs      r!   r.   6KroneckerProduct.structurally_equal.<locals>.<genexpr>   s!     T9Sv177*9S   (*)r5   r   rD   r   r?   r9   ziprF   others     r!   structurally_equal#KroneckerProduct.structurally_equal   sn    ( 5"23 UJJ%++-U		Nc%**o5U TTYY

9STT	Vr#   c                 4   [        U[        5      =(       a    U R                  UR                  :H  =(       ab    [	        U R
                  5      [	        UR
                  5      :H  =(       a0    [        S [        U R
                  UR
                  5       5       5      $ )a  Determine 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
c              3   X   #    U  H   u  pUR                   UR                  :H  v   M"     g 7fr)   )rE   r2   rw   s      r!   r.   6KroneckerProduct.has_matching_shape.<locals>.<genexpr>   s!     R7QVa!&&(7Qrz   )r5   r   rE   r2   r   r?   r9   r{   r|   s     r!   has_matching_shape#KroneckerProduct.has_matching_shape   sn    " 5"23 SII+S		Nc%**o5S Rs499ejj7QRR	Tr#   c                 x    [        [        [        [        [	        [        [
        5      05      5      " U 5      5      $ r)   )r   r   r   r   r   r   )rF   hintss     r!   _eval_expand_kroneckerproduct.KroneckerProduct._eval_expand_kroneckerproduct   s,    uU$4jAQSY6Z#[\]^bcddr#   c                     U R                  U5      (       aD  U R                  " [        U R                  UR                  5       VVs/ s H	  u  p#X#-   PM     snn6 $ X-   $ s  snnf r)   )r~   rA   r{   r?   rF   r}   r-   rx   s       r!   _kronecker_addKroneckerProduct._kronecker_add   sT    ""5))>>DIIuzz8R#S8RfqAE8R#STT< $T   A
c                     U R                  U5      (       aD  U R                  " [        U R                  UR                  5       VVs/ s H	  u  p#X#-  PM     snn6 $ X-  $ s  snnf r)   )r   rA   r{   r?   r   s       r!   _kronecker_mulKroneckerProduct._kronecker_mul   sT    ""5))>>c$))UZZ6P#Q6PFQAC6P#QRR< $Rr   c                     UR                  SS5      nU(       a,  U R                   Vs/ s H  o3R                  " S0 UD6PM     nnOU R                  n[        [	        U6 5      $ s  snf )NdeepT )getr?   r   canonicalizer   )rF   r   r   argr?   s        r!   r   KroneckerProduct.doit   sX    yy&15;#HH%u%D;D99D,d344 <s   A#r   )__name__
__module____qualname____firstlineno____doc__is_KroneckerProductr=   propertyrD   rS   rW   r[   r^   rb   rl   rr   r~   r   r   r   r   r   __static_attributes____classcell__)rA   s   @r!   r   r   V   s}    $ "& + +  GLI3=!V2T,e  5 5r#   r   c                  H    [        S U  5       5      (       d  [        S5      eg )Nc              3   8   #    U  H  oR                   v   M     g 7fr)   )	is_Matrix)r,   r   s     r!   r.   validate.<locals>.<genexpr>   s     -}}r0   z Mix of Matrix and Scalar symbols)r9   r   )r?   s    r!   r;   r;      s$    ----:;; .r#   c                     / n/ nU R                    HK  nUR                  5       u  pEUR                  U5        UR                  [        R
                  " U5      5        MM     [	        U6 nUS:w  a  U[        U6 -  $ U $ rJ   )r?   args_cncextendappendr   
_from_argsr   )kronc_partnc_partr   cncs         r!   extract_commutativer      sp    FGyyas~~b)* 
 &\F{&000Kr#   c            	         [        S U  5       5      (       d  [        S[        U 5      -  5      eU S   n[        U SS 5       H  nUR                  nUR
                  n[        U5       HZ  nXXT-     -  n[        US-
  5       H!  nUR                  XXT-  U-   S-      -  5      nM#     US:X  a  UnMI  WR                  U5      nM\     WnM     [        U S S9R                  n	[        X5      (       a  U$ U	" U5      $ )	a  Compute 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
c              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   r4   r,   rQ   s     r!   r.   +matrix_kronecker_product.<locals>.<genexpr>-  s     ;(Qz!Z(((r6   z&Sequence of Matrices expected, got: %sNr   r   c                     U R                   $ r)   )_class_priority)Ms    r!   <lambda>*matrix_kronecker_product.<locals>.<lambda>I  s
    a.?.?r#   )key)r9   r   reprrK   r2   rE   rangerow_joincol_joinmaxrA   r5   )
r    matrix_expansionrG   r2   rE   rM   startrN   nextMatrixClasss
             r!   matrix_kronecker_productr      s   Z ;(;;;4tH~E
 	

  |"&xxxx tA$[0E4!8_$!a%88 % Av}}U+   % '( h$?@JJK"00+,,r#   c                 l    [        S U R                   5       5      (       d  U $ [        U R                  6 $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   r4   r   s     r!   r.   -explicit_kronecker_product.<locals>.<genexpr>R  s     <)Qz!Z(()r6   )r9   r?   r   )r   s    r!   explicit_kronecker_productr   P  s+    <$))<<<#TYY//r#   c                 "    [        U [        5      $ r)   )r5   r   )xs    r!   r   r   ]  s    :a9I+Jr#   c                 f    [        U [        5      (       a  [        S U R                   5       5      $ g)Nc              3   8   #    U  H  oR                   v   M     g 7fr)   rv   r+   s     r!   r.   &_kronecker_dims_key.<locals>.<genexpr>c  s     0iWWir0   r   )r5   r   tupler?   exprs    r!   _kronecker_dims_keyr   a  s(    $())0dii000r#   c                     [        U R                  [        5      nUR                  SS 5      nU(       d  U $ UR	                  5        Vs/ s H  n[        S U5      PM     nnU(       d  [        U6 $ [        U6 U-   $ s  snf )Nr   c                 $    U R                  U5      $ r)   )r   )r   ys     r!   r   #kronecker_mat_add.<locals>.<lambda>n  s    !1!1!!4r#   )r   r?   r   popvaluesr   r   )r   r?   nonkronsgroupkronss        r!   kronecker_mat_addr   h  s~    		./Dxxd#H ++-)' 4e<' 
 ) u~u~(()s   A;c                 L   U R                  5       u  pSnU[        U5      S-
  :  at  X#US-    u  pE[        U[        5      (       a=  [        U[        5      (       a(  UR	                  U5      X#'   UR                  US-   5        OUS-  nU[        U5      S-
  :  a  Mt  U[        U6 -  $ )Nr   r      )as_coeff_matricesr   r5   r   r   r   r   )r   factorr    rM   ABs         r!   kronecker_mat_mulr   w  s    --/F	A
c(ma
!A#a)**z!=M/N/N**1-HKLL1FA c(ma
 &(###r#   c           	      $   [        U R                  [        5      (       ak  [        S U R                  R                   5       5      (       a@  [        U R                  R                   Vs/ s H  n[        XR                  5      PM     sn6 $ U $ s  snf )Nc              3   8   #    U  H  oR                   v   M     g 7fr)   rh   r+   s     r!   r.   $kronecker_mat_pow.<locals>.<genexpr>  s     6[Nq{{Nr0   )r5   baser   r9   r?   r   exp)r   r-   s     r!   kronecker_mat_powr     sb    $))-..36[DIINN6[3[3[tyy~~!N~!&HH"5~!NOO "Os   (Bc                     S n[        [        [        [        U[        [        [
        [        [        [        [        05      5      5      5      5      nU" U 5      n[        USS5      nUb  U" 5       $ U$ )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)
c                 Z    [        U [        5      =(       a    U R                  [        5      $ r)   )r5   r	   hasr   r   s    r!   haskron"combine_kronecker.<locals>.haskron  s    $
+J9I0JJr#   r   N)r   r   r   r   r   r   r   r   r   r   getattr)r   r   rulerP   r   s        r!   combine_kroneckerr     su    .K ')GU&&&(.) * + 	,-D
 $ZF664(Dvr#   N)4r   	functoolsr   mathr   
sympy.corer   r   sympy.functionsr   sympy.matrices.exceptionsr   "sympy.matrices.expressions.matexprr	   $sympy.matrices.expressions.transposer
   "sympy.matrices.expressions.specialr   sympy.matrices.matrixbaser   sympy.strategiesr   r   r   r   r   r   r   r   sympy.strategies.traverser   sympy.utilitiesr   mataddr   matmulr   matpowr   r"   r   r;   r   r   r   rulesr   r   r   r   r   r   r   r#   r!   <module>r     s    -   # # 0 9 : 7 0K K K /     =2@R5z R5j<M-`0 
	#			
 y!J!'1 2)$ $r#   