
    Ed]                     v    d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ  G d de          Zd	S )
    )ExprBuilder)FunctionFunctionClassLambda)Dummy)sympify_sympify)
MatrixExpr)
MatrixBasec                   ~    e Zd ZdZd Zed             Zed             Zed             Zd Z	d Z
d Zd	 Zd
 Zd ZdS )ElementwiseApplyFunctionag  
    Apply function to a matrix elementwise without evaluating.

    Examples
    ========

    It can be created by calling ``.applyfunc(<function>)`` on a matrix
    expression:

    >>> from sympy import MatrixSymbol
    >>> from sympy.matrices.expressions.applyfunc import ElementwiseApplyFunction
    >>> from sympy import exp
    >>> X = MatrixSymbol("X", 3, 3)
    >>> X.applyfunc(exp)
    Lambda(_d, exp(_d)).(X)

    Otherwise using the class constructor:

    >>> from sympy import eye
    >>> expr = ElementwiseApplyFunction(exp, eye(3))
    >>> expr
    Lambda(_d, exp(_d)).(Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]]))
    >>> expr.doit()
    Matrix([
    [E, 1, 1],
    [1, E, 1],
    [1, 1, E]])

    Notice the difference with the real mathematical functions:

    >>> exp(eye(3))
    Matrix([
    [E, 0, 0],
    [0, E, 0],
    [0, 0, E]])
    c                    t          |          }|j        s"t          d                    |                    |j        dk    r" ||          }t          |t                    r|S t          |t          t          f          s(t          d          }t          | ||                    }t          |          }t          |t          t          f          s"t          d                    |                    d|j        vr"t          d                    |                    t          |t                    s(t          d          }t          | ||                    }t          j        | ||          }|S )Nz{} must be a matrix instance.)   r   dz4{} should be compatible with SymPy function classes.r   z({} should be able to accept 1 arguments.)r	   	is_Matrix
ValueErrorformatshape
isinstancer
   r   r   r   r   nargs__new__)clsfunctionexprretr   objs         Dlib/python3.11/site-packages/sympy/matrices/expressions/applyfunc.pyr   z ElementwiseApplyFunction.__new__2   sp   ~~~ 	K<CCDIIJJJ: 	 (4..C#z** 
(]F$;<< 	.c

Aa!--H8$$(]F$;<< 	#F!!# # # HN" 	M:AA(KKM M M (F++ 	.c

Aa!--H h55
    c                     | j         d         S )Nr   argsselfs    r   r   z!ElementwiseApplyFunction.functionS       y|r   c                     | j         d         S )Nr   r    r"   s    r   r   zElementwiseApplyFunction.exprW   r$   r   c                     | j         j        S N)r   r   r"   s    r   r   zElementwiseApplyFunction.shape[   s    yr   c                 |    |                     dd          } j        |r j        di | j        }t	          |t
                    r	|j        rS t	          t                    r                     j                  S t	          t                    r$ t           fdj                  j        di |S  S )NdeepTc                 T                                              |                     S r'   )r   )xr   r#   s    r   <lambda>z/ElementwiseApplyFunction.doit.<locals>.<lambda>l   s    $--a(8(899 r    )
getr   doitr   r   r   is_identityr   	applyfuncr   )r#   hintsr)   r   r   s   `   @r   r/   zElementwiseApplyFunction.doit_   s    yy&&y 	&49%%u%%D=h'' 	H,@ 	KdJ'' 	>>$-000677 	+99999	      
 Kr   c                 P    |                       | j        j        ||fi |          S r'   )r   r   _entry)r#   ijkwargss       r   r4   zElementwiseApplyFunction._entryr   s.    }}-TY-a==f==>>>r   c                     t          d          }|                     |          }|                    |          }t          |t                    rt          |          }nt          ||          }|S )Nr   )r   r   diffr   r   typer   )r#   r   r   fdiffs       r   _get_function_fdiffz,ElementwiseApplyFunction._get_function_fdiffu   sb    #JJ==##a  eX&& 	%KKEE1e$$Er   c                     ddl m} | j                            |          }|                                 } ||t          || j                            S )Nr   )hadamard_product)#sympy.matrices.expressions.hadamardr>   r   r9   r<   r   )r#   r+   r>   dexprr;   s        r   _eval_derivativez)ElementwiseApplyFunction._eval_derivative   s_    HHHHHH	q!!((**$UDI66
 
 	
r   c                    ddl m} ddlm} ddlm} ddlm} |                                 }| j                            |          }t          || j                  }d|j
        v r| j
        d         dk    }	|D ]}
|	r|
j        } || j
        d                   }n || j
        d                   }|
j        }t          |t          ||||g          |	rdndg|j        	          }|g|
_        |j        d         j        |
_        d|
_        |j        d         j        |
_        d
|
_        n|D ]}
|
j        }|
j        } ||j
        d                   } ||j
        d                   }t          |t          ||||||g          ddg|j        	          }|j        d         j        |
_        d|
_        |j        d         j        |
_        d|
_        |g|
_        |S )Nr   )Identity)ArrayContraction)ArrayDiagonal)ArrayTensorProductr   )r      )r      )	validatorrG   )r   rG   rH   )         rH   )"sympy.matrices.expressions.specialrC   0sympy.tensor.array.expressions.array_expressionsrD   rE   rF   r<   r   _eval_derivative_matrix_linesr   r   first_pointersecond_pointerr   	_validate_linesr!   _first_pointer_parent_first_pointer_index_second_pointer_parent_second_pointer_index)r#   r+   rC   rD   rE   rF   r;   lrewdiffiscolumnr5   ptr1ptr2subexprnewptr1newptr2s                   r   rO   z6ElementwiseApplyFunction._eval_derivative_matrix_lines   s^   ??????UUUUUURRRRRRWWWWWW((**Y44Q77)%;;< 6	%z!})H , , ,?D#8DJqM22DD#8DJqM22D+D%!#. & $ $  #+6
 ,5   $9*1,q/*>')*&+2<?+?(*+''7,<  % %'"(4:a=11"(4:a=11%$#.!7FD'B  "! /8   +2,q/*>')*&+2<?+?(*+'#9	r   c                     ddl m} |                     | j         || j                                                            S )Nr   )	Transpose)$sympy.matrices.expressions.transposera   funcr   r   r/   )r#   ra   s     r   _eval_transposez(ElementwiseApplyFunction._eval_transpose   sB    BBBBBByy		$)(<(<(A(A(C(CDDDr   N)__name__
__module____qualname____doc__r   propertyr   r   r   r/   r4   r<   rA   rO   rd   r-   r   r   r   r   	   s        & &P  B   X   X   X  &? ? ?  
 
 
@ @ @DE E E E Er   r   N)sympy.core.exprr   sympy.core.functionr   r   r   sympy.core.symbolr   sympy.core.sympifyr   r	   sympy.matrices.expressionsr
   sympy.matrices.matricesr   r   r-   r   r   <module>rp      s    ' ' ' ' ' ' A A A A A A A A A A # # # # # # 0 0 0 0 0 0 0 0 1 1 1 1 1 1 . . . . . .CE CE CE CE CEz CE CE CE CE CEr   