Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error when passing `arma::cube`argument to function using RcppArmadillo

I am getting the following error when trying to compile using sourceCpp from Rcpppackage:

`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.

EDIT: Note that the problem seems to be that the function can't accept a arma::cube object as an argument. If we change arma::cube Bby arma::mat Bit does work:

#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.

like image 800
nopeva Avatar asked Dec 08 '25 09:12

nopeva


2 Answers

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.

like image 69
Dirk Eddelbuettel Avatar answered Dec 10 '25 00:12

Dirk Eddelbuettel


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. My guess is that the converter from sexp to cube may not be implemented yet (no pun intended ;-)). (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...

#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;

}
like image 34
Pascal Crépey Avatar answered Dec 10 '25 00:12

Pascal Crépey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!