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
+7.5k Golang : Check if one string(rune) is permutation of another string(rune)
+16k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+9.6k Golang : Scramble and unscramble text message by randomly replacing words
+9.7k Golang : Accessing content anonymously with Tor
+6.9k Golang : Check if password length meet the requirement
+7.4k Golang : alternative to os.Exit() function
+9.6k Golang : Qt Yes No and Quit message box example
+4.8k Mac OSX : Get disk partitions' size, type and name
+12.1k Golang : GTK Input dialog box examples
+83k Golang : How to return HTTP status code?
+4.8k Chrome : How to block socketloop.com links in Google SERP?
+32.3k Golang : Convert an image file to []byte