Page - 111 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 111 -
Text of the Page - 111 -
5.3 SymbolicComputations 111
thatall the100questionsactuallygetasked,andalso thatpointsaregivencorrectly.
The simplicity of the present program allows this to be done while running it.
Experiencedprogrammers,however,usually write dedicated code for such testing.
How to do this for implementations of numerical methods, will be presented later
(seeChap.6).
Note that, even if some error handling can be implemented by use of
if-elif-elseconstructions, exception handling allows better programming,and
is thepreferredandmodernwayofhandlingerrors.The recommendationtonovice
programmersis therefore todevelopthehabitofusingtry-exceptconstructions.
5.3 SymbolicComputations
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 NumericalVersusSymbolicComputations
Doing symbolic computations means, as the name suggests, that we do compu-
tations with the symbols themselves rather than with the numerical values they
could represent. Let us illustrate the difference between symbolic and numerical
computationswitha little example.A numericalcomputationcouldbe
x = 2
y = 3
z = x*y
print(z)
whichwill make thenumber6appearon thescreen.
A symbolic counterpart of this code could be written by use of the SymPy
package2 (namedsympy 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)
whichcausesthesymbolicresultx*y toappearonthescreen.Notethatnonumerical
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.Notealso howsymbolnamesmust bedeclaredbyusingsymbols.
2 SymPy (http://docs.sympy.org/latest/index.html) is included in Anaconda. In case you have not
installedAnaconda, you may have to installSymPy separately.
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik