From 0076a65a95281a7d25c7f25c2671fd19d83dc3ea Mon Sep 17 00:00:00 2001 From: Sklan Date: Tue, 30 Oct 2018 08:14:43 +0530 Subject: [PATCH 1/3] Create thomas.kt --- .../thomas_algorithm/code/kotlin/thomas.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 contents/thomas_algorithm/code/kotlin/thomas.kt diff --git a/contents/thomas_algorithm/code/kotlin/thomas.kt b/contents/thomas_algorithm/code/kotlin/thomas.kt new file mode 100644 index 000000000..e1ca741c2 --- /dev/null +++ b/contents/thomas_algorithm/code/kotlin/thomas.kt @@ -0,0 +1,34 @@ +private fun thomas(a: DoubleArray, b: DoubleArray, c: DoubleArray, d: DoubleArray): DoubleArray { + val cPrime = c.clone() + val x = d.clone() + val size = a.size + cPrime[0] /= b[0] + x[0] /= b[0] + for (i in 1 until size) { + val scale = 1.0 / (b[i] - cPrime[i - 1] * a[i]) + cPrime[i] *= scale + x[i] = (x[i] - a[i] * x[i - 1]) * scale + } + for (i in (size - 2) downTo 0) { + x[i] -= cPrime[i] * x[i + 1] + } + return x +} + +fun main(args: Array) { + val a = doubleArrayOf(0.0, 2.0, 3.0) + val b = doubleArrayOf(1.0, 3.0, 6.0) + val c = doubleArrayOf(4.0, 5.0, 0.0) + val x = doubleArrayOf(7.0, 5.0, 3.0) + val solution = thomas(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 (i in solution.indices) { + System.out.format("[% .5f]\n", solution[i]) + } +} From b5d81091828757326668c343a878f99e6d809c54 Mon Sep 17 00:00:00 2001 From: Sklan Date: Tue, 30 Oct 2018 08:16:46 +0530 Subject: [PATCH 2/3] Update thomas_algorithm.md --- contents/thomas_algorithm/thomas_algorithm.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/thomas_algorithm/thomas_algorithm.md b/contents/thomas_algorithm/thomas_algorithm.md index e7ed2c8ef..b1102336d 100644 --- a/contents/thomas_algorithm/thomas_algorithm.md +++ b/contents/thomas_algorithm/thomas_algorithm.md @@ -125,6 +125,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e [import, lang:"lua"](code/lua/thomas.lua) {% sample lang="crystal" %} [import, lang:"crystal"](code/crystal/thomas.cr) +{% sample lang="kotlin" %} +[import, lang:"kotlin"](code/kotlin/thomas.kt) {% endmethod %}