
    MZd|                         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 Zd	 Zd
 Zd Zd ZddZd Z d Z!ddZ"y)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                 l     t         t        t        t        t        t
        f}t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wN
issubclass).0aopops     4/usr/lib/python3/dist-packages/sympy/unify/usympy.py	<genexpr>z$sympy_associative.<locals>.<genexpr>   s     8sz"c"8   )r   r	   r
   r   r   r   any)r   	assoc_opss   ` r   sympy_associativer       s&    &&%yII8i888    c                 b     t         t        t        t        t        f}t         fd|D              S )Nc              3   6   K   | ]  }t        |        y wr   r   )r   copr   s     r   r   z$sympy_commutative.<locals>.<genexpr>   s     7sz"c"7r   )r   r	   r   r   r   r   )r   comm_opss   ` r   sympy_commutativer&      s$    VUL)<H7h777r!   c                 P    t        | t              xr t        | j                        S r   )
isinstancer   r    r   xs    r   is_associativer+      s    a">'8'>>r!   c                     t        | t              syt        | j                        ryt	        | j                  t
              rt        d | j                  D              S y )NFTc              3   F   K   | ]  }t        |      j                    y wr   )	constructis_commutative)r   args     r   r   z!is_commutative.<locals>.<genexpr>"   s     CS9S>00Cs   !)r(   r   r&   r   r   r   allargsr)   s    r   r/   r/      sF    a"!$$CAFFCCC r!   c                       fd}|S )Nc                 p    t        |       xs( t        | t              xr t        | j                        S r   )r(   r   r   r   )r*   typs    r   	matchtypezmk_matchtype.<locals>.matchtype%   s3    1c" B1h'AJqttS,A	Cr!    )r5   r6   s   ` r   mk_matchtyper8   $   s    C r!   c                     | v rt        |       S t        | t         t        f      r| S t        | t              r| j                  r| S t        | j                  t        fd| j                  D                    S )z% Turn a SymPy object into a Compound c              3   6   K   | ]  }t        |        y wr   deconstruct)r   r0   	variabless     r   r   zdeconstruct.<locals>.<genexpr>3   s     H#+c95Hr   )	r   r(   r   r   is_Atomr   	__class__tupler2   )sr=   s    `r   r<   r<   *   sd    I~{!h-.a199AKKHHHJ Jr!   c                     t         t        t        f      r j                  S t         t              s S t         fdt        D              r*  j                  t        t         j                        ddiS t         fdt        D              r8t        j                   j                  gt        t         j                         S   j                  t        t         j                         S )z% Turn a Compound into a SymPy object c              3   J   K   | ]  }t        j                  |        y wr   r   r   r   clsts     r   r   zconstruct.<locals>.<genexpr>;   s     
=S:addC 
=    #evaluateFc              3   J   K   | ]  }t        j                  |        y wr   rD   rE   s     r   r   zconstruct.<locals>.<genexpr>=   s     >sZc">rH   )r(   r   r   r0   r   r   eval_false_legalr   mapr.   r2   basic_new_legalr   __new__)rG   s   `r   r.   r.   5   s    !h-.uua"

=,<
==qttSAFF+<e<<	>o>	>}}QTT;C	166$:;;qttSAFF+,,r!   c                 *    t        t        |             S )z[ Rebuild a SymPy expression.

    This removes harm caused by Expr-Rules interactions.
    )r.   r<   )rA   s    r   rebuildrP   B   s    
 [^$$r!   Nc           	   +   v  K   fd}|xs i }|j                         D ci c]  \  }} ||       ||       }}}t        j                   ||        ||      |ft        t        d|}|D ]:  }	|	j                         D ci c]  \  }}t        |      t        |       c}} < yc c}}w c c}}w w)af   Structural unification of two expressions/patterns.

    Examples
    ========

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

    >>> next(unify(Basic(S(1), S(2)), Basic(S(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                     t        |       S r   r;   )r*   r=   s    r   <lambda>zunify.<locals>.<lambda>s   s    {1i0 r!   )r+   r/   N)itemsr   unifyr+   r/   r.   )
r*   yrA   r=   kwargsdeconskvdsds
      `      r   rU   rU   I   s     T 1F	RA*+'')4$!QF1I	4A4	F1Ivay! 
/4B4B
/ (.
/B  A67ggi@day|Yq\)@@A 	5 As   B9B-A
B9B3$B9)r7   )Nr7   )#__doc__
sympy.corer   r   r   r   sympy.core.operationsr   r   sympy.matricesr	   r
   r   sympy.sets.setsr   r   r   sympy.unify.corer   r   r   sympy.unifyr   rM   rK   illegalr    r&   r+   r/   r8   r<   r.   rP   rU   r7   r!   r   <module>re      su   3
 , + 4 5 5 : : = = ,S), +98?D	J-%3Ar!   