Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Adaptive numerical evaluation of SymPy expressions, using mpmath for mathematical functions. """ from __future__ import print_function, division
import math
import mpmath.libmp as libmp from mpmath import ( make_mpc, make_mpf, mp, mpc, mpf, nsum, quadts, quadosc, workprec) from mpmath import inf as mpmath_inf from mpmath.libmp import (from_int, from_man_exp, from_rational, fhalf, fnan, fnone, fone, fzero, mpf_abs, mpf_add, mpf_atan, mpf_atan2, mpf_cmp, mpf_cos, mpf_e, mpf_exp, mpf_log, mpf_lt, mpf_mul, mpf_neg, mpf_pi, mpf_pow, mpf_pow_int, mpf_shift, mpf_sin, mpf_sqrt, normalize, round_nearest, to_int, to_str) from mpmath.libmp import bitcount as mpmath_bitcount from mpmath.libmp.backend import MPZ from mpmath.libmp.libmpc import _infs_nan from mpmath.libmp.libmpf import dps_to_prec, prec_to_dps from mpmath.libmp.gammazeta import mpf_bernoulli
from .compatibility import SYMPY_INTS, range from .sympify import sympify from .singleton import S
from sympy.utilities.iterables import is_sequence
LG10 = math.log(10, 2) rnd = round_nearest
def bitcount(n):
# Used in a few places as placeholder values to denote exponents and # precision levels, e.g. of exact numbers. Must be careful to avoid # passing these to mpmath functions or returning them in final results. INF = float(mpmath_inf) MINUS_INF = float(-mpmath_inf)
# ~= 100 digits. Real men set this to INF. DEFAULT_MAXPREC = 333
class PrecisionExhausted(ArithmeticError): pass
#----------------------------------------------------------------------------# # # # Helper functions for arithmetic and complex parts # # # #----------------------------------------------------------------------------#
""" An mpf value tuple is a tuple of integers (sign, man, exp, bc) representing a floating-point number: [1, -1][sign]*man*2**exp where sign is 0 or 1 and bc should correspond to the number of bits used to represent the mantissa (man) in binary notation, e.g.
>>> from sympy.core.evalf import bitcount >>> sign, man, exp, bc = 0, 5, 1, 3 >>> n = [1, -1][sign]*man*2**exp >>> n, bitcount(man) (10, 3)
A temporary result is a tuple (re, im, re_acc, im_acc) where re and im are nonzero mpf value tuples representing approximate numbers, or None to denote exact zeros.
re_acc, im_acc are integers denoting log2(e) where e is the estimated relative accuracy of the respective complex part, but may be anything if the corresponding complex part is None.
"""
def fastlog(x): """Fast approximation of log2(x) for an mpf value tuple x.
Notes: Calculated as exponent + width of mantissa. This is an approximation for two reasons: 1) it gives the ceil(log2(abs(x))) value and 2) it is too high by 1 in the case that x is an exact power of 2. Although this is easy to remedy by testing to see if the odd mpf mantissa is 1 (indicating that one was dealing with an exact power of 2) that would decrease the speed and is not necessary as this is only being used as an approximation for the number of bits in x. The correct return value could be written as "x[2] + (x[3] if x[1] != 1 else 0)". Since mpf tuples always have an odd mantissa, no check is done to see if the mantissa is a multiple of 2 (in which case the result would be too large by 1).
Examples ========
>>> from sympy import log >>> from sympy.core.evalf import fastlog, bitcount >>> s, m, e = 0, 5, 1 >>> bc = bitcount(m) >>> n = [1, -1][s]*m*2**e >>> n, (log(n)/log(2)).evalf(2), fastlog((s, m, e, bc)) (10, 3.3, 4) """
def pure_complex(v, or_real=False): """Return a and b if v matches a + I*b where b is not zero and a and b are Numbers, else None. If `or_real` is True then 0 will be returned for `b` if `v` is a real number.
>>> from sympy.core.evalf import pure_complex >>> from sympy import sqrt, I, S >>> a, b, surd = S(2), S(3), sqrt(2) >>> pure_complex(a) >>> pure_complex(a, or_real=True) (2, 0) >>> pure_complex(surd) >>> pure_complex(a + b*I) (2, 3) >>> pure_complex(I) (0, 1) """
def scaled_zero(mag, sign=1): """Return an mpf representing a power of two with magnitude ``mag`` and -1 for precision. Or, if ``mag`` is a scaled_zero tuple, then just remove the sign from within the list that it was initially wrapped in.
Examples ========
>>> from sympy.core.evalf import scaled_zero >>> from sympy import Float >>> z, p = scaled_zero(100) >>> z, p (([0], 1, 100, 1), -1) >>> ok = scaled_zero(z) >>> ok (0, 1, 100, 1) >>> Float(ok) 1.26765060022823e+30 >>> Float(ok, p) 0.e+30 >>> ok, p = scaled_zero(100, -1) >>> Float(scaled_zero(ok), p) -0.e+30 """ raise ValueError('sign must be +/-1') else: raise ValueError('scaled zero expects int or scaled_zero tuple.')
def iszero(mpf, scaled=False):
def complex_accuracy(result): """ Returns relative accuracy of a complex number with given accuracies for the real and imaginary parts. The relative accuracy is defined in the complex norm sense as ||z|+|error|| / |z| where error is equal to (real absolute error) + (imag absolute error)*i.
The full expression for the (logarithmic) error can be approximated easily by using the max norm to approximate the complex norm.
In the worst case (re and im equal), this is wrong by a factor sqrt(2), or by log2(sqrt(2)) = 0.5 bit. """
def get_abs(expr, prec, options): if not re: re, re_acc, im, im_acc = im, im_acc, re, re_acc if im: return libmp.mpc_abs((re, im), prec), None, re_acc, None elif re: return mpf_abs(re), None, re_acc, None else: return None, None, None, None
def get_complex_part(expr, no, prec, options): """no = 0 for real part, no = 1 for imaginary part""" # XXX is the last one correct? Consider re((1+I)**2).n() workprec += max(30, 2**i) i += 1
def evalf_abs(expr, prec, options):
def evalf_re(expr, prec, options):
def evalf_im(expr, prec, options):
def finalize_complex(re, im, prec): raise ValueError("got complex zero with unknown accuracy") return None, im, None, prec return re, None, prec, None
else:
def chop_parts(value, prec): """ Chop off tiny real or complex parts. """ # Method 1: chop based on absolute value # Method 2: chop if inaccurate and relatively small re, re_acc = None, None im, im_acc = None, None
def check_target(expr, result, prec): raise PrecisionExhausted("Failed to distinguish the expression: \n\n%s\n\n" "from zero. Try simplifying the input, using chop=True, or providing " "a higher maxn for evalf" % (expr))
def get_integer_part(expr, no, options, return_ints=False): """ With no = 1, computes ceiling(expr) With no = -1, computes floor(expr)
Note: this function either gives the exact result or signals failure. """ # The expression is likely less than 2^30 or so
# We now know the size, so we can calculate how much extra precision # (if any) is needed to get within the nearest integer gap = max(fastlog(ire) - ire_acc, fastlog(iim) - iim_acc) elif iim: gap = fastlog(iim) - iim_acc else: # ... or maybe the expression was exactly zero return None, None, None, None
ire, iim, ire_acc, iim_acc = \ evalf(expr, margin + assumed_size + gap, options)
# We can now easily find the nearest integer, but to find floor/ceil, we # must also calculate whether the difference to the nearest integer is # positive or negative (which may fail if very close). # if there are subs and they all contain integer re/im parts # then we can (hopefully) safely substitute them into the # expression doit = True from sympy.core.compatibility import as_int for v in s.values(): try: as_int(v) except ValueError: try: [as_int(i) for i in v.as_real_imag()] continue except (ValueError, AttributeError): doit = False break if doit: expr = expr.subs(s)
except PrecisionExhausted: if not expr.equals(0): raise PrecisionExhausted x = fzero
im_, im_acc = calc_part(im(expr, evaluate=False), iim)
return re_, im_, re_acc, im_acc
def evalf_ceiling(expr, prec, options): return get_integer_part(expr.args[0], 1, options)
def evalf_floor(expr, prec, options): return get_integer_part(expr.args[0], -1, options)
#----------------------------------------------------------------------------# # # # Arithmetic operations # # # #----------------------------------------------------------------------------#
def add_terms(terms, prec, target_prec): """ Helper for evalf_add. Adds a list of (mpfval, accuracy) terms.
Returns -------
- None, None if there are no non-zero terms; - terms[0] if there is only 1 term; - scaled_zero if the sum of the terms produces a zero by cancellation e.g. mpfs representing 1 and -1 would produce a scaled zero which need special handling since they are not actually zero and they are purposely malformed to ensure that they can't be used in anything but accuracy calculations; - a tuple that is scaled to target_prec that corresponds to the sum of the terms.
The returned mpf tuple will be normalized to target_prec; the input prec is used to define the working precision.
XXX explain why this is needed and why one can't just loop using mpf_add """
# see if any argument is NaN or oo and thus warrants a special return special.append(arg) from sympy.core.add import Add rv = evalf(Add(*special), prec + 4, {}) return rv[0], rv[2]
# x much larger than existing sum? # first: quick test ((not sum_man) or delta - bitcount(abs(sum_man)) > working_prec)): sum_man = man sum_exp = exp else: else: # x much smaller than existing sum? sum_man, sum_exp = man, exp else: else: rnd), sum_accuracy
def evalf_add(v, prec, options):
[a[0::2] for a in terms if a[0]], prec, target_prec) [a[1::2] for a in terms if a[1]], prec, target_prec) print("ADD: wanted", target_prec, "accurate bits, got", re_acc, im_acc) else:
print("ADD: restarting with prec", prec)
def evalf_mul(v, prec, options): # the only pure complex that is a mul is h*I
# see if any argument is NaN or oo and thus warrants a special return special.append(arg) from sympy.core.mul import Mul special = Mul(*special) return evalf(special, prec + 4, {})
# With guard digits, multiplication in the real case does not destroy # accuracy. This is also true in the complex case when considering the # total accuracy; however accuracy for the real or imaginary parts # separately may be lower.
# XXX: big overestimate
# Empty product is 1
# First, we multiply all pure real or pure imaginary numbers. # direction tells us that the result should be multiplied by # I**direction; all other numbers get put into complex_factors # to be multiplied out after the first phase.
else: # multiply by i else: else: # initialize with the first term # there was a real part; give it an imaginary part else: # there is no real part to start (other than the starting 1) complex_accuracy((wre, wim, wre_acc, wim_acc)))
# acc is the overall accuracy of the product; we aren't # computing exact accuracies of the product. complex_accuracy((wre, wim, wre_acc, wim_acc)))
print("MUL: wanted", prec, "accurate bits, got", acc) # multiply by I
def evalf_pow(v, prec, options):
# We handle x**n separately. This has two purposes: 1) it is much # faster, because we avoid calling evalf on the exponent, and 2) it # allows better handling of real/imaginary parts that are exactly zero # Exact return fone, None, prec, None # Exponentiation by p magnifies relative error by |p|, so the # base must be evaluated with increased precision if p is large # Real to integer power # (x*I)**n = I**n * x**n z = mpf_pow_int(im, p, target_prec) case = p % 4 if case == 0: return z, None, target_prec, None if case == 1: return None, z, None, target_prec if case == 2: return mpf_neg(z), None, target_prec, None if case == 3: return None, mpf_neg(z), None, target_prec # Zero raised to an integer power # General complex number to arbitrary integer power # Assumes full accuracy in input
# Pure square root # General complex square root return None, None, None, None # Square root of a negative real number # Positive square root
# We first evaluate the exponent to find its magnitude # This determines the working precision that must be used # Special cases: x**0 return fone, None, prec, None
# Restart if too big # XXX: prec + ysize might exceed maxprec prec += ysize yre, yim, _, _ = evalf(exp, prec, options)
# Pure exponential function; no need to evalf the base return mpf_exp(yre, target_prec), None, target_prec, None
# 0**y return None, None, None, None
# (real ** complex) or (complex ** complex) re, im = libmp.mpc_pow( (xre or fzero, xim or fzero), (yre or fzero, yim), target_prec) return finalize_complex(re, im, target_prec) # complex ** real re, im = libmp.mpc_pow_mpf((xre or fzero, xim), yre, target_prec) return finalize_complex(re, im, target_prec) # negative ** real # positive ** real else:
#----------------------------------------------------------------------------# # # # Special functions # # # #----------------------------------------------------------------------------# def evalf_trig(v, prec, options): """ This function handles sin and cos of complex arguments.
TODO: should also handle tan of complex arguments. """ else: raise NotImplementedError # 20 extra bits is possibly overkill. It does make the need # to restart very unlikely if 'subs' in options: v = v.subs(options['subs']) return evalf(v._eval_evalf(prec), prec, options) if v.func is cos: return fone, None, prec, None elif v.func is sin: return None, None, None, None else: raise NotImplementedError # For trigonometric functions, we are interested in the # fixed-point (absolute) accuracy of the argument. # Magnitude <= 1.0. OK to compute directly, because there is no # danger of hitting the first root of cos (with sin, magnitude # <= 2.0 would actually be ok) # Very large xprec = prec + xsize re, im, re_acc, im_acc = evalf(arg, xprec, options) # Need to repeat in case the argument is very close to a # multiple of pi (or pi/2), hitting close to a root if options.get('verbose'): print("SIN/COS", accuracy, "wanted", prec, "gap", gap) print(to_str(y, 10)) if xprec > options.get('maxprec', DEFAULT_MAXPREC): return y, None, accuracy, None xprec += gap re, im, re_acc, im_acc = evalf(arg, xprec, options) continue else:
def evalf_log(expr, prec, options): expr = expr.doit() return evalf(expr, prec, options)
# XXX: use get_abs etc instead re = evalf_log( log(Abs(arg, evaluate=False), evaluate=False), prec, options) im = mpf_atan2(xim, xre or fzero, prec) return re[0], im, re[2], prec
# We actually need to compute 1+x accurately, not x arg = Add(S.NegativeOne, arg, evaluate=False) xre, xim, _, _ = evalf_add(arg, prec, options) prec2 = workprec - fastlog(xre) # xre is now x - 1 so we add 1 back here to calculate x re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd)
return re, mpf_pi(prec), re_acc, prec else:
def evalf_atan(v, prec, options): arg = v.args[0] xre, xim, reacc, imacc = evalf(arg, prec + 5, options) if xre is xim is None: return (None,)*4 if xim: raise NotImplementedError return mpf_atan(xre, prec, rnd), None, prec, None
def evalf_subs(prec, subs): """ Change all Float entries in `subs` to have precision prec. """ newsubs = {} for a, b in subs.items(): b = S(b) if b.is_Float: b = b._eval_evalf(prec) newsubs[a] = b return newsubs
def evalf_piecewise(expr, prec, options): from sympy import Float, Integer if 'subs' in options: expr = expr.subs(evalf_subs(prec, options['subs'])) newopts = options.copy() del newopts['subs'] if hasattr(expr, 'func'): return evalf(expr, prec, newopts) if type(expr) == float: return evalf(Float(expr), prec, newopts) if type(expr) == int: return evalf(Integer(expr), prec, newopts)
# We still have undefined symbols raise NotImplementedError
def evalf_bernoulli(expr, prec, options): arg = expr.args[0] if not arg.is_Integer: raise ValueError("Bernoulli number index must be an integer") n = int(arg) b = mpf_bernoulli(n, prec, rnd) if b == fzero: return None, None, None, None return b, None, prec, None
#----------------------------------------------------------------------------# # # # High-level operations # # # #----------------------------------------------------------------------------#
def as_mpmath(x, prec, options): from sympy.core.numbers import Infinity, NegativeInfinity, Zero x = sympify(x) if isinstance(x, Zero) or x == 0: return mpf(0) if isinstance(x, Infinity): return mpf('inf') if isinstance(x, NegativeInfinity): return mpf('-inf') # XXX re, im, _, _ = evalf(x, prec, options) if im: return mpc(re or fzero, im) return mpf(re)
def do_integral(expr, prec, options): func = expr.args[0] x, xlow, xhigh = expr.args[1] if xlow == xhigh: xlow = xhigh = 0 elif x not in func.free_symbols: # only the difference in limits matters in this case # so if there is a symbol in common that will cancel # out when taking the difference, then use that # difference if xhigh.free_symbols & xlow.free_symbols: diff = xhigh - xlow if not diff.free_symbols: xlow, xhigh = 0, diff
oldmaxprec = options.get('maxprec', DEFAULT_MAXPREC) options['maxprec'] = min(oldmaxprec, 2*prec)
with workprec(prec + 5): xlow = as_mpmath(xlow, prec + 15, options) xhigh = as_mpmath(xhigh, prec + 15, options)
# Integration is like summation, and we can phone home from # the integrand function to update accuracy summation style # Note that this accuracy is inaccurate, since it fails # to account for the variable quadrature weights, # but it is better than nothing
from sympy import cos, sin, Wild
have_part = [False, False] max_real_term = [MINUS_INF] max_imag_term = [MINUS_INF]
def f(t): re, im, re_acc, im_acc = evalf(func, mp.prec, {'subs': {x: t}})
have_part[0] = re or have_part[0] have_part[1] = im or have_part[1]
max_real_term[0] = max(max_real_term[0], fastlog(re)) max_imag_term[0] = max(max_imag_term[0], fastlog(im))
if im: return mpc(re or fzero, im) return mpf(re or fzero)
if options.get('quad') == 'osc': A = Wild('A', exclude=[x]) B = Wild('B', exclude=[x]) D = Wild('D') m = func.match(cos(A*x + B)*D) if not m: m = func.match(sin(A*x + B)*D) if not m: raise ValueError("An integrand of the form sin(A*x+B)*f(x) " "or cos(A*x+B)*f(x) is required for oscillatory quadrature") period = as_mpmath(2*S.Pi/m[A], prec + 15, options) result = quadosc(f, [xlow, xhigh], period=period) # XXX: quadosc does not do error detection yet quadrature_error = MINUS_INF else: result, quadrature_error = quadts(f, [xlow, xhigh], error=1) quadrature_error = fastlog(quadrature_error._mpf_)
options['maxprec'] = oldmaxprec
if have_part[0]: re = result.real._mpf_ if re == fzero: re, re_acc = scaled_zero( min(-prec, -max_real_term[0], -quadrature_error)) re = scaled_zero(re) # handled ok in evalf_integral else: re_acc = -max(max_real_term[0] - fastlog(re) - prec, quadrature_error) else: re, re_acc = None, None
if have_part[1]: im = result.imag._mpf_ if im == fzero: im, im_acc = scaled_zero( min(-prec, -max_imag_term[0], -quadrature_error)) im = scaled_zero(im) # handled ok in evalf_integral else: im_acc = -max(max_imag_term[0] - fastlog(im) - prec, quadrature_error) else: im, im_acc = None, None
result = re, im, re_acc, im_acc return result
def evalf_integral(expr, prec, options): limits = expr.limits if len(limits) != 1 or len(limits[0]) != 3: raise NotImplementedError workprec = prec i = 0 maxprec = options.get('maxprec', INF) while 1: result = do_integral(expr, workprec, options) accuracy = complex_accuracy(result) if accuracy >= prec: # achieved desired precision break if workprec >= maxprec: # can't increase accuracy any more break if accuracy == -1: # maybe the answer really is zero and maybe we just haven't increased # the precision enough. So increase by doubling to not take too long # to get to maxprec. workprec *= 2 else: workprec += max(prec, 2**i) workprec = min(workprec, maxprec) i += 1 return result
def check_convergence(numer, denom, n): """ Returns (h, g, p) where -- h is: > 0 for convergence of rate 1/factorial(n)**h < 0 for divergence of rate factorial(n)**(-h) = 0 for geometric or polynomial convergence or divergence
-- abs(g) is: > 1 for geometric convergence of rate 1/h**n < 1 for geometric divergence of rate h**n = 1 for polynomial convergence or divergence
(g < 0 indicates an alternating series)
-- p is: > 1 for polynomial convergence of rate 1/n**h <= 1 for polynomial divergence of rate n**(-h)
""" from sympy import Poly npol = Poly(numer, n) dpol = Poly(denom, n) p = npol.degree() q = dpol.degree() rate = q - p if rate: return rate, None, None constant = dpol.LC() / npol.LC() if abs(constant) != 1: return rate, constant, None if npol.degree() == dpol.degree() == 0: return rate, constant, 0 pc = npol.all_coeffs()[1] qc = dpol.all_coeffs()[1] return rate, constant, (qc - pc)/dpol.LC()
def hypsum(expr, n, start, prec): """ Sum a rapidly convergent infinite hypergeometric series with given general term, e.g. e = hypsum(1/factorial(n), n). The quotient between successive terms must be a quotient of integer polynomials. """ from sympy import Float, hypersimp, lambdify
if prec == float('inf'): raise NotImplementedError('does not support inf prec')
if start: expr = expr.subs(n, n + start) hs = hypersimp(expr, n) if hs is None: raise NotImplementedError("a hypergeometric series is required") num, den = hs.as_numer_denom()
func1 = lambdify(n, num) func2 = lambdify(n, den)
h, g, p = check_convergence(num, den, n)
if h < 0: raise ValueError("Sum diverges like (n!)^%i" % (-h))
term = expr.subs(n, 0) if not term.is_Rational: raise NotImplementedError("Non rational term functionality is not implemented.")
# Direct summation if geometric or faster if h > 0 or (h == 0 and abs(g) > 1): term = (MPZ(term.p) << prec) // term.q s = term k = 1 while abs(term) > 5: term *= MPZ(func1(k - 1)) term //= MPZ(func2(k - 1)) s += term k += 1 return from_man_exp(s, -prec) else: alt = g < 0 if abs(g) < 1: raise ValueError("Sum diverges like (%i)^n" % abs(1/g)) if p < 1 or (p == 1 and not alt): raise ValueError("Sum diverges like n^%i" % (-p)) # We have polynomial convergence: use Richardson extrapolation vold = None ndig = prec_to_dps(prec) while True: # Need to use at least quad precision because a lot of cancellation # might occur in the extrapolation process; we check the answer to # make sure that the desired precision has been reached, too. prec2 = 4*prec term0 = (MPZ(term.p) << prec2) // term.q
def summand(k, _term=[term0]): if k: k = int(k) _term[0] *= MPZ(func1(k - 1)) _term[0] //= MPZ(func2(k - 1)) return make_mpf(from_man_exp(_term[0], -prec2))
with workprec(prec): v = nsum(summand, [0, mpmath_inf], method='richardson') vf = Float(v, ndig) if vold is not None and vold == vf: break prec += prec # double precision each time vold = vf
return v._mpf_
def evalf_prod(expr, prec, options): from sympy import Sum if all((l[1] - l[2]).is_Integer for l in expr.limits): re, im, re_acc, im_acc = evalf(expr.doit(), prec=prec, options=options) else: re, im, re_acc, im_acc = evalf(expr.rewrite(Sum), prec=prec, options=options) return re, im, re_acc, im_acc
def evalf_sum(expr, prec, options): from sympy import Float if 'subs' in options: expr = expr.subs(options['subs']) func = expr.function limits = expr.limits if len(limits) != 1 or len(limits[0]) != 3: raise NotImplementedError if func is S.Zero: return mpf(0), None, None, None prec2 = prec + 10 try: n, a, b = limits[0] if b != S.Infinity or a != int(a): raise NotImplementedError # Use fast hypergeometric summation if possible v = hypsum(func, n, int(a), prec2) delta = prec - fastlog(v) if fastlog(v) < -10: v = hypsum(func, n, int(a), delta) return v, None, min(prec, delta), None except NotImplementedError: # Euler-Maclaurin summation for general series eps = Float(2.0)**(-prec) for i in range(1, 5): m = n = 2**i * prec s, err = expr.euler_maclaurin(m=m, n=n, eps=eps, eval_integral=False) err = err.evalf() if err <= eps: break err = fastlog(evalf(abs(err), 20, options)[0]) re, im, re_acc, im_acc = evalf(s, prec2, options) if re_acc is None: re_acc = -err if im_acc is None: im_acc = -err return re, im, re_acc, im_acc
#----------------------------------------------------------------------------# # # # Symbolic interface # # # #----------------------------------------------------------------------------#
def evalf_symbol(x, prec, options): if not val: return None, None, None, None return val._mpf_, None, prec, None else:
evalf_table = None
def _create_evalf_table(): global evalf_table from sympy.functions.combinatorial.numbers import bernoulli from sympy.concrete.products import Product from sympy.concrete.summations import Sum from sympy.core.add import Add from sympy.core.mul import Mul from sympy.core.numbers import Exp1, Float, Half, ImaginaryUnit, Integer, NaN, NegativeOne, One, Pi, Rational, Zero from sympy.core.power import Pow from sympy.core.symbol import Dummy, Symbol from sympy.functions.elementary.complexes import Abs, im, re from sympy.functions.elementary.exponential import exp, log from sympy.functions.elementary.integers import ceiling, floor from sympy.functions.elementary.piecewise import Piecewise from sympy.functions.elementary.trigonometric import atan, cos, sin from sympy.integrals.integrals import Integral Symbol: evalf_symbol, Dummy: evalf_symbol, Float: lambda x, prec, options: (x._mpf_, None, prec, None), Rational: lambda x, prec, options: (from_rational(x.p, x.q, prec), None, prec, None), Integer: lambda x, prec, options: (from_int(x.p, prec), None, prec, None), Zero: lambda x, prec, options: (None, None, prec, None), One: lambda x, prec, options: (fone, None, prec, None), Half: lambda x, prec, options: (fhalf, None, prec, None), Pi: lambda x, prec, options: (mpf_pi(prec), None, prec, None), Exp1: lambda x, prec, options: (mpf_e(prec), None, prec, None), ImaginaryUnit: lambda x, prec, options: (None, fone, None, prec), NegativeOne: lambda x, prec, options: (fnone, None, prec, None), NaN: lambda x, prec, options: (fnan, None, prec, None),
exp: lambda x, prec, options: evalf_pow( Pow(S.Exp1, x.args[0], evaluate=False), prec, options),
cos: evalf_trig, sin: evalf_trig,
Add: evalf_add, Mul: evalf_mul, Pow: evalf_pow,
log: evalf_log, atan: evalf_atan, Abs: evalf_abs,
re: evalf_re, im: evalf_im, floor: evalf_floor, ceiling: evalf_ceiling,
Integral: evalf_integral, Sum: evalf_sum, Product: evalf_prod, Piecewise: evalf_piecewise,
bernoulli: evalf_bernoulli, }
def evalf(x, prec, options): # Fall back to ordinary evalf if possible x = x.subs(evalf_subs(prec, options['subs'])) raise NotImplementedError re = None reprec = None elif im.is_number: im = im._to_mpmath(prec, allow_ints=False)._mpf_ imprec = prec raise NotImplementedError print("### input", x) print("### output", to_str(r[0] or fzero, 50)) print("### raw", r) # r[0], r[2] print() else: # convert (approximately) from given tolerance; # the formula here will will make 1e-i rounds to 0 for # i in the range +/-27 while 2e-i will not be chopped chop_prec = int(round(-3.321*math.log10(chop) + 2.5)) if chop_prec == 3: chop_prec -= 1 check_target(x, r, prec)
class EvalfMixin(object): """Mixin class adding evalf capabililty."""
__slots__ = []
def evalf(self, n=15, subs=None, maxn=100, chop=False, strict=False, quad=None, verbose=False): """ Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:
subs=<dict> Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}. The substitutions must be given as a dictionary.
maxn=<integer> Allow a maximum temporary working precision of maxn digits (default=100)
chop=<bool> Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool> Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str> Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad='osc'.
verbose=<bool> Print debug information (default=False)
"""
raise TypeError('subs must be given as a dictionary')
# for sake of sage that doesn't like evalf(1) from sympy.core.expr import _mag rv = self.evalf(2, subs, maxn, chop, strict, quad, verbose) m = _mag(rv) rv = rv.round(1 - m) return rv
_create_evalf_table() 'strict': strict, 'verbose': verbose} options['quad'] = quad # Fall back to the ordinary evalf # If the result is numerical, normalize it # Probably contains symbols or unknown functions else: else:
n = evalf
def _evalf(self, prec): """Helper for evalf. Does the same thing but takes binary precision"""
def _eval_evalf(self, prec):
def _to_mpmath(self, prec, allow_ints=True): # mpmath functions accept ints as input else: return make_mpf(fzero) return make_mpf(v._mpf_) # Number + Number*I is also fine re = from_int(re.p) re = re._mpf_ else: if allow_ints and im.is_Integer: im = from_int(im.p) elif im.is_Float: im = im._mpf_ else: raise ValueError(errmsg) return make_mpc((re, im))
def N(x, n=15, **options): """ Calls x.evalf(n, \*\*options).
Both .n() and N() are equivalent to .evalf(); use the one that you like better. See also the docstring of .evalf() for information on the options.
Examples ========
>>> from sympy import Sum, oo, N >>> from sympy.abc import k >>> Sum(1/k**k, (k, 1, oo)) Sum(k**(-k), (k, 1, oo)) >>> N(_, 4) 1.291
""" return sympify(x).evalf(n, **options) |