-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
57 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,16 @@ | ||
#include <Rcpp.h> | ||
using namespace Rcpp; | ||
#include <RcppArmadillo.h> | ||
|
||
// This is a simple example of exporting a C++ function to R. You can | ||
// source this function into an R session using the Rcpp::sourceCpp | ||
// function (or via the Source button on the editor toolbar). Learn | ||
// more about Rcpp at: | ||
// | ||
// http://www.rcpp.org/ | ||
// http://adv-r.had.co.nz/Rcpp.html | ||
// http://gallery.rcpp.org/ | ||
// | ||
|
||
// [[Rcpp::export]] | ||
NumericVector test(int n, double shape, double rate) { | ||
NumericVector result(n); | ||
for(int i = 0; i < n; i++) { | ||
result(i) = R::rgamma(shape, 1 / rate); | ||
} | ||
return result; | ||
double log_sum_exp(const arma::vec& x) { | ||
double max_val = x.max(); | ||
double sum_exp = arma::accu(exp(x - max_val)); | ||
return max_val + log(sum_exp); | ||
} | ||
|
||
|
||
// You can include R code blocks in C++ files processed with sourceCpp | ||
// (useful for testing and development). The R code will be automatically | ||
// run after the compilation. | ||
// | ||
|
||
/*** R | ||
mean(rgamma(1e5, shape = 2, rate = 2)) | ||
sd(rgamma(1e5, shape = 2, rate = 2)) | ||
mean(test(1e5, shape = 2, rate = 2)) | ||
sd(test(1e5, shape = 2, rate = 2)) | ||
x <- 1:3 | ||
sum(exp(x - log_sum_exp(x))) | ||
*/ |