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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                 C   s   t | jd f| jd  ƒS )Nr   é   ©ÚiterÚ_args©Ú	operation© rE   úu/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/utilities/matchpy_connector.pyÚ_[   s   rG   c                 C   ó
   t | jƒS ©Nr@   rC   rE   rE   rF   rG   _   ó   
c                 C   s   dt | jd ƒ S )Nr?   ©ÚlenrB   rC   rE   rE   rF   rG   c   s   c                 C   rH   rI   rK   rC   rE   rE   rF   rG   g   rJ   Tc                 C   s   t | ƒ|Ž S rI   )Útype)Úold_operationÚnew_operandsÚvariable_namerE   rE   rF   Úsympy_op_factoryk   s   rQ   )ÚWildcardc                   @   s   e Zd Zdd„ ZdS )rR   c                 C   s   || _ || _|| _|| _d S rI   )Ú	min_countÚ
fixed_sizerP   Úoptional)ÚselfÚ
min_lengthrT   rP   rU   rE   rE   rF   Ú__init__t   s   
zWildcard.__init__N)Ú__name__Ú
__module__Ú__qualname__rX   rE   rE   rE   rF   rR   s   s    rR   )r6   )Úmodulesc                       sz   e Zd ZU eed< eed< ddd„Zdd„ Zddd	„Zd
d„ Z	e
ddd„ƒZ‡ fdd„Zddd„Zdd„ Zdd„ Z‡  ZS )Ú_WildAbstractrW   rT   Nc                 K   s6   | j }| j}|d urt|ƒ}t | ||t|ƒ|¡ d S rI   )rW   rT   r   rR   rX   Ústr)rV   rP   rU   ÚassumptionsrW   rT   rE   rE   rF   rX   €   s
   z_WildAbstract.__init__c                 C   s   | j | j| j| j| jdœS )N©rW   rT   rS   rP   rU   r`   ©rV   rE   rE   rF   Ú__getstate__‡   s   ûz_WildAbstract.__getstate__c                 K   s"   |   || ¡ tj| ||fi |¤ŽS rI   )Ú	_sanitizer]   Ú__xnew__)ÚclsrP   rU   r_   rE   rE   rF   Ú__new__   s   z_WildAbstract.__new__c                 C   s   | j | jfS rI   ©rP   rU   ra   rE   rE   rF   Ú__getnewargs__”   ó   z_WildAbstract.__getnewargs__c                 K   s   t j| |fi |¤Ž}|S rI   )r1   rd   )re   rP   rU   r_   ÚobjrE   rE   rF   rd   —   s   z_WildAbstract.__xnew__c                    s>   | j rtƒ  ¡ | j| j| j| j f S tƒ  ¡ | j| j| jf S rI   )rU   ÚsuperÚ_hashable_contentrS   rT   rP   ra   ©Ú	__class__rE   rF   rl   œ   s   z_WildAbstract._hashable_contentÚreturnc                 C   s   t | ƒ| j| jdS )Nrg   )rM   rP   rU   ra   rE   rE   rF   Ú__copy__¢   s   z_WildAbstract.__copy__c                 C   s   t | ƒS rI   ©r^   ra   rE   rE   rF   Ú__repr__¥   s   z_WildAbstract.__repr__c                 C   s   | j S rI   ©Únamera   rE   rE   rF   Ú__str__¨   s   z_WildAbstract.__str__)NN)ro   r]   )rY   rZ   r[   ÚintÚ__annotations__ÚboolrX   rb   rf   rh   Ústaticmethodrd   rl   rp   rr   ru   Ú__classcell__rE   rE   rm   rF   r]   {   s   
 

	
r]   c                   @   ó   e Zd ZdZdZdS )ÚWildDotr?   TN©rY   rZ   r[   rW   rT   rE   rE   rE   rF   r|   ¬   ó    r|   c                   @   r{   )ÚWildPlusr?   FNr}   rE   rE   rE   rF   r   ²   r~   r   c                   @   r{   )ÚWildStarr   FNr}   rE   rE   rE   rF   r€   ¸   r~   r€   c                 C   s6   t | ƒ}t dd|¡}t dd|¡}t dd|¡}|S )NzWildDot\('(\w+)'\)z\1zWildPlus\('(\w+)'\)z*\1zWildStar\('(\w+)'\))r4   ÚreÚsub)ÚexprÚsrE   rE   rF   Ú
_get_srepr¾   s
   r…   c                   @   s   e Zd ZU eed< eed< dS )ÚReplacementInfoÚreplacementÚinfoN)rY   rZ   r[   r   rw   rE   rE   rE   rF   r†   Æ   s   
 r†   c                   @   sØ   e Zd ZdZg ddfdededefdd„Zded	ed
e	f fdd„Z
de	ded	ed
e	f fdd„Zde	d	ed
e	f fdd„Zde	d	ed
e	f fdd„Zg g dfde	dee	 dee	 ded	df
dd„Zddefdd„ZdS ) Ú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                 C   s&   t  ¡ | _|| _|| _|| _i | _d S rI   )r6   ÚManyToOneMatcherÚ_matcherÚ_common_constraintÚ	_lambdifyÚ_infoÚ
_wildcards)rV   rŠ   r‹   rˆ   rE   rE   rF   rX     s
   

zReplacer.__init__Ú
lambda_strro   .c                 C   s   t dƒ t|tƒ ƒS )Nzfrom sympy import *)ÚexecÚevalÚlocals)rV   r’   rE   rE   rF   Ú_get_lambda  s   zReplacer._get_lambdaÚconstraint_exprÚcondition_templatec              	   C   sN   dd„ |  t¡D ƒ}d |¡}t|ƒ}| |¡}t |  d|› d|› d¡¡S )Nc                 S   s   g | ]}|j ‘qS rE   rs   ©Ú.0ÚxrE   rE   rF   Ú
<listcomp>  s    z3Replacer._get_custom_constraint.<locals>.<listcomp>ú, úlambda z: (ú))Úatomsr]   Újoinr…   Úformatr6   ÚCustomConstraintr–   )rV   r—   r˜   ÚwildsÚ
lambdaargsÚfullexprÚ	conditionrE   rE   rF   Ú_get_custom_constraint  s   

ÿzReplacer._get_custom_constraintc                 C   ó   |   |d¡S )Nz({}) != False©r¨   ©rV   r—   rE   rE   rF   Ú_get_custom_constraint_nonfalse  ri   z(Replacer._get_custom_constraint_nonfalsec                 C   r©   )Nz({}) == Truerª   r«   rE   rE   rF   Ú_get_custom_constraint_true  ri   z$Replacer._get_custom_constraint_trueNrƒ   Úconditions_trueÚconditions_nonfalsec                    sæ   t |ƒ}t |ƒ}ˆ jd d … }‡ fdd„|D ƒ}‡ fdd„|D ƒ}| |¡ | |¡ tj|g|¢R Ž }	ˆ jrTdd dd„ | t¡D ƒ¡› dt	|ƒ› }
ˆ  
|
¡}|}nˆ j d	d
„ | t¡D ƒ¡ ˆ jrjt||ƒ}ˆ j |	|¡ d S )Nc                    ó   g | ]}ˆ   |¡‘qS rE   )r­   ©rš   Úcondra   rE   rF   rœ   #  ó    
ÿz Replacer.add.<locals>.<listcomp>c                    r°   rE   )r¬   r±   ra   rE   rF   rœ   %  r³   rž   r   c                 s   s    | ]}|j V  qd S rI   rs   r™   rE   rE   rF   Ú	<genexpr>+  s   € zReplacer.add.<locals>.<genexpr>z: c                 S   s   i | ]}t |ƒ|“qS rE   rq   )rš   ÚirE   rE   rF   Ú
<dictcomp>/  s    z Replacer.add.<locals>.<dictcomp>)r   rŽ   Úextendr6   ÚPatternr   r¡   r    r]   r…   r–   r‘   ÚupdaterR   r   r†   r   Úadd)rV   rƒ   r‡   r®   r¯   rˆ   ÚconstraintsÚconstraint_conditions_trueÚconstraint_conditions_nonfalseÚpatternr’   Úlambda_exprrE   ra   rF   rº     s(   
ÿ
ÿ

*

zReplacer.addéÿÿÿÿÚ	max_countc              	      sú   g }d}d}|rt|dk s||k rtd}t  |¡D ]N\}}zAttˆ j |¡ƒƒ\}}	ˆ jr5|j}
| |j	¡ n|}
ˆ j
rB|
di |	¤Ž}n|
 ‡ fdd„|	 ¡ D ƒ¡}t j |||¡}d}W  n	 tye   Y qw |d7 }|rt|dk s||k sˆ jr{||fS |S )NTr   Fc                    s   i | ]
\}}ˆ j | |“qS rE   )r‘   )rš   ÚkÚvra   rE   rF   r¶   I  s    z$Replacer.replace.<locals>.<dictcomp>r?   rE   )r6   Úpreorder_iter_with_positionÚnextrA   r   Úmatchr   r‡   Úappendrˆ   r   ÚxreplaceÚitemsÚ	functionsÚreplaceÚStopIteration)rV   Ú
expressionrÁ   ÚinfosÚreplacedÚreplace_countÚsubexprÚposÚreplacement_dataÚsubstr‡   ÚresultrE   ra   rF   rË   4  s4   ÿëzReplacer.replace)rÀ   )rY   rZ   r[   Ú__doc__Úlistrx   rX   r^   r   r,   r–   r¨   r¬   r­   r   r   rº   rv   rË   rE   rE   rE   rF   r‰   Ë   s"    8ÿÿÿ
ÿ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.relationalr/   r0   Úsympy.core.symbolr1   Ú&sympy.functions.elementary.exponentialr2   Úsympy.integrals.integralsr3   Úsympy.printing.reprr4   Úsympy.utilities.decoratorr5   r6   Ú__doctest_requires__r8   r9   r:   r;   Úmatchpy.expressions.functionsr<   r=   r>   ÚregisterrG   rQ   rR   r]   r|   r   r€   r…   r†   r‰   rE   rE   rE   rF   Ú<module>   sÆ    08 





















































0