5.3 Symbolic Computations
111
that all the 100 questions actually get asked, and also that points are given correctly.
The simplicity of the present program allows this to be done while running it.
Experienced programmers, however, usually write dedicated code for such testing.
How to do this for implementations of numerical methods, will be presented later
(see Chap. 6).
Note that, even if some error handling can be implemented by use of
if-elif-else constructions, exception handling allows better programming, and
is the preferred and modern way of handling errors. The recommendation to novice
programmers is therefore to develop the habit of using try-except constructions.
5.3 Symbolic Computations
Even though the main focus in this book is programming of numerical methods,
there are occasions where symbolic (also called exact or analytical) operations are
useful.
5.3.1 Numerical Versus Symbolic Computations
Doing symbolic computations means, as the name suggests, that we do computations with the symbols themselves rather than with the numerical values they
could represent. Let us illustrate the difference between symbolic and numerical
computations with a little example. A numerical computation could be
x = 2
y = 3
z = x*y
print(z)
which will make the number 6 appear on the screen.
A symbolic counterpart of this code could be written by use of the SymPy
package 2 (named sympy in Python):
import sympy as sym
x, y = sym.symbols(’x y’) # define x and y as a mathematical symbols
z = x*y
print(z)
which causes the symbolic result x*y to appear on the screen. Note that no numerical
value was assigned to any of the variables in the symbolic computation. Only the
symbols were used, as when you do symbolic mathematics by hand on a piece of
paper. Note also how symbol names must be declared by using symbols.
2 SymPy (http://docs.sympy.org/latest/index.html) is included in Anaconda. In case you have not
installed Anaconda, you may have to install SymPy separately.
Précédent

- 132/350

Suivant