K i0ddlmZddlmZddlmZmZddlmZm Z m Z m Z ddl m Z  ddZ dd Ze fd Ze d d fd Ze d fd ZdZdZe d ddfdZy)) FunctionType)CoercionFailed)ZZQQ)_get_intermediate_simp_iszero _dotprodsimp _simplify)_find_reasonable_pivotTc ~fd} fd} fd} ttd\} } g}g}| kr| |kr t| | | d||\}}}}|D]\}}|| z }||z| z<|| dz } I|j| |dk7r"| | || z|j| || zf|durB| | }}||z|z<t |z|zdz|dzzD]}||z |<|}t |D]1}|| k(r |dur|| kr|z| z}||r'| |||| 3| dz } | kr| |kr |d ur^|d urZt |D]L\}}|z|z}||z|z<t |z|zdz|dzzD]}||z |<Nt |t |fS) aRow reduce a flat list representation of a matrix and return a tuple (rref_matrix, pivot_cols, swaps) where ``rref_matrix`` is a flat list, ``pivot_cols`` are the pivot columns and ``swaps`` are any row swaps that were used in the process of row reduction. Parameters ========== mat : list list of matrix elements, must be ``rows`` * ``cols`` in length rows, cols : integer number of rows and columns in flat list representation one : SymPy object represents the value one, from ``Matrix.one`` iszerofunc : determines if an entry can be used as a pivot simpfunc : used to simplify elements and test if they are zero if ``iszerofunc`` returns `None` normalize_last : indicates where all row reduction should happen in a fraction-free manner and then the rows are normalized (so that the pivots are 1), or whether rows should be normalized along the way (like the naive row reduction algorithm) normalize : whether pivot rows should be normalized so that the pivot value is 1 zero_above : whether entries above the pivot should be zeroed. If ``zero_above=False``, an echelon matrix will be returned. c|dSN)icolsmats _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/matrices/reductions.pyget_colz!_row_reduce_list..get_col/s17d7|cp|z|dzz|z|dzzc|z|dzz|z|dzzy)Nrr)rjrrs rrow_swapz"_row_reduce_list..row_swap2s] $At| $c!D&!a%&> ;AdFAE4< #afa!eT\":rc||z z}t|z|dzzD]}||z|||zzz |<!y)z,Does the row op row[i] = a*row[i] - b*row[j]rN)range) arbrqprisimprs r cross_cancelz&_row_reduce_list..cross_cancel6sY UDLqvAt|, 4A1SV8aAE l23CF 4rrrNrrFT)rr r appendr enumeratetuple)rrowsrone iszerofuncsimpfuncnormalize_last normalize zero_aboverrr!piv_rowpiv_col pivot_colsswaps pivot_offset pivot_valassumed_nonzeronewly_determinedoffsetvalrrrrowpiv_ipiv_jr s` ` @r_row_reduce_listr: s|J?4 #< 0EGWJ E D.Wt^,B *J-B * i) . -MVS g F),Ct g% & -   qLG '" 1  WlW4 5 LL'<'#9: ; U "GqA!C$ O1T6A:>AE4<8 3s1v 12A 3I; 7Cg~U"sW}c$h()C# Cg 6 7 1 Y D.Wt^^)t"3%j1 3LE5E$J./I&)Cd U" #5:-1EAIt3CD 3s1v 12A 3 3 j!5< //rc tt||j|j|j||||| \}}}|j |j|j|||fS)Nr*r+r,)r:listr&rr'_new) Mr(r)r*r+r,rr/r0s r _row_reducer@|s].d1gqvvqvvquu J8CU 66!&&!&&# & E 99rc|jdks|jdkrytfd|dddfD}|dr|xrt|ddddfS|xrt|ddddfS)zReturns `True` if the matrix is in echelon form. That is, all rows of zeros are at the bottom, and below each leading non-zero in a row are exclusively zeros.rTc3.K|] }|ywrr).0tr(s r z_is_echelon..s6jm6srNr")r&rall _is_echelon)r?r( zeros_belows ` rrGrGs}  vv{affk6Qqr1uX66K!D'@{1QU8Z@@  =;qQRy*==rFclt|tr|nt}t|||ddd\}}}|r||fS|S)anReturns a matrix row-equivalent to ``M`` that is in echelon form. Note that echelon form of a matrix is *not* unique, however, properties like the row space and the null space are preserved. Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.echelon_form() Matrix([ [1, 2], [0, -2]]) TFr<) isinstancerr r@)r?r(simplify with_pivotsr)rpivots_s r _echelon_formrOsF &h =x9H J5UDNCF{ Jrcd}t|tr|nt}|jdks|jdkry|jdks|jdkr|Dcgc] }|| }}d|vry|jdk(rU|jdk(rF|Dcgc] }|| }}d|vrd|vry|j }||rd|vry||dury|||\}} t |||ddd \} } } t| Scc}wcc}w) zReturns the rank of a matrix. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x >>> m = Matrix([[1, 2], [x, 1 - 1/x]]) >>> m.rank() 2 >>> n = Matrix(3, 3, range(1, 10)) >>> n.rank() 2 cfd}tjDcgc] }|||f}}t|Dcgc]\}}| }}}j|d|fScc}wcc}}w)aPermute columns with complicated elements as far right as they can go. Since the ``sympy`` row reduction algorithms start on the left, having complexity right-shifted speeds things up. Returns a tuple (mat, perm) where perm is a permutation of the columns to perform to shift the complex columns right, and mat is the permuted matrix.c:tfddd|fDS)Nc36K|]}|dndyw)Nrrr)rCer(s rrEzO_rank.._permute_complexity_right..complexity..sJQJqM1qq8Js)sum)rr?r(s r complexityz<_rank.._permute_complexity_right..complexitysJ!AqD'JJ Jrr) orientation)rrsortedpermute)r?r(rVrcomplexrperms`` r_permute_complexity_rightz(_rank.._permute_complexity_rightsj K 05QVV}=!JqM1%==#)'?3!Q133 $F 3T::>3s A% A*rrFN)r(Tr<)rJrr r&rdetr@len) r?r(rKr\r)xzerosdrrNrMs r_rankrcs ;(&h =x9H  vv{affkvv{affk()*1A** E>vv{qvv{()*1A**  $e"3 EEG a=Ue^ a=E !,Q:FFCsJ/LAvq v;-+ +s DD cht|dsy|j}|j}|jr|S|jr |j t Std|Dsy |j t S#t$r|cYSwxYw#t$r|j tcYSwxYw)N_repc34K|]}|jywr) is_Rational)rCrTs rrEz_to_DM_ZZ_QQ..s,Q1==,s) hasattrredomainis_ZZis_QQ convert_torrrFr)r?repKs r _to_DM_ZZ_QQros 1f  &&C Aww  >>"% %,!,, &>>"% %  J  &>>"% % &s$A?*B? B  B B10B1c|j}|jr*|jd\}}}|j|z }n"|jr|j \}}nJ|j }||fS)z7Compute the reduced row echelon form of a DomainMatrix.F) keep_domain)rirjrref_dento_fieldrkrref to_Matrix)dMrndM_rrefdenrMM_rrefs r_rref_dmrzso Aww!{{u{=f""$s* '')u    F 6>rct|}|t|\}}n.t|tr|}nt}t ||||dd\}}} |r||fS|S)a- Return reduced row-echelon form of matrix and indices of pivot vars. Parameters ========== iszerofunc : Function A function used for detecting whether an element can act as a pivot. ``lambda x: x.is_zero`` is used by default. simplify : Function A function used to simplify elements when looking for a pivot. By default SymPy's ``simplify`` is used. pivots : True or False If ``True``, a tuple containing the row-reduced matrix and a tuple of pivot columns is returned. If ``False`` just the row-reduced matrix is returned. normalize_last : True or False If ``True``, no pivots are normalized to `1` until after all entries above and below each pivot are zeroed. This means the row reduction algorithm is fraction free until the very last step. If ``False``, the naive row reduction procedure is used where each pivot is normalized to be `1` before row operations are used to zero above and below the pivot. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x >>> m = Matrix([[1, 2], [x, 1 - 1/x]]) >>> m.rref() (Matrix([ [1, 0], [0, 1]]), (0, 1)) >>> rref_matrix, rref_pivots = m.rref() >>> rref_matrix Matrix([ [1, 0], [0, 1]]) >>> rref_pivots (0, 1) ``iszerofunc`` can correct rounding errors in matrices with float values. In the following example, calling ``rref()`` leads to floating point errors, incorrectly row reducing the matrix. ``iszerofunc= lambda x: abs(x) < 1e-9`` sets sufficiently small numbers to zero, avoiding this error. >>> m = Matrix([[0.9, -0.1, -0.2, 0], [-0.8, 0.9, -0.4, 0], [-0.1, -0.8, 0.6, 0]]) >>> m.rref() (Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]), (0, 1, 2)) >>> m.rref(iszerofunc=lambda x:abs(x)<1e-9) (Matrix([ [1, 0, -0.301369863013699, 0], [0, 1, -0.712328767123288, 0], [0, 0, 0, 0]]), (0, 1)) Notes ===== The default value of ``normalize_last=True`` can provide significant speedup to row reduction, especially on matrices with symbols. However, if you depend on the form row reduction algorithm leaves entries of the matrix, set ``normalize_last=False`` T)r+r,)rorzrJrr r@) r?r(rKrMr*rvrr/r)rNs r_rrefr|'siT aB ~"2,Z h -H H(J$4AZJ rN)TTT)typesrsympy.polys.polyerrorsrsympy.polys.domainsrr utilitiesrr r r determinantr r:r@rGrOrcrorzr|rrrrst1&OO/AEn0d9=+/:& > !(%U8 %CL&<" %\r