Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions contents/thomas_algorithm/code/javascript/thomas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function thomas(a, b, c, x) {
const result = [];
const y = [];

y[0] = c[0] / b[0];
result[0] = x[0] / b[0];

for (let i = 1; i < a.length; i++) {
const scale = 1 / (b[i] - a[i] * y[i - 1]);
y[i] = c[i] * scale;
result[i] = (x[i] - a[i] * result[i - 1]) * scale;
}

for (let i = a.length - 2; i >= 0; i--)
result[i] -= y[i] * result[i + 1];

return result;
}

let a = [0, 2, 3];
let b = [1, 3, 6];
let c = [4, 5, 0];
let x = [7, 5, 3];

console.log("The system,");
console.log("[1.0 4.0 0.0][x] = [7.0]");
console.log("[2.0 3.0 5.0][y] = [5.0]");
console.log("[0.0 3.0 6.0][z] = [3.0]");
console.log("has the solution:\n", thomas(a, b, c, x));
2 changes: 2 additions & 0 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"nim"](code/nim/thomas_algorithm.nim)
{% sample lang="cpp" %}
[import, lang:"c_cpp"](code/c++/thomas.cpp)
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/thomas.js)
{% endmethod %}

<script>
Expand Down