Scipy
SciPy Japan 2020 will be a virtual conference to bring together the growing Scientific Python community in Japan to showcase the latest projects, and learn from skilled users and developers. Learn more about the SciPy Conference and Community. Visit the SciPy Community.
- SciPy Tutorial
SciPy, a scientific library for Python is an open source, BSD-licensed library for mathematics, science and engineering. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. SciPy 2021, the 20th annual Scientific Computing with Python conference, will be a virtual conference held July 12-18, 2021. The annual SciPy Conference brings together attendees from industry, academia, and government to showcase their latest projects, learn from skilled users and developers, and collaborate on code development. SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. In particular, these are some of the core packages: NumPy Base N-dimensional array package SciPy library Fundamental library for scientific computing.
- SciPy Useful Resources
- Selected Reading
When a function cannot be integrated analytically, or is very difficult to integrate analytically, one generally turns to numerical integration methods. SciPy has a number of routines for performing numerical integration. Most of them are found in the same scipy.integrate library. The following table lists some commonly used functions.
Sr No. | Function & Description |
---|---|
1 | quad Single integration |
2 | dblquad Double integration |
3 | tplquad Triple integration |
4 | nquad n-fold multiple integration |
5 | fixed_quad Gaussian quadrature, order n |
6 | quadrature Gaussian quadrature to tolerance |
7 | romberg Romberg integration |
8 | trapz Trapezoidal rule |
9 | cumtrapz Trapezoidal rule to cumulatively compute integral |
10 | simps Simpson’s rule |
11 | romb Romberg integration |
12 | polyint Analytical polynomial integration (NumPy) |
13 | poly1d Helper function for polyint (NumPy) |
Single Integrals
The Quad function is the workhorse of SciPy’s integration functions. Numerical integration is sometimes called quadrature, hence the name. It is normally the default choice for performing single integrals of a function f(x) over a given fixed range from a to b.
$$int_{a}^{b} f(x)dx$$
The general form of quad is scipy.integrate.quad(f, a, b), Where ‘f’ is the name of the function to be integrated. Whereas, ‘a’ and ‘b’ are the lower and upper limits, respectively. Let us see an example of the Gaussian function, integrated over a range of 0 and 1.
We first need to define the function → $f(x) = e^{-x^2}$ , this can be done using a lambda expression and then call the quad method on that function.
The above program will generate the following output.
The quad function returns the two values, in which the first number is the value of integral and the second value is the estimate of the absolute error in the value of integral.
Note − Since quad requires the function as the first argument, we cannot directly pass exp as the argument. The Quad function accepts positive and negative infinity as limits. The Quad function can integrate standard predefined NumPy functions of a single variable, such as exp, sin and cos.
Multiple Integrals
The mechanics for double and triple integration have been wrapped up into the functions dblquad, tplquad and nquad. These functions integrate four or six arguments, respectively. The limits of all inner integrals need to be defined as functions.
Double Integrals
The general form of dblquad is scipy.integrate.dblquad(func, a, b, gfun, hfun). Where, func is the name of the function to be integrated, ‘a’ and ‘b’ are the lower and upper limits of the x variable, respectively, while gfun and hfun are the names of the functions that define the lower and upper limits of the y variable.
As an example, let us perform the double integral method.
Scipy Minimize
$$int_{0}^{1/2} dy int_{0}^{sqrt{1-4y^2}} 16xy :dx$$
We define the functions f, g, and h, using the lambda expressions. Note that even if g and h are constants, as they may be in many cases, they must be defined as functions, as we have done here for the lower limit.
The above program will generate the following output.
Scipy Definition
In addition to the routines described above, scipy.integrate has a number of other integration routines, including nquad, which performs n-fold multiple integration, as well as other routines that implement various integration algorithms. However, quad and dblquad will meet most of our needs for numerical integration.