K i. dZddlZddlmZmZmZmZmZddlm Z ddl m Z ddl m Z mZmZmZmZmZmZmZmZmZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*dd l+m,Z,m-Z-m.Z.m/Z/m0Z0dd l1m2Z2dd l3m4Z4dd l5m6Z6dd l7m8Z8ddl9m:Z:ddl;mm?Z?ddl@mAZAddlBmCZCddlDmEZEddlFmGZGe dZHddgiZIeHrddlHmJZJmKZKmLZLmMZMddlNmOZOmPZPmQZQeJjeCeJje:eMje:eJje2eMje2eKje2eLje2eJje8eMje8eKje8eLje8eJje<eKje<eJje=eKje=eJjeAeJje eJjeeJjeeJje-eJje,eJjeeJje0eJje.eJje/eJjeeJjeeJjeeJjeeJjeeJjeeJjeeJjeeJje eJje!eJje#eJje"eJje'eJje)eJje%eJje(eJje&eJje*eJjeeJjeeJjeeJjeeJjeeJjeeOjeCdZSeOje4dZSeQjeCdZSeQje4dZSePje4d0dZTeHrddlHmUZUn Gdd ZUeGd!"Gd#d$eUe?ZVeGd!"Gd%d&eVZWeGd!"Gd'd(eVZXeGd!"Gd)d*eVZYd+ZZGd,d-eZ[eGd!"Gd.d/Z\y)1zj The objects in this module allow the usage of the MatchPy pattern matching library on SymPy expressions. N)ListCallable NamedTupleAnyDict)_sympify) import_module) logsincostancotcscsecerfgamma uppergamma) acoshasinhatanhacothacschasechcoshsinhtanhcothsechcsch)atanacscasinacotacosasec)fresnelcfresnelserfcerfiEi)Add)Basic)Expr)Mul)Pow)Equality Unequality)Symbol)exp)Integral)srepr)doctest_depends_onmatchpy)*) OperationCommutativeOperationAssociativeOperationOneIdentityOperation)op_itercreate_operation_expressionop_lencTt|jdf|jdzS)Nriter_args operations g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/utilities/matchpy_connector.py_rH[s'Y__Q')IOOA,>>??c,t|jSNrBrEs rGrHrH_sIOO$$rIc8dt|jdzS)NrAlenrDrEs rGrHrHcs3yq)***rIc,t|jSrKrMrEs rGrHrHgs9??##rIct||SrK)type) old_operation new_operands variable_names rGsympy_op_factoryrUks"tM"L11rI)WildcardceZdZdZy)rVc<||_||_||_||_yrK) min_count fixed_sizerToptional)self min_lengthrZrTr[s rG__init__zWildcard.__init__ts'DN(DO!.D $DMrIN)__name__ __module__ __qualname__r^rIrGrVrVss %rIrV)r7)modulescveZdZUeed<eed<d dZdZd dZdZ e d dZ fdZ d d Z d Zd ZxZS) _WildAbstractr]rZc |j}|j}| t|}tj|||t ||yrK)r]rZrrVr^str)r\rTr[ assumptionsr]rZs rGr^z_WildAbstract.__init__s?__ __  )H$ JM8JHUrIcv|j|j|j|j|jdS)Nr]rZrYrTr[rjr\s rG __getstate__z_WildAbstract.__getstate__s1////!//   rIc V|j||tj|||fi|SrK) _sanitizere__xnew__)clsrTr[rhs rG__new__z_WildAbstract.__new__s* k3'%%c=(RkRRrIc2|j|jfSrKrTr[rks rG__getnewargs__z_WildAbstract.__getnewargs__s!!4==00rIc 4tj||fi|}|SrK)r2ro)rprTr[rhobjs rGroz_WildAbstract.__xnew__sooc=@K@ rIc|jr=t| |j|j|j |jfzSt| |j|j|j fzSrK)r[super_hashable_contentrYrZrT)r\ __class__s rGryz_WildAbstract._hashable_contentse ==7,.$..$//SWSeSegkgtgt1uu u7,.$..$//SWSeSe1ff frIcPt||j|jS)Nrs)rQrTr[rks rG__copy__z_WildAbstract.__copy__stDz(:(:T]]SSrIct|SrK)rgrks rG__repr__z_WildAbstract.__repr__s 4yrIc|jSrKnamerks rG__str__z_WildAbstract.__str__s yyrI)NN)returnre)r_r`raint__annotations__boolr^rlrqrt staticmethodroryr|r~r __classcell__)rzs@rGrere{sNOV S1g TrIreceZdZdZdZy)WildDotrATNr_r`rar]rZrbrIrGrrs JJrIrceZdZdZdZy)WildPlusrAFNrrbrIrGrr JJrIrceZdZdZdZy)WildStarrFNrrbrIrGrrrrIrct|}tjdd|}tjdd|}tjdd|}|S)NzWildDot\('(\w+)'\)z\1zWildPlus\('(\w+)'\)z*\1zWildStar\('(\w+)'\))r5resub)exprss rG _get_sreprrsI d A $eQ/A %vq1A %vq1A HrIc"eZdZUeed<eed<y)ReplacementInfo replacementinfoN)r_r`rarrrbrIrGrrs  IrIrc eZdZdZgddfdededefdZdeded e ffd Z d e d eded e ffd Z d e ded e ffdZ d e ded e ffdZ ggdfde dee dee deddf dZddefdZy)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 Fcommon_constraintslambdifyrcntj|_||_||_||_i|_yrK)r7ManyToOneMatcher_matcher_common_constraint _lambdify_info _wildcards)r\rrrs rGr^zReplacer.__init__s/002 "4! /1rI lambda_strr.c@tdt|tS)Nzfrom sympy import *)execevallocals)r\rs rG _get_lambdazReplacer._get_lambda s "#J))rIconstraint_exprcondition_templatec |jtDcgc]}|j}}dj|}t |}|j |}t j|jd|d|dScc}w)N, lambda z: ()) atomsrerjoinrformatr7CustomConstraintr)r\rrxwilds lambdaargsfullexpr conditions rG_get_custom_constraintzReplacer._get_custom_constraints!0!6!6}!EFAFFYYu% o.&--h7 ''   wzl#i[B CE E GsBc&|j|dS)Nz ({}) != Falserr\rs rG_get_custom_constraint_nonfalsez(Replacer._get_custom_constraint_nonfalses**?OLLrIc&|j|dS)Nz ({}) == Truerrs rG_get_custom_constraint_truez$Replacer._get_custom_constraint_trues**?NKKrINrconditions_trueconditions_nonfalsect|}t|}|jdd}|Dcgc]}|j|}}|Dcgc]}|j|} }|j ||j | t j |g|} |jrNddjd|jtDdt|} |j| } | }nF|jj|jtD cic]} t!| | c} |j"r t%||}|j&j)| |ycc}wcc}wcc} w)Nrrc34K|]}|jywrKr).0rs rG zReplacer.add..+s-Xaff-Xsz: )rrrrextendr7PatternrrrrerrrupdaterVrgrrradd)r\rrrrr constraintscondconstraint_conditions_trueconstraint_conditions_nonfalsepatternr lambda_expris rGrz Replacer.addsc~{+ --a0 ?N&P7;D , ,T 2&P"&PDW*X;?D 0 0 6*X&*X569://$55 >>"499-Xdjj>W-X#Y"ZZ\]ghs]t\uvJ**:6K%K OO " "tzz(7K#L!CFAI#L M ::)+tr?registerrHrUrVrerrrrrrrbrIrGrs  88'(WWW~~~~WWVV"  8$6.%8  " , ccZZIx Is!!!#&Is!!!#&!!!#&!!!#&Is!!!#&!!!#&!!!#&Ix !!!(+Iz"!!!*-IsIsIuIz"Ix Ix IsIrItItIsIsIsIsIsIsItItItItItItItItItItItItIuIuIuIuIuIuWh@ @We%%V__X++V__U$$* ))%0212  %%L)-Hf-*-`L)m* L)}* L)}*  j L)HH*HrI