Error passing `arma :: cube`argument to function using RcppArmadillo

I am getting the following error when I try to compile with a package sourceCpp

from Rcpp

:

`my path to R/.../Rcpp/internal/Exporter.h`
no matching function for call to 'arma::Cube<double>::Cube(SEXPREC*&)'

      

The object cube

is the equivalent armadillo

array

in R

.

EDIT . Note that the problem is that the function cannot accept an object arma::cube

as an argument. If we change arma::cube B

to arma::mat B

, it works:

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

}

      

Any hint would be appreciated.

+3


source to share


3 answers


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

+3


source


I had the same problem. The problem seems to be related to the combination of "Rcpp :: export" and the cube as an argument to the exported function. I'm guessing that the sexp to cube converter hasn't been implemented yet (no pun intended ;-)). (Or we are both missing something ...).

A workaround when you want to have an arma :: cube argument in the Rcpp :: export function: first get it as a NumericVector and just create the cube after that ...



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

}

      

+3


source


I think your code is failing because it is implicitly trying to do the 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)
*/

      

+2


source







All Articles