rcpp 犰狳中的三维场(矢量)错误

An error with a three dimensional field (of vectors) in rcpp armadillo

由于超过三次迭代,我正在尝试在 rcpp arma 中创建一个字段,但出现错误。作为示例,请参见以下简单代码:

//[[Rcpp::export]]
field<vec> testgg(int k, int h, int g){
  field<vec> res(k, h, g);
  return(res);
}

我在这段代码中什么也没做,所以这段代码应该给我一些东西。但是,当我像这样执行此功能时出现错误。

> testgg(3,4,5)
Error in testgg(3, 4, 5) : 
  dims [product 12] do not match the length of object [60]
> testgg(3,4,1)
     [,1]      [,2]      [,3]      [,4]     
[1,] numeric,0 numeric,0 numeric,0 numeric,0
[2,] numeric,0 numeric,0 numeric,0 numeric,0
[3,] numeric,0 numeric,0 numeric,0 numeric,0

我的猜测是rcpp没有得到这个三维场,但是最多可以得到二维场。为什么会发生这种情况,如何获得三维场?

请查看未解决的问题 #263 以及其他问题。有一个错误,有一个修复,但你目前需要设置一个 #define 来启用修复(因为我们需要在假设当前行为的情况下对其他包是否有 side-effects 进行排序)。

简而言之,举个例子

> Sys.setenv(PKG_CPPFLAGS="-DRCPP_ARMADILLO_FIX_Field")  ## sets #define RCPP_ARMADILLO_FIX_Field
> Rcpp::cppFunction("arma::field<arma::vec> testgg(int k, int h, int g) { arma::field<arma::vec> res(k, h, g); return(res);}", depends="RcppArmadillo")
> testgg(2,3,4)
, , 1

     [,1]      [,2]      [,3]     
[1,] numeric,0 numeric,0 numeric,0
[2,] numeric,0 numeric,0 numeric,0

, , 2

     [,1]      [,2]      [,3]     
[1,] numeric,0 numeric,0 numeric,0
[2,] numeric,0 numeric,0 numeric,0

, , 3

     [,1]      [,2]      [,3]     
[1,] numeric,0 numeric,0 numeric,0
[2,] numeric,0 numeric,0 numeric,0

, , 4

     [,1]      [,2]      [,3]     
[1,] numeric,0 numeric,0 numeric,0
[2,] numeric,0 numeric,0 numeric,0

> 

因此,如果您使用正确的标志进行编译,则字段维度不会降低。

编辑 也许您想要 cube 而不是 field ?

> Rcpp::cppFunction("arma::cube testhh(int k, int h, int g) { arma::cube res(k, h, g); return(res);}", depends="RcppArmadillo")
> testhh(4,3,2)
, , 1

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0

, , 2

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0

>