
    sgn                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ ddlmZmZmZmZ d d	lmZ d
 Zd Zd Zd Zy)    )defaultdict)Add)Mul)S)construct_domain)PolyNonlinearError   )SDM	sdm_irrefsdm_particular_from_rrefsdm_nullspace_from_rref)
filldedentc                    t        |      }t        | |      \  }}t        |||      }|j                  }|j                  s|j
                  r/|j                         j                         d   j                         }t        |      \  }}}	|r	|d   |k(  ryt        ||dz   |      }
t        ||j                  |||	      \  }}t        t              }|
j                         D ]+  \  }}|||      j!                  |j#                  |             - t%        ||      D ]K  \  }}||   }|j                         D ].  \  }}|||      j!                  ||j#                  |      z         0 M |j                         D ci c]  \  }}|t'        |  }}}t(        j*                  }t-        |      t-        |      z
  D ]  }|||<   	 |S c c}}w )a  Solve 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}

    r   Nr	   )len_linear_eq_to_dictsympy_dict_to_dmdomainis_RealFieldis_ComplexFieldto_ddmrrefto_sdmr   r   r   oner   listitemsappendto_sympyzipr   r   Zeroset)eqssymsnsymseqsdictconstAaugKArrefpivotsnzcolsPV	nonpivotssolivnpiVisymstermszeros                         P/var/www/html/venv/lib/python3.12/site-packages/sympy/polys/matrices/linsolve.py	_linsolver9   0   s   0 IE (T2NGUGUD1DA
 	~~**{{}!!#A&--/ &dOE66 &*% 	!a8A +5!%%OLAy d
C	 +1DGAJJqM*+y!$ 5R3iHHJ 	5DAqQLajjm 34	55 +.))+
6ha1c5k>
6C
6 66DYS! A J 7s   ;Gc                     t        |      j                  d | D         }t        |dd      \  }}t        t	        ||            }t        |       }t        |      }t        t	        |t        |                  }	g }
t	        | |      D ]M  \  }}|j                         D ci c]  \  }}|	|   ||    }}}|r	||    ||<   |s=|
j                  |       O t        t        |
      ||dz   f|      }|S c c}}w )z?Convert a system of dict equations to a sparse augmented matrixc              3   <   K   | ]  }|j                           y w)N)values).0es     r8   	<genexpr>z#sympy_dict_to_dm.<locals>.<genexpr>z   s      @ @s   T)field	extensionr	   )r!   unionr   dictr   r   ranger   r   r
   	enumerate)
eqs_coeffseqs_rhsr#   elemsr(   elems_Kelem_mapneqsr$   	sym2indexr%   eqrhsr5   ceqdictsdm_augs                    r8   r   r   x   s    CL @Z @AE!%ttDJAwCw'(Hz?DIESuU|,-IGz7+ #C8:
C1)A,+CC%c]NF5MNN6"# )G$tUQY&7;GN Ds   !C5c                    g }g }t        |      }| D ]  }|j                  rt        |j                  |      \  }}t        |j                  |      \  }}	||z  }|	j                         D ]  \  }
}|
|v r||
xx   |z  cc<   | ||
<    |j                         D 
ci c]  \  }
}|s	|
| }}
}||}}nt        ||      \  }}|j                  |       |j                  |        ||fS c c}}
w )am  Convert 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_eq2dictlhsrN   r   r   )r"   r#   coeffsindsymsetr>   coeffr6   cRtRkr1   rO   ds                 r8   r   r      s     F
CYF =='v6LE5!!%%0FB RKE
 "1:!HMH !rE!H	" ',kkm9daqQT9E9%qA6*DAqa

1%& 3; :s   
C*&C*c                 `   | |v r"t         j                  | t         j                  ifS | j                  rt	        t
              }g }| j                  D ]N  }t        ||      \  }}|j                  |       |j                         D ]  \  }}||   j                  |        P t        | }	|j                         D 
ci c]  \  }
}|
t        |  }}
}|	|fS | j                  rdx}}g }| j                  D ]B  }t        ||      \  }}|s|j                  |       &||}|}-t        t        d| z               t        j                  |      }	||	i fS |j                         D 
ci c]  \  }
}|
|	|z   }}
}|	|z  |fS | j!                  |      s| i fS t        d| z        c c}}
w c c}}
w )a  return (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: %sznonlinear term: %s)r   r    Oneis_Addr   r   argsrT   r   r   r   is_Mulr   r   r   
_from_args	has_xfree)arX   
terms_list
coeff_listaicitimijcijrY   r4   rV   r6   terms_coeffrO   s                  r8   rT   rT      s     	F{vv155z!!	
 &

&& 	,B!"f-FBb!HHJ ,S3&&s+,	,
 Z 6@6F6F6HI{sFc6l"IIe|	
""
&& 	6B!"f-FB!!"%  ) 502354 *5 6 6	6 z*="927++-@QS%!)^@E@K'..[[ "u !5!9::5 J* As   <F$'F*N)collectionsr   sympy.core.addr   sympy.core.mulr   sympy.core.singletonr   sympy.polys.constructorr   sympy.polys.solversr   sdmr
   r   r   r   sympy.utilities.miscr   r9   r   r   rT        r8   <module>rx      s?   : $   " 4 2  ,EP&#L5;rw   