NumComputations

Author: Thoril Kristiansen
Author: Kristin Pedersen
Date: 11.05.07

Contents

1   Introduction

NumComputations is a sort of library for numerical computations, implemented in Python. The functionality so far consists of numerical integration, finding a root, and fitting data points using the least square method. This library is created as a project in the spring semester of 2007, for our course of high level languages, INF5660, at UiO.

2   File structure and functionality

The functions are implemented in pure python, using Fortran extension modules for the calculations themselves. In the main directory NumComputations, the script numComputationsGUI.py resides. This is a script for graphically interfacing all the other functions. It is a sort of computational steering script, and you can and should run all functionality from here.

There are in addition three subdirectories, numInt, numRoot and numLeastSquares. The structure of the main directory is thus as follows:

NumComputations

      numComputationsGUI.py
      *numInt  
               *src
               *verify
               *doc

      *numRoot
               *src
               *verify
               *doc

      *numLeastSquares
               *src
               *verify
               *doc

All source code is gathered in the src directories.

2.1   numInt

numInt.py calculates the integral of a function from a to b. numInt's class NumInt uses 5 different formulas to calculate the integral:

  • The trapezoidal method
  • Simpson's rule
  • Simpson's 3/8 rule
  • Boole's rule
  • The Gauss-Legendre N-point rule

where the latter is implemented in Python as GaussLegendre.py, and the other in the fortran file numint.f. The numInt.NumInt instance finally gets a class attribute message, which is the answer from the different formulas.

2.2   numRoot

numRoot.py finds the root of a function if in the interval given. There are two methods here, both implemented in Fortran:

  • The bisection method
  • Regula Falsi

The root is saved as a class attribute, and is set to None if no root exsists. If the method fails to find the root, it is set to 100000.

2.3   numLeastSquares

numLeastSquares.py uses the method of least squares to fit a set of data points. The set is read from a file whose name is given by the user. A constant, linear or quadratic function is created by the aid of Fortran, solving a system of linear equations. If run through the GUI, this function is now plotted together with the datapoints. The user can then also choose to save a copy of the plot. For further details on the least square method, we refer to [2].

3   Installation

First unpack the tarball like this:

$ tar -zxvf NumComputations-1.0.tar.gz

then use standard distutil to install:

$ python setup.py install     

4   Usage

Typing:

$ python numComputationsGUI.py [option], 

starts up the GUI. The optional argument option can be either int, num or nls.

Each of the other python files are modules. Running them as scripts results in running the default test case. Rather import the module in a script and make an instance of the class in the module. The answer is now a class attribute of this instance. For function signatures, load the documentation for the module in the doc subdirectory.

Fortran extension modules is built automatically, so there should be no need to do this yourself. However, the task is accomplished by typing:

$ sh buildModule.sh

in src/

5   Regression tests

All Python programs are equipped with a regression test, testing all of the functions therein. Also, every Fortran extension module has its own regression test, testing only this small but important unit of code. The tests for numRoot, numInt and numLeastSquares are located in their respective verify subdirectories. All the regression test are run by the simple command:

$ regression.py verify .

in the main directory, but they can of course also be run one by one.

6   Documentation generation

Using docutils you can create an HTML or LaTeX version of this file like this:

$ rst2html.py README README.html

or:

$ rst2latex.py README README.tex,

respectively. The .pdf is then created and read by typing:

$ latex README.tex
$ dvipdf README.dvi
$ acroread README.pdf 

7   Download

Get this library at http://folk.uio.no/krispe/NumComputations.tar.gz.

8   References

[1]Hans Petter Langtangen, "Python Scripting for Computational Science"
[2]John H. Mathews, Kurtis D. Fink, "Numerical Methods Using Matlab"
[3]H. P. Langtangen, A. Tveito, X. Cai, T. Nielsen, B. F. Nilsen, "Introduction to Scientific Computing"