Skip to content

Commit f5885a6

Browse files
Julianzsparal
Julian
authored andcommitted
add the thomas algorithm in go (#405)
* add the thomas algorithm in go * change the name golang to go
1 parent 9b40580 commit f5885a6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func thomas(a, b, c, d []float64) []float64 {
6+
c[0] = c[0] / b[0]
7+
d[0] = d[0] / b[0]
8+
9+
for i := 1; i < len(d); i++ {
10+
scale := 1. / (b[i] - c[i-1]*a[i])
11+
c[i] *= scale
12+
d[i] = (d[i] - a[i]*d[i-1]) * scale
13+
}
14+
15+
for i := len(d) - 2; i >= 0; i-- {
16+
d[i] -= c[i] * d[i+1]
17+
}
18+
19+
return d
20+
}
21+
22+
func main() {
23+
a := []float64{0., 2., 3.}
24+
b := []float64{1., 3., 6.}
25+
c := []float64{4., 5., 0.}
26+
d := []float64{7., 5., 3.}
27+
28+
fmt.Println("The system,")
29+
fmt.Println("[1.0 4.0 0.0][x] = [7.0]")
30+
fmt.Println("[2.0 3.0 5.0][y] = [5.0]")
31+
fmt.Println("[0.0 3.0 6.0][z] = [3.0]")
32+
fmt.Println("has the solution:")
33+
solve := thomas(a, b, c, d)
34+
for _, i := range solve {
35+
fmt.Printf("[%f]\n", i)
36+
}
37+
}

contents/thomas_algorithm/thomas_algorithm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
111111
[import, lang:"java"](code/java/thomas.java)
112112
{% sample lang="hs" %}
113113
[import, lang:"haskell"](code/haskell/thomas.hs)
114+
{% sample lang="go" %}
115+
[import, lang:"go"](code/golang/thomas.go)
114116
{% sample lang="swift" %}
115117
[import, lang:"swift"](code/swift/thomas.swift)
116118
{% sample lang="php" %}

0 commit comments

Comments
 (0)