
    RieT'                     <
   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  eFjN        e@            eFjN        e7            eIjN        e7            eFjN        e/            eIjN        e/            eGjN        e/            eHjN        e/            eFjN        e5            eIjN        e5            eGjN        e5            eHjN        e5            eFjN        e9            eGjN        e9            eFjN        e:            eGjN        e:            eFjN        e>            eFjN        e
            eFjN        e            eFjN        e            eFjN        e*            eFjN        e)            eFjN        e            eFjN        e-            eFjN        e+            eFjN        e,            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e             eFjN        e            eFjN        e$            eFjN        e&            eFjN        e"            eFjN        e%            eFjN        e#            eFjN        e'            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eFjN        e            eKjN        e@          d             ZO eKjN        e1          d             ZO eMjN        e@          d             ZO eMjN        e1          d             ZO eLjN        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-                      ZWdS )/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    Alib/python3.11/site-packages/sympy/utilities/matchpy_connector.py_rD   V   s&    Y_Q')IOA,>>???    c                 *    t          | j                  S Nr>   rA   s    rC   rD   rD   Z   s    IO$$$rE   c                 <    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   Tc                 &     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dS )rR   c                 >    || _         || _        || _        || _        d S rG   )	min_count
fixed_sizerP   optional)self
min_lengthrV   rP   rW   s        rC   __init__zWildcard.__init__o   s#    'DN(DO!.D$DMMMrE   N)__name__
__module____qualname__rZ    rE   rC   rR   rR   n   s#        	% 	% 	% 	% 	%rE   rR   )r4   )modulesc                   |     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   Nc                     | j         }| j        }|t          |          }t          j        | ||t          |          |           d S rG   )rY   rV   r   rR   rZ   str)rX   rP   rW   assumptionsrY   rV   s         rC   rZ   z_WildAbstract.__init__{   sJ    _
_
))H$
JM8J8JHUUUUUrE   c                 D    | j         | j        | j        | j        | j        dS )NrY   rV   rU   rP   rW   rf   rX   s    rC   __getstate__z_WildAbstract.__getstate__   s+    //!/
 
 	
rE   c                 V    |                      ||            t          j        | ||fi |S rG   )	_sanitizera   __xnew__)clsrP   rW   rd   s       rC   __new__z_WildAbstract.__new__   s3    k3'''%c=(RRkRRRrE   c                     | j         | j        fS rG   rP   rW   rg   s    rC   __getnewargs__z_WildAbstract.__getnewargs__   s    !4=00rE   c                 ,    t          j        | |fi |}|S rG   )r/   rk   )rl   rP   rW   rd   objs        rC   rk   z_WildAbstract.__xnew__   s     oc=@@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   sh    = 	g77,,..$.$/SWSegkgt1uuu77,,..$.$/SWSe1fffrE   returnc                 J     t          |           | j        | j                  S )Nro   )rM   rP   rW   rg   s    rC   __copy__z_WildAbstract.__copy__   s"    tDzz(:T]SSSSrE   c                      t          |           S rG   )rc   rg   s    rC   __repr__z_WildAbstract.__repr__   s    4yyrE   c                     | j         S rG   namerg   s    rC   __str__z_WildAbstract.__str__   s
    yrE   )NN)rw   ra   )r[   r\   r]   int__annotations__boolrZ   rh   rm   rp   staticmethodrk   ru   ry   r{   r   __classcell__)rv   s   @rC   ra   ra   v   s         OOOV V V V
 
 
S S S S1 1 1    \g g g g gT T T T        rE   ra   c                       e Zd ZdZdZdS )WildDotr=   TNr[   r\   r]   rY   rV   r^   rE   rC   r   r      s        JJJJrE   r   c                       e Zd ZdZdZdS )WildPlusr=   FNr   r^   rE   rC   r   r              JJJJrE   r   c                       e Zd ZdZdZdS )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      sN    dA
$eQ//A
%vq11A
%vq1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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                 P    t                                           | _        || _        d S rG   )r4   ManyToOneReplacer	_replacer_common_constraint)rX   r   s     rC   rZ   zReplacer.__init__   s#     2244"4rE   
lambda_strrw   .c                 X    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           	      $   d |                     t                    D             }d                    |          }t          |          }|                    |          }t
                              |                     d| d| d                    S )Nc                     g | ]	}|j         
S r^   r}   .0xs     rC   
<listcomp>z3Replacer._get_custom_constraint.<locals>.<listcomp>  s    FFFAFFFrE   , lambda z: ())atomsra   joinr   formatr4   CustomConstraintr   )rX   r   r   wilds
lambdaargsfullexpr	conditions          rC   _get_custom_constraintzReplacer._get_custom_constraint  s    FF!6!6}!E!EFFFYYu%%
o..&--h77	''BzBBiBBBCCE E 	ErE   c                 .    |                      |d          S )Nz({}) != Falser   rX   r   s     rC   _get_custom_constraint_nonfalsez(Replacer._get_custom_constraint_nonfalse  s    **?OLLLrE   c                 .    |                      |d          S )Nz({}) == Truer   r   s     rC   _get_custom_constraint_truez$Replacer._get_custom_constraint_true  s    **?NKKKrE   r   resultconditions_trueconditions_nonfalseNc                 2    t          |          }t          |          }dd                    d |                    t                    D                        dt	          |           }                     |          } j        d d          } fd|D             } fd|D             }	|                    |           |                    |	            j        	                    t                              t          j        |g|R  |                     d S )Nr   r   c              3   $   K   | ]}|j         V  d S rG   r}   r   s     rC   	<genexpr>zReplacer.add.<locals>.<genexpr>  s$      )T)TQ!&)T)T)T)T)T)TrE   z: c                 :    g | ]}                     |          S r^   )r   r   condrX   s     rC   r   z Replacer.add.<locals>.<listcomp>  s:     &P &P &P7;D,,T22&P &P &PrE   c                 :    g | ]}                     |          S r^   )r   r   s     rC   r   z Replacer.add.<locals>.<listcomp>  s:     *X *X *X;?D0066*X *X *XrE   )r   r   r   ra   r   r   r   extendr   addr4   ReplacementRulePattern)
rX   r   r   r   r   r   lambda_exprconstraintsconstraint_conditions_trueconstraint_conditions_nonfalses
   `         rC   r   zReplacer.add  s]   ~~&!!mtyy)T)T$**]:S:S)T)T)TUUmmYcdjYkYkmm
&&z22-aaa0&P &P &P &P?N&P &P &P"*X *X *X *XCV*X *X *X&56669:::##GOD$G;$G$G$GUU	W 	W 	W 	W 	WrE   c                 6    | 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 6p 35 5 54 5 5 5 5*c *hsDy.A * * * *Ed EPS EX`adfjajXk E E E EMt MQTVZQZH[ M M M ML4 LHSRVYDW L L L L KMpr W W Wd WT$Z Wcghlcm Ww{ W 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 W W W W W W W W W W W W W W W W W W W ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ W W W W W W W W W W W W W W W W V V V V V V V V V V V V V V       " " " " " "                               8 8 8 8 8 8 8 8 $ $ $ $ $ $ 6 6 6 6 6 6 . . . . . . % % % % % % 8 8 8 8 8 8
-	
"
"
 L3ccccccccccccZZZZZZZZ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03 3 3 103  %       % % % % % % % % L)))- - - - -Hf - - *)-` L)))    m   *)
 L)))    }   *)
 L)))    }   *)
   L)))_, _, _, _, _, _, _, *)_, _, _,rE   