K iddlmZmZmZmZmZddlmZmZddl m Z ddl m Z m Z mZddlmZddlmZmZmZmZmZmZmZddlmZddlmZmZdd lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)dd l*m+Z+m,Z,m-Z-dd l.m/Z/m0Z0dd l1m2Z2ejfZ4Gd deZ5Gdde Z6ddZ7dZ8dZ9dZ:dZ;dZddddZ?y))SdiffTupleDummyMul)Basicas_Basic)DefinedFunction)Rational NumberSymbol_illegal)global_parameters)LtGtEqNe Relational _canonical_canonical_coeff)ordered)MaxMin) AndBooleandistribute_and_over_orNottruefalseOrITEsimplify_logicto_cnfdistribute_or_over_and)uniqsift common_prefix) filldedent func_name)productcReZdZdZdZedZedZedZdZ dZ y) ExprCondPairz)Represents an expression, condition pair.ct|}|dk(rtj||tS|dk(rtj||tSt |t rE|jtr0t|}t |tr|jt}t |ts ttdt|ztj|||S)NTFzL Second argument must be a Boolean, not `%s`)r r__new__rr isinstancerhas Piecewisepiecewise_foldrewriter r TypeErrorr'r()clsexprconds j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/functions/elementary/piecewise.pyr-zExprCondPair.__new__s~ 4<==dD1 1 U]==dE2 2 e $))<!$'D$ *||C($(J('o(./0 0}}S$--c |jdS)z6 Returns the expression of this pair. rargsselfs r7r5zExprCondPair.expr' yy|r8c |jdS)z5 Returns the condition of this pair. r:r<s r7r6zExprCondPair.cond.r>r8c.|jjSN)r5is_commutativer<s r7rCzExprCondPair.is_commutative5syy'''r8c#DK|j|jywrB)r5r6r<s r7__iter__zExprCondPair.__iter__9siiiis c z|j|jDcgc]}|jdi|c}Scc}w)N)funcr;simplify)r=kwargsas r7_eval_simplifyzExprCondPair._eval_simplify=s3tyyCA:1:://CDDCs8N) __name__ __module__ __qualname____doc__r-propertyr5r6rCrErLrGr8r7r+r+sQ3."  ((Er8r+cPeZdZdZdZdZdZedZdZ dZ dZ d Z d Z d Zd Zd ZdZdZd1dZd1fd Zd2dZd3dZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)Z,d*Z-d+Z.d,Z/ed-Z0d4d.Z1d/Z2d0Z3xZ4S)5r0a Represents a piecewise function. Usage: Piecewise( (expr,cond), (expr,cond), ... ) - Each argument is a 2-tuple defining an expression and condition - The conds are evaluated in turn returning the first that is True. If any of the evaluated conds are not explicitly False, e.g. ``x < 1``, the function is returned in symbolic form. - If the function is evaluated at a place where all conditions are False, nan will be returned. - Pairs where the cond is explicitly False, will be removed and no pair appearing after a True condition will ever be retained. If a single pair with a True condition remains, it will be returned, even when evaluation is False. Examples ======== >>> from sympy import Piecewise, log, piecewise_fold >>> from sympy.abc import x, y >>> f = x**2 >>> g = log(x) >>> p = Piecewise((0, x < -1), (f, x <= 1), (g, True)) >>> p.subs(x,1) 1 >>> p.subs(x,5) log(5) Booleans can contain Piecewise elements: >>> cond = (x < y).subs(x, Piecewise((2, x < 0), (3, True))); cond Piecewise((2, x < 0), (3, True)) < y The folded version of this results in a Piecewise whose expressions are Booleans: >>> folded_cond = piecewise_fold(cond); folded_cond Piecewise((2 < y, x < 0), (3 < y, True)) When a Boolean containing Piecewise (like cond) or a Piecewise with Boolean expressions (like folded_cond) is used as a condition, it is converted to an equivalent :class:`~.ITE` object: >>> Piecewise((1, folded_cond)) Piecewise((1, ITE(x < 0, y > 2, y > 3))) When a condition is an ``ITE``, it will be converted to a simplified Boolean expression: >>> piecewise_fold(_) Piecewise((1, ((x >= 0) | (y > 2)) & ((y > 3) | (x < 0)))) See Also ======== piecewise_fold piecewise_exclusive ITE NTct|dk(r tdg}|D]E}tt|d|}|j}|t ur+|j ||tusEn|jdtj}|r|j|}|1|St|dk(r!|djdk(r|djStj|g|i|S)Nrz(At least one (expr, cond) pair expected.r;evaluater@T)lenr3r+getattrr6rappendrpoprrTevalr5rr-) r4r;optionsnewargsecpairr6rYrs r7r-zPiecewise.__new__s t9>FG G BVR!89D99Du} NN4 t| {{:'8'A'AB '"A} \Q 71:??d#:1:?? "}}S676g66r8c|stSt|dk(r|dddk(r|ddSt|}t|t|k7}tdt ||D}|st t d|s|s||Sy)aEither return a modified version of the args or, if no modifications were made, return None. Modifications that are made here: 1. relationals are made canonical 2. any False conditions are dropped 3. any repeat of a previous condition is ignored 4. any args past one with a true condition are dropped If there are no args left, nan will be returned. If there is a single arg with a True condition, its corresponding expression will be returned. EXAMPLES ======== >>> from sympy import Piecewise >>> from sympy.abc import x >>> cond = -x < -1 >>> args = [(1, cond), (4, cond), (3, False), (2, True), (5, x < 1)] >>> Piecewise(*args, evaluate=False) Piecewise((1, -x < -1), (4, -x < -1), (2, True)) >>> Piecewise(*args) Piecewise((1, x > 1), (2, True)) r@rTc3,K|] \}}||k(ywrBrG).0rKbs r7 z!Piecewise.eval..s:da16:z There are no conditions (or none that are not trivially false) to define an expression.N) UndefinedrU_piecewise_collapse_argumentsallzip ValueErrorr')r4_argsr[missingsames r7rYzPiecewise.evals8  u:?uQx|t38A; /6g,#e*,:c'5&9::Z) ! ! $= r8c ,g}|jD]u\}}|jddrKt|tr|jdi|}||k7r|}t|tr|jdi|}|j ||fw|j |S)z3 Evaluate this piecewise function. deepTrG)r;getr.rdoitrWrH)r=hintsr[ecnewes r7rqzPiecewise.doitsII #DAqyy&a'!166?E?Dt| a'A NNAq6 " #tyy'""r8c t|fi|SrB)piecewise_simplify)r=rJs r7rLzPiecewise._eval_simplifys!$1&11r8c|jD]2\}}|dk(s|j|ddk(s!|j|cSy)NTr)r;subsas_leading_term)r=xlogxcdirrsrts r7_eval_as_leading_termzPiecewise._eval_as_leading_termsBII ,DAqDyAFF1aLD0((++ ,r8c|j|jDcgc]\}}|j|fc}}Scc}}wrB)rHr;adjointr=rsrts r7 _eval_adjointzPiecewise._eval_adjoints4tyy B1AIIK+BCCB= c|j|jDcgc]\}}|j|fc}}Scc}}wrB)rHr; conjugaters r7_eval_conjugatezPiecewise._eval_conjugate4tyy$))D$!QAKKM1-DEEDrc ~|j|jDcgc]\}}t|||fc}}Scc}}wrB)rHr;r)r=r{rsrts r7_eval_derivativezPiecewise._eval_derivatives3tyytyyAtq!DAJ?ABBAs9 c |j|jDcgc]\}}|j||fc}}Scc}}wrB)rHr;_evalf)r=precrsrts r7 _eval_evalfzPiecewise._eval_evalfs6tyy499E41aAHHTNA.EFFEs> c|jsy|jD]Y\}}|j||}|jry||j j vry|sG|j ||cSyrB)is_realr;ry is_Relationalas_setboundary_eval_is_meromorphic)r=r{rKrsrtr6s r7rzPiecewise._eval_is_meromorphicsoyyII 4DAq66!Q>> from sympy import Piecewise >>> from sympy.abc import x >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True)) >>> p.piecewise_integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x, True)) Note that this does not give a continuous function, e.g. at x = 1 the 3rd condition applies and the antiderivative there is 2*x so the value of the antiderivative is 2: >>> anti = _ >>> anti.subs(x, 1) 2 The continuous derivative accounts for the integral *up to* the point of interest, however: >>> p.integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True)) >>> _.subs(x, 1) 1 See Also ======== Piecewise._eval_integral r integrate)sympy.integralsrrHr;)r=r{rJrrsrts r7piecewise_integratezPiecewise.piecewise_integrates@D .tyydiiPdaIa5f5q9PQQPs= c |jt}tt|Dcgc]5}||jvs|t j t jfvr|7c}}|ri}g}tdt|D]-}tt||} d|vrd} nrt| D cgc] } | | s |  c} } t| j} t| dk(rddl m}m} || | d}nt%| }|t jur|} ||j'| }t)||j*r:t|j,dk(r"|j,d\}}t%|| dn| } | |j/|gj1| |j1|0|D]}t3||||<t5|Dcgc] }|||f }}|j1dft7|Sycc}wcc} w#t t"f$r,t%| Dcgc]}||| ddncc}wc}}YFwxYwcc}w) aoReturn either None (if the conditions of self depend only on x) else a Piecewise expression whose expressions (handled by the handler that was passed) are paired with the governing x-independent relationals, e.g. Piecewise((A, a(x) & b(y)), (B, c(x) | c(y)) -> Piecewise( (handler(Piecewise((A, a(x) & True), (B, c(x) | True)), b(y) & c(y)), (handler(Piecewise((A, a(x) & True), (B, c(x) | False)), b(y)), (handler(Piecewise((A, a(x) & False), (B, c(x) | True)), c(y)), (handler(Piecewise((A, a(x) & False), (B, c(x) | False)), True)) )r@r)repeatr@Nr)reduce_inequalities_solve_inequalityT)linear)atomsrlistr free_symbolsrrrr)rUdictrirsympy.solvers.inequalitiesrrrjNotImplementedErrorrxreplacer.rHr; setdefaultrWrr$r0)r=r{handlerrelr^irelr; exprinordertruthrepsr6iandargsfreerrtrKr5econdkrss r7 _handle_irelzPiecewise._handle_irel*sRjj$G,1q/F!&&!''**,-. DK D :' -Ce,-E>D#%AAaa%ABG 4 45D4yA~D3 3GT!W EA MAGG| Dt}}T23dDII.3tyy>Q3F"&))A,KD%udldED #OOD"-44T: &&t,O' -R 'd1g,Q '+/{*;>> from sympy import Piecewise >>> from sympy.abc import x >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True)) >>> p.integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True)) >>> p.piecewise_integrate(x) Piecewise((0, x < 0), (x, x < 1), (2*x, True)) See Also ======== Piecewise.piecewise_integrate rrct|jr|jfddiS|jfiS)N_firstF)r.rH_eval_integralr)ipwrJr=r{s r7rz)Piecewise._eval_integral..handlersDc499--3--aHHHH(3==5f55r8NIntegralr`r@c3.K|] \}}}|dk(yw)r`NrG)rbrKrcrs r7rdz+Piecewise._eval_integral..s-9Aq!qBw-sTF)sympy.integrals.integralsrr _intervalsrrInfinity enumeraterUreversed_clipanyrWrfry_eval_intervalr/r r;r6r0)r=r{rrJrrirvokabeirrKrc_piecesoodonerpjrNr;sumantirsr6s`` ` r7rzPiecewise._eval_integralms. 8  6 ##Aw/C ??1%D :D!$ $+/00ZQ1a1a&00 ZZb" f% 9DAqbS"I~$-dO*LAy1aBw"#Q'Q*D A A )(4. 9 9 9Aq!7AA%*1q!fa%8DAEN 9 9*.88gaAaAq 88 -- - KK"b)R0 1 %GAq!T!WR[!6v6D{hhq!n''1a0377H%)9C1HCAJJ472;',,11!Q75@AQ KKd $' %($[19sH?I$Ic 8 ||t |||Stt|||f\|rԈfd}j |}||Skt j us$t just jurTjd}t|tr*t|jD cgc] \}} | | f c} }}|S| }|Skt jus$t just jurntd} td} jrn| }jrn| }j||d} || k(r1|| k(r,| j| | i | j| | i} } n+jd | j| | i} } tdd }j rU| j|z ij|z i} | j|zij|z i} n`j rT| j|zij|z i} | j|z ij|z i} d }|| } || } || k(rt| kf| df}nt| kf| df}|t"k(r t%d t'd | | fDr t)|}|Sj+\}}|s!d dlm}|j1fS|Dcgc] \}}}}||f }}}}dfg}t j}t3|D]\}}|dd| |fk(r't3|D]\}\}}}|dk(s|||f||<nPt5|dz }t3t7|D])\}\}}}|dk(s||z }t9|||f||||dz+|Dcgc]\}}}||k7s |||f}}}}t j:}d}|D]L\}}}|dk(r#|t"cSt||kft"dfcS|||dj||z }|}N|Scc} }wcc}}}wcc}}}w)z?Evaluates the function along the sym in a given interval [a, b]Nct|jr|jdS|jS)Nr)r.rHr)rhilor=r{s r7rz)Piecewise._eval_interval..handlers@c499---aR-EE--aR88r8FrrrT)positivec*|jddS)Nc.t|ttfSrB)r.rrr{s r7z...sjS#J7r8c4|j|jSrBrHr;rs r7rz...sfaffaffor8)replacers r7rz*Piecewise._eval_interval..s!))7-#/r8z(Can't integrate across undefined region.c3<K|]}t|tywrB)r.r0)rbrs r7rdz+Piecewise._eval_interval..sDAz!Y/Dsrrr`r@r)superrmapr rrrrNegativeInfinityr.r0r;rr is_comparabler is_Symbolrfrjrr1rrrrrrUrrZero)!r=symrKrcrrrrvrsrt_a_bposnegrtouchrrrrrrrrrrrruptorrr{ __class__s!` @@@r7rzPiecewise._eval_intervals! 9 7)#q!4 4HsAqk2IAr2  9 ##Aw/C RAGG#!**$a.@.@(@((B5(Ab),"$AAqb!W$ABB B RAFF"!**$a.@.@(@4[4[**B**B))!Q%)@7qBw"%r2r2.>!? ? b"b"%56C "&!4!4QBu!4!M M b"b"%56C"t,<<,,BF|4==q"r'lKC,,BF|4==q"r'lKC\\,,BF|4==q"r'lKC,,BF|4==q"r'lKC/CjCj7"G% "#B #G% "#B ?$%OPPD#sDD'+B ??1%D :DIIaL1b"+6 6+/00ZQ1a1a&00R ~ ZZf% 9DAq!u"b !$-dO*LAy1aBw"#Q'Q*D A A )(4. 9 9 9Aq!7AA%*1q!fa%8DAEN 9 9*.88gaAaAq 88ff GAq!Bw<$$ #rTz!2Y4EFF 472;--aA6 6CD  I%BP19sR R RRc ddlmt|tsJfdfd}t |j }|j t}i}|D]}||\}} |dk7rd|fcS| ||<|j D cgc]} | j|}} g} dx} } t|D]\} \}}|tjur|tjur|} | } nt|tr |r dd|zfcSQt|}t|tr t!|}t|t"r=| j%|j Dcgc]}t|ts| ||fc}|tjur| j'| ||f|tjus|} | } ng}| D]\}}}t|trtj(}tj*}g}|j D]}t|tsy t|tr |}|ry nt|t,rJ|j \}}|k(r|j'|n#|k(r|j'|n |ccS|j.k(rt1|j2|}|j2k(rt5|j.|}|ccS|rat t7|}g}t|D]O\} }||cxkrdk(sn||cxkDrdk(rnn"|s|j'd|f|j'|d d |fQ|j'|d d |f|j9d| j%|D cgc]} ||t| dk| d kfc} t|trq|j:d k7rb|j.|j2}}|j.k(rtj(}n3|j2k(rtj*}n|cSdd|zfcSt5||}|r||k(ry||k\tjus|j'||||f| 1|j'tj(tj*| | fdt t=|fScc} wcc}wcc} w)a_Return a bool and a message (when bool is False), else a list of unique tuples, (a, b, e, i), where a and b are the lower and upper bounds in which the expression e of argument i in self is defined and $a < b$ (when involving numbers) or $a \le b$ when involving symbols. If there are any relationals not involving sym, or any relational cannot be solved for sym, the bool will be False a message be given as the second return value. The calling routine should have removed such relationals before calling this routine. The evaluated conditions will be returned as ranges. Discontinuous ranges will be returned separately with identical expressions. The first condition that evaluates to True will be returned as the last tuple with a, b = -oo, oo. r)rc*dtdd|fS)NFz; A condition not involving z appeared: )r')r6rs r7 nonsymfailz(Piecewise._intervals..nonsymfail^s!*&)4&122 2r8c|jvr|S |}t|tr|jdj}|jdk7s|vr dd|ddfS|j dk(rt j}d |fS|j dk(r+ t|jk|jkD}d |fSd |fS|t jkt jkzk(rt j}d |fS#t$rdd|ddfcYSwxYw#t$rt j}Yd |fSwxYw) NFzUnable to solve relational z for .r@rz==!=T)rrr.rr;rel_oprrrrhsr3rrr)r^rrrrrs r7_solve_relationalz/Piecewise._intervals.._solve_relationalcsQ!..(!!}$ Q&q#."j)wwqz..771:$t  QPS"TTT99$ B8OYY$&$bff cBFFl; 8O48O**S0S1::5EFFVV8O+' QCPPP Q %$VV8O $s# D &D% D"!D"%EETFNzencountered Eq condition: %s)Fzexpecting only Relationals)Fz"encountered secondary Eq conditionr`r@rzunrecognized condition: %s)Fzencountered Eq condition)rrr.r0rr;rrrrrrrrr"rr#rextendrWrrrltsrgtsrrrXrr$)r=r err_on_Eqrr;keysrr^rsr expr_conddefaultidefaultr5r6oint_expriarglowerupperexcludecond2lnewcondrsrrs ` @@r7rzPiecewise._intervalsHs$ A$ *** 2  8DIIzz*% A%a(EBTzby DG  +/))4Q 4 44 !!(( OA|dqwwqvv~$# "@4"GGG$s4#>#>$r8c$|jdS)N is_complexrr<s r7rzPiecewise.D$?$? $Mr8c$|jdS)Nis_evenrr<s r7rzPiecewise.!sd&A&A'r8c$|jdS)N is_integerrr<s r7rzPiecewise.r!r8c$|jdS)N is_irrationalrr<s r7rzPiecewise.st'B'B(r8c$|jdS)N is_negativerr<s r7rzPiecewise.!T%@%@%Or8c$|jdS)Nis_nonnegativerr<s r7rzPiecewise."(C(C)r8c$|jdS)Nis_nonpositiverr<s r7rzPiecewise.$r0r8c$|jdS)N is_nonzerorr<s r7rzPiecewise.&sD$?$?%r8c$|jdS)Nis_oddrr<s r7rzPiecewise.(s ; ;H Er8c$|jdS)Nis_polarrr<s r7rzPiecewise.)s$"="=j"Ir8c$|jdS)N is_positiverr<s r7rzPiecewise.*r-r8c$|jdS)Nis_extended_realrr<s r7rzPiecewise.+s$*E*E + r8c$|jdS)Nis_extended_positiverr<s r7rzPiecewise.-d.I.I "/$r8c$|jdS)Nis_extended_negativerr<s r7rzPiecewise./r?r8c$|jdS)Nis_extended_nonzerorr<s r7rzPiecewise.1sT-H-H !.#r8c$|jdS)Nis_extended_nonpositiverr<s r7rzPiecewise.31L1L %2'r8c$|jdS)Nis_extended_nonnegativerr<s r7rzPiecewise.5rFr8c$|jdS)Nrrr<s r7rzPiecewise.7r$r8c$|jdS)Nis_zerorr<s r7rzPiecewise.8s!>> from sympy import Piecewise, Interval >>> from sympy.abc import x >>> p = Piecewise( ... (1, x < 2), ... (2,(x > 0) & (x < 4)), ... (3, True)) >>> p.as_expr_set_pairs() [(1, Interval.open(-oo, 2)), (2, Interval.Ropen(2, 4)), (3, Interval(4, oo))] >>> p.as_expr_set_pairs(Interval(0, 3)) [(1, Interval.Ropen(0, 2)), (2, Interval(2, 3))] r@z= multivariate conditions are not handled.z Inequalities in the complex domain are not supported. Try the real domain by setting domain=S.Reals)rReals is_subsetsetr;rrUrr'rrr.rrrj intersectrEmptySetrW) r=domainexp_setsUcomplex cond_freer5r6rcond_ints r7as_expr_set_pairszPiecewise.as_expr_set_pairsHs2 >WWF &&qww//E )) 2JD$ ** *I9~!)*6@+ABBJ/8A%a"b2(56*7888 {{4;;=1HH A1::%x 01 2 r8c ji}t|}td|D}t|D]\}\}}t|ts%|dk7r t t dt|z|dk(rnt|tr |jn|gD]}|j}|j} |j| tjj|j!|| <|| tj$tj&fvst||||<dx}||d<n|dk(sndk7rt)t d|\} } t+|d|D]\} }t-|| | } t/| S#t"$r|st#t d|zYwxYw)Nc3,K|] \}}|dk(yw)TNrG)rbrcrts r7rdz1Piecewise._eval_rewrite_as_ITE..|s1DAqa4i1reTzP Expecting Boolean or bool but got `%s` a A method to determine whether a multivariate conditional is consistent with a complete coverage of all variables has not been implemented so the rewrite is being stopped after encountering `%s`. This error would not occur if a default expression like `(foo, True)` were given. r@z Conditions must cover all reals or a final default condition `(foo, True)` must be given. )rrrr.rr3r'r(rr;rrXrrrTunionrr UniversalSetrPrjrr r) r=r;rJbyfreerrrcrtrr{lastrrKs r7_eval_rewrite_as_ITEzPiecewise._eval_rewrite_as_ITEysDz1D11"4 IAv1a)a4i ,#A,,'!())Dy)!R0QVVqc ~~HHJ & & 1 11::!'',uQXXZ'81I!9 99"47mDG%))AQ ) *Dy= > 9Z) q'aT"1X& #DAqq!T?D #$3+ &"1*> #$>$3%&&# &s0AF  "F21F2c  ddlm tddgtddgtddgt ddgt ddgi Gddt fd g}d}|D]?\}}t| vr|j||f'|tjur|<|}?y|+|}|dddD]\}}  |}||zd |z |zz}|Sy# $rYywxYw) Nr)KroneckerDeltaFTc eZdZy)HPiecewise._eval_rewrite_as_KroneckerDelta..UnrecognizedConditionN)rMrNrOrGr8r7UnrecognizedConditionrfs r8rgc |t|tr|jSt|trd|jz St ||j}}| vr| |\}}|rt |Dcgc] }d |z c}nt |Dcgc] } | c}}|rd|z S|Scc}wcc}w)Nr@)r.rr;rtyper) r6r4r;b1b2rtrrdrgr2ruless r7r2z:Piecewise._eval_rewrite_as_KroneckerDelta..rewrites$#%tyy11$#>499555T DIIC%+C003ZFB8:d3a'!*n34Z^E_UVgajE_@`A1u H 4E_s 9B4B9r`r@) (sympy.functions.special.tensor_functionsrdrrrrr ExceptionrirWrr) r=r;rJ conditions true_valuevaluer6resultrrdrgr2rls @@@@r7_eval_rewrite_as_KroneckerDeltaz)Piecewise._eval_rewrite_as_KroneckerDeltasK % t $ t t   I  "   KE4DzU"!!5$-0%!&J   !F)$B$/  t AY!a%6)99F M "-s'CC  C T)F)rrB)5rMrNrOrPnargs is_Piecewiser- classmethodrYrqrLr~rrrrrrrrrrr rrrr_eval_is_finite_eval_is_complex _eval_is_even_eval_is_imaginary_eval_is_integer_eval_is_irrational_eval_is_negative_eval_is_nonnegative_eval_is_nonpositive_eval_is_nonzero _eval_is_odd_eval_is_polar_eval_is_positive_eval_is_extended_real_eval_is_extended_positive_eval_is_extended_negative_eval_is_extended_nonzero_eval_is_extended_nonpositive_eval_is_extended_nonnegative _eval_is_real _eval_is_zero_Piecewise__eval_condr[rbrs __classcell__)rs@r7r0r0AsO<| EL72/!/!b# 2, DFCG4&#RJA$FW r@Dd*L = (F OMGMMOELINO "$"$!#%'!%'!GMM  /b+ Z4r8r0Tc F t|tr|jts|Sg}t|ttfr|j D]\}}t|ts t |}|jtrJt|tr|j}t|d}t|trD|j|j Dcgc]\}}t |t||fc}}|j||fny|js|jr|jrt!|j dd\}}t!|d} t#t%| D]}}t'| |dkDrT| |D cgc]} t#| j } } t)| D cgc]} | D cgc]} | j*c} c} } } t'| }g}t-|D]@} |j|j.| Dcgc]}|| j0c}| | fBg}| D]_}|t'|k(r||j*dk(r|j||j0C|jt||dd d ia|r |j|j.|df|jt|d d ij|j| |n |j }t#t3t |}t5|D cgc]'} t| tr | j n| t6fg)c} D]3}t9|\}}|j|j.|t|f5|Tt#t;t;|Dcic]\}}|| c}}j=Dcgc] \}}||f c}}}t|d |i}|Mt'|j dk(r5|j d j*dk(r|j d j0St?d |jAtDr t |S|Scc}}wcc} wcc} wcc} } wcc}wcc} wcc}}wcc}}w) a Takes an expression containing a piecewise function and returns the expression in piecewise form. In addition, any ITE conditions are rewritten in negation normal form and simplified. The final Piecewise is evaluated (default) but if the raw form is desired, send ``evaluate=False``; if trivial evaluation is desired, send ``evaluate=None`` and duplicate conditions and processing of True and False will be handled. Examples ======== >>> from sympy import Piecewise, piecewise_fold, S >>> from sympy.abc import x >>> p = Piecewise((x, x < 1), (1, S(1) <= x)) >>> piecewise_fold(x*p) Piecewise((x**2, x < 1), (x, True)) See Also ======== Piecewise piecewise_exclusive cnf)formc|jSrB)rvrs r7rz piecewise_fold..s r8Tbinaryc^t|jDcgc]\}}| c}}Scc}}wrB)tupler;)r{rsrts r7rz piecewise_fold..s!5qvv)>!!)>#?)>s ) r@NrTFrc3|K|]4}|jD]#}|jjt%6ywrB)r;r5r/r0)rbrrs r7rdz!piecewise_fold..Gs. NQqvv N!166::i N Ns:<)!r.rr/r0r+r;r1r to_nnfr!rrrWis_Addis_MulrCr%rrrUr&r6rangerHr5rr)rriritemsrr)r5rTnew_argsrsrteicirr;pcrpargsrcomr  collectedairemainsrKfoldedr\rs r7r1r1s'4 dE "$((9*= H$y12II (DAqa+"1%uuY' ''!S!HHJ"151!Y'"#&&!*B#1"4c"aj!A!*+A' (: ;;$++$*=*=499&>tLGAta?@B'"+& #r!u:>35a59aT!&&\9E9'5:*<01+A+*<=CCA "I"1X%!((%DIIU'Cr1 'CDF*$%%!G"CA;$Q499,#NN1Q4995#NN )1QR5 A5 AC C!(()$))W*=t)DEKK 9 Eu EF BqE"3 #699Dc.$/0'-/"#&a3d)/0 6B8DAq OOYTYY]CG4 5  6 %h/61QAqD6116":TQ1a&":;< H 0x 0BCLA-"''!*//T2Iwwqz N"((9*= NNb!! IE!*.:,*< (D(/61":s<!Q7 Q= R R R R /,R/ RRR cv|\}}|\}}tt|||tt|||}}t||}g}||k7r|j||dfn ||k7r|j|||fn ||k7r8||k(r|r|dddk(r|dd|df|d<|S|j||df|S |S)aReturn interval B as intervals that are covered by A (keyed to k) and all other intervals of B not covered by A keyed to -1. The reference point of each interval is the rhs; if the lhs is greater than the rhs then an interval of zero width interval will result, e.g. (4, 1) is treated like (1, 1). Examples ======== >>> from sympy.functions.elementary.piecewise import _clip >>> from sympy import Tuple >>> A = Tuple(1, 3) >>> B = Tuple(2, 4) >>> _clip(A, B, 0) [(2, 3, 0), (3, 4, -1)] Interpretation: interval portion (2, 3) of interval (2, 4) is covered by interval (1, 3) and is keyed to 0 as requested; interval (3, 4) was not covered by (1, 3) and is keyed to -1. r`r)rrrW)ABrrKrcrtdrs r7rrLs, DAq DAq s1ay! c#a)Q/qA Aq A AAv !Q Av !Q Av 6aAbE"IObE!HaOAbE H HHaBZ  H Hr8c ddlm}|jdjj}d}t |dk(rd|j tsN|j}|j|d\}}d}|r"g}tj} ddl m } |D]\} } } }|j|j}|||| }|||| }| | | | | }|| z }|sK |j} |||| }|r|r| j r| j rtj"}n| j r| | vr|| kDn|| k\}n| j r|| k}n| | vrt%| |k|| k}nt%| |k|| k}n|r<| j r|| k}nm| | vrt%| |k|| k}nVt%| |k|| k}nC|r%| j r|| kD}n/t%| |k|| k}n| | vr|| k}nt%| |k|| k}| |z} | tj&ur|r| tj&hz} | tj(ur|r| tj(hz} |j+| |ftj,j/| s|j+t0df|Vt3|j}t5t |D]*}||\} }t7|t8r ||fi|}| |f||<,|jdd}t5t |D]3}||\} }t7| t8r|| fdd i|}|| k7r|} | |f||<5|||d<t;|S#t$rYuwxYw) Nr)rIr@T)rcL |j||dk(S#t$rYywxYw)z/return True if c.subs(x, a) is True, else FalseTF)ryr3)rtr{rKs r7includez-piecewise_simplify_arguments..includes. vva|t++  s  ##)IntervalrqF)sympy.simplify.simplifyrIr;r6rrUrrrXrrrTsympy.sets.setsrinfr is_infiniterrrrrWrPrQrfrrr.rr0)r5rJrIf1r;r{rabe_rcoveredrrKrcrsrrtincl_aincl_bivcsetrqrus r7piecewise_simplify_argumentsrzs0 1   ' 'B D 2w!|DJJrN FFH??1?5D  DjjG 0"/ $ 1aIIaL%% Aq) Aq)aJF ;G|.A%Q1-Ff}}FF'(G|QU!q&!VgAqAv.QQ/}}UgAq1u-QA.}}UAqAv.G|UAq1u-2 ***v 2 233G ?v |+G QF#_/ $`77$$W- Y-. |DIIs4y! AGDAq!U#Q)&)!fDG   ::fd #D 3t9 Q1 a A4E4V4Dqya&Q v d A+s# M"" M/.M/cg}t}|D]/\}}|jdt}t|trYg}t |j D]?\}\}}||vr||k(r|dk7r|rt |||fgz}n|}n|j||fAd} |gt|trt|j ngzD] }||vsd} n| rt|trg} |j D]d}t|trA|jj|vr,t|ttfr|j|vrd}n[| j|f|j | }n8t|tr(|jj|vrt"j$}|j'||rq|dj(|k(rKt+||dj,} t| tt*fr t/| } t1|| |d<|dj,|k(r|jt1||2|S)Nc|jSrB)rrs r7rz/_piecewise_collapse_arguments..s aoor8TFr`)rRrrr.r0rr;rWrrrnegated canonicalrrweakrHrraddr5rr6rr+) rkr[ current_condr5r6 unmatchingrrsrtgot nonredundantorconds r7rgrgsMG5LW1 d|| %'79 dI &J&tyy1 . 6Aq $9Dy &#, *q!fX 5$8D$%D%%q!f-# .(&&0s&;4 ? AL      dC LYY 0a,yy**l: "!b"X.,.$##A& 0!tyy,/ j )||%%5vv r{4'D'"+"2"23fsBi03F;F*48 !!T)|D$/0oW1p Nr8ct|jddxr9t|jddxs t|jtt fS)N _diff_wrtF)rVrMrr.r r )rss r7rr;sCWQUUK71 AEE;%0quux./r8c t|fi|}t|ts|St|j}t |}t |}t|SrB)rr.r0rr;_piecewise_simplify_eq_and)_piecewise_simplify_equal_to_next_segment)r5rJr;s r7rwrw@sL ' 7 7D dI &  ?D %d +D 4T :D d r8c"d}ttt|D]\}\}}|t|trt |j dd\}}nt|tr|gg}}ngx}}|}|}|ri|sgtt|}|D]N} t|dk(s t| s|j| j }|j| j }P||k(r"||j||dzd|||<|}|}|S)z See if expressions valid for an Equal expression happens to evaluate to the same function as in the next piecewise segment, see: https://github.com/sympy/sympy/issues/8458 Nc"t|tSrBr.rrs r7rz;_piecewise_simplify_equal_to_next_segment..Vs Jq",=r8Trrr@r) rrrr.rr%r;rrrU_blessedryrH) r;prevexprrr5r6eqsother _prevexpr_exprrss r7rrKs! H#D4$9: Kr8cnt|D]\}\}}t|trt|jdd\}}nt|t r|gg}}ngx}}|sTt t|}t|D]\}}t|s|j|j}||dzdDcgc]}|j|jc}||dzd|Dcgc]}|j|j}}t||z}||j||||<|Scc}wcc}w)z Try to simplify conditions and the expression for equalities that are part of the condition, e.g. Piecewise((n, And(Eq(n,0), Eq(n + m, 0))), (1, True)) -> Piecewise((0, And(Eq(n, 0), Eq(m, 0))), (1, True)) c"t|tSrBrrs r7rz,_piecewise_simplify_eq_and..~s 1b(9r8Trr@N) rr.rr%r;rrrrryrH) r;rr5r6rrrrsrs r7rrts4%T?/A!a%&k"J7277AFF#3"JCAK8=>"WRWWaff->E>  ?u&D1gll4.DG%/& K #K>s 4 D-! D2F)skip_nanrocfd}|r|jt|St|tr||jS|S)ac Rewrite :class:`Piecewise` with mutually exclusive conditions. Explanation =========== SymPy represents the conditions of a :class:`Piecewise` in an "if-elif"-fashion, allowing more than one condition to be simultaneously True. The interpretation is that the first condition that is True is the case that holds. While this is a useful representation computationally it is not how a piecewise formula is typically shown in a mathematical text. The :func:`piecewise_exclusive` function can be used to rewrite any :class:`Piecewise` with more typical mutually exclusive conditions. Note that further manipulation of the resulting :class:`Piecewise`, e.g. simplifying it, will most likely make it non-exclusive. Hence, this is primarily a function to be used in conjunction with printing the Piecewise or if one would like to reorder the expression-condition pairs. If it is not possible to determine that all possibilities are covered by the different cases of the :class:`Piecewise` then a final :class:`~sympy.core.numbers.NaN` case will be included explicitly. This can be prevented by passing ``skip_nan=True``. Examples ======== >>> from sympy import piecewise_exclusive, Symbol, Piecewise, S >>> x = Symbol('x', real=True) >>> p = Piecewise((0, x < 0), (S.Half, x <= 0), (1, True)) >>> piecewise_exclusive(p) Piecewise((0, x < 0), (1/2, Eq(x, 0)), (1, x > 0)) >>> piecewise_exclusive(Piecewise((2, x > 1))) Piecewise((2, x > 1), (nan, x <= 1)) >>> piecewise_exclusive(Piecewise((2, x > 1)), skip_nan=True) Piecewise((2, x > 1)) Parameters ========== expr: a SymPy expression. Any :class:`Piecewise` in the expression will be rewritten. skip_nan: ``bool`` (default ``False``) If ``skip_nan`` is set to ``True`` then a final :class:`~sympy.core.numbers.NaN` case will not be included. deep: ``bool`` (default ``True``) If ``deep`` is ``True`` then :func:`piecewise_exclusive` will rewrite any :class:`Piecewise` subexpressions in ``expr`` rather than just rewriting ``expr`` itself. Returns ======= An expression equivalent to ``expr`` but where all :class:`Piecewise` have been rewritten with mutually exclusive conditions. See Also ======== Piecewise piecewise_fold ct}g}|ddD]U\}}t|t|j}t ||j}|j ||fW|d\}}t|t|j}|j ||f sPt ||j}|t ur.|j tt|jft|ddiS)Nr`rTF) rrrrIrrWrrfr0) pwargscumcondr[expr_icond_icancondexpr_ncond_n cancond_nrs r7make_exclusivez+piecewise_exclusive..make_exclusives%Sbk .NFF&#g,/88:G)224G NNFG, - .  G -668  *+)224Gd" 3w<+@+@+BCD'2E22r8)rr0r.r;)r5rrors ` r7piecewise_exclusivers=@3. ||I~66 D) $tyy)) r8Nrt)@ sympy.corerrrrrsympy.core.basicrr sympy.core.functionr sympy.core.numbersr r r sympy.core.parametersrsympy.core.relationalrrrrrrrsympy.core.sortingr(sympy.functions.elementary.miscellaneousrrsympy.logic.boolalgrrrrrrrr r!r"r#sympy.utilities.iterablesr$r%r&sympy.utilities.miscr'r( itertoolsr)NaNrfr+r0r1rrrgrrwrrrrGr8r7rs11,/??3"""&=JJJJ??6 EE +E5+E\YYxl^+ \`F[| 1 &R:+0d\r8