RCPP中的一个三维场(矢量场)错误 [英] An error with a three dimensional field (of vectors) in rcpp armadillo

查看:0
本文介绍了RCPP中的一个三维场(矢量场)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于三次以上的迭代,我正在尝试在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

> 

这篇关于RCPP中的一个三维场(矢量场)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆