K i[ddlmZddlmZddlmZddlmZddlm Z m Z m Z ddl m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZmZmZm Z dd l!m"Z"m#Z#m$Z$m%Z%dd l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4dd l5m6Z6d d giZ7GddeZ8Gdde8Z9Gdde9Z:Gdde:Z;GddeZ<GddeZ=y))Basic)Dummy) MatrixCommon)NonSquareMatrixError)_iszero_is_zero_after_expand_mul _simplify)_find_reasonable_pivot_find_reasonable_pivot_naive _adjugate _charpoly _cofactor_cofactor_matrix_per_det _det_bareiss_det_berkowitz _det_bird _det_laplace_det_LU_minor_minor_submatrix) _is_echelon _echelon_form_rank_rref) _columnspace _nullspace _rowspace_orthogonalize) _eigenvals _eigenvects_bidiagonalize_bidiagonal_decomposition_is_diagonalizable _diagonalize_is_positive_definite_is_positive_semidefinite_is_negative_definite_is_negative_semidefinite_is_indefinite _jordan_form_left_eigenvects_singular_values) MatrixBase)zMatrixEigen.is_indefinitez MatrixEigen.is_negative_definitez$MatrixEigen.is_negative_semidefinitez MatrixEigen.is_positive_definitez$MatrixEigen.is_positive_semidefinite matplotlibceZdZdZefdZdZedfdZdZ dZ dZ dd Z d e fd Zdd Zdd ZddZdZddZdZeje_eje_eje_eje_eje _eje _eje_eje _eje _eje_eje_e je_eje_e!je_e"je_e#je_y)MatrixDeterminantzProvides basic matrix determinant operations. Should not be instantiated directly. See ``determinant.py`` for their implementations.ct||S)N) iszerofuncr)selfr5s ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/matrices.py_eval_det_bareissz#MatrixDeterminant._eval_det_bareiss3sDZ88ct|SN)rr7s r8_eval_det_berkowitzz%MatrixDeterminant._eval_det_berkowitz6s d##r:Nct|||S)N)r5simpfunc)r)r7r5r@s r8 _eval_det_luzMatrixDeterminant._eval_det_lu9st XFFr:ct|Sr<)rr=s r8_eval_det_birdz MatrixDeterminant._eval_det_bird<s r:ct|Sr<)rr=s r8_eval_det_laplacez#MatrixDeterminant._eval_det_laplace? D!!r:ct|Sr<rr=s r8_eval_determinantz#MatrixDeterminant._eval_determinantB Dzr:ct||SNmethod)r r7rNs r8adjugatezMatrixDeterminant.adjugateEsf--r:lambdact|||S)N)xsimplify)rr7rSrTs r8charpolyzMatrixDeterminant.charpolyHsX66r:c t||||SrL)rr7ijrNs r8cofactorzMatrixDeterminant.cofactorKsq!F33r:ct||SrL)rrOs r8cofactor_matrixz!MatrixDeterminant.cofactor_matrixNsV44r:ct|||S)N)rNr5rH)r7rNr5s r8detzMatrixDeterminant.detQsDJ??r:ct|Sr<)rr=s r8perzMatrixDeterminant.perTrJr:c t||||SrL)rrXs r8minorzMatrixDeterminant.minorWsdAq00r:ct|||Sr<)rr7rYrZs r8minor_submatrixz!MatrixDeterminant.minor_submatrixZsa++r: berkowitz)bareissN)$__name__ __module__ __qualname____doc__r r9r>rrArCrErIrPr rVr[r]r_rarcrfr r rrrrrrr rrrrrrr:r8r3r3/s@C,E9$'.G"."I745@1,,B+I+I"+G+O+O (+7+?+?+9+A+A(1(9(9N+7+?+?+2??L+/<<+4+<+>EM+;+C+COr:r3ceZdZdZeddfdZedZedfdZdZ edddfdZ e je_e je_e je_eje _dd Zd Zd Zd Zd ZdZdZddZddZy)MatrixReductionszProvides basic matrix row/column operations. Should not be instantiated directly. See ``reductions.py`` for some of their implementations.Fc t||||S)N)r5rT with_pivots)r)r7r5rTrrs r8 echelon_formzMatrixReductions.echelon_formssTj8') )r:ct|Sr<)rr=s r8 is_echelonzMatrixReductions.is_echelonws 4  r:ct|||S)N)r5rT)r)r7r5rTs r8rankzMatrixReductions.rank{sTj8DDr:ct|j||j|j|\}}|ddd|jf|dd|j dffS)aReturn reduced row-echelon form of matrix, matrix showing rhs after reduction steps. ``rhs`` must have the same number of rows as ``self``. Examples ======== >>> from sympy import Matrix, symbols >>> r1, r2 = symbols('r1 r2') >>> Matrix([[1, 1], [2, 1]]).rref_rhs(Matrix([r1, r2])) (Matrix([ [1, 0], [0, 1]]), Matrix([ [ -r1 + r2], [2*r1 - r2]])) N)rhstackeyerowscols)r7rhsr_s r8rref_rhszMatrixReductions.rref_rhs~sY"T[[txx ':C@A1JTYYJ1sxxij=!111r:Tc"t|||||S)N)r5rTpivotsnormalize_last)r)r7r5rTrrs r8rrefzMatrixReductions.rrefsTj8.: :r:c|dvrtdj|||dk(r |jn |j}|dk(rM||n|}||tdj|d|cxkr|ksntdj|||d k(r||||hj dg}t |d kDr|||hj dg}t |d k7rtd j||\}}d|cxkr|ksntdj||d|cxkr|ksntdj|||d k(r||n|}||n|}|||td j|||k(rtdj|d|cxkr|ksntdj||d|cxkr|ks4ntdj||tdt |z|||||fS)zValidate the arguments for a row/column operation. ``error_str`` can be one of "row" or "col" depending on the arguments being parsed.)n->knn<->mn->n+kmzOUnknown {} operation '{}'. Valid col operations are 'n->kn', 'n<->m', 'n->n+km'colrNzEFor a {0} operation 'n->kn' you must provide the kwargs `{0}` and `k`rz#This matrix does not have a {} '{}'rzIFor a {0} operation 'n<->m' you must provide the kwargs `{0}1` and `{0}2`rzPFor a {0} operation 'n->n+km' you must provide the kwargs `{0}`, `k`, and `{0}2`zAFor a {0} operation 'n->n+km' `{0}` and `{0}2` must be different.zinvalid operation %s) ValueErrorformatr|r{ differencelenrepr) r7oprkcol1col2 error_str self_colsr|s r8_normalize_op_argsz#MatrixReductions._normalize_op_argssT 2 2??EviQS?TV V"+e!3DII  =#dC{ai "88>y8IKK'i' !F!M!MiY\!]^^ 7]D$'22D6:D4y1}T4(33TF;4yA~ "<? ?34%%r:cdfd}jjj|S)Nc0|k(r ||fzS||fSr<rn)rYrZrrr7s r8entryzBMatrixReductions._eval_col_op_multiply_col_by_const..entry*Cx41:~%1: r:_newr{r|)r7rrrs``` r8"_eval_col_op_multiply_col_by_constz3MatrixReductions._eval_col_op_multiply_col_by_const% yyDIIu55r:cdfd}jjj|S)NcB|k(r|fS|k(r|fS||fSr<rn)rYrZrrr7s r8rz1MatrixReductions._eval_col_op_swap..entrys;DyAtG}$dAtG}$1: r:r)r7rrrs``` r8_eval_col_op_swapz"MatrixReductions._eval_col_op_swap%  yyDIIu55r:chfd}jjj|S)Nc@|k(r||f|fzzS||fSr<rn)rYrZrrrr7s r8rzFMatrixReductions._eval_col_op_add_multiple_to_other_col..entrys8CxAqDzAQW $5551: r:r)r7rrrrs```` r8&_eval_col_op_add_multiple_to_other_colz7MatrixReductions._eval_col_op_add_multiple_to_other_col% yyDIIu55r:cdfd}jjj|S)NcB|k(r|fS|k(r|fS||fSr<rn)rYrZrow1row2r7s r8rz1MatrixReductions._eval_row_op_swap..entrys;DyD!G}$dD!G}$1: r:r)r7rrrs``` r8_eval_row_op_swapz"MatrixReductions._eval_row_op_swaprr:cdfd}jjj|S)Nc0|k(r ||fzS||fSr<rn)rYrZrrowr7s r8rzBMatrixReductions._eval_row_op_multiply_row_by_const..entryrr:r)r7rrrs``` r8"_eval_row_op_multiply_row_by_constz3MatrixReductions._eval_row_op_multiply_row_by_constrr:chfd}jjj|S)Nc@|k(r||f|fzzS||fSr<rn)rYrZrrrr7s r8rzFMatrixReductions._eval_row_op_add_multiple_to_other_row..entrys8CxAqDzAT1W $5551: r:r)r7rrrrs```` r8&_eval_row_op_add_multiple_to_other_rowz7MatrixReductions._eval_row_op_add_multiple_to_other_rowrr:Nc|j|||||d\}}}}}|dk(r|j||S|dk(r|j||S|dk(r|j|||Sy)adPerforms the elementary column operation `op`. `op` may be one of * ``"n->kn"`` (column n goes to k*n) * ``"n<->m"`` (swap column n and column m) * ``"n->n+km"`` (column n goes to column n + k*column m) Parameters ========== op : string; the elementary row operation col : the column to apply the column operation k : the multiple to apply in the column operation col1 : one column of a column swap col2 : second column of a column swap or column "m" in the column operation "n->n+km" rrrrN)rrrr)r7rrrrrs r8elementary_col_opz"MatrixReductions.elementary_col_op("&!8!8S!T4QV!WCD$ =::3B B =))$5 5 ?>>sAtL L r:c|j|||||d\}}}}}|dk(r|j||S|dk(r|j||S|dk(r|j|||Sy)a4Performs the elementary row operation `op`. `op` may be one of * ``"n->kn"`` (row n goes to k*n) * ``"n<->m"`` (swap row n and row m) * ``"n->n+km"`` (row n goes to row n + k*row m) Parameters ========== op : string; the elementary row operation row : the row to apply the row operation k : the multiple to apply in the row operation row1 : one row of a row swap row2 : second row of a row swap or row "m" in the row operation "n->n+km" rrrrN)rrrr)r7rrrrrs r8elementary_row_opz"MatrixReductions.elementary_row_oprr:)r)rNNNN)rjrkrlrmrrspropertyrurwrrrrrrrrrrrrrrrrnr:r8rprposJ'.5)!!&E2(&d: )00L&..J ==DL ==DL5&n666666M<Mr:rpceZdZdZddZdefdZddZdZe je_e je_e je_e je_e eZy) MatrixSubspaceszProvides methods relating to the fundamental subspaces of a matrix. Should not be instantiated directly. See ``subspaces.py`` for their implementations.Fct||SN)rT)rr7rTs r8 columnspacezMatrixSubspaces.columnspaceCsD844r:ct|||S)N)rTr5)r)r7rTr5s r8 nullspacezMatrixSubspaces.nullspaceFs$jIIr:ct||Sr)r rs r8rowspacezMatrixSubspaces.rowspaceIs11r:c t|g|i|Sr<)r!)clsvecskwargss r8 orthogonalizezMatrixSubspaces.orthogonalizeOsc3D3F33r:NF)rjrkrlrmrrrrrrrr r! classmethodrnr:r8rr>sg5"'7J2 4)00K&..I%--H*22M' 6Mr:rceZdZdZddZdefdZddZddZddZ ddZ e d Z e d Z e d Ze d Ze d ZddZdZdZeje_eje_eje_eje_eje _eje _eje_eje_eje_eje_eje_eje_e je _e!je _y) MatrixEigenzProvides basic matrix eigenvalue/vector operations. Should not be instantiated directly. See ``eigen.py`` for their implementations.Tc t|fd|i|S)Nerror_when_incomplete)r")r7rflagss r8 eigenvalszMatrixEigen.eigenvals_s$U6KUuUUr:c t|f||d|S)N)rr5)r#)r7rr5rs r8 eigenvectszMatrixEigen.eigenvectsbs$407L%0).0 0r:c t|fd|i|S)N reals_only)r&)r7rrs r8is_diagonalizablezMatrixEigen.is_diagonalizablefs!$H:HHHr:c t||||S)N)rsort normalize)r')r7rrrs r8 diagonalizezMatrixEigen.diagonalizeisDZd#% %r:ct||SN)upper)r$r7rs r8 bidiagonalizezMatrixEigen.bidiagonalizemsd%00r:ct||Sr)r%rs r8bidiagonal_decompositionz$MatrixEigen.bidiagonal_decompositionps(U;;r:ct|Sr<)r(r=s r8is_positive_definitez MatrixEigen.is_positive_definites $T**r:ct|Sr<)r)r=s r8is_positive_semidefinitez$MatrixEigen.is_positive_semidefinitew (..r:ct|Sr<)r*r=s r8is_negative_definitez MatrixEigen.is_negative_definite{rr:ct|Sr<)r+r=s r8is_negative_semidefinitez$MatrixEigen.is_negative_semidefiniterr:ct|Sr<)r,r=s r8 is_indefinitezMatrixEigen.is_indefinites d##r:c t|fd|i|S)Ncalc_transform)r-)r7rrs r8 jordan_formzMatrixEigen.jordan_formsDJJ6JJr:c t|fi|Sr<)r.r7rs r8left_eigenvectszMatrixEigen.left_eigenvectss...r:ct|Sr<)r/r=s r8singular_valueszMatrixEigen.singular_valuess %%r:NTr)FFF)"rjrkrlrmrrrrrrrrrrrrrrrrr"r#r&r'r(r)r*r+r,r-r.r/r$r%rnr:r8rrZsnV040I%1<++//++//$$K/&*4););I)4)<))F)F )B)J)J$)>)F)F )B)J)J$)7)?)?M)5)=)=K)9)A)AO)9)A)AO)7)?)?M)B)J)J$r:rc4eZdZdZdddZdZdZdZdZy ) MatrixCalculusz,Provides calculus-related matrix operations.T)evaluatecnddlm}||g|d|i}t|ts|r|j S|S)aeCalculate the derivative of each element in the matrix. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.diff(x) Matrix([ [1, 0], [0, 0]]) See Also ======== integrate limit r)ArrayDerivativer)$sympy.tensor.array.array_derivativesr isinstancer as_mutable)r7rargsrrderivs r8diffzMatrixCalculus.diffs<* I?t?h?$&8##% % r:c,|jfdS)Nc&|jSr<r)rSargs r8z1MatrixCalculus._eval_derivative..ss r: applyfunc)r7rs `r8_eval_derivativezMatrixCalculus._eval_derivatives~~344r:c0|jfdS)aIntegrate each element of the matrix. ``args`` will be passed to the ``integrate`` function. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.integrate((x, )) Matrix([ [x**2/2, x*y], [ x, 0]]) >>> M.integrate((x, 0, 2)) Matrix([ [2, 2*y], [2, 0]]) See Also ======== limit diff c(|jiSr<) integrate)rSrrs r8rz*MatrixCalculus.integrate..s  T(DV(Dr:r)r7rrs ``r8r zMatrixCalculus.integrates2~~DEEr:cttsjjddk(rjd}n-jddk(rjd}n t djddk(rjd}n-jddk(rjd}n t dj||fdS)aCalculates the Jacobian matrix (derivative of a vector-valued function). Parameters ========== ``self`` : vector of expressions representing functions f_i(x_1, ..., x_n). X : set of x_i's in order, it can be a list or a Matrix Both ``self`` and X can be a row or a column matrix in any order (i.e., jacobian() should always work). Examples ======== >>> from sympy import sin, cos, Matrix >>> from sympy.abc import rho, phi >>> X = Matrix([rho*cos(phi), rho*sin(phi), rho**2]) >>> Y = Matrix([rho, phi]) >>> X.jacobian(Y) Matrix([ [cos(phi), -rho*sin(phi)], [sin(phi), rho*cos(phi)], [ 2*rho, 0]]) >>> X = Matrix([rho*cos(phi), rho*sin(phi)]) >>> X.jacobian(Y) Matrix([ [cos(phi), -rho*sin(phi)], [sin(phi), rho*cos(phi)]]) See Also ======== hessian wronskian rrz)``self`` must be a row or a column matrixz"X must be a row or a column matrixc2|j|Sr<r)rZrYXr7s r8rz)MatrixCalculus.jacobian..sDGLL1,>r:)rr0rshape TypeError)r7r mns`` r8jacobianzMatrixCalculus.jacobiansH!Z( ! A ::a=A  1 A ZZ]a  1 AGH H 771:? A WWQZ1_ A@A AyyA>??r:c,|jfdS)aCalculate the limit of each element in the matrix. ``args`` will be passed to the ``limit`` function. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[x, y], [1, 0]]) >>> M.limit(x, 2) Matrix([ [2, y], [1, 0]]) See Also ======== integrate diff c"|jSr<)limit)rSrs r8rz&MatrixCalculus.limit..+sr:r)r7rs `r8rzMatrixCalculus.limits*~~677r:N) rjrkrlrmrrr rrrnr:r8rrs$6#'85F67@r8r:rc|eZdZdZedefdZdZdZdZ dZ ddZ d Z d Z d Zdd Zdd ZdZdZdZy)MatrixDeprecatedz+A class to house deprecated matrix methods.rQc&|j|S)N)rS)rVrUs r8berkowitz_charpolyz#MatrixDeprecated.berkowitz_charpoly1s}}q}!!r:c&|jdS)zwComputes determinant using Berkowitz method. See Also ======== det berkowitz rhrMr_r=s r8 berkowitz_detzMatrixDeprecated.berkowitz_det4sxx{x++r:c &|jdi|S)zwComputes eigenvalues of a Matrix using Berkowitz method. See Also ======== berkowitz rn)rrs r8berkowitz_eigenvalsz$MatrixDeprecated.berkowitz_eigenvals?st~~&&&r:c|jg}}|jD]}|j||dz| }t|S)zpComputes principal minors using Berkowitz method. See Also ======== berkowitz )onerhappendtuple)r7signminorspolys r8berkowitz_minorsz!MatrixDeprecated.berkowitz_minorsIsNxxfNN$ D MM$b/ *5D V}r:cddlm}d}|s|S|js t||j}}dg|dz z}t |ddD]}||dz||dz }}||d|f |d||f} } |d|d|f|||f } }| g} t d|dz D]} | j || | zt| D]\} }| |zd| | <|j| g| z} t |D]} | d|| z dz|| d| f<|||dz <|j|j|d gg}t|D]\} }|j ||| z|ttt|zS)Nr)zeros))rrr r)rr) sympy.matricesr) is_squarerr{ranger" enumerater!rr#map)r7r)berkAN transformsrTrRCaitemsrYBpolyss r8rhzMatrixDeprecated.berkowitzYs(K~~&( (TYY1SAE] q!R "AQ?AEqAa!eH9aAhqARaR!V9qAwhqACE1a!e_ + Qq\* +"%( )1E4=a )XXqME)E1X - !a%!),!"a% -!"Jq1u ' "*DHHqwh/01j) 'DAq LLU1X & 'eCu-...r:c&|j|SrL)r]rOs r8cofactorMatrixzMatrixDeprecated.cofactorMatrixs##6#22r:ct|Sr<r6r=s r8 det_bareiszMatrixDeprecated.det_bareisrFr:c&|jdS)aCompute matrix determinant using LU decomposition. Note that this method fails if the LU decomposition itself fails. In particular, if the matrix has no inverse this method will fail. TODO: Implement algorithm for sparse matrices (SFF), https://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps See Also ======== det det_bareiss berkowitz_det lurMrr=s r8det_LU_decompositionz%MatrixDeprecated.det_LU_decompositions&xxtx$$r:c(|j||S)N)size eigenvalue) jordan_block)r7eigenvalrs r8 jordan_cellzMatrixDeprecated.jordan_cells  aH ==r:cL|j\}}||jfSr<)rget_diag_blocks)r7calc_transformationPJs r8 jordan_cellszMatrixDeprecated.jordan_cellss(!1!##%%%r:c*|j|||SrL)rcrXs r8 minorEntryzMatrixDeprecated.minorEntryszz!Qvz..r:c&|j||Sr<)rfres r8 minorMatrixzMatrixDeprecated.minorMatrixs##Aq))r:c(|j|dS)zEPermute the rows of the matrix with the given permutation in reverse.backward direction permute_rowsr7perms r8 permuteBkwdzMatrixDeprecated.permuteBkwds   <&/*=sympy.core.basicrsympy.core.symbolrcommonr exceptionsr utilitiesrr r determinantr r r rrrrrrrrrrrr reductionsrrrr subspacesrrr r!eigenr"r#r$r%r&r'r(r)r*r+r,r-r.r/ matrixbaser0__doctest_requires__r3rprrrrrnr:r8rhs ## ,DD A@JJ6666#-0