Learn R Programming

nimble (version 1.2.1)

nimIntegrate: Integration of One-Dimensional Functions

Description

NIMBLE wrapper around R's builtin integrate. Adaptive quadrature of functions of one variable over a finite or infinite interval.

Usage

nimIntegrate(
  f,
  lower,
  upper,
  param,
  subdivisions = 100L,
  rel.tol = .Machine$double.eps^0.25,
  abs.tol = .Machine$double.eps^0.25,
  stop.on.error = TRUE
)

Value

A vector with three values, the first the estimate of the integral, the second an estimate of the modulus of the absolute error, and the third a result code corresponding to the message returned by integrate. The numerical result code can be interpreted as follows:

  • 0: "OK"

  • 1: "maximum number of subdivisions reached"

  • 2: "roundoff error was detected"

  • 3: "extremely bad integrand behaviour"

  • 4: "roundoff error is detected in the extrapolation table"

  • 5: "the integral is probably divergent"

  • 6: "the input is invalid"

Arguments

f

nimbleFunction of one input for which the integral is desired. See below for details on requirements for how f must be defined.

lower

an optional scalar lower bound for the input of the function.

upper

an optional scalar upper bound for the input of the function.

param

additional parameter(s) to the function that are fixed with respect to the integration. If f takes no additional arguments (beyond the variable of integration), this must be provided but need not be used in f. Can be of length one or more. parameters.

subdivisions

the maximum number of subintervals.

rel.tol

relative accuracy requested.

abs.tol

absolute accuracy requested.

stop.on.error

logical. If TRUE (the default) an error stops the function. Otherwise some errors will give a result with the error code given in the third element of the result vector.

Author

Christopher Paciorek, Paul van Dam-Bates, Perry de Valpine

Details

The function f should take two arguments, the first of type double(1), i.e., vector. f should be vectorized in that it should also return a double(1) object, containing the result of applying the function to each element of the first argument. (The result can be calculated using vectorized NIMBLE code or using a loop.) The second argument is required to also be of type double(1), containing any additional parameter(s) to the function that are not being integrated over. This argument can be unused in the function if the function does not need additional parameters. Note that this must be of type double(1) even if param contains a single element (NIMBLE will manage the lengths behind the scenes).

Note that unlike with R's integrate, additional parameters must be passed as part of a vector, specified via param, and cannot be passed as individual named arguments.

See Also

Examples

Run this code
integrand <- nimbleFunction(
   run = function(x = double(1), theta = double(1)) {
       return(x*theta[1])   
   returnType(double(1))
 }
)

fun <- nimbleFunction(
   run = function(theta = double(0), lower = double(0), upper = double(0)) {
       param = c(theta, 0)  # cannot be scalar, so pad with zero.
       output = integrate(integrand, lower, upper, param)
       returnType(double(1))
       return(output)
 })

fun(3.1415927, 0, 1)
if (FALSE) {
cfun <- compileNimble(fun)
cfun(3.1415927, 0, 1)
}

Run the code above in your browser using DataLab