QuadratureΒΆ
Base classesΒΆ
- class modepy.Quadrature(nodes: ArrayF, weights: ArrayF, exact_to: int | _Inf | None = None)[source]ΒΆ
The basic interface for a quadrature rule.
- nodes: ArrayFΒΆ
An array of shape (dim, nnodes), where dim is the dimension of the quadrature rule. In 1D, the shape is just (nnodes,).
- property exact_to: int | _InfΒΆ
Summed polynomial degree up to which the quadrature is exact.
In higher-dimensions, the quadrature is supposed to be exact on (at least) \(P^N\), where \(N\) =
exact_to. If the quadrature accuracy is not known,exact_towill not be set, and anAttributeErrorwill be raised when attempting to access this information.
- class modepy.ZeroDimensionalQuadrature[source]ΒΆ
Bases:
QuadratureA quadrature rule that should be used for 0d domains (i.e. points).
- modepy.quadrature_for_space(space: FunctionSpace, shape: Shape) Quadrature[source]ΒΆ
- modepy.quadrature_for_space(space: PN, shape: Simplex) Quadrature
- modepy.quadrature_for_space(space: TensorProductSpace, shape: TensorProductShape) Quadrature
- Returns:
a
Quadraturethat exactly integrates the functions in space over shape.
Jacobi-Gauss quadrature in one dimensionΒΆ
- class modepy.JacobiGaussQuadrature(alpha: float, beta: float, N: int, backend: str | None = None, force_dim_axis: bool = False)[source]ΒΆ
Bases:
QuadratureA Gauss quadrature of order N associated with the Jacobi polynomials.
The quadrature rule can be used for weighted integrals of the form
\[I[f] = \int_{-1}^1 f(x) (1 - x)^\alpha (1 + x)^\beta\, \mathrm{d}x,\]where \(\alpha, \beta > -1\). The quadrature rule is exact up to degree \(2N + 1\). The quadrature has N+1 nodes.
- __init__(alpha: float, beta: float, N: int, backend: str | None = None, force_dim_axis: bool = False) None[source]ΒΆ
- Parameters:
backend β Either
"builtin"or"scipy". When the"builtin"backend is in use, there is an additional requirement that \(\alpha + \beta \ne -1\), with the exception of the Chebyshev quadrature \(\alpha = \beta = -1/2\). The"scipy"backend has no such restriction.
- static compute_weights_and_nodes(N: int, alpha: float, beta: float) tuple[ArrayF, ArrayF][source]ΒΆ
- Parameters:
N β order of the Gauss quadrature (the order of exactly integrated polynomials is \(2 N + 1\)).
alpha β power of \(1 - x\) in the Jacobi polynomial weight.
beta β power of \(1 + x\) in the Jacobi polynomial weight.
- Returns:
a tuple
(nodes, weights)of quadrature notes and weights.
- class modepy.LegendreGaussQuadrature(N: int, backend: str | None = None, force_dim_axis: bool = False)[source]ΒΆ
Bases:
JacobiGaussQuadratureA Gauss quadrature rule with weight \(1\) and N+1 nodes.
Corresponds to a Gauss-Jacobi quadrature rule with \(\alpha = \beta = 0\).
- class modepy.ChebyshevGaussQuadrature(N: int, kind: int = 1, backend: str | None = None, force_dim_axis: bool = False)[source]ΒΆ
Bases:
JacobiGaussQuadratureA Gauss quadrature rule with weight \(\sqrt{1-x^2}^{\mp 1}\) and N+1 nodes.
The Chebyshev-Gauss quadrature rules of the first kind and second kind correspond to Gauss-Jacobi quadrature rules with \(\alpha = \beta = -0.5\) and \(\alpha = \beta = 0.5\), respectively.
Added in version 2019.1.
- class modepy.GaussGegenbauerQuadrature(alpha: float, N: int, backend: str | None = None, force_dim_axis: bool = False)[source]ΒΆ
Bases:
JacobiGaussQuadratureGauss-Gegenbauer quadrature is a special case of Gauss-Jacobi quadrature with \(\alpha = \beta\) and N+1 nodes.
Added in version 2019.1.
- modepy.quadrature.jacobi_gauss.jacobi_gauss_lobatto_nodes(alpha: float, beta: float, N: int, backend: str | None = None, force_dim_axis: bool = False) ArrayF[source]ΒΆ
Compute Gauss-Lobatto quadrature nodes associated with
JacobiGaussQuadraturewith the same parameters.This helper returns only the N+1 nodes; the corresponding Gauss-Lobatto quadrature rule (using these nodes with appropriate weights) is exact for polynomials up to degree \(2N - 1\) when \(N \ge 1\).
For \(N = 0\), this function returns a single node at
0. This degenerate case does not correspond to a Gauss-Lobatto node set with endpoints \(\pm 1\), but is provided for convenience.
- modepy.quadrature.jacobi_gauss.legendre_gauss_lobatto_nodes(N: int, backend: str | None = None, force_dim_axis: bool = False) ArrayF[source]ΒΆ
Compute the Legendre-Gauss-Lobatto quadrature nodes. N+1 is the number of nodes.
Exact to degree \(2N - 1\).
- class modepy.JacobiGaussLobattoQuadrature(alpha: float, beta: float, N: int, *, backend: str | None = None, force_dim_axis: bool = False)[source]ΒΆ
Compute the Jacobi-Gauss-Lobatto quadrature with respect to the Jacobi weight function
\[I[f] = \int_{-1}^1 f(x) (1 - x)^\alpha (1 + x)^\beta\, \mathrm{d}x,\]There will be N+1 nodes. Exact to degree \(2N - 1\).
Added in version 2024.2.
Clenshaw-Curtis and FejΓ©r quadrature in one dimensionΒΆ
- class modepy.ClenshawCurtisQuadrature(N: int, force_dim_axis: bool = False)[source]ΒΆ
Bases:
QuadratureClenshaw-Curtis quadrature of order N with N + 1 points.
The quadrature rule is exact up to degree \(N\) and can be nested. Its performance for differentiable functions is comparable with the classic Gauss-Legendre quadrature, which is exact for polynomials of degree up to \(2N + 1\). Implementation is based on [Waldvogel2003].
Integrates on the interval \((-1, 1)\).
- class modepy.FejerQuadrature(N: int, kind: int = 1, force_dim_axis: bool = False)[source]ΒΆ
Bases:
QuadratureFejΓ©r quadrature rules of order N.
FejΓ©r quadrature of the first kind has N points and uses Chebyshev nodes, i.e. the roots of Chebyshev polynomials.
FejΓ©r quadrature of the second kind has N - 1 points and uses only the interior extrema of the Chebyshev nodes, i.e. the true stationary points. This rule is almost identical to Clenshaw-Curtis and can be nested.
Integrates on the interval \((-1, 1)\). Implementation is based on [Waldvogel2003].
Gauss-Kronrod quadratureΒΆ
- class modepy.quadrature.kronrod.KronrodGaussQuadrature(nodes: ArrayF, weights: ArrayF, exact_to: int | _Inf | None = None)[source]ΒΆ
- class modepy.quadrature.kronrod.KronrodQuadrature(nodes: ArrayF, weights: ArrayF, weights_at_gauss_nodes: ArrayF, exact_to: int, gauss_quadrature: KronrodGaussQuadrature)[source]ΒΆ
Uses a computation following [Laurie1997].
- gauss_quadrature: KronrodGaussQuadratureΒΆ
- __call__(f: Callable[[ArrayF], ArrayF]) ArrayF | floating[source]ΒΆ
Evaluate the Kronrod estimate for the integral.
Note
This will (re-)evaluate f at both the Gauss and the Kronrod nodes, likely negating any cost advantage of Kronrod. As a result, it is recommended to reimplement the evaluation logic, following the code of this function, while reusing the function values from the Gauss evaluation.
- modepy.quadrature.kronrod.make_kronrod_quadrature(n: int) KronrodQuadrature[source]ΒΆ
ReferencesΒΆ
Dirk P. Laurie, βCalculation of Gauss-Kronrod quadrature rules,β Mathematics of Computation, vol. 66, no. 219, pp. 1133-1145 (1997). DOI
Transplanted quadrature in one dimensionΒΆ
The transplanted maps implemented here include the conformal maps from Hale-Trefethen (the sausage polynomial family and the strip map) as well as the earlier Kosloff-Tal-Ezer \(\arcsin\) map.
Note
In using the term βtransplantedβ, we are following the terminology from [HaleTrefethen2008]. In other nomenclature, this is also referred to as a change of variables transformation using a conformal mapping.
Given a base rule \((s_i, w_i^{(s)})\) on \([-1,1]\), transplanted quadrature uses a map \(g(s): [-1, 1] \to [-1, 1]\) to build
so that
Map functionsΒΆ
Identity mapΒΆ
Sausage polynomial mapsΒΆ
- modepy.quadrature.transplanted.map_sausage(s: ArrayF, degree: int) tuple[ArrayF, ArrayF][source]ΒΆ
Odd-degree polynomial sausage map from Hale-Trefethen [HaleTrefethen2008].
This is the normalized odd Taylor truncation of \(\arcsin(s)\) through the monomial of degree degree.
- Parameters:
s β quadrature nodes on \([-1, 1]\).
degree β positive odd degree in
{1, 3, 5, ...}.
- Returns:
(nodes, jacobian).
Kosloff-Tal-Ezer mapΒΆ
- modepy.quadrature.transplanted.map_kosloff_tal_ezer(s: ArrayF, *, rho: float = 1.4, alpha: float | None = None) tuple[ArrayF, ArrayF][source]ΒΆ
Kosloff-Tal-Ezer map from [KosloffTalEzer1993].
The map is
\[g(s) = \frac{\arcsin(\alpha s)}{\arcsin(\alpha)},\]where \(0 < \alpha < 1\). If alpha is not provided, it is chosen from rho using
\[\alpha = \frac{2}{\rho + \rho^{-1}},\]matching the parameter choice discussed by Hale-Trefethen [HaleTrefethen2008] for a \(\rho\)-ellipse analyticity model.
- Parameters:
s β quadrature nodes on \([-1, 1]\).
rho β ellipse parameter, must satisfy
rho > 1. Ignored if alpha is given explicitly.alpha β map parameter satisfying
0 < alpha < 1. If None, computed from rho.
- Returns:
(nodes, jacobian).
Strip conformal mapΒΆ
- modepy.quadrature.transplanted.map_strip(s: ArrayF, *, rho: float = 1.4) tuple[ArrayF, ArrayF][source]ΒΆ
Strip map from Hale-Trefethen [HaleTrefethen2008] transplanted quadrature.
The map is based on the Schwarz-Christoffel transformation that maps the unit disk to a rectangle, composed with an \(\arcsin\) to pull back to \([-1, 1]\). The parameter rho controls the half-width of the analyticity strip: a larger rho concentrates nodes near the endpoints.
- Parameters:
s β quadrature nodes on \((-1, 1)\) (strict interior).
rho β strip parameter, must satisfy
rho > 1.
- Returns:
(nodes, jacobian).
Important
This map requires interior nodes (
abs(s) < 1), so it is intended for base rules such as Legendre-Gauss that do not include endpoints. Other common rules, such as Gauss-Lobatto or Clenshaw-Curtis, cannot be used.
Quadrature wrappersΒΆ
- modepy.transplanted_1d_quadrature(quadrature: Quadrature, map_fn: Callable[[ArrayF], tuple[ArrayF, ArrayF]]) Quadrature[source]ΒΆ
Map an existing 1D quadrature rule using a transplant map.
The transformed rule approximates
\[\int_{-1}^1 f(x)\,\mathrm{d}x = \int_{-1}^1 f(g(s)) g'(s)\,\mathrm{d}s,\]by mapping existing nodes \(s_i\) and scaling existing weights \(w_i\) with \(g'(s_i)\).
- Parameters:
quadrature β a one-dimensional
Quadraturewhose nodes lie in \([-1, 1]\).map_fn β a callable
(s: ArrayF) -> (nodes, jacobian), such asmap_identity(),map_sausage(),map_kosloff_tal_ezer(), ormap_strip(), with parameters (if any) bound viafunctools.partial().
- Returns:
a new
Quadraturewith mapped nodes and adjusted weights.
ExampleΒΆ
from functools import partial
import modepy as mp
from modepy.quadrature.transplanted import map_kosloff_tal_ezer, map_sausage
q_kte = mp.transplanted_legendre_gauss_quadrature(
20,
partial(map_kosloff_tal_ezer, rho=1.4),
force_dim_axis=True,
)
q_sausage = mp.transplanted_legendre_gauss_quadrature(
20,
partial(map_sausage, degree=9),
force_dim_axis=True,
)
ReferencesΒΆ
N. Hale and L. N. Trefethen, New Quadrature Formulas from Conformal Maps, SIAM Journal on Numerical Analysis 46(2), 930-948 (2008), doi:10.1137/07068607X.
D. Kosloff and H. Tal-Ezer, A Modified Chebyshev Pseudospectral Method with an \(O(N^{-1})\) Time Step Restriction, Journal of Computational Physics 104(2), 457-469 (1993), doi:10.1006/jcph.1993.1044.
Quadratures on the simplexΒΆ
Added in version 2013.3.
- class modepy.GrundmannMoellerSimplexQuadrature(order: int, dimension: int)[source]ΒΆ
Bases:
QuadratureCubature on an n-simplex from [Grundmann1978].
This cubature rule has both negative and positive weights. It is exact for polynomials up to order \(2s + 1\), where \(s\) is given as order. The integration domain is the unit simplex (see Coordinates on the triangle and Coordinates on the tetrahedron).
[Grundmann1978]A. Grundmann and H.M. Moeller, Invariant integration formulas for the n-simplex by combinatorial methods, SIAM Journal on Numerical Analysis, vol. 15, pp. 282β290, 1978. DOI
- class modepy.XiaoGimbutasSimplexQuadrature(order: int, dims: int)[source]ΒΆ
Bases:
QuadratureA (nearly) Gaussian simplicial quadrature with very few quadrature nodes from [Xiao2010].
This rule is available for low-to-moderate orders. The integration domain is the unit simplex (see Coordinates on the triangle and Coordinates on the tetrahedron).
- __init__(order: int, dims: int) None[source]ΒΆ
- Parameters:
order β the total degree to which the quadrature rule is exact.
dims β the number of dimensions for the quadrature rule. 2 for quadrature on triangles and 3 for tetrahedra.
- Raises:
modepy.QuadratureRuleUnavailableif no quadrature rule for therequested parameters is available.
- class modepy.VioreanuRokhlinSimplexQuadrature(order: int, dims: int)[source]ΒΆ
Bases:
QuadratureSimplicial quadratures with symmetric node sets and positive weights suitable for well-conditioned interpolation.
The integration domain is the unit simplex (see Coordinates on the triangle and Coordinates on the tetrahedron). When using these nodes, please acknowledge Zydrunas Gimbutas, who generated them as follows:
The 2D nodes are based on the interpolation node set derived in the article [Vioreanu2011].
Note that in Vioreanuβs tables, only orders 5, 6, 9, and 12 are rotationally symmetric, which gives one extra order for integration and better interpolation conditioning. Also note that since the tables have been re-generated independently, the nodes and weights may be different.
The 3D nodes were derived from the
modepy.warp_and_blend_nodes().A tightening algorithm was then applied, as described in [Vioreanu2012].
[Vioreanu2011]B. Vioreanu and V. Rokhlin, Spectra of Multiplication Operators as a Numerical Tool, Yale CS Tech Report 1443. PDF
[Vioreanu2012]B. Vioreanu, Spectra of Multiplication Operators as a Numerical Tool, Yale University, 2012. PDF
Added in version 2013.3.
- __init__(order: int, dims: int) None[source]ΒΆ
- Parameters:
order β The total degree to which the quadrature rule is exact for interpolation.
dims β The number of dimensions for the quadrature rule. 2 for quadrature on triangles and 3 for tetrahedra.
- Raises:
modepy.QuadratureRuleUnavailableif no quadrature rule for the requested parameters is available.
- class modepy.JaskowiecSukumarQuadrature(order: int, dims: int)[source]ΒΆ
Bases:
QuadratureSymmetric quadrature rules with positive weights for tetrahedra from [Jaskowiec2021].
The integration domain is the unit tetrahedron.
[Jaskowiec2021]J. JaΕkowiec, N. Sukumar, Highβorder Symmetric Cubature Rules for Tetrahedra and Pyramids, International Journal for Numerical Methods in Engineering, Vol. 122, pp. 148β171, 2021, DOI
- __init__(order: int, dims: int) None[source]ΒΆ
- Parameters:
nodes β an array of shape (d, nnodes), where d is the dimension of the quadrature rule.
weights β an array of length nnodes.
exact_to β an optional argument denoting the summed polynomial degree to which the quadrature is exact. By default, exact_to is None and will not be set as an attribute.
Quadratures on the hypercubeΒΆ
- class modepy.WitherdenVincentQuadrature(order: int, dims: int)[source]ΒΆ
Bases:
QuadratureSymmetric quadrature rules with positive weights for rectangles and hexahedra from [Witherden2015].
The integration domain is the unit hypercube \([-1, 1]^d\), where \(d\) is the dimension.
[Witherden2015]F. D. Witherden, P. E. Vincent, On the Identification of Symmetric Quadrature Rules for Finite Element Methods, Computers & Mathematics with Applications, Vol. 69, pp. 1232β1241, 2015. DOI
- __init__(order: int, dims: int) None[source]ΒΆ
- Parameters:
nodes β an array of shape (d, nnodes), where d is the dimension of the quadrature rule.
weights β an array of length nnodes.
exact_to β an optional argument denoting the summed polynomial degree to which the quadrature is exact. By default, exact_to is None and will not be set as an attribute.
- class modepy.TensorProductQuadrature(quads: Iterable[Quadrature])[source]ΒΆ
Bases:
QuadratureA tensor product quadrature of one-dimensional
Quadratures.- quadratures: Sequence[Quadrature]ΒΆ
The lower-dimensional quadratures from which the tensor product quadrature is composed.
- __init__(quads: Iterable[Quadrature]) None[source]ΒΆ
- Parameters:
quad β a iterable of
Quadratureobjects for one-dimensional intervals, one for each dimension of the tensor product.
- class modepy.LegendreGaussTensorProductQuadrature(N: int, dims: int, backend: str | None = None)[source]ΒΆ
Bases:
TensorProductQuadratureA tensor product using only
LegendreGaussQuadratureone-dimensional rules.