source : The Atlantic April 2014 "The State of American Beer"
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
#!/usr/bin/r | |
library(inline) | |
library(rbenchmark) | |
## openMPCode example from Rcpp/examples/OpenMP/ by Dirk E. | |
openMPCode <- ' | |
// assign to C++ vector | |
std::vector<double> x = Rcpp::as<std::vector< double > >(xs); |
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
soft_thresh = function(x, g) { | |
x = as.vector(x) | |
w1 = which(g >= abs(x)) | |
w2 = which(g < abs(x) & x > 0) | |
w3 = which(g < abs(x) & x < 0) | |
ret = x | |
ret[w1] = 0 | |
ret[w2] = x[w2]-g | |
ret[w3] = x[w3]+g | |
ret |