Skip to content

Commit 88a7963

Browse files
Julianberquist
authored andcommitted
1 parent afe41a1 commit 88a7963

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fn thomas(a []f32, b []f32, c []f32, d []f32) []f32 {
2+
mut new_c := c
3+
mut new_d := d
4+
new_c[0] = new_c[0] / b[0]
5+
new_d[0] = new_d[0] / b[0]
6+
7+
for i := 1; i < d.len; i++ {
8+
scale := 1. / (b[i] - new_c[i-1]*a[i])
9+
new_c[i] *= scale
10+
new_d[i] = (new_d[i] - a[i]*new_d[i-1]) * scale
11+
}
12+
13+
for i := d.len - 2; i >= 0; i-- {
14+
new_d[i] -= new_c[i] * new_d[i+1]
15+
}
16+
17+
return new_d
18+
}
19+
20+
fn main() {
21+
a := [0.0, 2.0, 3.0]
22+
b := [1.0, 3.0, 6.0]
23+
c := [4.0, 5.0, 0.0]
24+
d := [7.0, 5.0, 3.0]
25+
26+
println("The system,")
27+
println("[1.0 4.0 0.0][x] = [7.0]")
28+
println("[2.0 3.0 5.0][y] = [5.0]")
29+
println("[0.0 3.0 6.0][z] = [3.0]")
30+
println("has the solution:")
31+
solution := thomas(a, b, c, d)
32+
for i in solution {
33+
println("[$i]")
34+
}
35+
}

contents/thomas_algorithm/thomas_algorithm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
113113
[import, lang:"haskell"](code/haskell/thomas.hs)
114114
{% sample lang="go" %}
115115
[import, lang:"go"](code/golang/thomas.go)
116+
{% sample lang="v" %}
117+
[import, lang:"v"](code/v/thomas.v)
116118
{% sample lang="swift" %}
117119
[import, lang:"swift"](code/swift/thomas.swift)
118120
{% sample lang="php" %}

0 commit comments

Comments
 (0)