o
    oh\4                     @   s^  d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZmZmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& dd Z'G dd deZ(dd Z)dd Z*dd Z+dd Z,ee,ee*fZ-eedd ee- Z.d d! Z/d"d# Z0d$d% Z1d&d' Z2d(d) Z3d*S )+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                  G   s,   | st dt| dkr| d S t|   S )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 undefinedr   r   )	TypeErrorlenKroneckerProductdoit)matrices r    x/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/matrices/expressions/kronecker.pykronecker_product   s
   8r"   c                       s   e Zd ZdZdZdd fdd
Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z  ZS )"r   a  
    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                   st   t tt|}tdd |D r*ttdd |D }tdd |D r(| S |S |r0t|  t j	| g|R  S )Nc                 s       | ]}|j V  qd S N)is_Identity.0ar    r    r!   	<genexpr>m       z+KroneckerProduct.__new__.<locals>.<genexpr>c                 s   r$   r%   rowsr'   r    r    r!   r*   n   r+   c                 s       | ]}t |tV  qd S r%   
isinstancer   r'   r    r    r!   r*   o       )
listmapr   allr   r   as_explicitvalidatesuper__new__)clsr#   argsret	__class__r    r!   r8   k   s   zKroneckerProduct.__new__c                 C   s@   | j d j\}}| j dd  D ]}||j9 }||j9 }q||fS )Nr   r   )r:   shaper-   cols)selfr-   r?   matr    r    r!   r>   x   s
   
zKroneckerProduct.shapec                 K   sH   d}t | jD ]}t||j\}}t||j\}}||||f 9 }q|S Nr   )reversedr:   divmodr-   r?   )r@   ijkwargsresultrA   mnr    r    r!   _entry   s   zKroneckerProduct._entryc                 C      t ttt| j  S r%   )r   r2   r3   r   r:   r   r@   r    r    r!   _eval_adjoint      zKroneckerProduct._eval_adjointc                 C   s   t dd | jD   S )Nc                 S      g | ]}|  qS r    )	conjugater'   r    r    r!   
<listcomp>       z4KroneckerProduct._eval_conjugate.<locals>.<listcomp>)r   r:   r   rM   r    r    r!   _eval_conjugate   rO   z KroneckerProduct._eval_conjugatec                 C   rL   r%   )r   r2   r3   r
   r:   r   rM   r    r    r!   _eval_transpose   rO   z KroneckerProduct._eval_transposec                    s$   ddl m   t fdd| jD  S )Nr   tracec                    s   g | ]} |qS r    r    r'   rV   r    r!   rR      rS   z0KroneckerProduct._eval_trace.<locals>.<listcomp>)rW   r   r:   rM   r    rV   r!   _eval_trace   s   zKroneckerProduct._eval_tracec                    sL   ddl m m} tdd | jD s|| S | jt fdd| jD  S )Nr   )detDeterminantc                 s   r$   r%   	is_squarer'   r    r    r!   r*      r+   z5KroneckerProduct._eval_determinant.<locals>.<genexpr>c                    s   g | ]} ||j   qS r    r,   r'   rY   rI   r    r!   rR      s    z6KroneckerProduct._eval_determinant.<locals>.<listcomp>)determinantrY   rZ   r4   r:   r-   r   )r@   rZ   r    r]   r!   _eval_determinant   s
   z"KroneckerProduct._eval_determinantc                 C   s>   zt dd | jD  W S  ty   ddlm} ||  Y S w )Nc                 S   rP   r    )inverser'   r    r    r!   rR      rS   z2KroneckerProduct._eval_inverse.<locals>.<listcomp>r   )Inverse)r   r:   r   "sympy.matrices.expressions.inversera   )r@   ra   r    r    r!   _eval_inverse   s   zKroneckerProduct._eval_inversec                 C   sF   t |to"| j|jko"t| jt|jko"tdd t| j|jD S )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                 s   s     | ]\}}|j |j kV  qd S r%   r>   r(   r)   br    r    r!   r*          z6KroneckerProduct.structurally_equal.<locals>.<genexpr>)r0   r   r>   r   r:   r4   zipr@   otherr    r    r!   structurally_equal   s   

z#KroneckerProduct.structurally_equalc                 C   sF   t |to"| j|jko"t| jt|jko"tdd t| j|jD S )aq  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                 s   s     | ]\}}|j |jkV  qd S r%   )r?   r-   re   r    r    r!   r*      rg   z6KroneckerProduct.has_matching_shape.<locals>.<genexpr>)r0   r   r?   r-   r   r:   r4   rh   ri   r    r    r!   has_matching_shape   s   

z#KroneckerProduct.has_matching_shapec                 K   s   t tttttti| S r%   )r   r   r   r   r   r   )r@   hintsr    r    r!   _eval_expand_kroneckerproduct   s   z.KroneckerProduct._eval_expand_kroneckerproductc                 C   s0   |  |r| jdd t| j|jD  S | | S )Nc                 S   s   g | ]\}}|| qS r    r    re   r    r    r!   rR          z3KroneckerProduct._kronecker_add.<locals>.<listcomp>)rk   r=   rh   r:   ri   r    r    r!   _kronecker_add      
zKroneckerProduct._kronecker_addc                 C   s0   |  |r| jdd t| j|jD  S | | S )Nc                 S   s   g | ]\}}|| qS r    r    re   r    r    r!   rR      ro   z3KroneckerProduct._kronecker_mul.<locals>.<listcomp>)rl   r=   rh   r:   ri   r    r    r!   _kronecker_mul   rq   zKroneckerProduct._kronecker_mulc                    s8     dd}|r fdd| jD }n| j}tt| S )NdeepTc                    s   g | ]
}|j d i  qS )r    )r   r(   argrm   r    r!   rR      s    z)KroneckerProduct.doit.<locals>.<listcomp>)getr:   canonicalizer   )r@   rm   rs   r:   r    rv   r!   r      s
   zKroneckerProduct.doit)__name__
__module____qualname____doc__is_KroneckerProductr8   propertyr>   rK   rN   rT   rU   rX   r_   rc   rk   rl   rn   rp   rr   r   __classcell__r    r    r<   r!   r   V   s&    
r   c                  G   s   t dd | D stdd S )Nc                 s   r$   r%   )	is_Matrixrt   r    r    r!   r*      r+   zvalidate.<locals>.<genexpr>z Mix of Matrix and Scalar symbols)r4   r   )r:   r    r    r!   r6      s   r6   c                 C   sZ   g }g }| j D ]}| \}}|| |t| qt| }|dkr+|t|  S | S rB   )r:   args_cncextendappendr   
_from_argsr   )kronc_partnc_partru   cncr    r    r!   extract_commutative   s   

r   c            
   	   G   s   t dd | D stdt|  | d }t| dd D ]=}|j}|j}t|D ].}||||   }t|d D ]}||||| | d   }q9|dkrR|}q)||}q)|}qt	| dd	 d
j
}	t||	rk|S |	|S )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                 s   r.   r%   r/   r(   rI   r    r    r!   r*   -  r1   z+matrix_kronecker_product.<locals>.<genexpr>z&Sequence of Matrices expected, got: %sNr   r   c                 S   s   | j S r%   )_class_priority)Mr    r    r!   <lambda>I  s    z*matrix_kronecker_product.<locals>.<lambda>)key)r4   r   reprrC   r-   r?   rangerow_joincol_joinmaxr=   r0   )
r   matrix_expansionrA   r-   r?   rE   startrF   nextMatrixClassr    r    r!   matrix_kronecker_product   s,   -

r   c                 C   s"   t dd | jD s| S t| j S )Nc                 s   r.   r%   r/   r   r    r    r!   r*   R  r1   z-explicit_kronecker_product.<locals>.<genexpr>)r4   r:   r   )r   r    r    r!   explicit_kronecker_productP  s   
r   c                 C   s
   t | tS r%   )r0   r   )xr    r    r!   r   ]     
 r   c                 C   s"   t | trtdd | jD S dS )Nc                 s   r$   r%   rd   r'   r    r    r!   r*   c  r+   z&_kronecker_dims_key.<locals>.<genexpr>r   )r0   r   tupler:   exprr    r    r!   _kronecker_dims_keya  s   
r   c                 C   sJ   t | jt}|dd }|s| S dd | D }|st| S t| | S )Nr   c                 S   s   g | ]	}t d d |qS )c                 S   s
   |  |S r%   )rp   )r   yr    r    r!   r   n  r   z.kronecker_mat_add.<locals>.<listcomp>.<lambda>r   )r(   groupr    r    r!   rR   n  s    z%kronecker_mat_add.<locals>.<listcomp>)r   r:   r   popvaluesr   )r   r:   nonkronskronsr    r    r!   kronecker_mat_addh  s   r   c                 C   s   |   \}}d}|t|d k r?|||d  \}}t|tr3t|tr3||||< ||d  n|d7 }|t|d k s|t|  S )Nr   r      )as_coeff_matricesr   r0   r   rr   r   r   )r   factorr   rE   ABr    r    r!   kronecker_mat_mulw  s   r   c                    s@   t  jtrtdd  jjD rt fdd jjD  S  S )Nc                 s   r$   r%   r[   r'   r    r    r!   r*     r+   z$kronecker_mat_pow.<locals>.<genexpr>c                    s   g | ]}t | jqS r    )r   expr'   r   r    r!   rR     ro   z%kronecker_mat_pow.<locals>.<listcomp>)r0   baser   r4   r:   r   r    r   r!   kronecker_mat_pow  s   "r   c                 C   sT   dd }t tt t|ttttttt	i}|| }t
|dd}|dur(| 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)
    c                 S   s   t | to	| tS r%   )r0   r	   hasr   r   r    r    r!   haskron  s   z"combine_kronecker.<locals>.haskronr   N)r   r   r   r   r   r   r   r   r   r   getattr)r   r   rulerH   r   r    r    r!   combine_kronecker  s   
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   r6   r   r   r   rulesrx   r   r   r   r   r   r    r    r    r!   <module>   sH    (@ P
