Given that Python is an interpreted programming language, rather than a fast compiled
language, many people do not consider it for writing programs that involve extensive
numerical work. While Python programs are certainly slower to execute than the
exists in Python and has the inherent advantages of the language; it is easy for people to
use and conveniently links to other helpful data structures. Of course speed of calculation
may not be so important, for a scientific investigation it may not matter if something takes
1 second or 0.1 second to run. Fortunately, computers get faster and the Python interpreter
becomes improved, so you can do quite a bit of numerical work without concern.
However, if calculation speed really is important in a given situation then there are a few
things you can do to make things faster while still keeping the convenience of Python. For
interpreted language with speedy subroutines. More recently the language Cython has
helped make C extensions very easy to write. Cython is a Python-like language, and
virtually all Python programs can be interpreted by it, without alteration, but the language
ultimately generates C code that can be compiled. Cython can be used to call fast library
code written in pure C, and can incorporate a mixture of Python and C data structures in
the same code; although less flexible, the C data structures are very efficient. Writing C
extensions and Cython modules is discussed in
Chapter 27
.
Python includes standard arithmetic operations as part of the core functionality: add,
multiply etc. There
is an additional module, math, which always comes packaged with
Python and which provides further numerical functionality: logarithms, trigonometry etc.
For numerical calculations that are not especially intensive, the core functionality and the
math module will often suffice. There has been a history of trying to provide modules for
quick numerical algorithms in Python. The first attempt, begun in 1995, was called
Numeric, and the second attempt was called Numarray. These two are now deemed to be
obsolete, but the third attempt, begun in 2005, is called NumPy (
http://numpy.scipy.org/
),
incorporates elements from the earlier attempts and will hopefully last longer.
NumPy provides support for basic numerical operations, with an emphasis on
specifying calculations that operate on a whole array of numbers at once. As will be
discussed below, its operations include functionality for random numbers, linear algebra
and Fourier transforms. It is implemented in C underneath,
1
and thus is quick to run, but
can naturally be accessed in Python. NumPy is relatively easy to use because you are still
working with Python commands, but the way that some things work, especially how to
think about numeric array operations, can take some learning. For serious linear algebra
work in Python, NumPy is the method of choice. There is another closely related package,
called SciPy
2
(Scientific Python), which adds some higher-level numerical capabilities,
such
as integration, optimisation and signal processing. NumPy and SciPy are not part of
the standard Python software release, so require a separate download and installation,
although modern download managers ought to make this fairly easy to do. See
http://www.cambridge.org/pythonforbiology
for details of where to download SciPy and
NumPy. In some sense these packages could be deemed to be the Pythonic answer to the
analogous capabilities in, for example, the MATLAB system.
3
Do'stlaringiz bilan baham: