Skip to content

Commit 26ececd

Browse files
committed
removing principles of code chapter.
1 parent b7ed8e5 commit 26ececd

38 files changed

+496
-494
lines changed

SUMMARY.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
# Summary
22

33
* [Algorithm Archive](README.md)
4-
* [TODO](TODO.md)
54
* [Introduction](chapters/introduction/introduction.md)
6-
* [A Personal Note](chapters/introduction/my_introduction_to_hobby_programming.md)
75
* [How To Contribute](chapters/introduction/how_to_contribute.md)
8-
* [Principles of Code](chapters/principles_of_code/principles_of_code.md)
9-
* [Choosing A Language](chapters/principles_of_code/choosing_a_language/choosing_a_language.md)
10-
* [Compiled Languages](chapters/principles_of_code/choosing_a_language/compiled/compiled.md)
11-
* [Makefiles](chapters/principles_of_code/choosing_a_language/compiled/makefiles.md)
12-
* [FORTRAN](chapters/principles_of_code/choosing_a_language/compiled/FORTRAN.md)
13-
* [Building Blocks](chapters/principles_of_code/building_blocks/building_blocks.md)
14-
* [Variables and Types](chapters/principles_of_code/building_blocks/variables.md)
15-
* [Conditions](chapters/principles_of_code/building_blocks/conditions.md)
16-
* [Loops](chapters/principles_of_code/building_blocks/loops.md)
17-
* [Functions](chapters/principles_of_code/building_blocks/functions.md)
18-
* [Classes](chapters/principles_of_code/building_blocks/classes.md)
19-
* [Stacks and Queues](chapters/principles_of_code/building_blocks/stacks.md)
20-
* [Bit Logic](chapters/principles_of_code/building_blocks/bitlogic.md)
21-
* [Version Control](chapters/principles_of_code/version_control.md)
22-
* [Complexity Notation](chapters/principles_of_code/notation/notation.md)
23-
* [Convolutions](chapters/algorithms/convolutions/convolutions.md)
24-
* [Taylor Series](chapters/general/taylor_series_expansion/taylor_series_expansion.md)
6+
* [Version Control](chapters/introduction/version_control.md)
7+
* [Data Structures](chapters/data_structures/data_structures.md)
8+
* [Stacks and Queues](chapters/data_structures/stacks_and_queues/stacks_and_queues.md)
9+
* [Mathematical Background](chapters/mathematical_background/mathematical_background.md)
10+
* [Complexity Notation](chapters/mathematical_background/notation/notation.md)
11+
* [Bit Logic](chapters/mathematical_background/bitlogic/bitlogic.md)
12+
* [Convolutions](chapters/mathematical_background/convolutions/convolutions.md)
13+
* [Taylor Series](chapters/mathematical_background/taylor_series/taylor_series.md)
2514
* [Sorting and Searching](chapters/general/sorting_and_searching/sorting_and_searching.md)
2615
* [Bubble Sort](chapters/algorithms/bubble_sort/bubble_sort.md)
2716
* [Bogo Sort](chapters/algorithms/bogo_sort/bogo_sort.md)
2817
* [Tree Traversal](chapters/algorithms/tree_traversal/tree_traversal.md)
2918
* [Euclidean Algorithm](chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md)
30-
* [Multiplication](chapters/general/multiplication/multiplication.md)
3119
* [Monte Carlo](chapters/algorithms/monte_carlo_integration/monte_carlo_integration.md)
3220
* [Matrix Methods](chapters/general/matrix_methods/matrix_methods.md)
3321
* [Gaussian Elimination](chapters/algorithms/gaussian_elimination/gaussian_elimination.md)
@@ -45,7 +33,6 @@
4533
* [Backward Euler Methods](chapters/algorithms/backward_euler_method/backward_euler_method.md)
4634
* [Physics Solvers](chapters/general/physics_solvers/physics_solvers.md)
4735
* [Verlet Integration](chapters/algorithms/verlet_integration/verlet_integration.md)
48-
* [Barnes-Hut](chapters/algorithms/barnes_hut_algorithm/barnes_hut_algorithm.md)
4936
* [Quantum Systems](chapters/general/quantum_systems/quantum_systems.md)
5037
* [Split-Operator Method](chapters/algorithms/split-operator_method/split-operator_method.md)
5138
* [Data Compression](chapters/general/data_compression/data_compression.md)

TODO.md

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Data Structures
2+
3+
This is a book about algorithms.
4+
The fundamental building blocks of algorithms are data structures, and thus as more algorithms are added to the Archive, more data structures will be added to this section.

chapters/introduction/how_to_contribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The *Algorithm Archive* is an effort to learn about and teach algorithms as a community.
44
As such, it requires a certain level of trust between community members.
5-
For the most part, the collaboration can be done via GitHub and gitbook, so it is important to understand the basics of [version control](../principles_of_code/version_control.md).
5+
For the most part, the collaboration can be done via GitHub and gitbook, so it is important to understand the basics of [version control](version_control.md).
66
Ideally, all code provided by the community will be submitted via pull requests and discussed accordingly; however, I understand that many individuals are new to collaborative projects, so I will allow submissions by other means (comments, tweets, etc...).
77
As this project grows in size, it will be harder and harder to facilitate these submissions.
88
In addition, by submitting in any way other than pull requests, I cannot guarantee I will be able to list you as a collaborator (though I will certainly do my best to update the `CONTRIBUTORS.md` file accordingly).

chapters/principles_of_code/building_blocks/bitlogic.md renamed to chapters/mathematical_background/bitlogic/bitlogic.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
### Bit Logic
1+
# Bit Logic
22

33
We write code in a language that makes a little sense to us, but does not make sense at all to our computer without a compiler to transform the code we write into a language the computer can understand.
44
In the end, whenever we write code, all of the data structures we write are transformed into binary strings of 1's and 0's to be interpreted by our computer.
55
That said, it's not always obvious how this happens, so let's start the simple case of integer numbers.
66

7-
#### Integers
7+
## Integers
88
For integer numbers, 0 is still 0 and 1 is still 1; however, for 2, we need to use 2 digits because binary only has 0's and 1's. When we get to 4, we'll need 3 digits and when we get to 8, we'll need 4. Ever time we cross a power of 2, we'll need to add a new digit. Here's a table of the first 10 integers in binary:
99

1010
| Integer Number | Binary Number |
@@ -45,7 +45,7 @@ Another method is to "roll over" to negative numbers when the bit count gets too
4545

4646
Ultimately, integer numbers are not that difficult to deal with in binary, so let's move onto something more complicated: *floating-point numbers!*
4747

48-
#### Floating-point Numbers
48+
## Floating-point Numbers
4949
Floats are numbers with a decimal point.
5050
9.125 is a float. 9.000 is a float. 9 is an integer.
5151
Here are a few floats and their integer representations:
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <algorithm>
2+
#include <array>
3+
#include <cassert>
4+
#include <complex>
5+
#include <cstdint>
6+
#include <iterator>
7+
#include <vector>
8+
9+
// These headers are for presentation not for the algorithm.
10+
#include <iomanip>
11+
#include <iostream>
12+
#include <random>
13+
14+
using std::begin;
15+
using std::end;
16+
using std::swap;
17+
18+
using std::ptrdiff_t;
19+
using std::size_t;
20+
21+
using c64 = std::complex<double>;
22+
template <typename T>
23+
constexpr T pi() {
24+
return 3.14159265358979323846264338327950288419716;
25+
}
26+
27+
// This section is not a part of the algorithm
28+
template <typename Iter>
29+
void fft(Iter const first, Iter const last) {
30+
auto const size = last - first;
31+
if (size >= 2) {
32+
auto temp = std::vector<c64>(size / 2);
33+
for (ptrdiff_t i = 0; i < size / 2; ++i) {
34+
temp[i] = first[i * 2 + 1];
35+
first[i] = first[i * 2];
36+
}
37+
for (ptrdiff_t i = 0; i < size / 2; ++i) {
38+
first[i + size / 2] = temp[i];
39+
}
40+
41+
auto const split = first + size / 2;
42+
fft(first, split);
43+
fft(split, last);
44+
45+
for (ptrdiff_t k = 0; k < size / 2; ++k) {
46+
auto w = std::exp(c64(0, -2.0 * pi<double>() * k / size));
47+
48+
auto& bottom = first[k];
49+
auto& top = first[k + size / 2];
50+
top = bottom - w * top;
51+
bottom -= top - bottom;
52+
}
53+
}
54+
}
55+
56+
template <typename Iter>
57+
void inverse_fft(Iter const first, Iter const last) {
58+
std::for_each(first, last, [](auto& it) { it = std::conj(it); });
59+
60+
fft(first, last);
61+
62+
auto const size = static_cast<c64>(last - first);
63+
std::for_each(first, last, [&](auto& it) { it = std::conj(it) / size; });
64+
}
65+
66+
// This section is a part of the algorithm
67+
68+
template <typename S1, typename S2, typename Out>
69+
void conv(
70+
S1 const s1,
71+
S1 const s1_last,
72+
S2 const s2,
73+
S2 const s2_last,
74+
Out const out) {
75+
auto const size1 = s1_last - s1;
76+
auto const size2 = s2_last - s2;
77+
auto const size = size1 + size2;
78+
79+
for (ptrdiff_t i = 0; i < size; ++i) {
80+
c64 sum = 0;
81+
for (ptrdiff_t j = 0; j < i; ++j) {
82+
if (j < size1) {
83+
sum += s1[j] * s2[i - j];
84+
}
85+
}
86+
out[i] = sum;
87+
}
88+
}
89+
90+
template <typename S1, typename S2, typename Out>
91+
void conv_fft(S1 const s1, S1 const s1_last, S2 const s2, Out const out) {
92+
auto const size = s1_last - s1;
93+
94+
fft(s1, s1_last);
95+
fft(s2, s2 + size);
96+
97+
auto s1_it = s1;
98+
auto s2_it = s2;
99+
auto out_it = out;
100+
for (; s1_it != s1_last; ++s1_it, ++s2_it, ++out_it) {
101+
*out_it = *s1_it * *s2_it;
102+
}
103+
104+
inverse_fft(out, out_it);
105+
}
106+
107+
int main() {
108+
auto signal1 = std::array<c64, 64>();
109+
std::fill(begin(signal1) + 16, begin(signal1) + 48, 1);
110+
111+
auto signal2 = signal1;
112+
113+
auto out1 = std::array<c64, 128>();
114+
conv(begin(signal1), end(signal1), begin(signal2), end(signal2), begin(out1));
115+
116+
auto signal3 = std::array<c64, 128>();
117+
std::copy(begin(signal1), end(signal1), begin(signal3));
118+
auto signal4 = signal3;
119+
120+
auto out2 = std::array<c64, 128>();
121+
conv_fft(begin(signal3), end(signal3), begin(signal4), begin(out2));
122+
123+
std::cout << std::right << std::setw(16) << "i" << std::setw(16)
124+
<< "subtracted" << '\n';
125+
126+
for (size_t i = 0; i < signal1.size(); ++i) {
127+
std::cout << std::setw(16) << i << std::setw(16)
128+
<< (std::abs(out1[i]) - std::abs(out2[i])) << '\n';
129+
}
130+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "fft.h"
2+
3+
#include <stdio.h>
4+
5+
void conv(double complex *signal1, double complex *signal2, double complex* out,
6+
size_t n1, size_t n2) {
7+
double complex sum = 0;
8+
9+
for (size_t i = 0; i < (n1 < n2? n2 : n1); ++i) {
10+
for (size_t j = 0; j < i; ++j) {
11+
if (j < n1) {
12+
sum += signal1[j] * signal2[i-j];
13+
}
14+
}
15+
out[i] = sum;
16+
sum = 0;
17+
}
18+
}
19+
20+
void conv_fft(double complex *signal1, double complex *signal2,
21+
double complex* out, size_t n) {
22+
fft(signal1, n);
23+
fft(signal2, n);
24+
25+
for (size_t i = 0; i < n; ++i) {
26+
out[i] = signal1[i] * signal2[i];
27+
}
28+
29+
ifft(out, n);
30+
}
31+
32+
int main() {
33+
double complex signal1[64], signal2[64], signal3[128], signal4[128],
34+
out1[128], out2[128];
35+
36+
for (size_t i = 0; i < 128; ++i) {
37+
if (i >= 16 && i < 48) {
38+
signal1[i] = 1.0;
39+
signal2[i] = 1.0;
40+
signal3[i] = 1.0;
41+
signal4[i] = 1.0;
42+
out1[i] = 0.0;
43+
out2[i] = 0.0;
44+
} else if (i >= 64) {
45+
signal3[i] = 0.0;
46+
signal4[i] = 0.0;
47+
out1[i] = 0.0;
48+
out2[i] = 0.0;
49+
} else {
50+
signal1[i] = 0.0;
51+
signal2[i] = 0.0;
52+
signal3[i] = 0.0;
53+
signal4[i] = 0.0;
54+
out1[i] = 0.0;
55+
out2[i] = 0.0;
56+
}
57+
}
58+
59+
conv(signal1, signal2, out1, 64, 64);
60+
conv_fft(signal3, signal4, out2, 128);
61+
62+
for (size_t i = 0; i < 64; ++i) {
63+
printf("%zu %f %+fi\n", i, creal(out1[i]) - creal(out2[i]),
64+
cimag(out1[i]) - cimag(out2[i]));
65+
}
66+
67+
return 0;
68+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef FFT_H
2+
#define FFT_H
3+
4+
#include <complex.h>
5+
#include <math.h>
6+
7+
void fft(double complex *X, size_t N) {
8+
if (N >= 2) {
9+
double complex tmp [N / 2];
10+
for (size_t i = 0; i < N / 2; ++i) {
11+
tmp[i] = X[2 * i + 1];
12+
X[i] = X[2 * i];
13+
}
14+
15+
for (size_t i = 0; i < N / 2; ++i) {
16+
X[i + N / 2] = tmp[i];
17+
}
18+
19+
fft(X, N / 2);
20+
fft(X + N / 2, N / 2);
21+
22+
for (size_t i = 0; i < N / 2; ++i) {
23+
X[i + N/2] = X[i] - cexp(-2.0 * I * M_PI * i / N) * X[i + N / 2];
24+
X[i] -= (X[i + N / 2] - X[i]);
25+
}
26+
}
27+
}
28+
29+
void ifft(double complex *x, size_t n) {
30+
for (size_t i = 0; i < n; ++i) {
31+
x[i] = conj(x[i]);
32+
}
33+
34+
fft(x, n);
35+
36+
for (size_t i = 0; i < n; ++i) {
37+
x[i] = conj(x[i]) / n;
38+
}
39+
}
40+
41+
#endif //FFT_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Data.List (tails)
2+
3+
convolution :: (Num a) => [a] -> [a] -> [a]
4+
convolution x = map (sum . zipWith (*) (reverse x)) . spread
5+
where spread = init . tails . (replicate (length x - 1) 0 ++)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function conv(signal1::Vector{Complex}, signal2::Vector{Complex})
2+
n = length(signal1) + length(signal2)
3+
out = Vector{Complex}(n)
4+
sum = 0
5+
6+
for i = 0:n
7+
for j = 0:i
8+
if(j < length(signal1))
9+
sum += signal1[j] * signal2[i-j]
10+
end
11+
end
12+
out[i] = sum
13+
sum = 0
14+
end
15+
16+
return out
17+
end
18+
19+
function conv_fft(signal1::Vector{Complex}, signal2::Vector{Complex})
20+
return ifft(fft(signal1).*fft(signal2))
21+
end
22+

0 commit comments

Comments
 (0)