U
    9%e|                     @   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	lm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_rref)
filldedentc                 C   sH  t |}t| |\}}t|||}|j}|js4|jrH|  d  }t	|\}}}	|rj|d |krjdS t
||d |}
t||j|||	\}}tt}|
 D ] \}}|||  || qt||D ]>\}}|| }| D ]$\}}|||  |||  qqdd | D }tj}t|t| D ]}|||< q4|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   s   i | ]\}}|t | qS  r   ).0stermsr   r   \/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/polys/matrices/linsolve.py
<dictcomp>m   s      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   oner   listitemsappendZto_sympyzipr   Zeroset)eqssymsnsymseqsdictconstZAaugKZArrefZpivotsZnzcolsPVZ	nonpivotsZsolivZnpiZVisymzeror   r   r   r   	_linsolve0   s.     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 }|
r |
  ||< |rh|| qht	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>z   s     z#sympy_dict_to_dm.<locals>.<genexpr>T)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_coeffsZeqs_rhsr"   Zelemsr&   Zelems_KZneqsr#   r$   eqrhsZeqdictZsdm_augr   r4   r   r   x   s    r   c                 C   s   g }g }t |}t| D ]\}}|jrt|j|\}}t|j|\}	}
||	8 }|
 D ],\}}||krx||  |8  < qV| ||< qVdd | D }|| }}nt||\}}|| || q||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                 S   s   i | ]\}}|r||qS r   r   )r   kr*   r   r   r   r      s       z&_linear_eq_to_dict.<locals>.<dictcomp>)r    r8   Zis_Equality_lin_eq2dictlhsr:   r   r   )r!   r"   coeffsindsymsetr)   r/   coeffr   ZcRZtRr;   r*   r3   dr   r   r   r      s$    
r   c                    sT  | |krt j| t jifS | jrtt}g }| jD ]<}t||\}}|| |	 D ]\}}|| | qTq0t
|  dd |	 D }	 |	fS | jr0d }	}
g }| jD ]D}t||\}}|s|| q|	dkr|}	|}
qttd|  qt| |	dkr i fS  fdd|		 D }	 |
 |	fS n | |sD| i fS td|  dS )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                 S   s   i | ]\}}|t | qS r   r   )r   r+   r>   r   r   r   r      s      z _lin_eq2dict.<locals>.<dictcomp>Nz-
                    nonlinear cross-term: %sc                    s   i | ]\}}| | qS r   r   )r   r+   r3   rA   r   r   r      s      znonlinear term: %s)r   r   ZOneZis_Addr   r   argsr<   r   r   r   Zis_Mulr   r   r   Z
_from_argsZ	has_xfree)ar@   Z
terms_listZ
coeff_listZaicitiZmijZcijr   Zterms_coeffr   rC   r   r<      sD    





r<   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   Zsympy.utilities.miscr   r-   r   r   r<   r   r   r   r   <module>   s   H&