o
    *ήcY                     @   s   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
 Zdd Zdd Zdd Zdd Zdd ZdS )    )defaultdictAdd)Mul)S)construct_domain)PolyNonlinearError   )SDM	sdm_irrefsdm_particular_from_rrefsdm_nullspace_from_rrefc                 C   sF  t |}t| |\}}t|||}|j}|js|jr$|  d  }t	|\}}}	|r5|d |kr5dS t
||d |}
t||j|||	\}}tt}|
 D ]\}}|||  || qPt||D ]\}}|| }| D ]\}}|||  |||  qrqfdd | D }tj}t|t| D ]}|||< q|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                 S      i | ]	\}}|t | qS  r   ).0stermsr   r   D/tmp/pip-target-vg8gfxp4/lib/python/sympy/polys/matrices/linsolve.py
<dictcomp>j       z_linsolve.<locals>.<dictcomp>)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rhsAaugKArrefpivotsnzcolsPV	nonpivotssolivnpiVisymzeror   r   r   r   	_linsolve.   s0   
r<   c                    s   t |jdd | D  }t|ddd\}}tt|| t| }t|}tt|t|g }t| |D ] \}	}
 fdd|	 D }|
rM |
  ||< |rT|| q4t	t
|||d f|}|S )z?Convert a system of dict equations to a sparse augmented matrixc                 s   s    | ]}|  V  qd S N)values)r   er   r   r   	<genexpr>w   s    z#sympy_dict_to_dm.<locals>.<genexpr>T)field	extensionc                    s   i | ]\}}|  | qS r   r   )r   r   celem_map	sym2indexr   r   r      s    z$sympy_dict_to_dm.<locals>.<dictcomp>r	   )r'   unionr   dictr%   r   ranger"   r#   r
   	enumerate)
eqs_coeffseqs_rhsr)   elemsr.   elems_Kneqsr*   r+   eqr,   eqdictsdm_augr   rD   r   r   u   s    
r   c                    s   dd   fdd| D S )zUse expand to cancel nonlinear terms.

    This approach matches previous behaviour of linsolve but should be
    deprecated.
    c                 S   s   | j r	| j| j } |  S r=   )is_Equalitylhsr,   expand)rP   r   r   r   	expand_eq   s   z)_expand_eqs_deprecated.<locals>.expand_eqc                    s   g | ]} |qS r   r   )r   rP   rV   r   r   
<listcomp>   s    z*_expand_eqs_deprecated.<locals>.<listcomp>r   )r(   r   rW   r   _expand_eqs_deprecated   s   rY   c                 C   s2   zt | |W S  ty   t| } t | | Y S w z1Convert a system Expr/Eq equations into dict form)_linear_eq_to_dict_innerr   rY   )r(   r)   r   r   r   r      s   r   c                 C   sF   t |}g g }}| D ]}t||\}}|| || q||fS rZ   )r'   _lin_eq2dictr#   )r(   r)   r+   rL   rP   r,   rQ   r   r   r   r[      s   

r[   c                    sH  | |v rt j| t jifS | jrHtt}g }| jD ]}t||\}}|| |	 D ]\}}|| | q*qt
|  dd |	 D }	 |	fS | jrd }	}
g }| jD ]}t||\}}|se|| qT|	du rn|}	|}
qTtt|  |	du r| i fS  fdd|		 D }	 |
 |	fS | jrt| j| j |S | j| s| i fS t)z?Efficiently convert a linear equation to a dict of coefficientsc                 S   r   r   r   )r   r:   coeffsr   r   r   r      r   z _lin_eq2dict.<locals>.<dictcomp>Nc                    s   i | ]	\}}| | qS r   r   )r   r:   rC   coeffr   r   r      r   )r   r&   Oneis_Addr   r!   argsr\   r#   r"   r   is_Mulr   r   rS   rT   r,   has_free)asymset
terms_list
coeff_listaicitimijcijr   terms_coeffr   r^   r   r\      sF   



r\   N)collectionsr   sympy.core.addr   sympy.core.mulr   sympy.core.singletonr   sympy.polys.constructorr   sympy.polys.solversr   sdmr
   r   r   r   r<   r   rY   r   r[   r\   r   r   r   r   <module>   s   G
