SciPy

From Free net encyclopedia

Template:Infobox Software

SciPy is an open source library of high-quality scientific tools for the Python programming language. It must not be confused with the concurrent project Scientific Python.

SciPy uses the array handling of the NumPy or/and Numarray modules. Furthermore, it contains modules for graphics and plotting, optimization, integration, special functions, signal and image processing, genetic algorithms, ODE solvers, and others.

SciPy is targeted towards similar applications as MATLAB and Scilab.

SciPy is currently distributed under the BSD license and its development is sponsored by Enthought.

Contents

Data structures

The basic data structure in SciPy is a multidimensional array. The SciPy project was slowly moving from the original NumPy Numeric array type to the newer Numarray implementation. However, Numarray has since been abandoned, and the Numeric package is being merged directly into SciPy. For more information, see http://numeric.scipy.org/

Modules

Overview

Available subpackages:

  • plt, gplt, xplt: graphics
  • special: Special Functions
  • linalg: Linear algebra routines
    • sparse: Sparse matrix
    • cluster: Vector Quantization / Kmeans
  • fftpack: Discrete Fourier Transform algorithms
  • optimize: Optimization Tools
  • interpolate: Interpolation Tools
  • integrate: Integration routines
  • ga: Genetic Algorithms
  • stats: Statistical Functions
  • cow: Cluster Of Workstations
  • signal: Signal Processing Tools
  • io: Data input and output

Graphics

Currently, SciPy has three builtin plotting packages:

  • scipy.plt, a cross-platform package written from scratch,
  • scipy.gplt, a scipy front-end to the cross-platform plotting tool Gnuplot, and
  • scipy.xplt, based on Gist, therefore limited to systems running X11.

The following example (with scipy.plt) shows a SciPy session with a simple plot:

>>> import gui_thread
>>> gui_thread.start()
>>> from scipy import plt
>>> plt.plot([1,2,3,2])

The scipy.gplt version is simpler:

>>> from scipy import gplt
>>> gplt.plot([1,2,3,2])


The functionality of the three plotting packages is/was ment to be merged in one package scipy.plt. As of November 2004, scipy.plt is still missing advanced graphics capabilities such as 3-d-plotting. As of April 2006, scipy's plotting functionality seem to be outsourced to the Matplotlib project (also known as Pylab).


Image processing capabilities are available through the Python Imaging Library (PIL).

Complex example

The following code demonstrates several capabilities of SciPy: the one-dimensional motion of an object in the Earth's gravity field is calculated by integrating Newton's equation of motion, then fitted by a parabola:

## Initializations:

print "initializing ..."
import gui_thread
gui_thread.start()
from scipy import plt
from numarray import arange
from scipy.integrate import odeint
from scipy.optimize import leastsq
print "running ..."

## Simulate 1-dimensional Newtonian dynamics:

# set times for which we want data (these are NOT the integration steps):
T=arange(0,4.,.01) 

# given acceleration as function of position, velocity or/and time:
def acc(s,v,t):
    return -9.81

# set initial position and velocity:
sv0=[0,10]

# first derivative of [s,v] will be equated with fu:
def fu(sv,t):
	return[sv[1],acc(sv[0],sv[1],t)]

# integrate equation of motion:
SV = odeint(fu,sv0,T)
S=SV[:,0] # obtain s(t) as a slice of sv(t)

## Fit resulting s(t):

# model function (a parabola):
def model(p,t):
    return p[0]+p[1]*t+p[2]*t*t

# initial values for fit parameters:
p0=[.1,.6,4.]

# perform the fit:
def residue(param,datum,t):
    return datum - model(param,t)
fit=leastsq(residue,p0,args=(S,T))
p=fit[0] # final fit parameters

# print and plot results:
print "fit result: ", p
Sfit = model(p,T) # fitted parabola
plt.plot(T,S,'bx',T,Sfit,'m-')

External links