o
    à8Va¦$  ã                   @   sH  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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; edƒZ<e<rÌddl<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZD e= Ee3¡ e= Ee2¡ e@ Ee2¡ e= Ee1¡ e@ Ee1¡ e> Ee1¡ e? Ee1¡ e= Ee0¡ e@ Ee0¡ e> Ee0¡ e? Ee0¡ e= Ee8¡ e> Ee8¡ e= Ee9¡ e> Ee9¡ e= Ee4¡ e= Ee
¡ e= Ee¡ e= Ee¡ e= Ee*¡ e= Ee)¡ e= Ee¡ e= Ee-¡ e= Ee+¡ e= Ee,¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee ¡ e= Ee¡ e= Ee$¡ e= Ee&¡ e= Ee"¡ e= Ee%¡ e= Ee#¡ e= Ee'¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ e= Ee¡ eB Ee3¡dd„ ƒZFeB Ee/¡dd„ ƒZFeD Ee3¡dd„ ƒZFeD Ee/¡dd„ ƒZFeC Ee/¡d(dd„ƒZGe<rÖddl<mHZH nG dd„ dƒZHe;ddG dd„ deHe5ƒƒZIe;ddG dd„ deIƒƒZJe;ddG d d!„ d!eIƒƒZKe;ddG d"d#„ d#eIƒƒZLd$d%„ ZMe;ddG d&d'„ d'ƒƒZNd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)ÚBasicÚMulÚAddÚPowÚIntegralÚexpÚSymbolÚExprÚsreprÚEqualityÚ
Unequality)Ú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@   úC/usr/lib/python3/dist-packages/sympy/utilities/matchpy_connector.pyÚ_M   s   rB   c                 C   ó
   t | jƒS ©Nr<   r?   r@   r@   rA   rB   Q   ó   
c                 C   s   dt | jd ƒ S )Nr;   ©Úlenr>   r?   r@   r@   rA   rB   U   s   c                 C   rC   rD   rF   r?   r@   r@   rA   rB   Y   rE   Tc                 C   s   t | ƒ|Ž S rD   )Útype)Zold_operationZnew_operandsÚvariable_namer@   r@   rA   Úsympy_op_factory]   s   rJ   )ÚWildcardc                   @   s   e Zd Zdd„ ZdS )rK   c                 C   s   d S rD   r@   )ÚselfÚ
min_lengthÚ
fixed_sizerI   Úoptionalr@   r@   rA   Ú__init__f   s   zWildcard.__init__N)Ú__name__Ú
__module__Ú__qualname__rP   r@   r@   r@   rA   rK   e   s    rK   )r3   )Úmodulesc                       sh   e Zd ZdZdZd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 )Ú_WildAbstractNc                 K   s6   | j }| j}|d urt|ƒ}t | ||t|ƒ|¡ d S rD   )rM   rN   r   rK   rP   Ústr)rL   rI   rO   ÚassumptionsrM   rN   r@   r@   rA   rP   o   s
   z_WildAbstract.__init__c                 K   s"   |   || ¡ tj| ||fi |¤ŽS rD   )Z	_sanitizerU   Ú__xnew__)ÚclsrI   rO   rW   r@   r@   rA   Ú__new__v   s   z_WildAbstract.__new__c                 C   s   | j | j| j| jfS rD   )Ú	min_countrN   rI   rO   ©rL   r@   r@   rA   Ú__getnewargs__z   ó   z_WildAbstract.__getnewargs__c                 K   s   t j| |fi |¤Ž}|S rD   )r-   rX   )rY   rI   rO   rW   Úobjr@   r@   rA   rX   }   s   z_WildAbstract.__xnew__c                    s>   | j rtƒ  ¡ | j| j| j| j f S tƒ  ¡ | j| j| jf S rD   )rO   ÚsuperÚ_hashable_contentr[   rN   rI   r\   ©Ú	__class__r@   rA   ra   ‚   s   z_WildAbstract._hashable_contentÚreturnc                 C   s   t | ƒ| j| jdS )N)rI   rO   )rH   rI   rO   r\   r@   r@   rA   Ú__copy__ˆ   r^   z_WildAbstract.__copy__c                 C   s   t | ƒS rD   )rV   r\   r@   r@   rA   Ú__repr__‹   s   z_WildAbstract.__repr__c                 C   ó   | j S rD   ©Únamer\   r@   r@   rA   Ú__str__Ž   s   z_WildAbstract.__str__)NN)rd   rU   )rQ   rR   rS   rM   rN   rP   rZ   r]   ÚstaticmethodrX   ra   re   rf   rj   Ú__classcell__r@   r@   rb   rA   rU   j   s    


rU   c                   @   ó   e Zd ZdZdZdS )ÚWildDotr;   TN©rQ   rR   rS   rM   rN   r@   r@   r@   rA   rn   ’   ó    rn   c                   @   rm   )ÚWildPlusr;   FNro   r@   r@   r@   rA   rq   ˜   rp   rq   c                   @   rm   )ÚWildStarr   FNro   r@   r@   r@   rA   rr   ž   rp   rr   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+)'\))r/   ÚreÚsub)ÚexprÚsr@   r@   rA   Ú
_get_srepr¤   s
   rw   c                   @   sÌ   e Zd ZdZg f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 fdededee dee ddf
dd„Zde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 won't 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 won't 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 rD   )r3   ZManyToOneReplacerÚ	_replacerÚ_common_constraint)rL   ry   r@   r@   rA   rP   æ   s   

zReplacer.__init__Ú
lambda_strrd   .c                 C   s   t dƒ t|tƒ ƒS )Nzfrom sympy import *)ÚexecÚevalÚlocals)rL   r|   r@   r@   rA   Ú_get_lambdaê   s   zReplacer._get_lambdaÚconstraint_exprÚcondition_templatec              	   C   sR   t tdd„ | t¡ƒƒ}d |¡}t|ƒ}| |¡}t |  	d|› d|› d¡¡S )Nc                 S   rg   rD   rh   ©Úxr@   r@   rA   Ú<lambda>ï   ó    z1Replacer._get_custom_constraint.<locals>.<lambda>ú, úlambda z: (ú))
ÚlistÚmapÚatomsrU   Újoinrw   Úformatr3   ZCustomConstraintr€   )rL   r   r‚   ZwildsZ
lambdaargsZfullexprZ	conditionr@   r@   rA   Ú_get_custom_constraintî   s   

ÿzReplacer._get_custom_constraintc                 C   ó   |   |d¡S )Nz({}) != False©r   ©rL   r   r@   r@   rA   Ú_get_custom_constraint_nonfalseö   ó   z(Replacer._get_custom_constraint_nonfalsec                 C   r   )Nz({}) == Truer‘   r’   r@   r@   rA   Ú_get_custom_constraint_trueù   r”   z$Replacer._get_custom_constraint_trueru   ÚresultÚconditions_trueÚconditions_nonfalseNc           
         s°   t |ƒ}t |ƒ}dd tdd„ | t¡ƒ¡› dt|ƒ› }ˆ  |¡}ˆ jd d … }‡ fdd„|D ƒ}‡ fdd„|D ƒ}	| |¡ | |	¡ ˆ j	 
t tj|g|¢R Ž |¡¡ d S )	Nrˆ   r‡   c                 S   rg   rD   rh   rƒ   r@   r@   rA   r…   ÿ   r†   zReplacer.add.<locals>.<lambda>z: c                    ó   g | ]}ˆ   |¡‘qS r@   )r•   ©Ú.0Zcondr\   r@   rA   Ú
<listcomp>  ó    
ÿz Replacer.add.<locals>.<listcomp>c                    r™   r@   )r“   rš   r\   r@   rA   rœ     r   )r   r   r‹   rŒ   rU   rw   r€   r{   Úextendrz   Úaddr3   ZReplacementRuleÚPattern)
rL   ru   r–   r—   r˜   r|   Zlambda_exprZconstraintsZconstraint_conditions_trueZconstraint_conditions_nonfalser@   r\   rA   rŸ   ü   s    *

ÿ
ÿ

ÿzReplacer.addc                 C   s   | j  |¡S rD   )rz   Úreplace)rL   ru   r@   r@   rA   r¡     r”   zReplacer.replace)rQ   rR   rS   Ú__doc__rŠ   rP   rV   r   r.   r€   r   r“   r•   r   rŸ   r¡   r@   r@   r@   rA   rx   ¬   s    8,rx   )T)Or¢   rs   Ú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sympyr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   Zsympy.utilities.decoratorr2   r3   r4   r5   r6   r7   Zmatchpy.expressions.functionsr8   r9   r:   ÚregisterrB   rJ   rK   rU   rn   rq   rr   rw   rx   r@   r@   r@   rA   Ú<module>   s°    08 4




















































'