Skip to content

Commit 7c86fb9

Browse files
committed
Implemented Thomas Algorithm in Lua
1 parent b5c438b commit 7c86fb9

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
- PaddyKe
3939
- nic-hartley
4040
- crafter312
41+
- Vexatos
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function thomas(a, b, c, d)
2+
3+
-- Create tables and set initial elements
4+
local c_prime = {c[1] / b[1]}
5+
local result = {d[1] / b[1]}
6+
7+
for i = 2, #a do
8+
-- Scale factor is for c_prime and result
9+
local scale = 1.0 / (b[i] - a[i] * c_prime[i - 1])
10+
c_prime[i] = c[i] * scale
11+
result[i] = (d[i] - a[i] * result[i - 1]) * scale
12+
end
13+
14+
-- Back-substitution
15+
for i = #a-1, 1, -1 do
16+
result[i] = result[i] - (c_prime[i] * result [i + 1])
17+
end
18+
19+
return result
20+
end
21+
22+
local a = {0.0, 2.0, 3.0}
23+
local b = {1.0, 3.0, 6.0}
24+
local c = {4.0, 5.0, 0.0}
25+
local d = {7.0, 5.0, 3.0}
26+
27+
print("The system")
28+
print(table.unpack(a))
29+
print(table.unpack(b))
30+
print(table.unpack(c))
31+
print(table.unpack(d))
32+
print("Has the solution:")
33+
34+
local solution = thomas(a, b, c, d)
35+
36+
print(table.unpack(solution))

contents/thomas_algorithm/thomas_algorithm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
121121
[import, lang:"nim"](code/nim/thomas_algorithm.nim)
122122
{% sample lang="cpp" %}
123123
[import, lang:"c_cpp"](code/c++/thomas.cpp)
124+
{% sample lang="lua" %}
125+
[import, lang:"lua"](code/lua/thomas.lua)
124126
{% endmethod %}
125127

126128
<script>

0 commit comments

Comments
 (0)