integrate

m4opt.models.integrate(model: Model, *, quick_and_dirty_npts: int | None = None, **kwargs) Quantity | float[source] [edit on github]

Integrate a 1D model using adaptive trapezoidal quadrature.

Parameters:
modelastropy.modeling.Model

The model to integrate.

quick_and_dirty_nptsint, optional

Number of sample points. If provided, disable adaptive quadrature and instead use fixed-order quadrature with the specified number of regularly spaced sample points.

**kwargsdict

Additional keyword arguments passed to scipy.integrate.quad_vec().

Returns:
integralfloat, astropy.units.Quantity

The integral over the bounding box of the model’s inputs.

Notes

When integrating compound models, the domain of integration is subdivided using the bounding box endpoints of all of the submodels. The integrator minimizes the error by tracking the error contributions from all of the subdivisions. The integrator can be slow; passing lower tolerances can speed up the calculation with a loss of accuracy. For a faster version without convergence checking, try the quick_and_dirty_npts option.

Examples

You can integrate dimensionless models:

>>> from astropy.modeling import models
>>> from astropy import units as u
>>> from m4opt.models import integrate
>>> import numpy as np
>>> model = models.Box1D(width=3)
>>> integrate(model)
3.0
>>> integrate(model, quick_and_dirty_npts=10000)
3.0
>>> model = models.Gaussian1D() * models.Const1D(1 / np.sqrt(2 * np.pi))
>>> integrate(model, epsrel=1e-7)
0.9999999648585338
>>> integrate(model, quick_and_dirty_npts=10000)
0.9999999620207557

Or models with dimensions:

>>> model = models.Lorentz1D(x_0=1 * u.micron, fwhm=0.1 * u.micron,
...                          amplitude=1 * u.erg / u.micron)
>>> integrate(model, epsrel=1e-7)
<Quantity 0.1550799 erg>
>>> integrate(model, quick_and_dirty_npts=10000)
<Quantity 0.1550799 erg>