NumPy
Numpy
Documentation
Buffer Protocol
Compiled Code
Upgrading
Older Packages

Download NumPy

Download the Guide

NumPy

NumPy is the fundamental package needed for scientific computing with Python. It contains:

  • a powerful N-dimensional array object
  • sophisticated broadcasting functions
  • basic linear algebra functions
  • basic Fourier transforms
  • sophisticated random number capabilities
  • tools for integrating Fortran code.
  • tools for integrating C/C++ code.
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.


Documentation

See the numpy and scipy documentation page.

There is a book available "Guide to NumPy" which details NumPy.

Questions?  Ask them at the numpy-discussion@scipy.org mailing list.

Much of the documentation for Numeric and Numarray is applicable to the new NumPy package.  However, there are significant feature improvements.  A complete guide to the new system has been written by the primary developer, Travis Oliphant. It is now in the public domain. 
Other Documentation is available at the scipy website and in the docstrings (which can be extracted using pydoc). Free Documentation for Numeric (most of which is still valid) is here or as a pdf file.   Obviously you should replace references to Numeric in that document with numpy (i.e. instead of "import Numeric", use "import numpy"). 



The Buffer Protocol

Out of developer discussions between developers of Numarray, Numeric, and NumPy has arisen the concept of a buffer protocol to allow sharing of binary data between Python modules. By defining a common data structure, the protocol lets producers and consumers share binary data. For example, an image processing library can operate on numpy arrays without importing numpy, and the output could be displayed with a GUI module supporting buffer objects. This would all occur without copying the binary data or the modules knowing anything about each other beyond the buffer protocol. PEP 3118 describes the protocol, which was accepted and is now part of the Python 2.6/3.0 core language.

Of course, numpy makes a good interoperation library to support the buffer protocol. Numpy can both consume and produce protocol-supporting objects. A numpy ndarray can be constructed as a view of an object supporting the buffer protocol, and a numpy ndarray supports buffer object views of its data.

Although superceded by PEP 3118, the legacy array interface is still used in several places, and is documented here.

Cython natively supports numpy buffer objects (see the Cython numpy tutorial), and provides a way to write code that supports the buffer protocol with Python versions older than 2.6 because it has a backward-compatibile implementation utilizing the legacy array interface.



Interfacing to Compiled Code

There are several ways to use NumPy to interface with compiled code including f2py, swig, ctypes, pyrex, and Cython, as well as the NumPy C-API.
  • f2py uses interface files which can be auto-generated and tweaked by hand to produce nice-looking interfaces to Fortran code.
  • swig requires the numpy.i file in order to integrate with numpy. More information about the swig interface to numpy is here.
  • ctypes is included in Python 2.5 and numpy has a ctypes attribute and a ctypes library which makes it easy to use NumPy array objects as generic chunks of memory for ctypes.
  • pyrex is a Python-like language for writing C-extensions. Look at the mtrand code for an example of using NumPy arrays in pyrex extensions.
  • Cython is a fork of pyrex. It includes native numpy buffer object support. (See the tutorial.)



Upgrading from historical implementations

NumPy derives from the old Numeric code base and can be used as a replacement for Numeric.   It also adds the features introduced by Numarray and can also be used to replace Numarray. 

Numeric users should find the transition relatively easy (although not without some effort).  There is a module (numpy.oldnumeric.alter_code1) that can make most of the necessary changes to your Python code that used Numeric to work with NumPy's Numeric compatibility module.

Users of numarray can also transition their code using a similar module (numpy.numarray.alter_code1) and the numpy.numarray compatibility layer.

C-code written to either package can be easily ported to NumPy using "numpy/oldnumeric.h" and "numpy/libnumarray.h" for the Numeric C-API and the Numarray C-API respectively. Sourceforge download site


For about 6 months at the end of 2005, the new package was called SciPy Core (not to be confused with the full SciPy package which remains a separate package), and so you will occasionally see references to SciPy Core floating around.    It was decided in January 2006 to go with the historical name of NumPy for the new package.  Realize that NumPy (module name numpy) is the new name.   Because of the name-change, there were a lot of dicussions that took place on scipy-dev@scipy.org and scipy-user@scipy.org.  If you have a question about the new system, you may wish to run a search on those mailing lists as well as the main NumPy list (numpy-discussion@lists.sourceforge.net)




Older Array Packages

It may take months for the large code base that uses Numeric and/or Numarray to transition to the new NumPy system.   Links to the older packages are provided here.   New users should start out with NumPy.

Numeric (version 24.2)

Numeric was the first arrayobject built for Python.  It has been quite successful and is used in a wide variety of settings and applications.   Maintenance has ceased for Numeric, and users should transisition to NumPy as quickly as possible.   There is a module called numpy.oldnumeric.alter_code1 in NumPy that can make the transition to NumPy easier (it will automatically perform the search-and-replace style changes that need to be made to python code that uses Numeric to make it work with NumPy).

Documentation for Numeric is here or as a pdf file
Sourceforge Numeric Download Page

Numarray

Numarray is another implementation of an arrayobject for Python written after Numeric and before NumPy. Sponsors of numarray have indicated they will be moving to NumPy as soon as is feasible for them so that eventually numarray will be phased out (probably sometime in 2007). This project shares some of the resources with the Numeric sourceforge site but maintains its own web page at http://www.stsci.edu/resources/software_hardware/numarray
Sourceforge Numarray Download Page

Links

NumPy Sourceforge Home Page
SciPy Home Page

Visitors since February 9, 2005.

For web-page improvements, send mail to Travis Oliphant:  oliphant.travis@ieee.org