U
    -eV4                     @   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   s0   | st dt| dkr | d S t|   S d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   N)	TypeErrorlenKroneckerProductdoit)matrices r    e/var/www/html/Darija-Ai-Train/env/lib/python3.8/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                   sr   t tt|}tdd |D rTttdd |D }tdd |D rP| S |S |r`t|  t j	| f| S )Nc                 s   s   | ]}|j V  qd S N)Zis_Identity.0ar    r    r!   	<genexpr>m   s     z+KroneckerProduct.__new__.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r$   rowsr%   r    r    r!   r(   n   s     c                 s   s   | ]}t |tV  qd S r$   
isinstancer   r%   r    r    r!   r(   o   s     )
listmapr   allr   r   Zas_explicitvalidatesuper__new__)clsr#   argsret	__class__r    r!   r2   k   s    zKroneckerProduct.__new__c                 C   s@   | j d j\}}| j dd  D ]}||j9 }||j9 }q||fS )Nr   r   )r4   shaper*   cols)selfr*   r9   matr    r    r!   r8   x   s
    
zKroneckerProduct.shapec                 K   sH   d}t | jD ]4}t||j\}}t||j\}}||||f 9 }q|S Nr   )reversedr4   divmodr*   r9   )r:   ijkwargsresultr;   mnr    r    r!   _entry   s    zKroneckerProduct._entryc                 C   s   t ttt| j  S r$   )r   r-   r.   r   r4   r   r:   r    r    r!   _eval_adjoint   s    zKroneckerProduct._eval_adjointc                 C   s   t dd | jD   S )Nc                 S   s   g | ]}|  qS r    )	conjugater%   r    r    r!   
<listcomp>   s     z4KroneckerProduct._eval_conjugate.<locals>.<listcomp>)r   r4   r   rF   r    r    r!   _eval_conjugate   s    z KroneckerProduct._eval_conjugatec                 C   s   t ttt| j  S r$   )r   r-   r.   r
   r4   r   rF   r    r    r!   _eval_transpose   s    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%   rL   r    r!   rI      s     z0KroneckerProduct._eval_trace.<locals>.<listcomp>)rM   r   r4   rF   r    rL   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   s   | ]}|j V  qd S r$   Z	is_squarer%   r    r    r!   r(      s     z5KroneckerProduct._eval_determinant.<locals>.<genexpr>c                    s   g | ]} ||j   qS r    r)   r%   rO   rC   r    r!   rI      s     z6KroneckerProduct._eval_determinant.<locals>.<listcomp>)ZdeterminantrO   rP   r/   r4   r*   r   )r:   rP   r    rR   r!   _eval_determinant   s
    z"KroneckerProduct._eval_determinantc                 C   sD   zt dd | jD  W S  tk
r>   ddlm} ||  Y S X d S )Nc                 S   s   g | ]}|  qS r    )Zinverser%   r    r    r!   rI      s     z2KroneckerProduct._eval_inverse.<locals>.<listcomp>r   )Inverse)r   r4   r   Z"sympy.matrices.expressions.inverserT   )r:   rT   r    r    r!   _eval_inverse   s
    zKroneckerProduct._eval_inversec                 C   sF   t |toD| j|jkoDt| jt|jkoDt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$   r8   r&   r'   br    r    r!   r(      s     z6KroneckerProduct.structurally_equal.<locals>.<genexpr>)r,   r   r8   r   r4   r/   zipr:   otherr    r    r!   structurally_equal   s    

z#KroneckerProduct.structurally_equalc                 C   sF   t |toD| j|jkoDt| jt|jkoDt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$   )r9   r*   rW   r    r    r!   r(      s     z6KroneckerProduct.has_matching_shape.<locals>.<genexpr>)r,   r   r9   r*   r   r4   r/   rY   rZ   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   s4   |  |r(| jdd t| j|jD  S | | S d S )Nc                 S   s   g | ]\}}|| qS r    r    rW   r    r    r!   rI      s     z3KroneckerProduct._kronecker_add.<locals>.<listcomp>)r\   r7   rY   r4   rZ   r    r    r!   _kronecker_add   s    
zKroneckerProduct._kronecker_addc                 C   s4   |  |r(| jdd t| j|jD  S | | S d S )Nc                 S   s   g | ]\}}|| qS r    r    rW   r    r    r!   rI      s     z3KroneckerProduct._kronecker_mul.<locals>.<listcomp>)r]   r7   rY   r4   rZ   r    r    r!   _kronecker_mul   s    
zKroneckerProduct._kronecker_mulc                    s8     dd}|r& fdd| jD }n| j}tt| S )NdeepTc                    s   g | ]}|j f  qS r    )r   r&   argr^   r    r!   rI      s     z)KroneckerProduct.doit.<locals>.<listcomp>)getr4   canonicalizer   )r:   r^   rb   r4   r    re   r!   r      s
    zKroneckerProduct.doit)__name__
__module____qualname____doc__Zis_KroneckerProductr2   propertyr8   rE   rG   rJ   rK   rN   rS   rU   r\   r]   r_   r`   ra   r   __classcell__r    r    r6   r!   r   V   s$   
r   c                  G   s   t dd | D stdd S )Nc                 s   s   | ]}|j V  qd S r$   )Z	is_Matrixrc   r    r    r!   r(      s     zvalidate.<locals>.<genexpr>z Mix of Matrix and Scalar symbols)r/   r   )r4   r    r    r!   r0      s    r0   c                 C   sZ   g }g }| j D ]*}| \}}|| |t| qt| }|dkrV|t|  S | S r<   )r4   Zargs_cncextendappendr   Z
_from_argsr   )kronZc_partZnc_partrd   cncr    r    r!   extract_commutative   s    

rs   c            
   	   G   s   t dd | D s"tdt|  | d }t| dd D ]z}|j}|j}t|D ]\}||||   }t|d D ]"}||||| | d   }qr|dkr|}qR||}qR|}q:t	| dd	 d
j
}	t||	r|S |	|S d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   s   | ]}t |tV  qd S r$   r+   r&   rC   r    r    r!   r(   -  s     z+matrix_kronecker_product.<locals>.<genexpr>z&Sequence of Matrices expected, got: %sNr   r   c                 S   s   | j S r$   )Z_class_priority)Mr    r    r!   <lambda>I      z*matrix_kronecker_product.<locals>.<lambda>)key)r/   r   reprr=   r*   r9   rangeZrow_joinZcol_joinmaxr7   r,   )
r   Zmatrix_expansionr;   r*   r9   r?   startr@   nextZMatrixClassr    r    r!   matrix_kronecker_product   s,    -

r   c                 C   s"   t dd | jD s| S t| j S )Nc                 s   s   | ]}t |tV  qd S r$   r+   rt   r    r    r!   r(   R  s     z-explicit_kronecker_product.<locals>.<genexpr>)r/   r4   r   )rp   r    r    r!   explicit_kronecker_productP  s    r   c                 C   s
   t | tS r$   )r,   r   )xr    r    r!   rw   ]  rx   rw   c                 C   s&   t | trtdd | jD S dS d S )Nc                 s   s   | ]}|j V  qd S r$   rV   r%   r    r    r!   r(   c  s     z&_kronecker_dims_key.<locals>.<genexpr>r   )r,   r   tupler4   exprr    r    r!   _kronecker_dims_keya  s    
r   c                 C   sN   t | jt}|dd }|s | S dd | D }|s>t| S t| | S d S )Nr   c                 S   s   g | ]}t d d |qS )c                 S   s
   |  |S r$   )r`   )r   yr    r    r!   rw   n  rx   z.kronecker_mat_add.<locals>.<listcomp>.<lambda>r   )r&   groupr    r    r!   rI   n  s   z%kronecker_mat_add.<locals>.<listcomp>)r   r4   r   popvaluesr   )r   r4   ZnonkronsZkronsr    r    r!   kronecker_mat_addh  s    r   c                 C   s|   |   \}}d}|t|d k rp|||d  \}}t|trft|trf||||< ||d  q|d7 }q|t|  S )Nr   r      )Zas_coeff_matricesr   r,   r   ra   r   r   )r   factorr   r?   ABr    r    r!   kronecker_mat_mulw  s    
r   c                    sD   t  jtr<tdd  jjD r<t fdd jjD  S  S d S )Nc                 s   s   | ]}|j V  qd S r$   rQ   r%   r    r    r!   r(     s     z$kronecker_mat_pow.<locals>.<genexpr>c                    s   g | ]}t | jqS r    )r   expr%   r   r    r!   rI     s     z%kronecker_mat_pow.<locals>.<listcomp>)r,   baser   r/   r4   r   r    r   r!   kronecker_mat_pow  s    "r   c                 C   sX   dd }t tt t|ttttttt	i}|| }t
|dd}|dk	rP| S |S d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$   )r,   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   rulerB   r   r    r    r!   combine_kronecker  s     
  r   N)4rk   	functoolsr   mathr   Z
sympy.corer   r   Zsympy.functionsr   Zsympy.matrices.commonr   Z"sympy.matrices.expressions.matexprr	   Z$sympy.matrices.expressions.transposer
   Z"sympy.matrices.expressions.specialr   Zsympy.matrices.matricesr   Zsympy.strategiesr   r   r   r   r   r   r   r   Zsympy.strategies.traverser   Zsympy.utilitiesr   Zmataddr   matmulr   Zmatpowr   r"   r   r0   rs   r   r   rulesrg   r   r   r   r   r   r    r    r    r!   <module>   sF   (@ P
