K iddlmZddlmZddlmZmZmZddlm Z ddl m Z GddeZ Gdd eZ Gd d eZd Zy ))_sympify) MatrixExpr)SEqGe)Mul)KroneckerDeltacJeZdZdZedZedZedZdZy)DiagonalMatrixaDiagonalMatrix(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 |jdSNrargsselfs i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/expressions/diagonal.pyzDiagonalMatrix.2 ! c.|jjSN)argshapers rrzDiagonalMatrix.4s$((..rc0|j\}}|jr|jrt||}|S|jr|js|}|S|jr|js|}|S||k(r|}|S t||}|S#t$rd}Y|SwxYwr)r is_Integermin TypeErrorrrcms rdiagonal_lengthzDiagonalMatrix.diagonal_length6szz1 < ! 177]66Mxx1~nQ222rN __name__ __module__ __qualname____doc__propertyrrr"r+rrr r s7'P , -C 0 1E " 3rr cHeZdZdZedZedZedZdZy) DiagonalOfaDiagonalOf(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 |jdSr rrs rrzDiagonalOf.rrcZ|jj\}}|jr|jr t||}nK|jr|js|}n0|jr|js|}n||k(r|}n t||}|t j fS#t$rd}YwxYwr)rrrrrrOners rrzDiagonalOf.shapesxx~~1 < B B*)B*c |jdSr )rrs rr"zDiagonalOf.diagonal_lengthszz!}rc >|jj||fi|Sr)rr+)rr'r(r)s rr+zDiagonalOf._entrystxxq!.v..rNr,r2rrr4r4Vs@*V , -C "/rr4c>eZdZdZdZedZdZdZdZ dZ y) DiagMatrixz/ Turn a vector into a diagonal matrix. ct|}tj||}|j}|ddk(r|dn|d}|jddk7rd|_nd|_||f|_||_|S)NrTF)rr__new__r _iscolumn_shape_vector)clsvectorobjrdims rr>zDiagMatrix.__new__su&!  f- (a-eAhU1X <<?a  CM!CM3Z   rc|jSr)r@rs rrzDiagMatrix.shapes {{rc |jr|jj|dfi|}n|jjd|fi|}||k7r|t||z}|Sr )r?rAr+r )rr'r(r)results rr+zDiagMatrix._entrys` >>(T\\((A88F(T\\((A88F 6 nQ* *F rc|Srr2rs r_eval_transposezDiagMatrix._eval_transposes rcZddlm}|t|jj S)Nr)diag)sympy.matrices.denserLlistrA as_explicit)rrLs rrOzDiagMatrix.as_explicits"-T$,,224566rc ddlm}m}ddlm}ddlm}ddlm}ddl m }|j}||j|r|St||rS|t|j} t!| jdD] } || | | | f<t#|| S|j$r|j&D cgc]} | j(s| } } |j&D cgc] } | | vs|  } } | rLt+j,| t/|j-| j1j1zSt||r |j2}t/|Scc} wcc} w)Nr)askQ)MatMul) Transpose)eye) MatrixBase)sympy.assumptionsrQrR!sympy.matrices.expressions.matmulrS$sympy.matrices.expressions.transposerTrMrUsympy.matrices.matrixbaserVrAdiagonal isinstancemaxrrangetype is_MatMulr is_Matrixrfromiterr;doitr)rhintsrQrRrSrTrUrVrCretr'rmatricesscalarss rrczDiagMatrix.doits-,<B,8 qzz&! "M fj )c&,,'(C399Q<( &"1IAqD  &4<$ $   '-{{DcmmDHD&,kkIsS5HsIGI||G,Z8Q8V8V8X-Y-^-^-``` fi (ZZF&!! EIs?E&E&% E+/E+N) r-r.r/r0r>r1rr+rJrOrcr2rrr;r;s4 7"rr;c4t|jSr)r;rc)rCs rdiagonalize_vectorris f  " " $$rN)sympy.core.sympifyrsympy.matrices.expressionsr sympy.corerrrsympy.core.mulr(sympy.functions.special.tensor_functionsr r r4r;rir2rrrosG'1 CJ3ZJ3ZD/D/N;";"|%r