o
    ݹi.                     @   s  d Z ddlZddlmZmZmZ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/m0Z0 dd
l1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG edZHddgiZIeHrddlHmJZJmKZKmLZLmMZM ddlNmOZOmPZPmQZQ eJReC eJRe: eMRe: eJRe2 eMRe2 eKRe2 eLRe2 eJRe8 eMRe8 eKRe8 eLRe8 eJRe< eKRe< eJRe= eKRe= eJReA eJRe eJRe eJRe eJRe- eJRe, eJRe eJRe0 eJRe. eJRe/ eJRe eJRe eJRe eJRe eJRe eJRe eJRe eJRe eJRe  eJRe! eJRe# eJRe" eJRe' eJRe) eJRe% eJRe( eJRe& eJRe* eJRe eJRe eJRe eJRe eJRe eJRe eOReCdd ZSeORe4dd ZSeQReCdd ZSeQRe4dd ZSePRe4d4dd ZTeHrdd!lHmUZU nG d"d# d#ZUeGd$d%G d&d' d'eUe?ZVeGd$d%G d(d) d)eVZWeGd$d%G d*d+ d+eVZXeGd$d%G d,d- d-eVZYd.d/ ZZG d0d1 d1eZ[eGd$d%G d2d3 d3Z\dS )5zj
The objects in this module allow the usage of the MatchPy pattern matching
library on SymPy expressions.
    N)ListCallable
NamedTupleAnyDict)_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 rE   ^/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/sympy/utilities/matchpy_connector.py_[   s   rG   c                 C   
   t | jS Nr@   rC   rE   rE   rF   rG   _      
c                 C   s   dt | jd  S )Nr?   lenrB   rC   rE   rE   rF   rG   c   s   c                 C   rH   rI   rK   rC   rE   rE   rF   rG   g   rJ   Tc                 C   s   t | | S rI   )type)old_operationnew_operandsvariable_namerE   rE   rF   sympy_op_factoryk   s   rQ   )Wildcardc                   @   s   e Zd Zdd ZdS )rR   c                 C   s   || _ || _|| _|| _d S rI   )	min_count
fixed_sizerP   optional)self
min_lengthrT   rP   rU   rE   rE   rF   __init__t   s   
zWildcard.__init__N)__name__
__module____qualname__rX   rE   rE   rE   rF   rR   s   s    rR   )r6   )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 )_WildAbstractrW   rT   Nc                 K   s6   | j }| j}|d urt|}t| ||t|| d S rI   )rW   rT   r   rR   rX   str)rV   rP   rU   assumptionsrW   rT   rE   rE   rF   rX      s
   z_WildAbstract.__init__c                 C   s   | j | j| j| j| jdS )NrW   rT   rS   rP   rU   r`   rV   rE   rE   rF   __getstate__   s   z_WildAbstract.__getstate__c                 K   s"   |  ||  tj| ||fi |S rI   )	_sanitizer]   __xnew__)clsrP   rU   r_   rE   rE   rF   __new__   s   z_WildAbstract.__new__c                 C   s   | j | jfS rI   rP   rU   ra   rE   rE   rF   __getnewargs__      z_WildAbstract.__getnewargs__c                 K   s   t j| |fi |}|S rI   )r1   rd   )re   rP   rU   r_   objrE   rE   rF   rd      s   z_WildAbstract.__xnew__c                    s>   | j rt  | j| j| j| j f S t  | j| j| jf S rI   )rU   super_hashable_contentrS   rT   rP   ra   	__class__rE   rF   rl      s   z_WildAbstract._hashable_contentreturnc                 C   s   t | | j| jdS )Nrg   )rM   rP   rU   ra   rE   rE   rF   __copy__   s   z_WildAbstract.__copy__c                 C   s   t | S rI   r^   ra   rE   rE   rF   __repr__   s   z_WildAbstract.__repr__c                 C   s   | j S rI   namera   rE   rE   rF   __str__   s   z_WildAbstract.__str__)NN)ro   r]   )rY   rZ   r[   int__annotations__boolrX   rb   rf   rh   staticmethodrd   rl   rp   rr   ru   __classcell__rE   rE   rm   rF   r]   {   s   
 

	
r]   c                   @      e Zd ZdZdZdS )WildDotr?   TNrY   rZ   r[   rW   rT   rE   rE   rE   rF   r|          r|   c                   @   r{   )WildPlusr?   FNr}   rE   rE   rE   rF   r      r~   r   c                   @   r{   )WildStarr   FNr}   rE   rE   rE   rF   r      r~   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+)'\))r4   resub)exprsrE   rE   rF   
_get_srepr   s
   r   c                   @   s   e Zd ZU eed< eed< dS )ReplacementInforeplacementinfoN)rY   rZ   r[   r   rw   rE   rE   rE   rF   r      s   
 r   c                   @   s   e Zd ZdZg ddfdede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 dfde	dee	 dee	 ded	df
ddZd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._matcher.clear()
    >>> replacer.replace(eq)
    4/3
    Fcommon_constraintslambdifyr   c                 C   s&   t  | _|| _|| _|| _i | _d S rI   )r6   ManyToOneMatcher_matcher_common_constraint	_lambdify_info
_wildcards)rV   r   r   r   rE   rE   rF   rX     s
   

zReplacer.__init__
lambda_strro   .c                 C   s   t d t|t S )Nzfrom sympy import *)execevallocals)rV   r   rE   rE   rF   _get_lambda  s   zReplacer._get_lambdaconstraint_exprcondition_templatec              	   C   sN   dd | tD }d|}t|}||}t| d| d| dS )Nc                 S   s   g | ]}|j qS rE   rs   .0xrE   rE   rF   
<listcomp>  s    z3Replacer._get_custom_constraint.<locals>.<listcomp>, lambda z: ())atomsr]   joinr   formatr6   CustomConstraintr   )rV   r   r   wilds
lambdaargsfullexpr	conditionrE   rE   rF   _get_custom_constraint  s   

zReplacer._get_custom_constraintc                 C      |  |dS )Nz({}) != Falser   rV   r   rE   rE   rF   _get_custom_constraint_nonfalse  ri   z(Replacer._get_custom_constraint_nonfalsec                 C   r   )Nz({}) == Truer   r   rE   rE   rF   _get_custom_constraint_true  ri   z$Replacer._get_custom_constraint_trueNr   conditions_trueconditions_nonfalsec                    s   t |}t |} jd d  } fdd|D } fdd|D }|| || tj|g|R  }	 jrTdddd |tD  dt	| }
 
|
}|}n jd	d
 |tD   jrjt||} j|	| d S )Nc                       g | ]}  |qS rE   )r   r   condra   rE   rF   r   #      
z Replacer.add.<locals>.<listcomp>c                    r   rE   )r   r   ra   rE   rF   r   %  r   r   r   c                 s   s    | ]}|j V  qd S rI   rs   r   rE   rE   rF   	<genexpr>+  s    zReplacer.add.<locals>.<genexpr>z: c                 S   s   i | ]}t ||qS rE   rq   )r   irE   rE   rF   
<dictcomp>/  s    z Replacer.add.<locals>.<dictcomp>)r   r   extendr6   Patternr   r   r   r]   r   r   r   updaterR   r   r   r   add)rV   r   r   r   r   r   constraintsconstraint_conditions_trueconstraint_conditions_nonfalsepatternr   lambda_exprrE   ra   rF   r     s(   



*

zReplacer.add	max_countc              	      s   g }d}d}|rt|dk s||k rtd}t |D ]N\}}zAtt j|\}}	 jr5|j}
||j	 n|}
 j
rB|
di |	}n|
 fdd|	 D }t j|||}d}W  n	 tye   Y qw |d7 }|rt|dk s||k s jr{||fS |S )NTr   Fc                    s   i | ]
\}} j | |qS rE   )r   )r   kvra   rE   rF   r   I  s    z$Replacer.replace.<locals>.<dictcomp>r?   rE   )r6   preorder_iter_with_positionnextrA   r   matchr   r   appendr   r   xreplaceitems	functionsreplaceStopIteration)rV   
expressionr   infosreplacedreplace_countsubexprposreplacement_datasubstr   resultrE   ra   rF   r   4  s4   zReplacer.replace)r   )rY   rZ   r[   __doc__listrx   rX   r^   r   r,   r   r   r   r   r   r   r   rv   r   rE   rE   rE   rF   r      s"    8
r   )T)]r   r   typingr   r   r   r   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/   r0   sympy.core.symbolr1   &sympy.functions.elementary.exponentialr2   sympy.integrals.integralsr3   sympy.printing.reprr4   sympy.utilities.decoratorr5   r6   __doctest_requires__r8   r9   r:   r;   matchpy.expressions.functionsr<   r=   r>   registerrG   rQ   rR   r]   r|   r   r   r   r   r   rE   rE   rE   rF   <module>   s    08 





















































0