Skip to content
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
38 changes: 38 additions & 0 deletions contents/thomas_algorithm/code/ruby/thomas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# note this example is inplace and destructive
def thomas(a, b, c, d)
# set the initial elements
c[0] = c[0] / b[0]
d[0] = d[0] / b[0]

n = d.length # number of equations to solve
(1...n).each do |i|
scale = 1 / (b[i] - c[i - 1] * a[i]) # scale factor for c and d
c[i] *= scale
d[i] = (d[i] - a[i] * d[i - 1]) * scale
end

# do the back substitution
(n - 2).downto(0).each do |j|
d[j] -= c[j] * d[j + 1]
end

d
end

# example for matrix
# [1 4 0][x] [7]
# [2 3 5][y] = [5]
# [0 3 6][z] [3]

# [.8666]
# soln will equal [1.533]
# [-.266]
# note we index a from 1 and c from 0

a = [0.0, 2.0, 3.0]
b = [1.0, 3.0, 6.0]
c = [4.0, 5.0, 0.0]
d = [7.0, 5.0, 3.0]

soln = thomas(a, b, c, d)
puts soln
2 changes: 2 additions & 0 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"crystal"](code/crystal/thomas.cr)
{% sample lang="kotlin" %}
[import, lang:"kotlin"](code/kotlin/thomas.kt)
{% sample lang="ruby" %}
[import, lang="ruby"](code/ruby/thomas.rb)
{% endmethod %}

<script>
Expand Down