o
    *ήcd'                     @   s  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rddlEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZM eFNe@ eFNe7 eINe7 eFNe/ eINe/ eGNe/ eHNe/ eFNe5 eINe5 eGNe5 eHNe5 eFNe9 eGNe9 eFNe: eGNe: eFNe> eFNe
 eFNe eFNe eFNe* eFNe) eFNe eFNe- eFNe+ eFNe, eFNe eFNe eFNe eFNe eFNe eFNe eFNe eFNe eFNe eFNe eFNe  eFNe eFNe$ eFNe& eFNe" eFNe% eFNe# eFNe' eFNe eFNe eFNe eFNe eFNe eFNe eKNe@dd ZOeKNe1dd ZOeMNe@dd ZOeMNe1dd ZOeLNe1d1ddZPeErdd lEmQZQ nG d!d" d"ZQeDd#d$G d%d& d&eQe<ZReDd#d$G d'd( d(eRZSeDd#d$G d)d* d*eRZTeDd#d$G d+d, d,eRZUd-d. ZVeDd#d$G d/d0 d0ZWdS )2zj
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                 C   s   t | jd f| jd  S )Nr      iter_args	operation rA   H/tmp/pip-target-vg8gfxp4/lib/python/sympy/utilities/matchpy_connector.py_V   s   rC   c                 C   
   t | jS Nr<   r?   rA   rA   rB   rC   Z      
c                 C   s   dt | jd  S )Nr;   lenr>   r?   rA   rA   rB   rC   ^   s   c                 C   rD   rE   rG   r?   rA   rA   rB   rC   b   rF   Tc                 C   s   t | | S rE   )type)old_operationnew_operandsvariable_namerA   rA   rB   sympy_op_factoryf   s   rM   )Wildcardc                   @   s   e Zd Zdd ZdS )rN   c                 C   s   || _ || _|| _|| _d S rE   )	min_count
fixed_sizerL   optional)self
min_lengthrP   rL   rQ   rA   rA   rB   __init__o   s   
zWildcard.__init__N)__name__
__module____qualname__rT   rA   rA   rA   rB   rN   n   s    rN   )r3   )modulesc                       sz   e Zd ZU eed< eed< dddZdd Zddd	Zd
d Z	e
dddZ fddZdddZdd Zdd Z  ZS )_WildAbstractrS   rP   Nc                 K   s6   | j }| j}|d urt|}t| ||t|| d S rE   )rS   rP   r   rN   rT   str)rR   rL   rQ   assumptionsrS   rP   rA   rA   rB   rT   {   s
   z_WildAbstract.__init__c                 C   s   | j | j| j| j| jdS )NrS   rP   rO   rL   rQ   r\   rR   rA   rA   rB   __getstate__   s   z_WildAbstract.__getstate__c                 K   s"   |  ||  tj| ||fi |S rE   )	_sanitizerY   __xnew__)clsrL   rQ   r[   rA   rA   rB   __new__   s   z_WildAbstract.__new__c                 C   s   | j | jfS rE   rL   rQ   r]   rA   rA   rB   __getnewargs__      z_WildAbstract.__getnewargs__c                 K   s   t j| |fi |}|S rE   )r.   r`   )ra   rL   rQ   r[   objrA   rA   rB   r`      s   z_WildAbstract.__xnew__c                    s>   | j rt  | j| j| j| j f S t  | j| j| jf S rE   )rQ   super_hashable_contentrO   rP   rL   r]   	__class__rA   rB   rh      s   z_WildAbstract._hashable_contentreturnc                 C   s   t | | j| jdS )Nrc   )rI   rL   rQ   r]   rA   rA   rB   __copy__   s   z_WildAbstract.__copy__c                 C   s   t | S rE   )rZ   r]   rA   rA   rB   __repr__   s   z_WildAbstract.__repr__c                 C      | j S rE   namer]   rA   rA   rB   __str__   s   z_WildAbstract.__str__)NN)rk   rY   )rU   rV   rW   int__annotations__boolrT   r^   rb   rd   staticmethodr`   rh   rl   rm   rq   __classcell__rA   rA   ri   rB   rY   v   s   
 

	
rY   c                   @      e Zd ZdZdZdS )WildDotr;   TNrU   rV   rW   rS   rP   rA   rA   rA   rB   rx          rx   c                   @   rw   )WildPlusr;   FNry   rA   rA   rA   rB   r{      rz   r{   c                   @   rw   )WildStarr   FNry   rA   rA   rA   rB   r|      rz   r|   c                 C   s6   t | }tdd|}tdd|}tdd|}|S )NzWildDot\('(\w+)'\)z\1zWildPlus\('(\w+)'\)z*\1zWildStar\('(\w+)'\))r1   resub)exprsrA   rA   rB   
_get_srepr   s
   r   c                   @   s   e Zd ZdZg fdefddZdededef fdd	Z	d
edededef fddZ
d
ededef fddZd
ededef fddZg g fdededee dee ddf
ddZdedefddZdS )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                 C   s   t  | _|| _d S rE   )r3   ManyToOneReplacer	_replacer_common_constraint)rR   r   rA   rA   rB   rT      s   

zReplacer.__init__
lambda_strrk   .c                 C   s   t d t|t S )Nzfrom sympy import *)execevallocals)rR   r   rA   rA   rB   _get_lambda   s   zReplacer._get_lambdaconstraint_exprcondition_templatec              	   C   sR   t tdd |t}d|}t|}||}t| 	d| d| dS )Nc                 S   rn   rE   ro   xrA   rA   rB   <lambda>      z1Replacer._get_custom_constraint.<locals>.<lambda>, lambda z: ())
listmapatomsrY   joinr   formatr3   CustomConstraintr   )rR   r   r   wilds
lambdaargsfullexpr	conditionrA   rA   rB   _get_custom_constraint  s   

zReplacer._get_custom_constraintc                 C      |  |dS )Nz({}) != Falser   rR   r   rA   rA   rB   _get_custom_constraint_nonfalse  re   z(Replacer._get_custom_constraint_nonfalsec                 C   r   )Nz({}) == Truer   r   rA   rA   rB   _get_custom_constraint_true  re   z$Replacer._get_custom_constraint_truer   resultconditions_trueconditions_nonfalseNc           
         s   t |}t |}ddtdd |t dt| } |} jd d  } fdd|D } fdd|D }	|| ||	  j	
ttj|g|R  | d S )	Nr   r   c                 S   rn   rE   ro   r   rA   rA   rB   r     r   zReplacer.add.<locals>.<lambda>z: c                       g | ]}  |qS rA   )r   .0condr]   rA   rB   
<listcomp>      
z Replacer.add.<locals>.<listcomp>c                    r   rA   )r   r   r]   rA   rB   r     r   )r   r   r   r   rY   r   r   r   extendr   addr3   ReplacementRulePattern)
rR   r   r   r   r   r   lambda_exprconstraintsconstraint_conditions_trueconstraint_conditions_nonfalserA   r]   rB   r     s    *




zReplacer.addc                 C   s   | j |S rE   )r   replace)rR   r   rA   rA   rB   r      re   zReplacer.replace)rU   rV   rW   __doc__r   rT   rZ   r   r)   r   r   r   r   r   r   r   rA   rA   rA   rB   r      s    8,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.exponentialr/   sympy.integrals.integralsr0   sympy.printing.reprr1   sympy.utilities.decoratorr2   r3   r4   r5   r6   r7   matchpy.expressions.functionsr8   r9   r:   registerrC   rM   rN   rY   rx   r{   r|   r   r   rA   rA   rA   rB   <module>   s    08 




















































0