From d3e98dcea5b60f41d86d31c5c083b9ea14709adb Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Fri, 20 Aug 2021 04:20:35 +0200 Subject: [PATCH 1/7] Add Barnsley Farn java implementation --- contents/barnsley/barnsley.md | 2 + contents/barnsley/code/java/Barnsley.java | 96 +++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 contents/barnsley/code/java/Barnsley.java diff --git a/contents/barnsley/barnsley.md b/contents/barnsley/barnsley.md index 4464e4523..d214fdbd9 100644 --- a/contents/barnsley/barnsley.md +++ b/contents/barnsley/barnsley.md @@ -125,6 +125,8 @@ The biggest differences between the two code implementations is that the Barnsle {% method %} {% sample lang="jl" %} [import, lang:"julia"](code/julia/barnsley.jl) +{% sample lang="java" %} +[import, lang:"java"](code/java/Barnsley.java) {% endmethod %} ### Bibliography diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java new file mode 100644 index 000000000..5714506eb --- /dev/null +++ b/contents/barnsley/code/java/Barnsley.java @@ -0,0 +1,96 @@ +import java.io.FileWriter; +import java.io.IOException; +import java.util.Random; + +public class Barnsley { + + private static class Point { + double x, y, z; + + public Point(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public double[] toDoubleArray() { + return new double[]{this.x, this.y, this.z}; + } + } + + /* + This is a function that reads in the Hutchinson operator and corresponding + probabilities and outputs a randomly selected transform + This works by choosing a random number and then iterating through all + probabilities until it finds an appropriate bin + */ + public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabilities) { + Random rng = new Random(); + // Random number to be binned + double rand = rng.nextDouble(); + + // This checks to see if a random number is in a bin, if not, that + // probability is subtracted from the random number and we check the + // next bin in the list + for (int i = 0; i < probabilities.length; i++) { + if (rand < probabilities[i]) + return hutchinsonOp[i]; + rand -= probabilities[i]; + } + return null; + } + + /* + This is a general function to simulate a chaos game + n is the number of iterations + initialLocation is the starting point of the chaos game + hutchinsonOp is the set of functions to iterate through + probabilities is the set of probabilities corresponding to the likelihood + of choosing their corresponding function in hutchinsonOp + */ + public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutchinsonOp, double[] probabilities) { + // Initializing output points + Point[] outputPoints = new Point[n]; + Point point = initialLocation; + + for (int i = 0; i < n; i++) { + outputPoints[i] = point; + point = matrixMultiplication(selectArray(hutchinsonOp, probabilities), point.toDoubleArray()); + } + + return outputPoints; + } + + public static Point matrixMultiplication(double[][] operation, double[] point) { + double[] result = new double[3]; + for (int j = 0; j < operation[0].length; j++) + for (int i = 0; i < operation.length; i++) + result[j] += operation[j][i] * point[i]; + return new Point(result[0], result[1], result[2]); + } + + public static void main(String[] args) throws IOException { + double[][][] barnsleyHutchinson = { + {{0.0, 0.0, 0.0}, + {0.0, 0.16, 0.0}, + {0.0, 0.0, 1.0}}, + {{0.85, 0.04, 0.0}, + {-0.04, 0.85, 1.60}, + {0.0, 0.0, 1.0}}, + {{0.20, -0.26, 0.0}, + {0.23, 0.22, 1.60}, + {0.0, 0.0, 1.0}}, + {{-0.15, 0.28, 0.0}, + {0.26, 0.24, 0.44}, + {0.0, 0.0, 1.0}} + + }; + double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; + Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities); + FileWriter fw = new FileWriter("barnsley.dat"); + for (Point p : outputPoints) + fw.write(p.x + "\t" + p.y + "\n"); + fw.close(); + } + +} From af34bc275fad9c2b2c4d4ff0a9b910f182cf4982 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Fri, 20 Aug 2021 04:24:06 +0200 Subject: [PATCH 2/7] Change matrix formatting --- contents/barnsley/code/java/Barnsley.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index 5714506eb..a37ddd780 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -72,17 +72,17 @@ public static Point matrixMultiplication(double[][] operation, double[] point) { public static void main(String[] args) throws IOException { double[][][] barnsleyHutchinson = { {{0.0, 0.0, 0.0}, - {0.0, 0.16, 0.0}, - {0.0, 0.0, 1.0}}, + {0.0, 0.16, 0.0}, + {0.0, 0.0, 1.0}}, {{0.85, 0.04, 0.0}, - {-0.04, 0.85, 1.60}, - {0.0, 0.0, 1.0}}, + {-0.04, 0.85, 1.60}, + {0.0, 0.0, 1.0}}, {{0.20, -0.26, 0.0}, - {0.23, 0.22, 1.60}, - {0.0, 0.0, 1.0}}, + {0.23, 0.22, 1.60}, + {0.0, 0.0, 1.0}}, {{-0.15, 0.28, 0.0}, - {0.26, 0.24, 0.44}, - {0.0, 0.0, 1.0}} + {0.26, 0.24, 0.44}, + {0.0, 0.0, 1.0}} }; double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; From df42909cd914ed706b457c615e7dc350187dbb8f Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Fri, 20 Aug 2021 14:52:41 +0200 Subject: [PATCH 3/7] Change comment formatting --- contents/barnsley/code/java/Barnsley.java | 24 ++++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index a37ddd780..cb72c238b 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -18,12 +18,10 @@ public double[] toDoubleArray() { } } - /* - This is a function that reads in the Hutchinson operator and corresponding - probabilities and outputs a randomly selected transform - This works by choosing a random number and then iterating through all - probabilities until it finds an appropriate bin - */ + // This is a function that reads in the Hutchinson operator and corresponding + // probabilities and outputs a randomly selected transform + // This works by choosing a random number and then iterating through all + // probabilities until it finds an appropriate bin public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabilities) { Random rng = new Random(); // Random number to be binned @@ -40,14 +38,12 @@ public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabi return null; } - /* - This is a general function to simulate a chaos game - n is the number of iterations - initialLocation is the starting point of the chaos game - hutchinsonOp is the set of functions to iterate through - probabilities is the set of probabilities corresponding to the likelihood - of choosing their corresponding function in hutchinsonOp - */ + // This is a general function to simulate a chaos game + // n is the number of iterations + // initialLocation is the starting point of the chaos game + // hutchinsonOp is the set of functions to iterate through + // probabilities is the set of probabilities corresponding to the likelihood + // of choosing their corresponding function in hutchinsonOp public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutchinsonOp, double[] probabilities) { // Initializing output points Point[] outputPoints = new Point[n]; From d147092832028f04a23dacacac9b24eb04ea509c Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Fri, 20 Aug 2021 14:57:35 +0200 Subject: [PATCH 4/7] Small formatting issue --- contents/barnsley/code/java/Barnsley.java | 1 - 1 file changed, 1 deletion(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index cb72c238b..ec30d8165 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -79,7 +79,6 @@ public static void main(String[] args) throws IOException { {{-0.15, 0.28, 0.0}, {0.26, 0.24, 0.44}, {0.0, 0.0, 1.0}} - }; double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities); From a7f0d1e371345c977e567f2b6c9ce178e0a84772 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 21 Aug 2021 22:54:54 +0200 Subject: [PATCH 5/7] Various small fixes --- contents/barnsley/code/java/Barnsley.java | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index ec30d8165..f47d11ede 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -13,6 +13,12 @@ public Point(double x, double y, double z) { this.z = z; } + public Point(double[] coordinates) { + this.x = coordinates[0]; + this.y = coordinates[1]; + this.z = coordinates[2]; + } + public double[] toDoubleArray() { return new double[]{this.x, this.y, this.z}; } @@ -35,6 +41,8 @@ public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabi return hutchinsonOp[i]; rand -= probabilities[i]; } + // This return will never be reached, as the loop above ensures that at some point rand will be smaller + // than a probability. However, Java does not know this and thus this return is needed for compilation. return null; } @@ -51,21 +59,24 @@ public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutch for (int i = 0; i < n; i++) { outputPoints[i] = point; - point = matrixMultiplication(selectArray(hutchinsonOp, probabilities), point.toDoubleArray()); + point = new Point(matrixMultiplication(selectArray(hutchinsonOp, probabilities), point.toDoubleArray())); } return outputPoints; } - public static Point matrixMultiplication(double[][] operation, double[] point) { + public static double[] matrixMultiplication(double[][] operation, double[] point) { double[] result = new double[3]; - for (int j = 0; j < operation[0].length; j++) - for (int i = 0; i < operation.length; i++) + for (int j = 0; j < operation[0].length; j++) { + for (int i = 0; i < operation.length; i++){ result[j] += operation[j][i] * point[i]; - return new Point(result[0], result[1], result[2]); + } + } + + return result; } - public static void main(String[] args) throws IOException { + public static void main(String[] args) { double[][][] barnsleyHutchinson = { {{0.0, 0.0, 0.0}, {0.0, 0.16, 0.0}, @@ -82,10 +93,14 @@ public static void main(String[] args) throws IOException { }; double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities); - FileWriter fw = new FileWriter("barnsley.dat"); - for (Point p : outputPoints) - fw.write(p.x + "\t" + p.y + "\n"); - fw.close(); + try(FileWriter fw = new FileWriter("barnsley.dat")) { + for (Point p : outputPoints) { + fw.write(p.x + "\t" + p.y + "\n"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } } From e1f39bd06a4ce458c3f9d88ef13211f5de73eb2f Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 21 Aug 2021 23:17:45 +0200 Subject: [PATCH 6/7] Fixed spacing --- contents/barnsley/code/java/Barnsley.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index f47d11ede..b4584e147 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -68,7 +68,7 @@ public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutch public static double[] matrixMultiplication(double[][] operation, double[] point) { double[] result = new double[3]; for (int j = 0; j < operation[0].length; j++) { - for (int i = 0; i < operation.length; i++){ + for (int i = 0; i < operation.length; i++) { result[j] += operation[j][i] * point[i]; } } From 0b65d81e1ad0f177a5a4444e956f59c3dc99dde0 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 21 Aug 2021 23:41:25 +0200 Subject: [PATCH 7/7] Move Matrix multiplication to Point class --- contents/barnsley/code/java/Barnsley.java | 26 ++++++++--------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/contents/barnsley/code/java/Barnsley.java b/contents/barnsley/code/java/Barnsley.java index b4584e147..e18843ee2 100644 --- a/contents/barnsley/code/java/Barnsley.java +++ b/contents/barnsley/code/java/Barnsley.java @@ -5,7 +5,7 @@ public class Barnsley { private static class Point { - double x, y, z; + public double x, y, z; public Point(double x, double y, double z) { this.x = x; @@ -19,8 +19,12 @@ public Point(double[] coordinates) { this.z = coordinates[2]; } - public double[] toDoubleArray() { - return new double[]{this.x, this.y, this.z}; + public Point matrixMultiplication(double[][] matrix) { + double[] results = new double[3]; + for (int i = 0; i < 3; i++) { + results[i] = matrix[i][0] * x + matrix[i][1] * y + matrix[i][2] * z; + } + return new Point(results); } } @@ -59,23 +63,12 @@ public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutch for (int i = 0; i < n; i++) { outputPoints[i] = point; - point = new Point(matrixMultiplication(selectArray(hutchinsonOp, probabilities), point.toDoubleArray())); + point = point.matrixMultiplication(selectArray(hutchinsonOp, probabilities)); } return outputPoints; } - public static double[] matrixMultiplication(double[][] operation, double[] point) { - double[] result = new double[3]; - for (int j = 0; j < operation[0].length; j++) { - for (int i = 0; i < operation.length; i++) { - result[j] += operation[j][i] * point[i]; - } - } - - return result; - } - public static void main(String[] args) { double[][][] barnsleyHutchinson = { {{0.0, 0.0, 0.0}, @@ -93,14 +86,13 @@ public static void main(String[] args) { }; double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities); - try(FileWriter fw = new FileWriter("barnsley.dat")) { + try (FileWriter fw = new FileWriter("barnsley.dat")) { for (Point p : outputPoints) { fw.write(p.x + "\t" + p.y + "\n"); } } catch (IOException e) { e.printStackTrace(); } - } }