Skip to content

Improve STL exercise #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions code/stl/randomize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@
#include <random>
#include "Complex.hpp"

using namespace std;

template<typename T>
void compute(int len, T initial, T step) {
// allocate vectors
std::vector<T> v(len+1), diffs(len+1);

// fill and randomize v
generate(, , [](...) { ...; });
shuffle(..., std::default_random_engine{});
std::generate(, , [](...) { ...; });
std::shuffle(..., std::default_random_engine{});

// compute differences
adjacent_difference(...);
std::adjacent_difference(...);

// compute standard deviation of it
T sum = reduce(...);
T sumsq = reduce(..., [](...) { ...; });
T mean = sum/len;
T variance = sumsq/len - mean*mean;
// compute standard deviation of all differences
const T sum = std::reduce(...);
const T sumsq = std::reduce(..., [](...) { ...; });
const T mean = sum/len;
const T variance = sumsq/len - mean*mean;

std::cout << "Range = [" << initial << ", " << step*len << "]\n"
<< "Mean = " << mean << '\n'
Expand Down
31 changes: 20 additions & 11 deletions code/stl/solution/randomize.sol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <random>
#include "Complex.hpp"

using namespace std;

template<typename T>
struct Generator {
T m_value, m_step;
Expand All @@ -29,17 +27,28 @@ void compute(int len, T initial, T step) {
std::vector<T> v(len+1), diffs(len+1);

// fill and randomize v
generate(v.begin(), v.end(), Generator<T>(initial, step));
shuffle(v.begin(), v.end(), std::default_random_engine{});
std::generate(v.begin(), v.end(), Generator<T>(initial, step));
// Alternatively:
// std::generate(v.begin(), v.end(), [value = initial, step]() mutable {
// const T cur = value;
// value += step;
// return cur;
// });

// compute differences
adjacent_difference(v.begin(), v.end(), diffs.begin());
std::shuffle(v.begin(), v.end(), std::default_random_engine{});

// compute standard deviation of it
T sum = reduce(diffs.begin()+1, diffs.end(), T());
T sumsq = reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
T mean = sum/len;
T variance = sumsq/len - mean*mean;
// compute differences
std::adjacent_difference(v.begin(), v.end(), diffs.begin());

// compute standard deviation of all differences.
// Note that the first element is just the original element itself, so we need to skip it.
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
Comment on lines +45 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T());
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
const T sum = std::reduce(diffs.begin(), diffs.end(), T());
const T sumsq = std::reduce(diffs.begin(), diffs.end(), T(), sumsquare<T>());

Why do you skip the first element? Was this meant to be

    const T sum = std::reduce(diffs.begin(), diffs.end(), diffs[0]);

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is correct, since std::adjacent_difference only writes differences starting at element 1. So diffs[0] is just v[0] and diffs[1] is the first difference (v[1] - v[0]).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I ignored where those values came from. This is correct, but not very intuitive without any explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your suggestions (that I committed), this is resolved now I guess?

// Alternatively:
// const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(),
// [](const T& s, const T& a) { return s + a * a; });
const T mean = sum/len;
const T variance = sumsq/len - mean*mean;

std::cout << "Range = [" << initial << ", " << step*len << "]\n"
<< "Mean = " << mean << '\n'
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy