C++ 如何使用RcppGSL定义函数序列?

C++ 如何使用RcppGSL定义函数序列?,c++,r,rcpp,C++,R,Rcpp,我想做一个二维样条插值。由于GSL库没有多维函数可供使用,我想我可以使用两步插值。也就是说,我在第二维值的网格上沿第一维插值,然后沿第二维插值。在R中,我可以很容易地创建一个样条函数列表,它可以连接这两个步骤。例如,我有一个向量x和y,对应的矩阵z=fx,y。现在我想在x0和y0的值处插值fx0,y0 有人想过如何在RcppGSL中创建函数序列吗?或者使用RcppGSL获得二维样条插值的其他替代方法 x <- 1:10 y <- 3:8 z <- matrix(rnorm(le

我想做一个二维样条插值。由于GSL库没有多维函数可供使用,我想我可以使用两步插值。也就是说,我在第二维值的网格上沿第一维插值,然后沿第二维插值。在R中,我可以很容易地创建一个样条函数列表,它可以连接这两个步骤。例如,我有一个向量x和y,对应的矩阵z=fx,y。现在我想在x0和y0的值处插值fx0,y0

有人想过如何在RcppGSL中创建函数序列吗?或者使用RcppGSL获得二维样条插值的其他替代方法

x <- 1:10
y <- 3:8
z <- matrix(rnorm(length(x)*length(y)), length(x), length(y))
x0 <- 2.2; y0 <- 4.5

# Create a sequence of spline functions conditional on y
spl.list <- vector("list", length(y))
for(i in 1:length(y)){
    spl.list[[i]] <- splinefun(x, z[,i], "natural")
}
# The function values at (x0, y).
intp1 <- sapply(1:length(y), function(i) spl.list[[i]](x0) )
# Create the spline function along y.
intp2.spl <- splinefun(y, intp1, "natural")
intp2.spl(y0)
src <- '
#include <RcppGSL.h>
#include <gsl/gsl_spline.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppGSL)]]

// [[Rcpp::export]]
double my_fn(NumericVector x, NumericVector y, double x0){
    int nx = x.length();
    gsl_interp_accel *accP  = gsl_interp_accel_alloc();
    gsl_spline *spline  = gsl_spline_alloc( gsl_interp_cspline , nx );
    gsl_spline_init( spline, x.begin(), y.begin(), nx);

    double out = gsl_spline_eval(spline, x0, accP);
    gsl_interp_accel_free (accP);
    gsl_spline_free (spline);
    return(out);
}
'
sourceCpp(code = src)