aurelio-amerio / MultiQuad.jl

A convenient interface to perform 1D, 2D and 3D numerical integrations. Uses QuadGK.jl, Cuba.jl, HCubature.jl and FastGaussQuadrature.jl as back-ends.
MIT License
8 stars 2 forks source link

MultiQuad.jl

CI codecov.io

MultiQuad.jl is a convenient interface to perform 1D, 2D and 3D numerical integrations. It uses QuadGK, Cuba and HCubature as back-ends.

Usage

quad

quad(arg::Function, x1, x2; method = :quadgk, kwargs...)

It is possible to use quad to perform 1D integrals of the following kind:

The supported adaptive integration methods are:

There are several fixed order quadrature methods available based on FastGaussQuadrature

If a specific integration routine is not needed, it is suggested to use :quadgk (the default option). For highly oscillating integrals, it is advised to use :gausslegendrewith a high order (~10000).

Please note that :gausshermite and :gausslaguerre are used to specific kind of integrals with infinite bounds.

For more informations on these integration techniques, please follow the official documentation here.

While using fixed order quadrature methods, it is possible to use multithreading to compute the 1D quadrature, by passing the keyword argument mutithreading=true to quad. If the forseen integration time is long, it is also possible to display a progress bar by passing the keyword argument verbose=true.

See QuadGK and Cuba.jl for all the available keyword arguments.

Example 1

Compute:

using MultiQuad

func(x) = x^2*exp(-x)
quad(func, 0, 4) # equivalent to quad(func, 0, 4, method=:quadgk)
quad(func, 0, 4, method=:gausslegendre, order=1000)
# or add a progress bar and use multithreading
quad(func, 0, 4, method=:gausslegendre, order=10000, multithreading=true, verbose=true)

# for certain kinds of integrals with infinite bounds, it may be possible to use a specific integration routine
func(x) = x^2*exp(-x)
quad(func, 0, Inf, method=:quadgk)[1]
# gausslaguerre computes the integral of f(x)*exp(-x) from 0 to infinity
quad(x -> x^2, method=:gausslaguerre, order=10000)[1] 

Example 2

It is possible to compute integrals with unit of measurement using Unitful.

For example, let's compute:

using MultiQuad
using Unitful

func(x) = x^2
quad(func, 1u"m", 5u"m")

dblquad

dblquad(arg::Function, x1, x2, y1::Function, y2::Function; method = :cuhre, kwargs...)
dblquad(arg::Function, x1, x2, y1, y2; method = :cuhre, kwargs...)

It is possible to use dblquad to perform 2D integrals of the following kind:

The supported integration method are:

It is suggested to use :hcubature (the default option).

See Cuba.jl and HCubature for all the available keyword arguments.

Example 1

Compute:

using MultiQuad

func(y,x) = sin(x)*y^2
integral, error = dblquad(func, 1, 2, x->0, x->x^2, rtol=1e-9)

Example 2

It is possible to compute integrals with unit of measurement using Unitful.

For example, let's compute:

using MultiQuad
using Unitful

func(y,x) = sin(x)*y^2
integral, error = dblquad(func, 1u"m", 2u"m", x->0u"m^2", x->x^2, rtol=1e-9)

Example 3

Compute:

using MultiQuad

func(y,x) = sin(x)*y^2
integral, error = dblquad(func, 1, 2, 0, 4, rtol=1e-9)

tplquad

tplquad(arg::Function, x1, x2, y1::Function, y2::Function, z1::Function, z2::Function; method = :cuhre, kwargs...)

It is possible to use quad to perform 3D integrals of the following kind:

The supported integration method are:

It is suggested to use :cuhre (the default option)

See Cuba.jl for all the available keyword arguments.

Example 1

Compute:

using MultiQuad

func(z,y,x) = sin(z)*y*x
integral, error = tplquad(func, 0, 4, x->x, x->x^2, (x,y)->2, (x,y)->3*x)

Example 2

It is possible to compute integrals with unit of measurement using Unitful.

For example, let's compute:

using MultiQuad
using Unitful

func(z,y,x) = sin(z)*y*x
integral, error = tplquad(func, 0u"m", 4u"m", x->0u"m^2", x->x^2, (x,y)->0, (x,y)->3)

Example 3

Compute:

using MultiQuad

func(z,y,x) = sin(z)*y*x
integral, error = tplquad(func, 0, 4, 1, 2, 2, 3)