Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Rcpp.h>
- // [[Rcpp::depends(RcppParallel)]]
- #include <RcppParallel.h>
- #include <cmath>
- #include <algorithm>
- using namespace Rcpp;
- using namespace RcppParallel;
- // [[Rcpp::export]]
- Rcpp::NumericVector SqrtCpp(Rcpp::NumericVector orig) {
- // allocate the matrix we will return
- NumericVector res(orig.begin(), orig.end());
- // transform it
- std::transform(orig.begin(), orig.end(), res.begin(), ::sqrt);
- // return the new matrix
- return res;
- }
- struct SquareRoot : public Worker
- {
- const RVector<double> input;
- RVector<double> output;
- SquareRoot(const NumericVector input, NumericVector output)
- : input(input), output(output) {}
- void operator()(std::size_t begin, std::size_t end) {
- std::transform(input.begin() + begin,
- input.begin() + end,
- output.begin() + begin,
- ::sqrt);
- }
- };
- // [[Rcpp::export]]
- Rcpp::NumericVector SqrtCppPara(Rcpp::NumericVector x) {
- // allocate the output matrix
- NumericVector output(x.begin(), x.end());
- // SquareRoot functor (pass input and output matrixes)
- SquareRoot squareRoot(x, output);
- // call parallelFor to do the work
- parallelFor(0, x.length(), squareRoot);
- // return the output matrix
- return output;
- }
Add Comment
Please, Sign In to add comment