Book Image

Scientific Computing with Python 3

By : Claus Führer, Jan Erik Solem, Olivier Verdier
Book Image

Scientific Computing with Python 3

By: Claus Führer, Jan Erik Solem, Olivier Verdier

Overview of this book

Python can be used for more than just general-purpose programming. It is a free, open source language and environment that has tremendous potential for use within the domain of scientific computing. This book presents Python in tight connection with mathematical applications and demonstrates how to use various concepts in Python for computing purposes, including examples with the latest version of Python 3. Python is an effective tool to use when coupling scientific computing and mathematics and this book will teach you how to use it for linear algebra, arrays, plotting, iterating, functions, polynomials, and much more.
Table of Contents (23 chapters)
Scientific Computing with Python 3
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Acknowledgement
Preface
References

Scripts and modules


A collection of statements in a file (which usually has a py extension), is called a script. Suppose we put the contents of the following code into a file named smartscript.py:

def f(x):
    return 2*x + 1
z = []
for x in range(10):
    if f(x) > pi:
        z.append(x)
    else:
        z.append(-1)
print(z)

In a Python or IPython shell, such a script can then be executed with the exec command after opening and reading the file. Written as a one-liner it reads:

exec(open('smartscript.py').read())

The IPython shell provides the magic command %run as a handy alternative way to execute a script:

%run smartscript

Simple modules - collecting functions

Often one collects functions in a script. This creates a module with additional Python functionality. To demonstrate this, we create a module by collecting functions in a single file, for example smartfunctions.py:

def f(x):
    return 2*x + 1
def g(x):
    return x**2 + 4*x - 5
def h(x):
    return 1/f(x)
  • These functions can now be used by any external script or directly in the IPython environment.
  • Functions within the module can depend on each other.
  • Grouping functions with a common theme or purpose gives modules that can be shared and used by others.

Again, the command exec(open('smartfunctions.py').read()) makes these functions available to your IPython shell (note that there is also the IPython magic function run). In Python terminology, one says that they are put into the actual namespace.

Using modules and namespaces

Alternatively, the modules  can be imported by the command import. It creates a named namespace. The command from puts the functions into the general namespace:

import smartfunctions
print(smartfunctions.f(2))      # 5

from smartfunctions import g    #import just this function
print(g(1)) # 0
  
from smartfunctions import *    #import all
print(h(2)*f(2))                # 1.0

Tip

Import

The commands import and from  import the functions only once into the respective namespace. Changing the functions after the import has no effect for the current Python session. More on modules can be found in section Modules of Chapter 11, Namespaces, Scopes and Modules.