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
+11.6k Golang : Change date format to yyyy-mm-dd
+5.3k PHP : See installed compiled-in-modules
+5.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+8.3k Golang : HttpRouter multiplexer routing example
+5.4k Golang : Get FX sentiment from website example
+11k Golang : Removes punctuation or defined delimiter from the user's input
+6.4k Golang : How to search a list of records or data structures
+11.8k Golang : Secure file deletion with wipe example
+10.7k Golang : Allow Cross-Origin Resource Sharing request
+36.4k Golang : Convert(cast) int64 to string
+12.9k Golang : http.Get example
+17.8k Golang : delete and modify XML file content