
    MZdT'                     
   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' dd	l(m)Z)m*Z*m+Z+m,Z,m-Z- dd
l.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD  ed      ZEeErddlEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZM  eFj                  e@        eFj                  e7        eIj                  e7        eFj                  e/        eIj                  e/        eGj                  e/        eHj                  e/        eFj                  e5        eIj                  e5        eGj                  e5        eHj                  e5        eFj                  e9        eGj                  e9        eFj                  e:        eGj                  e:        eFj                  e>        eFj                  e
        eFj                  e        eFj                  e        eFj                  e*        eFj                  e)        eFj                  e        eFj                  e-        eFj                  e+        eFj                  e,        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e         eFj                  e        eFj                  e$        eFj                  e&        eFj                  e"        eFj                  e%        eFj                  e#        eFj                  e'        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eFj                  e        eKj                  e@      d        ZO eKj                  e1      d        ZO eMj                  e@      d        ZO eMj                  e1      d        ZO eLj                  e1      d-d       ZPeErddlEmQZQ n
 G d d      ZQ eDd !       G d" d#eQe<             ZR eDd !       G d$ d%eR             ZS eDd !       G d& d'eR             ZT eDd !       G d( d)eR             ZUd* ZV eDd !       G d+ d,             ZWy).zj
The objects in this module allow the usage of the MatchPy pattern matching
library on SymPy expressions.
    N)ListCallable)_sympify)import_module)
logsincostancotcscsecerfgamma
uppergamma)acoshasinhatanhacothacschasechcoshsinhtanhcothsechcsch)atanacscasinacotacosasec)fresnelcfresnelserfcerfiEi)Add)Basic)Expr)Mul)Pow)Equality
Unequality)Symbol)exp)Integral)srepr)doctest_depends_onmatchpy)	OperationCommutativeOperationAssociativeOperationOneIdentityOperation)op_itercreate_operation_expressionop_lenc                 T    t        | j                  d   f| j                  d   z         S )Nr      iter_args	operations    C/usr/lib/python3/dist-packages/sympy/utilities/matchpy_connector.py_rD   V   s'    Y__Q')IOOA,>>??    c                 ,    t        | j                        S Nr>   rA   s    rC   rD   rD   Z   s    IOO$$rE   c                 8    dt        | j                  d         z   S )Nr=   lenr@   rA   s    rC   rD   rD   ^   s    3yq)***rE   c                 ,    t        | j                        S rG   rI   rA   s    rC   rD   rD   b   s    9??##rE   c                      t        |       | S rG   )type)old_operationnew_operandsvariable_names      rC   sympy_op_factoryrQ   f   s    #]#\2	2rE   )Wildcardc                       e Zd Zd Zy)rR   c                 <    || _         || _        || _        || _        y rG   )	min_count
fixed_sizerP   optional)self
min_lengthrV   rP   rW   s        rC   __init__zWildcard.__init__o   s    'DN(DO!.D$DMrE   N)__name__
__module____qualname__rZ    rE   rC   rR   rR   n   s    	%rE   rR   )r4   )modulesc                   v     e Zd ZU eed<   eed<   ddZd ZddZd Z	e
dd       Z fdZdd	Zd
 Zd Z xZS )_WildAbstractrY   rV   c                     | j                   }| j                  }|t        |      }t        j                  | ||t        |      |       y rG   )rY   rV   r   rR   rZ   str)rX   rP   rW   assumptionsrY   rV   s         rC   rZ   z_WildAbstract.__init__{   s?    __
__
)H$
JM8JHUrE   c                 v    | j                   | j                  | j                  | j                  | j                  dS )NrY   rV   rU   rP   rW   rf   rX   s    rC   __getstate__z_WildAbstract.__getstate__   s1    ////!//
 	
rE   c                 V    | j                  ||        t        j                  | ||fi |S rG   )	_sanitizera   __xnew__)clsrP   rW   rd   s       rC   __new__z_WildAbstract.__new__   s*    k3'%%c=(RkRRrE   c                 2    | j                   | j                  fS rG   rP   rW   rg   s    rC   __getnewargs__z_WildAbstract.__getnewargs__   s    !!4==00rE   c                 4    t        j                  | |fi |}|S rG   )r/   rk   )rl   rP   rW   rd   objs        rC   rk   z_WildAbstract.__xnew__   s    ooc=@K@
rE   c                     | j                   r=t        | 	         | j                  | j                  | j
                  | j                   fz   S t        | 	         | j                  | j                  | j
                  fz   S rG   )rW   super_hashable_contentrU   rV   rP   )rX   	__class__s    rC   ru   z_WildAbstract._hashable_content   se    ==7,.$..$//SWSeSegkgtgt1uuu7,.$..$//SWSeSe1fffrE   c                 P     t        |       | j                  | j                        S )Nro   )rM   rP   rW   rg   s    rC   __copy__z_WildAbstract.__copy__   s    tDz(:(:T]]SSrE   c                     t        |       S rG   )rc   rg   s    rC   __repr__z_WildAbstract.__repr__   s    4yrE   c                     | j                   S rG   namerg   s    rC   __str__z_WildAbstract.__str__   s    yyrE   )NN)returnra   )r[   r\   r]   int__annotations__boolrZ   rh   rm   rp   staticmethodrk   ru   rx   rz   r~   __classcell__)rv   s   @rC   ra   ra   v   sN    OV
S1  gTrE   ra   c                       e Zd ZdZdZy)WildDotr=   TNr[   r\   r]   rY   rV   r^   rE   rC   r   r      s    JJrE   r   c                       e Zd ZdZdZy)WildPlusr=   FNr   r^   rE   rC   r   r          JJrE   r   c                       e Zd ZdZdZy)WildStarr   FNr   r^   rE   rC   r   r      r   rE   r   c                     t        |       }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }|S )NzWildDot\('(\w+)'\)z\1zWildPlus\('(\w+)'\)z*\1zWildStar\('(\w+)'\))r2   resub)exprss     rC   
_get_sreprr      sI    dA
$eQ/A
%vq1A
%vq1AHrE   c                       e Zd ZdZg fdefdZdededef   fdZ	ded	ededef   fd
Z
dededef   fdZdededef   fdZg g fdededee   dee   ddf
dZdedefdZy)Replacera  
    Replacer object to perform multiple pattern matching and subexpression
    replacements in SymPy expressions.

    Examples
    ========

    Example to construct a simple first degree equation solver:

    >>> from sympy.utilities.matchpy_connector import WildDot, Replacer
    >>> from sympy import Equality, Symbol
    >>> x = Symbol("x")
    >>> a_ = WildDot("a_", optional=1)
    >>> b_ = WildDot("b_", optional=0)

    The lines above have defined two wildcards, ``a_`` and ``b_``, the
    coefficients of the equation `a x + b = 0`. The optional values specified
    indicate which expression to return in case no match is found, they are
    necessary in equations like `a x = 0` and `x + b = 0`.

    Create two constraints to make sure that ``a_`` and ``b_`` will not match
    any expression containing ``x``:

    >>> from matchpy import CustomConstraint
    >>> free_x_a = CustomConstraint(lambda a_: not a_.has(x))
    >>> free_x_b = CustomConstraint(lambda b_: not b_.has(x))

    Now create the rule replacer with the constraints:

    >>> replacer = Replacer(common_constraints=[free_x_a, free_x_b])

    Add the matching rule:

    >>> replacer.add(Equality(a_*x + b_, 0), -b_/a_)

    Let's try it:

    >>> replacer.replace(Equality(3*x + 4, 0))
    -4/3

    Notice that it will not match equations expressed with other patterns:

    >>> eq = Equality(3*x, 4)
    >>> replacer.replace(eq)
    Eq(3*x, 4)

    In order to extend the matching patterns, define another one (we also need
    to clear the cache, because the previous result has already been memorized
    and the pattern matcher will not iterate again if given the same expression)

    >>> replacer.add(Equality(a_*x, b_), b_/a_)
    >>> replacer._replacer.matcher.clear()
    >>> replacer.replace(eq)
    4/3
    common_constraintsc                 D    t         j                         | _        || _        y rG   )r4   ManyToOneReplacer	_replacer_common_constraint)rX   r   s     rC   rZ   zReplacer.__init__   s     224"4rE   
lambda_strr   .c                 @    t        d       t        |t                     S )Nzfrom sympy import *)execevallocals)rX   r   s     rC   _get_lambdazReplacer._get_lambda   s    "#J))rE   constraint_exprcondition_templatec           	         |j                  t              D cg c]  }|j                   }}dj                  |      }t	        |      }|j                  |      }t        j                  | j                  d| d| d            S c c}w )N, lambda z: ())	atomsra   r}   joinr   formatr4   CustomConstraintr   )rX   r   r   xwilds
lambdaargsfullexpr	conditions           rC   _get_custom_constraintzReplacer._get_custom_constraint  s    !0!6!6}!EFAFFYYu%
o.&--h7	''wzl#i[BCE 	E	 Gs   Bc                 &    | j                  |d      S )Nz({}) != Falser   rX   r   s     rC   _get_custom_constraint_nonfalsez(Replacer._get_custom_constraint_nonfalse  s    **?OLLrE   c                 &    | j                  |d      S )Nz({}) == Truer   r   s     rC   _get_custom_constraint_truez$Replacer._get_custom_constraint_true  s    **?NKKrE   r   resultconditions_trueconditions_nonfalseNc                 :   t        |      }t        |      }ddj                  d |j                  t              D               dt	        |       }| j                  |      }| j                  d d  }|D cg c]  }| j                  |       }	}|D cg c]  }| j                  |       }
}|j                  |	       |j                  |
       | j                  j                  t        j                  t        j                  |g| |             y c c}w c c}w )Nr   r   c              3   4   K   | ]  }|j                     y wrG   r|   ).0r   s     rC   	<genexpr>zReplacer.add.<locals>.<genexpr>  s     )TQ!&&)Ts   z: )r   r   r   ra   r   r   r   r   r   extendr   addr4   ReplacementRulePattern)rX   r   r   r   r   r   lambda_exprconstraintscondconstraint_conditions_trueconstraint_conditions_nonfalses              rC   r   zReplacer.add  s   ~&!tyy)T$**]:S)TUVVXYcdjYkXlm
&&z2--a0?N&P7;D,,T2&P" &P DW*X;?D006*X& *X569:##GOOD$G;$GU	W&P*Xs   5DDc                 8    | j                   j                  |      S rG   )r   replace)rX   r   s     rC   r   zReplacer.replace   s    ~~%%d++rE   )r[   r\   r]   __doc__listrZ   rc   r   r*   r   r   r   r   r   r   r   r^   rE   rC   r   r      s    6p 35 54 5*c *hsDy.A *Ed EPS EX`adfjajXk EMt MQTVZQZH[ ML4 LHSRVYDW L KMpr W Wd WT$Z Wcghlcm Ww{ W,D ,T ,rE   r   )T)Xr   r   typingr   r   sympy.core.sympifyr   sympy.externalr   sympy.functionsr   r   r	   r
   r   r   r   r   r   r   %sympy.functions.elementary.hyperbolicr   r   r   r   r   r   r   r   r   r   r   r   (sympy.functions.elementary.trigonometricr   r   r   r    r!   r"   'sympy.functions.special.error_functionsr#   r$   r%   r&   r'   sympy.core.addr(   sympy.core.basicr)   sympy.core.exprr*   sympy.core.mulr+   sympy.core.powerr,   sympy.core.relationalr-   r.   sympy.core.symbolr/   &sympy.functions.elementary.exponentialr0   sympy.integrals.integralsr1   sympy.printing.reprr2   sympy.utilities.decoratorr3   r4   r5   r6   r7   r8   matchpy.expressions.functionsr9   r:   r;   registerrD   rQ   rR   ra   r   r   r   r   r   r^   rE   rC   <module>r      s   
 ! ' ( W W W ~ ~ ~ ~ W W V V  "      8 $ 6 . % 8
	
"
ccZZIx Is!!!#&Is!!!#&!!!#&!!!#&Is!!!#&!!!#&!!!#&Ix !!!(+Iz"!!!*-IsIsIuIz"Ix Ix IsIrItItIsIsIsIsIsIsItItItItItItItItItItItItIuIuIuIuIuIuWh@  @ We% % V__X+ + V__U$ $ * ))%03 13  % % L)-Hf - *-` L)m  *
 L)}  *
 L)}  *
 L)_, _, *_,rE   