[Rcpp-devel] Using sparse matrices from the Matrix package in Rcpp
Søren Højsgaard
sorenh at math.aau.dk
Mon Jun 25 23:57:58 CEST 2012
I am trying to extract elements from a sparse matrix using RcppEigen by a function called Aiijj (to mimic A[ii,jj]). I do
library(inline)
library(RcppEigen)
src <- '
using Eigen::Map;
using Eigen::SparseMatrix;
using Eigen::MappedSparseMatrix;
using Eigen::VectorXd;
using namespace Rcpp;
typedef Eigen::MappedSparseMatrix<double> MSpMat;
const MSpMat A(as<MSpMat>(AA_));
int i = as<int>(ii_)-1;
int j = as<int>(jj_)-1;
double ans;
ans = A(i,j);
return(wrap(ans));
'
Aiijj <- cxxfunction(signature(AA_="matrix", ii_="integer", jj_="integer"),
body=src, plugin="RcppEigen")
Compilation gives:
31: using Eigen::Map;
32: using Eigen::SparseMatrix;
33: using Eigen::MappedSparseMatrix;
34: using Eigen::VectorXd;
35: using namespace Rcpp;
36: typedef Eigen::MappedSparseMatrix<double> MSpMat;
37: const MSpMat A(as<MSpMat>(AA_));
38: int i = as<int>(ii_)-1;
39: int j = as<int>(jj_)-1;
40: double ans;
41: ans = A(i,j);
42: return(wrap(ans));
43:
44: END_RCPP
45: }
46:
47:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file82c57e4a4f.cpp: In function 'SEXPREC* file82c57e4a4f(SEXP, SEXP, SEXP)':
file82c57e4a4f.cpp:41:12: error: no match for call to '(const MSpMat {aka const Eigen::MappedSparseMatrix<double>}) (int&, int&)'
make: *** [file82c57e4a4f.o] Error 1
In addition: Warning message:
To me it seems that I can not acces entries (i,j) using A(i,j) when A is a sparse matrix. Is that so? - and if yes, any suggestions on what I can do alternatively?
(I am new to the Rcpp-world so apologies if this is a trivial question).
Regards
Søren
-----Original Message-----
From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of Douglas Bates
Sent: 25. juni 2012 17:37
To: Dirk Eddelbuettel
Cc: rcpp-devel at r-forge.wu-wien.ac.at
Subject: Re: [Rcpp-devel] Using sparse matrices from the Matrix package in Rcpp
On Sun, Jun 24, 2012 at 1:19 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 24 June 2012 at 19:56, Glenn Lawyer wrote:
> | I would suggest using the eigen library, Rccp has an interface to this.
> | Alternately, you could link to the boost graph library.
>
> Spot on.
>
> It was in fact the availability of (more) sparse matrix methods (even
> though they were (are ?) under development and not fully mature) that
> lead Doug from Armadillo to Eigen. I am sure he will follow-up here too.
Indeed. There is good support for sparse matrices in Eigen including the use of the Cholmod functions from the Matrix package, although the latter requires a bit of effort.
With the release of Eigen 3.1 today, sparse matrices are now a supported part of Eigen. I have not yet incorporated Eigen 3.1 into RcppEigen but will do so.
> Boost Graph is interesting as CRAN had the RBGL package to access it
> for such a long time. It would be nice if someone looked more closely
> if we can in fact add appropriate as<>() and wrap() methods/functions
> to make interfacing this library more easy / more accessible from R.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> _______________________________________________
> 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-deve
> l
_______________________________________________
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