+
    i`6                         ^ RI Ht ^ RIHtHt ^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ R	IHtHt ^ R
IHtHtHtHtHtHt ^ RIHt R t ! R R]4      tR t R t! ! R R]4      t"R# )    )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                     V '       g   \        R4      h\        V 4      ^8X  d
   V ^ ,          # \        V !  P                  4       # )aA  
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)	TypeErrorlenHadamardProductdoit)matricess   *ڃ/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/matrices/expressions/hadamard.pyhadamard_productr      s=    " =>>
8}{H%**,,    c                   t   a a ] tR t^)t oRtRtRRRR/V 3R llt]R 4       tR	 t	R
 t
R tR tR tRtVtV ;t# )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``
TevaluateFcheckNc                 < \        \        \        V4      4      p\        V4      ^ 8X  d   \	        R4      h\
        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       g   \        R4      hVe   \        RRRR	7       VRJd
   \        V!   \        SV `(  ! V .VO5!  pV'       d   VP                  RR
7      pV# )r   z+HadamardProduct needs at least one argumentc              3   B   "   T F  p\        V\        4      x  K  	  R # 5iN)
isinstancer
   ).0args   & r   	<genexpr>*HadamardProduct.__new__.<locals>.<genexpr>G   s     ?$3:c:..$   FTz 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_target)deep)listmapr   r   
ValueErrorallr   r   validatesuper__new__r   )clsr   r    argsobj	__class__s   &$$* r   r3   HadamardProduct.__new__A   s    C&'t9>JKKs?$?sss?$???>??%|)/+Y[
 dOgoc)D)(((&C
r   c                <    V P                   ^ ,          P                  # r   )r5   shapeselfs   &r   r;   HadamardProduct.shapeX   s    yy|!!!r   c           
     r    \        V P                   Uu. uF  qDP                  ! W3/ VB NK  	  up!  # u upi r#   )r   r5   _entry)r=   ijkwargsr&   s   &&&, r   r@   HadamardProduct._entry\   s/    499E9CZZ//9EFFEs   4c                X    ^ RI Hp \        \        \	        WP
                  4      4      !  # r   )	transpose)$sympy.matrices.expressions.transposerG   r   r-   r.   r5   r=   rG   s   & r   _eval_transposeHadamardProduct._eval_transpose_   s    BSII%> ?@@r   c                  a V P                   ! V3R  lV P                   4       !  p^ RIHp ^ RIHp VP                   Uu. uF  p\        WS4      '       g   K  VNK  	  ppV'       d   VP                   Uu. uF  qUV9  g   K  VNK  	  ppT! \        V!   Uu. uF  p\        P                  ! V4      NK  	  up4      P                  ! V P                  !  p\        V.V,           !  p\        V4      # u upi u upi u upi )c              3   F   <"   T F  qP                   ! R/ SB x  K  	  R # 5i)N )r   )r%   rA   hintss   & r   r'   'HadamardProduct.doit.<locals>.<genexpr>d   s     >Iq66?E?Is   !)
MatrixBase)ImmutableMatrix)funcr5   sympy.matrices.matrixbaserQ   sympy.matrices.immutablerR   r$   zipr   fromiterreshaper;   r   canonicalize)	r=   rO   exprrQ   rR   rA   explicit	remainderexpl_mats	   &l       r   r   HadamardProduct.doitc   s    yy>DII>?8<#yyFy!Jq,EAAyF$(IICIq(1BIIC&),h()7AQ( w

$H #hZ)%;=DD!! GC(s    C2C26C7C7C<c                &   . p\        V P                  4      p\        \        V4      4       FM  pVR V W4,          P	                  V4      .,           W4^,           R  ,           pVP                  \        V!  4       KO  	  \        P                  ! V4      # r#   )	r-   r5   ranger   diffappendr   r   rW   )r=   xtermsr5   rA   factorss   &&    r   _eval_derivative HadamardProduct._eval_derivatives   st    DIIs4y!A2Ah$',,q/!22TA#$Z?GLL)734 " ||E""r   c                   ^ RI Hp ^ RI Hp ^ RIHp \        V P                  4       UUu. uF   w  rVVP                  V4      '       g   K  VNK"  	  ppp. pV EF  p	V P                  RV	 p
V P                  V	^,           R pV P                  V	,          P                  V4      p\        W,           !  pRR.p\        V4       UUu. uF"  w  ppV P                  V,          ^8w  g   K   VNK$  	  pppV F  pVP                  VP                  ,          pVP                  VP                  ,          p\        V\        V\        VV.4      V\        VV.4      .4      .VO4      pVP                  ^ ,          P                  ^ ,          P                  Vn        ^ Vn        VP                  ^ ,          P                  ^,          P                  Vn        ^ Vn        V.Vn        VP'                  V4       K  	  EK  	  V# u uppi u uppi )r   ArrayDiagonalArrayTensorProduct_make_matrixN)r            )0sympy.tensor.array.expressions.array_expressionsrj   rl   "sympy.matrices.expressions.matexprrn   	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_indexrb   )r=   rc   rj   rl   rn   rA   r&   
with_x_indlinesind	left_args
right_argsdhadamdiagonalrB   el1l2subexprs   &&                  r   rw   -HadamardProduct._eval_derivative_matrix_lines{   s   RWC&/		&:I&:FAcggajaa&:
IC		$3I3q56*J		#<<Q?A$z'=?E'H&/&9P&9daTZZ]a=O&9H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   G8G8G>&G>rN   )__name__
__module____qualname____firstlineno____doc__is_HadamardProductr3   propertyr;   r@   rJ   r   rf   rw   __static_attributes____classdictcell____classcell__r7   __classdict__s   @@r   r   r   )   sY     * U $ . " "GA" #' 'r   r   c                   \        R \        4      p\        V4      pV! V 4      p \        R \        R 4      4      pV! V 4      p R p\        R V4      pV! V 4      p \	        V \
        4      '       dn   \        V P                  4      p. pVP                  4        F9  w  rgV^8X  d   VP                  V4       K  VP                  \        Wg4      4       K;  	  \        V!  p \        R \        \        4      4      pV! V 4      p \        V 4      p V # )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                 "    \        V \        4      # r#   r$   r   rc   s   &r   <lambda>canonicalize.<locals>.<lambda>       jO4r   c                 "    \        V \        4      # r#   r   r   s   &r   r   r     r   r   c                 "    \        V \        4      # r#   )r$   r   r   s   &r   r   r     s    Jq)4r   c                     \         ;QJ d&    R  V P                   4       F  '       g   K   RM	  RM! R  V P                   4       4      '       d   \        V P                  !  # V # )c              3   B   "   T F  p\        V\        4      x  K  	  R # 5ir#   )r$   r   )r%   cs   & r   r'   /canonicalize.<locals>.absorb.<locals>.<genexpr>
  s     9&Qz!Z((&r)   TF)anyr5   r   r;   r   s   &r   absorbcanonicalize.<locals>.absorb	  s?    39!&&93339!&&999qww''Hr   c                 "    \        V \        4      # r#   r   r   s   &r   r   r     r   r   c                 "    \        V \        4      # r#   r   r   s   &r   r   r   #  r   r   )r   r   r   r   r$   r   r   r5   itemsrb   HadamardPowerr   r   r   )rc   rulefunr   tallynew_argbaseexps   &       r   rY   rY      s   f 4
D $-CAA 445
C 	AA
 4
C 	AA !_%%IDaxt$}T78	 ' W% 4!"
C 	AA 	q	AHr   c                     \        V 4      p \        V4      pV^8X  d   V # V P                  '       g	   W,          # VP                  '       d   \        R4      h\        W4      # )   z#cannot raise expression to a matrix)r   	is_Matrixr/   r   )r   r   s   &&r   hadamard_powerr   -  sQ    4=D
#,C
ax>>>y
}}}>??##r   c                   ~   a a ] tR tRt oRtV 3R lt]R 4       t]R 4       t]R 4       t	R t
R tR	 tR
 tRtVtV ;t# )r   i9  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                  < \        V4      p\        V4      pVP                  '       d   VP                  '       d	   W,          # \        V\        4      '       d"   \        V\        4      '       d   \	        W4       \
        SV `  WV4      pV# r#   )r   	is_scalarr$   r
   r1   r2   r3   )r4   r   r   r6   r7   s   &&& r   r3   HadamardPower.__new__r  sd    t}cl>>>cmmm;dJ''JsJ,G,GTgoc-
r   c                (    V P                   ^ ,          # r:   _argsr<   s   &r   r   HadamardPower.base      zz!}r   c                (    V P                   ^,          # )r   r   r<   s   &r   r   HadamardPower.exp  r   r   c                    V P                   P                  '       d   V P                   P                  # V P                  P                  # r#   )r   r   r;   r   r<   s   &r   r;   HadamardPower.shape  s.    9999??"xx~~r   c                   V P                   pV P                  pVP                  '       d   VP                  ! W3/ VB pM/VP                  '       d   TpM\        R P                  V4      4      hVP                  '       d   VP                  ! W3/ VB pWg,          # VP                  '       d   TpWg,          # \        RP                  V4      4      h)z)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=   rA   rB   rC   r   r   abs   &&&,    r   r@   HadamardPower._entry  s    yyhh>>>A+F+A^^^A;BB4HJ J ===

1*6*A v ]]]A
 v ?FFsKM Mr   c                Z    ^ RI Hp \        V! V P                  4      V P                  4      # rF   )rH   rG   r   r   r   rI   s   & r   rJ   HadamardPower._eval_transpose  s    BYtyy1488<<r   c                    V P                   P                  V4      pV P                  P                  \        4      pVP                  V4      p\        W#,          V P                   V,          ,           V 4      # r#   )r   ra   r   	applyfuncr	   r   )r=   rc   dexplogbasedlbases   &&   r   rf   HadamardPower._eval_derivative  sW    xx}}Q))%%c*aL488F?*
 	
r   c                z   ^ RI Hp ^ RI Hp ^ RIHp V P
                  P                  V4      pV EF  pRR.p\        V4       UU	u. uF+  w  rV P
                  P                  V,          ^8w  g   K)  V	NK-  	  ppp	VP                  VP                  ,          p
VP                  VP                  ,          p\        V\        V\        WJ.4      V P                  \        V P
                  V P                  ^,
          4      ,          \        WK.4      .4      .VOVP                  R7      pVP                   ^ ,          P                   ^ ,          P                   Vn        ^ Vn        ^ Vn
        VP                   ^ ,          P                   ^,          P                   Vn        ^ Vn        ^ Vn        V.Vn	        EK  	  V# u up	pi )r   rk   ri   rm   )	validator)r   ro   rp   )rs   rl   rj   rt   rn   r   rw   ru   r;   rx   ry   rz   r   r   r   	_validater5   r{   r|   r}   r~   )r=   rc   rl   rj   rn   lrrA   r   rB   r   r   r   r   s   &&           r   rw   +HadamardPower._eval_derivative_matrix_lines  ss   WRCYY44Q7A'H&/&9U&9daTYY__Q=OST=T&9HU!--.B!../B!*'d; HH^DIItxxz%JJ'd;	 	 (11G '.ll1o&:&:1&=&B&BA#%&A""#A'.||A';';A'>'C'CA$&'A##$A yAH3 4 	1 Vs   &F71F7rN   )r   r   r   r   r   r3   r   r   r   r;   r@   rJ   rf   rw   r   r   r   r   s   @@r   r   r   9  se     6p      
,=
   r   r   N)#collectionsr   
sympy.corer   r   sympy.core.addr   sympy.core.exprr   sympy.core.sortingr   &sympy.functions.elementary.exponentialr	   rt   r
   !sympy.matrices.expressions._shaper   r1   "sympy.matrices.expressions.specialr   r   sympy.strategiesr   r   r   r   r   r   sympy.utilities.exceptionsr   r   r   rY   r   r   rN   r   r   <module>r      s^     #  ' / 6 9 Q D  A-0yj y|CL	$WJ Wr   