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
+18k Golang : For loop continue,break and range
+3.8k Cash Flow : 50 days to pay your credit card debt
+21k Golang : Create PDF file from HTML file
+9.7k Golang : Convert int(year) to time.Time type
+6.5k Useful methods to access blocked websites
+6k Golang : How to extract video or image files from html source code
+9.3k Golang : Concurrency and goroutine example
+9.7k Golang : 2 dimensional array example
+11k Golang : How to get URL port?
+4k Javascript : How to refresh page with JQuery ?
+5.1k Javascript : Push notifications to browser with Push.js