
    \h.                     
   S r SSKrSSKJrJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJrJrJrJrJrJrJrJrJrJr  SSKJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#  SSK$J%r%J&r&J'r'J(r(J)r)J*r*  SS	K+J,r,J-r-J.r.J/r/J0r0  SS
K1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:  SSK;J<r<J=r=  SSK>J?r?  SSK@JArA  SSKBJCrC  SSKDJErE  SSKFJGrG  \" S5      rHSS/0rI\H(       Ga  SSKHJJrJJKrKJLrLJMrM  SSKNJOrOJPrPJQrQ  \JR                  " \C5        \JR                  " \:5        \MR                  " \:5        \JR                  " \25        \MR                  " \25        \KR                  " \25        \LR                  " \25        \JR                  " \85        \MR                  " \85        \KR                  " \85        \LR                  " \85        \JR                  " \<5        \KR                  " \<5        \JR                  " \=5        \KR                  " \=5        \JR                  " \A5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \-5        \JR                  " \,5        \JR                  " \5        \JR                  " \05        \JR                  " \.5        \JR                  " \/5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \ 5        \JR                  " \!5        \JR                  " \#5        \JR                  " \"5        \JR                  " \'5        \JR                  " \)5        \JR                  " \%5        \JR                  " \(5        \JR                  " \&5        \JR                  " \*5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \JR                  " \5        \OR                  " \C5      S 5       rS\OR                  " \45      S 5       rS\QR                  " \C5      S 5       rS\QR                  " \45      S 5       rS\PR                  " \45      S0S j5       rT\H(       a  SSKHJUrU  O
 " S S 5      rU\G" S!S"9 " S# S$\U\?5      5       rV\G" S!S"9 " S% S&\V5      5       rW\G" S!S"9 " S' S(\V5      5       rX\G" S!S"9 " S) S*\V5      5       rYS+ rZ " S, S-\5      r[\G" S!S"9 " S. S/5      5       r\g)1zj
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                 T    [        U R                  S   4U R                  S   -   5      $ )Nr      iter_args	operations    Y/var/www/auris/envauris/lib/python3.13/site-packages/sympy/utilities/matchpy_connector.py_rH   [   s'    Y__Q')IOOA,>>??    c                 ,    [        U R                  5      $ NrB   rE   s    rG   rH   rH   _   s    IOO$$rI   c                 8    S[        U R                  S   5      -   $ )NrA   lenrD   rE   s    rG   rH   rH   c   s    3yq)***rI   c                 ,    [        U R                  5      $ rK   rM   rE   s    rG   rH   rH   g   s    9??##rI   c                     [        U 5      " U6 $ rK   )type)old_operationnew_operandsvariable_names      rG   sympy_op_factoryrU   k   s    M"L11rI   )Wildcardc                       \ rS rSrS rSrg)rV   s   c                 4    Xl         X l        X0l        X@l        g rK   )	min_count
fixed_sizerT   optional)self
min_lengthr[   rT   r\   s        rG   __init__Wildcard.__init__t   s    'N(O!.$MrI   )r[   rZ   r\   rT   N)__name__
__module____qualname____firstlineno__r_   __static_attributes__ rI   rG   rV   rV   s   s    	%rI   rV   )r7   )modulesc                      ^  \ rS rSr% \\S'   \\S'   SS jrS rSS jr	S r
\SS j5       rU 4S	 jrSS
 jrS rS rSrU =r$ )_WildAbstract{   r^   r[   c                     U R                   nU R                  nUb  [        U5      n[        R                  " XU[        U5      U5        g rK   )r^   r[   r   rV   r_   str)r]   rT   r\   assumptionsr^   r[   s         rG   r_   _WildAbstract.__init__   s=    __
__
)H$JM8JHUrI   c                 v    U R                   U R                  U R                  U R                  U R                  S.$ )Nr^   r[   rZ   rT   r\   rp   r]   s    rG   __getstate___WildAbstract.__getstate__   s1    ////!//
 	
rI   c                 T    U R                  X05        [        R                  " XU40 UD6$ rK   )	_sanitizeri   __xnew__)clsrT   r\   rm   s       rG   __new___WildAbstract.__new__   s&    k'%%c(RkRRrI   c                 2    U R                   U R                  4$ rK   rT   r\   rq   s    rG   __getnewargs___WildAbstract.__getnewargs__   s    !!4==00rI   c                 4    [         R                  " X40 UD6nU$ rK   )r2   rv   )rw   rT   r\   rm   objs        rG   rv   _WildAbstract.__xnew__   s    ooc@K@
rI   c                   > U R                   (       a=  [        TU ]	  5       U R                  U R                  U R
                  U R                   4-   $ [        TU ]	  5       U R                  U R                  U R
                  4-   $ rK   )r\   super_hashable_contentrZ   r[   rT   )r]   	__class__s    rG   r   _WildAbstract._hashable_content   se    ==7,.$..$//SWSeSegkgtgt1uuu7,.$..$//SWSeSe1fffrI   c                 J    [        U 5      " U R                  U R                  S9$ )Nr{   )rQ   rT   r\   rq   s    rG   __copy___WildAbstract.__copy__   s    Dz(:(:T]]SSrI   c                     [        U 5      $ rK   )rl   rq   s    rG   __repr___WildAbstract.__repr__   s    4yrI   c                     U R                   $ rK   namerq   s    rG   __str___WildAbstract.__str__   s    yyrI   rf   )NN)returnri   )ra   rb   rc   rd   int__annotations__boolr_   rr   rx   r|   staticmethodrv   r   r   r   r   re   __classcell__)r   s   @rG   ri   ri   {   sS    OV
S1  gT rI   ri   c                       \ rS rSrSrSrSrg)WildDot   rA   Trf   Nra   rb   rc   rd   r^   r[   re   rf   rI   rG   r   r      s    JJrI   r   c                       \ rS rSrSrSrSrg)WildPlus   rA   Frf   Nr   rf   rI   rG   r   r          JJrI   r   c                       \ rS rSrSrSrSrg)WildStar   r   Frf   Nr   rf   rI   rG   r   r      r   rI   r   c                     [        U 5      n[        R                  " SSU5      n[        R                  " SSU5      n[        R                  " SSU5      nU$ )NzWildDot\('(\w+)'\)z\1zWildPlus\('(\w+)'\)z*\1zWildStar\('(\w+)'\))r5   resub)exprss     rG   
_get_sreprr      sI    dA
$eQ/A
%vq1A
%vq1AHrI   c                   *    \ rS rSr% \\S'   \\S'   Srg)ReplacementInfo   replacementinforf   N)ra   rb   rc   rd   r   r   re   rf   rI   rG   r   r      s    
IrI   r   c                       \ rS rSrSr/ SS4S\S\S\4S jjrS\S	\	S
\
4   4S jrS\
S\S	\	S
\
4   4S jrS\
S	\	S
\
4   4S jrS\
S	\	S
\
4   4S jr/ / S4S\
S\\
   S\\
   S\S	S4
S jjrSS\4S jjrSrg)Replacer   ak  
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                 h    [         R                  5       U l        Xl        X l        X0l        0 U l        g rK   )r7   ManyToOneMatcher_matcher_common_constraint	_lambdify_info
_wildcards)r]   r   r   r   s       rG   r_   Replacer.__init__  s)    002"4!
/1rI   
lambda_strr   .c                 @    [        S5        [        U[        5       5      $ )Nzfrom sympy import *)execevallocals)r]   r   s     rG   _get_lambdaReplacer._get_lambda  s    "#J))rI   constraint_exprcondition_templatec           	         UR                  [        5       Vs/ s H  o3R                  PM     nnSR                  U5      n[	        U5      nUR                  U5      n[        R                  U R                  SU SU S35      5      $ s  snf )N, lambda z: ())	atomsri   r   joinr   formatr7   CustomConstraintr   )r]   r   r   xwilds
lambdaargsfullexpr	conditions           rG   _get_custom_constraintReplacer._get_custom_constraint  s    !0!6!6}!EF!EA!EFYYu%
o.&--h7	''wzl#i[BCE 	E	 Gs   Bc                 &    U R                  US5      $ )Nz({}) != Falser   r]   r   s     rG   _get_custom_constraint_nonfalse(Replacer._get_custom_constraint_nonfalse  s    **?OLLrI   c                 &    U R                  US5      $ )Nz({}) == Truer   r   s     rG   _get_custom_constraint_true$Replacer._get_custom_constraint_true  s    **?NKKrI   Nr   conditions_trueconditions_nonfalsec                    [        U5      n[        U5      nU R                  S S  nU Vs/ s H  opR                  U5      PM     nnU Vs/ s H  opR                  U5      PM     n	nUR	                  U5        UR	                  U	5        [
        R                  " U/UQ76 n
U R                  (       aN  SSR                  S UR                  [        5       5       5       S[        U5       3nU R                  U5      nUnOIU R                  R                  UR                  [        5       Vs0 s H  n[!        U5      U_M     sn5        U R"                  (       a  [%        X%5      nU R&                  R)                  X5        g s  snf s  snf s  snf )Nr   r   c              3   8   #    U  H  oR                   v   M     g 7frK   r   ).0r   s     rG   	<genexpr>Replacer.add.<locals>.<genexpr>+  s     -X>Wff>Ws   z: )r   r   r   r   extendr7   Patternr   r   r   ri   r   r   r   updaterV   rl   r   r   r   add)r]   r   r   r   r   r   constraintscondconstraint_conditions_trueconstraint_conditions_nonfalsepatternr   lambda_expris                 rG   r   Replacer.add  sb   ~{+--a0?N&P?Nt,,T2 	# &P DW*XCV4006CV 	' *X569://$55>>"499-Xdjj>W-X#Y"ZZ\]ghs]t\uvJ**:6K%KOO""tzz(7K#L7K!CFAI7K#LM::)+<K'/&P*X $Ms   E?
F*F		max_countc           	         / nSnSnU(       Ga2  US:  d  XR:  Ga&  Sn[         R                  U5       H  u  pg [        [        U R                  R                  U5      5      5      u  pU R                  (       a(  UR                  n
UR                  UR                  5        OUn
U R                  (       a	  U
" S0 U	D6nOBU
R                  U	R                  5        VVs0 s H  u  pU R                  U   U_M     snn5      n[         R                  R                  XU5      nSn  O   US-  nU(       a  US:  a  GM  XR:  a  GM&  U R                  (       a  X4$ U$ s  snnf ! [          a     GM9  f = f)NTr   FrA   rf   )r7   preorder_iter_with_positionnextrC   r   matchr   r   appendr   r   xreplaceitemsr   	functionsreplaceStopIteration)r]   
expressionr   infosreplacedreplace_countsubexprposreplacement_datasubstr   resultkvs                 rG   r   Replacer.replace4  sN    IM]-FH ' C CJ O.248K8KG8T3U.V+$zz&6&B&B%5%:%:;&6~~!,!5u!5!,!5!5Y^YdYdYf6gYfQUQRtq7I17LYf6g!h!(!2!2!:!::F!SJ#H! !P& QM+ IM]-F, ::$$ 7h
 % s$   B"EE6*EE
E,+E,)r   r   r   r   r   ))ra   rb   rc   rd   __doc__listr   r_   rl   r   r-   r   r   r   r   r   r   r   r   r   re   rf   rI   rG   r   r      s   6p 35u[` 24 2 2TX 2*c *hsDy.A *Ed EPS EX`adfjajXk EMt MQTVZQZH[ ML4 LHSRVYDW L JL.0d0 0DJ 0!%d08;0GK0, S    rI   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.relationalr0   r1   sympy.core.symbolr2   &sympy.functions.elementary.exponentialr3   sympy.integrals.integralsr4   sympy.printing.reprr5   sympy.utilities.decoratorr6   r7   __doctest_requires__r9   r:   r;   r<   matchpy.expressions.functionsr=   r>   r?   registerrH   rU   rV   ri   r   r   r   r   r   r   rf   rI   rG   <module>r     s   
 8 8 ' ( W W W ~ ~ ~ ~ W W V V  "      8 $ 6 . % 8 	
" ,  ccZZx s!!#&s!!#&!!#&!!#&s!!#&!!#&!!#&x !!(+z"!!*-ssuz"x x srttssssssttttttttttttuuuuuuh@  @ e% % __X+ + __U$ $ !))%02 12  % % L)-Hf - *-` L)m  *
 L)}  *
 L)}  *
j 
 L)H H *HrI   