o
    8Va]                     @   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   ?/usr/lib/python3/dist-packages/sympy/polys/matrices/linsolve.py
<dictcomp>j       z_linsolve.<locals>.<dictcomp>)len_linear_eq_to_dictsympy_dict_to_dmdomainZis_RealFieldZis_ComplexFieldZto_ddmZrrefZto_sdmr   r   r   Zoner   listitemsappendZto_sympyzipr   Zeroset)eqssymsnsymseqsdictrhsZAaugKZArrefZpivotsZnzcolsPVZ	nonpivotsZsolivZnpiZVisymZ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)Zfield	extensionc                    s   i | ]\}}|  | qS r   r   )r   r   cZelem_mapZ	sym2indexr   r   r      s    z$sympy_dict_to_dm.<locals>.<dictcomp>r	   )r    unionr   dictr   r   ranger   r   r
   	enumerate)Z
eqs_coeffseqs_rhsr"   Zelemsr&   Zelems_KZneqsr#   r$   eqr%   eqdictZsdm_augr   r3   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)r9   r   r   r   	expand_eq   s   z)_expand_eqs_deprecated.<locals>.expand_eqc                    s   g | ]} |qS r   r   )r   r9   r>   r   r   
<listcomp>   s    z*_expand_eqs_deprecated.<locals>.<listcomp>r   )r!   r   r?   r   _expand_eqs_deprecated   s   rA   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   rA   )r!   r"   r   r   r   r      s   r   c                 C   sF   t |}g g }}| D ]}t||\}}|| || q||fS rB   )r    _lin_eq2dictr   )r!   r"   r$   r8   r9   r%   r:   r   r   r   rC      s   

rC   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+   Zcoeffsr   r   r   r      r   z _lin_eq2dict.<locals>.<dictcomp>Nc                    s   i | ]	\}}| | qS r   r   )r   r+   r2   Zcoeffr   r   r      r   )r   r   ZOneZis_Addr   r   argsrD   r   r   r   Zis_Mulr   r   r;   r<   r%   Zfree_symbols)aZsymsetZ
terms_listZ
coeff_listZaiZciZtiZmijZcijr   Zterms_coeffr   rE   r   rD      sF   



rD   N)collectionsr   Zsympy.core.addr   Zsympy.core.mulr   Zsympy.core.singletonr   Zsympy.polys.constructorr   Zsympy.polys.solversr   Zsdmr
   r   r   r   r,   r   rA   r   rC   rD   r   r   r   r   <module>   s   G
