Created
October 19, 2012 19:27
-
-
Save jjallaire/3920180 to your computer and use it in GitHub Desktop.
Rcpp example functions using attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
using namespace Rcpp; | |
// [[Rcpp::export]] | |
int fibonacciCpp(const int x) { | |
if (x == 0) return(0); | |
if (x == 1) return(1); | |
return (fibonacciCpp(x - 1)) + fibonacciCpp(x - 2); | |
} | |
// [[Rcpp::export]] | |
NumericVector convolveCpp(NumericVector a, NumericVector b) { | |
int na = a.size(), nb = b.size(); | |
int nab = na + nb - 1; | |
NumericVector xab(nab); | |
for (int i = 0; i < na; i++) | |
for (int j = 0; j < nb; j++) | |
xab[i + j] += a[i] * b[j]; | |
return xab; | |
} | |
// [[Rcpp::export]] | |
List lapplyCpp(List input, Function f) { | |
List output(input.size()); | |
std::transform(input.begin(), input.end(), output.begin(), f); | |
output.names() = input.names(); | |
return output; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment