
    gn                         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dS )    )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>|                                                                d                                         }t          |          \  }}}	|r|d         |k    rdS t          ||dz   |          }
t          ||j        |||	          \  }}t          t                    }|
                                D ]9\  }}|||                                      |                    |                     :t%          ||          D ]^\  }}||         }|                                D ]<\  }}|||                                      ||                    |          z             =_d |                                D             }t&          j        }t+          |          t+          |          z
  D ]}|||<   |S )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
   c                 (    i | ]\  }}|t          | S  r   ).0stermss      Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/polys/matrices/linsolve.py
<dictcomp>z_linsolve.<locals>.<dictcomp>m   s"    
6
6
6ha1c5k
6
6
6    )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   Zeroset)eqssymsnsymseqsdictconstAaugKArrefpivotsnzcolsPV	nonpivotssolivnpiVisymzeror   s                        r   	_linsolver?   0   s    0 IIE (T22NGUGUD11DA
 	~ 0* 0{{}}!!##A&--// &dOOE66  &*%%t 	!a88A +5!%OOLAy d

C		 + +1DGAJJqMM****y!$$ 5 5R3iHHJJ 	5 	5DAqQLajjmm 34444	5 7
6#))++
6
6
6C 6DYYS!  A Jr   c                 B    t          |          j        d | D              }t          |dd          \  }}t          t	          ||                    t          |           }t          |          }t          t	          |t          |                              g }t	          | |          D ]K\  }	}
fd|	                                D             }|
r|
          ||<   |r|                    |           Lt          t          |          ||dz   f|          }|S )z?Convert a system of dict equations to a sparse augmented matrixc              3   >   K   | ]}|                                 V  d S )N)values)r   es     r   	<genexpr>z#sympy_dict_to_dm.<locals>.<genexpr>z   s*       @ @ @ @ @ @ @ @r   T)field	extensionc                 4    i | ]\  }}|         |         S r   r   )r   r   celem_map	sym2indexs      r   r   z$sympy_dict_to_dm.<locals>.<dictcomp>   s'    CCC1)A,CCCr   r
   )r*   unionr   dictr(   r   ranger%   r&   r   	enumerate)
eqs_coeffseqs_rhsr,   elemsr1   elems_Kneqsr-   r.   eqrhseqdictsdm_augrI   rJ   s                @@r   r   r   x   s)   CLL @ @Z @ @ @AE!%ttDDDJAwCw''((Hz??DIIESuU||,,--IGz7++ # #CCCCCC

CCC 	+%c]NF5M 	#NN6""")G$$tUQY&7;;GNr   c                    g }g }t          |          }| D ]}|j        rt          |j        |          \  }}t          |j        |          \  }}	||z  }|	                                D ] \  }
}|
|v r||
xx         |z  cc<   | ||
<   !d |                                D             }||}}nt          ||          \  }}|                    |           |                    |           ||fS )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])
    c                     i | ]
\  }}|||S r   r   )r   kr:   s      r   r   z&_linear_eq_to_dict.<locals>.<dictcomp>   s#    999daq9Q999r   )r*   is_Equality_lin_eq2dictlhsrU   r%   r&   )r+   r,   coeffsindsymsetrC   coeffr   cRtRrZ   r:   rH   ds                 r   r   r      s    F
CYYF  = 	+'v66LE5!!%00FB RKE

 " "1::!HHHMHHHH !rE!HH99ekkmm999E%qAA6**DAqa

13;r   c                 D   | |v rt           j        | t           j        ifS | j        rt	          t
                    }g }| j        D ]_}t          ||          \  }}|                    |           |	                                D ] \  }}||                             |           !`t          | d |	                                D             }	|	fS | j        rdx}	}
g }| j        D ]R}t          ||          \  }}|s|                    |           -|	|}	|}
4t          t          d| z                      t          j        |          |	i fS fd|		                                D             }	|
z  |	fS |                     |          s| i fS t          d| z            )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})
    c                 (    i | ]\  }}|t          | S r   r   )r   r=   r^   s      r   r   z _lin_eq2dict.<locals>.<dictcomp>   s"    III{sFc6lIIIr   Nz-
                    nonlinear cross-term: %sc                 "    i | ]\  }}||z  S r   r   )r   r=   rH   ra   s      r   r   z _lin_eq2dict.<locals>.<dictcomp>   s#    @@@QS%!)@@@r   znonlinear term: %s)r   r)   Oneis_Addr   r$   argsr\   r&   r%   r   is_Mulr	   r   r   
_from_args	has_xfree)ar`   
terms_list
coeff_listaicitimijcijr   terms_coeffra   s              @r   r\   r\      s     	F{{v15z!!	
 #; &&

& 	, 	,B!"f--FBb!!!HHJJ , ,S3&&s++++,Z IIj6F6F6H6HIIIe|	
 ;""
& 	6 	6B!"f--FB 	6!!"%%%%  ) 502354 *5 *5 6 6 6z**="9@@@@%++--@@@EK'..[[   ;"u !5!9:::r   N)collectionsr   sympy.core.addr   sympy.core.mulr   sympy.core.singletonr   sympy.polys.constructorr   sympy.polys.solversr	   sdmr   r   r   r   sympy.utilities.miscr   r?   r   r   r\   r   r   r   <module>r      s  : $ # # # # #             " " " " " " 4 4 4 4 4 4 2 2 2 2 2 2            , + + + + +E E EP  &# # #L5; 5; 5; 5; 5;r   