Skip to content

Commit 53337bb

Browse files
ubsanleios
authored andcommitted
switch over to clang-format style (#61)
* switch over to clang-format style I can upload a .clang-format if you want, as well * fix the md files that refer to line nums also, fix Tree.cpp * add a clang-format to the base directory
1 parent be04fc9 commit 53337bb

File tree

11 files changed

+335
-365
lines changed

11 files changed

+335
-365
lines changed

.clang-format

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BasedOnStyle: LLVM
2+
3+
AlignAfterOpenBracket: AlwaysBreak
4+
AlwaysBreakTemplateDeclarations: true
5+
BinPackArguments: false
6+
BinPackParameters: false
7+
PointerAlignment: Left

chapters/computational_mathematics/FFT/code/c++/fft.cpp

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// written by Gathros, modernized by Nicole Mazzuca.
22

3-
#include <complex>
4-
#include <vector>
3+
#include <algorithm>
54
#include <array>
5+
#include <complex>
66
#include <cstdint>
7-
#include <algorithm>
7+
#include <vector>
88

99
// These headers are for presentation not for the algorithm.
10-
#include <random>
11-
#include <iostream>
1210
#include <iomanip>
11+
#include <iostream>
12+
#include <random>
1313

1414
using std::begin;
1515
using std::end;
@@ -51,12 +51,11 @@ void cooley_tukey(Iter first, Iter last) {
5151
auto& bottom = first[k];
5252
auto& top = first[k + size / 2];
5353
top = bottom - w * top;
54-
bottom -= (top - bottom);
54+
bottom -= top - bottom;
5555
}
5656
}
5757
}
5858

59-
6059
// note: (last - first) must be less than 2**32 - 1
6160
template <typename Iter>
6261
void sort_by_bit_reverse(Iter first, Iter last) {
@@ -90,7 +89,7 @@ void iterative_cooley_tukey(Iter first, Iter last) {
9089
auto v = c64(1.0);
9190
for (size_t k = 0; k < stride / 2; k++) {
9291
first[k + j + stride / 2] =
93-
first[k + j] - v * first[k + j + stride / 2];
92+
first[k + j] - v * first[k + j + stride / 2];
9493
first[k + j] -= (first[k + j + stride / 2] - first[k + j]);
9594
v *= w;
9695
}
@@ -106,9 +105,7 @@ int main() {
106105

107106
std::array<c64, 64> initial;
108107
std::generate(
109-
begin(initial),
110-
end(initial),
111-
[&] { return distribution(rng); });
108+
begin(initial), end(initial), [&] { return distribution(rng); });
112109

113110
auto recursive = initial;
114111
auto iterative = initial;
@@ -118,21 +115,13 @@ int main() {
118115
iterative_cooley_tukey(begin(iterative), end(iterative));
119116

120117
// Check if the arrays are approximately equivalent
121-
std::cout
122-
<< std::right
123-
<< std::setw(16) << "idx"
124-
<< std::setw(16) << "rec"
125-
<< std::setw(16) << "it"
126-
<< std::setw(16) << "subtracted"
127-
<< '\n';
118+
std::cout << std::right << std::setw(16) << "idx" << std::setw(16) << "rec"
119+
<< std::setw(16) << "it" << std::setw(16) << "subtracted" << '\n';
128120
for (int i = 0; i < initial.size(); ++i) {
129121
auto rec = recursive[i];
130122
auto it = iterative[i];
131-
std::cout
132-
<< std::setw(16) << i
133-
<< std::setw(16) << std::abs(rec)
134-
<< std::setw(16) << std::abs(it)
135-
<< std::setw(16) << (std::abs(rec) - std::abs(it))
136-
<< '\n';
123+
std::cout << std::setw(16) << i << std::setw(16) << std::abs(rec)
124+
<< std::setw(16) << std::abs(it) << std::setw(16)
125+
<< (std::abs(rec) - std::abs(it)) << '\n';
137126
}
138127
}

chapters/computational_mathematics/decision_problems/stable_marriage/code/c++/stable_marriage.cpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include <vector>
21
#include <algorithm>
3-
#include <numeric>
2+
#include <iostream>
43
#include <iterator>
4+
#include <numeric>
55
#include <random>
6-
#include <iostream>
6+
#include <vector>
77

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

5151
std::vector<person> ret;
52-
std::generate_n(
53-
std::back_inserter(ret),
54-
number_of_partners,
55-
[&] { return person{false, 0, random_pref_list()}; });
52+
std::generate_n(std::back_inserter(ret), number_of_partners, [&] {
53+
return person{false, 0, random_pref_list()};
54+
});
5655

5756
return ret;
5857
}
@@ -61,11 +60,8 @@ template <typename LeadIter, typename FollowIter>
6160
void stable_match(LeadIter leads, LeadIter leads_end, FollowIter follows) {
6261
// for each index in the leads' preference list, we'll go through this
6362
size_t const number_of_partners = leads_end - leads;
64-
for (
65-
size_t proposal_index = 0;
66-
proposal_index < number_of_partners;
67-
++proposal_index)
68-
{
63+
for (size_t proposal_index = 0; proposal_index < number_of_partners;
64+
++proposal_index) {
6965
/*
7066
each follow will get their own vector of proposals to them
7167
for each entry in the leads' proposal list
@@ -85,8 +81,8 @@ void stable_match(LeadIter leads, LeadIter leads_end, FollowIter follows) {
8581

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

115111
// the happy marriages are announced to the console here :)
116112
for (size_t i = 0; i < number_of_partners; ++i) {
117-
std::cout
118-
<< "the partnership of lead "
119-
<< i
120-
<< " and follow "
121-
<< leads[i].preference
122-
<< " shall commence forthwith!\n";
113+
std::cout << "the partnership of lead " << i << " and follow "
114+
<< leads[i].preference << " shall commence forthwith!\n";
123115
}
124116
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include <iostream>
1+
#include <algorithm>
22
#include <cmath>
3-
#include <vector>
43
#include <cstddef>
4+
#include <iostream>
55
#include <iterator>
6-
#include <algorithm>
76
#include <utility>
7+
#include <vector>
88

99
using std::begin;
1010
using std::end;
@@ -14,10 +14,9 @@ using std::size_t;
1414
std::vector<double> solve_euler(double timestep, size_t size) {
1515
std::vector<double> result;
1616
double current = 1.0;
17-
std::generate_n(
18-
std::back_inserter(result),
19-
size,
20-
[&] { return std::exchange(current, current - 3.0 * current * timestep); });
17+
std::generate_n(std::back_inserter(result), size, [&] {
18+
return std::exchange(current, current - 3.0 * current * timestep);
19+
});
2120
return result;
2221
}
2322

@@ -31,39 +30,27 @@ bool check_result(Iter first, Iter last, double threshold, double timestep) {
3130
for (size_t idx = 0; it != last; ++idx, ++it) {
3231
double solution = std::exp(-3.0 * idx * timestep);
3332
if (std::abs(*it - solution) > threshold) {
34-
std::cout
35-
<< "We found a value outside the threshold; the "
36-
<< idx
37-
<< "-th value was "
38-
<< *it
39-
<< ", but the expected solution was "
40-
<< solution
41-
<< '\n';
42-
std::cout
43-
<< "(the threshold was "
44-
<< threshold
45-
<< " and the difference was "
46-
<< std::abs(*it - solution)
47-
<< ")\n";
33+
std::cout << "We found a value outside the threshold; the " << idx
34+
<< "-th value was " << *it << ", but the expected solution was "
35+
<< solution << '\n';
36+
std::cout << "(the threshold was " << threshold
37+
<< " and the difference was " << std::abs(*it - solution)
38+
<< ")\n";
4839
return true;
4940
}
5041
}
5142
return false;
5243
}
5344

54-
int main(){
45+
int main() {
5546
double threshold = 0.01;
5647
double timestep = 0.01;
5748

5849
auto result = solve_euler(timestep, 100);
5950
auto outside_threshold =
60-
check_result(begin(result), end(result), threshold, timestep);
51+
check_result(begin(result), end(result), threshold, timestep);
6152
auto msg = outside_threshold ? "yes :(" : "no :D";
6253

63-
std::cout
64-
<< "Were any of the values outside of the threshold ("
65-
<< threshold
66-
<< ")? "
67-
<< msg
68-
<< '\n';
54+
std::cout << "Were any of the values outside of the threshold (" << threshold
55+
<< ")? " << msg << '\n';
6956
}
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,67 @@
11
#include <iostream>
22

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

6-
// Note that we are using a temp variable for the previous position
7-
double prev_pos, temp_pos, time;
8-
prev_pos = pos;
9-
time = 0;
6+
// Note that we are using a temp variable for the previous position
7+
double prev_pos, temp_pos, time;
8+
prev_pos = pos;
9+
time = 0;
1010

11-
while (pos > 0){
12-
time += dt;
13-
temp_pos = pos;
14-
pos = pos*2 - prev_pos + acc * dt * dt;
15-
prev_pos = temp_pos;
16-
}
17-
18-
std::cout << time << '\n';
11+
while (pos > 0) {
12+
time += dt;
13+
temp_pos = pos;
14+
pos = pos * 2 - prev_pos + acc * dt * dt;
15+
prev_pos = temp_pos;
16+
}
1917

18+
std::cout << time << '\n';
2019
}
2120

2221
// Simple function for stormer-verlet
23-
void stormer_verlet(double pos, double acc, double dt){
24-
25-
// Note that we are using a temp variable for the previous position
26-
double prev_pos, temp_pos, time, vel;
27-
prev_pos = pos;
28-
vel = 0;
29-
time = 0;
30-
while (pos > 0){
31-
time += dt;
32-
temp_pos = pos;
33-
pos = pos*2 - prev_pos + acc * dt * dt;
34-
prev_pos = temp_pos;
35-
36-
// The acceleration is constant, so the velocity is straightforward
37-
vel += acc*dt;
38-
}
39-
40-
std::cout << time << '\n';
41-
22+
void stormer_verlet(double pos, double acc, double dt) {
23+
24+
// Note that we are using a temp variable for the previous position
25+
double prev_pos, temp_pos, time, vel;
26+
prev_pos = pos;
27+
vel = 0;
28+
time = 0;
29+
while (pos > 0) {
30+
time += dt;
31+
temp_pos = pos;
32+
pos = pos * 2 - prev_pos + acc * dt * dt;
33+
prev_pos = temp_pos;
34+
35+
// The acceleration is constant, so the velocity is straightforward
36+
vel += acc * dt;
37+
}
38+
39+
std::cout << time << '\n';
4240
}
4341

44-
void velocity_verlet(double pos, double acc, double dt){
45-
46-
double time, vel;
47-
vel = 0;
48-
time = 0;
49-
while (pos > 0){
50-
time += dt;
51-
pos += vel*dt + 0.5*acc * dt * dt;
52-
vel += acc*dt;
53-
}
42+
void velocity_verlet(double pos, double acc, double dt) {
5443

55-
std::cout << time << '\n';
44+
double time, vel;
45+
vel = 0;
46+
time = 0;
47+
while (pos > 0) {
48+
time += dt;
49+
pos += vel * dt + 0.5 * acc * dt * dt;
50+
vel += acc * dt;
51+
}
5652

53+
std::cout << time << '\n';
5754
}
5855

59-
int main(){
56+
int main() {
6057

61-
// Note that depending on the simulation, you might want to have the verlet
62-
// loop outside.
63-
64-
// For example, if your acceleration chages as a function of time, you might
65-
// need to also change the acceleration to be read into each of these
66-
// functions
67-
verlet(5.0, -10, 0.01);
68-
stormer_verlet(5.0, -10, 0.01);
69-
velocity_verlet(5.0, -10, 0.01);
58+
// Note that depending on the simulation, you might want to have the verlet
59+
// loop outside.
7060

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

0 commit comments

Comments
 (0)