Greetings, I am new to Rcpp, RInside and the entire family of related packages. I am experienced with R and also C++ using Eigen but I have never combined the two before. I would like to create a matrix using R and then play with it using Eigen. From what I have read so far, it seems like I can use RInside to create an Rcpp::NumericMatrix. My question is this: to transform a matrix from a Rcpp::NumericMatrix to an Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling C++/Eigen from R and not the other way around? Thanks, Stephen -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/82c04d8b/attachment.html>
[Rcpp-devel] clarification of interaction between RInside and RcppEigen
10 messages · Douglas Bates, Dirk Eddelbuettel, Stephen J. Barr
Probably the best place to start with RcppEigen is the vignette that Dirk and I wrote for the RcppEigen package. I can email you a copy of the PDF file off-list if you wish. With RcppEigen you can skip the creation of an Rcpp::NumericMatrix object if you wish and go directly to a mapped Eigen Matrix. There is a specialization of the Rcpp::as templated function that allows for creating an Eigen::MatrixXd or an Eigen::Map<Eigen::MatrixXd> object directly from an SEXP, which is the R's internal representation of data.
On Tue, Aug 7, 2012 at 12:55 PM, Stephen J. Barr <stephenjbarr at gmail.com> wrote:
Greetings, I am new to Rcpp, RInside and the entire family of related packages. I am experienced with R and also C++ using Eigen but I have never combined the two before. I would like to create a matrix using R and then play with it using Eigen. From what I have read so far, it seems like I can use RInside to create an Rcpp::NumericMatrix. My question is this: to transform a matrix from a Rcpp::NumericMatrix to an Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling C++/Eigen from R and not the other way around? Thanks, Stephen
_______________________________________________ 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
Hi Stephen,
On 7 August 2012 at 10:55, Stephen J. Barr wrote:
| Greetings,
|
| I am new to Rcpp, RInside and the entire family of related packages. I am
| experienced with R and also C++ using Eigen but I have never combined the two
| before.
You are in a good starting position.
| I would like to create a matrix using R and then play with it using
| Eigen. From what I have read so far, it seems like I can use RInside to create
| an Rcpp::NumericMatrix.?
That works. Even one of the very oldest examples (rinside_sample1.cpp) does
something related with a Rcpp::NumericMatrix (eg creates one and passes it around).
| My question is this: to transform a matrix from a Rcpp::NumericMatrix to an
| Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling C++/Eigen
| from R and not the other way around?
I think I have an idea as to what you are asking. I think it may help to step
back for a second and try to imagine the big picture:
a) Rcpp helps us with the interface defined by .Call:
SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)
so everthing going in and out is a SEXP type.
b) RInside lets you interchange with an embedded R process, again talking
SEXPs.
c) Packages like RcppEigen provide wrappers to and from SEXPs.
So if you don't need a Rcpp::NumericMatrix, don't create one. Create an
Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP
interfaces. That should work just fine with RInside as well.
You will have to expand the RInside Makefile to point to the RcppEigen
headers as well, and then it should just work.
Hope this helps, Dirk
| Thanks,
| Stephen?
|
| ----------------------------------------------------------------------
| _______________________________________________
| 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
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Thank you Dirk and also Douglas. This definitely helps. I think I can get this going. Thank you so much for the quick replies. Best, Stephen
On Tue, Aug 7, 2012 at 11:29 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
Hi Stephen,
On 7 August 2012 at 10:55, Stephen J. Barr wrote:
| Greetings,
|
| I am new to Rcpp, RInside and the entire family of related packages. I am
| experienced with R and also C++ using Eigen but I have never combined
the two
| before.
You are in a good starting position.
| I would like to create a matrix using R and then play with it using
| Eigen. From what I have read so far, it seems like I can use RInside to
create
| an Rcpp::NumericMatrix.
That works. Even one of the very oldest examples (rinside_sample1.cpp) does
something related with a Rcpp::NumericMatrix (eg creates one and passes it
around).
| My question is this: to transform a matrix from a Rcpp::NumericMatrix to
an
| Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling
C++/Eigen
| from R and not the other way around?
I think I have an idea as to what you are asking. I think it may help to
step
back for a second and try to imagine the big picture:
a) Rcpp helps us with the interface defined by .Call:
SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)
so everthing going in and out is a SEXP type.
b) RInside lets you interchange with an embedded R process, again talking
SEXPs.
c) Packages like RcppEigen provide wrappers to and from SEXPs.
So if you don't need a Rcpp::NumericMatrix, don't create one. Create an
Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP
interfaces. That should work just fine with RInside as well.
You will have to expand the RInside Makefile to point to the RcppEigen
headers as well, and then it should just work.
Hope this helps, Dirk
| Thanks,
| Stephen
|
| ----------------------------------------------------------------------
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/d0941f5f/attachment.html>
Hello again,
I am just trying to straighten this out. Say I want to have the exact same
random matrix in C++ as I do in R using rnorm. Is what I am doing below on
the right track?
Thanks,
Stephen
//
// Stephen J. Barr
// - trying to combine RInside and Eigen
#include <RInside.h>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]) {
RInside R(argc, argv);
int nrow = 3;
int ncol = 4;
R.parseEvalQ("set.seed(1)");
MatrixXd mymat = MatrixXd(nrow, ncol);
R.parseEvalQ("MYMAT = matrix(rnorm(3*12),nrow=3)");
const Map<MatrixXd> mymat(as<Map<MatrixXd>>(MYMAT));
exit(0);
}
On Tue, Aug 7, 2012 at 11:32 AM, Stephen J. Barr <stephenjbarr at gmail.com>wrote:
Thank you Dirk and also Douglas. This definitely helps. I think I can get this going. Thank you so much for the quick replies. Best, Stephen On Tue, Aug 7, 2012 at 11:29 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
Hi Stephen,
On 7 August 2012 at 10:55, Stephen J. Barr wrote:
| Greetings,
|
| I am new to Rcpp, RInside and the entire family of related packages. I
am
| experienced with R and also C++ using Eigen but I have never combined
the two
| before.
You are in a good starting position.
| I would like to create a matrix using R and then play with it using
| Eigen. From what I have read so far, it seems like I can use RInside to
create
| an Rcpp::NumericMatrix.
That works. Even one of the very oldest examples (rinside_sample1.cpp)
does
something related with a Rcpp::NumericMatrix (eg creates one and passes
it around).
| My question is this: to transform a matrix from a Rcpp::NumericMatrix
to an
| Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling
C++/Eigen
| from R and not the other way around?
I think I have an idea as to what you are asking. I think it may help to
step
back for a second and try to imagine the big picture:
a) Rcpp helps us with the interface defined by .Call:
SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)
so everthing going in and out is a SEXP type.
b) RInside lets you interchange with an embedded R process, again talking
SEXPs.
c) Packages like RcppEigen provide wrappers to and from SEXPs.
So if you don't need a Rcpp::NumericMatrix, don't create one. Create an
Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP
interfaces. That should work just fine with RInside as well.
You will have to expand the RInside Makefile to point to the RcppEigen
headers as well, and then it should just work.
Hope this helps, Dirk
| Thanks,
| Stephen
|
| ----------------------------------------------------------------------
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/e98ae0fc/attachment.html>
Ah, I see the error. Please disregard this. I see how Rcpp::as works now. Best, Stephen
On Tue, Aug 7, 2012 at 2:08 PM, Stephen J. Barr <stephenjbarr at gmail.com>wrote:
Hello again,
I am just trying to straighten this out. Say I want to have the exact same
random matrix in C++ as I do in R using rnorm. Is what I am doing below on
the right track?
Thanks,
Stephen
//
// Stephen J. Barr
// - trying to combine RInside and Eigen
#include <RInside.h>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]) {
RInside R(argc, argv);
int nrow = 3;
int ncol = 4;
R.parseEvalQ("set.seed(1)");
MatrixXd mymat = MatrixXd(nrow, ncol);
R.parseEvalQ("MYMAT = matrix(rnorm(3*12),nrow=3)");
const Map<MatrixXd> mymat(as<Map<MatrixXd>>(MYMAT));
exit(0);
}
On Tue, Aug 7, 2012 at 11:32 AM, Stephen J. Barr <stephenjbarr at gmail.com>wrote:
Thank you Dirk and also Douglas. This definitely helps. I think I can get this going. Thank you so much for the quick replies. Best, Stephen On Tue, Aug 7, 2012 at 11:29 AM, Dirk Eddelbuettel <edd at debian.org>wrote:
Hi Stephen,
On 7 August 2012 at 10:55, Stephen J. Barr wrote:
| Greetings,
|
| I am new to Rcpp, RInside and the entire family of related packages. I
am
| experienced with R and also C++ using Eigen but I have never combined
the two
| before.
You are in a good starting position.
| I would like to create a matrix using R and then play with it using
| Eigen. From what I have read so far, it seems like I can use RInside
to create
| an Rcpp::NumericMatrix.
That works. Even one of the very oldest examples (rinside_sample1.cpp)
does
something related with a Rcpp::NumericMatrix (eg creates one and passes
it around).
| My question is this: to transform a matrix from a Rcpp::NumericMatrix
to an
| Eigen MatrixXd, do I use RcppEigen? Or is RcppEigen only for calling
C++/Eigen
| from R and not the other way around?
I think I have an idea as to what you are asking. I think it may help to
step
back for a second and try to imagine the big picture:
a) Rcpp helps us with the interface defined by .Call:
SEXP .Call(string somefuncname, SEXP arg1, SEXP arg2, ...)
so everthing going in and out is a SEXP type.
b) RInside lets you interchange with an embedded R process, again talking
SEXPs.
c) Packages like RcppEigen provide wrappers to and from SEXPs.
So if you don't need a Rcpp::NumericMatrix, don't create one. Create an
Eigen matrix, and use RcppEigen's wrappers to pass it throught the SEXP
interfaces. That should work just fine with RInside as well.
You will have to expand the RInside Makefile to point to the RcppEigen
headers as well, and then it should just work.
Hope this helps, Dirk
| Thanks,
| Stephen
|
| ----------------------------------------------------------------------
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/90b2e8cf/attachment-0001.html>
Also note that "just" getting random numbers is taken care of via Rcpp sugar which wraps rnorm() etc: R> library(inline) R> R> R> f <- cxxfunction(signature(), plugin="Rcpp", body=' + Rcpp::RNGScope tmp; + return rnorm(3); + ') R> R> set.seed(42) R> rnorm(3) ## using R [1] 1.370958 -0.564698 0.363128 R> set.seed(42) R> f() ## using the Rcpp function just created [1] 1.370958 -0.564698 0.363128 R> Same RNG draws, as we deal properly with the RNG state (where RNGScope helps). Look at the list archives, this has been discussed a few times if it seems too "magic". Dirk
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Thanks for the pointer to sugar.
I was just trying to think of a simple example. So my C++ code will be
essentially:
int main() {
RInside R(argc, argv); // create an embedded R instance
R.parseEval("X = generateSomeMatrix()");
Eigen::MatrixXd Xeigen;
// this is the part that I am still a bit unclear of:
// use Rcpp::as to make Xeigen take the value of X
}
On Tue, Aug 7, 2012 at 2:34 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
Also note that "just" getting random numbers is taken care of via Rcpp sugar which wraps rnorm() etc: R> library(inline) R> R> R> f <- cxxfunction(signature(), plugin="Rcpp", body=' + Rcpp::RNGScope tmp; + return rnorm(3); + ') R> R> set.seed(42) R> rnorm(3) ## using R [1] 1.370958 -0.564698 0.363128 R> set.seed(42) R> f() ## using the Rcpp function just created [1] 1.370958 -0.564698 0.363128 R> Same RNG draws, as we deal properly with the RNG state (where RNGScope helps). Look at the list archives, this has been discussed a few times if it seems too "magic". Dirk -- Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/1dae211c/attachment.html>
On 7 August 2012 at 14:43, Stephen J. Barr wrote:
| Thanks for the pointer to sugar.?
|
| I was just trying to think of a simple example. So my C++ code will be
| essentially:
|
| int main() {
|
| ? ? RInside R(argc, argv); ? ? ? ? ?// create an embedded R instance?
| ? ? R.parseEval("X = generateSomeMatrix()");
You need something like we use in the example rinside_sample1.cpp I already
pointed you too:
Rcpp::NumericVector v = R.parseEval(str); // eval string, Z then assigned to num. vec
as you need __assign__ the result of the parseEval() to something.
This could be as generic as
SEXP mytmp = R.parseEval("X = generateSomeMatrix()");
and you can then use mytmp instantiate an RcppEigen object as you would have
if the object had been passed down from R.
Hth, Dirk
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Ah, that made it click. Thank you so much for the help.
On Tue, Aug 7, 2012 at 2:56 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
On 7 August 2012 at 14:43, Stephen J. Barr wrote:
| Thanks for the pointer to sugar.
|
| I was just trying to think of a simple example. So my C++ code will be
| essentially:
|
| int main() {
|
| RInside R(argc, argv); // create an embedded R instance
| R.parseEval("X = generateSomeMatrix()");
You need something like we use in the example rinside_sample1.cpp I already
pointed you too:
Rcpp::NumericVector v = R.parseEval(str); // eval string, Z then
assigned to num. vec
as you need __assign__ the result of the parseEval() to something.
This could be as generic as
SEXP mytmp = R.parseEval("X = generateSomeMatrix()");
and you can then use mytmp instantiate an RcppEigen object as you would
have
if the object had been passed down from R.
Hth, Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120807/353b856e/attachment.html>