Create an interpolating function from given values. Several interpolation methods are supported.

ipol(val, grid = NULL, ...)

Arguments

val

array or function. Function values on a grid.

grid

list. Each element is a vector of ordered grid-points for a dimension.

...

Further arguments to the function, if is.function(val). And some extra arguments for interpolant creation described in section Details.

Value

A function(x, threads=getOption('chebpol.threads')) defined on a hypercube, an interpolant

for the given function. The argument x can be a matrix of column vectors which are evaluated in parallel in a number of threads. The function yields values for arguments outside the hypercube as well, though it will typically be a poor approximation. threads is an integer specifying the number of parallel threads which should be used when evaluating a matrix of column vectors.

Author

Simen Gaure

Examples


## evenly spaced grid-points
su <- seq(0,1,length.out=10)
## irregularly spaced grid-points
s <- su^3
## create approximation on the irregularly spaced grid
ml1 <- ipol(exp(s), grid=list(s))
## test it, since exp is convex, the linear approximation lies above
## the exp between the grid points
ml1(su) - exp(su)
#>  [1] 0.0000000000 0.0007963441 0.0023666771 0.0036681447 0.0028930495
#>  [6] 0.0111173403 0.0064666457 0.0191999204 0.0246303860 0.0000000000

## multi dimensional approximation
f <- function(x) 10/(1+25*mean(x^2))
# a 3-dimensional 10x10x10 grid, first and third coordinate are non-uniform
grid <- list(s, su, sort(1-s))

# make multilinear spline.
ml2 <- ipol(array(apply(expand.grid(grid), 1, f), c(10, 10, 10)), grid=grid)
# make 7 points in R3 to test them on
m <- matrix(runif(3*7),3)
rbind(true=apply(m,2,f), ml=ml2(m))
#>           [,1]     [,2]     [,3]      [,4]     [,5]     [,6]      [,7]
#> true 0.5822993 7.116889 2.076607 0.9263997 2.682573 1.010214 0.8778287
#> ml   0.5824148 6.687305 2.097367 0.9278375 2.681718 1.027964 0.8796506