Showing posts with label Rcpp. Show all posts
Showing posts with label Rcpp. Show all posts

Tuesday, September 09, 2014

Ceemple vs. Rcpp

Ceemple is a cool way to do C++. Rcpp is another cool way to do C++. Each of them has its own strengths and weaknesses. I am amazed to see how little change is required to get the same source to compile and run under these environments. For example, Ceemple comes with an example that uses the Eigen matrix library:

--------------------------------------------
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;

int main()
{
  ArrayXXf  m(2,2);
  // assign some values coefficient by coefficient
  m(0,0) = 1.0; m(0,1) = 2.0;
  m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
  // print values to standard output
  cout << m << endl << endl;
  // using the comma-initializer is also allowed
  m << 1.0,2.0,
       3.0,4.0;
  // print values to standard output
  cout << m << endl;
}
-------------------------------------------

With Rcpp (using the Rstudio IDE), this becomes:
-------------------------------------------
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
using namespace std;
using namespace Rcpp;
using namespace Eigen;

// [[Rcpp::export]]
int test_eigen()
{
  ArrayXXf  m(2,2);
  // assign some values coefficient by coefficient
  m(0,0) = 1.0; m(0,1) = 2.0;
  m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
  // print values to standard output
  cout << m << endl << endl;
  // using the comma-initializer is also allowed
  m << 1.0,2.0,
       3.0,4.0;
  // print values to standard output
  cout << m << endl;
  return 0;
}

/*** R
test_eigen()
*/
-------------------------------------------

Virtually no changes required! 

Tuesday, May 20, 2014

R vs Python: Why R is still the king of statistical computing

Here are some good arguments. I particularly like the author's argument regarding the vital role of Rcpp in the future development of the R ecosystem.

Friday, May 16, 2014

LaplacesDemonCpp

This is a young project, but it looks really promising!

Saturday, May 10, 2014

Monday, April 07, 2014

Play with C++ code in Rstudio

I am not a C++ programmer but sometimes I need to play with some C++ code. On my Linux workstation, I have the complete GNU tool chain installed. I don't really want to do that on my tiny Windows ultrabook because ... well, it's tiny and I want to keep it that way.

I do have R (and the magical Rcpp package), Rtools, and Rstudio installed on my ultrabook. I just realized how trivial it is to tweak C++ code fragments using these tools together.

For example, in order to run this very simple C++ code fragment:

===========================================
#include <iostream>
using std::cout;

int main() {
for (int hashNum = 1; hashNum <= 5; hashNum++) {
cout << "#";
}
cout << "\n";
return 0;
}
===========================================

One just need to add a few lines so that the code looks like this:

===========================================
#include <Rcpp.h>

using std::cout;
using namespace Rcpp;

// [[Rcpp::export]]
int main() {
  for (int hashNum = 1; hashNum <= 5; hashNum++) {
cout << "#";
}
cout << "\n";
return 0;
}

/*** R
main()
*/
===========================================

Hit Ctrl + Enter, problem solved!

Monday, June 10, 2013

Using Rcpp in agent-based models

Here is an example. The Rcpp code has to be slightly modified to run on my Mint linux machine.

Friday, November 30, 2012

Using Rcpp with Rstudio

It is quite amazing to see how easy and straightforward it has become to integrate C++ and R, with the help from Rcpp (especially with the new "attribute" function) and Rstudio. More information can be found here.

Thursday, November 29, 2012

Rcpp tutorial

Here is a good introduction to Rcpp.

Monday, May 28, 2012

Simple rcpp examples

Here are some nice examples for Rcpp package.

Sunday, January 22, 2012

Some Rcpp benchmarks

I ran the Fibonacci number example from the Rcpp package on a number of computers and operating systems. Here are the results:

A. On my main computer (Core 2 Extreme 3.06GHz, 8 GB memory) running Ubuntu 10.04 (g++ 4.4.3):
        test replications elapsed relative user.self sys.self
3 fibRcpp(N)            1   0.148   1.0000      0.14     0.01
1    fibR(N)            1  87.078 588.3649     87.03     0.04
2   fibRC(N)            1  91.209 616.2770     91.14     0.07

B. Same computer running Windows Vista (g++ 4.5.0):

        test replications elapsed relative user.self sys.self
3 fibRcpp(N)            1    0.21   1.0000      0.21     0.00
1    fibR(N)            1   92.08 438.4762     90.47     0.05
2   fibRC(N)            1   94.39 449.4762     93.13     0.03


C. On my second laptop (Core 2 Duo 2.53GHz, 4 GB memory) running Windows 7 (g++ 4.5.0):

        test replications elapsed relative user.self sys.self
3 fibRcpp(N)            1    0.17   1.0000      0.17     0.00
1    fibR(N)            1   73.62 433.0588     73.47     0.00
2   fibRC(N)            1   74.27 436.8824     74.20     0.03

D. On the same computer running Revolution R Enterprise 5:
      test replications elapsed relative user.self sys.self
2 fibRC(N)            1   72.31 1.000000     72.09        0
1  fibR(N)            1   72.99 1.009404     72.79        0 

E. On my third laptop (Core 2 Duo 2.50GHz, 2 GB memory) running Mint Debian (g++ 4.6.2):
        test replications elapsed relative user.self sys.self
3 fibRcpp(N)            1   0.148   1.0000     0.148     0.00
1    fibR(N)            1  65.535 442.8041     65.328    0.200
2   fibRC(N)            1  65.664 443.6757     65.492    0.172


Why the faster computer performed worse, on both R and Rcpp versions?

Rcpp on windows

I got Rcpp working on my windows machine by installing the Rtools bundle. It is not clearly to me how to get GSL installed so the RcppGSL will also work.

Counter