U
    ZÊ¹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ƒs`t	dƒ}t|||ƒƒ}t
|ƒ}t|ttfƒs„td |¡ƒ‚d|jkrœt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   Z	is_MatrixÚ
ValueErrorÚformatÚshapeÚ
isinstancer	   r   r   r   r   ÚnargsÚ__new__)ÚclsÚfunctionÚexprÚretr   Úobj© r   úc/var/www/html/bwave/python/venv/lib/python3.8/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 |Ž‰ ˆj}t|tƒr<|jr<ˆ S tˆ tƒrRˆ  ˆj¡S tˆ t	ƒrzt	‡ ‡fdd„ˆ jƒjf |Ž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   Úhintsr    r   r   r"   r   r&   _   s"    

þýzElementwiseApplyFunction.doitc                 K   s   |   | jj||f|Ž¡S r   )r   r   Ú_entry)r   ÚiÚjÚkwargsr   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.hadamardr0   r   r,   r/   r   )r   r!   r0   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
krú| j
d dk}	|D ]Š}
|	rŠ|
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
|
_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   é   )Ú	validatorr6   )r   r6   r7   )é   é   é   r7   )Z"sympy.matrices.expressions.specialr2   Z0sympy.tensor.array.expressions.array_expressionsr3   r4   r5   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!   r2   r3   r4   r5   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/   r1   r<   rB   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.matrixbaser
   r   r   r   r   r   Ú<module>   s   