
    Ed                         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e          Z G d d	e          Z G d
 de          Zd ZdS )    )_sympify)
MatrixExpr)SEqGe)Mul)KroneckerDeltac                   ^    e Zd ZdZ ed           Z ed           Zed             Z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                     | j         d         S Nr   argsselfs    Clib/python3.11/site-packages/sympy/matrices/expressions/diagonal.py<lambda>zDiagonalMatrix.<lambda>2       	!     c                     | j         j        S N)argshaper   s    r   r   zDiagonalMatrix.<lambda>4   s
    $(. r   c                     | j         \  }}|j        r|j        rt          ||          }nO|j        r
|j        s|}n>|j        r
|j        s|}n-||k    r|}n$	 t          ||          }n# t          $ r d }Y nw xY w|S r   )r   
is_Integermin	TypeErrorr   rcms       r   diagonal_lengthzDiagonalMatrix.diagonal_length6   s    z1< 	AL 	Aq		AA\ 
	!, 
	AA\ 	!, 	AA!V 	AA1II   s   A' 'A65A6c                    | j         Zt          || 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 rt          j        S | j        ||f         t          ||          z  S r   )	r"   r   r   trueZeror   r   falser	   )r   ijkwargseqs        r   _entryzDiagonalMatrix._entryH   s     	!T)**af4 vAt+,,6 v1XX< 	8AqD>!17] 	6Mx1~nQ2222r   N	__name__
__module____qualname____doc__propertyr   r   r"   r+    r   r   r   r   	   so        ' 'P (,,
-
-CH0011E  X"3 3 3 3 3r   r   c                   \    e Zd ZdZ ed           Zed             Zed             Z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                     | j         d         S r   r   r   s    r   r   zDiagonalOf.<lambda>   r   r   c                    | j         j        \  }}|j        r|j        rt          ||          }nO|j        r
|j        s|}n>|j        r
|j        s|}n-||k    r|}n$	 t          ||          }n# t          $ r d }Y nw xY w|t
          j        fS r   )r   r   r   r   r   r   Oner   s       r   r   zDiagonalOf.shape   s    x~1< 	AL 	Aq		AA\ 
	!, 
	AA\ 	!, 	AA!V 	AA1II   !%xs   A, ,A;:A;c                     | j         d         S r   )r   r   s    r   r"   zDiagonalOf.diagonal_length   s    z!}r   c                 *     | j         j        ||fi |S r   )r   r+   )r   r'   r(   r)   s       r   r+   zDiagonalOf._entry   s     txq!..v...r   Nr,   r2   r   r   r4   r4   V   sw        * *V (,,
-
-C  X"   X/ / / / /r   r4   c                   F    e Zd ZdZd Zed             Zd Zd Zd Z	d Z
dS )	
DiagMatrixz/
    Turn a vector into a diagonal matrix.
    c                     t          |          }t          j        | |          }|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   dims        r   r>   zDiagMatrix.__new__   s    &!! f--(a-5eAhhU1X<?a 	" CMM!CM3Z

r   c                     | j         S r   )r@   r   s    r   r   zDiagMatrix.shape   s
    {r   c                     | j         r | j        j        |dfi |}n | j        j        d|fi |}||k    r|t          ||          z  }|S r   )r?   rA   r+   r	   )r   r'   r(   r)   results        r   r+   zDiagMatrix._entry   so    > 	9(T\(A8888FF(T\(A8888F6 	+nQ***Fr   c                     | S r   r2   r   s    r   _eval_transposezDiagMatrix._eval_transpose   s    r   c                 `    ddl m}  |t          | j                                                   S )Nr   )diag)sympy.matrices.denserL   listrA   as_explicit)r   rL   s     r   rO   zDiagMatrix.as_explicit   s7    ------tT$,22445566r   c                    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          ||          r_ |t          |j                            }	t!          |	j        d                   D ]}
||
         |	|
|
f<    t#          |          |	          S |j        rd |j        D             fd|j        D             }|r[t)          j        |          t-          |                                                                                                        z  S t          ||          r|j        }t-          |          S )	Nr   )askQ)MatMul)	Transpose)eye)
MatrixBasec                      g | ]}|j         	|S r2   )	is_Matrix).0r   s     r   
<listcomp>z#DiagMatrix.doit.<locals>.<listcomp>   s    DDDcmDDDDr   c                     g | ]}|v|	S r2   r2   )rY   r   matricess     r   rZ   z#DiagMatrix.doit.<locals>.<listcomp>   s#    IIIsS5HIsIIIr   )sympy.assumptionsrQ   rR   !sympy.matrices.expressions.matmulrS   $sympy.matrices.expressions.transposerT   rM   rU   sympy.matrices.matricesrV   rA   diagonal
isinstancemaxr   rangetype	is_MatMulr   r   fromiterr;   doitr   )r   hintsrQ   rR   rS   rT   rU   rV   rC   retr'   scalarsr\   s               @r   rh   zDiagMatrix.doit   s   ,,,,,,,,<<<<<<BBBBBB,,,,,,6666663qzz&!!"" 	Mfj)) 	%#c&,''((C39Q<(( & &"1IAqD		4<<$$$ 	aDDv{DDDHIIIIfkIIIG a|G,,Z8Q8Q8V8V8X8X-Y-Y-^-^-`-```fi(( 	 ZF&!!!r   N)r-   r.   r/   r0   r>   r1   r   r+   rJ   rO   rh   r2   r   r   r;   r;      s              X    7 7 7" " " " "r   r;   c                 D    t          |                                           S r   )r;   rh   )rC   s    r   diagonalize_vectorrm      s    f""$$$r   N)sympy.core.sympifyr   sympy.matrices.expressionsr   
sympy.corer   r   r   sympy.core.mulr   (sympy.functions.special.tensor_functionsr	   r   r4   r;   rm   r2   r   r   <module>rs      s   ' ' ' ' ' ' 1 1 1 1 1 1                           C C C C C CJ3 J3 J3 J3 J3Z J3 J3 J3ZD/ D/ D/ D/ D/ D/ D/ D/N;" ;" ;" ;" ;" ;" ;" ;"|% % % % %r   