gauss_legendre#

sunkit_spex.integrate.gauss_legendre(func, a, b, n=5, args=(), func_kwargs={})[source]#

Compute a definite integral using fixed-order Gaussian quadrature. Integrate func from a to b using Gaussian quadrature of order n.

Parameters:
  • func (callable) – A Python function or method to integrate (must accept vector inputs). If integrating a vector-valued function, the returned array must have shape (..., len(x)).

  • a (float) – Lower limit of integration.

  • b (float) – Upper limit of integration.

  • n (int, optional) – Order of quadrature integration. Default is 5.

  • args (tuple, optional) – Extra arguments to pass to function, if any.

  • func_kwargs – Keyword arguments to the function func to be integrated.

Returns:

integral – Gaussian quadrature approximation to the integral

Return type:

float

Examples

>>> from sunkit_spex.integrate  import gauss_legendre
>>> f = lambda x: x**8
>>> gauss_legendre(f,0.0,1.0,n=4)
array([0.11108844])
>>> gauss_legendre(f,0.0,1.0,n=5)
array([0.11111111])
>>> print(1/9.0)  # analytical result
0.1111111111111111
>>> gauss_legendre(f, [0, 1, 2], [1, 2, 3], n=5)
array([1.11111111e-01, 5.67777778e+01, 2.13011111e+03])
>>> 1/9, (2**9 - 1**9)/9, (3**9 - 2**9)/9 # analytical result
(0.1111111111111111, 56.77777777777778, 2130.1111111111113)
>>> gauss_legendre(np.cos,0.0,np.pi/2,n=4)
array([0.99999998])
>>> gauss_legendre(np.cos,0.0,np.pi/2,n=5)
array([1.])
>>> np.sin(np.pi/2)-np.sin(0)  # analytical result
1.0