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 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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- crafter312
- Christopher Milan
- Vexatos
- Raven-Blue Dragon
- Björn Heinrichs
- Olav Sundfør
- dovisutu
Expand Down
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 @@ -419,6 +419,8 @@ When we put everything together, it looks like this:
[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 %}

To be clear: if the matrix is found to be singular during this process, the system of equations is either over- or under-determined and no general solution exists.
Expand Down Expand Up @@ -459,6 +461,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 %}

As a note: Gauss-Jordan elimination can also be used to find the inverse of a matrix by following the same procedure to generate a reduced row echelon matrix, but with an identity matrix on the other side instead of the right-hand side of each equation.
Expand Down Expand Up @@ -501,6 +505,8 @@ In code, it looks like this:
[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 %}

## Visual Representation
Expand Down Expand Up @@ -569,6 +575,8 @@ Here's a video describing Gaussian elimination:
[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