
    Ed^8                         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 d d
lmZmZmZmZmZmZ d dlmZ d Z G d de          Zd Zd Z d Z! G d de          Z"dS )    )Counter)Mulsympify)Add)ExprBuilder)default_sort_key)log)
ShapeError)
MatrixExpr)
ZeroMatrix	OneMatrix)unpackflatten	conditionexhaustrm_idsort)sympy_deprecation_warningc                      | st          d          t          |   t          |           dk    r| d         S d | D             } t          |                                  S )au  
    Return the elementwise (aka Hadamard) product of matrices.

    Examples
    ========

    >>> from sympy import hadamard_product, MatrixSymbol
    >>> A = MatrixSymbol('A', 2, 3)
    >>> B = MatrixSymbol('B', 2, 3)
    >>> hadamard_product(A)
    A
    >>> hadamard_product(A, B)
    HadamardProduct(A, B)
    >>> hadamard_product(A, B)[0, 1]
    A[0, 1]*B[0, 1]
    z#Empty Hadamard product is undefined   r   c                      g | ]}|j         	|S  )is_Identity.0is     Clib/python3.11/site-packages/sympy/matrices/expressions/hadamard.py
<listcomp>z$hadamard_product.<locals>.<listcomp>(   s    ===!q}=A===    )	TypeErrorvalidatelenHadamardProductdoit)matricess    r   hadamard_productr&      sm    "  ?=>>>h
8}} 1{==x===)..000r   c                   b     e Zd ZdZdZddd fd
Zed             Zd Zd	 Z	d
 Z
d Zd Z xZS )r#   a(  
    Elementwise product of matrix expressions

    Examples
    ========

    Hadamard product for matrix symbols:

    >>> from sympy import hadamard_product, HadamardProduct, MatrixSymbol
    >>> A = MatrixSymbol('A', 5, 5)
    >>> B = MatrixSymbol('B', 5, 5)
    >>> isinstance(hadamard_product(A, B), HadamardProduct)
    True

    Notes
    =====

    This is a symbolic object that simply stores its argument without
    evaluating it. To actually compute the product, use the function
    ``hadamard_product()`` or ``HadamardProduct.doit``
    TFN)evaluatecheckc                   t          t          t          |                    }|t          ddd           |dv r
t	          |  nt          ddd            t                      j        | g|R  }|r|                    d          }|S )	NzjPassing check to HadamardProduct is deprecated and the check argument will be removed in a future version.z1.11z,remove-check-argument-from-matrix-operations)deprecated_since_versionactive_deprecations_target)TNzpPassing check=False to HadamardProduct is deprecated and the check argument will be removed in a future version.F)deep)listmapr   r   r!   super__new__r$   )clsr(   r)   argsobj	__class__s        r   r1   zHadamardProduct.__new__D   s    C&&'' 	[%|)/+Y[ [ [ [
 L  	[dOOO% C)/+Y[ [ [ [
 eggoc)D))) 	'(((&&C
r   c                 &    | j         d         j        S Nr   )r3   shapeselfs    r   r8   zHadamardProduct.shapeY   s    y|!!r   c                 @    t          fd| j        D              S )Nc                 .    g | ]} |j         fi S r   )_entry)r   argr   jkwargss     r   r   z*HadamardProduct._entry.<locals>.<listcomp>^   s/    EEECZSZ1////EEEr   )r   r3   )r:   r   r?   r@   s    ```r   r=   zHadamardProduct._entry]   s-    EEEEEE49EEEFFr   c                 `    ddl m} t          t          t	          || j                             S Nr   )	transpose)$sympy.matrices.expressions.transposerC   r#   r.   r/   r3   r:   rC   s     r   _eval_transposezHadamardProduct._eval_transpose`   s3    BBBBBBSDI%>%> ? ?@@r   c                 0    | j         fd| j        D              }ddlm ddlm} fd|j        D             rIfd|j        D             }  |d t           D                       j        | j         }t          |g|z    }t          |          S )Nc                 *    g | ]} |j         d i S )r   )r$   )r   r   hintss     r   r   z(HadamardProduct.doit.<locals>.<listcomp>e   s'    >>>q616??E??>>>r   r   )
MatrixBase)ImmutableMatrixc                 4    g | ]}t          |          |S r   )
isinstance)r   r   rJ   s     r   r   z(HadamardProduct.doit.<locals>.<listcomp>i   s(    FFF!Jq*,E,EFAFFFr   c                     g | ]}|v|	S r   r   )r   r   explicits     r   r   z(HadamardProduct.doit.<locals>.<listcomp>k   s#    CCCq(1BCCCCr   c                 6    g | ]}t          j        |          S r   )r   fromiterr   s     r   r   z(HadamardProduct.doit.<locals>.<listcomp>l   s-     ( ( ($%Q( ( (r   )funcr3   sympy.matrices.matricesrJ   sympy.matrices.immutablerK   zipreshaper8   r#   canonicalize)r:   rI   exprrK   	remainderexpl_matrJ   rO   s    `    @@r   r$   zHadamardProduct.doitd   s    ty>>>>DI>>>?666666<<<<<<FFFFtyFFF 	>CCCCDICCCI ( (),h( ( (  
$H #hZ)%;=DD!!!r   c                 6   g }t          | j                  }t          t          |                    D ]S}|d |         ||                             |          gz   ||dz   d          z   }|                    t          |            Tt          j        |          S Nr   )	r.   r3   ranger"   diffappendr&   r   rQ   )r:   xtermsr3   r   factorss         r   _eval_derivativez HadamardProduct._eval_derivatives   s    DIs4yy!! 	5 	5A2A2h$q',,q//!22T!A#$$Z?GLL)734444|E"""r   c                     ddl m} ddl m} ddlm} fdt           j                  D             }g }|D ]I} j        d |         } j        |dz   d          }	 j        |                                       }
t          |	|z    }ddg} fd	t          |          D             }|
D ]}|j	        |j
                 }|j	        |j                 }t          |t          |t          ||g          |t          ||g          g          g|          }|j        d         j        d         j        |_        d|_        |j        d         j        d
         j        |_        d|_        |g|_	        |                    |           ؐK|S )Nr   ArrayDiagonalArrayTensorProduct_make_matrixc                 D    g | ]\  }}|                               |S r   )has)r   r   r>   r`   s      r   r   zAHadamardProduct._eval_derivative_matrix_lines.<locals>.<listcomp>   s,    IIIFAscggajjIaIIIr   r   )r            c                 <    g | ]\  }}j         |         d k    |S r   )r8   r   r?   er:   s      r   r   zAHadamardProduct._eval_derivative_matrix_lines.<locals>.<listcomp>   s-    PPPdaTZ]a=OPPPPr   rm   )0sympy.tensor.array.expressions.array_expressionsrf   rh   "sympy.matrices.expressions.matexprrj   	enumerater3   _eval_derivative_matrix_linesr&   _lines_first_line_index_second_line_indexr   _first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_indexr_   )r:   r`   rf   rh   rj   
with_x_indlinesind	left_args
right_argsdhadamdiagonalr   l1l2subexprs   ``               r   rx   z-HadamardProduct._eval_derivative_matrix_lines{   s   RRRRRRWWWWWWCCCCCCIIIIi	&:&:III
 	  	 C	$3$I3q566*J	#<<Q??A$zI'=?E'HPPPPi&9&9PPPH    Xa12Xa23%!#. +L2$ ? ? % +L2$ ? ? 	 	  +2,q/*>q*A*F')*&+2<?+?+B+G(*+'#9Q- 0 r   )__name__
__module____qualname____doc__is_HadamardProductr1   propertyr8   r=   rF   r$   rc   rx   __classcell__r5   s   @r   r#   r#   ,   s         * %*$       * " " X"G G GA A A" " "# # #' ' ' ' ' ' 'r   r#   c                      t          d | D                       st          d          | d         }| dd          D ](}|j        |j        k    rt          d|d|d          )d S )Nc              3   $   K   | ]}|j         V  d S N)	is_Matrix)r   r>   s     r   	<genexpr>zvalidate.<locals>.<genexpr>   s$      --s}------r   z Mix of Matrix and Scalar symbolsr   r   z	Matrices z and z are not aligned)allr    r8   r
   )r3   ABs      r   r!   r!      s    ------- <:;;;QA!""X L L7ag 	L*QQQJKKK	LL Lr   c                    t          d t                    }t          |          } ||           } t          d t          d                     } ||           } d }t          d |          } ||           } t	          | t
                    rxt          | j                  }g }|                                D ]D\  }}|dk    r|	                    |           !|	                    t          ||                     Et          | } t          d t          t                              } ||           } t          |           } | S )a  Canonicalize the Hadamard product ``x`` with mathematical properties.

    Examples
    ========

    >>> from sympy import MatrixSymbol, HadamardProduct
    >>> from sympy import OneMatrix, ZeroMatrix
    >>> from sympy.matrices.expressions.hadamard import canonicalize
    >>> from sympy import init_printing
    >>> init_printing(use_unicode=False)

    >>> A = MatrixSymbol('A', 2, 2)
    >>> B = MatrixSymbol('B', 2, 2)
    >>> C = MatrixSymbol('C', 2, 2)

    Hadamard product associativity:

    >>> X = HadamardProduct(A, HadamardProduct(B, C))
    >>> X
    A.*(B.*C)
    >>> canonicalize(X)
    A.*B.*C

    Hadamard product commutativity:

    >>> X = HadamardProduct(A, B)
    >>> Y = HadamardProduct(B, A)
    >>> X
    A.*B
    >>> Y
    B.*A
    >>> canonicalize(X)
    A.*B
    >>> canonicalize(Y)
    A.*B

    Hadamard product identity:

    >>> X = HadamardProduct(A, OneMatrix(2, 2))
    >>> X
    A.*1
    >>> canonicalize(X)
    A

    Absorbing element of Hadamard product:

    >>> X = HadamardProduct(A, ZeroMatrix(2, 2))
    >>> X
    A.*0
    >>> canonicalize(X)
    0

    Rewriting to Hadamard Power

    >>> X = HadamardProduct(A, A, A)
    >>> X
    A.*A.*A
    >>> canonicalize(X)
     .3
    A

    Notes
    =====

    As the Hadamard product is associative, nested products can be flattened.

    The Hadamard product is commutative so that factors can be sorted for
    canonical form.

    A matrix of only ones is an identity for Hadamard product,
    so every matrices of only ones can be removed.

    Any zero matrix will make the whole product a zero matrix.

    Duplicate elements can be collected and rewritten as HadamardPower

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hadamard_product_(matrices)
    c                 ,    t          | t                    S r   rM   r#   r`   s    r   <lambda>zcanonicalize.<locals>.<lambda>      jO44 r   c                 ,    t          | t                    S r   r   r   s    r   r   zcanonicalize.<locals>.<lambda>  r   r   c                 ,    t          | t                    S r   )rM   r   r   s    r   r   zcanonicalize.<locals>.<lambda>  s    Jq)44 r   c                 ^    t          d | j        D                       rt          | j         S | S )Nc              3   @   K   | ]}t          |t                    V  d S r   )rM   r   )r   cs     r   r   z/canonicalize.<locals>.absorb.<locals>.<genexpr>  s,      99Qz!Z((999999r   )anyr3   r   r8   r   s    r   absorbzcanonicalize.<locals>.absorb  s5    99!&99999 	qw''Hr   c                 ,    t          | t                    S r   r   r   s    r   r   zcanonicalize.<locals>.<lambda>  r   r   r   c                 ,    t          | t                    S r   r   r   s    r   r   zcanonicalize.<locals>.<lambda>,  r   r   )r   r   r   r   rM   r#   r   r3   itemsr_   HadamardPowerr   r   r   )r`   rulefunr   tallynew_argbaseexps           r   rW   rW      ss   f 44
 
D $--CAA 444455
 
C 	AA  
 44
 
C 	AA !_%% 
& 	9 	9ID#ax 9t$$$$}T3778888W% 44!""
 
C 	AA 	q		AHr   c                     t          |           } t          |          }|dk    r| S | j        s| |z  S |j        rt          d          t          | |          S )Nr   z#cannot raise expression to a matrix)r   r   
ValueErrorr   )r   r   s     r   hadamard_powerr   6  sg    4==D
#,,C
ax > Sy
} @>???s###r   c                   |     e Zd ZdZ fdZed             Zed             Zed             Zd Z	d Z
d Zd	 Z xZS )
r   a  
    Elementwise power of matrix expressions

    Parameters
    ==========

    base : scalar or matrix

    exp : scalar or matrix

    Notes
    =====

    There are four definitions for the hadamard power which can be used.
    Let's consider `A, B` as `(m, n)` matrices, and `a, b` as scalars.

    Matrix raised to a scalar exponent:

    .. math::
        A^{\circ b} = \begin{bmatrix}
        A_{0, 0}^b   & A_{0, 1}^b   & \cdots & A_{0, n-1}^b   \\
        A_{1, 0}^b   & A_{1, 1}^b   & \cdots & A_{1, n-1}^b   \\
        \vdots       & \vdots       & \ddots & \vdots         \\
        A_{m-1, 0}^b & A_{m-1, 1}^b & \cdots & A_{m-1, n-1}^b
        \end{bmatrix}

    Scalar raised to a matrix exponent:

    .. math::
        a^{\circ B} = \begin{bmatrix}
        a^{B_{0, 0}}   & a^{B_{0, 1}}   & \cdots & a^{B_{0, n-1}}   \\
        a^{B_{1, 0}}   & a^{B_{1, 1}}   & \cdots & a^{B_{1, n-1}}   \\
        \vdots         & \vdots         & \ddots & \vdots           \\
        a^{B_{m-1, 0}} & a^{B_{m-1, 1}} & \cdots & a^{B_{m-1, n-1}}
        \end{bmatrix}

    Matrix raised to a matrix exponent:

    .. math::
        A^{\circ B} = \begin{bmatrix}
        A_{0, 0}^{B_{0, 0}}     & A_{0, 1}^{B_{0, 1}}     &
        \cdots & A_{0, n-1}^{B_{0, n-1}}     \\
        A_{1, 0}^{B_{1, 0}}     & A_{1, 1}^{B_{1, 1}}     &
        \cdots & A_{1, n-1}^{B_{1, n-1}}     \\
        \vdots                  & \vdots                  &
        \ddots & \vdots                      \\
        A_{m-1, 0}^{B_{m-1, 0}} & A_{m-1, 1}^{B_{m-1, 1}} &
        \cdots & A_{m-1, n-1}^{B_{m-1, n-1}}
        \end{bmatrix}

    Scalar raised to a scalar exponent:

    .. math::
        a^{\circ b} = a^b
    c                 F   t          |          }t          |          }|j        r|j        r||z  S |j        rD|j        r=|j        |j        k    r-t	          d                    |j        |j                            t                                          | ||          }|S )NzGThe shape of the base {} and the shape of the exponent {} do not match.)r   	is_scalarr   r8   r   formatr0   r1   )r2   r   r   r4   r5   s       r   r1   zHadamardPower.__new__{  s    t}}cll> 	cm 	3;> 	cm 	
ci0G 	=
CI..   ggooc4--
r   c                     | j         d         S r7   _argsr9   s    r   r   zHadamardPower.base      z!}r   c                     | j         d         S r\   r   r9   s    r   r   zHadamardPower.exp  r   r   c                 J    | j         j        r| j         j        S | j        j        S r   )r   r   r8   r   r9   s    r   r8   zHadamardPower.shape  s#    9 	#9?"x~r   c                 4   | j         }| j        }|j        r |j        ||fi |}n,|j        r|}n"t          d                    |                    |j        r |j        ||fi |}n,|j        r|}n"t          d                    |                    ||z  S )Nz)The base {} must be a scalar or a matrix.z-The exponent {} must be a scalar or a matrix.)r   r   r   r=   r   r   r   )r:   r   r?   r@   r   r   abs           r   r=   zHadamardPower._entry  s    yh> 	JAq++F++AA^ 	JAA;BB4HHJ J J = 	M
1a**6**AA] 	MAA?FFsKKM M M Avr   c                 T    ddl m} t           || j                  | j                  S rB   )rD   rC   r   r   r   rE   s     r   rF   zHadamardPower._eval_transpose  s2    BBBBBBYYty1148<<<r   c                     | j                             |          }| j                            t                    }|                    |          }t          ||z  | j         |z  z   |           S r   )r   r^   r   	applyfuncr	   r&   )r:   r`   dexplogbasedlbases        r   rc   zHadamardPower._eval_derivative  s`    x}}Q)%%c**aL48F?*
 
 	
r   c                     ddl m} ddl m} ddlm}  j                            |          }|D ]}ddg} fdt          |          D             }|j        |j	                 }|j        |j
                 }	t          |t          |t          ||g           j        t           j         j        dz
            z  t          ||	g          g          g||j        	          }
|
j        d         j        d         j        |_        d|_        d|_	        |
j        d         j        d
         j        |_        d|_        d|_
        |
g|_        |S )Nr   rg   re   ri   )r   rm   rn   c                 F    g | ]\  }}j         j        |         d k    |S rr   )r   r8   rs   s      r   r   z?HadamardPower._eval_derivative_matrix_lines.<locals>.<listcomp>  s1    UUUdaTY_Q=OST=TUUUUr   r   )	validatorrm   )ru   rh   rf   rv   rj   r   rx   rw   ry   rz   r{   r   r   r   	_validater3   r|   r}   r~   r   )r:   r`   rh   rf   rj   lrr   r   r   r   r   s   `          r   rx   z+HadamardPower._eval_derivative_matrix_lines  s   WWWWWWRRRRRRCCCCCCY44Q77 	! 	!A'HUUUUi&9&9UUUH!-.B!./B!*'rd;; H^DItxz%J%JJ'rd;; 	 	 (1  G '.l1o&:1&=&BA#%&A""#A'.|A';A'>'CA$&'A##$A yAHH	r   )r   r   r   r   r1   r   r   r   r8   r=   rF   rc   rx   r   r   s   @r   r   r   B  s        6 6p        X   X   X
  ,= = =
 
 
             r   r   N)#collectionsr   
sympy.corer   r   sympy.core.addr   sympy.core.exprr   sympy.core.sortingr   &sympy.functions.elementary.exponentialr	   sympy.matrices.commonr
   rv   r   "sympy.matrices.expressions.specialr   r   sympy.strategiesr   r   r   r   r   r   sympy.utilities.exceptionsr   r&   r#   r!   rW   r   r   r   r   r   <module>r      s         # # # # # # # #       ' ' ' ' ' ' / / / / / / 6 6 6 6 6 6 , , , , , , 9 9 9 9 9 9 D D D D D D D D                A @ @ @ @ @1 1 16v v v v vj v v vrL L LC C CL	$ 	$ 	$Z Z Z Z ZJ Z Z Z Z Zr   