Seite - 16 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 16 -
Text der Seite - 16 -
16 1 TheFirstFewSteps
call. This sounds good to us, so based on what we were told, we start writing our
programas
from numpy import exp
x = exp([0, 1, 2]) # do all 3 calculations
print(x) # print all 3 results
Thescript runswithoutanyproblemsand theprintednumbersseem fine,
[ 1. 2.71828183 7.3890561 ]
Movingon,wewant to test anumberof functionsfromthemath library(cos,sin,
tan, etc.). Since we foresee testing quite many functions, we choose the “lazy”
import techniqueandplan toextend thecodewithone functionat a time.
Extendingtheprogramwith asimpleusageof thecos function, it reads
from numpy import exp
from math import *
x = exp([0, 1, 2]) # do all 3 calculations
print(x) # print all 3 results
y = cos(0)
print(y)
Runningthisversionof thescript,however,wegeta longerprintout (mostofwhich
is irrelevantat thispoint)endingwith
TypeError: a float is required
Clearly, somethinghasgonewrong!But why?
Theexplanation is the following.With thesecond import statement, i.e.,
from math import *
all items from mathwere imported, including a function from math that is also
namedexp. That is, there are two functions in play here that both go by the name
exp!Oneexp functionis foundin thenumpy library,while theotherexp functionis
found in themath library, and the implementationsof these two are different. This
now becomes a problem, since the last imported exp function silently “takes the
place” of the previous one, so that the nameexp hereafter will be associated with
theexp functionfrommath!Thus,whenPythoninterpretsx = exp([0, 1, 2]),
it tries to useexp frommath for the calculations, but that version ofexp can only
take a single number (real or integer) as input argument, not several (asexp from
numpy can). This mismatch then triggers the error message12 and causes program
execution tostopbefore reachingy = cos(0).
Similar name conflicts may arise also with other functions thanexp, since a lot
of itemsappearwith identicalnames in different libraries (e.g., alsocos,sin,tan,
and many more, exist with different implementations in both numpy and math).
Thefact thatprogrammersmaycreate,andshare, theirownlibrariescontainingself
12 It should be mentioned here, that error messages can not always be very accurate. With
some experience, however, you will find them very helpful at many occasions. More about error
messages later (Sect.1.7).
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik