From 5ccad4438fc6a0708673c518445b413de7ad1836 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Thu, 21 Oct 2021 17:37:01 +0200 Subject: [PATCH 1/7] added approximative counting in java --- .../code/java/ApproximateCounting.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 contents/approximate_counting/code/java/ApproximateCounting.java diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java new file mode 100644 index 000000000..d59cbbf72 --- /dev/null +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -0,0 +1,82 @@ +import java.lang.Math; +import java.util.stream.DoubleStream; + +public class ApproximateCounting { + + /* + * This function taks + * - v: value in register + * - a: a scaling value for the logarithm based on Morris's paper + * It returns the approximate count + */ + static double n(double v, double a) { + return a * (Math.pow(1 + 1 / a, v) - 1); + } + + + /* + * This function takes + * - v: value in register + * - a: a scaling value for the logarithm based on Morris's paper + * It returns the new value for v + */ + static double increment(double v, double a) { + double delta = 1 / (n(v + 1, a) - n(v, a)); + + if (Math.random() <= delta) { + return v + 1; + } else { + return v; + } + } + + + + /* + * This function takes + * - v: value in register + * - a: a scaling value for the logarithm based on Morris's paper + * It returns the new value for v + */ + static double approximateCount(int nItems, double a) { + double v = 0; + + for (int i = 1; i < nItems + 1; i++) { + v = increment(v, a); + } + + return n(v, a); + } + + /* + * This function takes + * - nTrails: the number of counting trails + * - nItems: the number of items to count + * - a: a scaling value for th elogarithm based on Morris's paper + * - threshold: the maximum percent error allowed + * It terminates the program on failure + */ + static void testApproximateCount(int nTrails, int nItems, double a, double threshold) { + double avg = DoubleStream.generate(() -> approximateCount(nItems, a)) + .limit(nTrails) + .average() + .getAsDouble(); + + if (Math.abs((avg - nItems) / nItems) < threshold) { + System.out.println("passed"); + } + } + + + public static void main(String args[]) { + System.out.println("testing 1,000, a = 30, 1% error"); + testApproximateCount(100, 1_000, 30, 0.1); + + System.out.println("testing 12,345, a = 10, 1% error"); + testApproximateCount(100, 12_345, 10, 0.1); + + System.out.println("testing 222,222, a = 0.5, 10% error"); + testApproximateCount(100, 222_222, 0.5, 0.2); + } + +} From 6a2dacdd3064c0d8b6267c32a5c18bb8617d1142 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:28:17 +0200 Subject: [PATCH 2/7] added code examples to markdown files --- contents/approximate_counting/approximate_counting.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/approximate_counting/approximate_counting.md b/contents/approximate_counting/approximate_counting.md index 654721844..5dc21396e 100644 --- a/contents/approximate_counting/approximate_counting.md +++ b/contents/approximate_counting/approximate_counting.md @@ -366,6 +366,8 @@ As we do not have any objects to count, we will instead simulate the counting wi [import, lang:"cpp"](code/c++/approximate_counting.cpp) {% sample lang="python" %} [import, lang:"python"](code/python/approximate_counting.py) +{% sample lang:"java" %} +[import, lang="java"](code/java/ApproximateCounting.java) {% endmethod %} ### Bibliography From 75814d9fba76e9ff00e0c4a7375bd0dfad08731c Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:30:57 +0200 Subject: [PATCH 3/7] fixed typo --- contents/approximate_counting/approximate_counting.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/approximate_counting/approximate_counting.md b/contents/approximate_counting/approximate_counting.md index 5dc21396e..aa678bd03 100644 --- a/contents/approximate_counting/approximate_counting.md +++ b/contents/approximate_counting/approximate_counting.md @@ -366,8 +366,8 @@ As we do not have any objects to count, we will instead simulate the counting wi [import, lang:"cpp"](code/c++/approximate_counting.cpp) {% sample lang="python" %} [import, lang:"python"](code/python/approximate_counting.py) -{% sample lang:"java" %} -[import, lang="java"](code/java/ApproximateCounting.java) +{% sample lang="java" %} +[import, lang:"java"](code/java/ApproximateCounting.java) {% endmethod %} ### Bibliography From 03e8c216b8e1585452efbb317ee6c6c30e80798d Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 22 Oct 2021 14:16:12 +0200 Subject: [PATCH 4/7] added iterated function systems in rust --- contents/IFS/code/rust/IFS.rs | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 contents/IFS/code/rust/IFS.rs diff --git a/contents/IFS/code/rust/IFS.rs b/contents/IFS/code/rust/IFS.rs new file mode 100644 index 000000000..ad38aee87 --- /dev/null +++ b/contents/IFS/code/rust/IFS.rs @@ -0,0 +1,49 @@ +use std::fs::File; +use std::io::Write; + +// require create rand: +use rand::prelude::SliceRandom; +use rand::Rng; + +type Point = (f64, f64); + +// This function simulates a "chaos game" +fn chaos_game(n: u32, shape_points: Vec) -> Vec { + let mut rng = rand::thread_rng(); + + // initialize the output vector and the initial point + let mut output_points: Vec = Vec::new(); + let mut point = (rng.gen(), rng.gen()); + + for _ in 0..n { + output_points.push(point); + + let tmp = shape_points + .choose(&mut rng) + .expect("could not choose a shape point"); + + point = (0.5 * (point.0 + tmp.0), 0.5 * (point.1 + tmp.1)); + } + + output_points +} + +fn main() { + + // This will generate a Sierpinski triangle with a chaos game of n points for an + // initial triangle with three points on the vertices of an equilateral triangle: + // A = (0.0, 0.0) + // B = (0.5, sqrt(0.75)) + // C = (1.0, 0.0) + // It will output the file sierpinski.dat, which can be plotted after + + let shape_points = vec![(0.0, 0.0), (0.5, 0.75_f64.sqrt()), (1.0, 0.0)]; + + let output_points = chaos_game(10000, shape_points); + + let mut file = File::create("sierpinski.dat").expect("Unable to open/create the file"); + + for (x, y) in output_points { + writeln!(&mut file, "{}\t{}", x, y).expect("Unable to write to file"); + } +} From c8b7233825f8aee731ddc7e31ba324c4da6b073a Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 22 Oct 2021 14:19:35 +0200 Subject: [PATCH 5/7] added rust implementation to markdown file --- contents/IFS/IFS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 2ed1fe484..25769c59a 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -146,6 +146,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:4-16, lang:"coconut"](code/coconut/IFS.coco) {% sample lang="java" %} [import:16-39, lang:"java"](code/java/IFS.java) +{% sample lang="rs" %} +[import:11-29 lang:"rust"](code/rust/IFS.rs) {% endmethod %} If we set the initial point to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below: @@ -232,6 +234,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"coconut"](code/coconut/IFS.coco) {%sample lang="java" %} [import, lang:"java"](code/java/IFS.java) +{% sample, lang="rs" %} +[import, lang:"rust"](code/rust/IFS.rs) {% endmethod %} ### Bibliography From 3673be6952b32ed28b68abde2a279015a135d989 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 22 Oct 2021 21:06:07 +0200 Subject: [PATCH 6/7] fixed typo --- contents/IFS/IFS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 25769c59a..7338ad5b6 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -147,7 +147,7 @@ Here, instead of tracking children of children, we track a single individual tha {% sample lang="java" %} [import:16-39, lang:"java"](code/java/IFS.java) {% sample lang="rs" %} -[import:11-29 lang:"rust"](code/rust/IFS.rs) +[import:11-29, lang:"rust"](code/rust/IFS.rs) {% endmethod %} If we set the initial point to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below: From 1b4582227d7aaae6736990f759e9ef525ca022c0 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 22 Oct 2021 21:13:21 +0200 Subject: [PATCH 7/7] removed another typo --- contents/IFS/IFS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 7338ad5b6..4b2531095 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -234,7 +234,7 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"coconut"](code/coconut/IFS.coco) {%sample lang="java" %} [import, lang:"java"](code/java/IFS.java) -{% sample, lang="rs" %} +{% sample lang="rs" %} [import, lang:"rust"](code/rust/IFS.rs) {% endmethod %}