U
    L?h                     @   sh   d dl mZ ddl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mZ G dd	 d	eZd
S )   )
MatrixExpr    )FunctionClassLambda)Dummy)_sympifysympify)Matrix)reimc                       sP   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dd Z
  ZS )FunctionMatrixa  Represents a matrix using a function (``Lambda``) which gives
    outputs according to the coordinates of each matrix entries.

    Parameters
    ==========

    rows : nonnegative integer. Can be symbolic.

    cols : nonnegative integer. Can be symbolic.

    lamda : Function, Lambda or str
        If it is a SymPy ``Function`` or ``Lambda`` instance,
        it should be able to accept two arguments which represents the
        matrix coordinates.

        If it is a pure string containing Python ``lambda`` semantics,
        it is interpreted by the SymPy parser and casted into a SymPy
        ``Lambda`` instance.

    Examples
    ========

    Creating a ``FunctionMatrix`` from ``Lambda``:

    >>> from sympy import FunctionMatrix, symbols, Lambda, MatPow
    >>> i, j, n, m = symbols('i,j,n,m')
    >>> FunctionMatrix(n, m, Lambda((i, j), i + j))
    FunctionMatrix(n, m, Lambda((i, j), i + j))

    Creating a ``FunctionMatrix`` from a SymPy function:

    >>> from sympy import KroneckerDelta
    >>> X = FunctionMatrix(3, 3, KroneckerDelta)
    >>> X.as_explicit()
    Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]])

    Creating a ``FunctionMatrix`` from a SymPy undefined function:

    >>> from sympy import Function
    >>> f = Function('f')
    >>> X = FunctionMatrix(3, 3, f)
    >>> X.as_explicit()
    Matrix([
    [f(0, 0), f(0, 1), f(0, 2)],
    [f(1, 0), f(1, 1), f(1, 2)],
    [f(2, 0), f(2, 1), f(2, 2)]])

    Creating a ``FunctionMatrix`` from Python ``lambda``:

    >>> FunctionMatrix(n, m, 'lambda i, j: i + j')
    FunctionMatrix(n, m, Lambda((i, j), i + j))

    Example of lazy evaluation of matrix product:

    >>> Y = FunctionMatrix(1000, 1000, Lambda((i, j), i + j))
    >>> isinstance(Y*Y, MatPow) # this is an expression object
    True
    >>> (Y**2)[10,10] # So this is evaluated lazily
    342923500

    Notes
    =====

    This class provides an alternative way to represent an extremely
    dense matrix with entries in some form of a sequence, in a most
    sparse way.
    c                    s   t |t | }}| | | | t|}t|ttfsJtd|d|jkrbtd|t|tst	dt	d }}t||f|||}t
 | |||S )Nz4{} should be compatible with SymPy function classes.   z({} should be able to accept 2 arguments.ij)r   Z
_check_dimr   
isinstancer   r   
ValueErrorformatnargsr   super__new__)clsrowscolslamdar   r   	__class__ W/var/www/html/venv/lib/python3.8/site-packages/sympy/matrices/expressions/funcmatrix.pyr   P   s$    



zFunctionMatrix.__new__c                 C   s   | j dd S )Nr   r   argsselfr   r   r   shapee   s    zFunctionMatrix.shapec                 C   s
   | j d S )Nr   r   r    r   r   r   r   i   s    zFunctionMatrix.lamdac                 K   s   |  ||S N)r   )r!   r   r   kwargsr   r   r   _entrym   s    zFunctionMatrix._entryc                 C   s*   ddl m} ddlm} || | S )Nr   )Trace)Sum)Z sympy.matrices.expressions.tracer&   Zsympy.concrete.summationsr'   ZrewriteZdoit)r!   r&   r'   r   r   r   _eval_tracep   s    zFunctionMatrix._eval_tracec                 C   s   t t| tt| fS r#   )r
   r	   r   r    r   r   r   _eval_as_real_imagu   s    z!FunctionMatrix._eval_as_real_imag)__name__
__module____qualname____doc__r   propertyr"   r   r%   r(   r)   __classcell__r   r   r   r   r   	   s   F

r   N)Zmatexprr   Zsympy.core.functionr   r   Zsympy.core.symbolr   Zsympy.core.sympifyr   r   Zsympy.matricesr	   Z$sympy.functions.elementary.complexesr
   r   r   r   r   r   r   <module>   s   