View source: R/nimbleFunction_Rexecution.R
nimIntegrate | R Documentation |
NIMBLE wrapper around R's builtin integrate
. Adaptive quadrature
of functions of one variable over a finite or infinite interval.
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
)
f |
nimbleFunction of one input for which the integral is desired.
See below for details on requirements for how |
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 |
subdivisions |
the maximum number of subintervals. |
rel.tol |
relative accuracy requested. |
abs.tol |
absolute accuracy requested. |
stop.on.error |
logical. If |
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.
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"
Christopher Paciorek, Paul van Dam-Bates, Perry de Valpine
integrate
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)
## Not run:
cfun <- compileNimble(fun)
cfun(3.1415927, 0, 1)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.