Maths encyclopedia and lessons  
Search

Mathematics Encyclopedia and Lessons

 
     
 

Lessons

Popular
Subjects

algebra
arithmetic
calculus
equations
geometry
differential equations
trigonometry
number theory
probability theory
more
 

References

applied mathematics
mathematical games
mathematicians
more
 
 

SciPy

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 sponsored by Enthought . It is currently distributed under the BSD license («just don't sue us»).

Contents

Data structures

The basic data structure in SciPy is the array. The SciPy project is slowly moving from the original NumPy Numeric array type to the newer Numarray implementation.

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 graphics packages:

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

Ultimately, their functionalities shall be merged in scipy.plt. As of November 2004, scipy.plt is still missing advanced graphics capabilities such as 3-d-plotting.

The following sample shows a SciPy session with a simple plot:

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

If SciPy is used from a wxPython shell such as Boa constructor or PyCrust , the above sample simplifies to

>>> from scipy import plt
>>> plt.plot((1,2,3))

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

01-04-2007 01:18:14
The contents of this article are licensed from Wikipedia.org
under the GNU Free Documentation License. How to see transparent copy