C++ functions belong to C++ files Romain Franois Consulting - - PowerPoint PPT Presentation

c functions belong to c files
SMART_READER_LITE
LIVE PREVIEW

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 +


slide-1
SLIDE 1

DataCamp Optimizing R Code with Rcpp

C++ functions belong to C++ files

OPTIMIZING R CODE WITH RCPP

Romain François

Consulting Datactive, ThinkR

slide-2
SLIDE 2

DataCamp Optimizing R Code with Rcpp

Previously, in this course

evalCpp() cppFunction()

evalCpp( "40 + 2" ) 42 cppFunction( "int fun(){ return 42; }") fun() 42

slide-3
SLIDE 3

DataCamp Optimizing R Code with Rcpp

Using .cpp files

C++ code in code.cpp The sourceCpp() function compiles and loads it

#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } library(Rcpp) sourceCpp( "code.cpp" ) timesTwo( 21 ) 42

slide-4
SLIDE 4

DataCamp Optimizing R Code with Rcpp

Include the Rcpp header file

Include only Rcpp.h It includes all other header files automatically

#include <Rcpp.h> _____ _________ ____ _ __ ________________ ___ _________ ___ _ __ ______ ___ _ _

slide-5
SLIDE 5

DataCamp Optimizing R Code with Rcpp

Using the Rcpp namespace

Use Something instead of Rcpp::Something, when Something is in Rcpp

#include <Rcpp.h> using namespace Rcpp ; __ ________________ ___ _________ ___ _ __ ______ ___ _ _

slide-6
SLIDE 6

DataCamp Optimizing R Code with Rcpp

Exporting the function to R

#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] ___ _________ ___ _ __ ______ ___ _ _

slide-7
SLIDE 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 ; }

slide-8
SLIDE 8

DataCamp Optimizing R Code with Rcpp

source the C++ file

load the function into R Call it, just as any other R function.

#include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] int timesTwo( int x ){ return 2*x ; } library(Rcpp) sourceCpp( "code.cpp" ) timesTwo( 21 ) 42

slide-9
SLIDE 9

DataCamp Optimizing R Code with Rcpp

Let's practice!

OPTIMIZING R CODE WITH RCPP

slide-10
SLIDE 10

DataCamp Optimizing R Code with Rcpp

Writing functions in C++

OPTIMIZING R CODE WITH RCPP

Romain François

Consulting Datactive, ThinkR

slide-11
SLIDE 11

DataCamp Optimizing R Code with Rcpp

Just don't export internal functions

Calling from R:

#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

slide-12
SLIDE 12

DataCamp Optimizing R Code with Rcpp

C++ comments

Comment until the end of the line: Comments spanning multiple lines

// A comment

slide-13
SLIDE 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() */

slide-14
SLIDE 14

DataCamp Optimizing R Code with Rcpp

if and else

if( condition ){ // code if true } else { // code otherwise }

slide-15
SLIDE 15

DataCamp Optimizing R Code with Rcpp

if/else example

Calling the function with various arguments:

// [[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

slide-16
SLIDE 16

DataCamp Optimizing R Code with Rcpp

Let's practice!

OPTIMIZING R CODE WITH RCPP

slide-17
SLIDE 17

DataCamp Optimizing R Code with Rcpp

For loops

OPTIMIZING R CODE WITH RCPP

Romain François

Consulting Datactive, ThinkR

slide-18
SLIDE 18

DataCamp Optimizing R Code with Rcpp

The 4 parts of C++ for loops

Initialization Continue condition Increment Body

slide-19
SLIDE 19

DataCamp Optimizing R Code with Rcpp

For loops - the initialization

What happens at the very beginning of the loop:

for( init ; ; ){ }

slide-20
SLIDE 20

DataCamp Optimizing R Code with Rcpp

For loops - the continue condition

Logical condition to control if the loop continues

for( ; condition ; ){ }

slide-21
SLIDE 21

DataCamp Optimizing R Code with Rcpp

For loops - the increment

Executed at the end of each iteration

for( ; ; increment ){ }

slide-22
SLIDE 22

DataCamp Optimizing R Code with Rcpp

For loops - the body

Executed at each iteration. What the loop does.

for( ; ; ){ body }

slide-23
SLIDE 23

DataCamp Optimizing R Code with Rcpp

Typical for loop

for (int i=0; i<n; i++ ){ // some code using i }

slide-24
SLIDE 24

DataCamp Optimizing R Code with Rcpp

Typical for loop

for (int i=0; ; ){ }

slide-25
SLIDE 25

DataCamp Optimizing R Code with Rcpp

Typical for loop

for (int i=0; i<n; ){ }

slide-26
SLIDE 26

DataCamp Optimizing R Code with Rcpp

Typical for loop

for (int i=0; i<n; i++){ }

slide-27
SLIDE 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 ; }

slide-28
SLIDE 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 ; }

slide-29
SLIDE 29

DataCamp Optimizing R Code with Rcpp

Newton iterative method to calculate square roots

Finding is the same as finding the root of f(x) = x − S Leading to the iterative expression: x = x − = x − = (x + ) Algorithm: Take an initial value x Update x using the formula above a given number of times √ S

2 n+1 n

f (x )

′ n

f(x )

n n

2xn x − S

n 2

2 1

n

xn S

slide-30
SLIDE 30

DataCamp Optimizing R Code with Rcpp

Newton's method in C++

x = (x + ) translates to the pseudo code

n+1

2 1

n

xn S

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 ;

slide-31
SLIDE 31

DataCamp Optimizing R Code with Rcpp

Let's practice!

OPTIMIZING R CODE WITH RCPP

slide-32
SLIDE 32

DataCamp Optimizing R Code with Rcpp

While loops

OPTIMIZING R CODE WITH RCPP

Romain François

Consulting Datactive, ThinkR

slide-33
SLIDE 33

DataCamp Optimizing R Code with Rcpp

While loops are simpler

Continue condition Loop body

while( condition ){ body }

slide-34
SLIDE 34

DataCamp Optimizing R Code with Rcpp

Example

Once the function is compiled with sourceCpp, you can call it:

// [[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

slide-35
SLIDE 35

DataCamp Optimizing R Code with Rcpp

For loops are just while loops

is equivalent to

for( init ; condition; increment ){ body } init while( condition ){ body increment }

slide-36
SLIDE 36

DataCamp Optimizing R Code with Rcpp

do / while loops

do { body } while( condition ) ;

slide-37
SLIDE 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 ; }

slide-38
SLIDE 38

DataCamp Optimizing R Code with Rcpp

Let's practice!

OPTIMIZING R CODE WITH RCPP