o
    8Vap                     @   s   d Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZmZ ddlmZ egZeeegZegZdd	 Zd
d Zdd Zdd Zdd ZdddZdd Z dd Z!dddZ"dS )z SymPy interface to Unification engine

See sympy.unify for module level docstring
See sympy.unify.core for algorithmic docstring     )BasicAddMulPow)AssocOp	LatticeOp)MatAddMatMul
MatrixExpr)UnionIntersection	FiniteSet)CompoundVariableCondVariable)corec                    s&   t tttttf}t fdd|D S )Nc                 3       | ]}t  |V  qd S N
issubclass).0Zaopop 4/usr/lib/python3/dist-packages/sympy/unify/usympy.py	<genexpr>       z$sympy_associative.<locals>.<genexpr>)r   r   r	   r   r   r   any)r   Z	assoc_opsr   r   r   sympy_associative   s   r   c                    s$   t ttttf}t fdd|D S )Nc                 3   r   r   r   )r   Zcopr   r   r   r      r   z$sympy_commutative.<locals>.<genexpr>)r   r   r   r   r   r   )r   Zcomm_opsr   r   r   sympy_commutative   s   r   c                 C   s   t | to	t| jS r   )
isinstancer   r   r   xr   r   r   is_associative   s   r#   c                 C   s@   t | tsdS t| jrdS t| jtrtdd | jD S d S )NFTc                 s   s    | ]}t |jV  qd S r   )	constructis_commutativer   argr   r   r   r   "   r   z!is_commutative.<locals>.<genexpr>)r    r   r   r   r   r   allargsr!   r   r   r   r%      s   

r%   c                    s    fdd}|S )Nc                    s    t |  pt | tot| j S r   )r    r   r   r   r!   typr   r   	matchtype%   s   
zmk_matchtype.<locals>.matchtyper   )r+   r,   r   r*   r   mk_matchtype$   s   r-   r   c                    sV   |  v rt | S t| t tfr| S t| tr| jr| S t| jt fdd| jD S )z% Turn a SymPy object into a Compound c                 3   s    | ]}t | V  qd S r   deconstructr&   	variablesr   r   r   3   r   zdeconstruct.<locals>.<genexpr>)	r   r    r   r   Zis_Atomr   	__class__tupler)   )sr1   r   r0   r   r/   *   s   r/   c                    s   t  ttfr
 jS t  ts S t fddtD r( jtt	 j
ddiS t fddtD rAtj jgtt	 j
R  S  jtt	 j
 S )z% Turn a Compound into a SymPy object c                 3       | ]	}t  j|V  qd S r   r   r   r   clstr   r   r   ;       zconstruct.<locals>.<genexpr>ZevaluateFc                 3   r5   r   r6   r7   r9   r   r   r   =   r;   )r    r   r   r'   r   r   eval_false_legalr   mapr$   r)   basic_new_legalr   __new__r9   r   r9   r   r$   5   s   
r$   c                 C   s   t t| S )z[ Rebuild a SymPy expression.

    This removes harm caused by Expr-Rules interactions.
    )r$   r/   )r4   r   r   r   rebuildB   s   r@   Nc                 +   sr    fdd |p
i } fdd|  D }tj |  ||fttd|}|D ]}dd |  D V  q*dS )aZ   Structural unification of two expressions/patterns.

    Examples
    ========

    >>> from sympy.unify.usympy import unify
    >>> from sympy import Basic
    >>> from sympy.abc import x, y, z, p, q

    >>> next(unify(Basic(1, 2), Basic(1, x), variables=[x]))
    {x: 2}

    >>> expr = 2*x + y + z
    >>> pattern = 2*p + q
    >>> next(unify(expr, pattern, {}, variables=(p, q)))
    {p: x, q: y + z}

    Unification supports commutative and associative matching

    >>> expr = x + y + z
    >>> pattern = p + q
    >>> len(list(unify(expr, pattern, {}, variables=(p, q))))
    12

    Symbols not indicated to be variables are treated as literal,
    else they are wild-like and match anything in a sub-expression.

    >>> expr = x*y*z + 3
    >>> pattern = x*y + 3
    >>> next(unify(expr, pattern, {}, variables=[x, y]))
    {x: y, y: x*z}

    The x and y of the pattern above were in a Mul and matched factors
    in the Mul of expr. Here, a single symbol matches an entire term:

    >>> expr = x*y + 3
    >>> pattern = p + 3
    >>> next(unify(expr, pattern, {}, variables=[p]))
    {p: x*y}

    c                    s
   t |  S r   r.   r!   r0   r   r   <lambda>s   s   
 zunify.<locals>.<lambda>c                    s   i | ]\}} | |qS r   r   r   kv)deconsr   r   
<dictcomp>u       zunify.<locals>.<dictcomp>)r#   r%   c                 S   s   i | ]\}}t |t |qS r   )r$   rB   r   r   r   rF   |   rG   N)itemsr   unifyr#   r%   )r"   yr4   r1   kwargsZdsdr   )rE   r1   r   rI   I   s   *rI   )r   )Nr   )#__doc__Z
sympy.corer   r   r   r   Zsympy.core.operationsr   r   Zsympy.matricesr   r	   r
   Zsympy.sets.setsr   r   r   Zsympy.unify.corer   r   r   Zsympy.unifyr   r>   r<   Zillegalr   r   r#   r%   r-   r/   r$   r@   rI   r   r   r   r   <module>   s&    

