API Reference

API Reference#

This is the public API for gaussquad.

gaussquad.gauss_quadrature.wquad(fn, weight_fn, interval, degree, moment_method='legendre', moment_degree=None, verbose=False) float[source]#

Approximate the integral of a weighted function defined on a finite interval by computing the quadrature nodes and weights using the Golub–Welsch algorithm (see Golub & Welsch, 1967).

Parameters:
  • fn (Callable[[np.ndarray], np.ndarray]) – The function to integrate f(x), must be vectorized.

  • weight_fn (Callable[[np.ndarray], np.ndarray]) – The weight function w(x), must be vectorized.

  • interval (Tuple[float, float]) – The finite integration bounds (a, b).

  • degree (int) – The number of nodes and weights for the quadrature.

  • moment_method (Optional[Union[str, Callable]]) – Optional custom method to compute the k-th moment. If None, defaults to “legendre” for Gauss-Legendre.

  • moment_degree (Optional[int]) – The degree of the quadrature rule used to compute the moments. If None, defaults to 2 * degree.

  • verbose (Optional[bool]) – If True, enables verbose logging. If None, defaults to False.

Returns:

result – The computed integral of the weighted function.

Return type:

float

Raises:
  • ValueError – If the moment Hankel matrix is not positive-definite.

  • ValueError – If any of the input parameters are invalid.

  • TypeError – If any of the input parameters are of the wrong type.

Examples

>>> import numpy as np
>>> from gaussquad import wquad
>>> result = wquad(
...     fn=lambda x: x**2,
...     weight_fn=lambda x: np.ones_like(x),
...     interval=(0, 1),
...     degree=3,
...     moment_method="exact"
... )  # --> 0.3333333333333335
gaussquad.gauss_quadrature.wquad_nodes_weights(weight_fn, interval, degree, moment_method='legendre', moment_degree=None, verbose=False) tuple[ndarray, ndarray][source]#

Compute general-purpose Gauss quadrature nodes and weights defined on a finite interval using the Golub–Welsch algorithm (see Golub & Welsch, 1967).

Parameters:
  • weight_fn (Callable[[np.ndarray], np.ndarray]) – The weight function w(x), must be vectorized.

  • interval (Tuple[float, float]) – The finite integration bounds (a, b).

  • degree (int) – The number of nodes and weights for the quadrature.

  • moment_method (Optional[Union[str, Callable]]) – Optional custom method to compute the k-th moment. If None, defaults to “legendre” for Gauss-Legendre.

  • moment_degree (Optional[int]) – The degree of the quadrature rule used to compute the moments. If None, defaults to 2 * degree.

  • verbose (Optional[bool]) – If True, enables verbose logging. If None, defaults to False.

Returns:

  • nodes (np.ndarray) – The quadrature nodes.

  • weights (np.ndarray) – The quadrature weights.

Raises:
  • ValueError – If the moment Hankel matrix is not positive-definite.

  • ValueError – If any of the input parameters are invalid.

  • TypeError – If any of the input parameters are of the wrong type.

Examples

>>> import numpy as np
>>> from gaussquad import wquad_nodes_weights
>>> nodes, weights = wquad_nodes_weights(
...     weight_fn=lambda x: np.ones_like(x),
...     interval=(0, 1),
...     degree=3,
...     moment_method="exact",
... )  # --> [-1.04471955,  0.23701648,  0.80770307], [0.00101608, 0.53588233, 0.46310159]