U
    ˜9%eT'  ã                   @   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ràddlEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZM eF Ne@¡ eF Ne7¡ eI Ne7¡ eF Ne/¡ eI Ne/¡ eG Ne/¡ eH Ne/¡ eF Ne5¡ eI Ne5¡ eG Ne5¡ eH Ne5¡ eF Ne9¡ eG Ne9¡ eF Ne:¡ eG Ne:¡ eF Ne>¡ eF Ne
¡ eF Ne¡ eF Ne¡ eF Ne*¡ eF Ne)¡ eF Ne¡ eF Ne-¡ eF Ne+¡ eF Ne,¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne ¡ eF Ne¡ eF Ne$¡ eF Ne&¡ eF Ne"¡ eF Ne%¡ eF Ne#¡ eF Ne'¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eF Ne¡ eK Ne@¡dd„ ƒZOeK Ne1¡dd„ ƒZOeM Ne@¡dd„ ƒZOeM Ne1¡dd„ ƒZOeL Ne1¡d1dd„ƒZPeErôd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©Z	operation© r@   ú`/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/utilities/matchpy_connector.pyÚ_V   s    rB   c                 C   s
   t | jƒS ©Nr<   r?   r@   r@   rA   rB   Z   s    c                 C   s   dt | jd ƒ S )Nr;   ©Úlenr>   r?   r@   r@   rA   rB   ^   s    c                 C   s
   t | jƒS rC   rD   r?   r@   r@   rA   rB   b   s    Tc                 C   s   t | ƒ|Ž S rC   )Útype)Zold_operationZnew_operandsÚvariable_namer@   r@   rA   Úsympy_op_factoryf   s    rH   )ÚWildcardc                   @   s   e Zd Zdd„ ZdS )rI   c                 C   s   || _ || _|| _|| _d S rC   )Ú	min_countÚ
fixed_sizerG   Úoptional)ÚselfÚ
min_lengthrK   rG   rL   r@   r@   rA   Ú__init__o   s    zWildcard.__init__N)Ú__name__Ú
__module__Ú__qualname__rO   r@   r@   r@   rA   rI   n   s   rI   )r3   )Úmodulesc                       s~   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d„Zdd„ Zdd„ Z‡  ZS )Ú_WildAbstractrN   rK   Nc                 K   s6   | j }| j}|d k	rt|ƒ}t | ||t|ƒ|¡ d S rC   )rN   rK   r   rI   rO   Ústr)rM   rG   rL   ÚassumptionsrN   rK   r@   r@   rA   rO   {   s
    z_WildAbstract.__init__c                 C   s   | j | j| j| j| jdœS )N©rN   rK   rJ   rG   rL   rW   ©rM   r@   r@   rA   Ú__getstate__‚   s    ûz_WildAbstract.__getstate__c                 K   s   |   || ¡ tj| ||f|ŽS rC   )Ú	_sanitizerT   Ú__xnew__)ÚclsrG   rL   rV   r@   r@   rA   Ú__new__‹   s    z_WildAbstract.__new__c                 C   s   | j | jfS rC   ©rG   rL   rX   r@   r@   rA   Ú__getnewargs__   s    z_WildAbstract.__getnewargs__c                 K   s   t j| |f|Ž}|S rC   )r.   r[   )r\   rG   rL   rV   Úobjr@   r@   rA   r[   ’   s    z_WildAbstract.__xnew__c                    sB   | j r$tƒ  ¡ | j| j| j| j f S tƒ  ¡ | j| j| jf S d S rC   )rL   ÚsuperÚ_hashable_contentrJ   rK   rG   rX   ©Ú	__class__r@   rA   rb   —   s    z_WildAbstract._hashable_content)Úreturnc                 C   s   t | ƒ| j| jdS )Nr^   )rF   rG   rL   rX   r@   r@   rA   Ú__copy__   s    z_WildAbstract.__copy__c                 C   s   t | ƒS rC   )rU   rX   r@   r@   rA   Ú__repr__    s    z_WildAbstract.__repr__c                 C   s   | j S rC   ©ÚnamerX   r@   r@   rA   Ú__str__£   s    z_WildAbstract.__str__)NN)NN)NN)rP   rQ   rR   ÚintÚ__annotations__ÚboolrO   rY   r]   r_   Ústaticmethodr[   rb   rf   rg   rj   Ú__classcell__r@   r@   rc   rA   rT   v   s   

	
rT   c                   @   s   e Zd ZdZdZdS )ÚWildDotr;   TN©rP   rQ   rR   rN   rK   r@   r@   r@   rA   rp   §   s   rp   c                   @   s   e Zd ZdZdZdS )ÚWildPlusr;   FNrq   r@   r@   r@   rA   rr   ­   s   rr   c                   @   s   e Zd ZdZdZdS )ÚWildStarr   FNrq   r@   r@   r@   rA   rs   ³   s   rs   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Úsr@   r@   rA   Ú
_get_srepr¹   s
    rx   c                   @   s¸   e Zd ZdZg fedœdd„Zeedef dœdd„Z	eeedef d	œd
d„Z
eedef dœdd„Zeedef dœdd„Zg g feeee ee ddœdd„Zeedœ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 rC   )r3   ZManyToOneReplacerÚ	_replacerÚ_common_constraint)rM   rz   r@   r@   rA   rO   û   s    
zReplacer.__init__.)Ú
lambda_strre   c                 C   s   t dƒ t|tƒ ƒS )Nzfrom sympy import *)ÚexecÚevalÚlocals)rM   r}   r@   r@   rA   Ú_get_lambdaÿ   s    zReplacer._get_lambda)Úconstraint_exprÚcondition_templatere   c              	   C   sN   dd„ |  t¡D ƒ}d |¡}t|ƒ}| |¡}t |  d|› d|› d¡¡S )Nc                 S   s   g | ]
}|j ‘qS r@   rh   ©Ú.0Úxr@   r@   rA   Ú
<listcomp>  s     z3Replacer._get_custom_constraint.<locals>.<listcomp>ú, úlambda z: (ú))ÚatomsrT   Újoinrx   Úformatr3   ZCustomConstraintr   )rM   r‚   rƒ   ZwildsZ
lambdaargsZfullexprÚ	conditionr@   r@   rA   Ú_get_custom_constraint  s    

ÿzReplacer._get_custom_constraint)r‚   re   c                 C   s   |   |d¡S )Nz({}) != False©r   ©rM   r‚   r@   r@   rA   Ú_get_custom_constraint_nonfalse  s    z(Replacer._get_custom_constraint_nonfalsec                 C   s   |   |d¡S )Nz({}) == Truer   r‘   r@   r@   rA   Ú_get_custom_constraint_true  s    z$Replacer._get_custom_constraint_trueN)rv   ÚresultÚconditions_trueÚconditions_nonfalsere   c           
         s®   t |ƒ}t |ƒ}dd dd„ | t¡D ƒ¡› dt|ƒ› }ˆ  |¡}ˆ jd d … }‡ fdd„|D ƒ}‡ fdd„|D ƒ}	| |¡ | |	¡ ˆ j 	t
 t
j|f|žŽ |¡¡ d S )	Nr‰   rˆ   c                 s   s   | ]}|j V  qd S rC   rh   r„   r@   r@   rA   Ú	<genexpr>  s     zReplacer.add.<locals>.<genexpr>z: c                    s   g | ]}ˆ   |¡‘qS r@   )r“   ©r…   ZcondrX   r@   rA   r‡     s    z Replacer.add.<locals>.<listcomp>c                    s   g | ]}ˆ   |¡‘qS r@   )r’   r˜   rX   r@   rA   r‡     s    )r   rŒ   r‹   rT   rx   r   r|   Úextendr{   Úaddr3   ZReplacementRuleÚPattern)
rM   rv   r”   r•   r–   r}   Zlambda_exprÚconstraintsZconstraint_conditions_trueZconstraint_conditions_nonfalser@   rX   rA   rš     s     *

ÿ
ÿ

ÿzReplacer.add)rv   re   c                 C   s   | j  |¡S rC   )r{   Úreplace)rM   rv   r@   r@   rA   r      s    zReplacer.replace)rP   rQ   rR   Ú__doc__ÚlistrO   rU   r   r)   r   r   r’   r“   r   rš   r   r@   r@   r@   rA   ry   Á   s   8$ry   )T)Xrž   rt   Útypingr   r   Zsympy.core.sympifyr   Zsympy.externalr   Zsympy.functionsr   r   r   r	   r
   r   r   r   r   r   Z%sympy.functions.elementary.hyperbolicr   r   r   r   r   r   r   r   r   r   r   r   Z(sympy.functions.elementary.trigonometricr   r   r   r   r    r!   Z'sympy.functions.special.error_functionsr"   r#   r$   r%   r&   Zsympy.core.addr'   Zsympy.core.basicr(   Zsympy.core.exprr)   Zsympy.core.mulr*   Zsympy.core.powerr+   Zsympy.core.relationalr,   r-   Zsympy.core.symbolr.   Z&sympy.functions.elementary.exponentialr/   Zsympy.integrals.integralsr0   Zsympy.printing.reprr1   Zsympy.utilities.decoratorr2   r3   r4   r5   r6   r7   Zmatchpy.expressions.functionsr8   r9   r:   ÚregisterrB   rH   rI   rT   rp   rr   rs   rx   ry   r@   r@   r@   rA   Ú<module>   sÀ   08 




















































0