Skip to content

Commit bdf101f

Browse files
committed
tests: timesTwo rather than square root for transform
1 parent 5378ffb commit bdf101f

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

tests/testthat/cpp/transform.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ using namespace Rcpp;
1010
#include <cmath>
1111
#include <algorithm>
1212

13+
double timesTwo(double x) {
14+
return x * 2;
15+
}
16+
1317
// [[Rcpp::export]]
14-
NumericMatrix matrixSqrt(NumericMatrix orig) {
18+
NumericMatrix matrixTimesTwo(NumericMatrix orig) {
1519

1620
// allocate the matrix we will return
1721
NumericMatrix mat(orig.nrow(), orig.ncol());
1822

1923
// transform it
20-
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
24+
std::transform(orig.begin(), orig.end(), mat.begin(), ::timesTwo);
2125

2226
// return the new matrix
2327
return mat;
@@ -26,7 +30,7 @@ NumericMatrix matrixSqrt(NumericMatrix orig) {
2630
#include <RcppParallel.h>
2731
using namespace RcppParallel;
2832

29-
struct SquareRoot : public Worker
33+
struct TimesTwo : public Worker
3034
{
3135
// source matrix
3236
const RMatrix<double> input;
@@ -35,29 +39,28 @@ struct SquareRoot : public Worker
3539
RMatrix<double> output;
3640

3741
// initialize with source and destination
38-
SquareRoot(const NumericMatrix input, NumericMatrix output)
42+
TimesTwo(const NumericMatrix input, NumericMatrix output)
3943
: input(input), output(output) {}
4044

41-
// take the square root of the range of elements requested
4245
void operator()(std::size_t begin, std::size_t end) {
4346
std::transform(input.begin() + begin,
4447
input.begin() + end,
4548
output.begin() + begin,
46-
::sqrt);
49+
::timesTwo);
4750
}
4851
};
4952

5053
// [[Rcpp::export]]
51-
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
54+
NumericMatrix parallelMatrixTimesTwo(NumericMatrix x) {
5255

5356
// allocate the output matrix
5457
NumericMatrix output(x.nrow(), x.ncol());
5558

56-
// SquareRoot functor (pass input and output matrixes)
57-
SquareRoot squareRoot(x, output);
59+
// TimesTwo functor (pass input and output matrixes)
60+
TimesTwo timesTwo(x, output);
5861

5962
// call parallelFor to do the work
60-
parallelFor(0, x.nrow() * x.ncol(), squareRoot);
63+
parallelFor(0, x.nrow() * x.ncol(), timesTwo);
6164

6265
// return the output matrix
6366
return output;

tests/testthat/test-transform.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ test_that( "transform works with Rcpp", {
55

66
m <- matrix(as.numeric(c(1:1000000)), nrow = 1000, ncol = 1000)
77

8-
expect_equal(matrixSqrt(m), parallelMatrixSqrt(m))
8+
expect_equal(matrixTimesTwo(m), parallelMatrixTimesTwo(m))
99
})
1010

0 commit comments

Comments
 (0)