c functions belong to c files
play

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 +


  1. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP C++ functions belong to C++ files Romain François Consulting Datactive, ThinkR

  2. DataCamp Optimizing R Code with Rcpp Previously, in this course evalCpp() evalCpp( "40 + 2" ) 42 cppFunction() cppFunction( "int fun(){ return 42; }") fun() 42

  3. DataCamp Optimizing R Code with Rcpp Using .cpp files C++ code in code.cpp #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } The sourceCpp() function compiles and loads it library(Rcpp) sourceCpp( "code.cpp" ) timesTwo( 21 ) 42

  4. DataCamp Optimizing R Code with Rcpp Include the Rcpp header file #include <Rcpp.h> _____ _________ ____ _ __ ________________ ___ _________ ___ _ __ ______ ___ _ _ Include only Rcpp.h It includes all other header files automatically

  5. DataCamp Optimizing R Code with Rcpp Using the Rcpp namespace #include <Rcpp.h> using namespace Rcpp ; __ ________________ ___ _________ ___ _ __ ______ ___ _ _ Use Something instead of Rcpp::Something , when Something is in Rcpp

  6. DataCamp Optimizing R Code with Rcpp Exporting the function to R #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] ___ _________ ___ _ __ ______ ___ _ _

  7. DataCamp Optimizing R Code with Rcpp The function itself #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; }

  8. DataCamp Optimizing R Code with Rcpp source the C++ file #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } load the function into R library(Rcpp) sourceCpp( "code.cpp" ) Call it, just as any other R function. timesTwo( 21 ) 42

  9. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP Let's practice!

  10. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP Writing functions in C++ Romain François Consulting Datactive, ThinkR

  11. DataCamp Optimizing R Code with Rcpp Just don't export internal functions #include <Rcpp.h> using namespace Rcpp ; int twice( int x ){ return 2*x ; } // [[Rcpp::export]] int universal(){ return twice(21) ; } Calling from R: # Not possible, twice is internal twice(21) Error in twice(21) : could not find function "twice" # Fine universal() 42

  12. DataCamp Optimizing R Code with Rcpp C++ comments Comment until the end of the line: // A comment Comments spanning multiple lines

  13. DataCamp Optimizing R Code with Rcpp R code special comment #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() */

  14. DataCamp Optimizing R Code with Rcpp if and else if( condition ){ // code if true } else { // code otherwise }

  15. DataCamp Optimizing R Code with Rcpp if/else example // [[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" ) ; } } Calling the function with various arguments: info(-2) info(0) x is negative x is zero info(3) info(NaN) x is positive x is not a number

  16. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP Let's practice!

  17. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP For loops Romain François Consulting Datactive, ThinkR

  18. DataCamp Optimizing R Code with Rcpp The 4 parts of C++ for loops Initialization Continue condition Increment Body

  19. DataCamp Optimizing R Code with Rcpp For loops - the initialization What happens at the very beginning of the loop: for( init ; ; ){ }

  20. DataCamp Optimizing R Code with Rcpp For loops - the continue condition Logical condition to control if the loop continues for( ; condition ; ){ }

  21. DataCamp Optimizing R Code with Rcpp For loops - the increment Executed at the end of each iteration for( ; ; increment ){ }

  22. DataCamp Optimizing R Code with Rcpp For loops - the body Executed at each iteration. What the loop does. for( ; ; ){ body }

  23. DataCamp Optimizing R Code with Rcpp Typical for loop for (int i=0; i<n; i++ ){ // some code using i }

  24. DataCamp Optimizing R Code with Rcpp Typical for loop for (int i=0; ; ){ }

  25. DataCamp Optimizing R Code with Rcpp Typical for loop for (int i=0; i<n; ){ }

  26. DataCamp Optimizing R Code with Rcpp Typical for loop for (int i=0; i<n; i++){ }

  27. DataCamp Optimizing R Code with Rcpp Example: sum of n first integers // [[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 ; }

  28. DataCamp Optimizing R Code with Rcpp Breaking out of a for loop // [[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 ; }

  29. DataCamp Optimizing R Code with Rcpp Newton iterative method to calculate square roots √ 2 Finding is the same as finding the root of f ( x ) = x − S S Leading to the iterative expression: 2 f ( x ) x − S 1 S n = x − = x − = ( x + ) n x n +1 n n n f ( x ) ′ 2 x n 2 x n n Algorithm: Take an initial value x 0 Update x using the formula above a given number of times

  30. DataCamp Optimizing R Code with Rcpp Newton's method in C++ 1 S = ( x + ) x n +1 n 2 x n translates to the pseudo code 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 ;

  31. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP Let's practice!

  32. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP While loops Romain François Consulting Datactive, ThinkR

  33. DataCamp Optimizing R Code with Rcpp While loops are simpler while( condition ){ body } Continue condition Loop body

  34. DataCamp Optimizing R Code with Rcpp Example // [[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 ; } Once the function is compiled with sourceCpp , you can call it: power( 1000 ) 1024 power( 17 ) 32

  35. DataCamp Optimizing R Code with Rcpp For loops are just while loops for( init ; condition; increment ){ body } is equivalent to init while( condition ){ body increment }

  36. DataCamp Optimizing R Code with Rcpp do / while loops do { body } while( condition ) ;

  37. DataCamp Optimizing R Code with Rcpp Example of a do / while loop // [[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 ; }

  38. DataCamp Optimizing R Code with Rcpp OPTIMIZING R CODE WITH RCPP Let's practice!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend