Importing and Calling Routines

General Code Structure

You must have an import statement for each routine you’ll be using. The import statement takes the basic form:

from pyimsl.math.<name> import <name>
from pyimsl.stat.<name> import <name>

For example, the import statement for fftReal looks like:

from pyimsl.math.fftReal import fftReal

As a shortcut you can also import all of the math or stat modules with these commands. Note that this increases the possibilities of namespace collisions and there is additional module loading overhead, so this is not recommended for production code:

from pyimsl.stat.stat import *
from pyimsl.math.math import *

After the import, you may call functions from the numerical library. For example, here is a sample program that uses the fftReal function:

from pyimsl.math.fftReal import fftReal
n = 7
two_pi = 2*3.1415927
p = empty(n)
for k in range(0,n):
   p[k] = cos(k*two_pi/n)
q = fftReal(p)
print q

Some routines use named constants, which are used like enumerated types in other languages. To use these import the named constant, as in this example which uses PERMUTE_COLUMNS:

from pyimsl.stat.permuteMatrix import permuteMatrix, PERMUTE_COLUMNS
a = [[3.0, 5.0, 1.0, 2.0, 4.0], \
     [3.0, 5.0, 1.0, 2.0, 4.0], \
     [3.0, 5.0, 1.0, 2.0, 4.0]]
permutation = [2, 3, 0, 4, 1]
output = permuteMatrix (a, permutation, PERMUTE_COLUMNS)
print output

After importing a function you can obtain basic information on calling it with the command:

help(imslFunctionName)

Running PyIMSL Programs

There are three ways to run PyIMSL programs:

  1. Run Python, and type the PyIMSL commands at the Python prompt (>>>).
python
>>> from pyimsl.math.fftReal import fftReal
>>> result = fftReal (…)
  1. Run Python with the name of a source file as a command-line argument:
python fft_real_test.py
  1. Run Python, and import a command file (note that this will create a .pyc file):
python
>>> import fft_real_test.py

Running Example Programs

Example programs for all of the PyIMSL Math Library routines are available broken down by Chapter in the example directory in the distribution.

To run one of the examples, change your working directory to the example subdirectory containing the example, and type “python [test-name]”. For example, to run the example code for fftReal, you would type:

cd example/math/ch6
python fftReal_ex1.py