SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Rcpp
Romain François
romain@r-enthusiasts.com @romain_francois
jeudi 27 juin 13
R / C++
Romain François
romain@r-enthusiasts.com @romain_francois
jeudi 27 juin 13
n <- length(x)
m <- 0.0
for( i in 1:n ){
m <- m + x[i]^2 / n
}
jeudi 27 juin 13
m <- mean( x^2 )
n <- length(x)
m <- 0.0
for( i in 1:n ){
m <- m + x[i]^2 / n
}
Vectorization
jeudi 27 juin 13
Writing efficient R code
requires you to
know a lot of tools
jeudi 27 juin 13
#include <Rcpp.h>
using namespace Rcpp ;
double square(x){ return x*x ; }
// [[Rcpp::export]]
double fun( NumericVector x){
int n = x.size() ;
double res = 0.0 ;
for( int i=0; i<n; i++){
res += square(x[i]) / n ;
}
return res ;
}
jeudi 27 juin 13
10 000 100 000 1 000 000
Dumb R 1008 10 214 104 000
Vectorized R 24 125 1 021
C++ 13 80 709
Execution times (micro seconds)
jeudi 27 juin 13
m <- mean( x^2 )
Why vectorization
is not enough ?
jeudi 27 juin 13
Get StartedGet Started
jeudi 27 juin 13
My First Rcpp function
cppFunction("double add( double x, double y){
return x + y ;
}")
add( 1.0, 2.0 )
cppFunction for embedding directly small functions
jeudi 27 juin 13
Going further
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
double add( double x, double y){
return x + y ;
}
add.cpp (C++ code)
sourceCpp( "add.cpp" )
add( 1.0, 2.0 )
R code
jeudi 27 juin 13
Manipulate R data
structures
jeudi 27 juin 13
Most commonly used
R Rcpp
numeric vector NumericVector
integer vector IntegerVector
list List
environment Environment
function Function
jeudi 27 juin 13
Vectors
// create a NumericVector of length 10
NumericVector x(10);
// access elements
x[0] = 2.0 ;
x[1] = x[0] ;
// example
double sum = 0.0 ; int n = x.size() ;
for( int i=0; i<n; i++){
sum += x[i] ;
}
jeudi 27 juin 13
Lists
// create a List with names
List obj = List::create(
_["a"] = 1.0,
_["b"] = "foo"
) ;
double a = obj["a"] ;
std::string b = obj["b"] ;
jeudi 27 juin 13
C++ data structures
Modules
jeudi 27 juin 13
The usual bank account example
class Account {
private:
double balance ;
public:
Account( ) : balance(0){}
double get_balance(){
return balance ;
}
void withdraw(double x){
balance -= x ;
}
void deposit(double x ){
balance += x ;
}
} ;
RCPP_MODULE(BankAccount){
class_<Account>( "Account" )
.constructor()
.property( "balance", Account::get_balance )
.method( "deposit", Account::deposit)
.method( "withdraw", Account::withdraw)
;
}
account <- new( Account )
account$deposit( 1000 )
account$balance
account$withdraw( 200 )
account$balance
account$balance <- 200
jeudi 27 juin 13
Learn More
- Rcpp book
- Vignettes
- Tutorial at useR
- Mailing List
- Custom Training
jeudi 27 juin 13
Romain François
romain@r-enthusiasts.com @romain_francois
Questions
jeudi 27 juin 13

Contenu connexe

Similaire à Rcpp

Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1Laurent BUNIET
 
LES ALGORITHMES D’APPROXIMATION
LES ALGORITHMES D’APPROXIMATIONLES ALGORITHMES D’APPROXIMATION
LES ALGORITHMES D’APPROXIMATIONborhen boukthir
 
TP3: Comportement Temps Réel de l'Agent Perception
TP3: Comportement Temps Réel de l'Agent PerceptionTP3: Comportement Temps Réel de l'Agent Perception
TP3: Comportement Temps Réel de l'Agent PerceptionSaid Benaissa
 

Similaire à Rcpp (6)

Initiation r
Initiation rInitiation r
Initiation r
 
Slide matlab
Slide matlab Slide matlab
Slide matlab
 
Mathématiques et Python
Mathématiques et PythonMathématiques et Python
Mathématiques et Python
 
Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1
 
LES ALGORITHMES D’APPROXIMATION
LES ALGORITHMES D’APPROXIMATIONLES ALGORITHMES D’APPROXIMATION
LES ALGORITHMES D’APPROXIMATION
 
TP3: Comportement Temps Réel de l'Agent Perception
TP3: Comportement Temps Réel de l'Agent PerceptionTP3: Comportement Temps Réel de l'Agent Perception
TP3: Comportement Temps Réel de l'Agent Perception
 

Plus de Romain Francois

Plus de Romain Francois (19)

dplyr and torrents from cpasbien
dplyr and torrents from cpasbiendplyr and torrents from cpasbien
dplyr and torrents from cpasbien
 
dplyr use case
dplyr use casedplyr use case
dplyr use case
 
dplyr
dplyrdplyr
dplyr
 
user2015 keynote talk
user2015 keynote talkuser2015 keynote talk
user2015 keynote talk
 
SevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittrSevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittr
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Rcpp11 genentech
Rcpp11 genentechRcpp11 genentech
Rcpp11 genentech
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
R and C++
R and C++R and C++
R and C++
 
R and cpp
R and cppR and cpp
R and cpp
 
Rcpp attributes
Rcpp attributesRcpp attributes
Rcpp attributes
 
Rcpp is-ready
Rcpp is-readyRcpp is-ready
Rcpp is-ready
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 

Rcpp

  • 2. R / C++ Romain François romain@r-enthusiasts.com @romain_francois jeudi 27 juin 13
  • 3. n <- length(x) m <- 0.0 for( i in 1:n ){ m <- m + x[i]^2 / n } jeudi 27 juin 13
  • 4. m <- mean( x^2 ) n <- length(x) m <- 0.0 for( i in 1:n ){ m <- m + x[i]^2 / n } Vectorization jeudi 27 juin 13
  • 5. Writing efficient R code requires you to know a lot of tools jeudi 27 juin 13
  • 6. #include <Rcpp.h> using namespace Rcpp ; double square(x){ return x*x ; } // [[Rcpp::export]] double fun( NumericVector x){ int n = x.size() ; double res = 0.0 ; for( int i=0; i<n; i++){ res += square(x[i]) / n ; } return res ; } jeudi 27 juin 13
  • 7. 10 000 100 000 1 000 000 Dumb R 1008 10 214 104 000 Vectorized R 24 125 1 021 C++ 13 80 709 Execution times (micro seconds) jeudi 27 juin 13
  • 8. m <- mean( x^2 ) Why vectorization is not enough ? jeudi 27 juin 13
  • 10. My First Rcpp function cppFunction("double add( double x, double y){ return x + y ; }") add( 1.0, 2.0 ) cppFunction for embedding directly small functions jeudi 27 juin 13
  • 11. Going further #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] double add( double x, double y){ return x + y ; } add.cpp (C++ code) sourceCpp( "add.cpp" ) add( 1.0, 2.0 ) R code jeudi 27 juin 13
  • 13. Most commonly used R Rcpp numeric vector NumericVector integer vector IntegerVector list List environment Environment function Function jeudi 27 juin 13
  • 14. Vectors // create a NumericVector of length 10 NumericVector x(10); // access elements x[0] = 2.0 ; x[1] = x[0] ; // example double sum = 0.0 ; int n = x.size() ; for( int i=0; i<n; i++){ sum += x[i] ; } jeudi 27 juin 13
  • 15. Lists // create a List with names List obj = List::create( _["a"] = 1.0, _["b"] = "foo" ) ; double a = obj["a"] ; std::string b = obj["b"] ; jeudi 27 juin 13
  • 17. The usual bank account example class Account { private: double balance ; public: Account( ) : balance(0){} double get_balance(){ return balance ; } void withdraw(double x){ balance -= x ; } void deposit(double x ){ balance += x ; } } ; RCPP_MODULE(BankAccount){ class_<Account>( "Account" ) .constructor() .property( "balance", Account::get_balance ) .method( "deposit", Account::deposit) .method( "withdraw", Account::withdraw) ; } account <- new( Account ) account$deposit( 1000 ) account$balance account$withdraw( 200 ) account$balance account$balance <- 200 jeudi 27 juin 13
  • 18. Learn More - Rcpp book - Vignettes - Tutorial at useR - Mailing List - Custom Training jeudi 27 juin 13