o
    §ƒðj_  ã                   @   sh   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„ deƒZd	S )
é    )ÚExprBuilder)ÚFunctionÚFunctionClassÚLambda)ÚDummy)ÚsympifyÚ_sympify)Ú
MatrixExpr)Ú
MatrixBasec                   @   sl   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd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                 C   sÎ   t |ƒ}|jstd |¡ƒ‚|jdkr||ƒ}t|tƒr|S t|ttfƒs0t	dƒ}t|||ƒƒ}t
|ƒ}t|ttfƒsBtd |¡ƒ‚d|jvrNtd |¡ƒ‚t|tƒs^t	dƒ}t|||ƒƒ}t | ||¡}|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   Úobj© r   ú¦/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/matrices/expressions/applyfunc.pyr   2   s2   

þ
ÿ
z ElementwiseApplyFunction.__new__c                 C   ó
   | j d S )Nr   ©Úargs©Úselfr   r   r   r   S   ó   
z!ElementwiseApplyFunction.functionc                 C   r   )Nr   r   r   r   r   r   r   W   r!   zElementwiseApplyFunction.exprc                 C   s   | j jS ©N)r   r   r   r   r   r   r   [   s   zElementwiseApplyFunction.shapec                    s†   |  dd¡}ˆj‰ |rˆ jdi |¤Ž‰ ˆj}t|tƒr |jr ˆ S tˆ tƒr+ˆ  ˆj¡S tˆ t	ƒrAt	‡ ‡fdd„ˆ jƒjdi |¤ŽS ˆS )NÚdeepTc                    s   ˆ  ˆ   | ¡¡S r"   )r   )Úx©r   r    r   r   Ú<lambda>l   s    z/ElementwiseApplyFunction.doit.<locals>.<lambda>r   )
Úgetr   Údoitr   r   r   Úis_identityr
   Ú	applyfuncr   )r    Úhintsr#   r   r   r%   r   r(   _   s&   

þýýzElementwiseApplyFunction.doitc                 K   s   |   | jj||fi |¤Ž¡S r"   )r   r   Ú_entry)r    ÚiÚjÚkwargsr   r   r   r,   r   s   zElementwiseApplyFunction._entryc                 C   s@   t dƒ}|  |¡}| |¡}t|tƒrt|ƒ}|S t||ƒ}|S )Nr   )r   r   Údiffr   r   Útyper   )r    r   r   Úfdiffr   r   r   Ú_get_function_fdiffu   s   



ÿz,ElementwiseApplyFunction._get_function_fdiffc                 C   s2   ddl m} | j |¡}|  ¡ }||t|| jƒƒS )Nr   )Úhadamard_product)Ú#sympy.matrices.expressions.hadamardr4   r   r0   r3   r   )r    r$   r4   Údexprr2   r   r   r   Ú_eval_derivative   s   
þz)ElementwiseApplyFunction._eval_derivativec              
   C   sˆ  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 ]E}
|	rE|
j}|| j
d ƒ}n
|| j
d ƒ}|
j}t|t||||gƒ|	r\dndg|jd	�}|g|
_|jd j|
_d|
_|jd j|
_d
|
_q6|S |D ]A}
|
j}|
j}||j
d ƒ}||j
d ƒ}t|t||||||gƒddg|jd	�}|jd j|
_d|
_|jd j|
_d|
_|g|
_q€|S )Nr   )ÚIdentity)ÚArrayContraction)ÚArrayDiagonal)ÚArrayTensorProductr   )r   é   )r   é   )Ú	validatorr<   )r   r<   r=   )é   é   é   r=   )Ú"sympy.matrices.expressions.specialr8   Ú0sympy.tensor.array.expressions.array_expressionsr9   r:   r;   r3   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$   r8   r9   r:   r;   r2   ÚlrÚewdiffÚiscolumnr-   Úptr1Úptr2ÚsubexprÚnewptr1Únewptr2r   r   r   rD   ˆ   sp   
ýþ
÷óêþúö
z6ElementwiseApplyFunction._eval_derivative_matrix_linesc                 C   s$   ddl m} |  | j|| jƒ ¡ ¡S )Nr   )Ú	Transpose)Ú$sympy.matrices.expressions.transposerU   Úfuncr   r   r(   )r    rU   r   r   r   Ú_eval_transposeÊ   s   z(ElementwiseApplyFunction._eval_transposeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r(   r,   r3   r7   rD   rX   r   r   r   r   r   	   s    (!



	Br   N)Úsympy.core.exprr   Úsympy.core.functionr   r   r   Úsympy.core.symbolr   Úsympy.core.sympifyr   r   Úsympy.matrices.expressionsr	   Úsympy.matrices.matrixbaser
   r   r   r   r   r   Ú<module>   s    