knitr::opts_chunk$set(echo = TRUE)
interpn is a N-dimensional linear interpolant based on the original Scilab function linear_interpln
Download the package from GitHub and then install and load it.
library(interpn)
Let's play with a simple sinus function:
x <- matrix(0:10, ncol = 11) y <- sin(x)
We want intermediate values of the function based on the linear interpolation:
xguess <- matrix(1.5:10.5, ncol = 10) yguess <- interpn(outmode = 2, y, x, xguess)
Note that the point (10.5) is out of bounds, see the value of the argument 'outmode' for some information on the possible extrapolation choices. Here value 4 is for 'by_NaN'.
print(c(sin(10.5), yguess[10]))
Plot the results:
plot(x, y, main = "interpn in 1D", type = "l") points(xguess, yguess, col = "red")
Let's define two variables now and a summation function:
x1 <- matrix(1:5, ncol = 5) x2 <- matrix(1:7, ncol = 7) y <- as.vector(outer(x1, x2, "+"))
Interpolate for three query points:
xguess1 <- c(1.5, 1.6, 1.7) # dimension 1 xguess2 <- c(2.5, 4.6, 6.7) # dimension 2 yguess <- interpn(outmode = 2, y, x1, x2, xguess1, xguess2)
print(yguess)
Finally let's see a 3D case with a multiplication function:
nx1 <- 4 # number of points in dimension 1 nx2 <- 7 # number of points in dimension 2 nx3 <- 10 # number of points in dimension 3 ny <- nx1 * nx2 * nx3 # dimension of the output vector y y <- double(ny) # size x1 <- as.double(seq(1, nx1)) # grid definition x2 <- as.double(seq(1, nx2)) x3 <- as.double(seq(1, nx3))
The multiplication is done with nested loops:
# function values a <- 1 for(i in 1:nx3){ for(j in 1:nx2){ for(k in 1:nx1){ y[a] <- x1[k] * x2[j] * x3[i] a <- a + 1 } } }
Approximating the multiplication...
# two query points xguess1 <- c(1.5, 2.5) xguess2 <- c(2.5, 3.5) xguess3 <- c(5, 10) yguess <- interpn(0, y, x1, x2, x3, xguess1, xguess2, xguess3) print(yguess)
to continue
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.