Guest User

Untitled

a guest
Jul 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <Rcpp.h>
  2. // [[Rcpp::depends(RcppParallel)]]
  3. #include <RcppParallel.h>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. using namespace Rcpp;
  8. using namespace RcppParallel;
  9.  
  10. // [[Rcpp::export]]
  11. Rcpp::NumericVector SqrtCpp(Rcpp::NumericVector orig) {
  12.  
  13. // allocate the matrix we will return
  14. NumericVector res(orig.begin(), orig.end());
  15.  
  16. // transform it
  17. std::transform(orig.begin(), orig.end(), res.begin(), ::sqrt);
  18.  
  19. // return the new matrix
  20. return res;
  21. }
  22.  
  23.  
  24. struct SquareRoot : public Worker
  25. {
  26. const RVector<double> input;
  27.  
  28. RVector<double> output;
  29.  
  30. SquareRoot(const NumericVector input, NumericVector output)
  31. : input(input), output(output) {}
  32.  
  33. void operator()(std::size_t begin, std::size_t end) {
  34. std::transform(input.begin() + begin,
  35. input.begin() + end,
  36. output.begin() + begin,
  37. ::sqrt);
  38. }
  39. };
  40.  
  41.  
  42. // [[Rcpp::export]]
  43. Rcpp::NumericVector SqrtCppPara(Rcpp::NumericVector x) {
  44.  
  45. // allocate the output matrix
  46. NumericVector output(x.begin(), x.end());
  47.  
  48. // SquareRoot functor (pass input and output matrixes)
  49. SquareRoot squareRoot(x, output);
  50.  
  51. // call parallelFor to do the work
  52. parallelFor(0, x.length(), squareRoot);
  53.  
  54. // return the output matrix
  55. return output;
  56. }
Add Comment
Please, Sign In to add comment