
    \h4                         S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	  SSK
Jr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJr   " S S\5      r " S S\5      rS rS rSS jrS rS rg)z!Low-level linear systems solver.     )sympy_deprecation_warning)connected_components)sympify)IntegerRational)MutableDenseMatrix)ZZQQ)EX)sring)NotInvertible)DomainMatrixc                       \ rS rSrSrSrg)PolyNonlinearError   z/Raised by solve_lin_sys for nonlinear equations N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/solvers.pyr   r      s    9r   r   c                   0    \ rS rSrSr\" S 5      rS rSrg)	RawMatrix   aa  
.. 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                     U $ Nr   )xargskwargss      r   <lambda>RawMatrix.<lambda>"   s    qr   c                    [        SSSS9  [        n[        U R                  5       GH:  n[        U R                  5       GH  nXU4   n[        USS5      (       a*  UR                  UR                     nUR                  5       nO[        US5      (       a"  UR                  5       nUR                  U5      nO[        U[        [        45      (       a  [        n[        U5      nO`[        U[         5      (       a	  ["        nUnOB[        ["        4 H,  nUR%                  U5      (       d  M  UR                  U5      n  O   [&        eUR)                  U5      nXXE4'   GM     GM=     X0l        g )Nzs
            The RawMatrix class is deprecated. Use either DomainMatrix or
            Matrix instead.
            z1.9zdeprecated-rawmatrix)deprecated_since_versionactive_deprecations_targetis_PolyFparent)r   r	   rangerowscolsgetattrdomaingensas_exprhasattrr)   to_sympy
isinstanceintr   r   r   r
   of_type	TypeErrorunifyring)	selfr!   r"   r.   ijvalK	val_sympys	            r   __init__RawMatrix.__init__$   s%   ! &+'=	
 tyy!A499%Q3i3	511

388,A #IS(++

A !

3Ic7^44A 'IX..A #IV99S>>()

3I! $
 (a%QS	- & "0 	r   )r8   N)	r   r   r   r   r   staticmethod_sympifyr?   r   r   r   r   r   r      s    	 89H#r   r   c                    [        U5       VVs0 s H  u  pEXT_M	     nnn[        U 5      n[        U5      S-   n[        U5       V	s/ s H  oR                  /U-  PM     n
n	[	        XU5       HJ  u  pnUR                  5        H  u  pUR                  U5      XU   '   M     UR                  U5      * US'   ML     [        XU4U5      $ s  snnf s  sn	f )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
   )	enumeratelenr*   zerozipitemsconvertr   )
eqs_coeffseqs_rhsr/   r.   nr    	sym2indexnrowsncols_r+   roweq_coeffeq_rhssymcoeffs                   r   eqs_to_matrixrX   J   s    \ #,D/2/$!/I2
OEIME+0<8<a[[ME!<D8!$Tw!?v"..*JC"(.."7C# +>>&))B "@
 enf55 3 9s   C	Cc                      [        XSSS9u  p#X2R                  5       4$ ! [         a    [        X[        S9u  p# N-f = f)as  Convert 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])
<class 'sympy.polys.rings.PolyElement'>
>>> 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)eqssymbolsr=   eqs_Ks       r   sympy_eqs_to_ringr`      sJ    ^2TTB ++-  2b152s     ==c                   ^ U(       + nTR                   R                  (       d   eU  Vs/ s H  n[        U5      PM     nnTR                  R	                  5       S   nTR                   R
                  n/ n/ n	U H  n
U
R                  Xg5      n0 nU
R                  5        HA  u  p[        U5      S:w  a  Sn[        U5      eXTR                  UR                  S5         '   MC     U(       d  U(       d  M{    gUR                  U5        U	R                  U5        M     [        XT5      nUb  U(       a  U4S jnUR                  5        VVs0 s H  u  nnU" U5      U" U5      _M     nnn0 nUR                  5        H9  u  nnUR                  (       a  UR                  5       u  nnUU-  UU'   M4  UUU'   M;     U$ s  snf s  snnf )a  Solve 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.
r   rD   z+Nonlinear term encountered in solve_lin_sysNc                 p   > [        U SS 5      nU(       a  U" 5       $ TR                  R                  U 5      $ )Nr0   )r-   r.   r2   )r    r0   r8   s     r   r2   solve_lin_sys.<locals>.to_sympy  s1    aD1Gy {{++A..r   )r.   is_FielddictonemonomsrH   poprJ   sumr   r/   indexappend_solve_lin_sysis_Mulas_coeff_Mul)r]   r8   _rawr0   eqeqs_dict	one_monomrH   rM   rL   eq_dictrU   	eq_coeffsmonomrW   msgresultr2   rV   r<   tresultkvcss    `                       r   solve_lin_sysr}      s   B hG;;#&'3RR3H'!!$I;;DGJY-	#MMOLE5zQC(--38diiA/0	 ,
 v)$   J6Fg	/ AGOHC8C=(3-/O MMODAqxx~~'1aCq	q	 $ M[ (D Ps   F<Gc           	      X   UR                   n/ nU  H/  n[        U5      nUR                  [        USS USS 5      5        M1     X44n[	        U5      n0 n	[        U5       H  u  pU H  nXU'   M	     M     [        [        U5      5       Vs/ s H  n/ / 4PM	     nn[        X5       HG  u  nn[        [        U5      S5      nXU      u  nnUR                  U5        UR                  U5        MI     0 nU H,  n[        US   US   U5      nUc    gUR                  U5        M.     U$ s  snf )a  Solve 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`.
NrE   rD   r   )r/   listextendrI   r   rF   r*   rG   nextiterrk   _solve_lin_sys_componentupdate)rL   rM   r8   VErt   symsG
componentssym2comprN   	componentrV   rR   
subsystemsrT   rU   	sub_coeffsub_rhssol	subsystemsubsols                         r   rl   rl   0  s9   < 			A
A	I	T#2YQR)*   	
A%a(JH!*-CSM  . %*#j/$:;$:q2r($:J;
4&4>4('6	7"v	 5 C	))A,	!dK>

6	   J <s   D'c                 N  ^^^ [        XUR                  UR                  5      nUR                  R                  (       d  UR	                  5       nUR                  5       u  mnUR                  nU(       a  US   [        U5      :X  a  g[        U5      [        U5      :X  a_  / nTR                  R                  5        Vs/ s H  owS   PM	     sn H  nUn	UR                  U	5        M     [        [        XV5      5      n
U
$ 0 n
UR                  m[        US5      (       a  UR                  R                  nOUR                  nTR                  R                  5       mT VVs1 s H  ow  H  oiM     M     nnnU Vs0 s H  oU" U5      _M     nnT VVs/ s H  o Vs/ s H  nUU   PM
     snPM     snnm[        U5       HC  u  mnTT   S   [!        UUU4S j[#        US-   [        T5      5       5       5      -
  nXUU   '   ME     U
$ s  snf s  snnf s  snf s  snf s  snnf )a  Solve 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. After :func:`_solve_lin_sys` splits the
system into connected components this function is called for each
component. The system of equations is solved using Gauss-Jordan
elimination with division followed by back-substitution.

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_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`.
rE   Nr8   c              3   ^   >#    U  H"  nTT   U   (       d  M  TT   U   TU   -  v   M$     g 7fr   r   ).0r;   echelongr:   s     r   	<genexpr>+_solve_lin_sys_component.<locals>.<genexpr>  s6     $eASAW^_`WabcWd%7WQZ]1Q4%7ASs   --rD   )rX   r/   r.   rd   to_fieldrrefrG   repto_ddmrk   re   rI   r1   r8   
ground_newrF   ri   r*   )rL   rM   r8   matrixpivotskeysr   rS   r|   asolsrK   rz   vals_setvals_mapeieijpr   r   r:   s                     @@@r   r   r   m  s   B :		4;;GF ==!!" kkmOGV 99D&*D	)
6{c$i%,[[%7%7%9:%9cb'%9:AAJJqM ; CN#" K II4  ii**GooG++$$&!(6##QA#A6+348awqzM84;BC7RR0RcHSMR07Cf%DAq
2$eqsCPQFAS$e!eeAaM & K) ; 740Cs*   ?H!H<H	H!H-H!H!N)T)r   sympy.utilities.exceptionsr   sympy.utilities.iterablesr   sympy.core.sympifyr   sympy.core.numbersr   r   sympy.matrices.denser   sympy.polys.domainsr	   r
   r   sympy.polys.ringsr   sympy.polys.polyerrorsr   sympy.polys.domainmatrixr   	Exceptionr   r   rX   r`   r}   rl   r   r   r   r   <module>r      sb    ' A : & 0 3 & " # 0 1	 	
0" 0f76t4 nrj:zFr   