K i4dZddlmZddlmZddlmZddlmZm Z ddl m Z ddl m Z mZddl mZdd lmZdd lmZdd lmZGd d eZGdde ZdZdZddZdZdZy)z!Low-level linear systems solver. )sympy_deprecation_warning)connected_components)sympify)IntegerRational)MutableDenseMatrix)ZZQQ)EX)sring) NotInvertible) DomainMatrixceZdZdZy)PolyNonlinearErrorz/Raised by solve_lin_sys for nonlinear equationsN)__name__ __module__ __qualname____doc__Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/solvers.pyrrs9rrc(eZdZdZedZdZy) RawMatrixa} .. deprecated:: 1.9 This class fundamentally is broken by design. Use ``DomainMatrix`` if you want a matrix over the polys domains or ``Matrix`` for a matrix with ``Expr`` elements. The ``RawMatrix`` class will be removed/broken in future in order to reestablish the invariant that the elements of a Matrix should be of type ``Expr``. c|SNr)xargskwargss rzRawMatrix."sqrctdddt}t|jD]}t|jD]}|||f}t |ddr*|j |j}|j}nt|dr"|j}|j|}n|t|ttfrt}t|}nTt|t r t"}|}n;tt"fD]&}|j%|s|j|}nt&|j)|}||||f<"||_y)Nzs The RawMatrix class is deprecated. Use either DomainMatrix or Matrix instead. z1.9zdeprecated-rawmatrix)deprecated_since_versionactive_deprecations_targetis_PolyFparent)rr rangerowscolsgetattrdomaingensas_exprhasattrr$to_sympy isinstanceintrrrr of_type TypeErrorunifyring) selfrrr)ijvalK val_sympys r__init__zRawMatrix.__init__$s.! &+'=  tyy! &A499% &1Q3i3 51 388,A # IS(+ A ! 3Ic7^4A ' IX.A #IV(99S>() 3I!( (a%QqS - & &0 rN)rrrr staticmethod_sympifyr:rrrrrs 89H#rrct|Dcic]\}}|| }}}t|}t|dz}t|D cgc]} |jg|z} } t | ||D]J\} } } | j D]\}}|j || ||<|j |  | d<Lt| ||f|Scc}}wcc} w)a:Get matrix from linear equations in dict format. Explanation =========== Get the matrix representation of a system of linear equations represented as dicts with low-level DomainElement coefficients. This is an *internal* function that is used by solve_lin_sys. Parameters ========== eqs_coeffs: list[dict[Symbol, DomainElement]] The left hand sides of the equations as dicts mapping from symbols to coefficients where the coefficients are instances of DomainElement. eqs_rhs: list[DomainElements] The right hand sides of the equations as instances of DomainElement. gens: list[Symbol] The unknowns in the system of equations. domain: Domain The domain for coefficients of both lhs and rhs. Returns ======= The augmented matrix representation of the system as a DomainMatrix. Examples ======== >>> from sympy import symbols, ZZ >>> from sympy.polys.solvers import eqs_to_matrix >>> x, y = symbols('x, y') >>> eqs_coeff = [{x:ZZ(1), y:ZZ(1)}, {x:ZZ(1), y:ZZ(-1)}] >>> eqs_rhs = [ZZ(0), ZZ(-1)] >>> eqs_to_matrix(eqs_coeff, eqs_rhs, [x, y], ZZ) DomainMatrix([[1, 1, 0], [1, -1, 1]], (2, 3), ZZ) See also ======== solve_lin_sys: Uses :func:`~eqs_to_matrix` internally ) enumeratelenr%zerozipitemsconvertr) eqs_coeffseqs_rhsr*r)nr sym2indexnrowsncols_r&roweq_coeffeq_rhssymcoeffs r eqs_to_matrixrRJs\#,D/2$!QA2I2  OE IME+0< 8aV[[ME ! 8D 8!$T:w!?*Xv"..* 8JC"(.."7C #  8>>&))B* uenf 553 9s CC c t||dd\}}||jfS#t$rt||t\}}Y2wxYw)aConvert a system of equations from Expr to a PolyRing Explanation =========== High-level functions like ``solve`` expect Expr as inputs but can use ``solve_lin_sys`` internally. This function converts equations from ``Expr`` to the low-level poly types used by the ``solve_lin_sys`` function. Parameters ========== eqs: List of Expr A list of equations as Expr instances symbols: List of Symbol A list of the symbols that are the unknowns in the system of equations. Returns ======= Tuple[List[PolyElement], Ring]: The equations as PolyElement instances and the ring of polynomials within which each equation is represented. Examples ======== >>> from sympy import symbols >>> from sympy.polys.solvers import sympy_eqs_to_ring >>> a, x, y = symbols('a, x, y') >>> eqs = [x-y, x+a*y] >>> eqs_ring, ring = sympy_eqs_to_ring(eqs, [x, y]) >>> eqs_ring [x - y, x + a*y] >>> type(eqs_ring[0]) >>> ring ZZ(a)[x,y] With the equations in this form they can be passed to ``solve_lin_sys``: >>> from sympy.polys.solvers import solve_lin_sys >>> solve_lin_sys(eqs_ring, ring) {y: 0, x: 0} T)field extension)r))r r r to_domain)eqssymbolsr8eqs_Ks rsympy_eqs_to_ringrZsR^2gTTB5 !++-  2gb152s&AAc:| }jjsJ|Dcgc] }t|}}jj d}jj }g}g} |D]} | j ||} i} | jD]A\} }t| dk7r d}t||| j| jd<C| s| spy|j| | j| t| |}|}|r{fd}|jDcic]\}}||||}}}i}|jD]2\}}|jr|j\}}||z ||<.|||<4|Scc}wcc}}w)aSolve a system of linear equations from a PolynomialRing Explanation =========== Solves a system of linear equations given as PolyElement instances of a PolynomialRing. The basic arithmetic is carried out using instance of DomainElement which is more efficient than :class:`~sympy.core.expr.Expr` for the most common inputs. While this is a public function it is intended primarily for internal use so its interface is not necessarily convenient. Users are suggested to use the :func:`sympy.solvers.solveset.linsolve` function (which uses this function internally) instead. Parameters ========== eqs: list[PolyElement] The linear equations to be solved as elements of a PolynomialRing (assumed equal to zero). ring: PolynomialRing The polynomial ring from which eqs are drawn. The generators of this ring are the unknowns to be solved for and the domain of the ring is the domain of the coefficients of the system of equations. _raw: bool If *_raw* is False, the keys and values in the returned dictionary will be of type Expr (and the unit of the field will be removed from the keys) otherwise the low-level polys types will be returned, e.g. PolyElement: PythonRational. Returns ======= ``None`` if the system has no solution. dict[Symbol, Expr] if _raw=False dict[Symbol, DomainElement] if _raw=True. Examples ======== >>> from sympy import symbols >>> from sympy.polys.solvers import solve_lin_sys, sympy_eqs_to_ring >>> x, y = symbols('x, y') >>> eqs = [x - y, x + y - 2] >>> eqs_ring, ring = sympy_eqs_to_ring(eqs, [x, y]) >>> solve_lin_sys(eqs_ring, ring) {y: 1, x: 1} Passing ``_raw=False`` returns the same result except that the keys are ``Expr`` rather than low-level poly types. >>> solve_lin_sys(eqs_ring, ring, _raw=False) {x: 1, y: 1} See also ======== sympy_eqs_to_ring: prepares the inputs to ``solve_lin_sys``. linsolve: ``linsolve`` uses ``solve_lin_sys`` internally. sympy.solvers.solvers.solve: ``solve`` uses ``solve_lin_sys`` internally. rr>z+Nonlinear term encountered in solve_lin_sysNcft|dd}|r|Sjj|S)Nr+)r(r)r-)rr+r3s rr-zsolve_lin_sys..to_sympys1aD1Gy {{++A..r)r)is_FielddictonemonomsrBpoprDsumrr*indexappend_solve_lin_sysis_Mul as_coeff_Mul)rWr3_rawr+eqeqs_dict one_monomrBrGrFeq_dictrO eq_coeffsmonomrQmsgresultr-rPr7tresultkvcss ` r solve_lin_sysrvsBhG ;;   #&'RR'H'!!$I ;;  DGJ%Y- #MMO 9LE55zQC(--38Idii A/ 0  9 v)$% J 6F g /AG OHC8C=(3-/OOMMO DAqxx~~'1aCq q   M[(DPs F-Fc F|j}g}|D].}t|}|jt|dd|dd0||f}t |}i} t |D]\} } | D]} | | | < t t|D cgc]} ggf}} t||D]G\}}tt|d} || | \}}|j||j|Ii}|D]*}t|d|d|}|y|j|,|Scc} w)aaSolve a linear system from dict of PolynomialRing coefficients Explanation =========== This is an **internal** function used by :func:`solve_lin_sys` after the equations have been preprocessed. The role of this function is to split the system into connected components and pass those to :func:`_solve_lin_sys_component`. Examples ======== Setup a system for $x-y=0$ and $x+y=2$ and solve: >>> from sympy import symbols, sring >>> from sympy.polys.solvers import _solve_lin_sys >>> x, y = symbols('x, y') >>> R, (xr, yr) = sring([x, y], [x, y]) >>> eqs = [{xr:R.one, yr:-R.one}, {xr:R.one, yr:R.one}] >>> eqs_rhs = [R.zero, -2*R.one] >>> _solve_lin_sys(eqs, eqs_rhs, R) {y: 1, x: 1} See also ======== solve_lin_sys: This function is used internally by :func:`solve_lin_sys`. Nr?r>r) r*listextendrCrr@r%rAnextiterrd_solve_lin_sys_componentupdate)rFrGr3VErmsymsG componentssym2comprH componentrPrL subsystemsrNrO sub_coeffsub_rhssol subsystemsubsols rrere0sY< A A+ I T#2YQR)*+ 1A%a(JH!*- 9 CHSM %*#j/$:;q2r(;J; G4&4>4(' 6 7"v  C ))A, ! dK > 6  J>> from sympy import symbols, sring >>> from sympy.polys.solvers import _solve_lin_sys_component >>> x, y = symbols('x, y') >>> R, (xr, yr) = sring([x, y], [x, y]) >>> eqs = [{xr:R.one, yr:-R.one}, {xr:R.one, yr:R.one}] >>> eqs_rhs = [R.zero, -2*R.one] >>> _solve_lin_sys_component(eqs, eqs_rhs, R) {y: 1, x: 1} See also ======== solve_lin_sys: This function is used internally by :func:`solve_lin_sys`. r?Nr3c3LK|]}|s ||zywrr).0r6echelongr5s r z+_solve_lin_sys_component..s1$eAW^_`WabcWdWQZ]1Q4%7$es $$r>)rRr*r)r]to_fieldrrefrArepto_ddmrdr^rCr,r3 ground_newr@rbr%)rFrGr3matrixpivotskeysrrMruasolsrErsvals_setvals_mapeieijprrr5s @@@rr|r|msB:w 4;; GF == ! !"kkmOGV 99D &*D ) 6{c$i%,[[%7%7%9:c#b': AA JJqM CcN#" K II 4 ii**GooG++$$&!(6##6QA6A66+34aAwqzM44;BCRR0cHSM0Cf% DAq 2$eqsCPQFAS$e!eeADaM  K);740Cs*4 G,G1%G7< H G< H<HN)T)rsympy.utilities.exceptionsrsympy.utilities.iterablesrsympy.core.sympifyrsympy.core.numbersrrsympy.matrices.densersympy.polys.domainsr r r sympy.polys.ringsr sympy.polys.polyerrorsr sympy.polys.domainmatrixr ExceptionrrrRrZrvrer|rrrrsb'A:&03&"#01  0"0f76t4 nrj:zFr