Computes the integrals of functions or characters in arbitrary orthogonal coordinate systems.

integral(
f,
bounds,
params = list(),
coordinates = "cartesian",
relTol = 0.001,
absTol = 1e-12,
method = NULL,
vectorize = NULL,
drop = TRUE,
verbose = FALSE,
...
)

## Arguments

f array of characters or a function returning a numeric array. list containing the lower and upper bounds for each variable. If the two bounds coincide, or if a single number is specified, the corresponding variable is not integrated and its value is fixed. list of additional parameters passed to f. coordinate system to use. One of: cartesian, polar, spherical, cylindrical, parabolic, parabolic-cylindrical or a character vector of scale factors for each variable. the maximum relative tolerance. the absolute tolerance. the method to use. One of "mc", "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas". Methods other than "mc" (naive Monte Carlo) require the cubature package to be installed (efficient integration in C). The defaul uses "hcubature" if cubature is installed or "mc" otherwise. logical. Use vectorization? If TRUE, it can significantly boost performance but f needs to handle the vector of inputs appropriately. The default uses FALSE if f is a function, TRUE otherwise. if TRUE, return the integral as a vector and not as an array for vector-valued functions. logical. Print on progress? additional arguments passed to cubintegrate, when method "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas" is used.

## Value

list with components

value

the final estimate of the integral.

error

estimate of the modulus of the absolute error.

cuba

cubature output when method "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas" is used.

## Details

The function integrates seamlessly with cubature for efficient numerical integration in C. If the package cubature is not installed, the function implements a naive Monte Carlo integration by default. For arbitrary orthogonal coordinates $$q_1\dots q_n$$ the integral is computed as:

$$\int J\cdot f(q_1\dots q_n) dq_1\dots dq_n$$

where $$J=\prod_i h_i$$ is the Jacobian determinant of the transformation and is equal to the product of the scale factors $$h_1\dots h_n$$.

## References

Guidotti, E. (2020). "calculus: High dimensional numerical and symbolic calculus in R". https://arxiv.org/abs/2101.00086

Other integrals: ode()

## Examples

### unidimensional integral
i <- integral("sin(x)", bounds = list(x = c(0,pi)))
i$value #> [1] 2 ### multidimensional integral f <- function(x,y) x*y i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value
#> [1] 0.25
### vector-valued integrals
f <- function(x,y) c(x, y, x*y)
i <- integral(f, bounds = list(x = c(0,1), y = c(0,1)))
i$value #> [1] 0.50 0.50 0.25 ### tensor-valued integrals f <- function(x,y) array(c(x^2, x*y, x*y, y^2), dim = c(2,2)) i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value
#>           [,1]      [,2]
#> [1,] 0.3333333 0.2500000
#> [2,] 0.2500000 0.3333333
### area of a circle
i <- integral(1,
bounds = list(r = c(0,1), theta = c(0,2*pi)),
coordinates = "polar")
i$value #> [1] 3.141593 ### surface of a sphere i <- integral(1, bounds = list(r = 1, theta = c(0,pi), phi = c(0,2*pi)), coordinates = "spherical") i$value
#> [1] 12.56639
### volume of a sphere
i <- integral(1,
bounds = list(r = c(0,1), theta = c(0,pi), phi = c(0,2*pi)),
coordinates = "spherical")
i\$value
#> [1] 4.188794