由于三次以上的迭代,我试图在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没有得到这个三维场,但可以得到高达2维的场。为什么会发生这种情况,我怎样才能得到三维场?
请参阅公开问题#263,也许还有其他问题。有一个错误,有一个修复程序,但您目前需要设置一个#define
来启用修复程序(因为我们需要在假设当前行为的情况下,找出这是否会对其他包产生副作用(。
简而言之,例如
> 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
>