o
    9ôj\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    úu/var/www/html/academy/tools/voices/clone/sv-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   Z"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-   rd   r    r    r!   r*   Ñ   rf   z6KroneckerProduct.has_matching_shape.<locals>.<genexpr>)r0   r   r?   r-   r   r:   r4   rg   rh   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    rd   r    r    r!   rR   Ø   ó    z3KroneckerProduct._kronecker_add.<locals>.<listcomp>)rj   r=   rg   r:   rh   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    rd   r    r    r!   rR   Þ   rn   z3KroneckerProduct._kronecker_mul.<locals>.<listcomp>)rk   r=   rg   r:   rh   r    r    r!   Ú_kronecker_mulÜ   rp   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©rl   r    r!   rR   å   s    z)KroneckerProduct.doit.<locals>.<listcomp>)Úgetr:   Úcanonicalizer   )r@   rl   rr   r:   r    ru   r!   r   â   s
   zKroneckerProduct.doit)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zis_KroneckerProductr8   Úpropertyr>   rK   rN   rT   rU   rX   r_   rb   rj   rk   rm   ro   rq   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_Matrixrs   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_partrt   Ú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: %séÿÿÿÿNr   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   Zmatrix_expansionrA   r-   r?   rE   ÚstartrF   ÚnextZ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%   rc   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%   )ro   )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:   ZnonkronsZ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   rq   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   ‰  rn   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   Zsympy.matrices.exceptionsr   Ú"sympy.matrices.expressions.matexprr	   Ú$sympy.matrices.expressions.transposer
   Z"sympy.matrices.expressions.specialr   Úsympy.matrices.matrixbaser   Úsympy.strategiesr   r   r   r   r   r   r   r   Zsympy.strategies.traverser   Úsympy.utilitiesr   Úmataddr   Úmatmulr   Úmatpowr   r"   r   r6   rˆ   r–   r—   Úrulesrw   rž   r£   r©   r¬   r±   r    r    r    r!   Ú<module>   sH    (@ Pý
ÿ