K inddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z mZmZmZdd lmZd Zd Zd Zd Zy)) defaultdict)Add)Mul)S)construct_domain)PolyNonlinearError)SDM sdm_irrefsdm_particular_from_rrefsdm_nullspace_from_rref) filldedentct|}t||\}}t|||}|j}|js |j r/|j jdj}t|\}}} |r |d|k(ryt||dz|} t||j||| \} } tt} | jD]+\}}| ||j!|j#|-t%| | D]K\}}||}|jD].\}}| ||j!||j#|z0M| jDcic]\}}|t'|} }}t(j*}t-|t-| z D]}|| |< | Scc}}w)aSolve a linear system of equations. Examples ======== Solve a linear system with a unique solution: >>> from sympy import symbols, Eq >>> from sympy.polys.matrices.linsolve import _linsolve >>> x, y = symbols('x, y') >>> eqs = [Eq(x + y, 1), Eq(x - y, 2)] >>> _linsolve(eqs, [x, y]) {x: 3/2, y: -1/2} In the case of underdetermined systems the solution will be expressed in terms of the unknown symbols that are unconstrained: >>> _linsolve([Eq(x + y, 0)], [x, y]) {x: -y, y: y} rNr )len_linear_eq_to_dictsympy_dict_to_dmdomain is_RealFieldis_ComplexFieldto_ddmrrefto_sdmr r r onerlistitemsappendto_sympyziprrZeroset)eqssymsnsymseqsdictconstAaugKArrefpivotsnzcolsPV nonpivotssolivnpiVisymstermszeros c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/matrices/linsolve.py _linsolver90s0 IE(T2NGU GUD 1D A  ~~**{{}!!#A&--/&dOE66&*% !a8A+5!%%OLAy d C +1 DG AJJqM*+y!$5R3iHHJ 5DAq QL  ajjm 3 4 55 +.))+ 6ha1c5k> 6C 6 66D YS !A J 7s;Gct|jd|D}t|dd\}}tt ||}t |}t |}tt |t |} g} t ||D]M\} } | jD cic]\} }| | ||}} }| r ||  ||<|s=| j|Ott| ||dzf|}|Scc}} w)z?Convert a system of dict equations to a sparse augmented matrixc3<K|]}|jyw)N)values).0es r8 z#sympy_dict_to_dm..zs @ @sT)field extensionr ) r!unionrdictrrrangerrr enumerate) eqs_coeffseqs_rhsr#elemsr(elems_Kelem_mapneqsr$ sym2indexr%eqrhsr5ceqdictsdm_augs r8rrxs CL   @Z @ AE!%ttDJAwCw'(H z?D IESuU|,-IGz7+#C8: C1)A, +CC %c]NF5M  NN6 " # )G$tUQY&7;G N Ds!C5cg}g}t|}|D]}|jrt|j|\}}t|j|\}} ||z}| j D]\} } | |vr|| xx| zcc<| || <|j D cic] \} } | s | |  }} } ||} } nt||\} } |j | |j | ||fScc} } w)amConvert a system Expr/Eq equations into dict form, returning the coefficient dictionaries and a list of syms-independent terms from each expression in ``eqs```. Examples ======== >>> from sympy.polys.matrices.linsolve import _linear_eq_to_dict >>> from sympy.abc import x >>> _linear_eq_to_dict([2*x + 3], {x}) ([{x: 2}], [3]) )r! is_Equality _lin_eq2dictlhsrNrr)r"r#coeffsindsymsetr>coeffr6cRtRkr1rOds r8rrsF C YF  =='v6LE5!!%%0FB RKE  "1:!HMH !rE!H  " ',kkm9daqQT9E9%qA6*DAq a 1 %& 3; :s  C*&C*c`||vr"tj|tjifS|jrt t }g}|j D]N}t||\}}|j||jD]\}}||j|Pt|} |jD cic]\} } | t| } } } | | fS|jrdx} } g}|j D]B}t||\}}|s|j|&| |} |} -ttd|ztj|} | | ifS| jD cic] \} }| | |z } } }| | z| fS|j!|s|ifStd|zcc} } wcc}} w)areturn (c, d) where c is the sym-independent part of ``a`` and ``d`` is an efficiently calculated dictionary mapping symbols to their coefficients. A PolyNonlinearError is raised if non-linearity is detected. The values in the dictionary will be non-zero. Examples ======== >>> from sympy.polys.matrices.linsolve import _lin_eq2dict >>> from sympy.abc import x, y >>> _lin_eq2dict(x + 2*y + 3, {x, y}) (3, {x: 1, y: 2}) Nz- nonlinear cross-term: %sznonlinear term: %s)rr Oneis_AddrrargsrTrrris_Mulrrr _from_args has_xfree)arX terms_list coeff_listaicitimijcijrYr4rVr6 terms_coeffrOs r8rTrTs  F{vv155z!!  &  && ,B!"f-FB   b !HHJ ,S3&&s+ , , Z 6@6F6F6HI{sFc6l"IIe| ""  && 6B!"f-FB!!"% )502354*566 6z* ="9 27++-@QS%!)^@E@K'. . [[ "u  !5!9::5J*As <F$'F*N) collectionsrsympy.core.addrsympy.core.mulrsympy.core.singletonrsympy.polys.constructorrsympy.polys.solversrsdmr r r r sympy.utilities.miscrr9rrrTr8rxs?:$"42,EP&#L5;rw