Navigation

Exact SymPy expressions can be converted to floating-point approximations
(decimal numbers) using either the .evalf() method or the N() function.
N(expr,<args>) is equivalent to sympify(expr).evalf(<args>).

If the expression contains symbols or for some other reason cannot be evaluated
numerically, calling .evalf() or N() returns the original expression, or
in some cases a partially evaluated expression. For example, when the
expression is a polynomial in expanded form, the coefficients are evaluated:

High-precision calculations can be slow. It is recommended (but entirely
optional) to install gmpy (http://code.google.com/p/gmpy/), which will
significantly speed up computations such as the one above.

As the last example shows, some Python floats are only accurate to about 15
digits as inputs, while others (those that have a denominator that is a
power of 2, like .125 = 1/4) are exact. To create a Float from a
high-precision decimal number, it is better to pass a string, Rational,
or evalf a Rational:

The precision of a number determines 1) the precision to use when performing
arithmetic with the number, and 2) the number of digits to display when printing
the number. When two numbers with different precision are used together in an
arithmetic operation, the higher of the precisions is used for the result. The
product of 0.1 +/- 0.001 and 3.1415 +/- 0.0001 has an uncertainty of about 0.003
and yet 5 digits of precision are shown.

>>> Float(0.1,3)*Float(3.1415,5)0.31417

So the displayed precision should not be used as a model of error propagation or
significance arithmetic; rather, this scheme is employed to ensure stability of
numerical algorithms.

N and evalf can be used to change the precision of existing
floating-point numbers:

When the input to N or evalf is a complicated expression, numerical
error propagation becomes a concern. As an example, consider the 100’th
Fibonacci number and the excellent (but not exact) approximation \(\varphi^{100} / \sqrt{5}\)
where \(\varphi\) is the golden ratio. With ordinary floating-point arithmetic,
subtracting these numbers from each other erroneously results in a complete
cancellation:

N and evalf keep track of errors and automatically increase the
precision used internally in order to obtain a correct result:

>>> N(fibonacci(100)-GoldenRatio**100/sqrt(5))-5.64613129282185e-22

Unfortunately, numerical evaluation cannot tell an expression that is exactly
zero apart from one that is merely very small. The working precision is
therefore capped, by default to around 100 digits. If we try with the 1000’th
Fibonacci number, the following happens:

>>> N(fibonacci(1000)-(GoldenRatio)**1000/sqrt(5))0.e+85

The lack of digits in the returned number indicates that N failed to achieve
full accuracy. The result indicates that the magnitude of the expression is something
less than 10^84, but that is not a particularly good answer. To force a higher working
precision, the maxn keyword argument can be used:

Normally, maxn can be set very high (thousands of digits), but be aware that
this may cause significant slowdown in extreme cases. Alternatively, the
strict=True option can be set to force an exception instead of silently
returning a value with less than the requested accuracy:

By default, the tanh-sinh quadrature algorithm is used to evaluate integrals.
This algorithm is very efficient and robust for smooth integrands (and even
integrals with endpoint singularities), but may struggle with integrals that
are highly oscillatory or have mid-interval discontinuities. In many cases,
evalf/N will correctly estimate the error. With the following integral,
the result is accurate but only good to four digits:

Infinite series use direct summation if the series converges quickly enough.
Otherwise, extrapolation methods (generally the Euler-Maclaurin formula but
also Richardson extrapolation) are used to speed up convergence. This allows
high-precision evaluation of slowly convergent series:

The Euler-Maclaurin formula is also used for finite series, allowing them to
be approximated quickly without evaluating all terms:

>>> Sum(1/k,(k,10000000,20000000)).evalf()0.693147255559946

Note that evalf makes some assumptions that are not always optimal. For
fine-tuned control over numerical summation, it might be worthwhile to manually
use the method Sum.euler_maclaurin.

Special optimizations are used for rational hypergeometric series (where the
term is a product of polynomials, powers, factorials, binomial coefficients and
the like). N/evalf sum series of this type very rapidly to high
precision. For example, this Ramanujan formula for pi can be summed to 10,000
digits in a fraction of a second with a simple command:

The function nsimplify attempts to find a formula that is numerically equal
to the given input. This feature can be used to guess an exact formula for an
approximate floating-point input, or to guess a simpler formula for a
complicated symbolic input. The algorithm used by nsimplify is capable of
identifying simple fractions, simple algebraic expressions, linear combinations
of given constants, and certain elementary functional transformations of any of
the preceding.

Optionally, nsimplify can be passed a list of constants to include (e.g. pi)
and a minimum numerical tolerance. Here are some elementary examples: