chapter
- Introduction SciPy
- SciPy installation
- SciPy basic functions
- SciPy Special Functions
- SciPy k-means clustering
- Constant SciPy
- SciPy fftpack (Fourier Transform)
- SciPy Points
- SciPy interpolation
- SciPy input and output
- SciPy Linear Algebra
- SciPy image processing
- SciPy optimization
- SciPy signal processing
- SciPy statistics
Scipy The integrate
module provides a number of numerical integration, e.g., a multiple integral, double integral, triple integral, multiple integration, like Gaussian integration.
Here are some commonly integration function.
A re-integration
SciPy integral module, Quad function is an important function for seeking a double integral. For example, in a given range into the b, a function f (x) seeking a double integral.
$$\int_a^bf(x)dx$$
The general form of a quad scipy.integrate.quad(f, a, b)
, which f
is the quadrature of the function name, a
and b
respectively lower and upper.
Examples
Let's look at an example of a Gaussian function, integrating the range of 0-5.
First need to define the function $ → f (x) = e ^ {- x2} $, which may be used to represent a lambda expression, and then use a quad double integral method to evaluate it.
import scipy.integrate
from numpy import exp
f = lambda x:exp(-x**2)
i = scipy.integrate.quad(f, 0, 5)
print(i)
Export
(0.8862269254513955, 2.3183115159980698e-14)
quad function returns two values, the first value is the integral value, the second value is the value of the integral absolute error estimates.
Examples
If the integral of the function f with coefficient parameters, namely:
$$I(a,b) = \int_0^1(ax^2+b)dx$$
So a and b can be passed through the quad function args:
from scipy.integrate import quad
def f(x, a, b):
return a * (x ** 2) + b
ret = quad(f, 0, 1, args=(3, 1))
print (ret)
Export
(2.0, 2.220446049250313e-14)
Re-integration
To calculate the double integral, triple integral, multiple integrals, use dblquad, tplquad and nquad function.
Double integrals
dblquad general form is scipy.integrate.dblquad(func, a, b, gfun, hfun)
, where func
is the name to be integral function a
, b
is the lower limit of the x variable gfun
, hfun
the upper limit of the variable y defined function name.
Examples
Seeking double integral:
$$\int_0^{\frac{1}{2}}dy\int_0^{\sqrt[]{1-4y^2}}19xydx$$
We use lambda expressions defined functions f
, g
and h
. Note that, in many cases g
, and h
it may be constant, but even g
and h
constant, must also be defined as a function.
import scipy.integrate
from numpy import exp
from math import sqrt
f = lambda x, y : 19*x*y
g = lambda x : 0
h = lambda y : sqrt(1-4*y**2)
i = scipy.integrate.dblquad(f, 0, 0.5, g, h)
print (i)
Export
(0.59375, 2.029716563995638e-14)
In addition to the methods described above, Scipy the integrate
module integration There are many other methods, e.g. nquad, seeking for multiple integration. However, most of the scenes quad and dblquad suffice.