
    sg`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"y)    )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                  n    | st        d      t        |       dk(  r| d   S t        |  j                         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    V/var/www/html/venv/lib/python3.12/site-packages/sympy/matrices/expressions/hadamard.pyhadamard_productr      s=    " =>>
8}{H%**,,    c                   \     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        | (  | g| }|r|j                  d	
      }|S )Nr   z+HadamardProduct needs at least one argumentc              3   <   K   | ]  }t        |t                y wN)
isinstancer
   ).0args     r   	<genexpr>z*HadamardProduct.__new__.<locals>.<genexpr>G   s     ?3:c:.?   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   r3   zHadamardProduct.__new__A   s    C&'t9>JKK?$??>??%|)/+Y[
 dOgoc)D)(((&C
r   c                 4    | j                   d   j                  S Nr   )r5   shapeselfs    r   r:   zHadamardProduct.shapeX   s    yy|!!!r   c           
      p    t        | j                  D cg c]  } |j                  ||fi | c} S c c}w r$   )r   r5   _entry)r<   ijkwargsr'   s        r   r>   zHadamardProduct._entry\   s1    499ECZSZZ1//EFFEs   3c                 X    ddl m} t        t        t	        || j
                               S Nr   )	transpose)$sympy.matrices.expressions.transposerD   r   r-   r.   r5   r<   rD   s     r   _eval_transposezHadamardProduct._eval_transpose_   s     BSDII%> ?@@r   c           	          | j                   fd| j                  D         }ddlm} ddlm} |j                  D cg c]  }t        ||      s| }}|rp|j                  D cg c]	  }||vs| }}  |t        | D cg c]  }t        j                  |       c}      j                  | j                   }t        |g|z    }t        |      S c c}w c c}w c c}w )Nc              3   B   K   | ]  } |j                   di   y w)N )r   )r&   r?   hintss     r   r(   z'HadamardProduct.doit.<locals>.<genexpr>d   s     >q6166?E?>s   r   )
MatrixBase)ImmutableMatrix)funcr5   sympy.matrices.matrixbaserL   sympy.matrices.immutablerM   r%   zipr   fromiterreshaper:   r   canonicalize)	r<   rK   exprrL   rM   r?   explicit	remainderexpl_mats	    `       r   r   zHadamardProduct.doitc   s    tyy>DII>?8<#yyF!Jq*,EAFF$(IICq(1BCIC),h($%Q( w

$H #hZ)%;=DD!! GC(s   CC&	C0CC
c                    g }t        | j                        }t        t        |            D ]=  }|d | ||   j	                  |      gz   ||dz   d  z   }|j                  t        |        ? t        j                  |      S Nr   )	r-   r5   ranger   diffappendr   r   rR   )r<   xtermsr5   r?   factorss         r   _eval_derivativez HadamardProduct._eval_derivatives   s}    DIIs4y! 	5A2Ah$q',,q/!22T!A#$Z?GLL)734	5 ||E""r   c                    ddl m} ddl m} ddlm} t        | j                        D cg c]  \  }}|j                  |      s| }}}g }|D ]i  }	| j                  d |	 }
| j                  |	dz   d  }| j                  |	   j                  |      }t        ||
z    }ddg}t        |      D cg c]  \  }}| j                  |   dk7  s| }}}|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|_        |j'                  |        l |S c c}}w c c}}w )	Nr   ArrayDiagonalArrayTensorProduct_make_matrixr   )r            ri   )0sympy.tensor.array.expressions.array_expressionsrd   rf   "sympy.matrices.expressions.matexprrh   	enumerater5   has_eval_derivative_matrix_linesr   r:   _lines_first_line_index_second_line_indexr   _first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_indexr]   )r<   r^   rd   rf   rh   r?   r'   
with_x_indlinesind	left_args
right_argsdhadamdiagonalr@   el1l2subexprs                       r   rq   z-HadamardProduct._eval_derivative_matrix_lines{   s   RWC&/		&:IFAscggajaI
I 	 C		$3I3q56*J		#<<Q?A$zI'=?E'H&/&9PdaTZZ]a=OPHP  XXa112XXa223%!#. +L2$ ? % +L2$ ?	 	 +2,,q/*>*>q*A*F*F')*&+2<<?+?+?+B+G+G(*+'#9Q- 	 @ E J Qs   F?F?0GG)__name__
__module____qualname____doc__is_HadamardProductr3   propertyr:   r>   rG   r   ra   rq   __classcell__r7   s   @r   r   r   )   sI    * %*$ . " "GA" #'r   r   c                    t        d t              }t        |      } ||       } t        d t        d             } ||       } d }t        d |      } ||       } t	        | t
              rit        | j                        }g }|j                         D ]7  \  }}|dk(  r|j                  |       |j                  t        ||             9 t        | } 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   r^   s    r   <lambda>zcanonicalize.<locals>.<lambda>       jO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 r   c                 b    t        d | j                  D              rt        | j                   S | S )Nc              3   <   K   | ]  }t        |t                y wr$   )r%   r   )r&   cs     r   r(   z/canonicalize.<locals>.absorb.<locals>.<genexpr>
  s     9Qz!Z(9r)   )anyr5   r   r:   r   s    r   absorbzcanonicalize.<locals>.absorb	  s(    9!&&99qww''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   r5   itemsr]   HadamardPowerr   r   r   )r^   rulefunr   tallynew_argbaseexps           r   rT   rT      s
   f 4
D $-CAA 445
C 	AA
 4
C 	AA !_% 	9ID#axt$}T378		9 W% 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   -  sQ    4=D
#,C
ax>>Sy
}}>??s##r   c                   j     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
   r1   r2   r3   )r4   r   r   r6   r7   s       r   r3   zHadamardPower.__new__r  s`    t}cl>>cmm3;dJ'JsJ,GT3goc4-
r   c                      | j                   d   S r9   _argsr;   s    r   r   zHadamardPower.base      zz!}r   c                      | j                   d   S rZ   r   r;   s    r   r   zHadamardPower.exp  r   r   c                     | j                   j                  r| j                   j                  S | j                  j                  S r$   )r   r   r:   r   r;   s    r   r:   zHadamardPower.shape  s+    9999??"xx~~r   c                 j   | j                   }| j                  }|j                  r |j                  ||fi |}n)|j                  r|}nt        dj                  |            |j                  r |j                  ||fi |}||z  S |j                  r|}||z  S t        dj                  |            )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?   r@   rA   r   r   abs           r   r>   zHadamardPower._entry  s    yyhh>>Aq+F+A^^A;BB4HJ J ==

1a*6*A Av ]]A
 Av ?FFsKM Mr   c                 Z    ddl m} t         || j                        | j                        S rC   )rE   rD   r   r   r   rF   s     r   rG   zHadamardPower._eval_transpose  s    BYtyy1488<<r   c                     | j                   j                  |      }| j                  j                  t              }|j                  |      }t        ||z  | j                   |z  z   |       S r$   )r   r\   r   	applyfuncr	   r   )r<   r^   dexplogbasedlbases        r   ra   zHadamardPower._eval_derivative  sY    xx}}Q))%%c*aL488F?*
 	
r   c                 *   ddl m} ddl m} ddlm} | j
                  j                  |      }|D ]Y  }ddg}t        |      D 	cg c]$  \  }}	| j
                  j                  |   dk7  s#|	& }}}	|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 c c}	}w )
Nr   re   rc   rg   )r   ri   rj   r   )	validatorri   )rm   rf   rd   rn   rh   r   rq   ro   r:   rr   rs   rt   r   r   r   	_validater5   ru   rv   rw   rx   )r<   r^   rf   rd   rh   lrr?   r   r@   r   r   r   r   s                r   rq   z+HadamardPower._eval_derivative_matrix_lines  sy   WRCYY44Q7 	!A'H&/&9UdaTYY__Q=OST=TUHU!--.B!../B!*'rd; HH^DIItxxz%JJ'rd;	 	 (11G '.ll1o&:&:1&=&B&BA#%&A""#A'.||A';';A'>'C'CA$&'A##$A yAH3	!4 	1 Vs   $F+F)r   r   r   r   r3   r   r   r   r:   r>   rG   ra   rq   r   r   s   @r   r   r   9  s^    6p      
,=
 r   r   N)#collectionsr   
sympy.corer   r   sympy.core.addr   sympy.core.exprr   sympy.core.sortingr   &sympy.functions.elementary.exponentialr	   rn   r
   !sympy.matrices.expressions._shaper   r1   "sympy.matrices.expressions.specialr   r   sympy.strategiesr   r   r   r   r   r   sympy.utilities.exceptionsr   r   r   rT   r   r   rJ   r   r   <module>r      s^     #  ' / 6 9 Q D  A-0yj y|CL	$WJ Wr   