Information Technology Reference
In-Depth Information
PATH
variable, either by executing
path(path, '/home/me/matlab/int')
inside Matlab or by setting the
MATLABPATH
environment variable in the start-up
file. In case your start-up file is
.bashrc
(Unix Bash environment), the relevant
statement becomes
export MATLABPATH=$MATLABPATH:/home/me/matlab/int
The current working directory is always in Matlab's search path.
To summarize, one collects each Matlab library function to be offered to users in
a separate M-file, one organizes these M-files in a suitable directory structure, and
Matlab's search path must be updated so that Matlab searches these directories for
M-files.
Python Libraries
Python libraries are composed by
modules
and
packages
. A module is a file with
Python functions and classes, whereas a package is a collection of modules, often
with a tree structure (actually reflecting the organization of modules files in a
directory tree).
To make a Python module, just place the desired functions in a file. In our exam-
ple on numerical integration functions, we collect the functions in one file, say,
numint.py
, and place the file in a directory where Python looks for modules.
The main program for experimenting with different values of
n
and different
methods can look like this:
#!/usr/bin/env python
import numint # give access to library for numerical integration
from math import
*
def f1(x):
return exp(-x
*
x)
*
log(1+x
*
sin(x))
a=0;b=2
n_values = (10, 100, 1000, 10000)
for n in n_values:
for method in (Trapezoidal, Simpson, GaussLegendre,
GaussLobatto):
result = method(a, b, f1, n)
print method.__name__, ":", result
In this code example,
method
is a function. We can run through a list of functions,
call an entry, and write the name of an entry. This flexibility is not found in the other
languages we address in this chapter. As a result, Python makes code organization
and experimentation very simple and convenient.
The
import
statement causes Python to search for a file
numint.py
in a set
of directories specified by the internal Python variable
sys.path
or the envi-
ronment variable
PYTHONPATH
. Suppose you stored
numint.py
in the directory