语法:使用Vectorize对此示例中的多维函数进行矢量化R

时间:2017-05-04 21:11:56

标签: r function syntax vectorization interpolation

我正在查看这篇简短文档中的最后一个例子:

https://www.rdocumentation.org/packages/chebpol/versions/1.3-952/topics/mlappx

转载于此,

require(chebpol)
## 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 <- Vectorize(mlappx(exp,list(s)))
## test it, since exp is convex, the linear approximation lies above
## the exp between the grid points
ml1(su) - exp(su)

## multi linear approx
f <- function(x) exp(sum(x^2))
grid <- list(s,su)

ml2 <- mlappx(evalongrid(f,grid=grid),grid)
# an equivalent would be ml2 <- mlappx(f,grid)

a <- runif(2); ml2(a); f(a)
# we also get an approximation outside of the domain, of disputable quality
ml2(c(1,2)); f(c(1,2))

最后一行评估点(1,2)的f的近似值(通过线性插值)。如果我们使用以下方法进行矢量化,那么用它来评估多对点的语法是什么:

ml2 <- Vectorize(mlappx(evalongrid(f,grid=grid),grid))

1 个答案:

答案 0 :(得分:1)

只需简短说明。我是chebpol的作者。最近,我已经使用其他几种插值方法对其进行了升级。它们通过ipol()函数具有统一的接口,并且都被矢量化(它们可以采用列矢量矩阵)并被并行化。