Skip to content

Commit 1920e2f

Browse files
authored
Merge branch 'algorithm-archivists:master' into computus_in_javascript_typescript
2 parents 8ef64b1 + 2797d85 commit 1920e2f

Some content is hidden

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

51 files changed

+1296
-32
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: build
2+
on: pull_request
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout
9+
uses: actions/checkout@v2
10+
with:
11+
persist-credentials: false
12+
13+
- name: Install and build
14+
run: |
15+
npm install
16+
npx honkit build

.github/workflows/deploy.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Build and Deploy
2+
on:
3+
push:
4+
branches:
5+
- master
6+
7+
jobs:
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
10+
if: github.repository == 'algorithm-archivists/algorithm-archive'
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
with:
15+
persist-credentials: false
16+
17+
- name: Install and build
18+
run: |
19+
npm install
20+
npx honkit build
21+
22+
- name: Deploy
23+
uses: JamesIves/github-pages-deploy-action@4.1.4
24+
with:
25+
branch: gh-pages
26+
folder: _book

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
5555
- Jonathan Dönszelmann
5656
- Ishaan Verma
5757
- Delphi1024
58+
- ntindle

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [Plotting](contents/plotting/plotting.md)
77
* [Domain Coloring](contents/domain_coloring/domain_coloring.md)
88
* [Iterated Function Systems](contents/IFS/IFS.md)
9+
* [The Barnsley Fern](contents/barnsley/barnsley.md)
910
* [Data Structures](contents/data_structures/data_structures.md)
1011
* [Stacks and Queues](contents/stacks_and_queues/stacks_and_queues.md)
1112
* [Mathematical Background](contents/mathematical_background/mathematical_background.md)
@@ -43,3 +44,4 @@
4344
* [Flood Fill](contents/flood_fill/flood_fill.md)
4445
* [Quantum Information](contents/quantum_information/quantum_information.md)
4546
* [Computus](contents/computus/computus.md)
47+
* [Approximate Counting Algorithm](contents/approximate_counting/approximate_counting.md)

contents/IFS/IFS.md

Lines changed: 47 additions & 29 deletions
Large diffs are not rendered by default.

contents/IFS/code/c/IFS.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main() {
3636

3737
chaos_game(shape_points, 3, out_points, 1000);
3838

39-
FILE *fp = fopen("out.dat", "w+");
39+
FILE *fp = fopen("sierpinksi.dat", "w+");
4040

4141
for (int i = 0; i < 1000; ++i) {
4242
fprintf(fp, "%f\t%f\n", out_points[i].x, out_points[i].y);

contents/IFS/code/java/IFS.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.FileWriter;
2+
import java.util.Random;
3+
4+
public class IFS {
5+
6+
private static class Point {
7+
double x, y;
8+
9+
public Point(double x, double y) {
10+
this.x = x;
11+
this.y = y;
12+
}
13+
}
14+
15+
// This is a function to simulate a "chaos game"
16+
public static Point[] chaosGame(int n, Point[] shapePoints) {
17+
Random rng = new Random();
18+
19+
// Initialize output vector
20+
Point[] outputPoints = new Point[n];
21+
22+
// Choose first point randomly
23+
Point point = new Point(rng.nextDouble(), rng.nextDouble());
24+
25+
for (int i = 0; i < n; i++) {
26+
outputPoints[i] = point;
27+
28+
// Clone point to get a new reference
29+
point = new Point(point.x, point.y);
30+
31+
// Retrieve random shape point
32+
Point temp = shapePoints[rng.nextInt(shapePoints.length)];
33+
// Calculate midpoint
34+
point.x = 0.5 * (point.x + temp.x);
35+
point.y = 0.5 * (point.y + temp.y);
36+
}
37+
38+
return outputPoints;
39+
}
40+
41+
public static void main(String[] args) throws Exception {
42+
// This will generate a Sierpinski triangle with a chaos game of n points for an
43+
// initial triangle with three points on the vertices of an equilateral triangle:
44+
// A = (0.0, 0.0)
45+
// B = (0.5, sqrt(0.75))
46+
// C = (1.0, 0.0)
47+
// It will output the file sierpinski.dat, which can be plotted after
48+
Point[] shapePoints = new Point[]{
49+
new Point(0.0, 0.0),
50+
new Point(0.5, Math.sqrt(0.75)),
51+
new Point(1.0, 0.0)
52+
};
53+
Point[] outputPoints = chaosGame(10000, shapePoints);
54+
55+
FileWriter fw = new FileWriter("sierpinski.dat");
56+
for (Point p : outputPoints)
57+
fw.write(p.x + "\t" + p.y + "\n");
58+
fw.close();
59+
}
60+
61+
}

contents/IFS/code/julia/IFS.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ shape_points = [[0.0, 0.0],
2626
[0.5, sqrt(0.75)],
2727
[1.0, 0.0]]
2828
output_points = chaos_game(10000, shape_points)
29-
writedlm("out.dat", output_points)
29+
writedlm("sierpinski.dat", output_points)

contents/IFS/res/IFS_square_3.png

2.44 MB
Loading

contents/approximate_counting/approximate_counting.md

Lines changed: 398 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Test
2+
3+
# This function takes
4+
# - v: value in register
5+
# - a: a scaling value for the logarithm based on Morris's paper
6+
# It returns n(v,a), the approximate count
7+
function n(v, a)
8+
a*((1+1/a)^v-1)
9+
end
10+
11+
# This function takes
12+
# - v: value in register
13+
# - a: a scaling value for the logarithm based on Morris's paper
14+
# It returns a new value for v
15+
function increment(v, a)
16+
# delta is the probability of incrementing our counter
17+
delta = 1/(n(v+1, a)-n(v, a))
18+
19+
if rand() <= delta
20+
return v += 1
21+
else
22+
return v
23+
end
24+
end
25+
26+
# This simulates counting and takes
27+
# - n_items: number of items to count and loop over
28+
# - a: a scaling value for the logarithm based on Morris's paper
29+
# It returns n(v,a), the approximate count
30+
function approximate_count(n_items, a)
31+
v = 0
32+
for i = 1:n_items
33+
v = increment(v, a)
34+
end
35+
36+
return n(v, a)
37+
end
38+
39+
# This function takes
40+
# - n_trials: the number of counting trials
41+
# - n_items: the number of items to count to
42+
# - a: a scaling value for the logarithm based on Morris's paper
43+
# - threshold: the maximum percent error allowed
44+
# It returns a true / false test value
45+
function test_approximate_count(n_trials, n_items, a, threshold)
46+
samples = [approximate_count(n_items, a) for i = 1:n_trials]
47+
48+
avg = sum(samples)/n_trials
49+
50+
@test ((avg - n_items) / n_items < threshold)
51+
end
52+
53+
@testset "Counting Tests, 100 trials" begin
54+
println("testing 1,000, a = 30, 1% error")
55+
test_approximate_count(100, 1000, 30, 0.1)
56+
println("testing 12,345, a = 10, 1% error")
57+
test_approximate_count(100, 12345, 10, 0.1)
58+
# Note: with a lower a, we need more trials, so a higher % error here.
59+
println("testing 222,222, a = 0.5, 10% error")
60+
test_approximate_count(100, 222222, 0.5, 0.2)
61+
end
Loading
Loading
Loading
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using DelimitedFiles
2+
3+
# n = number of events
4+
# prob = probability of incrementing counter
5+
function random_count(n, prob; a=30, prob_calc=false)
6+
v = 0
7+
base = 1+1/a
8+
output = zeros(n)
9+
10+
if prob_calc
11+
prob = 1
12+
v = 1
13+
end
14+
15+
if rand() < prob
16+
output[1] = 1/prob
17+
end
18+
19+
for i = 2:n
20+
if prob_calc
21+
prob = 1/((a*(base^(v+1)-1))-output[i-1])
22+
23+
if rand() <= prob
24+
v += 1
25+
end
26+
output[i] = a*(base^v-1)
27+
else
28+
if rand() <= prob
29+
output[i] = output[i-1]+1/prob
30+
else
31+
output[i] = output[i-1]
32+
end
33+
end
34+
end
35+
36+
return output
37+
end
38+
39+
# m = number of counting trials
40+
# l = number of saved trials
41+
function multi_count(n, m, l, prob; a=30, prob_calc=false, file_mod="",
42+
stops = [100000, 500000, 1000000])
43+
out = zeros(n, l)
44+
extremes = zeros(n, 2)
45+
46+
chosen_set = zeros(Int, l)
47+
48+
for i = 1:l
49+
chosen_number = rand(1:m)
50+
while chosen_number in chosen_set
51+
chosen_number = rand(1:m)
52+
end
53+
chosen_set[i] = chosen_number
54+
end
55+
56+
histograms = zeros(Float64, m, length(stops))
57+
58+
out_count = 1
59+
for i = 1:m
60+
current_dist = random_count(n, prob; a, prob_calc)
61+
if i == 1
62+
extremes[:,1] .= current_dist
63+
extremes[:,2] .= current_dist
64+
else
65+
for j = 1:n
66+
if current_dist[j] < extremes[j,1]
67+
extremes[j,1] = current_dist[j]
68+
end
69+
if current_dist[j] > extremes[j,2]
70+
extremes[j,2] = current_dist[j]
71+
end
72+
73+
end
74+
end
75+
76+
for j = 1:length(stops)
77+
histograms[i,j] = current_dist[stops[j]]
78+
end
79+
80+
if i in chosen_set
81+
out[:,out_count] = current_dist
82+
out_count += 1
83+
end
84+
end
85+
86+
output_file = open("out"*file_mod*".dat", "w")
87+
writedlm(output_file, out)
88+
close(output_file)
89+
90+
extreme_output_file = open("extremes"*file_mod*".dat", "w")
91+
writedlm(extreme_output_file, extremes)
92+
close(extreme_output_file)
93+
94+
formatted_histograms = [[],[],[]]
95+
# Going through the histogram data to put it into the right format
96+
for j = 1:length(stops)
97+
98+
max = floor(Int,maximum(histograms[:,j]))
99+
min = floor(Int,minimum(histograms[:,j]))
100+
101+
println(min, '\t', max, '\t', sum(histograms[:,j])/m)
102+
103+
temp_array = zeros(max - min+1)
104+
105+
for i = 1:m
106+
107+
temp_array[floor(Int,histograms[i,j])-min+1] += 1
108+
109+
end
110+
111+
formatted_histograms[j] = temp_array
112+
end
113+
114+
# output histograms into different files for each one
115+
116+
for i = 1:length(stops)
117+
histogram_file = open("histogram_" * string(i)*file_mod* ".dat", "w")
118+
writedlm(histogram_file, formatted_histograms[i])
119+
close(histogram_file)
120+
end
121+
return formatted_histograms
122+
end
123+
72.4 KB
Loading
77.8 KB
Loading
39.8 KB
Loading
46.9 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
set terminal epslatex standalone color
2+
3+
#set output "histexp_1.tex"
4+
#set output "histexp_2.tex"
5+
set output "histexp_3.tex"
6+
7+
set size square
8+
9+
#set title "True count of 10,000"
10+
#set title "True count of 500,000"
11+
set title "True count of 1,000,000"
12+
13+
set xlabel ''
14+
#set xrange [-255:9745]
15+
#set xtics ("6000" -255, "10,000" 3745, "16,000" 9745)
16+
17+
#set xrange [352000:644000]
18+
#set xrange[-1458:538542]
19+
#set xtics ("320,000" -1458, "500,000" 178542, "860,000" 538542)
20+
21+
#set xrange [808000:1240000]
22+
set xrange[-19374:1100626]
23+
set xtics ("600,0000" -19374, "1,000,000" 380626, "1,720,000" 1100626)
24+
25+
#set ylabel 'Approximate count $\left( \times 10^{5} \right)$'
26+
#set ytics ("0" 0, "2" 200000, "4" 400000, "6" 600000, "8" 800000, "10" 1000000)
27+
28+
#plot "histogram_1exp.dat" w l lw 10 title ""
29+
#plot "histogram_2exp.dat" w l lw 10 title ""
30+
plot "histogram_3exp.dat" w l lw 10 title ""
31+
41.3 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
set terminal epslatex standalone color
2+
3+
#set output "hist_1.tex"
4+
#set output "hist_2.tex"
5+
set output "hist_3.tex"
6+
7+
set size square
8+
9+
#set title "True count of 10,000"
10+
#set title "True count of 500,000"
11+
set title "True count of 1,000,000"
12+
13+
#set xlabel ''
14+
#set xrange [0:40000]
15+
#set xtics ("0" 0, "20,000" 20000, "40,000" 40000)
16+
17+
#set xrange [352000:644000]
18+
#set xrange[10000:310000]
19+
#set xtics ("350,000" 10000, "500,000" 160000, "650,000" 310000)
20+
21+
#set xrange [808000:1240000]
22+
set xrange[-10000:490000]
23+
set xtics ("750,0000" -10000, "1,000,000" 240000, "1,250,000" 490000)
24+
25+
#set ylabel 'Approximate count $\left( \times 10^{5} \right)$'
26+
#set ytics ("0" 0, "2" 200000, "4" 400000, "6" 600000, "8" 800000, "10" 1000000)
27+
28+
#plot "histogram_1.dat" w l lw 10 title ""
29+
#plot "histogram_2.dat" w l lw 10 title ""
30+
plot "histogram_3.dat" w l lw 10 title ""
31+
51.7 KB
Loading
51.6 KB
Loading
41.8 KB
Loading
94.3 KB
Loading

0 commit comments

Comments
 (0)