
    g`6                         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! G d de          Z"dS )    )Counter)Mulsympify)Add)ExprBuilder)default_sort_key)log)
MatrixExpr)validate_matadd_integer)
ZeroMatrix	OneMatrix)unpackflatten	conditionexhaustrm_idsort)sympy_deprecation_warningc                      | st          d          t          |           dk    r| d         S 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   )	TypeErrorlenHadamardProductdoit)matricess    _/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/matrices/expressions/hadamard.pyhadamard_productr      sM    "  ?=>>>
8}}{H%**,,,    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k    rt	          d          t          d |D                       st          d          |t          ddd           |d	ur	t          |   t                      j
        | g|R  }|r|                    d	
          }|S )Nr   z+HadamardProduct needs at least one argumentc              3   @   K   | ]}t          |t                    V  d S N)
isinstancer
   ).0args     r   	<genexpr>z*HadamardProduct.__new__.<locals>.<genexpr>G   s,      ??3:c:..??????r   z Mix of Matrix and Scalar symbolszjPassing 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_targetF)deep)listmapr   r   
ValueErrorallr   r   validatesuper__new__r   )clsr    r!   argsobj	__class__s        r   r2   zHadamardProduct.__new__A   s    C&&''t99>>JKKK??$????? 	@>???%|)/+Y[ [ [ [
 dOOeggoc)D))) 	'(((&&C
r   c                 &    | j         d         j        S Nr   )r4   shapeselfs    r   r9   zHadamardProduct.shapeX   s    y|!!r   c                 @    t          fd| j        D              S )Nc                 .    g | ]} |j         fi S  )_entry)r&   r'   ijkwargss     r   
<listcomp>z*HadamardProduct._entry.<locals>.<listcomp>]   s/    EEECZSZ1////EEEr   )r   r4   )r;   r@   rA   rB   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.transposerF   r   r,   r-   r4   r;   rF   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              3   2   K   | ]} |j         di V  d S )Nr>   )r   )r&   r@   hintss     r   r(   z'HadamardProduct.doit.<locals>.<genexpr>d   s1      >>q616??E??>>>>>>r   r   )
MatrixBase)ImmutableMatrixc                 4    g | ]}t          |          |S r>   )r%   )r&   r@   rM   s     r   rC   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   rC   z(HadamardProduct.doit.<locals>.<listcomp>k   s#    CCCq(1B1B1B1B1Br   c                 6    g | ]}t          j        |          S r>   )r   fromiter)r&   r@   s     r   rC   z(HadamardProduct.doit.<locals>.<listcomp>l   s-     ( ( ($%Q( ( (r   )funcr4   sympy.matrices.matrixbaserM   sympy.matrices.immutablerN   zipreshaper9   r   canonicalize)r;   rL   exprrN   	remainderexpl_matrM   rQ   s    `    @@r   r   zHadamardProduct.doitc   s    ty>>>>DI>>>?888888<<<<<<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,   r4   ranger   diffappendr   r   rS   )r;   xtermsr4   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'   rb   s      r   rC   zAHadamardProduct._eval_derivative_matrix_lines.<locals>.<listcomp>   s,    IIIFAscggajjIaIIIr   r   )r            c                 <    g | ]\  }}j         |         d k    |S r   )r9   r&   rA   er;   s      r   rC   zAHadamardProduct._eval_derivative_matrix_lines.<locals>.<listcomp>   s-    PPPdaTZ]a=O=O=O=O=Or   ro   )0sympy.tensor.array.expressions.array_expressionsrh   rj   "sympy.matrices.expressions.matexprrl   	enumerater4   _eval_derivative_matrix_linesr   _lines_first_line_index_second_line_indexr   _first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_indexra   )r;   rb   rh   rj   rl   
with_x_indlinesind	left_args
right_argsdhadamdiagonalr@   l1l2subexprs   ``               r   rz   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_HadamardProductr2   propertyr9   r?   rI   r   re   rz   __classcell__r6   s   @r   r   r   )   s         * %*$       . " " X"G G GA A A" " " # # #' ' ' ' ' ' 'r   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$   r%   r   rb   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$   )r%   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$   )r%   r   )r&   cs     r   r(   z/canonicalize.<locals>.absorb.<locals>.<genexpr>
  s,      99Qz!Z((999999r   )anyr4   r   r9   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   r%   r   r   r4   itemsra   HadamardPowerr   r   r   )rb   rulefunr   tallynew_argbaseexps           r   rY   rY      sp   f 44
 
D $--CAA 444455
 
C 	AA  
 44
 
C 	AA !_%% 
& 	9 	9ID#axx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   	is_Matrixr.   r   )r   r   s     r   hadamard_powerr   -  sd    4==D
#,,C
axx> 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                 $   t          |          }t          |          }|j        r|j        r||z  S t          |t                    r%t          |t                    rt	          ||           t                                          | ||          }|S r$   )r   	is_scalarr%   r
   r0   r1   r2   )r3   r   r   r5   r6   s       r   r2   zHadamardPower.__new__r  s    t}}cll> 	cm 	3;dJ'' 	 JsJ,G,G 	 T3ggooc4--
r   c                     | j         d         S r8   _argsr:   s    r   r   zHadamardPower.base      z!}r   c                     | j         d         S r^   r   r:   s    r   r   zHadamardPower.exp  r   r   c                 J    | j         j        r| j         j        S | j        j        S r$   )r   r   r9   r   r:   s    r   r9   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.   format)r;   r@   rA   rB   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 rE   )rG   rF   r   r   r   rH   s     r   rI   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;   rb   dexplogbasedlbases        r   re   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   ri   rg   rk   )r   ro   rp   c                 F    g | ]\  }}j         j        |         d k    |S rt   )r   r9   ru   s      r   rC   z?HadamardPower._eval_derivative_matrix_lines.<locals>.<listcomp>  s1    UUUdaTY_Q=OST=T=T=T=T=Tr   r   )	validatorro   )rw   rj   rh   rx   rl   r   rz   ry   r{   r|   r}   r   r   r   	_validater4   r~   r   r   r   )r;   rb   rj   rh   rl   lrr@   r   r   r   r   s   `          r   rz   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   r2   r   r   r   r9   r?   rI   re   rz   r   r   s   @r   r   r   9  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	   rx   r
   !sympy.matrices.expressions._shaper   r0   "sympy.matrices.expressions.specialr   r   sympy.strategiesr   r   r   r   r   r   sympy.utilities.exceptionsr   r   r   rY   r   r   r>   r   r   <module>r      s         # # # # # # # #       ' ' ' ' ' ' / / / / / / 6 6 6 6 6 6 9 9 9 9 9 9 Q Q Q Q Q Q D D D D D D D D                A @ @ @ @ @- - -0y y y y yj y y y|C C CL	$ 	$ 	$W W W W WJ W W W W Wr   