
    g.                     n
   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  eJjR        eC            eJjR        e:            eMjR        e:            eJjR        e2            eMjR        e2            eKjR        e2            eLjR        e2            eJjR        e8            eMjR        e8            eKjR        e8            eLjR        e8            eJjR        e<            eKjR        e<            eJjR        e=            eKjR        e=            eJjR        eA            eJjR        e            eJjR        e            eJjR        e            eJjR        e-            eJjR        e,            eJjR        e            eJjR        e0            eJjR        e.            eJjR        e/            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e             eJjR        e!            eJjR        e#            eJjR        e"            eJjR        e'            eJjR        e)            eJjR        e%            eJjR        e(            eJjR        e&            eJjR        e*            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eJjR        e            eOjR        eC          d             ZS eOjR        e4          d             ZS eQjR        eC          d             ZS eQjR        e4          d             ZS ePjR        e4          d1d            ZTeHrddlHmUZU n G d  d!          ZU eGd"#           G d$ d%eUe?                      ZV eGd"#           G d& d'eV                      ZW eGd"#           G d( d)eV                      ZX eGd"#           G d* d+eV                      ZYd, ZZ G d- d.e          Z[ eGd"#           G d/ d0                      Z\dS )2zj
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    t          | j        d         f| j        d         z             S )Nr      iter_args	operations    ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/utilities/matchpy_connector.py_rH   [   s&    Y_Q')IOA,>>???    c                 *    t          | j                  S NrB   rE   s    rG   rH   rH   _   s    IO$$$rI   c                 <    dt          | j        d                   z   S )NrA   lenrD   rE   s    rG   rH   rH   c   s    3yq)****rI   c                 *    t          | j                  S rK   rM   rE   s    rG   rH   rH   g   s    9?###rI   Tc                 &     t          |           | S rK   )type)old_operationnew_operandsvariable_names      rG   sympy_op_factoryrU   k   s    "tM""L11rI   )Wildcardc                       e Zd Zd ZdS )rV   c                 >    || _         || _        || _        || _        d S rK   )	min_count
fixed_sizerT   optional)self
min_lengthrZ   rT   r[   s        rG   __init__zWildcard.__init__t   s#    'DN(DO!.D$DMMMrI   N)__name__
__module____qualname__r^    rI   rG   rV   rV   s   s#        	% 	% 	% 	% 	%rI   rV   )r7   )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 )_WildAbstractr]   rZ   Nc                     | j         }| j        }|t          |          }t          j        | ||t          |          |           d S rK   )r]   rZ   r   rV   r^   str)r\   rT   r[   assumptionsr]   rZ   s         rG   r^   z_WildAbstract.__init__   sJ    _
_
))H$
JM8J8JHUUUUUrI   c                 D    | j         | j        | j        | j        | j        dS )Nr]   rZ   rY   rT   r[   rj   r\   s    rG   __getstate__z_WildAbstract.__getstate__   s+    //!/
 
 	
rI   c                 V    |                      ||            t          j        | ||fi |S rK   )	_sanitizere   __xnew__)clsrT   r[   rh   s       rG   __new__z_WildAbstract.__new__   s3    k3'''%c=(RRkRRRrI   c                     | j         | j        fS rK   rT   r[   rk   s    rG   __getnewargs__z_WildAbstract.__getnewargs__   s    !4=00rI   c                 ,    t          j        | |fi |}|S rK   )r2   ro   )rp   rT   r[   rh   objs        rG   ro   z_WildAbstract.__xnew__   s     oc=@@K@@
rI   c                     | j         r;t                                                      | j        | j        | j        | j         fz   S t                                                      | j        | j        | j        fz   S rK   )r[   super_hashable_contentrY   rZ   rT   )r\   	__class__s    rG   ry   z_WildAbstract._hashable_content   sh    = 	g77,,..$.$/SWSegkgt1uuu77,,..$.$/SWSe1fffrI   returnc                 J     t          |           | j        | j                  S )Nrs   )rQ   rT   r[   rk   s    rG   __copy__z_WildAbstract.__copy__   s"    tDzz(:T]SSSSrI   c                      t          |           S rK   rg   rk   s    rG   __repr__z_WildAbstract.__repr__   s    4yyrI   c                     | j         S rK   namerk   s    rG   __str__z_WildAbstract.__str__   s
    yrI   )NN)r{   re   )r_   r`   ra   int__annotations__boolr^   rl   rq   rt   staticmethodro   ry   r}   r   r   __classcell__)rz   s   @rG   re   re   {   s         OOOV V V V
 
 
S S S S1 1 1    \g g g g gT T T T        rI   re   c                       e Zd ZdZdZdS )WildDotrA   TNr_   r`   ra   r]   rZ   rb   rI   rG   r   r      s        JJJJrI   r   c                       e Zd ZdZdZdS )WildPlusrA   FNr   rb   rI   rG   r   r              JJJJrI   r   c                       e Zd ZdZdZdS )WildStarr   FNr   rb   rI   rG   r   r      r   rI   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+)'\))r5   resub)exprss     rG   
_get_sreprr      sN    dA
$eQ//A
%vq11A
%vq11AHrI   c                   $    e Zd ZU eed<   eed<   dS )ReplacementInforeplacementinfoN)r_   r`   ra   r   r   rb   rI   rG   r   r      s%         
IIIIIrI   r   c                       e Zd ZdZg ddfdede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 dfde	dee	         dee	         deddf
dZd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._matcher.clear()
    >>> replacer.replace(eq)
    4/3
    Fcommon_constraintslambdifyr   c                 z    t                                           | _        || _        || _        || _        i | _        d S rK   )r7   ManyToOneMatcher_matcher_common_constraint	_lambdify_info
_wildcards)r\   r   r   r   s       rG   r^   zReplacer.__init__  s6    0022"4!
/1rI   
lambda_strr{   .c                 X    t          d           t          |t                                S )Nzfrom sympy import *)execevallocals)r\   r   s     rG   _get_lambdazReplacer._get_lambda  s%    "###J)))rI   constraint_exprcondition_templatec           	      $   d |                     t                    D             }d                    |          }t          |          }|                    |          }t
                              |                     d| d| d                    S )Nc                     g | ]	}|j         
S rb   r   .0xs     rG   
<listcomp>z3Replacer._get_custom_constraint.<locals>.<listcomp>  s    FFFAFFFrI   , lambda z: ())atomsre   joinr   formatr7   CustomConstraintr   )r\   r   r   wilds
lambdaargsfullexpr	conditions          rG   _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rI   c                 .    |                      |d          S )Nz({}) != Falser   r\   r   s     rG   _get_custom_constraint_nonfalsez(Replacer._get_custom_constraint_nonfalse  s    **?OLLLrI   c                 .    |                      |d          S )Nz({}) == Truer   r   s     rG   _get_custom_constraint_truez$Replacer._get_custom_constraint_true  s    **?NKKKrI   Nr   conditions_trueconditions_nonfalsec                     t          |          }t          |          } j        d d          } fd|D             } fd|D             }|                    |           |                    |           t          j        |g|R  }	 j        rbdd                    d |                    t                    D                        dt          |           }
 
                    |
          }|}n< j                            d |                    t                    D                         j        rt          ||          } j                            |	|           d S )Nc                 :    g | ]}                     |          S rb   )r   r   condr\   s     rG   r   z Replacer.add.<locals>.<listcomp>#  s:     &P &P &P7;D,,T22&P &P &PrI   c                 :    g | ]}                     |          S rb   )r   r   s     rG   r   z Replacer.add.<locals>.<listcomp>%  s:     *X *X *X;?D0066*X *X *XrI   r   r   c              3   $   K   | ]}|j         V  d S rK   r   r   s     rG   	<genexpr>zReplacer.add.<locals>.<genexpr>+  s$      -X-Xaf-X-X-X-X-X-XrI   z: c                 .    i | ]}t          |          |S rb   r   )r   is     rG   
<dictcomp>z Replacer.add.<locals>.<dictcomp>/  s     #L#L#L!CFFA#L#L#LrI   )r   r   extendr7   Patternr   r   r   re   r   r   r   updaterV   r   r   r   add)r\   r   r   r   r   r   constraintsconstraint_conditions_trueconstraint_conditions_nonfalsepatternr   lambda_exprs   `           rG   r   zReplacer.add  s   ~~{++-aaa0&P &P &P &P?N&P &P &P"*X *X *X *XCV*X *X *X&56669:::/$5555> 	Nv499-X-Xdjj>W>W-X-X-X#Y#Yvv]ghs]t]tvvJ**:66K%KKO""#L#Ltzz(7K7K#L#L#LMMM: 	=)+t<<K';/////rI   	max_countc                 l    g }d}d}|r|dk     s||k     rd}t                               |          D ]\  }}	 t          t           j                            |                              \  }}	 j        r"|j        }
|                    |j	                   n|}
 j
        r	 |
di |	}n3|
                     fd|	                                D                       }t           j                            |||          }d} n# t          $ r Y w xY w|dz  }|r|dk     
||k      j        r||fS |S )NTr   Fc                 2    i | ]\  }}j         |         |S rb   )r   )r   kvr\   s      rG   r   z$Replacer.replace.<locals>.<dictcomp>I  s)    6g6g6gQUQRTUtq7I16g6g6grI   rA   rb   )r7   preorder_iter_with_positionnextrC   r   matchr   r   appendr   r   xreplaceitems	functionsreplaceStopIteration)r\   
expressionr   infosreplacedreplace_countsubexprposreplacement_datasubstr   results   `           rG   r   zReplacer.replace4  s     	IMM]Y-F-FH ' C CJ O O  .248K8KG8T8T3U3U.V.V+$ez 7&6&B%5%:;;;;&6~ i!,!5!5u!5!5!,!5!56g6g6g6gY^YdYdYfYf6g6g6g!h!h!(!2!:!::sF!S!SJ#HE$   DQM+  	IMM]Y-F-F, : 	u$$s   CD
DD)r   )r_   r`   ra   __doc__listr   r^   rg   r   r-   r   r   r   r   r   r   r   r   r   rb   rI   rG   r   r      s       6 6p 35u[` 2 24 2 2TX 2 2 2 2*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 JL.0d0 0 0DJ 0!%d08;0GK0 0 0 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   re   r   r   r   r   r   r   rb   rI   rG   <module>r     s	    
			 8 8 8 8 8 8 8 8 8 8 8 8 8 8 ' ' ' ' ' ' ( ( ( ( ( ( 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2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2 2 2 102  %       % % % % % % % % L)))- - - - -Hf - - *)-` L)))    m   *)
 L)))    }   *)
 L)))    }   *)
      j   
 L)))H H H H H H H *)H H HrI   