Skip to content

Commit 5095802

Browse files
authored
Merge branch 'master' into stable_marriage_in_python
2 parents 833161c + a7a8dfb commit 5095802

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1428
-148
lines changed

CONTRIBUTORS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ This file lists everyone, who contributed to this repo and wanted to show up her
4242
- Christopher Milan
4343
- Vexatos
4444
- Raven-Blue Dragon
45-
- Björn Heinrichs
45+
- Björn Heinrichs
4646
- Olav Sundfør
4747
- Ben Chislett
4848
- dovisutu
4949
- Antetokounpo
5050
- Akash Dhiman
51+
- Vincent Zalzal
52+
- Jonathan D B Van Schenck
5153
- Amaras

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [Introduction](contents/introduction/introduction.md)
55
* [How To Contribute](contents/how_to_contribute/how_to_contribute.md)
66
* [Plotting](contents/plotting/plotting.md)
7+
* [Iterated Function Systems](contents/IFS/IFS.md)
78
* [Data Structures](contents/data_structures/data_structures.md)
89
* [Stacks and Queues](contents/stacks_and_queues/stacks_and_queues.md)
910
* [Mathematical Background](contents/mathematical_background/mathematical_background.md)

contents/IFS/IFS.md

Lines changed: 239 additions & 0 deletions
Large diffs are not rendered by default.

contents/IFS/code/c++/IFS.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <cmath>
2+
#include <fstream>
3+
#include <random>
4+
#include <vector>
5+
6+
// Simple X-Y point structure, along with some operators
7+
struct Point {
8+
double x, y;
9+
};
10+
11+
Point operator+(Point lhs, Point rhs) { return {lhs.x + rhs.x, lhs.y + rhs.y}; }
12+
Point operator*(double k, Point pt) { return {k * pt.x, k * pt.y}; }
13+
Point operator*(Point pt, double k) { return k * pt; }
14+
15+
using PointVector = std::vector<Point>;
16+
17+
// Returns a pseudo-random number generator
18+
std::default_random_engine& rng() {
19+
// Initialize static pseudo-random engine with non-deterministic random seed
20+
static std::default_random_engine randEngine(std::random_device{}());
21+
return randEngine;
22+
}
23+
24+
// Returns a random double in [0, 1)
25+
double drand() {
26+
return std::uniform_real_distribution<double>(0.0, 1.0)(rng());
27+
}
28+
29+
// Returns a random integer in [0, numElems-1]
30+
std::size_t randrange(std::size_t numElems) {
31+
return std::uniform_int_distribution<std::size_t>(0, numElems - 1)(rng());
32+
}
33+
34+
// Return a random point from the non-empty PointVector
35+
Point choose(const PointVector& points) {
36+
return points[randrange(points.size())];
37+
}
38+
39+
// This is a function to simulate a "chaos game"
40+
PointVector chaosGame(int numOutputPoints, const PointVector& inputPoints) {
41+
// Choose first point randomly
42+
Point curPoint = {drand(), drand()};
43+
44+
// For each output point, compute midpoint to random input point
45+
PointVector outputPoints(numOutputPoints);
46+
for (auto& outPoint : outputPoints) {
47+
outPoint = curPoint;
48+
curPoint = 0.5 * (curPoint + choose(inputPoints));
49+
}
50+
51+
return outputPoints;
52+
}
53+
54+
int main() {
55+
// This will generate a Sierpinski triangle with a chaos game of n points for
56+
// an initial triangle with three points on the vertices of an equilateral
57+
// triangle.
58+
PointVector inputPoints = {{0.0, 0.0}, {0.5, std::sqrt(0.75)}, {1.0, 0.0}};
59+
auto outputPoints = chaosGame(10000, inputPoints);
60+
61+
// It will output the file sierpinski.dat, which can be plotted after
62+
std::ofstream ofs("sierpinski.dat");
63+
for (auto pt : outputPoints)
64+
ofs << pt.x << '\t' << pt.y << '\n';
65+
}

contents/IFS/code/julia/IFS.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using DelimitedFiles
2+
3+
# This is a function to simulate a "chaos game"
4+
function chaos_game(n::Int, shape_points)
5+
6+
# Initializing the output array and the initial point
7+
output_points = zeros(n,2)
8+
point = [rand(), rand()]
9+
10+
for i = 1:n
11+
output_points[i,:] .= point
12+
point = 0.5*(rand(shape_points) .+ point)
13+
end
14+
15+
return output_points
16+
17+
end
18+
19+
# This will generate a Sierpinski triangle with a chaos game of n points for an
20+
# initial triangle with three points on the vertices of an equilateral triangle:
21+
# A = (0.0, 0.0)
22+
# B = (0.5, sqrt(0.75))
23+
# C = (1.0, 0.0)
24+
# It will output the file sierpinski.dat, which can be plotted after
25+
shape_points = [[0.0, 0.0],
26+
[0.5, sqrt(0.75)],
27+
[1.0, 0.0]]
28+
output_points = chaos_game(10000, shape_points)
29+
writedlm("out.dat", output_points)

contents/IFS/code/python/IFS.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from random import random, choice
2+
from math import sqrt
3+
4+
# This generator simulates a "chaos game"
5+
def chaos_game(n, shape_points):
6+
# Initialize the starting point
7+
point = [random(), random()]
8+
9+
for _ in range(n):
10+
# Update the point position and yield the result
11+
point = [(p + s) / 2 for p, s in zip(point, choice(shape_points))]
12+
yield point
13+
14+
# This will generate a Sierpinski triangle with a chaos game of n points for an
15+
# initial triangle with three points on the vertices of an equilateral triangle:
16+
# A = (0.0, 0.0)
17+
# B = (0.5, sqrt(0.75))
18+
# C = (1.0, 0.0)
19+
# It will output the file sierpinski.dat, which can be plotted after
20+
shape_points = [[0.0, 0.0],
21+
[0.5, sqrt(0.75)],
22+
[1.0, 0.0]]
23+
with open("sierpinski.dat", "w") as f:
24+
for point in chaos_game(10000, shape_points):
25+
f.write("{0}\t{1}\n".format(*point))

contents/IFS/res/IFS_square_1.png

6.84 KB
Loading

contents/IFS/res/IFS_square_2.png

93.2 KB
Loading

contents/IFS/res/IFS_square_vid_1.mp4

5.16 MB
Binary file not shown.

contents/IFS/res/IFS_triangle_1.png

372 KB
Loading

contents/IFS/res/IFS_triangle_2.png

6.32 KB
Loading

contents/IFS/res/IFS_triangle_3.png

7.48 KB
Loading

contents/IFS/res/IFS_triangle_4.png

14.8 KB
Loading

contents/IFS/res/IFS_triangle_5.png

219 KB
Loading
21.3 KB
Binary file not shown.
1.99 MB
Binary file not shown.

contents/IFS/res/chaos_1.png

375 KB
Loading

contents/IFS/res/chaos_2.png

413 KB
Loading

contents/IFS/res/chaos_vid_1.mp4

1.21 MB
Binary file not shown.

contents/IFS/res/chaos_vid_2.mp4

637 KB
Binary file not shown.

contents/bitlogic/bitlogic.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,37 @@ These operations are called *gates*, and follow somewhat straightforward logic.
9696
The *AND* gate, for example, reads in 2 bits and will only output a 1 value if both inputs are 1. This can be seen in the corresponding truth table:
9797

9898
<p>
99-
<img class="center" src="res/and.jpg" width="423" />
99+
<img class="center" src="res/and.jpg" style="width:50%" />
100100
</p>
101101

102102
The *OR* gate will output 1 if either input bits are 1:
103103

104104
<p>
105-
<img class="center" src="res/or.jpg" width="423" />
105+
<img class="center" src="res/or.jpg" style="width:50%" />
106106
</p>
107107

108108
The *exclusive OR* or *XOR* gate is the same as the *OR* gate, but will not output 1 if both bits are 1:
109109

110110
<p>
111-
<img class="center" src="res/xor.jpg" width="423" />
111+
<img class="center" src="res/xor.jpg" style="width:50%" />
112112
</p>
113113

114114
The *NOT* gate simply flips the input bit:
115115

116116
<p>
117-
<img class="center" src="res/not.jpg" width="423" />
117+
<img class="center" src="res/not.jpg" style="width:50%" />
118118
</p>
119119

120120
By combining the NOT and AND gates, we get the *NAND* gate:
121121

122122
<p>
123-
<img class="center" src="res/nand.jpg" width="423" />
123+
<img class="center" src="res/nand.jpg" style="width:50%" />
124124
</p>
125125

126126
And NOT and OR create *NOR*:
127127

128128
<p>
129-
<img class="center" src="res/nor.jpg" width="423" />
129+
<img class="center" src="res/nor.jpg" style="width:50%" />
130130
</p>
131131

132132
There are a few other gates, but this is enough for most things. We'll add more as the need arises!
@@ -157,12 +157,12 @@ The text of this chapter was written by [James Schloss](https://github.com/leios
157157
[<p><img class="center" src="../cc/CC-BY-SA_icon.svg" /></p>](https://creativecommons.org/licenses/by-sa/4.0/)
158158

159159
##### Images/Graphics
160-
- The image "[ANDgate](res/and.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
161-
- The image "[ORgate](res/or.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
162-
- The image "[XORgate](res/xor.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
163-
- The image "[NOTgate](res/not.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
164-
- The image "[NANDgate](res/nand.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
165-
- The image "[NORgate](res/nor.jpg)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
160+
- The image "[ANDgate](res/and.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
161+
- The image "[ORgate](res/or.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
162+
- The image "[XORgate](res/xor.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
163+
- The image "[NOTgate](res/not.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
164+
- The image "[NANDgate](res/nand.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
165+
- The image "[NORgate](res/nor.jpg)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
166166

167167
##### Pull Requests
168168

contents/bubble_sort/bubble_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
7272
[import:1-11, lang:"v"](code/v/bubble_sort.v)
7373
{% sample lang="scratch" %}
7474
<p>
75-
<img class="center" src="code/scratch/bubble_sort.svg" width="400" />
75+
<img class="center" src="code/scratch/bubble_sort.svg" style="width:75%"/>
7676
</p>
7777
{% sample lang="coffee" %}
7878
[import:1-6, lang:"coffeescript"](code/coffeescript/bubblesort.coffee)

contents/cc/license.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The text of this chapter was written by [James Schloss](https://github.com/leios
1212
[<p><img class="center" src="../cc/CC-BY-SA_icon.svg" /></p>](https://creativecommons.org/licenses/by-sa/4.0/)
1313

1414
##### Images/Graphics
15-
- The image "[example Image](res/example.png)" was created by [James Schloss](https://github.com/leios) and is licenced under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
15+
- The image "[example Image](res/example.png)" was created by [James Schloss](https://github.com/leios) and is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/legalcode).
1616

1717
##### Pull Requests
1818

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
std::string computus(int year, bool servois = false) {
5+
// Year's position on the 19 year metonic cycle
6+
int a = year % 19;
7+
8+
// Century index
9+
int k = year / 100;
10+
11+
// Shift of metonic cycle, add a day offset every 300 years
12+
int p = (13 + 8 * k) / 25;
13+
14+
// Correction for non-observed leap days
15+
int q = k / 4;
16+
17+
// Correction to starting point of calculation each century
18+
int M = (15 - p + k - q) % 30;
19+
20+
// Number of days from March 21st until the full moon
21+
int d = (19 * a + M) % 30;
22+
23+
// Returning if user wants value for Servois' table
24+
if (servois) {
25+
return std::to_string((21 + d) % 31);
26+
}
27+
28+
// Finding the next Sunday
29+
// Century-based offset in weekly calculation
30+
int N = (4 + k - q) % 7;
31+
32+
// Correction for leap days
33+
int b = year % 4;
34+
int c = year % 7;
35+
36+
// Days from d to next Sunday
37+
int e = (2 * b + 4 * c + 6 * d + N) % 7;
38+
39+
// Historical corrections for April 26 and 25
40+
if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) {
41+
e = -1;
42+
}
43+
44+
// Determination of the correct month for Easter
45+
return 22 + d + e > 31 ? "April " + std::to_string(d + e - 9)
46+
: "March " + std::to_string(22 + d + e);
47+
}
48+
49+
// Here, we will output the date of the Paschal full moon (using Servois
50+
// notation), and Easter for 2020-2030
51+
int main() {
52+
std::cout << "The following are the dates of the Paschal full moon (using "
53+
"Servois notation) and the date of Easter for 2020-2030 AD:\n"
54+
"Year\tServois number\tEaster\n";
55+
56+
for (int year = 2020; year <= 2030; year++) {
57+
std::cout << year << "\t\t" << computus(year, true) << '\t'
58+
<< computus(year) << std::endl;
59+
}
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
3+
char *computus(int year, int servois, char *out, size_t out_size) {
4+
// Year's position on the 19 year metonic cycle
5+
int a = year % 19;
6+
7+
// Century index
8+
int k = year / 100;
9+
10+
//Shift of metonic cycle, add a day offset every 300 years
11+
int p = (13 + 8 * k) / 25;
12+
13+
// Correction for non-observed leap days
14+
int q = k / 4;
15+
16+
// Correction to starting point of calculation each century
17+
int M = (15 - p + k - q) % 30;
18+
19+
// Number of days from March 21st until the full moon
20+
int d = (19 * a + M) % 30;
21+
22+
// Returning if user wants value for Servois' table
23+
if (servois) {
24+
snprintf(out, out_size, "%d",(21 + d) % 31);
25+
return out;
26+
}
27+
28+
// Finding the next Sunday
29+
// Century-based offset in weekly calculation
30+
int N = (4 + k - q) % 7;
31+
32+
// Correction for leap days
33+
int b = year % 4;
34+
int c = year % 7;
35+
36+
// Days from d to next Sunday
37+
int e = (2 * b + 4 * c + 6 * d + N) % 7;
38+
39+
// Historical corrections for April 26 and 25
40+
if ((d == 29 && e == 6) || (d == 28 && e == 6 && a > 10)) {
41+
e = -1;
42+
}
43+
44+
if ((22 + d + e) > 31) {
45+
snprintf(out, out_size, "April %d", d + e - 9);
46+
} else {
47+
snprintf(out, out_size, "March %d", 22 + d + e);
48+
}
49+
50+
return out;
51+
}
52+
53+
int main() {
54+
char tmp1[9], tmp2[9];
55+
56+
printf("The following are the dates of the Paschal full moon (using "
57+
"Servois notation) and the date of Easter for 2020-2030 AD:\n");
58+
59+
printf("Year\tServois number\tEaster\n");
60+
61+
for (int year = 2020; year <= 2030; year++) {
62+
printf("%d\t\t%s\t%s\n", year, computus(year, 1, tmp1, 9),
63+
computus(year, 0, tmp2, 9));
64+
}
65+
66+
return 0;
67+
}
68+

0 commit comments

Comments
 (0)