[英]error when passing `arma::cube`argument to function using RcppArmadillo
I am getting the following error when trying to compile using sourceCpp
from Rcpp
package:尝试使用
Rcpp
包中的sourceCpp
进行编译时出现以下错误:
`my path to R/.../Rcpp/internal/Exporter.h`
no matching function for call to 'arma::Cube<double>::Cube(SEXPREC*&)'
The object cube
is the armadillo
equivalent of an array
in R
.对象
cube
是R
array
的armadillo
等价物。
EDIT : Note that the problem seems to be that the function can't accept a arma::cube
object as an argument.编辑:请注意,问题似乎是该函数不能接受
arma::cube
对象作为参数。 If we change arma::cube B
by arma::mat B
it does work:如果我们通过
arma::mat B
更改arma::cube B
arma::mat B
它确实有效:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace arma;
// [[Rcpp::export]]
arma::cube ssmooth(arma::mat A,
arma::cube B) {
int ns = A.n_rows;
int nk = A.n_cols;
int np = B.n_rows;
arma::mat C = zeros<mat>(nk, ns);
arma::cube D = zeros<cube>(nk, nk, ns);
return D;
}
I would appreciate any hint.我将不胜感激任何提示。
A basic example works:一个基本的例子有效:
R> cppFunction("arma::cube getCube(int n) { arma::cube a(n,n,n);\
a.zeros(); return a; }", depends="RcppArmadillo")
R> getCube(2)
, , 1
[,1] [,2]
[1,] 0 0
[2,] 0 0
, , 2
[,1] [,2]
[1,] 0 0
[2,] 0 0
R>
so either you are doing something wrong or your installation is off.所以要么你做错了什么,要么你的安装被关闭了。
I had the same issue.我遇到过同样的问题。 The problem seems to be related to the combination "Rcpp::export" and cube as an argument of the exported function.
该问题似乎与组合“Rcpp::export”和作为导出函数参数的立方体有关。 My guess is that the converter from sexp to cube may not be implemented yet (no pun intended ;-)).
我的猜测是从 sexp 到立方体的转换器可能还没有实现(没有双关语;-))。 (Or we are both missing something...).
(或者我们都错过了一些东西......)。
Workaround when you want to have an arma::cube argument in a Rcpp::export function: get it first as a NumericVector and simply create the cube afterward...当您想在 Rcpp::export 函数中使用 arma::cube 参数时的解决方法:首先将其作为 NumericVector 获取,然后简单地创建多维数据集...
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace arma;
// [[Rcpp::export]]
arma::cube ssmooth(arma::mat A,
NumericVector B_) {
IntegerVector dimB=B_.attr("dim");
arma::cube B(B_.begin(), dimB[0], dimB[1], dimB[2]);
//(rest of your code unchanged...)
int ns = A.n_rows;
int nk = A.n_cols;
int np = B.n_rows;
arma::mat C = zeros<mat>(nk, ns);
arma::cube D = zeros<cube>(nk, nk, ns);
return D;
}
I think your code fails because implicitly it tries to do casting like this:我认为您的代码失败了,因为它隐式地尝试进行这样的转换:
#include<RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
cube return_thing(SEXP thing1){
cube thing2 = as<cube>(thing1);
return thing2;
}
/***R
thing <- 1:8
dim(thing) <- c(2, 2, 2)
return_thing(thing)
*/
which doesn't work, whereas it works for matrices:这不起作用,而它适用于矩阵:
#include<RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
mat return_thing(SEXP thing1){
mat thing2 = as<mat>(thing1);
return thing2;
}
/***R
thing <- 1:4
dim(thing) <- c(2, 2)
return_thing(thing)
*/
I am able to read and return an arma cube with the following function :我能够读取并返回具有以下功能的 arma 立方体:
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
arma::cube return_cube(arma::cube X)
{
return(X);
}
For example, I obtain the following result when I run the following in R :例如,当我在R 中运行以下命令时,我得到以下结果:
my_cube <- array(data = rnorm(5 * 3 * 2), dim = c(5,3, 2))
return_cube(my_cube)
, , 1
[,1] [,2] [,3]
[1,] 0.4815994 1.0863765 0.3278728
[2,] 1.4138699 -0.7809922 0.8341867
[3,] 0.6555752 -0.2708001 0.7701501
[4,] 1.1447104 -1.4064894 -0.2653888
[5,] 1.5972670 1.8368235 -2.2814959
, , 2
[,1] [,2] [,3]
[1,] -0.485091067 1.1826162 -0.3524851
[2,] 0.227652584 0.3005968 -0.6079604
[3,] -0.147653664 1.3463318 -1.2238623
[4,] 0.067090580 -0.8982740 -0.8903684
[5,] 0.006421618 -1.7156955 -1.2813880
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.