diff --git a/contents/thomas_algorithm/code/nim/thomas_algorithm.nim b/contents/thomas_algorithm/code/nim/thomas_algorithm.nim new file mode 100644 index 000000000..717948fd9 --- /dev/null +++ b/contents/thomas_algorithm/code/nim/thomas_algorithm.nim @@ -0,0 +1,39 @@ +proc thomas_algorithm(a, b, c_in, d_in: seq[float]): seq[float] = + + let n: int = len(d_in) + + var c: seq[float] = c_in + var d: seq[float] = d_in + + c[0] /= b[0] + d[0] /= b[0] + + for i in 1..n - 1: + let scale: float = (1 / (b[i] - c[i - 1] * a[i])) + + c[i] *= scale + d[i] = (d[i] - a[i] * d[i - 1]) * scale + + for i in countdown(n - 2,0): + d[i] -= c[i] * d[i + 1] + + + return d + + +const x: seq[float] = @[0.0, 2.0, 3.0] +const y: seq[float] = @[1.0, 3.0, 6.0] +const z: seq[float] = @[4.0, 5.0, 0.0] +const w: seq[float] = @[7.0, 5.0, 3.0] + +echo "The system," +echo "[1.0 4.0 0.0][x] = [7.0]" +echo "[2.0 3.0 5.0][y] = [5.0]" +echo "[0.0 3.0 6.0][z] = [3.0]" + +echo "has the solution:" + +const soln: seq[float] = thomas_algorithm(x, y, z, w) + +for i in 0..len(w) - 1: + echo soln[i] diff --git a/contents/thomas_algorithm/thomas_algorithm.md b/contents/thomas_algorithm/thomas_algorithm.md index e2af37a68..ab934eb5a 100644 --- a/contents/thomas_algorithm/thomas_algorithm.md +++ b/contents/thomas_algorithm/thomas_algorithm.md @@ -113,6 +113,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e [import, lang:"haskell"](code/haskell/thomas.hs) {% sample lang="swift" %} [import, lang:"swift"](code/swift/thomas.swift) +{%sample lang="nim" %} +[import, lang:"nim"](code/nim/thomas_algorithm.nim) {% sample lang="cpp" %} [import, lang:"c_cpp"](code/c++/thomas.cpp) {% endmethod %}