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/8] 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/8] 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/8] 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 b21bfc9b525b1ae70c393f25a52ff8b569748703 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 22 Oct 2021 21:29:54 +0200 Subject: [PATCH 4/8] fixed indentation --- .../code/java/ApproximateCounting.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java index d59cbbf72..9363d8c73 100644 --- a/contents/approximate_counting/code/java/ApproximateCounting.java +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -10,7 +10,7 @@ public class ApproximateCounting { * It returns the approximate count */ static double n(double v, double a) { - return a * (Math.pow(1 + 1 / a, v) - 1); + return a * (Math.pow(1 + 1 / a, v) - 1); } @@ -21,13 +21,13 @@ static double n(double v, double a) { * It returns the new value for v */ static double increment(double v, double a) { - double delta = 1 / (n(v + 1, a) - n(v, a)); + double delta = 1 / (n(v + 1, a) - n(v, a)); - if (Math.random() <= delta) { - return v + 1; - } else { - return v; - } + if (Math.random() <= delta) { + return v + 1; + } else { + return v; + } } @@ -39,13 +39,13 @@ static double increment(double v, double a) { * 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); - } + double v = 0; + + for (int i = 1; i < nItems + 1; i++) { + v = increment(v, a); + } - return n(v, a); + return n(v, a); } /* @@ -57,26 +57,26 @@ static double approximateCount(int nItems, double a) { * 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"); - } + 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 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); + 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 b0b382df0819a2f2cee73caab6ad85376af742b4 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Sat, 23 Oct 2021 13:51:22 +0200 Subject: [PATCH 5/8] fixed typos --- .../code/java/ApproximateCounting.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java index 9363d8c73..e9d35d36b 100644 --- a/contents/approximate_counting/code/java/ApproximateCounting.java +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -41,7 +41,7 @@ static double increment(double v, double a) { static double approximateCount(int nItems, double a) { double v = 0; - for (int i = 1; i < nItems + 1; i++) { + for (int i = 0; i < nItems; i++) { v = increment(v, a); } @@ -50,15 +50,15 @@ static double approximateCount(int nItems, double a) { /* * This function takes - * - nTrails: the number of counting trails + * - nTrials: the number of counting trails * - nItems: the number of items to count - * - a: a scaling value for th elogarithm based on Morris's paper + * - a: a scaling value for the logarithm 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) { + static void testApproximateCount(int nTrials, int nItems, double a, double threshold) { double avg = DoubleStream.generate(() -> approximateCount(nItems, a)) - .limit(nTrails) + .limit(nTrials) .average() .getAsDouble(); From aa6d0dc7a347b0e6578b9df9a5071696ec8cc14e Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Sat, 23 Oct 2021 13:52:46 +0200 Subject: [PATCH 6/8] fixed printed comments --- .../approximate_counting/code/java/ApproximateCounting.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java index e9d35d36b..438455e45 100644 --- a/contents/approximate_counting/code/java/ApproximateCounting.java +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -69,13 +69,13 @@ static void testApproximateCount(int nTrials, int nItems, double a, double thres public static void main(String args[]) { - System.out.println("testing 1,000, a = 30, 1% error"); + System.out.println("testing 1,000, a = 30, 10% error"); testApproximateCount(100, 1_000, 30, 0.1); - System.out.println("testing 12,345, a = 10, 1% error"); + System.out.println("testing 12,345, a = 10, 10% error"); testApproximateCount(100, 12_345, 10, 0.1); - System.out.println("testing 222,222, a = 0.5, 10% error"); + System.out.println("testing 222,222, a = 0.5, 20% error"); testApproximateCount(100, 222_222, 0.5, 0.2); } From 073978a7a52db2646495cf93f9dcac70705f9091 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Sat, 23 Oct 2021 13:55:02 +0200 Subject: [PATCH 7/8] added failed message --- .../approximate_counting/code/java/ApproximateCounting.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java index 438455e45..ed0a9b01f 100644 --- a/contents/approximate_counting/code/java/ApproximateCounting.java +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -64,6 +64,8 @@ static void testApproximateCount(int nTrials, int nItems, double a, double thres if (Math.abs((avg - nItems) / nItems) < threshold) { System.out.println("passed"); + } else { + System.out.println("failed"); } } From 3e98fb9a1c3e78710432e88c274e538eb1c23e27 Mon Sep 17 00:00:00 2001 From: James Schloss Date: Thu, 2 Dec 2021 16:46:28 +0100 Subject: [PATCH 8/8] Update contents/approximate_counting/code/java/ApproximateCounting.java Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com> --- .../code/java/ApproximateCounting.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/contents/approximate_counting/code/java/ApproximateCounting.java b/contents/approximate_counting/code/java/ApproximateCounting.java index ed0a9b01f..2dd49c0f0 100644 --- a/contents/approximate_counting/code/java/ApproximateCounting.java +++ b/contents/approximate_counting/code/java/ApproximateCounting.java @@ -71,13 +71,14 @@ static void testApproximateCount(int nTrials, int nItems, double a, double thres public static void main(String args[]) { - System.out.println("testing 1,000, a = 30, 10% error"); + System.out.println("[#]\nCounting Tests, 100 trials"); + System.out.println("[#]\ntesting 1,000, a = 30, 10% error"); testApproximateCount(100, 1_000, 30, 0.1); - System.out.println("testing 12,345, a = 10, 10% error"); + System.out.println("[#]\ntesting 12,345, a = 10, 10% error"); testApproximateCount(100, 12_345, 10, 0.1); - System.out.println("testing 222,222, a = 0.5, 20% error"); + System.out.println("[#]\ntesting 222,222, a = 0.5, 20% error"); testApproximateCount(100, 222_222, 0.5, 0.2); }