a
    <bY                     @   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s`t	d}t|||}t
|}t|ttfstd|d|jvrtd|t|tst	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   Z	is_Matrix
ValueErrorformatshape
isinstancer	   r   r   r   r   nargs__new__)clsfunctionexprretr   obj r   t/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/matrices/expressions/applyfunc.pyr   2   s4    



z ElementwiseApplyFunction.__new__c                 C   s
   | j d S )Nr   argsselfr   r   r   r   S   s    z!ElementwiseApplyFunction.functionc                 C   s
   | j d S )Nr   r   r   r   r   r   r   W   s    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f i | j}t|tr@|jr@ S t trV jS t t	rzt	 fdd j S S d S )NdeepTc                    s      | S r   )r   )xr   r   r   r   <lambda>l       z/ElementwiseApplyFunction.doit.<locals>.<lambda>)
getr   doitr   r   r   Zis_identityr
   Z	applyfuncr   )r   kwargsr    r   r   r"   r   r&   _   s    

zElementwiseApplyFunction.doitc                 K   s   |  | jj||fi |S r   )r   r   _entry)r   ijr'   r   r   r   r(   r   s    zElementwiseApplyFunction._entryc                 C   s>   t d}| |}||}t|tr0t|}n
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)Z#sympy.matrices.expressions.hadamardr/   r   r+   r.   r   )r   r!   r/   Zdexprr-   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 ]}
|	r|
j}|| j
d }n|| j
d }|
j}t|t||||g|	rdndg|jd	}|g|
_|jd j|
_d|
_|jd j|
_d
|
_qln|D ]}
|
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      )Z	validatorr5   )r   r5   r6   )         r6   )Z"sympy.matrices.expressions.specialr1   Z0sympy.tensor.array.expressions.array_expressionsr2   r3   r4   r.   r   _eval_derivative_matrix_linesr   r   Zfirst_pointerZsecond_pointerr   	_validate_linesr   Z_first_pointer_parentZ_first_pointer_indexZ_second_pointer_parentZ_second_pointer_index)r   r!   r1   r2   r3   r4   r-   lrZewdiffZiscolumnr)   Zptr1Zptr2ZsubexprZnewptr1Znewptr2r   r   r   r:      sn    



z6ElementwiseApplyFunction._eval_derivative_matrix_linesc                 C   s$   ddl m} | | j|| j S )Nr   )	Transpose)Z$sympy.matrices.expressions.transposer>   funcr   r   r&   )r   r>   r   r   r   _eval_transpose   s    z(ElementwiseApplyFunction._eval_transposeN)__name__
__module____qualname____doc__r   propertyr   r   r   r&   r(   r.   r0   r:   r@   r   r   r   r   r   	   s   (!



	Br   N)Zsympy.core.exprr   Zsympy.core.functionr   r   r   Zsympy.core.symbolr   Zsympy.core.sympifyr   r   Zsympy.matrices.expressionsr	   Zsympy.matrices.matricesr
   r   r   r   r   r   <module>   s   