Translator's Preface
Hello everyone, here we come to the final part. Enjoy reading!
Navigation:
Math Polynomials
NumPy provides methods for working with polynomials. By passing the list of roots, you can get the coefficients of the equation
>>> np.poly([-1, 1, 1, 10]) array([ 1, -11, 9, 11, -10])
Here, the array returns the coefficients corresponding to the equation:
.
The inverse operation can also be performed: passing the list of coefficients, the root function returns all the roots of the polynomial:
>>> np.roots([1, 4, -2, 3]) array([-4.57974010+0.j , 0.28987005+0.75566815j, 0.28987005-0.75566815j])
Note that in this equation
two roots are imaginary.
The coefficients of the polynomial can be integrated. Consider integrating
at
. Usually the constant C is zero:
>>> np.polyint([1, 1, 1, 1]) array([ 0.25 , 0.33333333, 0.5 , 1. , 0. ])
Similarly, derivatives can be taken:
>>> np.polyder([1./4., 1./3., 1./2., 1., 0.]) array([ 1., 1., 1., 1.])
The functions polyadd, polysub, polymul and polydiv also support summation, subtraction, multiplication and division of the coefficients of the polynomial, respectively.
The polyval function substitutes a given value in a polynomial. Consider a polynomial
with x = 4:
>>> np.polyval([1, -2, 0, 2], 4) 34
Finally, the polyfit function can be used to select (interpolate) a polynomial of a given order to a set of values:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8] >>> y = [0, 2, 1, 3, 7, 10, 11, 19] >>> np.polyfit(x, y, 2) array([ 0.375 , -0.88690476, 1.05357143])
The returned array is a list of polynomial coefficients. More sophisticated interpolation functions can be found in SciPy.
Statistics
In addition to the mean, var, and std functions, NumPy provides some more methods for working with statistical data in arrays.
The median can be found as follows:
>>> a = np.array([1, 4, 3, 8, 9, 2, 3], float) >>> np.median(a) 3.0
The correlation coefficient for some variables is observed several times and can be found from arrays of the form: [[x1, x2, ...], [y1, y2, ...], [z1, z2, ...], .. .], where x, y, z are different quantum observables and numbers indicate the number of “observations”:
>>> a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float) >>> c = np.corrcoef(a) >>> c array([[ 1. , 0.72870505], [ 0.72870505, 1. ]])
We have the returned array c [i, j] which stores the correlation coefficient for the i-th and j-th quantum observables.
Similarly, the covariance moment can be found:
>>> np.cov(a) array([[ 0.91666667, 2.08333333], [ 2.08333333, 8.91666667]])
Random numbers
An important part of each simulation is the ability to generate random numbers. To do this, we use the pseudo-random number generator built into NumPy in the random sub-module. Numbers are
pseudo- random, in the sense that they are generated deterministically from a generating element (seed number), but dispersed in statistical similarity with a random pattern. To generate NumPy uses a special algorithm which has the name Mersenne Twister.
You can set the generating element of a sequence of random numbers as follows:
>>> np.random.seed(293423)
Seed is an integer. Each program that runs with the same seed will generate the same sequence of numbers each time. This may be useful for debugging, but in general we do not need to specify a seed, in fact, when we run the program several times, we want to receive a different sequence of numbers each time. If this command is not executed, the NumPy automatically selects a random seed (based on time), which is different each time the program is started.
An array of random numbers from the half-interval [0.0, 1.0) can be generated as follows:
>>> np.random.rand(5) array([ 0.40783762, 0.7550402 , 0.00919317, 0.01713451, 0.95299583])
The rand function can be used to generate two-dimensional arrays, or you can use the reshape function:
>>> np.random.rand(2,3) array([[ 0.50431753, 0.48272463, 0.45811345], [ 0.18209476, 0.48631022, 0.49590404]]) >>> np.random.rand(6).reshape((2,3)) array([[ 0.72915152, 0.59423848, 0.25644881], [ 0.75965311, 0.52151819, 0.60084796]])
To generate a single random number in the interval [0.0, 1.0):
>>> np.random.random() 0.70110427435769551
To generate a random integer number in the range [min, max), we use the randint (min, max) function:
>>> np.random.randint(5, 10) 9
In each of our examples, we generated numbers from a continuous uniform distribution. NumPy also includes generators for other distributions, such as: Beta, Binomial, chi-square Dirichlet, exponential, Fischer, Gamma, geometric Gambala, hypergeometric, Laplace, Logistic, lognormal, logarithmic, multinomial, multivariate normal, negative binomial, noncentral Chi-square, non-central Fisher, normal (Gauss), Pareto, Poisson, power, Rayleigh, Cauchy, Student t, triangular, Von Mies, Wald, Weibull and Zipf. Consider two examples.
To generate from a discrete Poisson distribution at λ = 6.0,
>>> np.random.poisson(6.0) 5
To generate a number from the normal distribution (Gauss) with the mean value μ = 1.5 and the standard deviation σ = 4.0:
>>> np.random.normal(1.5, 4.0) 0.83636555041094318
To get the number from the normal distribution (μ = 0, σ = 1), without specifying arguments:
>>> np.random.normal() 0.27548716940682932
To generate multiple values, use the size argument:
>>> np.random.normal(size=5) array([-1.67215088, 0.65813053, -0.70150614, 0.91452499, 0.71440557])
A module for generating random numbers can also be used to randomly distribute values in a list. This can be useful if we want to randomly distribute the values in the list:
>>> l = range(10) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> np.random.shuffle(l) >>> l [4, 9, 5, 0, 2, 7, 6, 8, 1, 3]
Note that the shuffle function modifies an already existing array and does not return a new one.
Some additional information
NumPy includes many other features that we did not mention here. In particular, these are functions for working with the discrete Fourier transform, more complex operations in linear algebra, testing arrays for size / dimension / type, dividing and joining arrays, histograms, creating arrays from any data in different ways, creating and operating grid arrays , special values (NaN, Inf), set-operations, creation of different types of special matrices and calculation of special mathematical functions (for example: Bessel functions). You can also see the
NumPy documentation for more precise details.
SciPy Modules
SciPy extends NumPy very well. We will not talk about its details, but consider some of its features. Most of the SciPy features are available after importing the module:
>>> import scipy
The help function will provide useful information about SciPy:
>>> help(scipy)
Help on package scipy:
NAME
scipy
FILE
c:\python25\lib\site-packages\scipy\__init__.py
DESCRIPTION
SciPy --- A scientific computing package for Python
===================================================
Documentation is available in the docstrings and
online at http://docs.scipy.org.
Contents
--------
SciPy imports all the functions from the NumPy namespace, and in
addition provides:
Available subpackages
---------------------
odr --- Orthogonal Distance Regression [*]
misc --- Various utilities that don't have
another home.sparse.linalg.eigen.arpack --- Eigenvalue solver using iterative methods. [*]
fftpack --- Discrete Fourier Transform algorithms[*]
io --- Data input and output [*]
sparse.linalg.eigen.lobpcg --- Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG) [*]
special --- Airy Functions [*]
lib.blas --- Wrappers to BLAS library [*]
sparse.linalg.eigen --- Sparse Eigenvalue Solvers [*]
stats --- Statistical Functions [*]
lib --- Python wrappers to external libraries [*]
lib.lapack --- Wrappers to LAPACK library [*]
maxentropy --- Routines for fitting maximum entropymodels [*]
integrate --- Integration routines [*]
ndimage --- n-dimensional image package [*]
linalg --- Linear algebra routines [*]
spatial --- Spatial data structures and algorithms[*]
interpolate --- Interpolation Tools [*]
sparse.linalg --- Sparse Linear Algebra [*]
sparse.linalg.dsolve.umfpack --- :Interface to the UMFPACK library: [*]
sparse.linalg.dsolve --- Linear Solvers [*]
optimize --- Optimization Tools [*]
cluster --- Vector Quantization / Kmeans [*]
signal --- Signal Processing Tools [*]
sparse --- Sparse Matrices [*]
[*] - using a package requires explicit import (see pkgload)
...
Note that some sub-modules directly need additional imports, which are marked with a star:
>>> import scipy >>> import scipy.interpolate
The functions in each module are well documented in the internal docstring `s and in the official documentation. Most of them directly provide functions for working with numerical algorithms and they are very easy to use. Thus, SciPy can save a huge amount of time in scientific computing, because it provides already written and tested functions.
We will not cover SciPy in detail, but the table below will cover some of its features:
Module | What is used for |
---|
scipy.constants | A set of mathematical and physical constants |
scipy.special | Many special functions for mathematical physics, such as: Airy, elliptic, Bessel, gamma, beta, hypergeometric, parabolic cylinder, Mathieu, spherical wave, Struve, Kelvin. |
scipy.integrate | Functions for working with numerical integration using the methods of trapezoid, Simpson, Romberg and others. Also provides methods for working with full differential equations. |
scipy.optimize | Standard maximum / minimum search methods for working with generic user-defined functions. Included algorithms: Nelder - Mead, Poull ( Powell's ), conjugate gradients, Broyden - Fletcher - Goldfarb - Shanno, least squares, conditional optimization, simulated annealing, brute force, Brent, Newton, bisection, Broyden, Anderson and linear search. |
scipy.linalg | Broader functionality for working with linear algebra than in NumPy. It provides more opportunities for using special and fast functions for specific objects (for example: a three-diagonal matrix). Included methods: search for non-degenerate matrix, search for determinant, solution of linear systems of equations, calculation of norms and pseudoinverse matrix, spectral decomposition, singular decomposition, LU decomposition, Cholesky decomposition, QR decomposition, Schur decomposition and many other mathematical operations for working with matrices. |
scipy.sparse | Functions for working with large sparse matrices |
scipy.interpolate | Methods and classes for interpolating objects that can be used for discrete numeric data. Linear and spline ( Approx. Translator: mathematical representation of smooth curves ) interpolation is available for one- and two-dimensional data sets. |
scipy.fftpack | Methods for processing Fourier transforms. |
scipy.signal | Methods for signal processing, for example: convolution of functions, correlation, discrete Fourier transform, smoothing B-spline, filtering, etc., etc. |
scipy.stats | A large library of statistical functions and distributions for working with data sets. |
A large group of developers continuously continues to develop new SciPy features. A good practical approach is this: if you are thinking about implementing any numeric functions and methods in your code, you can first look at the original SciPy documentation. There is a possibility that this has already been implemented and introduced into SciPy.
Afterword
Our article series has come to an end. Thanks to everyone who read and spent time. I also hope that you learned some useful information and learned something new. Keep developing and learning new things! See you soon.