Page - 113 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 113 -
Text of the Page - 113 -
5.4 MakingOurOwnModule 113
package is that the results cannot automatically be imported into your code and
used for further analysis. On the other hand, WolframAlpha has the advantage that
it displays many additional mathematical results related to the given problem. For
example, if we type 2x + 3x - y in WolframAlpha, it not only simplifies the
expression to 5x - y, but it also makes plots of the functionf(x,y) = 5x−y,
solves the equation5x−y= 0, and calculates the integral∫ ∫(5x+y)dxdy.The
commercial Pro version also offers a step-by-step demonstration of the analytical
computationsthatsolvetheproblem.Youareencouragedtotryout thesecommands
inWolframAlpha:
• diff(x^2, x)ordiff(x**2, x)
• integrate(cos(x), x)
• simplify((x**2 + x**3)/x**2)
• limit(sin(x)/x, x, 0)
• solve(5*x - 15, x)
WolframAlpha is very flexible with respect to syntax. In fact, WolframAlpha will
use your input to guess what you want it to do! Depending on what you write, it
may be more or less easy to do that guess, of course. However, in WolframAlpha’s
response, you are also told how your input was interpreted, so that you may adjust
your input ina second try.
Another impressive tool for symbolic computations is Sage,6 which is a very
comprehensive package with the aim of “creating a viable free open source
alternative to Magma, Maple, Mathematica and Matlab”. Sage is implemented in
Python. Projects with extensive symbolic computations will certainly benefit from
exploringSage.
5.4 MakingOurOwnModule
As we knowby now,Pythonhasa hugecollectionof usefulmodulesand packages
written by clever people. This far, we have experienced how these libraries (math,
numpy,matplotlib,etc.)couldsimplifyourownprogramming,makingreadyand
professionalcodeavailable throughsimpleimportstatements.This isverygood,but
itgetsevenbetter,sinceit isstraightforward(whetherweareprogrammingnewbies
ornot) toalso createmodulescontainingourowncode.
What is a module then, really? The truth is, that we may regard any of the
Python scripts we have presented in this book as a module! In fact, any text file
withextension.py thatcontainsPythoncodewrittenwitha texteditor, isamodule.
If the file name ismy_module.py, then the module name ismy_module. Up until
now, we have written.pyfiles for execution as programs. To design and use such
filesasmodulefiles, however, therearea few thingswe bettergetconsciousabout.
Tobringacross theessentialpoints,wewilldevelopourownlittledemonstration
module for vertical motion, named vertical_motion (surprising!). The motion
is of the kind we have addressed also previously in this book, i.e., a special case
of projectile motion, in which an object starts out with some vertical velocity, and
6 http://sagemath.org/.
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