
    \h`6                         S SK Jr  S SKJrJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S S	KJrJr  S S
KJrJrJrJrJrJr  S SKJr  S r " S S\5      rS r S r! " S S\5      r"g)    )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                  z    U (       d  [        S5      e[        U 5      S:X  a  U S   $ [        U 6 R                  5       $ )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   r   )	TypeErrorlenHadamardProductdoit)matricess    [/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/expressions/hadamard.pyhadamard_productr      s=    " =>>
8}{H%**,,    c                   h   ^  \ rS rSrSrSrSSS.U 4S jjr\S 5       rS	 r	S
 r
S rS rS rSrU =r$ )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                6  > [        [        [        U5      5      n[        U5      S:X  a  [	        S5      e[        S U 5       5      (       d  [        S5      eUb  [        SSSS9  US	La  [        U6   [        TU ](  " U /UQ76 nU(       a  UR                  S	S
9nU$ )Nr   z+HadamardProduct needs at least one argumentc              3   B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancer
   ).0args     r   	<genexpr>*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   r5   HadamardProduct.__new__A   s    C&'t9>JKK?$???>??%|)/+Y[
 dOgoc)D)(((&C
r   c                 4    U R                   S   R                  $ Nr   )r7   shapeselfs    r   r=   HadamardProduct.shapeX   s    yy|!!!r   c           
      r    [        U R                   Vs/ s H  oDR                  " X40 UD6PM     sn6 $ s  snf r%   )r   r7   _entry)r?   ijkwargsr(   s        r   rB   HadamardProduct._entry\   s/    499E9CZZ//9EFFEs   4c                 V    SSK Jn  [        [        [	        XR
                  5      5      6 $ Nr   )	transpose)$sympy.matrices.expressions.transposerI   r   r/   r0   r7   r?   rI   s     r   _eval_transposeHadamardProduct._eval_transpose_   s    BSII%> ?@@r   c                   ^ U R                   " U4S jU R                   5       6 nSSKJn  SSKJn  UR                   Vs/ s H  n[        XS5      (       d  M  UPM     nnU(       ay  UR                   Vs/ s H  oUU;  d  M
  UPM     nnU" [        U6  Vs/ s H  n[        R                  " U5      PM     sn5      R                  " U R                  6 n[        U/U-   6 n[        U5      $ s  snf s  snf s  snf )Nc              3   F   >#    U  H  oR                   " S0 TD6v   M     g 7f)N )r   )r'   rC   hintss     r   r)   'HadamardProduct.doit.<locals>.<genexpr>d   s     >Iq66?E?Is   !r   )
MatrixBase)ImmutableMatrix)funcr7   sympy.matrices.matrixbaserS   sympy.matrices.immutablerT   r&   zipr   fromiterreshaper=   r   canonicalize)	r?   rQ   exprrS   rT   rC   explicit	remainderexpl_mats	    `       r   r   HadamardProduct.doitc   s    yy>DII>?8<#yyFy!Jq,EAyF$(IICIq(1BIIC&),h()7AQ( w

$H #hZ)%;=DD!! GC(s   C*C*4	C/C/ C4c                    / n[        U R                  5      n[        [        U5      5       H<  nUS U X4   R	                  U5      /-   X4S-   S  -   nUR                  [        U6 5        M>     [        R                  " U5      $ Nr   )	r/   r7   ranger   diffappendr   r   rY   )r?   xtermsr7   rC   factorss         r   _eval_derivative HadamardProduct._eval_derivatives   st    DIIs4y!A2Ah$',,q/!22TA#$Z?GLL)734 " ||E""r   c                    SSK Jn  SSK Jn  SSKJn  [        U R                  5       VVs/ s H  u  pVUR                  U5      (       d  M  UPM!     nnn/ nU GHp  n	U R                  S U	 n
U R                  U	S-   S  nU R                  U	   R                  U5      n[        X-   6 nSS/n[        U5       VVs/ s H  u  nnU R                  U   S:w  d  M  UPM     nnnU H  nUR                  UR                     nUR                  UR                     n[        U[        U[        UU/5      U[        UU/5      /5      /UQ5      nUR                  S   R                  S   R                  Ul        SUl        UR                  S   R                  S   R                  Ul        SUl        U/Ul        UR'                  U5        M     GMs     U$ s  snnf s  snnf )	Nr   ArrayDiagonalArrayTensorProduct_make_matrixr   )r            rr   )0sympy.tensor.array.expressions.array_expressionsrm   ro   "sympy.matrices.expressions.matexprrq   	enumerater7   has_eval_derivative_matrix_linesr   r=   _lines_first_line_index_second_line_indexr   _first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_indexre   )r?   rf   rm   ro   rq   rC   r(   
with_x_indlinesind	left_args
right_argsdhadamdiagonalrD   el1l2subexprs                       r   rz   -HadamardProduct._eval_derivative_matrix_lines{   s   RWC&/		&:I&:FAcggaja&:
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   GG7GGrP   )__name__
__module____qualname____firstlineno____doc__is_HadamardProductr5   propertyr=   rB   rL   r   ri   rz   __static_attributes____classcell__r9   s   @r   r   r   )   sS    * %*$  . " "GA" #' 'r   r   c                    [        S [        5      n[        U5      nU" U 5      n [        S [        S 5      5      nU" U 5      n S n[        S U5      nU" U 5      n [	        U [
        5      (       ak  [        U R                  5      n/ nUR                  5        H8  u  pgUS:X  a  UR                  U5        M  UR                  [        Xg5      5        M:     [        U6 n [        S [        [        5      5      nU" U 5      n [        U 5      n U $ )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                 "    [        U [        5      $ r%   r&   r   rf   s    r   <lambda>canonicalize.<locals>.<lambda>       jO4r   c                 "    [        U [        5      $ r%   r   r   s    r   r   r     r   r   c                 "    [        U [        5      $ r%   )r&   r   r   s    r   r   r     s    Jq)4r   c                 l    [        S U R                   5       5      (       a  [        U R                  6 $ U $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr%   )r&   r   )r'   cs     r   r)   /canonicalize.<locals>.absorb.<locals>.<genexpr>
  s     9&Qz!Z((&r+   )anyr7   r   r=   r   s    r   absorbcanonicalize.<locals>.absorb	  s+    9!&&999qww''Hr   c                 "    [        U [        5      $ r%   r   r   s    r   r   r     r   r   r   c                 "    [        U [        5      $ r%   r   r   s    r   r   r   #  r   r   )r   r   r   r   r&   r   r   r7   itemsre   HadamardPowerr   r   r   )rf   rulefunr   tallynew_argbaseexps           r   r[   r[      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                     [        U 5      n [        U5      nUS:X  a  U $ U R                  (       d  X-  $ UR                  (       a  [        S5      e[        X5      $ )Nr   z#cannot raise expression to a matrix)r   	is_Matrixr1   r   )r   r   s     r   hadamard_powerr   -  sM    4=D
#,C
ax>>y
}}>??##r   c                   t   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	S r
S rS	 rS
 rSrU =r$ )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                 
  > [        U5      n[        U5      nUR                  (       a  UR                  (       a  X-  $ [        U[        5      (       a   [        U[        5      (       a  [	        X5        [
        TU ]  XU5      nU$ r%   )r   	is_scalarr&   r
   r3   r4   r5   )r6   r   r   r8   r9   s       r   r5   HadamardPower.__new__r  s`    t}cl>>cmm;dJ''JsJ,G,GTgoc-
r   c                      U R                   S   $ r<   _argsr>   s    r   r   HadamardPower.base      zz!}r   c                      U R                   S   $ rb   r   r>   s    r   r   HadamardPower.exp  r   r   c                     U R                   R                  (       a  U R                   R                  $ U R                  R                  $ r%   )r   r   r=   r   r>   s    r   r=   HadamardPower.shape  s+    9999??"xx~~r   c                    U R                   nU R                  nUR                  (       a  UR                  " X40 UD6nO.UR                  (       a  UnO[        SR                  U5      5      eUR                  (       a  UR                  " X40 UD6nXg-  $ UR                  (       a  UnXg-  $ [        SR                  U5      5      e)Nz)The base {} must be a scalar or a matrix.z-The exponent {} must be a scalar or a matrix.)r   r   r   rB   r   r1   format)r?   rC   rD   rE   r   r   abs           r   rB   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    SSK Jn  [        U" U R                  5      U R                  5      $ rH   )rJ   rI   r   r   r   rK   s     r   rL   HadamardPower._eval_transpose  s    BYtyy1488<<r   c                     U R                   R                  U5      nU R                  R                  [        5      nUR                  U5      n[        X#-  U R                   U-  -   U 5      $ r%   )r   rd   r   	applyfuncr	   r   )r?   rf   dexplogbasedlbases        r   ri   HadamardPower._eval_derivative  sW    xx}}Q))%%c*aL488F?*
 	
r   c                 ,   SSK Jn  SSK Jn  SSKJn  U R
                  R                  U5      nU GHY  nSS/n[        U5       VV	s/ s H&  u  pU R
                  R                  U   S:w  d  M$  U	PM(     nnn	UR                  UR                     n
UR                  UR                     n[        U[        U[        XJ/5      U R                  [        U R
                  U R                  S-
  5      -  [        XK/5      /5      /UQUR                  S9nUR                   S   R                   S   R                   Ul        SUl        SUl
        UR                   S   R                   S	   R                   Ul        SUl        SUl        U/Ul	        GM\     U$ s  sn	nf )
Nr   rn   rl   rp   )r   rr   rs   r   )	validatorrr   )rv   ro   rm   rw   rq   r   rz   rx   r=   r{   r|   r}   r   r   r   	_validater7   r~   r   r   r   )r?   rf   ro   rm   rq   lrrC   r   rD   r   r   r   r   s                r   rz   +HadamardPower._eval_derivative_matrix_lines  sq   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   #F-FrP   )r   r   r   r   r   r5   r   r   r   r=   rB   rL   ri   rz   r   r   r   s   @r   r   r   9  sc    6p      
,=
   r   r   N)#collectionsr   
sympy.corer   r   sympy.core.addr   sympy.core.exprr   sympy.core.sortingr   &sympy.functions.elementary.exponentialr	   rw   r
   !sympy.matrices.expressions._shaper   r3   "sympy.matrices.expressions.specialr   r   sympy.strategiesr   r   r   r   r   r   sympy.utilities.exceptionsr   r   r   r[   r   r   rP   r   r   <module>r      s^     #  ' / 6 9 Q D  A-0yj y|CL	$WJ Wr   