B
    ˜‘[‘  ã               @   sJ   d Z ddlmZmZ ddlmZmZ G dd„ deƒZdd„ Zdd	d
„Z	dS )z!Low-level linear systems solver. é    )Úprint_functionÚdivision)ÚMatrixÚzerosc               @   s    e Zd Zedd„ ƒZdd„ ZdS )Ú	RawMatrixc             C   s   | S )N© )Úxr   r   ú2lib/python3.7/site-packages/sympy/polys/solvers.pyÚ<lambda>   s    zRawMatrix.<lambda>c              C   s   ddl m}  |  t¡S )Nr   )ÚMatrixShaping)Úsympy.matricesr   Úis_zeroÚself)r   r   r   r	   r   
   s    zRawMatrix.is_zeroN)Ú__name__Ú
__module__Ú__qualname__ÚstaticmethodZ_sympifyr   r   r   r   r	   r      s   r   c             C   sv   |j }tt| ƒt|ƒd td}xPt| ƒD ]D\}}x&t|ƒD ]\}}| |¡|||f< q<W | d¡ ||df< q*W |S )z)Transform from equations to matrix form. é   )Úclséÿÿÿÿ)Úgensr   Úlenr   Ú	enumerateZcoeff)ÚeqsÚringZxsÚMÚjZe_jÚiZx_ir   r   r	   Úeqs_to_matrix   s    r   Tc             C   s>  | }|j jst‚t| |ƒ}|jdd„ dd„ d\}}|r@|jn|j}|d t|ƒkrZdS t|ƒt|ƒkr¸g }x8|dd…df D ]$}	| |	¡}
|rš|
 	¡ }
| 
|
¡ q€W tt||ƒƒ}n‚i }|j}dd„ |D ƒ}xht|ƒD ]\\}}t||d	 d… |jgg ƒ}|||d	 d…f | d
 }|r*| 	¡ }|||| < qÚW |S )a%  Solve a system of linear equations.

    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.
    c             S   s   |  S )Nr   )r   r   r   r	   r
   *   s    zsolve_lin_sys.<locals>.<lambda>c             S   s   | S )Nr   )r   r   r   r	   r
   *   s    )Z
iszerofuncZsimplifyr   Nc             S   s   g | ]}| g‘qS r   r   )Ú.0r   r   r   r	   ú
<listcomp>=   s    z!solve_lin_sys.<locals>.<listcomp>r   r   )ZdomainZis_FieldÚAssertionErrorr   ZrrefZsymbolsr   r   Z
ground_newÚas_exprÚappendÚdictÚzipr   r   Zone)r   r   Z_rawr"   ZmatrixZechelonZpivotsÚkeysZsolÚsÚaZsolsÚgZ_gr   ÚpZvectÚvr   r   r	   Úsolve_lin_sys   s2    

r,   N)T)
Ú__doc__Z
__future__r   r   r   r   r   r   r   r,   r   r   r   r	   Ú<module>   s
   