
    sg                         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y)    )_sympify)
MatrixExpr)SEqGe)Mul)KroneckerDeltac                   J    e Zd ZdZ ed       Z ed       Zed        Zd Zy)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    V/var/www/html/venv/lib/python3.12/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                 0   | j                   \  }}|j                  r|j                  rt        ||      }|S |j                  r|j                  s|}|S |j                  r|j                  s|}|S ||k(  r|}|S 	 t        ||      }|S # t        $ r d }Y |S w xY wr   )r   
is_Integermin	TypeErrorr   rcms       r   diagonal_lengthzDiagonalMatrix.diagonal_length6   s    zz1<<ALLAq	A  \\!,,A  \\!,,A  !VA 	1I   s   8B BBc                    | j                   lt        || 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))*aff4vvAt++,6vv1X<88AqD>!177]66Mxx1~nQ222r   N	__name__
__module____qualname____doc__propertyr   r   r"   r+    r   r   r   r   	   s7    'P ,
-C01E "3r   r   c                   H    e Zd ZdZ ed       Zed        Zed        Zd Zy)
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                 Z   | j                   j                  \  }}|j                  r|j                  rt        ||      }nK|j                  r|j                  s|}n0|j                  r|j                  s|}n||k(  r|}n	 t        ||      }|t
        j                  fS # t        $ r d }Y w xY wr   )r   r   r   r   r   r   Oner   s       r   r   zDiagonalOf.shape   s    xx~~1<<ALLAq	A\\!,,A\\!,,A!VA1I !%%x  s   >B B*)B*c                      | j                   d   S r   )r   r   s    r   r"   zDiagonalOf.diagonal_length   s    zz!}r   c                 >     | j                   j                  ||fi |S r   )r   r+   )r   r'   r(   r)   s       r   r+   zDiagonalOf._entry   s    txxq!.v..r   Nr,   r2   r   r   r4   r4   V   s@    *V ,
-C "  /r   r4   c                   >    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
y)	
DiagMatrixz/
    Turn a vector into a diagonal matrix.
    c                     t        |      }t        j                  | |      }|j                  }|d   dk(  r|d   n|d   }|j                  d   dk7  rd|_        nd|_        ||f|_        ||_        |S )Nr      TF)r   r   __new__r   	_iscolumn_shape_vector)clsvectorobjr   dims        r   r>   zDiagMatrix.__new__   su    &!  f-(a-eAhU1X<<?a CM!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 |}||k7  r|t        ||      z  }|S r   )r?   rA   r+   r	   )r   r'   r(   r)   results        r   r+   zDiagMatrix._entry   s`    >>(T\\((A88F(T\\((A88F6nQ**Fr   c                     | S r   r2   r   s    r   _eval_transposezDiagMatrix._eval_transpose   s    r   c                 Z    ddl m}  |t        | j                  j	                                S )Nr   )diag)sympy.matrices.denserL   listrA   as_explicit)r   rL   s     r   rO   zDiagMatrix.as_explicit   s"    -T$,,224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                  } ||j                  |            r|S t        ||      rS |t        |j                              }	t!        |	j                  d         D ]  }
||
   |	|
|
f<     t#        |      |	      S |j$                  r|j&                  D cg c]  }|j(                  s| }}|j&                  D cg c]	  }||vs| }}|rLt+        j,                  |      t/        |j-                  |      j1                               j1                         z  S t        ||      r|j2                  }t/        |      S c c}w c c}w )Nr   )askQ)MatMul)	Transpose)eye)
MatrixBase)sympy.assumptionsrQ   rR   !sympy.matrices.expressions.matmulrS   $sympy.matrices.expressions.transposerT   rM   rU   sympy.matrices.matrixbaserV   rA   diagonal
isinstancemaxr   rangetype	is_MatMulr   	is_Matrixr   fromiterr;   doitr   )r   hintsrQ   rR   rS   rT   rU   rV   rC   retr'   r   matricesscalarss                 r   rc   zDiagMatrix.doit   s-   ,<B,8qzz&!"Mfj)c&,,'(C399Q<( &"1IAqD	&4<$$'-{{DcmmDHD&,kkIsS5HsIGI||G,Z8Q8V8V8X-Y-^-^-```fi(ZZF&!! EIs   ?E&E&%	E+/E+N)r-   r.   r/   r0   r>   r1   r   r+   rJ   rO   rc   r2   r   r   r;   r;      s4      7"r   r;   c                 4    t        |       j                         S r   )r;   rc   )rC   s    r   diagonalize_vectorri      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;   ri   r2   r   r   <module>ro      sG    ' 1      CJ3Z J3ZD/ D/N;" ;"|%r   