a
    RG5d                     @   s   d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 d dl
mZ G dd deZG dd	 d	eZG d
d deZdd ZdS )    )_sympify)
MatrixExpr)SEqGe)Mul)KroneckerDeltac                   @   s<   e Zd ZdZedd Zedd Zedd Zdd Zd	S )
DiagonalMatrixa  DiagonalMatrix(M) will create a matrix expression that
    behaves as though all off-diagonal elements,
    `M[i, j]` where `i != j`, are zero.

    Examples
    ========

    >>> from sympy import MatrixSymbol, DiagonalMatrix, Symbol
    >>> n = Symbol('n', integer=True)
    >>> m = Symbol('m', integer=True)
    >>> D = DiagonalMatrix(MatrixSymbol('x', 2, 3))
    >>> D[1, 2]
    0
    >>> D[1, 1]
    x[1, 1]

    The length of the diagonal -- the lesser of the two dimensions of `M` --
    is accessed through the `diagonal_length` property:

    >>> D.diagonal_length
    2
    >>> DiagonalMatrix(MatrixSymbol('x', n + 1, n)).diagonal_length
    n

    When one of the dimensions is symbolic the other will be treated as
    though it is smaller:

    >>> tall = DiagonalMatrix(MatrixSymbol('x', n, 3))
    >>> tall.diagonal_length
    3
    >>> tall[10, 1]
    0

    When the size of the diagonal is not known, a value of None will
    be returned:

    >>> DiagonalMatrix(MatrixSymbol('x', n, m)).diagonal_length is None
    True

    c                 C   s
   | j d S Nr   argsself r   _/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/matrices/expressions/diagonal.py<lambda>2       zDiagonalMatrix.<lambda>c                 C   s   | j jS N)argshaper   r   r   r   r   4   r   c                 C   s~   | j \}}|jr"|jr"t||}nX|jr4|js4|}nF|jrF|jsF|}n4||krT|}n&zt||}W n tyx   d }Y n0 |S r   )r   
is_Integermin	TypeErrorr   rcmr   r   r   diagonal_length6   s    

zDiagonalMatrix.diagonal_lengthc                 K   s   | j d ur:t|| j tju r"tjS t|| j tju r:tjS t||}|tju r\| j||f S |tju rltjS | j||f t|| S r   )	r   r   r   trueZeror   r   falser   )r   ijkwargseqr   r   r   _entryH   s    



zDiagonalMatrix._entryN	__name__
__module____qualname____doc__propertyr   r   r   r%   r   r   r   r   r	   	   s   (
r	   c                   @   s<   e Zd ZdZedd Zedd Zedd Zdd	 Zd
S )
DiagonalOfa  DiagonalOf(M) will create a matrix expression that
    is equivalent to the diagonal of `M`, represented as
    a single column matrix.

    Examples
    ========

    >>> from sympy import MatrixSymbol, DiagonalOf, Symbol
    >>> n = Symbol('n', integer=True)
    >>> m = Symbol('m', integer=True)
    >>> x = MatrixSymbol('x', 2, 3)
    >>> diag = DiagonalOf(x)
    >>> diag.shape
    (2, 1)

    The diagonal can be addressed like a matrix or vector and will
    return the corresponding element of the original matrix:

    >>> diag[1, 0] == diag[1] == x[1, 1]
    True

    The length of the diagonal -- the lesser of the two dimensions of `M` --
    is accessed through the `diagonal_length` property:

    >>> diag.diagonal_length
    2
    >>> DiagonalOf(MatrixSymbol('x', n + 1, n)).diagonal_length
    n

    When only one of the dimensions is symbolic the other will be
    treated as though it is smaller:

    >>> dtall = DiagonalOf(MatrixSymbol('x', n, 3))
    >>> dtall.diagonal_length
    3

    When the size of the diagonal is not known, a value of None will
    be returned:

    >>> DiagonalOf(MatrixSymbol('x', n, m)).diagonal_length is None
    True

    c                 C   s
   | j d S r
   r   r   r   r   r   r      r   zDiagonalOf.<lambda>c                 C   s   | j j\}}|jr$|jr$t||}nX|jr6|js6|}nF|jrH|jsH|}n4||krV|}n&zt||}W n tyz   d }Y n0 |tjfS r   )r   r   r   r   r   r   Oner   r   r   r   r      s    
zDiagonalOf.shapec                 C   s
   | j d S r
   )r   r   r   r   r   r      s    zDiagonalOf.diagonal_lengthc                 K   s   | j j||fi |S r   )r   r%   )r   r!   r"   r#   r   r   r   r%      s    zDiagonalOf._entryNr&   r   r   r   r   r,   V   s   +

r,   c                   @   sD   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dS )
DiagMatrixz/
    Turn a vector into a diagonal matrix.
    c                 C   sf   t |}t| |}|j}|d dkr.|d n|d }|jd dkrLd|_nd|_||f|_||_|S )Nr      TF)r   r   __new__r   	_iscolumn_shape_vector)clsvectorobjr   dimr   r   r   r0      s    
zDiagMatrix.__new__c                 C   s   | j S r   )r2   r   r   r   r   r      s    zDiagMatrix.shapec                 K   sN   | j r| jj|dfi |}n| jjd|fi |}||krJ|t||9 }|S r
   )r1   r3   r%   r   )r   r!   r"   r#   resultr   r   r   r%      s    zDiagMatrix._entryc                 C   s   | S r   r   r   r   r   r   _eval_transpose   s    zDiagMatrix._eval_transposec                 C   s   ddl m} |t| j  S )Nr   )diag)sympy.matrices.denser:   listr3   as_explicit)r   r:   r   r   r   r=      s    zDiagMatrix.as_explicitc                    s  ddl m}m} ddlm} ddlm} ddlm} ddl	m
} | j}|||rX|S t||r|t|j}	t|	jd D ]}
||
 |	|
|
f< q~t||	S |jrdd |jD   fd	d|jD }|rt|t|    S t||r|j}t|S )
Nr   )askQ)MatMul)	Transpose)eye)
MatrixBasec                 S   s   g | ]}|j r|qS r   )	is_Matrix.0r   r   r   r   
<listcomp>   r   z#DiagMatrix.doit.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   rE   matricesr   r   rG      r   )sympy.assumptionsr>   r?   !sympy.matrices.expressions.matmulr@   $sympy.matrices.expressions.transposerA   r;   rB   sympy.matrices.matricesrC   r3   diagonal
isinstancemaxr   rangetype	is_MatMulr   r   fromiterr.   doitr   )r   hintsr>   r?   r@   rA   rB   rC   r5   retr!   scalarsr   rH   r   rU      s*    
 
zDiagMatrix.doitN)r'   r(   r)   r*   r0   r+   r   r%   r9   r=   rU   r   r   r   r   r.      s   
	r.   c                 C   s   t |  S r   )r.   rU   )r5   r   r   r   diagonalize_vector   s    rY   N)sympy.core.sympifyr   sympy.matrices.expressionsr   
sympy.corer   r   r   Zsympy.core.mulr   (sympy.functions.special.tensor_functionsr   r	   r,   r.   rY   r   r   r   r   <module>   s   MG>