Skip to content

switch over to clang-format style #61

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
Feb 23, 2018
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
7 changes: 7 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BasedOnStyle: LLVM

AlignAfterOpenBracket: AlwaysBreak
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
PointerAlignment: Left
37 changes: 13 additions & 24 deletions chapters/computational_mathematics/FFT/code/c++/fft.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// written by Gathros, modernized by Nicole Mazzuca.

#include <complex>
#include <vector>
#include <algorithm>
#include <array>
#include <complex>
#include <cstdint>
#include <algorithm>
#include <vector>

// These headers are for presentation not for the algorithm.
#include <random>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <random>

using std::begin;
using std::end;
Expand Down Expand Up @@ -51,12 +51,11 @@ void cooley_tukey(Iter first, Iter last) {
auto& bottom = first[k];
auto& top = first[k + size / 2];
top = bottom - w * top;
bottom -= (top - bottom);
bottom -= top - bottom;
}
}
}


// note: (last - first) must be less than 2**32 - 1
template <typename Iter>
void sort_by_bit_reverse(Iter first, Iter last) {
Expand Down Expand Up @@ -90,7 +89,7 @@ void iterative_cooley_tukey(Iter first, Iter last) {
auto v = c64(1.0);
for (size_t k = 0; k < stride / 2; k++) {
first[k + j + stride / 2] =
first[k + j] - v * first[k + j + stride / 2];
first[k + j] - v * first[k + j + stride / 2];
first[k + j] -= (first[k + j + stride / 2] - first[k + j]);
v *= w;
}
Expand All @@ -106,9 +105,7 @@ int main() {

std::array<c64, 64> initial;
std::generate(
begin(initial),
end(initial),
[&] { return distribution(rng); });
begin(initial), end(initial), [&] { return distribution(rng); });

auto recursive = initial;
auto iterative = initial;
Expand All @@ -118,21 +115,13 @@ int main() {
iterative_cooley_tukey(begin(iterative), end(iterative));

// Check if the arrays are approximately equivalent
std::cout
<< std::right
<< std::setw(16) << "idx"
<< std::setw(16) << "rec"
<< std::setw(16) << "it"
<< std::setw(16) << "subtracted"
<< '\n';
std::cout << std::right << std::setw(16) << "idx" << std::setw(16) << "rec"
<< std::setw(16) << "it" << std::setw(16) << "subtracted" << '\n';
for (int i = 0; i < initial.size(); ++i) {
auto rec = recursive[i];
auto it = iterative[i];
std::cout
<< std::setw(16) << i
<< std::setw(16) << std::abs(rec)
<< std::setw(16) << std::abs(it)
<< std::setw(16) << (std::abs(rec) - std::abs(it))
<< '\n';
std::cout << std::setw(16) << i << std::setw(16) << std::abs(rec)
<< std::setw(16) << std::abs(it) << std::setw(16)
<< (std::abs(rec) - std::abs(it)) << '\n';
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iterator>
#include <numeric>
#include <random>
#include <iostream>
#include <vector>

// this header is so that we can use `not` and `and` on MSVC
#include <ciso646>
Expand Down Expand Up @@ -49,10 +49,9 @@ std::vector<person> make_person_list(size_t number_of_partners) {
};

std::vector<person> ret;
std::generate_n(
std::back_inserter(ret),
number_of_partners,
[&] { return person{false, 0, random_pref_list()}; });
std::generate_n(std::back_inserter(ret), number_of_partners, [&] {
return person{false, 0, random_pref_list()};
});

return ret;
}
Expand All @@ -61,11 +60,8 @@ template <typename LeadIter, typename FollowIter>
void stable_match(LeadIter leads, LeadIter leads_end, FollowIter follows) {
// for each index in the leads' preference list, we'll go through this
size_t const number_of_partners = leads_end - leads;
for (
size_t proposal_index = 0;
proposal_index < number_of_partners;
++proposal_index)
{
for (size_t proposal_index = 0; proposal_index < number_of_partners;
++proposal_index) {
/*
each follow will get their own vector of proposals to them
for each entry in the leads' proposal list
Expand All @@ -85,8 +81,8 @@ void stable_match(LeadIter leads, LeadIter leads_end, FollowIter follows) {

// for each follow, we'll look at their preference list
for (size_t i = 0; i < number_of_partners; ++i) {
for (size_t pref: follows[i].preference_list) {
for (size_t proposal: proposals[i]) {
for (size_t pref : follows[i].preference_list) {
for (size_t proposal : proposals[i]) {
// and, if they were given a proposal, then they'll choose their
// favorite here
if (pref == proposal and not follows[i].finished) {
Expand Down Expand Up @@ -114,11 +110,7 @@ int main() {

// the happy marriages are announced to the console here :)
for (size_t i = 0; i < number_of_partners; ++i) {
std::cout
<< "the partnership of lead "
<< i
<< " and follow "
<< leads[i].preference
<< " shall commence forthwith!\n";
std::cout << "the partnership of lead " << i << " and follow "
<< leads[i].preference << " shall commence forthwith!\n";
}
}
45 changes: 16 additions & 29 deletions chapters/computational_physics/euler/code/c++/euler.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <utility>
#include <vector>

using std::begin;
using std::end;
Expand All @@ -14,10 +14,9 @@ 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); });
std::generate_n(std::back_inserter(result), size, [&] {
return std::exchange(current, current - 3.0 * current * timestep);
});
return result;
}

Expand All @@ -31,39 +30,27 @@ bool check_result(Iter first, Iter last, double threshold, double timestep) {
for (size_t idx = 0; it != last; ++idx, ++it) {
double solution = std::exp(-3.0 * idx * timestep);
if (std::abs(*it - solution) > threshold) {
std::cout
<< "We found a value outside the threshold; the "
<< idx
<< "-th value was "
<< *it
<< ", but the expected solution was "
<< solution
<< '\n';
std::cout
<< "(the threshold was "
<< threshold
<< " and the difference was "
<< std::abs(*it - solution)
<< ")\n";
std::cout << "We found a value outside the threshold; the " << idx
<< "-th value was " << *it << ", but the expected solution was "
<< solution << '\n';
std::cout << "(the threshold was " << threshold
<< " and the difference was " << std::abs(*it - solution)
<< ")\n";
return true;
}
}
return false;
}

int main(){
int main() {
double threshold = 0.01;
double timestep = 0.01;

auto result = solve_euler(timestep, 100);
auto outside_threshold =
check_result(begin(result), end(result), threshold, timestep);
check_result(begin(result), end(result), threshold, timestep);
auto msg = outside_threshold ? "yes :(" : "no :D";

std::cout
<< "Were any of the values outside of the threshold ("
<< threshold
<< ")? "
<< msg
<< '\n';
std::cout << "Were any of the values outside of the threshold (" << threshold
<< ")? " << msg << '\n';
}
103 changes: 49 additions & 54 deletions chapters/computational_physics/verlet/code/c++/verlet.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
#include <iostream>

// Simple function for velocity-verlet
void verlet(double pos, double acc, double dt){
void verlet(double pos, double acc, double dt) {

// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;

while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}

std::cout << time << '\n';
while (pos > 0) {
time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}

std::cout << time << '\n';
}

// Simple function for stormer-verlet
void stormer_verlet(double pos, double acc, double dt){

// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;

// The acceleration is constant, so the velocity is straightforward
vel += acc*dt;
}

std::cout << time << '\n';

void stormer_verlet(double pos, double acc, double dt) {

// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0) {
time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;

// The acceleration is constant, so the velocity is straightforward
vel += acc * dt;
}

std::cout << time << '\n';
}

void velocity_verlet(double pos, double acc, double dt){

double time, vel;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
pos += vel*dt + 0.5*acc * dt * dt;
vel += acc*dt;
}
void velocity_verlet(double pos, double acc, double dt) {

std::cout << time << '\n';
double time, vel;
vel = 0;
time = 0;
while (pos > 0) {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}

std::cout << time << '\n';
}

int main(){
int main() {

// Note that depending on the simulation, you might want to have the verlet
// loop outside.

// For example, if your acceleration chages as a function of time, you might
// need to also change the acceleration to be read into each of these
// functions
verlet(5.0, -10, 0.01);
stormer_verlet(5.0, -10, 0.01);
velocity_verlet(5.0, -10, 0.01);
// Note that depending on the simulation, you might want to have the verlet
// loop outside.

// For example, if your acceleration chages as a function of time, you might
// need to also change the acceleration to be read into each of these
// functions
verlet(5.0, -10, 0.01);
stormer_verlet(5.0, -10, 0.01);
velocity_verlet(5.0, -10, 0.01);
}

Loading