o
    §ƒðj|  ã                   @   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)Ú.0Úaop©Úop© ú”/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/unify/usympy.pyÚ	<genexpr>   ó   € z$sympy_associative.<locals>.<genexpr>)r   r   r	   r   r   r   Úany)r   Ú	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   Úcopr   r   r   r      r   z$sympy_commutative.<locals>.<genexpr>)r   r   r   r   r   r   )r   Ú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/   r0   r   r.   r   Úmk_matchtype$   s   r1   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   Úis_Atomr   Ú	__class__Útupler-   )Úsr5   r   r4   r   r3   *   s   ÿr3   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>ÚevaluateFc                 3   r:   r   r;   r<   r>   r   r   r   =   r@   )r$   r   r   r+   r   r   Úeval_false_legalr   Úmapr(   r-   Úbasic_new_legalr   Ú__new__r>   r   r>   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(   r3   )r9   r   r   r   ÚrebuildB   s   rF   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 )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   r2   r%   r4   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(   rH   r   r   r   rL   |   rM   N)Úitemsr   Úunifyr'   r)   )r&   Úyr9   r5   ÚkwargsÚdsÚdr   )rK   r5   r   rO   I   s   €*þýÿrO   )r   )Nr   )#Ú__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   rD   rB   Úillegalr    r#   r'   r)   r1   r3   r(   rF   rO   r   r   r   r   Ú<module>   s&    

