Integration

galsim.integ.int1d(func, min, max, rel_err=1e-06, abs_err=1e-12)[source]

Integrate a 1-dimensional function from min to max.

Example usage:

>>> def func(x): return x**2
>>> galsim.integ.int1d(func, 0, 1)
0.33333333333333337
>>> galsim.integ.int1d(func, 0, 2)
2.666666666666667
>>> galsim.integ.int1d(func, -1, 1)
0.66666666666666674

Note

This uses an adaptive Gauss-Kronrod-Patterson method for doing the integration.

cf. https://www.jstor.org/stable/2004583

If one or both endpoints are infinite, it will automatically use an appropriate transformation to turn it into a finite integral.

Parameters:
  • func – The function to be integrated. y = func(x) should be valid.

  • min – The lower end of the integration bounds (anything < -1.e10 is treated as negative infinity).

  • max – The upper end of the integration bounds (anything > 1.e10 is treated as positive infinity).

  • rel_err – The desired relative error [default: 1.e-6]

  • abs_err – The desired absolute error [default: 1.e-12]

Returns:

the value of the integral.

galsim.integ.hankel(func, k, nu=0, rmax=None, rel_err=1e-06, abs_err=1e-12)[source]

Perform an order nu Hankel transform of the given function f(r) at a specific k value.

\[F(k) = \int_0^\infty f(r) J_\nu(k r) r dr\]

Note

For non-truncated Hankel integrals, this uses the method outlined in Ogata, 2005: http://www.kurims.kyoto-u.ac.jp/~prims/pdf/41-4/41-4-40.pdf

For truncated integrals (and k=0), it uses the same adaptive Gauss-Kronrod-Patterson method used for int1d.

Parameters:
  • func – The function f(r)

  • k – (float or numpy array) The value(s) of k for which to calculate F(k).

  • nu – The order of the Bessel function to use for the transform. [default: 0]

  • rmax – An optional truncation radius at which to have f(r) drop to 0. [default: None]

  • rel_err – The desired relative accuracy [default: 1.e-6]

  • abs_err – The desired absolute accuracy [default: 1.e-12]

Returns:

An estimate of F(k)

class galsim.integ.IntegrationRule[source]

A class that can be used to integrate something more complicated than a normal scalar function.

In GalSim, we use it to do the integration of chromatic images over a bandpass. Typically f is some kind of draw function, xs are the wavelengths, and w is the bandpass throughput. But this class is abstracted away from all of that and can be used for anything where the function returns something complicated, but which can be added together to compute the quadrature.

Specifically the return value from f must be closed under both addition and multiplication by a scalar (a float value).

class galsim.integ.MidptRule[source]

Midpoint rule for integration.

calculateWeights(xs, w)[source]

Calculate the apporpriate weights for the midpoint rule integration

Parameters:
  • xs – Locations at which to evaluate f.

  • w – Weight function if desired [default: None]

Returns:

The net weights to use at each location.

class galsim.integ.TrapzRule[source]

Trapezoidal rule for integration.

calculateWeights(xs, w)[source]

Calculate the apporpriate weights for the trapezoidal rule integration

Parameters:
  • xs – Locations at which to evaluate f.

  • w – Weight function if desired [default: None]

Returns:

The net weights to use at each location.

class galsim.integ.QuadRule[source]

Quadratic rule for integration

This models both f and w as linear between the evaluation points, so the product is quadratic.

calculateWeights(xs, w)[source]

Calculate the apporpriate weights for the quadratic rule integration

Parameters:
  • xs – Locations at which to evaluate f.

  • w – Weight function if desired [default: None]

Returns:

The net weights to use at each location.

galsim.integ.midptRule = <galsim.integ.MidptRule object>

For convenience, an instance of MidptRule

galsim.integ.trapzRule = <galsim.integ.TrapzRule object>

For convenience, an instance of TrapzRule

galsim.integ.quadRule = <galsim.integ.QuadRule object>

For convenience, an instance of QuadRule

class galsim.integ.ImageIntegrator[source]

A base class for integrators used by ChromaticObject to integrate the drawn images over wavelengthh using a Bandpass as a weight function.

__call__(evaluateAtWavelength, bandpass, image, drawImageKwargs, doK=False)[source]
Parameters:
  • evaluateAtWavelength – Function that returns a monochromatic surface brightness profile as a function of wavelength.

  • bandpassBandpass object representing the filter being imaged through.

  • imageImage used to set size and scale of output

  • drawImageKwargs – dict with other kwargs to send to ChromaticObject.drawImage function.

  • doK – Integrate up results of ChromaticObject.drawKImage instead of results of ChromaticObject.drawImage. [default: False]

Returns:

the result of integral as an Image

class galsim.integ.SampleIntegrator(rule)[source]

Bases: ImageIntegrator

Create a chromatic surface brightness profile integrator, which will integrate over wavelength using a Bandpass as a weight function.

This integrator will evaluate the integrand only at the wavelengths in bandpass.wave_list. See ContinuousIntegrator for an integrator that evaluates the integrand at a given number of points equally spaced apart.

Parameters:

rule

Which integration rule to apply to the wavelength and monochromatic surface brightness samples. Options include:

  • galsim.integ.midptRule: Use the midpoint integration rule

  • galsim.integ.trapzRule: Use the trapezoidal integration rule

  • galsim.integ.quadRule: Use the quadratic integration rule

class galsim.integ.ContinuousIntegrator(rule, N=250, use_endpoints=True)[source]

Bases: ImageIntegrator

Create a chromatic surface brightness profile integrator, which will integrate over wavelength using a Bandpass as a weight function.

This integrator will evaluate the integrand at a given number N of equally spaced wavelengths over the interval defined by bandpass.blue_limit and bandpass.red_limit. See SampleIntegrator for an integrator that only evaluates the integrand at the predefined set of wavelengths in bandpass.wave_list.

Parameters:
  • rule

    Which integration rule to apply to the wavelength and monochromatic surface brightness samples. Options include:

    • galsim.integ.midptRule: Use the midpoint integration rule

    • galsim.integ.trapzRule: Use the trapezoidal integration rule

    • galsim.integ.quadRule: Use the quadratic integration rule

  • N – Number of equally-wavelength-spaced monochromatic surface brightness samples to evaluate. [default: 250]

  • use_endpoints – Whether to sample the endpoints bandpass.blue_limit and bandpass.red_limit. This should probably be True for a rule like numpy.trapz, which explicitly samples the integration limits. For a rule like the midpoint rule, however, the integration limits are not generally sampled, (only the midpoint between each integration limit and its nearest interior point is sampled), thus use_endpoints should be set to False in this case. [default: True]