o
    oh_                     @   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   x/var/www/html/construction_image-detection-poc/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    