Skip to content

Added implementation of Gaussian Elimination in Go #552

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
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -39,4 +39,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- nic-hartley
- crafter312
- Christopher Milan
- Vexatos
- Vexatos
- Raven-Blue Dragon
135 changes: 135 additions & 0 deletions contents/gaussian_elimination/code/go/gaussian_elimination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Package demonstrates Gaussian Elimination
package main

import (
"fmt"
"math"
)

func gaussianElimination(a [][]float64) {
singular := false
rows := len(a)
cols := len(a[0])

for c, r := 0, 0; c < cols && r < rows; c++ {
// 1. find highest value in column below row to be pivot
p, highest := r, 0.
for i, row := range a[r:] {
if abs := math.Abs(row[c]); abs > highest {
p = r + i
highest = abs
}
}
highest = a[p][c] // correct sign

if highest == 0. {
if !singular {
singular = true
fmt.Println("This matrix is singular.")
}
continue
}

// 2. swap pivot with current row
if p != r {
a[r], a[p] = a[p], a[r]
}

for _, row := range a[r+1:] {
// 3. find fraction from pivot value
frac := row[c] / highest

// 4. subtract row to set rest of column to zero
for j := range row {
row[j] -= frac * a[r][j]
}

// 5. ensure col goes to zero (no float rounding)
row[c] = 0.
}

r++
}
}

func gaussJordan(a [][]float64) {
for r := len(a) - 1; r >= 0; r-- {
// Find pivot col
p := -1
for c, cell := range a[r] {
if cell != 0. {
p = c
break
}
}
if p < 0 {
continue
}

// Scale pivot r to 1.
scale := a[r][p]
for c := range a[r][p:] {
a[r][p+c] /= scale
}
// Subtract pivot row from each row above
for _, row := range a[:r] {
scale = row[p]
for c, cell := range a[r][p:] {
row[p+c] -= cell * scale
}
}
}
}

func backSubstitution(a [][]float64) []float64 {
rows := len(a)
cols := len(a[0])
x := make([]float64, rows)
for r := rows - 1; r >= 0; r-- {
sum := 0.

for c := cols - 2; c > r; c-- {
sum += x[c] * a[r][c]
}

x[r] = (a[r][cols-1] - sum) / a[r][r]
}
return x
}

func printMatrixRow(row []float64) {
fmt.Print("[")
for _, cell := range row {
fmt.Printf("%9.4f ", cell)
}
fmt.Println("]")
}

func printMatrix(a [][]float64) {
for _, row := range a {
printMatrixRow(row)
}
fmt.Println()
}

func main() {
a := [][]float64{
{2, 3, 4, 6},
{1, 2, 3, 4},
{3, -4, 0, 10},
}
fmt.Println("Original Matrix:")
printMatrix(a)

fmt.Println("Gaussian elimination:")
gaussianElimination(a)
printMatrix(a)

gaussJordan(a)
fmt.Println("Gauss-Jordan:")
printMatrix(a)

fmt.Println("Solutions are:")
x := backSubstitution(a)
printMatrixRow(x)
}
8 changes: 8 additions & 0 deletions contents/gaussian_elimination/gaussian_elimination.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ In code, this looks like:
[import:5-47, lang:"java"](code/java/GaussianElimination.java)
{% sample lang="js" %}
[import:1-38, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% sample lang="go" %}
[import:9-53, lang:"go"](code/go/gaussian_elimination.go)
{% endmethod %}

Now, to be clear: this algorithm creates an upper-triangular matrix.
Expand Down Expand Up @@ -405,6 +407,8 @@ This code does not exist yet in rust, so here's Julia code (sorry for the inconv
[import:49-70, lang:"java"](code/java/GaussianElimination.java)
{% sample lang="js" %}
[import:57-76, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% sample lang="go" %}
[import:55-82, lang:"go"](code/go/gaussian_elimination.go)
{% endmethod %}

## Back-substitution
Expand Down Expand Up @@ -441,6 +445,8 @@ In code, this involves keeping a rolling sum of all the values we substitute in
[import:72-87, lang:"java"](code/java/GaussianElimination.java)
{% sample lang="js" %}
[import:40-55, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% sample lang="go" %}
[import:84-98, lang:"go"](code/go/gaussian_elimination.go)
{% endmethod %}

## Conclusions
Expand Down Expand Up @@ -469,6 +475,8 @@ As for what's next... Well, we are in for a treat! The above algorithm clearly h
[import, lang:"java"](code/java/GaussianElimination.java)
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% sample lang="go" %}
[import, lang:"go"](code/go/gaussian_elimination.go)
{% endmethod %}


Expand Down