[Rcpp-devel] Eigen::RowMajor

Jelmer Ypma jelmerypma at gmail.com
Tue Jan 17 23:51:04 CET 2012


Hi all,

I've been trying some examples using the RcppEigen package, which at
first sight seems to work great. However, I ran into something that I
don't completely understand. Adapting from
/pkg/RcppEigen/inst/unitTests/runit.sparse.R, I tried the code below,
where I use Eigen::RowMajor storage instead of the default. From C++ I
output the matrix mm, which is then passed back to R.

Within C++ the matrix is a 9 x 3 matrix as specified, but when the
matrix is passed to R, it turns into a 3 x 9 matrix. I'm assuming the
'C' in dgCMatrix stands form ColMajor. Does this mean that the
Rcpp::wrap (and potentially Rcpp::as) are not implemented for
Eigen::RowMajor sparse matrices?

Many thanks in advance,
Jelmer

The output from the script below is:
> fx_row1()
Nonzero entries:
(1,0) (1,0) (1,0) (1,1) (1,1) (1,1) (1,2) (1,2) (1,2)

Column pointers:
0 1 2 3 4 5 6 7 8  $

1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1

3 x 9 sparse Matrix of class "dgCMatrix"

[1,] 1 1 1 . . . . . .
[2,] . . . 1 1 1 . . .
[3,] . . . . . . 1 1 1


====================
library('inline')
library('RcppEigen')

fx_row <- cxxfunction( , '

    Eigen::SparseMatrix<double, Eigen::RowMajor>  mm(9,3);
    mm.reserve(9);
    for (int irow = 0; irow < 9; ++irow) {
        mm.startVec(irow);
        mm.insertBack(irow, irow / 3) = 1.0;
    }
    mm.finalize();
	
    Rcpp::Rcout << mm << std::endl;
    return wrap(mm);
' , plugin = "RcppEigen", verbose=TRUE )

fx_row()


More information about the Rcpp-devel mailing list