-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.cpp
130 lines (114 loc) · 3.39 KB
/
simple.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*********************************************************************
*
* simple.cpp
* Includes simple examples for learning how to use Rcpp
* Some material taken from Hadley Wickham's tutorial:
* http://adv-r.had.co.nz/Rcpp.html
* Philip Barrett, Chicago, 01may2014
*
*********************************************************************/
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void helloWorld_cpp() {
// Simple 'Hello World' example. Illustrates how to source and call a C++
// function from R
Rcout << "Hello World!" << std::endl ;
}
// [[Rcpp::export]]
int timesTwo(int x) {
// Simple example to multiply an integer by 2
int ans = x * 2 ;
return ans ;
}
// [[Rcpp::export]]
double timesTwo_d(double x) {
// Now multiply a double by 2
return x * 2 ;
}
// [[Rcpp::export(equals.two)]]
bool equals_two(double x) {
// Checks if a number equals 2
return x == 2 ;
}
// [[Rcpp::export]]
void equals_two_vocal(double x) {
// Checks if a number equals 2
if( x == 2 )
Rcout << "This number equals 2" << std::endl ;
else
Rcout << "This number does not equal 2" << std::endl ;
}
// [[Rcpp::export]]
void I_like_to_move_it( int iNN ){
// Sings a ridiculous song
for( int iII = 0; iII < iNN; iII++ )
Rcout << "I like to move it, move it." << std::endl ;
Rcout << "I to ... move it!" << std::endl << std::endl;
}
// [[Rcpp::export]]
void n_green_bottles( int iNN ){
// Also sings a song.
for ( int iII = iNN; iII > 0; iII-- ){
Rcout << iII << " bottles of beer on the wall,"<< std::endl ;
Rcout << iII << " bottles of beer,"<< std::endl ;
Rcout << "Take one down, pass it around," << std::endl ;
Rcout << iII - 1 << " bottles of beer on the wall." << std::endl << std::endl ;
}
}
// [[Rcpp::export]]
double sumC(NumericVector x) {
// Sum an R vector using a loop. Note the base zero counting!!!
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}
// [[Rcpp::export]]
double euclidian( NumericVector vX, NumericVector vY ) {
// Computes the Euclidian distance between two vectors
// Error checking
int iNN = vX.size() ;
int iMM = vY.size() ;
// The size of each vector
// This is an example of a METHOD
if( iNN != iMM ){
Rcout << "Error: Vectors must be same size" << std::endl ;
return( NA_REAL );
// Return NA if don't agree
// return( INFINITY );
// stop( "Error: Vectors must be same size" )
}
double dist_sq = 0;
// Initialize the distance calculator
for ( int iII = 0; iII < iNN; ++iII ) {
dist_sq += pow( vX[iII] - vY[iII], 2 ) ;
// Compute the distance
}
return sqrt( dist_sq );
// Return the sqaure of the sum of the entries
}
// [[Rcpp::export]]
double mat_row_sum( NumericMatrix vMM, int iRow ){
// Sums a given row from a matrix
NumericVector vRow = vMM( iRow - 1, _ ) ;
return sumC( vRow ) ;
}
// [[Rcpp::export]]
double mpe(List mod) {
// Computes the mean percentage error in a regression. Illustrates how to use
// an R list
if ( !mod.inherits("lm") ) stop("Input must be a linear model");
// Error checking
NumericVector resid = as<NumericVector>(mod["residuals"]);
NumericVector fitted = as<NumericVector>(mod["fitted.values"]);
// Map the vectors in mod
int n = resid.size();
double err = 0;
for(int i = 0; i < n; ++i) {
err += resid[i] / (fitted[i] + resid[i]);
}
return err / n;
}