[Rcpp-devel] Eigen::RowMajor

romain at r-enthusiasts.com romain at r-enthusiasts.com
Wed Jan 18 08:52:51 CET 2012


Le 18 janv. 2012 à 00:27, Douglas Bates <bates at stat.wisc.edu> a écrit :

> On Tue, Jan 17, 2012 at 4:51 PM, Jelmer Ypma <jelmerypma at gmail.com> wrote:
>> 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?
> 
> Yes.  I do not have sufficient experience in template metaprogramming
> to be able to detect the RowMajor setting in the corresponding wrap
> function.  The wrap function for sparse matrices is, naturally, a
> specialization of eigen_wrap_plain_dense in the file
> RcppEigen/inst/include/RcppEigenWrap.h.  I believe Romain wrote that
> code as it looks beyond my capabilities.

I will have a look. 

> That templated function should have a specialization for
> Eigen::RowMajor and produce a Matrix::dgRMatrix object instead of a
> Matrix::dgCMatrix.  It happens that getting such an object back into
> Eigen from R would be a problem because the as methods for
> SparseMatrix<double> assume they will be passed a dgCMatrix.
> 
> Safest thing to do is to stick with dgCMatrix objects and Eigen::ColMajor.
> 
>> 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()
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


More information about the Rcpp-devel mailing list