Skip to content

Commit 10006cf

Browse files
Use accumulate over reduce when necessary
`std::reduce` can only be used over `std::accumulate` when the binary operation is commutative.
1 parent bd6344f commit 10006cf

File tree

8 files changed

+19
-19
lines changed

8 files changed

+19
-19
lines changed

code/header_units/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ void compute(int len, T initial, T step) {
2424
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
2525

2626
// compute standard deviation of it
27-
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
28-
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
29-
[](const T& s, const T& a) { return s + a * a; });
27+
const T sum = std::reduce(diffs.begin()+1, diffs.end());
28+
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
29+
[](const T& s, const T& a) { return s + a * a; });
3030
const T mean = sum/len;
3131
const T variance = sumsq/len - mean*mean;
3232

code/header_units/solution/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void compute(int len, T initial, T step) {
2222
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
2323

2424
// compute standard deviation of it
25-
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
26-
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
27-
[](const T& s, const T& a) { return s + a * a; });
25+
const T sum = std::reduce(diffs.begin()+1, diffs.end());
26+
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
27+
[](const T& s, const T& a) { return s + a * a; });
2828
const T mean = sum/len;
2929
const T variance = sumsq/len - mean*mean;
3030

code/modules/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ void compute(int len, T initial, T step) {
2424
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
2525

2626
// compute standard deviation of it
27-
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
28-
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
29-
[](const T& s, const T& a) { return s + a * a; });
27+
const T sum = std::reduce(diffs.begin()+1, diffs.end());
28+
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
29+
[](const T& s, const T& a) { return s + a * a; });
3030
const T mean = sum/len;
3131
const T variance = sumsq/len - mean*mean;
3232

code/modules/solution/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ void compute(int len, T initial, T step) {
2525
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
2626

2727
// compute standard deviation of it
28-
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
29-
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
30-
[](const T& s, const T& a) { return s + a * a; });
28+
const T sum = std::reduce(diffs.begin()+1, diffs.end());
29+
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
30+
[](const T& s, const T& a) { return s + a * a; });
3131
const T mean = sum/len;
3232
const T variance = sumsq/len - mean*mean;
3333

code/smartPointers/smartPointers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ double sumEntries(std::span<double const> range) {
3939
// Simulate an error
4040
throw std::invalid_argument("Error when summing over data.");
4141

42-
return std::reduce(range.begin(), range.end(), 0.);
42+
return std::reduce(range.begin(), range.end());
4343
}
4444

4545
// Often, data are owned by one entity, and merely used by others. In this case, we hand the data to

code/smartPointers/solution/smartPointers.sol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ double sumEntries(std::span<double const> range) {
4040
// Simulate an error
4141
throw std::invalid_argument("Error when summing over data.");
4242

43-
return std::reduce(range.begin(), range.end(), 0.);
43+
return std::reduce(range.begin(), range.end());
4444
}
4545

4646
// Often, data are owned by one entity, and merely used by others. In this case, we hand the data to

code/stl/randomize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void compute(int len, T initial, T step) {
1919

2020
// compute standard deviation of all differences
2121
const T sum = std::reduce(...);
22-
const T sumsq = std::reduce(..., [](...) { ...; });
22+
const T sumsq = std::accumulate(..., [](...) { ...; });
2323
const T mean = sum/len;
2424
const T variance = sumsq/len - mean*mean;
2525

code/stl/solution/randomize.sol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ void compute(int len, T initial, T step) {
4242

4343
// compute standard deviation of all differences.
4444
// Note that the first element is just the original element itself, so we need to skip it.
45-
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
46-
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
45+
const T sum = std::reduce(diffs.begin()+1, diffs.end());
46+
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
4747
// Alternatively:
48-
// const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
49-
// [](const T& s, const T& a) { return s + a * a; });
48+
// const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
49+
// [](const T& s, const T& a) { return s + a * a; });
5050
const T mean = sum/len;
5151
const T variance = sumsq/len - mean*mean;
5252

0 commit comments

Comments
 (0)