Reputation: 6723
Newbie to Rcpp here. I have a list of vector of vectors in C++, where I am using the vector of vectors to represent a matrix, say of size Mi x N
, where i
is from 1 to K
and K
is the number of elements of the list:
std::map<double, vector<vector<double> > x;
Put alternatively, for element 1 of the list, the vector of vector is of dimensions M1 x N
, for element 2 of the list, the vector of vector is of dimensions M2 x N
, ..., the vector of vector is of dimensions MK x N
for element K
of the list.
How can I convert this to a list of matrices in R. If I try:
return wrap(x)
I get a list of list of vectors, NOT list of matrices.
Here is a sample example, where x
in C++ is :
1st element of map:
within that, element of the 1st vector: 1 1 1 1
element of the 2nd vector: 1 1 1 1
element of the 3rd vector: 2 2 2 2
element of the 4th vector: 3 3 3 3
2nd element of map:
within that, element of the 1st vector: 4 4 4 4
element of the 2nd vector: 3 3 3 3
element of the 3rd vector: 2 2 2 2
element of the 4th vector: 5 5 5 5
element of the 5th vector: 1 1 1 1
If I try return wrap(x)
I get in R (p.s. extra indentation added below to make R's output clearer):
$`1`
$`1`[[1]]
[1] 1 1 1 1
$`1`[[2]]
[1] 1 1 1 1
$`1`[[3]]
[1] 2 2 2 2
$`1`[[4]]
[1] 3 3 3 3
$`2`
$`2`[[1]]
[1] 4 4 4 4
$`2`[[2]]
[1] 3 3 3 3
$`2`[[3]]
[1] 2 2 2 2
$`2`[[4]]
[1] 5 5 5 5
$`2`[[5]]
[1] 1 1 1 1
What I want to be returned in R is:
$`1`
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
[3,] 2 2 2 2
[4,] 3 3 3 3
$`2`
[,1] [,2] [,3] [,4]
[1,] 4 4 4 4
[2,] 3 3 3 3
[3,] 2 2 2 2
[4,] 5 5 5 5
[5,] 1 1 1 1
What could be the most efficient way to do this in RCpp?
Upvotes: 0
Views: 742
Reputation: 368509
It was your design choice: I am using the vector of vectors to represent a matrix. So maybe you have to write yourself a converter. As you are a relatively frequent poster here, you by now surely know that we have documentation on how to write your own wrap()
function in the vignette Rcpp-extending.
If you want to avoid that, my suggestion would be to actually use a matrix class when you need a matrix. Rcpp has one in NumericMatrix. RcppArmadillo and RcppEigen bring their own with additional focus on matrix algebra.
To me, RcppArmadillo offers a pretty perfect combination of features and ease of use. It also allows you to write code which depends only on Armadillo headers if you want to re-use the C++ code outside of R, but then very easily connect it to R thanks to Rcpp / RcppArmadillo.
Upvotes: 2
Reputation: 17642
vector<vector<>>
does not carry rectangularity information. I'd recommend you to have your own class MyMatrixClass
or whatever, which could very well hold a vector<vector<>>
if you like.
But you define MyMatrixClass::operator SEXP()
so that that it gets converted to the appropriate stucture. You can leverage the NumericMatrix
class for example.
wrap
does the best it can with the information it has at compile time, and it does not know all vectors will have the same size, so the best it can do is make a list of numeric vectors.
Upvotes: 1