K i0nddlZddlmZddlmZmZddlmZddlm Z  GddeZ Gdd eZ y) N)Interval) is_increasing is_decreasing) Optimization)UndefinedFunctionc4eZdZdZfdZdZdZdZxZS) SumApproxa8 Approximates sum by neglecting small terms. Explanation =========== If terms are expressions which can be determined to be monotonic, then bounds for those expressions are added. Parameters ========== bounds : dict Mapping expressions to length 2 tuple of bounds (low, high). reltol : number Threshold for when to ignore a term. Taken relative to the largest lower bound among bounds. Examples ======== >>> from sympy import exp >>> from sympy.abc import x, y, z >>> from sympy.codegen.rewriting import optimize >>> from sympy.codegen.approximations import SumApprox >>> bounds = {x: (-1, 1), y: (1000, 2000), z: (-10, 3)} >>> sum_approx3 = SumApprox(bounds, reltol=1e-3) >>> sum_approx2 = SumApprox(bounds, reltol=1e-2) >>> sum_approx1 = SumApprox(bounds, reltol=1e-1) >>> expr = 3*(x + y + exp(z)) >>> optimize(expr, [sum_approx3]) 3*(x + y + exp(z)) >>> optimize(expr, [sum_approx2]) 3*y + 3*exp(z) >>> optimize(expr, [sum_approx1]) 3*y c @t|di|||_||_y)N)super__init__boundsreltol)selfrrkwargs __class__s b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/codegen/approximations.pyr zSumApprox.__init__6s! "6"  c^|jjjfdS)Nc&j|SNvalueargrs rz$SumApprox.__call__..<TZZ_rfactorreplacequeryrexprs` r__call__zSumApprox.__call__;!{{}$$TZZ1LMMrc|jSr)is_Addr"s rr!zSumApprox.query>s {{rc |jD]-}|js&|jvst|jdk7r7|j\}|jvrTt j|}t |||rT|j|j|di|j|j|difj|<t|||rU|j|j|di|j|j|difj|<,|cStfd|jDr|jDcgc]!}|jr||fnj|#}}d}|D]<\}}|dcxkr|krnnt|tt|t|}>g} t|j|D]H\}\}}tt|t||jzk\s8| j|J|j | S|Scc}w)Nrc3VK|] }|jxs|jv"ywr) is_numberr).0termrs r z"SumApprox.value..Vs&Jt~~4!44Js&))argsr+rlen free_symbolsrrsubsrallmaxminabsziprappendfunc) raddr-fsintrvlrlargest_abs_guaranteelohi new_termss ` rrzSumApprox.valueAs+HH D~~!4D>> from sympy import sin, pi >>> from sympy.abc import x, y >>> from sympy.codegen.rewriting import optimize >>> from sympy.codegen.approximations import SeriesApprox >>> bounds = {x: (-.1, .1), y: (pi-1, pi+1)} >>> series_approx2 = SeriesApprox(bounds, reltol=1e-2) >>> series_approx3 = SeriesApprox(bounds, reltol=1e-3) >>> series_approx8 = SeriesApprox(bounds, reltol=1e-8) >>> expr = sin(x)*sin(y) >>> optimize(expr, [series_approx2]) x*(-y + (y - pi)**3/6 + pi) >>> optimize(expr, [series_approx3]) (-x**3/6 + x)*sin(y) >>> optimize(expr, [series_approx8]) sin(x)*sin(y) c t|di|||_||_||_|dzdk(r t d||_tjtj|j |_ y)Nr)z7Checking the solution at expansion point is not helpfulr ) r r rr max_order ValueErrorn_point_checksmathceillog10_prec)rrrrLrNrrs rr zSeriesApprox.__init__si "6"  " A  "VW W,YY 4;; 778 rc^|jjjfdS)Nc&j|Srrrs rrz'SeriesApprox.__call__..rrrr"s` rr$zSeriesApprox.__call__r%rct|jxr+t|t xrt|jdk(S)Nr)) is_Function isinstancerr0r/r"s rr!zSeriesApprox.querys7  (D:K)L%L( Na' )rcd|j}t|dk7r|S|\}||jvr|S|j|\}}||zdz }d}t|jdzddD]}|j |||j } d} t|jD]~} || ||z z|jdz z z} | j|| i} |j|| i}td| |z z j|j|jkDs|d} n| r| }n||S|S)Nr)rKr)x0nTF) r1r0rrangerLseriesremoveOrNxreplacer6evalfrRr)rfexprr1symbr>r?rZcheapestr[fserin_okidxxvalrefs rrzSeriesApprox.valuesM)) |  !L t{{ "LT"B2gq[t~~a'B/ ALL"L2::ros6 $E01V VrT<Tr