[Rcpp-devel] Why this is not working

Dirk Eddelbuettel edd at debian.org
Tue Dec 4 03:24:51 CET 2012


Here is a version that builds. Not sure if it does what you want it to:

// [[Rcpp::depends(RcppArmadillo)]]

#include <RcppArmadillo.h>

using namespace Rcpp;

// [[Rcpp::export]]
List test(NumericVector yr, NumericMatrix Xr) {
  int n = Xr.nrow(), k = Xr.ncol();
  arma::mat X(Xr.begin(), n, k, false);
  arma::mat Y = arma::randu<arma::mat>(3,3);
  arma::colvec y(yr.begin(), yr.size(), false);
  X.each_col()+= y;
  Y.each_col()%= y;
  arma::mat Z = arma::repmat(X,4,5);
  return List::create(Named("Addition") = X,
		      Named("Multiplication") = Y, 
		      Named("Rep") =Z);
}


Demo:

R> Rcpp::sourceCpp('/tmp/honglang.cpp')
R> test(1:3, matrix(1:9,3))
$Addition
     [,1] [,2] [,3]
[1,]    2    5    8
[2,]    4    7   10
[3,]    6    9   12

$Multiplication
         [,1]     [,2]       [,3]
[1,] 0.717054 0.617308 0.82638894
[2,] 1.881861 1.079653 1.14938092
[3,] 2.333529 0.808417 0.00566863

$Rep
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
 [1,]    2    5    8    2    5    8    2    5    8     2     5     8     2     5
 [2,]    4    7   10    4    7   10    4    7   10     4     7    10     4     7
 [3,]    6    9   12    6    9   12    6    9   12     6     9    12     6     9
 [4,]    2    5    8    2    5    8    2    5    8     2     5     8     2     5
 [5,]    4    7   10    4    7   10    4    7   10     4     7    10     4     7
 [6,]    6    9   12    6    9   12    6    9   12     6     9    12     6     9
 [7,]    2    5    8    2    5    8    2    5    8     2     5     8     2     5
 [8,]    4    7   10    4    7   10    4    7   10     4     7    10     4     7
 [9,]    6    9   12    6    9   12    6    9   12     6     9    12     6     9
[10,]    2    5    8    2    5    8    2    5    8     2     5     8     2     5
[11,]    4    7   10    4    7   10    4    7   10     4     7    10     4     7
[12,]    6    9   12    6    9   12    6    9   12     6     9    12     6     9
      [,15]
 [1,]     8
 [2,]    10
 [3,]    12
 [4,]     8
 [5,]    10
 [6,]    12
 [7,]     8
 [8,]    10
 [9,]    12
[10,]     8
[11,]    10
[12,]    12


Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list