Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 16 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 16 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 16 -

Image of the Page - 16 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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).
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python