K i<ddlmZddlmZmZmZddlmZddlm Z m Z dddZ ddd d Z d Z d Zd ZdZdZdZdZdddZdddZdddZdZdZdZdZy))ZZ)SDM sdm_irref sdm_rref_den)DDM) ddm_irref ddm_irref_denauto)methodcxt||d\}}t||\}}|dk(rt|}t|\}}ni|dk(rt |\}}}t||z }nF|dk(r3|j d\} } t | \}}}t||z }nt d|t||\}} ||fS) a Compute the reduced row echelon form of a ``DomainMatrix``. This function is the implementation of :meth:`DomainMatrix.rref`. Chooses the best algorithm depending on the domain, shape, and sparsity of the matrix as well as things like the bit count in the case of :ref:`ZZ` or :ref:`QQ`. The result is returned over the field associated with the domain of the Matrix. See Also ======== sympy.polys.matrices.domainmatrix.DomainMatrix.rref The ``DomainMatrix`` method that calls this function. sympy.polys.matrices.rref._dm_rref_den Alternative function for computing RREF with denominator. F denominatorGJFFCDTconvertUnknown method for rref: )_dm_rref_choose_method _dm_to_fmt _to_field _dm_rref_GJ_dm_rref_den_FFclear_denoms_rowwise ValueError) Mr use_fmtold_fmtMfM_rrefpivotsM_rref_fden_Mrs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/matrices/rref.py_dm_rrefr'%s&-QEJOFGAw'JAw ~ q\$R 4 / 2#v8$s* 4&&t&42 / 3#v8$s*4VH=>>67+IFA 6>T) keep_domainr c t||d\}}t||\}}|dk(rt|\}}}n:|dk(rtt |\}}|r\|j |j k7rC|j d\} }|r|d|dfj}n|j j}n|}|j j}n|dk(r|jd\} } t| \} }}|r>| j |j k7r%t | |z }|j j}n>| }|r|d|dfj}n%|j j}ntd|t||\}} |||fS) a Compute the reduced row echelon form of a ``DomainMatrix`` with denominator. This function is the implementation of :meth:`DomainMatrix.rref_den`. Chooses the best algorithm depending on the domain, shape, and sparsity of the matrix as well as things like the bit count in the case of :ref:`ZZ` or :ref:`QQ`. The result is returned over the same domain as the input matrix unless ``keep_domain=False`` in which case the result might be over an associated ring or field domain. See Also ======== sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den The ``DomainMatrix`` method that calls this function. sympy.polys.matrices.rref._dm_rref Alternative function for computing RREF without denominator. Tr rrrrrr) rrrrrdomain clear_denomselementonerr) rr)r rrr r#r!r"r$r%M_rref_rs r& _dm_rref_denr0Ys(-QDIOFGAw'JAw ~-a0V 4&y|4& 8??ahh6 --d-;IAvQq \*22mm''F--##C 4&&t&42 / 3#v 8??ahh6x(3.F((,,CFQq \*22mm''4VH=>>67+IFA 3 r(c|jj}||k(r ||fS|dk(r|j}||fS|dk(r|j}||fSt d|)z?Convert a matrix to the given format and return the old format.densesparsezUnknown format: )repfmtto_dense to_sparser)rr5rs r&rrszeeiiG#~  g:  JJL g:  KKM g:+C5122r(c`|jjdk(r t|St|S)z:Compute RREF using Gauss-Jordan elimination with division.r3)r4r5_dm_rref_GJ_sparse_dm_rref_GJ_densers r&rrs(uuyyH!!$$ ##r(c`|jjdk(r t|St|S)z:Compute RREF using fraction-free Gauss-Jordan elimination.r3)r4r5_dm_rref_den_FF_sparse_dm_rref_den_FF_denser;s r&rrs(uuyyH%a(($Q''r(ct|j\}}}t||j|j}t |}|j ||fS)zACompute RREF using sparse Gauss-Jordan elimination with division.)rr4rshaper+tuplefrom_rep)rM_rref_dr!r$ M_rref_sdms r&r9r9sJ#AEE*HfaXqww1J 6]F ::j !6 ))r(cb|jjxs|jj}|jj j }t ||}t||j|j}t|}|j|j|fS)z@Compute RREF using dense Gauss-Jordan elimination with division.)_partial_pivot) r+is_RRis_CCr4to_ddmcopyrrr@rArB to_dfm_or_ddm)r partial_pivotddmr! M_rref_ddms r&r:r:s|HHNN4ahhnnM %%,,.   C s= 9FS!''188,J 6]F ::j..0 16 99r(ct|j|j\}}}t||j|j}t |}|j |||fSzACompute RREF using sparse fraction-free Gauss-Jordan elimination.)rr4r+rr@rArB)rrCr#r!rDs r&r=r=sR(9Hc6Xqww1J 6]F ::j !3 ..r(c |jjj}t||j\}}t ||j |j}t|}|j|j||fSrP) r4rIrJr r+rr@rArBrK)rrMr#r!rNs r&r>r>sl %%,,.   CQXX.KCS!''188,J 6]F ::j..0 13 >>r(Fr c|dk7r,|jdr|dtd }d}||fSd}||fSd}|j}|jrt ||}||fS|j rt ||}||fS|js |jrd}d}||fS|jr#|jjdk(r |sd}d}||fS|rd}||fSd}||fS) z3Choose the fastest method for computing RREF for M.r _denseNr2r3r rr) endswithlenr+is_ZZ_dm_rref_choose_method_ZZis_QQ_dm_rref_choose_method_QQrGrHis_EXr4r5)rr rrKs r&rrs ??8 $Oc(m^,FGJ 7?GGF 7?? HH 77.qkJF4 7?3WW.qkJF0 7?/WWFG( 7?'WWg-kFG 7?  7? 7?r(crt|\}}}|td|dz kryt|\}}t|Dcgc]}|j c}d}t j } |D]0} t j| | } | j d|zkDs0y| j dkryycc}w) z5Choose the fastest method for computing RREF over QQ.rdefault2rr)_dm_row_densitymin_dm_QQ_numers_denomsmax bit_lengthrr.lcm) rrdensityr$ncolsnumersdenomsn numer_bits denom_lcmds r&rYrYs(*GQQa *!,NFFf5alln5qAJI FF9a(    !AjL 0")6sB4c,d}t|\}}}|dkr ||dz kryy|dkry|d||z zkDryt|}t|Dcgc]}|jc}d}tdd |z|z } d|||dzzz z| z} || kryycc}w) z5Choose the fastest method for computing RREF over ZZ.i' r^rrr]r_r`gUUUUUU?)rc _dm_elementsrfrg) rrPARAMrinrows_nzrjelementsebitswideness max_densitys r&rWrWFs$ E /q1GXu"} U1W { 1uX~% %AH 11 11 =D1c%i()HuhtQw.//8;K2sBc|jd}|jjj}|sdd|fSt |}t t t||z }|||fS)aDensity measure for sparse matrices. Defines the "density", ``d`` as the average number of non-zero entries per row except ignoring rows that are fully zero. RREF can ignore fully zero rows so they are excluded. By definition ``d >= 1`` except that we define ``d = 0`` for the zero matrix. Returns ``(density, nrows_nz, ncols)`` where ``nrows_nz`` counts the number of nonzero rows and ``ncols`` is the number of columns. r_r)r@r4to_sdmvaluesrUsummap)rrjrows_nzruris r&rcrc|sd GGAJEeelln##%G !U{w<c#w'(83%''r(c,|j\}}|S)z*Return nonzero elements of a DomainMatrix.) to_flat_nz)rrvr$s r&rsrss,,.KHa Or(ct|}|Dcgc]}|j}}|Dcgc]}|j}}||fScc}wcc}w)zBReturns the numerators and denominators of a DomainMatrix over QQ.)rs numeratorr)Mqrvrwrkrls r&reresLBH#+ ,aakk ,F ,%- .amm .F . 6>- .s AAcV|j}|jr|jS|S)z.Convert a DomainMatrix to a field if possible.)r+has_assoc_Fieldto_field)rr[s r&rrs% Azz|r(N)sympy.polys.domainsrsympy.polys.matrices.sdmrrrsympy.polys.matrices.ddmrsympy.polys.matrices.denserr r'r0rrrr9r:r=r>rrYrWrcrsrerr(r&rs<#AA(?!1h$(K\ "$(*:/?6;+\16*Z163l(, r(