K i^dZddlZddlmZmZmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZddlmZmZmZmZdd lmZdd lmZdd lmZdd lmZdd lm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,dZ-dZ.dZ/ddZ0dZ1dZ2dejfdfdZ4dZ5d dZ6dZ7gfdZ8y)!z>> from sympy import solve_poly_inequality, Poly >>> from sympy.abc import x >>> solve_poly_inequality(Poly(x, x, domain='ZZ'), '==') [{0}] >>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '!=') [Interval.open(-oo, -1), Interval.open(-1, 1), Interval.open(1, oo)] >>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '==') [{-1}, {1}] See Also ======== solve_poly_inequalities z8For efficiency reasons, `poly` should be a Poly instancer%could not determine truth value of %sF)multiple==!=T)NF><>=)r$T<=)r%Tz'%s' is not a valid relation) isinstancer ValueErroras_expr is_numberrrtrueRealsfalseEmptySetNotImplementedError real_rootsrappendNegativeInfinityInfinityLCreversedinsert)polyreltreals intervalsroot_intervalleftrightsigneq_signequal right_open multiplicitys `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/inequalities.pysolve_poly_inequalityrKs, dD ! FH H ||~ t||~q# . ;GG9  !''\JJ< %7!;= =69E d{ 'GD!d+H   X & 'd _ !!!**a 11 HE1eT48H   X &D X O 779q=DD$ #:G CZG D[%NGU D[%NGU;cAB BJJz"*5/ > D,a7?$$8D%UJGI,0%5yZe7?5$$8D%zBD(,d:EW_$$Qt(<= > 7?   8A..tZH J c Xt|Dcgc]}t|D]}|c}}Scc}}w)aSolve polynomial inequalities with rational coefficients. Examples ======== >>> from sympy import Poly >>> from sympy.solvers.inequalities import solve_poly_inequalities >>> from sympy.abc import x >>> solve_poly_inequalities((( ... Poly(x**2 - 3), ">"), ( ... Poly(-x**2 + 1), ">"))) Union(Interval.open(-oo, -sqrt(3)), Interval.open(-1, 1), Interval.open(sqrt(3), oo)) )rrK)polyspss rJsolve_poly_inequalitiesrQqs/ eG-BA-FG1G1G HHGs& c<tj}|D]}|sttjtjg}|D]\\}}}t ||z|}t |d}g} t j||D]:\} } | j| } | tjus*| j| <| }g} |D]2} |D]} | | z} | tjus"| j| 4| }|rn|D]} |j| }|S)a3Solve a system of rational inequalities with rational coefficients. Examples ======== >>> from sympy.abc import x >>> from sympy import solve_rational_inequalities, Poly >>> solve_rational_inequalities([[ ... ((Poly(-x + 1), Poly(1, x)), '>='), ... ((Poly(-x + 1), Poly(1, x)), '<=')]]) {1} >>> solve_rational_inequalities([[ ... ((Poly(x), Poly(1, x)), '!='), ... ((Poly(-x + 1), Poly(1, x)), '>=')]]) Union(Interval.open(-oo, 0), Interval.Lopen(0, 1)) See Also ======== solve_poly_inequality r") rr2rr6r7rK itertoolsproduct intersectr5union)eqsresult_eqsglobal_intervalsnumerdenomr<numer_intervalsdenom_intervalsr?numer_intervalglobal_intervalrBdenom_intervals rJsolve_rational_inequalitiesrbsK.ZZF$, $Q%7%7DE#'  NUEC3E%KEO3E4@OI3<3D3D#%547 //)33OD1::-$$X.  / ) I#3 6&56N#~5O6#!**4$$_5  6 ) #7 :) ,H\\(+F ,G$,L MrLTc d}g}tj}|D]}|sg}tj}|D]} t| tr| \} } n4| j r&| j | jz | j} } nd} | tjur#tjtjd} } } nV| tjur#tjtjd} } } n!| jj\} } t| | f|\\} } } | j$j&s"| j)| j)d}} } | j$j+}|j,s1|j.s%| | z } t1| d| } |t3| |dz}}|j5| | f| f|r`|t7|gz}t7|Dcgc]0}|D])\\}}}|j9|r||j:fdf+2c}}}}g}||z}||z}|s|r|j=}|r|j?|}|S#t $rt!t#dwxYwcc}}}}w)a8Reduce a system of rational inequalities with rational coefficients. Examples ======== >>> from sympy import Symbol >>> from sympy.solvers.inequalities import reduce_rational_inequalities >>> x = Symbol('x', real=True) >>> reduce_rational_inequalities([[x**2 <= 0]], x) Eq(x, 0) >>> reduce_rational_inequalities([[x + 2 > 0]], x) -2 < x >>> reduce_rational_inequalities([[(x + 2, ">")]], x) -2 < x >>> reduce_rational_inequalities([[x + 2]], x) Eq(x, -2) This function find the non-infinite solution set so if the unknown symbol is declared as extended real rather than real then the result may include finiteness conditions: >>> y = Symbol('y', extended_real=True) >>> reduce_rational_inequalities([[y + 2 > 0]], y) (-2 < y) & (y < oo) Tr"z only polynomials and rational functions are supported in this context. Fr) relational) rr2r0r+tuple is_Relationallhsrhsrel_opr/ZeroOner1togetheras_numer_denomrrrdomainis_Exactto_exact get_exactis_ZZis_QQrsolve_univariate_inequalityr5rbhasoneevalf as_relational)exprsgenrdexactrWsolution_exprsrY_solexprr<r[r\optrnindrAexcludes rJreduce_rational_inequalitiesrsq: E CzzH0 ww# 3D$& c%% $488 3T[[#DCqvv~$%FFAEE4cu$%EE155$cu#}}==? u &=ENC')#::&&&+nn&68H%euZZ))+FLLFLLU{!$3/3D#%PP eU^S12G# 3J  /7 7D14A4A4A!,&1a!QUU3Z78Z4F4A4F4A3BCG GODDa0d X>>#))#. OA# %j2' (4As I$5I;I8c$|jdurttdfdddd}g}|D]K\}}||jvrt |d|}nt | d||}|j |g|zMt ||S)aReduce an inequality with nested absolute values. Examples ======== >>> from sympy import reduce_abs_inequality, Abs, Symbol >>> x = Symbol('x', real=True) >>> reduce_abs_inequality(Abs(x - 5) - 3, '<', x) (2 < x) & (x < 8) >>> reduce_abs_inequality(Abs(x + 2)*3 - 13, '<', x) (-19/3 < x) & (x < 7/3) See Also ======== reduce_abs_inequalities Fzs Cannot solve inequalities with absolute values containing non-real variables. c g}|js |jrj|j}|jD]M} |}|s|}t j ||Dcgc]\\}}\}}|||||zf}}}}}O|S|j rO|jjs td|jfd |jD|St|trd |jd}|D]H\}}|j||t|dgzf|j| |t!|dgzfJ|S|gfg}|Scc}}}}w)Nz'Only Integer Powers are allowed on Abs.c32K|]\}}|z|fywN).0rcondsrs rJ zAreduce_abs_inequality.._bottom_up_scan..LsXkdE$'5)Xsr)is_Addis_MulfuncargsrSrTis_Powexp is_Integerr,extendbaser+rr5r r ) rryopargr}r_expr_condsr_bottom_up_scans @rJrz.reduce_abs_inequality.._bottom_up_scan9sj ;;$++Byy >(-"E&--eV<>>Ca=D%RaSXZ`buouv~>>E>  >. [[A<< !JKK LLX_TYY=WX X c "$TYYq\2F% = e tUbqk]%:;< teUbqk]%:;< =  BZLE #>s E r&r(r'r)r)is_extended_real TypeErrorrkeysrr5r)rr<rzmapping inequalitiesrrs @rJreduce_abs_inequalityrs( u$ $  >t$GL&t,, e glln $tQ,DteQ 5DTFUN+ , ( c ::rLc Zt|Dcgc]\}}t|||c}}Scc}}w)aReduce a system of inequalities with nested absolute values. Examples ======== >>> from sympy import reduce_abs_inequalities, Abs, Symbol >>> x = Symbol('x', extended_real=True) >>> reduce_abs_inequalities([(Abs(3*x - 5) - 7, '<'), ... (Abs(x + 25) - 13, '>')], x) (-2/3 < x) & (x < 4) & (((-oo < x) & (x < -38)) | ((-12 < x) & (x < oo))) >>> reduce_abs_inequalities([(Abs(x - 4) + Abs(3*x - 5) - 7, '<')], x) (1/2 < x) & (x < 4) See Also ======== reduce_abs_inequality )rr)ryrzrr<s rJreduce_abs_inequalitiesrfs9* ! D#(c37! ""!s' Fc J)ddlm}|jtjdurt t d|tjur3td|j|}|r|j}|S }|}jdur%tj}|s|S|j|Sj!tdd  j|id}tjur|}nމtj urtj}nj"j$z } t'| } | tj(k(rXt+| } j-| d} | tjur|}n,| tj urtj}n| t/| |} j0} | d vrLj-| j2dr|}n|j-| j4ds`tj}nO| d vrKj-| j4dr|}n,j-| j2dstj}|j4|j2}}||z tj6urt9d| ddj;|}|}| | j=\}} |j>vrtA| j>d kDrtBtE| |}|tB t+| ))fd}g}|D]}|jKtE|| |s tM)|}dj0vxrj0dk7} tO|jPtS|j4|j2z }tS||ztU|zjt9|j4|j2|j4|v|j2|v}tWd|DrtY|dd}n7t[|d}|drt  |d}tA|d kDr t]|}tj})j_tj`x}tj(k7rd}tS} tc||}te|t8s1|D]+}||vs||s|js|tS|z }-n|j4|j2}!} tY|tS|!zD]}|| }"| |!k7r||}#tg| |}$|$|vry|$jrm||$re|"r|#r|t9| |z }nQ|"r|t9jh| |z }n5|#r|t9jj| |z }n|t9jl| |z }|} |D]}%|tS|%z}|tjur+tCt djG|d|d|j;|}tjg}&|j4} | |vr.|| r&| jnr|&jqtS| |D]}'|'}!|tg| |!r|&jqt9| |!dd|'|vr|js|'n<|'|vr|js|'||'}(n|}(|(r|&jqtS|'|!} |j2}!|!|vr.||!r&|!jnr|&jqtS|!|tg| |!r%|&jqt9jl| |!|tj(k7rr|j;|}n#tutw|&||jG|}|s|S|j|S#t$rtt d wxYw#tBt f$r1t t djGtIdzwxYw#t$rt wxYw#t $r t dwxYw#t$rtj}d}YwxYw)aTSolves a real univariate inequality. Parameters ========== expr : Relational The target inequality gen : Symbol The variable for which the inequality is solved relational : bool A Relational type output is expected or not domain : Set The domain over which the equation is solved continuous: bool True if expr is known to be continuous over the given domain (and so continuous_domain() does not need to be called on it) Raises ====== NotImplementedError The solution of the inequality cannot be determined due to limitation in :func:`sympy.solvers.solveset.solvify`. Notes ===== Currently, we cannot solve all the inequalities due to limitations in :func:`sympy.solvers.solveset.solvify`. Also, the solution returned for trigonometric inequalities are restricted in its periodic interval. See Also ======== sympy.solvers.solveset.solvify: solver returning solveset solutions with solve's output API Examples ======== >>> from sympy import solve_univariate_inequality, Symbol, sin, Interval, S >>> x = Symbol('x') >>> solve_univariate_inequality(x**2 >= 4, x) ((2 <= x) & (x < oo)) | ((-oo < x) & (x <= -2)) >>> solve_univariate_inequality(x**2 >= 4, x, relational=False) Union(Interval(-oo, -2), Interval(2, oo)) >>> domain = Interval(0, S.Infinity) >>> solve_univariate_inequality(x**2 >= 4, x, False, domain) Interval(2, oo) >>> solve_univariate_inequality(sin(x) > 0, x, relational=False) Interval.open(0, pi) rdenomsFz| Inequalities in the complex domain are not supported. Try the real domain by setting domain=S.Reals)rd continuousNrzT extended_realz When gen is real, the relational has a complex part which leads to an invalid comparison like I < 0. r)r&r(r$z The inequality, %s, cannot be solved using solve_univariate_inequality. xcjt|} j|d}|tj tj fvr|S|jdurtj S|jd}|jrj|dStd|z#t$rtj }YwxYw)NrFr*z!relationship did not evaluate: %s) subsrrrrr1r/rr is_comparabler3)rvr expanded_errzs rJvalidz*solve_univariate_inequality..validsOOCA7  !QA))H%%.77NAA#yyA.-;a?AA! A sB//C  C =r#c34K|]}|jywr)r.)rrs rJrz.solve_univariate_inequality..@sz-solve_univariate_inequality..Cs Q=O=OrLz'sorting of these roots is not supportedz zZ contains imaginary parts which cannot be made 0 for any value of zm satisfying the inequality, leading to relations like I < 0. )<sympy.solvers.solversr is_subsetrr0r3rrt intersectionrxrr2r xreplacerr/r1rgrhrrjrrrrisupinfr7rrUrm free_symbolslenr,rrr rrsetboundaryrlistallrrsortedcoeff ImaginaryUnitrr+_ptRopenLopenopen is_finiter5removerr)*rrzrdrnrrrv_gen_domaineperiodconstfranger<rrrrsolnsr singularities include_xdiscontinuitiescritical_pointsr>sifted make_realcoeffIcheckim_solazstartend valid_startvalid_zptrPsol_setsr_validrs*`` @rJrtrtsr-  E)!*."##$ $ qww  ( ce <$$WQV%<= >*:sFCt{{*Bt{{d/BI U"%foofjj&**5'6#7#,em.Cd#G%/%#''3|VZZJJf,fjj.FH(I $*5MEI$**1??;;F"" f5A%a2!"7A 5%(qGYGY &)A, 67&'UUAEEs!')C.(H!I&A*/,K$|*/(%(]#%]#:r?R?RW\]_W`'2w(.(5!2D(D)4(.(..2J(J)0(.(..2J(J(.(--q2I(I$%E&"/3A"il2F3 QZZ'$Z!% #t 4d 1<&=>> &//7  |HJJE5>> from sympy import Eq, Symbol >>> from sympy.solvers.inequalities import _solve_inequality as f >>> from sympy.abc import x, y For linear expressions, the symbol can be isolated: >>> f(x - 2 < 0, x) x < 2 >>> f(-x - 6 < x, x) x > -3 Sometimes nonlinear relationships will be False >>> f(x**2 + 4 < 0, x) False Or they may involve more than one region of values: >>> f(x**2 - 4 < 0, x) (-2 < x) & (x < 2) To restrict the solution to a relational, set linear=True and only the x-dependent portion will be isolated on the left: >>> f(x**2 - 4 < 0, x, linear=True) x**2 < 4 Division of only nonzero quantities is allowed, so x cannot be isolated by dividing by y: >>> y.is_nonzero is None # it is unknown whether it is 0 or not True >>> f(x*y < 1, x) x*y < 1 And while an equality (or inequality) still holds after dividing by a non-zero quantity >>> nz = Symbol('nz', nonzero=True) >>> f(Eq(x*nz, 1), x) Eq(x, 1/nz) the sign must be known for other inequalities involving > or <: >>> f(x*nz <= 1, x) nz*x <= 1 >>> p = Symbol('p', positive=True) >>> f(x*p <= 1, x) x <= 1/p When there are denominators in the original expression that are removed by expansion, conditions for them will be returned as part of the result: >>> f(x < x*(2/x - 1), x) (x < 1) & Ne(x, 0) rrc |j||}|tjur|S|dvry|S#t$rtjcYSwxYw)NTF)rrNaNr)ierPrrs rJclassifyz#_solve_inequality..classifysN 1 AAEEz-'H 55L s%///A  A Nr$T)as_AddF)r#r")linear)rrrrhr9rgrr7rdegreerr-r3rrrtr/r1rras_independentris_zero is_negative is_positiverirk_solve_inequalityr r+r5)rrPrrrroorrOokoooknoorrrhbaxefrbeginning_denomscurrent_denomsrcrs rJrrsT-  vv{ [[ vv{q 3 33   B B 66BFF?D qM 88:?a(BAHHJN% %0 E z IIK   4 02 Q q !_  5 11 II   &!%& -AA q ==CB!!!S)B""&&>F266N:!N2 %A!"Q(Af=A!R QUUaZB155)QVV3LL!$  % #r :AQ"aff,RA&aff4 a2gQU1q59 :  LL ;A 0 1 81B4&!<" 80Q7 8B2&Dqvv~(2q""5"@WWQVT*RRC(ERRC(AGG3WWbS1Wd+WWQ"Wd+QVV|"&!&&.a2gq2v&bS1Wb)BT A+s8 AK))P6=L  P6 L$!P6#L$$DP65P6c  ii}}g}|D]_}|j|j}}|jt}t |dk(r|j nh|j |z} t | dk(r7| j |jtt|d| ttd|j r$|j gj||f|j fd} | r7td| Dr%|j gj||f:|jtt|d| b|j!D cgc]\} } t#| g| } } } |j!D cgc]\} } t%| | }} } t'| |z|zScc} } wcc} } w)Nr$rzZ inequality has more than one symbol of interest. c|jxr3|jxs%|jxr|jj Sr)ru is_Functionrrr)urzs rJrz&_reduce_inequalities..s;c D B!B!%%2B2B.BrLc3<K|]}t|tywr)r+rrrs rJrz'_reduce_inequalities..s!I*Q"4!Is)rgriatomsr rpoprr5rrr3r is_polynomial setdefaultfindritemsrrr)rsymbols poly_partabs_partother inequalityrr<genscommon componentsrzry poly_reduced abs_reduceds ` rJ_reduce_inequalitiesrtsbxI E"O NNJ$5$5c zz&! t9>((*C&&0F6{ajjl .z$3/GMN)*6+   c "  b ) 0 0$ =$DEJc!Ij!II##C,33T3K@ .z$3/GMN?OBR[Q`Q`Qbc:30%#>cLcIQIYZ:3*5#6ZKZ  +e3 55dZs G-GcVt|s|g}|Dcgc] }t|}}tj|Dcgc]}|jc}}t|s|g}t|xs||z}t d|Drt td|Dcic]&}|j|t|jd(}}|Dcgc]}|j|}}|Dchc]}|j|}}g}|D]}t|trF|j|jj!|j"j!z d}n|dvr t%|d}|dk(ro|dk(rt&j(cS|jj*rt-d|z|j/||}~t1||}|j|j3Dcic]\}}|| c}}Scc}wcc}wcc}wcc}wcc}wcc}}w) aEReduce a system of inequalities with rational coefficients. Examples ======== >>> from sympy.abc import x, y >>> from sympy import reduce_inequalities >>> reduce_inequalities(0 <= x + 3, []) (-3 <= x) & (x < oo) >>> reduce_inequalities(0 <= x + y*2 - 1, [x]) (x < oo) & (x >= 1 - 2*y) c38K|]}|jduyw)FNrr s rJrz&reduce_inequalities..s 811   & 8szP inequalities cannot contain symbols that are not real. TrrrFr )rrrrVranyrrrr namerr+rrrgr-rhr rr1r.r3r5rr) rrrrrecastkeeprkrs rJreduce_inequalitiesr"s L !$~ (451GAJ5L5 35;;>A> ?D G )7|#tt+G 8 88 $  5 ++3qvvT225F50<=1AJJv&=L=+23aqzz&!3G3 D   a $quu}}8!5=309s"H H +HH/H 7 H% )T)F)9__doc__rSsympy.calculus.utilrrr sympy.corersympy.core.exprtoolsrsympy.core.relationalrr r r sympy.core.symbolr r sympy.sets.setsrrrrsympy.core.singletonrsympy.core.functionr$sympy.functions.elementary.complexesr sympy.logicr sympy.polysrrrsympy.polys.polyutilsrsympy.solvers.solvesetrrsympy.utilities.iterablesrrsympy.utilities.miscrrKrQrbrrrr0rtrrrr"rrLrJr3sB-88+DD"*4FF(44+XvI"?DXvD;N"27;177W\d