From 6e66fad2a8bd8c91b2075f379c1d5147da5aba50 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 00:11:17 +0200 Subject: [PATCH 1/7] Rename thomas.java to Thomas.java Classes in Java are usually PascalCase and the file names have to match --- .../thomas_algorithm/code/java/{thomas.java => Thomas.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename contents/thomas_algorithm/code/java/{thomas.java => Thomas.java} (98%) diff --git a/contents/thomas_algorithm/code/java/thomas.java b/contents/thomas_algorithm/code/java/Thomas.java similarity index 98% rename from contents/thomas_algorithm/code/java/thomas.java rename to contents/thomas_algorithm/code/java/Thomas.java index b7c630853..25483dc7f 100644 --- a/contents/thomas_algorithm/code/java/thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -1,4 +1,4 @@ -public class thomas { +public class Thomas { private static void thomasAlgorithm(double a[], double b[], double c[], double x[], int size) { double y[] = new double[size]; From 40bb84b3571c51ee9c0b11fc16d0781ade1be9c5 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 00:25:00 +0200 Subject: [PATCH 2/7] Rewrite Java Thomas so it returns the solution --- contents/thomas_algorithm/code/java/Thomas.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java index 25483dc7f..55bda6eac 100644 --- a/contents/thomas_algorithm/code/java/Thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -1,20 +1,23 @@ public class Thomas { - private static void thomasAlgorithm(double a[], double b[], double c[], double x[], int size) { - + private static double[] thomasAlgorithm(double a[], double b[], double c[], double x[]) { + int size = a.length; double y[] = new double[size]; + double solution[] = new double[size]; y[0] = c[0] / b[0]; - x[0] = x[0] / b[0]; + solution[0] = x[0] / b[0]; for (int i = 1; i < size; ++i) { double scale = 1.0 / (b[i] - a[i] * y[i - 1]); y[i] = c[i] * scale; - x[i] = (x[i] - a[i] * x[i - 1]) * scale; + solution[i] = (x[i] - a[i] * solution[i - 1]) * scale; } for (int i = size - 2; i >= 0; --i) { - x[i] -= y[i] * x[i + 1]; + solution[i] -= y[i] * solution[i + 1]; } + + return solution; } public static void main(String[] args) { @@ -29,9 +32,9 @@ public static void main(String[] args) { System.out.println("[0.0 3.0 6.0][z] = [3.0]\n"); System.out.println("has the solution:\n"); - thomasAlgorithm(a, b, c, x, 3); + double solution[] = thomasAlgorithm(a, b, c, x); for (int i = 0; i < 3; ++i) - System.out.println("[" + x[i] + "]\n"); + System.out.println("[" + solution[i] + "]\n"); } } From ecd5a1be8aa9fcd90673f41127dce031d0a13502 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 00:36:50 +0200 Subject: [PATCH 3/7] Clean up output - There were lots of unnecessary newlines - Some of the output was hardcoded --- .../thomas_algorithm/code/java/Thomas.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java index 55bda6eac..c30244c14 100644 --- a/contents/thomas_algorithm/code/java/Thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -25,16 +25,16 @@ public static void main(String[] args) { double b[] = {1.0, 3.0, 6.0}; double c[] = {4.0, 5.0, 0.0}; double x[] = {7.0, 5.0, 3.0}; - - System.out.println("The system,\n"); - System.out.println("[1.0 4.0 0.0][x] = [7.0]\n"); - System.out.println("[2.0 3.0 5.0][y] = [5.0]\n"); - System.out.println("[0.0 3.0 6.0][z] = [3.0]\n"); - System.out.println("has the solution:\n"); - double solution[] = thomasAlgorithm(a, b, c, x); - for (int i = 0; i < 3; ++i) - System.out.println("[" + solution[i] + "]\n"); + System.out.format("The system,\n"); + System.out.format("[%.1f, %.1f, %.1f][x] = [%.1f]\n", b[0], c[0], 0f, x[0]); + System.out.format("[%.1f, %.1f, %.1f][y] = [%.1f]\n", a[1], b[1], c[1], x[1]); + System.out.format("[%.1f, %.1f, %.1f][z] = [%.1f]\n", 0f, a[2], b[2], x[2]); + System.out.format("has the solution:\n"); + + for (int i = 0; i < 3; ++i) { + System.out.format("[% .5f]\n", solution[i]); + } } } From 06acece4f6e8469861c9d098ece586e8c9e2bd51 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 00:38:52 +0200 Subject: [PATCH 4/7] Add a few comments to the code --- contents/thomas_algorithm/code/java/Thomas.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java index c30244c14..71e8ea677 100644 --- a/contents/thomas_algorithm/code/java/Thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -1,18 +1,21 @@ public class Thomas { private static double[] thomasAlgorithm(double a[], double b[], double c[], double x[]) { int size = a.length; - double y[] = new double[size]; + double y[] = new double[size]; // This is needed so that we don't have to modify c double solution[] = new double[size]; + // Set initial elements y[0] = c[0] / b[0]; solution[0] = x[0] / b[0]; for (int i = 1; i < size; ++i) { + // Scale factor is for c and x double scale = 1.0 / (b[i] - a[i] * y[i - 1]); y[i] = c[i] * scale; solution[i] = (x[i] - a[i] * solution[i - 1]) * scale; } + // Back-substitution for (int i = size - 2; i >= 0; --i) { solution[i] -= y[i] * solution[i + 1]; } From de85dcf4ecbe70f9d0d7e6a9b0409b5d43a185ef Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 00:52:30 +0200 Subject: [PATCH 5/7] Changed all double x[] to double[] x --- contents/thomas_algorithm/code/java/Thomas.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java index 71e8ea677..632fcce63 100644 --- a/contents/thomas_algorithm/code/java/Thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -1,8 +1,8 @@ public class Thomas { - private static double[] thomasAlgorithm(double a[], double b[], double c[], double x[]) { + private static double[] thomasAlgorithm(double[] a, double[] b, double[] c, double[] x) { int size = a.length; - double y[] = new double[size]; // This is needed so that we don't have to modify c - double solution[] = new double[size]; + double[] y = new double[size]; // This is needed so that we don't have to modify c + double[] solution = new double[size]; // Set initial elements y[0] = c[0] / b[0]; @@ -24,11 +24,11 @@ private static double[] thomasAlgorithm(double a[], double b[], double c[], doub } public static void main(String[] args) { - double a[] = {0.0, 2.0, 3.0}; - double b[] = {1.0, 3.0, 6.0}; - double c[] = {4.0, 5.0, 0.0}; - double x[] = {7.0, 5.0, 3.0}; - double solution[] = thomasAlgorithm(a, b, c, x); + double[] a = {0.0, 2.0, 3.0}; + double[] b = {1.0, 3.0, 6.0}; + double[] c = {4.0, 5.0, 0.0}; + double[] x = {7.0, 5.0, 3.0}; + double[] solution = thomasAlgorithm(a, b, c, x); System.out.format("The system,\n"); System.out.format("[%.1f, %.1f, %.1f][x] = [%.1f]\n", b[0], c[0], 0f, x[0]); From cf207118db4dedfda7f88b26a2b2113e344f1a7c Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 03:11:31 +0200 Subject: [PATCH 6/7] Removed hard-coded array length in for-loop --- contents/thomas_algorithm/code/java/Thomas.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java index 632fcce63..c351cad3b 100644 --- a/contents/thomas_algorithm/code/java/Thomas.java +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -36,7 +36,7 @@ public static void main(String[] args) { System.out.format("[%.1f, %.1f, %.1f][z] = [%.1f]\n", 0f, a[2], b[2], x[2]); System.out.format("has the solution:\n"); - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < solution.length; i++) { System.out.format("[% .5f]\n", solution[i]); } } From f1ffb5b358949bab97cfdefe828c466c827facd3 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Fri, 12 Oct 2018 03:16:56 +0200 Subject: [PATCH 7/7] Fixed code import in thomas_algorithm.md --- contents/thomas_algorithm/thomas_algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/thomas_algorithm/thomas_algorithm.md b/contents/thomas_algorithm/thomas_algorithm.md index de0c690b1..314dbd69b 100644 --- a/contents/thomas_algorithm/thomas_algorithm.md +++ b/contents/thomas_algorithm/thomas_algorithm.md @@ -108,7 +108,7 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e

{% sample lang="java" %} -[import, lang:"java"](code/java/thomas.java) +[import, lang:"java"](code/java/Thomas.java) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/thomas.hs) {% sample lang="go" %}