Skip to content

Implemented Thomas Algorithm in Lua #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2018
Merged
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
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- PaddyKe
- nic-hartley
- crafter312
- Christopher Milan
- Christopher Milan
- Vexatos
35 changes: 35 additions & 0 deletions contents/thomas_algorithm/code/lua/thomas.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function thomas(a, b, c, d)

-- Create tables and set initial elements
local c_prime = {c[1] / b[1]}
local result = {d[1] / b[1]}

for i = 2, #a do
-- Scale factor is for c_prime and result
local scale = 1.0 / (b[i] - a[i] * c_prime[i - 1])
c_prime[i] = c[i] * scale
result[i] = (d[i] - a[i] * result[i - 1]) * scale
end

-- Back-substitution
for i = #a-1, 1, -1 do
result[i] = result[i] - (c_prime[i] * result [i + 1])
end

return result
end

local a = {0.0, 2.0, 3.0}
local b = {1.0, 3.0, 6.0}
local c = {4.0, 5.0, 0.0}
local d = {7.0, 5.0, 3.0}

print("The system")
print(b[1], c[1], "", "|", d[1])
print(a[2], b[2], c[2], "|", d[2])
print("", a[3], b[3], "|", d[3])
print("Has the solution:")

local solution = thomas(a, b, c, d)

print(table.unpack(solution))
2 changes: 2 additions & 0 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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="lua" %}
[import, lang:"lua"](code/lua/thomas.lua)
{% endmethod %}

<script>
Expand Down