Evaluating symbolic expressions

In the context of scientific computing, there is often the need of first making symbolic manipulations and then converting the symbolic result into a floating-point number .

The central tool for evaluating a symbolic expression is evalf. It converts symbolic expressions to floating-point numbers by using the following:

pi.evalf()   # returns 3.14159265358979

The data type of the resulting object is Float (note the capitalization), which is a SymPy data type that allows floating-point numbers with an arbitrary number of digits (arbitrary precision). The default precision corresponds to 15 digits, but it can be changed by giving evalf an extra positive integer argument specifying the desired precision in terms the numbers of digits,

pi.evalf(30)   # returns  3.14159265358979323846264338328

A consequence of working with arbitrary precision is that numbers can be arbitrary small, that is, the limits of the classical floating-point representation are broken; refer Floating Point Numbers section in Chapter 2, Variables and Basic Types.

Interestingly enough,  evaluating a SymPy function with an input of type Float returns a Float with the same precision as the input. We demonstrate the use of this fact in a more elaborated example from numerical analysis.

Example: A study on the convergence order of Newton's Method

An iterative method with iterates xn is said to converge with order q with Example: A study on the convergence order of Newton's Method, if there exists a positive constant C such that

Example: A study on the convergence order of Newton's Method.

Newton's method when started with a good initial has order q = 2, and for certain problems, even q = 3. Newton's method when applied to the problem arctan(x) = 0 gives the following iteration scheme:

Example: A study on the convergence order of Newton's Method

which converges cubically; that is q = 3.

This implies that the number of correct digits triples from iteration to iteration. To demonstrate cubic convergence and to numerically determine the constant C is hardly possible with the standard 16-digit float data type.

The following code, uses SymPy together with high-precision evaluation instead and puts a study on cubic convergence to the extreme:

x = sp.Rational(1,2)
xns=[x]

for i in range(1,9):
    x = (x - sp.atan(x)*(1+x**2)).evalf(3000)
    xns.append(x)

The result is depicted in the next figure (Figure 15.4) which shows that the number of correct digits triples from iteration to iteration.

Example: A study on the convergence order of Newton's Method

  Figure 15.4: A study on the convergence of Newton's method applied to arctan(x)=0

This extreme precision requirement (3,000 digits!) enables us to evaluate seven terms of the preceding sequence to demonstrate cubic convergence in the following way:

# Test for cubic convergence
print(array(abs(diff(xns[1:]))/abs(diff(xns[:-1]))**3,dtype=float64))

The result is a list of seven terms which let us assume that C = 2/3:

[ 0.41041618, 0.65747717, 0.6666665,  0.66666667, 0.66666667, 0.66666667, 0.66666667]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.8