Skip to content

Commit f3f28b9

Browse files
Vexatosleios
authored andcommitted
Implemented Thomas Algorithm in Lua (#522)
1 parent 6f386fa commit f3f28b9

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3838
- PaddyKe
3939
- nic-hartley
4040
- crafter312
41-
- Christopher Milan
41+
- Christopher Milan
42+
- Vexatos
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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(b[1], c[1], "", "|", d[1])
29+
print(a[2], b[2], c[2], "|", d[2])
30+
print("", a[3], b[3], "|", d[3])
31+
print("Has the solution:")
32+
33+
local solution = thomas(a, b, c, d)
34+
35+
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)