NumPy
One of the most important libraries for scientific work in Python is numpy
. It provides methods and algorithms for numerical calculations and is based on many established libraries written in C and Fortran. This makes working with Python much easier and has contributed significantly to its success in science. Instead of writing detailed hardware-related code, we often only write glue code
, which describes the data flow and provides the information as required by the efficient libraries.
In a way, numpy
builds a bridge between the easily readable and maintainable Python code and the hardware-related efficiency. One of the ways this is achieved is that numpy
has its own data structure that is not part of the standard library: the array
. The easiest way to visualise this is as a list, which may be concatenated to describe a matrix or a higher tensor.
In addition to efficient data types, numpy
provides a variety of specialised functions: from linear algebra, to interpolation, polynomial functions, Fourier transformations, random numbers and much more.
'numpy' must be explicitly loaded as a module, as it is not part of the standard functionality. The convention is that it is loaded as follows:
import numpy as np
This loads numpy
under the alias np
. Functions or data types with np
used in the following therefore require this import. For the sake of readability, however, this import is no longer listed in later code examples.
In order to utilise the special efficiency of numpy
, it is important to use the internal functions. As soon as a regular Python loop is used, the advantages offered by the library are lost.