
    sg_                     n    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y	)
    )ExprBuilder)FunctionFunctionClassLambda)Dummy)sympify_sympify)
MatrixExpr)
MatrixBasec                   j    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y)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                 n   t        |      }|j                  st        dj                  |            |j                  dk(  r ||      }t        |t              r|S t        |t        t        f      st        d      }t        | ||            }t        |      }t        |t        t        f      st        dj                  |            d|j                  vrt        dj                  |            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         W/var/www/html/venv/lib/python3.12/site-packages/sympy/matrices/expressions/applyfunc.pyr   z ElementwiseApplyFunction.__new__2   s   ~~~<CCDIJJ:: 4.C#z*
(]F$;<c
Aa!-H8$(]F$;<F!# # HNN":AA(KM M (F+c
Aa!-H  h5
    c                      | j                   d   S )Nr   argsselfs    r   r   z!ElementwiseApplyFunction.functionS       yy|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    yyr   c                     |j                  dd      } j                  |r j                  di | j                  }t	        |t
              r|j                  rS t	        t              rj                   j                        S t	        t              r* t         fdj                        j                  di |S  S )NdeepTc                 D    j                  j                  |             S r'   )r   )xr   r#   s    r   <lambda>z/ElementwiseApplyFunction.doit.<locals>.<lambda>l   s    $--a(89 r    )
getr   doitr   r   r   is_identityr   	applyfuncr   )r#   hintsr)   r   r   s   `   @r   r/   zElementwiseApplyFunction.doit_   s    yy&yy499%u%D==h'H,@,@KdJ'>>$--0067+9		 d  
 Kr   c                 \    | j                   | j                  j                  ||fi |      S r'   )r   r   _entry)r#   ijkwargss       r   r4   zElementwiseApplyFunction._entryr   s*    }}-TYY--a=f=>>r   c                     t        d      }| j                  |      }|j                  |      }t        |t              rt        |      }|S t        ||      }|S )Nr   )r   r   diffr   r   typer   )r#   r   r   fdiffs       r   _get_function_fdiffz,ElementwiseApplyFunction._get_function_fdiffu   sT    #J==#a eX&KE  1e$Er   c                     ddl m} | j                  j                  |      }| 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   sC    H		q!((*$UDII6
 	
r   c                    ddl m} ddlm} ddlm} ddlm} | j                         }| j                  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
|
_         |S |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RW((*YY44Q7)%;<zz!})H ,??D#DJJqM2D#DJJqM2D++D%!#. & $ $ #+
 ,55 $9*1,,q/*>*>')*&+2<<?+?+?(*+'7,h 	-  %''"4::a=1"4::a=1%$#.!7FD'B "! /88 +2,,q/*>*>')*&+2<<?+?+?(*+'#9+%, 	r   c                     ddl m} | j                  | j                   || j                        j                               S )Nr   )	Transpose)$sympy.matrices.expressions.transposera   funcr   r   r/   )r#   ra   s     r   _eval_transposez(ElementwiseApplyFunction._eval_transpose   s,    Byy	$))(<(A(A(CDDr   N)__name__
__module____qualname____doc__r   propertyr   r   r   r/   r4   r<   rA   rO   rd   r-   r   r   r   r   	   sk    &PB      &?
@DEr   r   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   <module>rp      s*    ' A A # 0 1 0CEz CEr   