o
    ohn                     @   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   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   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/matrices/linsolve.py
<dictcomp>m       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constAaugKArrefpivotsnzcolsPV	nonpivotssolivnpiVisymzeror   r   r   r   	_linsolve0   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>z   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,   eqrhseqdictsdm_augr   rD   r   r   x   s    
r   c                 C   s   g }g }t |}| D ]T}|jrMt|j|\}}t|j|\}}	||8 }|	 D ]\}
}|
|v r8||
  |8  < q'| ||
< q'd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   kr8   r   r   r   r      r   z&_linear_eq_to_dict.<locals>.<dictcomp>)r(   is_Equality_lin_eq2dictlhsrQ   r#   r$   )r)   r*   coeffsindsymsetr?   coeffr   cRtRrT   r8   rC   dr   r   r   r      s$   
r   c                    sF  | |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|  t| |	du r i fS  fdd|		 D }	 |
 |	fS | |s| i fS td|  )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   r   r   r   )r   r;   rX   r   r   r   r      r   z _lin_eq2dict.<locals>.<dictcomp>Nz-
                    nonlinear cross-term: %sc                    s   i | ]	\}}| | qS r   r   )r   r;   rC   r[   r   r   r      r   znonlinear term: %s)r   r'   Oneis_Addr   r"   argsrV   r$   r#   r   is_Mulr   r   r   
_from_args	has_xfree)arZ   
terms_list
coeff_listaicitimijcijr   terms_coeffr   r_   r   rV      sF   




rV   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   rV   r   r   r   r   <module>   s   H&