DataCamp Optimizing R Code with Rcpp
C++ functions belong to C++ files
OPTIMIZING R CODE WITH RCPP
C++ functions belong to C++ files Romain Franois Consulting - - PowerPoint PPT Presentation
DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP C++ functions belong to C++ files Romain Franois Consulting Datactive, ThinkR DataCamp Optimizing R Code with Rcpp Previously, in this course evalCpp() evalCpp( "40 +
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
evalCpp( "40 + 2" ) 42 cppFunction( "int fun(){ return 42; }") fun() 42
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } library(Rcpp) sourceCpp( "code.cpp" ) timesTwo( 21 ) 42
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> _____ _________ ____ _ __ ________________ ___ _________ ___ _ __ ______ ___ _ _
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; __ ________________ ___ _________ ___ _ __ ______ ___ _ _
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] ___ _________ ___ _ __ ______ ___ _ _
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; }
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } library(Rcpp) sourceCpp( "code.cpp" ) timesTwo( 21 ) 42
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; int twice( int x ){ return 2*x ; } // [[Rcpp::export]] int universal(){ return twice(21) ; } # Not possible, twice is internal twice(21) Error in twice(21) : could not find function "twice" # Fine universal() 42
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
#include <Rcpp.h> using namespace Rcpp ; int twice( int x ){ return 2*x ; } // [[Rcpp::export]] int universal(){ return twice(21) ; } /*** R # This is R code 12 + 30 # Calling the `universal` function universal() */
DataCamp Optimizing R Code with Rcpp
if( condition ){ // code if true } else { // code otherwise }
DataCamp Optimizing R Code with Rcpp
// [[Rcpp::export]] void info( double x){ if( x < 0 ){ Rprintf( "x is negative" ) ; } else if( x == 0 ){ Rprintf( "x is zero" ) ; } else if( x > 0 ){ Rprintf( "x is positive" ) ; } else { Rprintf( "x is not a number" ) ; } } info(-2) info(0) x is negative x is zero info(3) info(NaN) x is positive x is not a number
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
// [[Rcpp::export]] int nfirst( int n ){ if( n < 0 ) { stop( "n must be positive, I see n=%d", n ) ; } int result = 0 ; for( int i=0; i<n; i++){ result = result + (i+1) ; } return result ; }
DataCamp Optimizing R Code with Rcpp
// [[Rcpp::export]] int nfirst( int n ){ if( n < 0 ) { stop( "n must be positive, I see n=%d", n ) ; } int result = 0 ; for( int i=0; i<n; i++){ if( i == 13 ){ Rprintf( "I cannot handle that, I am superstitious" ) ; break ; } result = result + (i+1) ; } return result ; }
DataCamp Optimizing R Code with Rcpp
2 n+1 n
′ n
n n
n 2
n
DataCamp Optimizing R Code with Rcpp
n+1
n
int n = ... // number of iterations double res = ... // initialization for( int i=0; i<n; i++){ // update the value of res // i.e. calculate x_{n+1} given x_{n} res = ( res + S / res ) / 2.0 ; } return res ;
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP
DataCamp Optimizing R Code with Rcpp
DataCamp Optimizing R Code with Rcpp
// [[Rcpp::export]] int power( int n ){ if( n < 0 ){ stop( "n must be positive" ) ; } int value = 1 ; while( value < n ){ value = value * 2 ; } return value ; } power( 1000 ) 1024 power( 17 ) 32
DataCamp Optimizing R Code with Rcpp
for( init ; condition; increment ){ body } init while( condition ){ body increment }
DataCamp Optimizing R Code with Rcpp
do { body } while( condition ) ;
DataCamp Optimizing R Code with Rcpp
// [[Rcpp::export]] int power( int n ){ if( n < 0 ){ stop( "n must be positive" ) ; } int value = 1 ; do { value = value * 2 ; } while( value < n ); return value ; }
DataCamp Optimizing R Code with Rcpp
OPTIMIZING R CODE WITH RCPP