o
    GZŽh¸  ã                   @   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   ó
   | j d S ©Nr   ©Úargs©Úself© r   úR/var/www/auris/lib/python3.10/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   s    c                 C   s†   | j \}}|jr|jrt||ƒ}|S |jr|js|}|S |jr&|js&|}|S ||kr.|}|S zt||ƒ}W |S  tyB   d }Y |S w 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 r6tjS | j||f t||ƒ S r   )	r   r   r   ÚtrueZ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   r
   r   r   r   r   r   r   r   ‚   r   zDiagonalOf.<lambda>c                 C   s†   | j j\}}|jr|jrt||ƒ}n,|jr|js|}n#|jr$|js$|}n||kr+|}nzt||ƒ}W n ty=   d }Y nw |tjfS r   )r   r   r   r   r   r   ZOner   r   r   r   r   ƒ   s   ÿ
zDiagonalOf.shapec                 C   r
   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r&d|_nd|_||f|_||_|S )Nr   é   TF)r   r   Ú__new__r   Ú	_iscolumnÚ_shapeÚ_vector)ÚclsÚvectorÚobjr   Údimr   r   r   r/   ¡   s   
zDiagMatrix.__new__c                 C   s   | j S r   )r1   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r%|t||ƒ9 }|S r   )r0   r2   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.denser9   Úlistr2   Úas_explicit)r   r9   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}|| |¡ƒr,|S t||ƒrP|t|jƒƒ}	t|	jd ƒD ]
}
||
 |	|
|
f< q?t|ƒ|	ƒS |jrwdd„ |jD ƒ‰ ‡ fd	d„|jD ƒ}|rwt |¡t| ˆ ¡ ¡ ƒ ¡  S t||ƒr|j}t|ƒS )
Nr   )ÚaskÚQ)ÚMatMul)Ú	Transpose)Úeye)Ú
MatrixBasec                 S   s   g | ]}|j r|‘qS r   )Z	is_Matrix©Ú.0r   r   r   r   Ú
<listcomp>Ò   s    z#DiagMatrix.doit.<locals>.<listcomp>c                    s   g | ]}|ˆ vr|‘qS r   r   rC   ©Zmatricesr   r   rE   Ó   s    )Zsympy.assumptionsr=   r>   Z!sympy.matrices.expressions.matmulr?   Z$sympy.matrices.expressions.transposer@   r:   rA   Zsympy.matrices.matrixbaserB   r2   ZdiagonalÚ
isinstanceÚmaxr   ÚrangeÚtypeZ	is_MatMulr   r   Zfromiterr-   Údoitr   )r   Úhintsr=   r>   r?   r@   rA   rB   r4   Úretr!   Zscalarsr   rF   r   rK   Â   s*   
 
zDiagMatrix.doitN)r'   r(   r)   r*   r/   r+   r   r%   r8   r<   rK   r   r   r   r   r-      s    
	r-   c                 C   s   t | ƒ ¡ S r   )r-   rK   )r4   r   r   r   Údiagonalize_vectorÛ   s   rN   N)Zsympy.core.sympifyr   Zsympy.matrices.expressionsr   Z
sympy.corer   r   r   Zsympy.core.mulr   Z(sympy.functions.special.tensor_functionsr   r	   r,   r-   rN   r   r   r   r   Ú<module>   s    MG>