U
    «Ì0e|  ã                   @   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   s   | ]}t ˆ |ƒV  qd S ©N©Ú
issubclass)Ú.0Zaop©Úop© ú6/tmp/pip-unpacked-wheel-_6tpq7m6/sympy/unify/usympy.pyÚ	<genexpr>   s     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   s   | ]}t ˆ |ƒV  qd S r   r   )r   Zcopr   r   r   r      s     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   "   s     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   | ˆ krt | ƒS t| t tfƒr"| S t| tƒr2| jr6| 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   s     zdeconstruct.<locals>.<genexpr>)	r   r   r   r   Zis_Atomr   Ú	__class__Útupler'   )Úsr/   r   r.   r   r-   *   s    ÿr-   c                    s–   t ˆ ttfƒrˆ jS t ˆ tƒs"ˆ S t‡ fdd„tD ƒƒrPˆ jtt	ˆ j
ƒddiŽS t‡ fdd„tD ƒƒr€tjˆ jftt	ˆ j
ƒžŽ S ˆ jtt	ˆ j
ƒŽ S dS )z% Turn a Compound into a SymPy object c                 3   s   | ]}t ˆ j|ƒV  qd S r   ©r   r   ©r   Úcls©Útr   r   r   ;   s     zconstruct.<locals>.<genexpr>ÚevaluateFc                 3   s   | ]}t ˆ j|ƒV  qd S r   r3   r4   r6   r   r   r   =   s     N)r   r   r   r%   r   r   Úeval_false_legalr   Úmapr"   r'   Úbasic_new_legalr   Ú__new__r6   r   r6   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-   )r2   r   r   r   ÚrebuildB   s    r=   Nc                 +   sp   ‡fdd„‰ |pi }‡ fdd„|  ¡ D ƒ}tjˆ | ƒˆ |ƒ|fttdœ|—Ž}|D ]}dd„ |  ¡ D ƒV  qRdS )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                    s
   t | ˆ ƒS r   r,   r   r.   r   r   Ú<lambda>s   ó    zunify.<locals>.<lambda>c                    s   i | ]\}}ˆ |ƒˆ |ƒ“qS r   r   ©r   ÚkÚv)Údeconsr   r   Ú
<dictcomp>u   s      zunify.<locals>.<dictcomp>)r!   r#   c                 S   s   i | ]\}}t |ƒt |ƒ“qS r   )r"   r@   r   r   r   rD   |   s      N)Úitemsr   Úunifyr!   r#   )r    Úyr2   r/   ÚkwargsZdsÚdr   )rC   r/   r   rF   I   s    *þýrF   )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;   r9   Úillegalr   r   r!   r#   r+   r-   r"   r=   rF   r   r   r   r   Ú<module>   s$   

