Re: [Rcpp-devel] Integrals with Rcpp
Thanks a lot, Baptiste! I'll check your code asap. Cheers, /f On Tue, Jan 13, 2015 at 7:14 PM, Baptiste Auguie wrote: > Hi, > > I might be the author of the SO question you refer to. In the planar > package I've tried a number of different strategies for numerical > integration, but the most efficient I managed was at the C++ level, with > direct call to the cubature library, > https://github.com/baptiste/planar/blob/master/src/gaussian_beam.cpp#L478 > > At the time, I wrote for myself a minimal example, > https://github.com/baptiste/cubature/blob/master/minimal.c > > HTH, > > baptiste > > On 9 January 2015 at 11:10, Federico Andreis > wrote: > >> Dear all, >> >> I was wondering what, in your opinion, would be the best way to handle >> the computation of definite integrals using Rcpp. >> >> To be more precise, I need to evaluate a double integral that turns out >> to be somewhat nasty (really slow computation and presumably inaccurate >> results using the R cubature package). >> >> Should I write the integration algorithm from scratch, or is there any >> external library you would suggest? >> >> I've already found the post on Stackoverflow 'using C function from other >> package in Rcpp' that dealt with an integration problem as well, but it >> looked too general for my problem and also, it's one year old, maybe >> something else has turned up in the meanwhile.. >> >> Thanks in advance, and congrats for the great work with Rcpp! >> >> /federico >> >> ___ >> Rcpp-devel mailing list >> Rcpp-devel@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@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Advice on making interface into existing C++ library
On 13 January 2015 at 08:51, Jeffrey Wong wrote: | Thanks Dirk, would I still create a rcpp skeleton package and dump the *.cpp | files into src, then just use Rcpp::export? What about all the .h files in that | library? Yes you need those too, either in the same directory or in one the compiler is told as about as eg via PKG_CXXFLAGS = -I../../inst/include as we often do when export headers for use by other packages. Rcpp has 321 reverse dependencies on CRAN. That is 321 worked, and working, examples of how to do this. Use them to your advantage. Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Integrals with Rcpp
On 13 January 2015 at 15:14, Baptiste Auguie wrote: | Hi, | | I might be the author of the SO question you refer to. In the planar package | I've tried a number of different strategies for numerical integration, but the | most efficient I managed was at the C++ level, with direct call to the cubature | library, | https://github.com/baptiste/planar/blob/master/src/gaussian_beam.cpp#L478 | | At the time, I wrote for myself a minimal example, https://github.com/baptiste/ | cubature/blob/master/minimal.c You did just say you were turning this into a piece for the Rcpp Gallery, didn't you? ;-) Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Integrals with Rcpp
On 14 January 2015 at 10:01, Dirk Eddelbuettel wrote: > > On 13 January 2015 at 15:14, Baptiste Auguie wrote: > | Hi, > | > | I might be the author of the SO question you refer to. In the planar > package > | I've tried a number of different strategies for numerical integration, > but the > | most efficient I managed was at the C++ level, with direct call to the > cubature > | library, > | > https://github.com/baptiste/planar/blob/master/src/gaussian_beam.cpp#L478 > | > | At the time, I wrote for myself a minimal example, > https://github.com/baptiste/ > | cubature/blob/master/minimal.c > > You did just say you were turning this into a piece for the Rcpp Gallery, > didn't you? ;-) > > Good point, I'll try to find the time. b. Dirk > > -- > http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org > ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Rcpp modules and S3 dispatch on rbind/cbind
Just for the list record I think I've figured this out. It turns out that there is an S4 approach to this now, whereby one actually sets appropriate methods for the rbind2 and cbind2 functions in the methods package and then call: On 12 January 2015 at 13:44, Louis Aslett wrote: > I've encountered a problem when trying to perform S3 method dispatch > for rbind() with an Rcpp module I've written. Obviously Rcpp modules > are S4, but as per many Google-able discussions, rbind/cbind can't > support S4 method dispatch due to the first argument being a > dot-dot-dot one, so S3 on the first argument type is the best that can > be done. I think this is good enough for my problem as my types can't > be mixed with base types so I've a fairly short list of possible first > argument types and can disentangle in my own code. > > I won't bog the list down with my real problem which is hundreds of > lines of code ... I've managed to narrow it down to the following > minimal working example which produces the same issue (description to > follow below). > > == Start content of test.cpp == > #include > using namespace Rcpp; > > class TestClass1 { > public: > TestClass1() {} > void hello() const { > Rcout << "Hello world!" << std::endl; > } > }; > > class TestClass2 { > public: > TestClass2() {} > void hello() const { > Rcout << "Hello world!" << std::endl; > } > }; > > RCPP_MODULE(test_mod) { > class_( "TestClass1" ) > .constructor() > .method("hello", &TestClass1::hello) > ; > > class_( "TestClass2" ) > .constructor() > .method("hello", &TestClass2::hello) > ; > } > == End content of test.cpp== > > == Start example R script == > Rcpp::sourceCpp('test.cpp') > > # Define S3 method for the Rcpp class types > rbind.Rcpp_TestClass1 <- function(...) { > args <- list(...) > cat("Calling type 1 hello\n") > args[[1]]$hello() > } > rbind.Rcpp_TestClass2 <- function(...) { > args <- list(...) > cat("Calling type 2 hello\n") > args[[1]]$hello() > } > > a <- new(TestClass1) > b <- new(TestClass2) > > # OK, this goes to plan ... I can has an Rcpp module, many of same > # type or mix with R types > rbind(a) > rbind(a,a) > rbind(a,2,matrix(1:4,2)) > rbind(b) > rbind(b,b) > rbind(b,2,matrix(1:4,2)) > > # ... but I can't mix two Rcpp module types > rbind(a,b) > rbind(b,a) > > # Get rid of one of the S3 methods and it works to mix Rcpp module types > rm(rbind.Rcpp_TestClass2) > rbind(a,b) > == End example R script == > > The output for the problem part is: > >> # ... but I can't mix two Rcpp module types >> rbind(a,b) > Error in rbind(a, b) : environments cannot be coerced to other types >> rbind(b,a) > Error in rbind(b, a) : environments cannot be coerced to other types > > All other lines in the script behave as expected. > > So in a nut shell the problem seems to be that one can't have first > argument S3 method dispatch defined for more than one Rcpp module type > at a time. I am mystified why this should be? Any insights greatly > appreciated! > > All the best, > > Louis > > > PS In case this arrives twice in error I resent from my subscribed address. ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Rcpp modules and S3 dispatch on rbind/cbind
Sorry hit send prematurely Just for the list record I think I've figured this out. It turns out that there is an S4 approach to this now, whereby one actually sets appropriate methods for the rbind2 and cbind2 functions in the methods package and then call: methods:::bind_activation(on = TRUE) This then recursively calls rbind2 and cbind2 with pairs of arguments whenever the base rbind/cbind are called with S4 arguments matching the signature. Of course, it means all the other functions such as nrow/ncol/rownames/... must be overridden too for the classes used, in order that the knitting together that the methods package does will work correctly. See the rbind.R source file in the methods package for details. Hope that helps if anyone encounters this in future, Louis On 12 January 2015 at 13:44, Louis Aslett wrote: > I've encountered a problem when trying to perform S3 method dispatch > for rbind() with an Rcpp module I've written. Obviously Rcpp modules > are S4, but as per many Google-able discussions, rbind/cbind can't > support S4 method dispatch due to the first argument being a > dot-dot-dot one, so S3 on the first argument type is the best that can > be done. I think this is good enough for my problem as my types can't > be mixed with base types so I've a fairly short list of possible first > argument types and can disentangle in my own code. > > I won't bog the list down with my real problem which is hundreds of > lines of code ... I've managed to narrow it down to the following > minimal working example which produces the same issue (description to > follow below). > > == Start content of test.cpp == > #include > using namespace Rcpp; > > class TestClass1 { > public: > TestClass1() {} > void hello() const { > Rcout << "Hello world!" << std::endl; > } > }; > > class TestClass2 { > public: > TestClass2() {} > void hello() const { > Rcout << "Hello world!" << std::endl; > } > }; > > RCPP_MODULE(test_mod) { > class_( "TestClass1" ) > .constructor() > .method("hello", &TestClass1::hello) > ; > > class_( "TestClass2" ) > .constructor() > .method("hello", &TestClass2::hello) > ; > } > == End content of test.cpp== > > == Start example R script == > Rcpp::sourceCpp('test.cpp') > > # Define S3 method for the Rcpp class types > rbind.Rcpp_TestClass1 <- function(...) { > args <- list(...) > cat("Calling type 1 hello\n") > args[[1]]$hello() > } > rbind.Rcpp_TestClass2 <- function(...) { > args <- list(...) > cat("Calling type 2 hello\n") > args[[1]]$hello() > } > > a <- new(TestClass1) > b <- new(TestClass2) > > # OK, this goes to plan ... I can has an Rcpp module, many of same > # type or mix with R types > rbind(a) > rbind(a,a) > rbind(a,2,matrix(1:4,2)) > rbind(b) > rbind(b,b) > rbind(b,2,matrix(1:4,2)) > > # ... but I can't mix two Rcpp module types > rbind(a,b) > rbind(b,a) > > # Get rid of one of the S3 methods and it works to mix Rcpp module types > rm(rbind.Rcpp_TestClass2) > rbind(a,b) > == End example R script == > > The output for the problem part is: > >> # ... but I can't mix two Rcpp module types >> rbind(a,b) > Error in rbind(a, b) : environments cannot be coerced to other types >> rbind(b,a) > Error in rbind(b, a) : environments cannot be coerced to other types > > All other lines in the script behave as expected. > > So in a nut shell the problem seems to be that one can't have first > argument S3 method dispatch defined for more than one Rcpp module type > at a time. I am mystified why this should be? Any insights greatly > appreciated! > > All the best, > > Louis > > > PS In case this arrives twice in error I resent from my subscribed address. ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Rcpp modules and S3 dispatch on rbind/cbind
On 14 January 2015 at 15:00, Louis Aslett wrote: | Sorry hit send prematurely | | Just for the list record I think I've figured this out. It turns out | that there is an S4 approach to this now, whereby one actually sets Sweet, and well done. I had meant to reply and suggest something like that -- Modules, after all, are "just" extensions of S4 and don;t add anything to R proper (as they can't go there anyway). But I do so little S4 that my usual approach is to hide my head in the sand til Martin Morgan comes along and explains it :) | appropriate methods for the rbind2 and cbind2 functions in the methods | package and then call: | | methods:::bind_activation(on = TRUE) | | This then recursively calls rbind2 and cbind2 with pairs of arguments | whenever the base rbind/cbind are called with S4 arguments matching | the signature. Of course, it means all the other functions such as | nrow/ncol/rownames/... must be overridden too for the classes used, in | order that the knitting together that the methods package does will | work correctly. See the rbind.R source file in the methods package | for details. | | Hope that helps if anyone encounters this in future, I think you also just volunteered a little demo for the Rcpp Gallery :) [ Only half joking. We seem to have a buglet at the knitr / jekyll side of things as this works via sourceCpp but not in the default Rcpp Gallery setup. ] Dirk | Louis | | | On 12 January 2015 at 13:44, Louis Aslett wrote: | > I've encountered a problem when trying to perform S3 method dispatch | > for rbind() with an Rcpp module I've written. Obviously Rcpp modules | > are S4, but as per many Google-able discussions, rbind/cbind can't | > support S4 method dispatch due to the first argument being a | > dot-dot-dot one, so S3 on the first argument type is the best that can | > be done. I think this is good enough for my problem as my types can't | > be mixed with base types so I've a fairly short list of possible first | > argument types and can disentangle in my own code. | > | > I won't bog the list down with my real problem which is hundreds of | > lines of code ... I've managed to narrow it down to the following | > minimal working example which produces the same issue (description to | > follow below). | > | > == Start content of test.cpp == | > #include | > using namespace Rcpp; | > | > class TestClass1 { | > public: | > TestClass1() {} | > void hello() const { | > Rcout << "Hello world!" << std::endl; | > } | > }; | > | > class TestClass2 { | > public: | > TestClass2() {} | > void hello() const { | > Rcout << "Hello world!" << std::endl; | > } | > }; | > | > RCPP_MODULE(test_mod) { | > class_( "TestClass1" ) | > .constructor() | > .method("hello", &TestClass1::hello) | > ; | > | > class_( "TestClass2" ) | > .constructor() | > .method("hello", &TestClass2::hello) | > ; | > } | > == End content of test.cpp== | > | > == Start example R script == | > Rcpp::sourceCpp('test.cpp') | > | > # Define S3 method for the Rcpp class types | > rbind.Rcpp_TestClass1 <- function(...) { | > args <- list(...) | > cat("Calling type 1 hello\n") | > args[[1]]$hello() | > } | > rbind.Rcpp_TestClass2 <- function(...) { | > args <- list(...) | > cat("Calling type 2 hello\n") | > args[[1]]$hello() | > } | > | > a <- new(TestClass1) | > b <- new(TestClass2) | > | > # OK, this goes to plan ... I can has an Rcpp module, many of same | > # type or mix with R types | > rbind(a) | > rbind(a,a) | > rbind(a,2,matrix(1:4,2)) | > rbind(b) | > rbind(b,b) | > rbind(b,2,matrix(1:4,2)) | > | > # ... but I can't mix two Rcpp module types | > rbind(a,b) | > rbind(b,a) | > | > # Get rid of one of the S3 methods and it works to mix Rcpp module types | > rm(rbind.Rcpp_TestClass2) | > rbind(a,b) | > == End example R script == | > | > The output for the problem part is: | > | >> # ... but I can't mix two Rcpp module types | >> rbind(a,b) | > Error in rbind(a, b) : environments cannot be coerced to other types | >> rbind(b,a) | > Error in rbind(b, a) : environments cannot be coerced to other types | > | > All other lines in the script behave as expected. | > | > So in a nut shell the problem seems to be that one can't have first | > argument S3 method dispatch defined for more than one Rcpp module type | > at a time. I am mystified why this should be? Any insights greatly | > appreciated! | > | > All the best, | > | > Louis | > | > | > PS In case this arrives twice in error I resent from my subscribed address. | ___ | Rcpp-devel mailing list | Rcpp-devel@lists.r-forge.r-project.org | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman