List of matrices multiplied by scalar, dimension attributes not stored in Rcpp

I use Rcpp to speed up some R-codes (and actually this is one of the elements of my “make” List for 2014), the part of the code is to multiply the list of matrices by a scalar, I am able to get results, except that the matrices are not longer matrices, they are vectors instead, and I want a list of matrices to be selected as the end result.

Here is the code that I still have:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;     
using namespace arma;     

// I got this template from here: http://stackoverflow.com/a/18014655/1315767
template <typename WHAT>
class ListOf : public List {
public:
    template <typename T>
    ListOf( const T& x) : List(x){}

    WHAT operator[](int i){ return as<WHAT>( ( (List*)this)->operator[]( i) ) ; }

} ;

// [[Rcpp::export]]


List FooList(NumericVector fi1, ListOf<NumericMatrix> Ct){

  List TempList(Ct.size());
  NumericMatrix ct(2,2);


  for(int i=0; i<Ct.size(); i++){
    ct = Ct[i] ;
    TempList[i] = ct * fi1[i] ;  // multiply each matrix by each scalar in fi1
  }
   return TempList;
}

When I run this code, I get the following:

> sourceCpp("FooList.cpp")
> A <- replicate(3,matrix(1:4, 2), simplify=FALSE) # a list of matrices
> vec <- 0.5 * c(1:3)                              # a vector
> FooList(vec, A)  # dim are not preserved
[[1]]
[1] 0.5 1.0 1.5 2.0

[[2]]
[1] 1 2 3 4

[[3]]
[1] 1.5 3.0 4.5 6.0

The output given FooListis fine, but the format is missing, I expected to get something like this:

[[1]]
     [,1] [,2]
[1,]  0.5  1.5
[2,]  1.0  2.0

[[2]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

[[3]]
     [,1] [,2]
[1,]  1.5  4.5
[2,]  3.0  6.0

, , ct - , fi1[i], , as_scalar(fi) , . ct.attr("dim") = Dimension(2, 2); sucess.

+4
1

, ++, Rcpp *, . - , ( ).

, :

FooList.R

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;

// I got this template from here: http://stackoverflow.com/a/18014655/1315767
template <typename WHAT>
class ListOf : public List {
public:
    template <typename T>
    ListOf( const T& x) : List(x){}

    WHAT operator[](int i){ return as<WHAT>( ( (List*)this)->operator[]( i) ) ; } 

} ;

// [[Rcpp::export]]


List FooList(NumericVector fi1, ListOf<NumericMatrix> Ct){

  List TempList(Ct.size());
  NumericMatrix ct(2,2);


  for(int i=0; i<Ct.size(); i++){
    ct = Ct[i] ;
    for (int j=0; j < ct.nrow(); j++) {
      for (int k=0; k < ct.ncol(); k++) {
        ct(j, k) *= fi1[i];  // Multiply each element of the matrix by the scalar in fi1
      }   
    }   
    TempList[i] = ct;                                                                                                                                                                                                                      
  }
   return TempList;
}

:

> sourceCpp("FooList.cpp")
> A <- replicate(3,matrix(1:4, 2), simplify=FALSE) # a list of matrices
> vec <- 0.5 * c(1:3)                              # a vector
> FooList(vec, A) 
[[1]]
     [,1] [,2]
[1,]  0.5  1.5
[2,]  1.0  2.0

[[2]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

[[3]]
     [,1] [,2]
[1,]  1.5  4.5
[2,]  3.0  6.0
+2

All Articles