Skip to content

Minor clean up for Forward Euler Method in C++ #275

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
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
13 changes: 6 additions & 7 deletions contents/forward_euler_method/code/c++/euler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ using std::size_t;
std::vector<double> solve_euler(double timestep, size_t size) {
std::vector<double> result;
double current = 1.0;
std::generate_n(std::back_inserter(result), size, [&] {
return std::exchange(current, current - 3.0 * current * timestep);
});
for (size_t i = 0; i < size; ++i) {
result.push_back(current);
current -= 3.0 * current * timestep;
}
return result;
}

/*
check_result takes an iterator over doubles,
and returns whether any value is outside the passed threshold.
*/
// check_result takes an iterator over doubles,
// and returns whether any value is outside the passed threshold.
template <typename Iter>
bool check_result(Iter first, Iter last, double threshold, double timestep) {
auto it = first;
Expand Down