Skip to content

Commit 9dcaf93

Browse files
committed
Added thomas algo in js
1 parent e8fdf02 commit 9dcaf93

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function thomas(a, b, c, d) {
2+
const size = a.length;
3+
4+
c[0] /= b[0];
5+
d[0] /= b[0];
6+
7+
for(let i = 1; i < size; ++i ) {
8+
let scale = 1 / (b[i] - c[i-1] * a[i]);
9+
c[i] *= scale;
10+
d[i] = (d[i] - a[i] * d[i-1]) * scale;
11+
}
12+
13+
for(let i = size-2; i>=0; --i) {
14+
d[i] -= c[i] * d[i+1];
15+
}
16+
17+
return d;
18+
}
19+
20+
a = [0, 2, 3];
21+
b = [1, 3, 6];
22+
c = [4, 5, 0];
23+
d = [7, 5, 3];
24+
25+
sol = thomas(a,b,c,d);
26+
console.log(sol);

contents/thomas_algorithm/thomas_algorithm.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Thomas Algorithm
1+
s# Thomas Algorithm
22

33
As alluded to in the [Gaussian Elimination chapter](../gaussian_elimination/gaussian_elimination.md), the Thomas Algorithm (or TDMA, Tri-Diagonal Matrix Algorithm) allows for programmers to **massively** cut the computational cost of their code from $$ O(n^3)$$ to $$O(n)$$ in certain cases!
44
This is done by exploiting a particular case of Gaussian Elimination where the matrix looks like this:
@@ -111,8 +111,6 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
111111
[import, lang:"java"](code/java/thomas.java)
112112
{% sample lang="hs" %}
113113
[import, lang:"haskell"](code/haskell/thomas.hs)
114-
{% sample lang="go" %}
115-
[import, lang:"go"](code/golang/thomas.go)
116114
{% sample lang="swift" %}
117115
[import, lang:"swift"](code/swift/thomas.swift)
118116
{% sample lang="php" %}
@@ -121,6 +119,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
121119
[import, lang:"nim"](code/nim/thomas_algorithm.nim)
122120
{% sample lang="cpp" %}
123121
[import, lang:"c_cpp"](code/c++/thomas.cpp)
122+
{% sample lang="js" %}
123+
[import, lang:"js"](code/javascript/thomas.js)
124124
{% endmethod %}
125125

126126
<script>

0 commit comments

Comments
 (0)