K i pzddlmZmZmZmZddlmZddlmZddl m Z m Z m Z ddl mZmZmZmZddlmZddlmZmZddlmZdd lmZdd lmZdd lmZGd d eZeeedZ eeedZ eeedZ!eeedZ!eeedZ!eeedZ!eZ"y))AddMulPowS)Basic)Expr) _sympifyitoozoo)is_leis_ltis_geis_gt)_sympify)MinMax)And)dispatch)Order) FiniteSetceZdZdZdZdZdefdZdZdZ e dZ e d Z e d Z e d Zed ed Zed edZeZdZed edZed edZed edZeZed edZed edZed edZed edZdZdZdZdZ y)AccumulationBoundsaAn accumulation bounds. # Note AccumulationBounds has an alias: AccumBounds AccumulationBounds represent an interval `[a, b]`, which is always closed at the ends. Here `a` and `b` can be any value from extended real numbers. The intended meaning of AccummulationBounds is to give an approximate location of the accumulation points of a real function at a limit point. Let `a` and `b` be reals such that `a \le b`. `\left\langle a, b\right\rangle = \{x \in \mathbb{R} \mid a \le x \le b\}` `\left\langle -\infty, b\right\rangle = \{x \in \mathbb{R} \mid x \le b\} \cup \{-\infty, \infty\}` `\left\langle a, \infty \right\rangle = \{x \in \mathbb{R} \mid a \le x\} \cup \{-\infty, \infty\}` `\left\langle -\infty, \infty \right\rangle = \mathbb{R} \cup \{-\infty, \infty\}` ``oo`` and ``-oo`` are added to the second and third definition respectively, since if either ``-oo`` or ``oo`` is an argument, then the other one should be included (though not as an end point). This is forced, since we have, for example, ``1/AccumBounds(0, 1) = AccumBounds(1, oo)``, and the limit at `0` is not one-sided. As `x` tends to `0-`, then `1/x \rightarrow -\infty`, so `-\infty` should be interpreted as belonging to ``AccumBounds(1, oo)`` though it need not appear explicitly. In many cases it suffices to know that the limit set is bounded. However, in some other cases more exact information could be useful. For example, all accumulation values of `\cos(x) + 1` are non-negative. (``AccumBounds(-1, 1) + 1 = AccumBounds(0, 2)``) A AccumulationBounds object is defined to be real AccumulationBounds, if its end points are finite reals. Let `X`, `Y` be real AccumulationBounds, then their sum, difference, product are defined to be the following sets: `X + Y = \{ x+y \mid x \in X \cap y \in Y\}` `X - Y = \{ x-y \mid x \in X \cap y \in Y\}` `X \times Y = \{ x \times y \mid x \in X \cap y \in Y\}` When an AccumBounds is raised to a negative power, if 0 is contained between the bounds then an infinite range is returned, otherwise if an endpoint is 0 then a semi-infinite range with consistent sign will be returned. AccumBounds in expressions behave a lot like Intervals but the semantics are not necessarily the same. Division (or exponentiation to a negative integer power) could be handled with *intervals* by returning a union of the results obtained after splitting the bounds between negatives and positives, but that is not done with AccumBounds. In addition, bounds are assumed to be independent of each other; if the same bound is used in more than one place in an expression, the result may not be the supremum or infimum of the expression (see below). Finally, when a boundary is ``1``, exponentiation to the power of ``oo`` yields ``oo``, neither ``1`` nor ``nan``. Examples ======== >>> from sympy import AccumBounds, sin, exp, log, pi, E, S, oo >>> from sympy.abc import x >>> AccumBounds(0, 1) + AccumBounds(1, 2) AccumBounds(1, 3) >>> AccumBounds(0, 1) - AccumBounds(0, 2) AccumBounds(-2, 1) >>> AccumBounds(-2, 3)*AccumBounds(-1, 1) AccumBounds(-3, 3) >>> AccumBounds(1, 2)*AccumBounds(3, 5) AccumBounds(3, 10) The exponentiation of AccumulationBounds is defined as follows: If 0 does not belong to `X` or `n > 0` then `X^n = \{ x^n \mid x \in X\}` >>> AccumBounds(1, 4)**(S(1)/2) AccumBounds(1, 2) otherwise, an infinite or semi-infinite result is obtained: >>> 1/AccumBounds(-1, 1) AccumBounds(-oo, oo) >>> 1/AccumBounds(0, 2) AccumBounds(1/2, oo) >>> 1/AccumBounds(-oo, 0) AccumBounds(-oo, 0) A boundary of 1 will always generate all nonnegatives: >>> AccumBounds(1, 2)**oo AccumBounds(0, oo) >>> AccumBounds(0, 1)**oo AccumBounds(0, oo) If the exponent is itself an AccumulationBounds or is not an integer then unevaluated results will be returned unless the base values are positive: >>> AccumBounds(2, 3)**AccumBounds(-1, 2) AccumBounds(1/3, 9) >>> AccumBounds(-2, 3)**AccumBounds(-1, 2) AccumBounds(-2, 3)**AccumBounds(-1, 2) >>> AccumBounds(-2, -1)**(S(1)/2) sqrt(AccumBounds(-2, -1)) Note: `\left\langle a, b\right\rangle^2` is not same as `\left\langle a, b\right\rangle \times \left\langle a, b\right\rangle` >>> AccumBounds(-1, 1)**2 AccumBounds(0, 1) >>> AccumBounds(1, 3) < 4 True >>> AccumBounds(1, 3) < -1 False Some elementary functions can also take AccumulationBounds as input. A function `f` evaluated for some real AccumulationBounds `\left\langle a, b \right\rangle` is defined as `f(\left\langle a, b\right\rangle) = \{ f(x) \mid a \le x \le b \}` >>> sin(AccumBounds(pi/6, pi/3)) AccumBounds(1/2, sqrt(3)/2) >>> exp(AccumBounds(0, 1)) AccumBounds(1, E) >>> log(AccumBounds(1, E)) AccumBounds(0, 1) Some symbol in an expression can be substituted for a AccumulationBounds object. But it does not necessarily evaluate the AccumulationBounds for that expression. The same expression can be evaluated to different values depending upon the form it is used for substitution since each instance of an AccumulationBounds is considered independent. For example: >>> (x**2 + 2*x + 1).subs(x, AccumBounds(-1, 1)) AccumBounds(-1, 4) >>> ((x + 1)**2).subs(x, AccumBounds(-1, 1)) AccumBounds(0, 4) References ========== .. [1] https://en.wikipedia.org/wiki/Interval_arithmetic .. [2] https://fab.cba.mit.edu/classes/S62.12/docs/Hickey_interval.pdf Notes ===== Do not use ``AccumulationBounds`` for floating point interval arithmetic calculations, use ``mpmath.iv`` instead. TFreturnc\t|}t|}|jr |js td||k(r|S|jr.|jr"|jxr|jxr||k}n||z j }|r tdt j|||S)Nz*Only real AccumulationBounds are supportedz.Lower limit should be smaller than upper limit)ris_extended_real ValueError is_number is_comparableis_extended_negativer__new__)clsminmaxbads g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/calculus/accumulationbounds.pyr zAccumulationBounds.__new__ssmsm##3+?+?IJ J #:J ==S]]##G(9(9GcCiC922C @B B}}S#s++g&@c`|jjr|jjryyy)NT)r"is_realr#selfs r% _eval_is_realz AccumulationBounds._eval_is_reals& 88   0 0!1 r&c |jdS)z Returns the minimum possible value attained by AccumulationBounds object. Examples ======== >>> from sympy import AccumBounds >>> AccumBounds(1, 3).min 1 rargsr)s r%r"zAccumulationBounds.minyy|r&c |jdS)z Returns the maximum possible value attained by AccumulationBounds object. Examples ======== >>> from sympy import AccumBounds >>> AccumBounds(1, 3).max 3 r-r)s r%r#zAccumulationBounds.maxr/r&c4|j|jz S)a7 Returns the difference of maximum possible value attained by AccumulationBounds object and minimum possible value attained by AccumulationBounds object. Examples ======== >>> from sympy import AccumBounds >>> AccumBounds(1, 3).delta 2 )r#r"r)s r%deltazAccumulationBounds.deltasxx$((""r&c:|j|jzdz S)a/ Returns the mean of maximum possible value attained by AccumulationBounds object and minimum possible value attained by AccumulationBounds object. Examples ======== >>> from sympy import AccumBounds >>> AccumBounds(1, 3).mid 2 )r"r#r)s r%midzAccumulationBounds.mid s488#q((r&otherc$|j|SN)__pow__r*r7s r% _eval_powerzAccumulationBounds._eval_powers||E""r&ct|trt|trHtt|j|jt|j |j S|t jur|jt jus.|t jur1|j t jurtt tS|jr|jt jur1|j t jurtt tS|jt jurtt |j |zS|j t jurt|j|ztStt|j|t|j |St||dStSNFevaluate) isinstancer AccumBoundsrr"r#rInfinityNegativeInfinityr rNotImplementedr;s r%__add__zAccumulationBounds.__add__sS eT "%-"%)),%)),.. "txx13E3E'EQ///DHH 4J"B3++''88q111dhh!**6L&sB//XX!3!33&sDHHu,<==XX+&txx%'7<<&s488U';S5=QRRtUU3 3r&cFt|j |j Sr9)rBr#r"r)s r%__neg__zAccumulationBounds.__neg__7sDHH9txxi00r&ct|trt|trJtt|j|j t|j |j S|t jur|jt jus.|t jur1|j t jurtt tS|jr|jt jur1|j t jurtt tS|jt jurtt |j |z S|j t jurt|j|z tStt|j| t|j | St|| dStSr>) rArrBrr"r#rrDrCr rrEr;s r%__sub__zAccumulationBounds.__sub__:se eT "%-"599*-599*-//***txx1;M;M/MQZZ'DHH ,B"B3++''88q111dhh!**6L&sB//XX!3!33&sDHHu,<==XX+&txx%'7<<&DHHuf-DHHuf-//teVe4 4r&c(|j|zSr9)rHr;s r%__rsub__zAccumulationBounds.__rsub__Rs||~%%r&c.|jt tfk(r|St|trct|trt|jt tfk(r|St }|jD]'}||z}|j |jxs|f)t t|t|S|tjurM|jjrt dtS|jjrt t dS|tjurM|jjrt t dS|jjrt dtS|jr|jri|jtjurt dtS|jtjurt t dStj S|j"r4t t%|j|t%|j|S|j&r4t t%|j|t%|j|St|t(r|St%||dSt*S)NrFr?)r.r rArrBsetupdaterrrrCr"is_zeror#rDrZerois_extended_positiverrrrE)r*r7vavis r%__mul__zAccumulationBounds.__mul__Vs 99"b !K eT "%-::2#r* LE/AqBHHRWW-./#37CG44 "88##&q"--88##&sA..***88##&sA..88##&q"--%%==xx1::-*1b11xx1#5#55*B32266M--&DHHe,DHHe,..//&DHHe,DHHe,..%' tUU3 3r&cL t|trt|tr|jjs|j j r)|td|j z d|jz zS|jjr|j jr|jjr|j jr|jjr&|jjrtdtS|j jr'|jjrtt dStt tS|j jr|jjrh|j jr't|j |jz tS|j jrtt tS|jjr>|j jr(tt |j |j z S|jjr|jjri|j jr(tt |j|jz S|j jrtt tS|jjrx|j jrat|j|j z tS|jr-|tjtj fvr|tt tk(rtt tS|j tjur tt#d|t%d|S|jtj ur"tt#d| t%d| S|jr&t|j|z |j |z S|jr&t|j |z |j|z Sd|z tj&urt)|d|z dSt)|d|z St*S)Nr1rFr?)rArrBr" is_positiver# is_negativeis_extended_nonpositiveis_extended_nonnegativerPr rrRrrrCrDrrComplexInfinityrrEr;s r% __truediv__zAccumulationBounds.__truediv__s, eT "%-99((EII,A,A+a k1UYY;"GGGHH449Y9YII55%)):[:[xx''EII,=,=*1b11xx''EII,=,=*B322&sB//8800yy55 99,,#.txx%))/CR#HH 9999$/sB#77yy((UYY-K-K*B35990DEE8800yy55 99,,#.sDHHuyy4H#II 9999$/sB#77yy((UYY-K-K*488eii+?DD''QZZ););<<{B333*B333xx1::-*3q%=#a-HHxx1#5#55*3q5&>3q5&>JJ--&txx%'7E9IJJ//&txx%'7E9IJJE a///4UU;;4U++r&ct|tr|jr|jrtj S|j jr&|jjr|j jrg|jr'tt|d|jz tS|jr(tt t|d|jz S|jjrg|jr(tt t|d|j z S|jr'tt|d|j z tStt tStt||j z ||jz t!||j z ||jz St|d|z dSt"S)Nr1Fr?)rArrrPrrQr"rZr#r[rRrBrr rrrrEr;s r% __rtruediv__zAccumulationBounds.__rtruediv__s^ eT "%%==66MHH449Y9Yxx'' 55#.s5!dhh,/G#LL 55#.sCq488|4L#MMxx'' 55#.sCq488|4L#MM 55#.s5!dhh,/G#LL&sB//&s5488+;UTXX=M'N'*5488+;UTXX=M'NPPua$h7 7! !r&c t|tr|tjur|jj rN|j dkrtjS|jdkDrtjStdtS|j jrD|jdkDrtjS|j dkrtStjS|jdkDr/|j dkrtjStdtStt tS|tjur d|z tzS|j |jz jrN|jjr8|jr,|j|j|z|j |zS|j rtj"S|j$s |j&r?|jj(rTtt+|j|z|j |zt-|j|z|j |zS|j jrTtt+|j |z|j|zt-|j |z|j|zS|dzdk(r|jro|jj rt|j |ztS|j j rt|j|ztSd|z | zSttjt-|j|z|j |zS|dzdk(r|jrp|jj rt|j |ztS|j j rtt |j|zSd|z | zSt|j|z|j |zS|j.s |j0r|jj s"|j r~|jj rh|j3\}}|tj"ur&t|j4Dcgc] }|d|z z c}S|tj"ur ||zd|z zSt|tr|jj(s,|jj r|jj rz|j4Dcgc]}||z }}t7d|DsL|Dcgc]}|j4xs|fD]}|}}} |jt |t |St;||dSt<Scc}wcc}wcc}}w#t8$rY/wxYw)Nr1rr5c34K|]}|jywr9)is_Pow).0is r% z-AccumulationBounds.__pow__..,s3Aqxx3sFr?)rArrrCr"r[r#rQrBr rr NaNrDis_nonnegativefuncrPOne is_Integer is_integerrRrrr is_rationalas_numer_denomr.any TypeErrorrrE)r*r7numdenrepjrTs r%r:zAccumulationBounds.__pow__s eT " "8833xx!| vv xx!| zz)&q"--XX22xx"} vv xx"}" 55Lxx"}88a<#$66M*1b11&sB//***$|#488#3388**++#yy5$((E/JJ}}uu 5#3#38800&DHHeOTXXu_=DHHeOTXXu_=??XX22&DHHeOTXXu_=DHHeOTXXu_=??19>1188++#.txx#CC88++#.txx#CC !$5&11&DHHeOTXXu_ EGGQY!^1188++#.txx#CC88++#.sDHHeO#DD !$5&11&txx%HH5#4#4HH4411HH44 //1S!%%<&TYY(GQsU(GHH% #I3//%-HH1188 99*/**5Qq5A5333()B1166>aTBaQBQBB!#'99SVSV#<<tUU3 3')H6B )! !s$W W6W#W!! W-,W-c8|jr|jr|j|jz jr|t j urt j S|jrE|jDcgc]}||z c}\}}t|||k7r||}}|j||S|jrN|jjr|jddS|jjrt jSt||dScc}w)Nrr1Fr?) r(r[r#r"rRrrjr.rirPrQr)r*r7rerTbs r%__rpow__zAccumulationBounds.__rpow__7s ==U::488#%9%9@:~uu ))*.))4Qq41q!9>aqAyyA&}}88##99Q?*880066M5$//5s8 Dc|jjr|jS|jjrAt t j tt|j|jS|Sr9) r#rrHr"rBrrQrabsr)s r%__abs__zAccumulationBounds.__abs__JsQ 88 ( (<<> ! XX * *qvvs3txx=$(('CD DKr&c>t|}|tjtjfvr:|jtjus|j tjuryyt |j|k|j |k\}|dvr td|S)a Returns ``True`` if other is contained in self, where other belongs to extended real numbers, ``False`` if not contained, otherwise TypeError is raised. Examples ======== >>> from sympy import AccumBounds, oo >>> 1 in AccumBounds(-1, 3) True -oo and oo go together as limits (in AccumulationBounds). >>> -oo in AccumBounds(1, oo) True >>> oo in AccumBounds(-oo, 0) True TF)TFzinput failed to evaluate)rrrCrDr"r#rrp)r*r7rvs r% __contains__zAccumulationBounds.__contains__Ss, QZZ!3!34 4xx1---QZZ1G U"DHH$5 6 ] "67 7 r&ct|ttfs tdt|tr,tj }|D]}||vs|t|z}|S|j |jks|j|j kDrtj S|j|jkrT|j |j kr t|j|j S|j |j kDr|S|j|jkrU|j |j kr t|j|j S|j |j kDr|Syy)a Returns the intersection of 'self' and 'other'. Here other can be an instance of :py:class:`~.FiniteSet` or AccumulationBounds. Parameters ========== other : AccumulationBounds Another AccumulationBounds object with which the intersection has to be computed. Returns ======= AccumulationBounds Intersection of ``self`` and ``other``. Examples ======== >>> from sympy import AccumBounds, FiniteSet >>> AccumBounds(1, 3).intersection(AccumBounds(2, 4)) AccumBounds(2, 3) >>> AccumBounds(1, 3).intersection(AccumBounds(4, 6)) EmptySet >>> AccumBounds(1, 4).intersection(FiniteSet(1, 2, 5)) {1, 2} 4Input must be AccumulationBounds or FiniteSet objectN)rArBrrprEmptySetr#r")r*r7fin_setres r% intersectionzAccumulationBounds.intersectionus-@%+y!9:FH H eY 'jjG 59% ! 4G 5N 88eii 488eii#7::  88uyy xx599$"599dhh77xx%))# 99 yy488#"488UYY77yy488# $ !r&ct|ts td|j|jkrM|j|jk\r4t|jt |j|jS|j|jkrN|j|jk\r4t|jt |j|jSyy)Nr)rArBrpr"r#rr;s r%unionzAccumulationBounds.unions%-FH H 88uyy TXX%:txxTXXuyy)AB B 99 UYY$((%:uyy#dhh *BC C&; r&N)!__name__ __module__ __qualname____doc__rrrr _op_priorityr+propertyr"r#r3r6r rEr<rF__radd__rHrJrLrV__rmul__r]r_r:rwrzr}rrr&r%rrsgRI,$,0L    ## )) (#)#()*H1().(&)&(()(TH(9)9v(")"0(\)\|(0)0$ D8t Dr&rct|j|jryt|j|jryyNTF)r r#r"rlhsrhss r% _eval_is_ler1 SWWcgg SWWcggr&c|jstdt|d||jr/t |j |ryt |j|ryyy)af Returns ``True `` if range of values attained by ``lhs`` AccumulationBounds object is greater than the range of values attained by ``rhs``, where ``rhs`` may be any value of type AccumulationBounds object or extended real number value, ``False`` if ``rhs`` satisfies the same property, else an unevaluated :py:class:`~.Relational`. Examples ======== >>> from sympy import AccumBounds, oo >>> AccumBounds(1, 3) > AccumBounds(4, oo) False >>> AccumBounds(1, 4) > AccumBounds(3, 4) AccumBounds(1, 4) > AccumBounds(3, 4) >>> AccumBounds(1, oo) > -1 True Invalid comparison of  TFNrrptyperr r#rr"rs r%rrs_,   cC!" "   #  #   r&ct|j|jryt|j|jryyrrr"r#r rs r% _eval_is_gerrr&c|jstdt|d||jr/t |j |ryt |j|ryyy)ab Returns ``True`` if range of values attained by ``lhs`` AccumulationBounds object is less that the range of values attained by ``rhs``, where other may be any value of type AccumulationBounds object or extended real number value, ``False`` if ``rhs`` satisfies the same property, else an unevaluated :py:class:`~.Relational`. Examples ======== >>> from sympy import AccumBounds, oo >>> AccumBounds(1, 3) >= AccumBounds(4, oo) False >>> AccumBounds(1, 4) >= AccumBounds(3, 4) AccumBounds(1, 4) >= AccumBounds(3, 4) >>> AccumBounds(1, oo) >= 1 True rrTFN)rrprrrr"r r#rs r%rrs_*    #Y     #  #   r&c|jstdt|d||jr/t |j |ryt |j|ryyy)NrrTFrrs r%rrs]    #Y     #  #   r&ct|j|jryt|j|jryyrrrs r%rrrr&N)# sympy.corerrrrsympy.core.basicrsympy.core.exprrsympy.core.numbersr r r sympy.core.relationalr r rrsympy.core.sympifyr(sympy.functions.elementary.miscellaneousrrsympy.logic.boolalgrsympy.multipledispatchrsympy.series.orderrsympy.sets.setsrrrrrBrr&r%rs''" 22<<'=#+$%m Dm D` 012 e$%@ 012 d#$> $"# $  012! r&