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