
    Ed|                         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"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                      t           t          t          t          t          t
          f}t           fd|D                       S )Nc              3   8   K   | ]}t          |          V  d S N
issubclass).0aopops     2lib/python3.11/site-packages/sympy/unify/usympy.py	<genexpr>z$sympy_associative.<locals>.<genexpr>   s-      88sz"c""888888    )r   r	   r
   r   r   r   any)r   	assoc_opss   ` r   sympy_associativer       s8    &&%yII8888i888888r   c                 z     t           t          t          t          t          f}t           fd|D                       S )Nc              3   8   K   | ]}t          |          V  d S r   r   )r   copr   s     r   r   z$sympy_commutative.<locals>.<genexpr>   s-      77sz"c""777777r   )r   r	   r   r   r   r   )r   comm_opss   ` r   sympy_commutativer%      s6    VUL)<H7777h777777r   c                 T    t          | t                    ot          | j                  S r   )
isinstancer   r    r   xs    r   is_associativer*      s"    a"">'8'>'>>r   c                     t          | t                    sdS t          | j                  rdS t	          | j        t
                    rt          d | j        D                       S d S )NFTc              3   >   K   | ]}t          |          j        V  d S r   )	constructis_commutative)r   args     r   r   z!is_commutative.<locals>.<genexpr>"   s+      CCS9S>>0CCCCCCr   )r'   r   r%   r   r   r   allargsr(   s    r   r.   r.      sp    a"" u t!$ DCCAFCCCCCCD Dr   c                       fd}|S )Nc                 x    t          |           p)t          | t                    ot          | j                  S r   )r'   r   r   r   )r)   typs    r   	matchtypezmk_matchtype.<locals>.matchtype%   s:    1c"" B1h''AJqtS,A,A	Cr    )r4   r5   s   ` r   mk_matchtyper7   $   s)    C C C C C r   r6   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   8   K   | ]}t          |          V  d S r   deconstruct)r   r/   	variabless     r   r   zdeconstruct.<locals>.<genexpr>3   s-      HH#+c955HHHHHHr   )	r   r'   r   r   is_Atomr   	__class__tupler1   )sr<   s    `r   r;   r;   *   s    I~ {{!h-.. a 19 AKHHHHHHHHHJ 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                       r/t          j         j        gt          t           j
                  R  S   j        t          t           j
                   S )z% Turn a Compound into a SymPy object c              3   B   K   | ]}t          j        |          V  d S r   r   r   r   clsts     r   r   zconstruct.<locals>.<genexpr>;   s/      
=
=S:adC  
=
=
=
=
=
=r   evaluateFc              3   B   K   | ]}t          j        |          V  d S r   rC   rD   s     r   r   zconstruct.<locals>.<genexpr>=   s/      >>sZc"">>>>>>r   )r'   r   r   r/   r   r   eval_false_legalr   mapr-   r1   basic_new_legalr   __new__)rF   s   `r   r-   r-   5   s    !h-.. ua"" 

=
=
=
=,<
=
=
=== -qtSAF++<e<<<	>>>>o>>>	>	> -}QT;C	16$:$:;;;;qtSAF++,,r   c                 :    t          t          |                     S )z[ Rebuild a SymPy expression.

    This removes harm caused by Expr-Rules interactions.
    )r-   r;   )r@   s    r   rebuildrN   B   s    
 [^^$$$r   Nc              +     K   fd|pi }fd|                                 D             }t          j         |            |          |ft          t          d|}|D ]"}d |                                 D             V  #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                 $    t          |           S r   r:   )r)   r<   s    r   <lambda>zunify.<locals>.<lambda>s   s    {1i00 r   c                 @    i | ]\  }} |           |          S r6   r6   )r   kvdeconss      r   
<dictcomp>zunify.<locals>.<dictcomp>u   s/    444$!QFF1II444r   )r*   r.   c                 N    i | ]"\  }}t          |          t          |          #S r6   )r-   )r   rS   rT   s      r   rV   zunify.<locals>.<dictcomp>|   s*    @@@day||Yq\\@@@r   N)itemsr   unifyr*   r.   )r)   yr@   r<   kwargsdsdrU   s      `   @r   rY   rY   I   s      T 1000F	RA4444!''))444A	FF1IIvvayy! 
/4B4B
/ 
/ (.
/ 
/B  A A@@aggii@@@@@@@A Ar   )r6   )Nr6   )#__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   rK   rI   illegalr    r%   r*   r.   r7   r;   r-   rN   rY   r6   r   r   <module>rf      s|  3 3
 , + + + + + + + + + + + 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 : : : : : : : : : : = = = = = = = = = =      ,S), +9 9 98 8 8? ? ?D D D  	J 	J 	J 	J- - -% % %3A 3A 3A 3A 3A 3Ar   