diff --git a/contents/thomas_algorithm/code/java/Thomas.java b/contents/thomas_algorithm/code/java/Thomas.java new file mode 100644 index 000000000..c351cad3b --- /dev/null +++ b/contents/thomas_algorithm/code/java/Thomas.java @@ -0,0 +1,43 @@ +public class Thomas { + 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]; + + // 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]; + } + + return solution; + } + + 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); + + 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 < solution.length; i++) { + System.out.format("[% .5f]\n", solution[i]); + } + } +} diff --git a/contents/thomas_algorithm/code/java/thomas.java b/contents/thomas_algorithm/code/java/thomas.java deleted file mode 100644 index b7c630853..000000000 --- a/contents/thomas_algorithm/code/java/thomas.java +++ /dev/null @@ -1,37 +0,0 @@ -public class thomas { - private static void thomasAlgorithm(double a[], double b[], double c[], double x[], int size) { - - double y[] = new double[size]; - - y[0] = c[0] / b[0]; - x[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; - } - - for (int i = size - 2; i >= 0; --i) { - x[i] -= y[i] * x[i + 1]; - } - } - - 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}; - - 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"); - - thomasAlgorithm(a, b, c, x, 3); - - for (int i = 0; i < 3; ++i) - System.out.println("[" + x[i] + "]\n"); - } -} 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" %}