gridIntCB: Grid interpolation in arbitrary dimension through Cardinal...

View source: R/gridIntCard.R

gridIntCBR Documentation

Grid interpolation in arbitrary dimension through Cardinal Basis

Description

***************************************************************************** Grid linear interpolation in arbitrary dimension through Cardinal Basis.

Usage

gridIntCB(
  X,
  Y,
  Xout,
  interpCB = function(x, xout) {
     cardinalBasis_lagrange(x = x, xout = xout)$CB
 },
  intOrder = NULL,
  trace = 1L,
  out_of_bounds = stop,
  ...
)

Arguments

X

An object that can be coerced into Grid. This can be a data.frame or a matrix in Scattered Data style in which case the column number is equal to the spatial dimension d, and the row number is then equal to the number of nodes n. But it can also be a Grid object previously created. A data frame or matrix X will first be coerced into Grid by using the the S3 method as.Grid.

Y

Response to be interpolated. It must be a vector of length n equal to the number of nodes. When X has class Grid, the order of the elements in Y must conform to the order of the nodes as given in X, see the help for gridInt.

Xout

Interpolation locations. Can be a vector or a matrix. In the first case, the length of the vector must be equal to the spatial dimension d as given by xLev or X. In the second case, each row will be considered as a response to be interpolated, and the number of columns of Xout must be equal to the spatial dimension.

interpCB

Function evaluating the interpolation Cardinal Basis. This function must have as its first 2 formals 'x', and 'xout'. It must return a matrix with length(x) columns and length(xout) rows. The j-th column is the vector of the values of the j-th cardinal basis function on the vector xout.

intOrder

Order of the one-dimensional interpolations. Must be a permutation of 1:d where d is the spatial dimension. NOT IMPLEMENTED YET. This argument is similar to the argument of the aperm method.

trace

Level of verbosity.

out_of_bounds

Function to handle Xout outside x (default is stop). Then Xout will be bounded by x range.

...

Further arguments to be passed to interpCB. NOT IMPLEMENTED YET.

Details

This is a grid interpolation as in gridInt but it is required here that the one-dimensional interpolation method is linear w.r.t. the vector of interpolated values. For each dimension, a one-dimensional interpolation is carried out, leading to a collection of interpolation problems each with a dimension reduced by one. The same cardinal basis can be used for all interpolations w.r.t. the same variable, so the number of call to the interpCB function is equal to the interpolation dimension, making this method very fast compared to the general grid interpolation as implemented in gridInt, see the Examples section.

Value

A single interpolated value if Xout is a vector or a row matrix. If Xout is a matrix with several rows, the result is a vector of interpolated values, in the order of the rows of Xout.

Author(s)

Yves Deville

Examples

## Natural spline for use through Cardinal Basis in 'gridIntCB'
myInterpCB <-  function(x, xout) cardinalBasis_natSpline(x = x, xout = xout)$CB
## Natural spline for use through Cardinal Basis
myInterp <- function(x, y, xout) {
     spline(x = x, y = y, n = 3 * length(x), method = "natural", xout = xout)$y
}

## generate Grid and function
set.seed(2468)
d <- 5
nLev <- 4L + rpois(d, lambda = 4)
a <- runif(d)
myFun2 <- function(x) exp(-crossprod(a, x^2))
myGD2 <- Grid(nlevels = nLev)
Y2 <- apply_Grid(myGD2, myFun2)
n <- 10
Xout3 <- matrix(runif(n * d), ncol = d)
t1 <- system.time(GI1 <- gridInt(X = myGD2, Y = Y2, Xout = Xout3, interpFun = myInterp))
t2 <- system.time(GI2 <- gridInt(X = myGD2, Y = Y2, Xout = Xout3, interpFun = myInterp,
                                 useC = FALSE))
t3 <- system.time(GI3 <- gridIntCB(X = myGD2, Y = Y2, Xout = Xout3, interpCB = myInterpCB))
df <- data.frame(true = apply(Xout3, 1, myFun2),
                 gridInt_C = GI1, gridInt_R = GI2, gridIntCB = GI3)
head(df)
rbind(gridInt_C = t1, gridInt_C = t2, gridIntCB = t3)


IRSN/smint documentation built on Dec. 9, 2023, 9:53 p.m.