Golang : Linear algebra and matrix calculation example
Continuing from our previous tutorial on how to create matrix with Gonum, we will explore how to perform matrix and linear algebra calculations in this tutorial. Honestly, I found that NumPy
is more elegant in handling matrix in Python, but since we are in Golang, we will stick to what is available.
Here you go!
package main
import (
"fmt"
"github.com/gonum/matrix/mat64"
)
func main() {
// first there was a void and then
// this matrix is created...
// ⎡1 2 3⎤
// ⎢4 5 6⎥
// ⎣7 8 9⎦
row1 := []float64{1, 2, 3}
row2 := []float64{4, 5, 6}
row3 := []float64{7, 8, 9}
m := mat64.NewDense(3, 3, nil)
m.SetRow(0, row1)
m.SetRow(1, row2)
m.SetRow(2, row3)
// print all m elements
fmt.Printf("m :\n%v\n\n", mat64.Formatted(m, mat64.Prefix(""), mat64.Excerpt(0)))
// then followed by the matrix transpose
mT := m.T()
// print all mT elements
fmt.Printf("mT :\n%v\n\n", mat64.Formatted(mT, mat64.Prefix(""), mat64.Excerpt(0)))
// the matrices decided to go forth and multiply...
//mX := m * mT
mX := mat64.NewDense(3, 3, nil) // a new nil matrix of 3 x 3
mX.MulElem(m, mT)
// print all mX elements
fmt.Printf("mX :\n%v\n\n", mat64.Formatted(mX, mat64.Prefix(""), mat64.Excerpt(0)))
// and add another matrix to make another family member...
mA := mat64.NewDense(3, 3, nil)
mA.Add(mX, mT)
// print all mA elements
fmt.Printf("mA :\n%v\n", mat64.Formatted(mA, mat64.Prefix(""), mat64.Excerpt(0)))
fmt.Println(" = ")
fmt.Printf("mX :\n%v\n", mat64.Formatted(mX, mat64.Prefix(""), mat64.Excerpt(0)))
fmt.Println(" + ")
fmt.Printf("mT :\n%v\n\n", mat64.Formatted(mT, mat64.Prefix(""), mat64.Excerpt(0)))
// in order to be more fruitful, first...one must find the determination (determinant)
determinant := mat64.Det(mX)
fmt.Println("Determinant of mX : ", determinant)
// with strong determination (determinant), then linear algebra operations can be solved ....
err := mX.Solve(mX, m)
fmt.Println("Any error? : ", err)
fmt.Printf("Solved mX :\n%v\n", mat64.Formatted(mX, mat64.Prefix(""), mat64.Excerpt(0)))
}
Output:
m :
⎡1 2 3⎤
⎢4 5 6⎥
⎣7 8 9⎦
mT :
⎡1 4 7⎤
⎢2 5 8⎥
⎣3 6 9⎦
mX :
⎡ 1 8 21⎤
⎢ 8 25 48⎥
⎣21 48 81⎦
mA :
⎡ 2 12 28⎤
⎢10 30 56⎥
⎣24 54 90⎦
=
mX :
⎡ 1 8 21⎤
⎢ 8 25 48⎥
⎣21 48 81⎦
+
mT :
⎡1 4 7⎤
⎢2 5 8⎥
⎣3 6 9⎦
Determinant of mX : -360.00000000000006
Any error? :
Solved mX :
⎡ -0.48333333333333306 -0.31666666666666626 -0.1499999999999994⎤
⎢ 0.6666666666666663 0.33333333333333287 -6.614094614788165e-16⎥
⎣ -0.18333333333333318 -0.01666666666666651 0.15000000000000024⎦
References:
https://socketloop.com/tutorials/golang-create-matrix-with-gonum-matrix-package-example
https://godoc.org/github.com/gonum/matrix/mat64#Dense.MulElem
See also : Golang : Create matrix with Gonum Matrix package example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+22.4k Golang : untar or extract tar ball archive example
+39.2k Golang : Remove dashes(or any character) from string
+14.2k Golang : GUI with Qt and OpenCV to capture image from camera
+22k Golang : Read directory content with filepath.Walk()
+16.8k Golang : Covert map/slice/array to JSON or XML format
+51.6k Golang : How to get time in milliseconds?
+8.3k Golang : Another camera capture GUI application with GTK and OpenCV
+7.9k Golang : Randomize letters from a string example
+10.2k Golang : Meaning of omitempty in struct's field tag
+11.4k Golang : Gorilla web tool kit secure cookie example
+19.7k Golang : Reset or rewind io.Reader or io.Writer
+35.8k Golang : How to split or chunking a file to smaller pieces?