- Machine Learning With Go
- Daniel Whitenack
- 505字
- 2021-07-08 10:37:31
Matrix operations
As with vectors, matrices have their own set of rules for arithmetic, along with a whole set of special operations. Some of the arithmetic associated with matrices behaves in a similar way to what you might expect. However, you need to take special care when doing things such as multiplying matrices together or taking an inverse.
Conveniently, gonum.org/v1/gonum/mat provides a nice API for this arithmetic and many other special operations. Here is an example showing a few operations, such as adding, multiplying, dividing, and so on:
// Create two matrices of the same size, a and b. a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6}) b := mat.NewDense(3, 3, []float64{8, 9, 10, 1, 4, 2, 9, 0, 2}) // Create a third matrix of a different size. c := mat.NewDense(3, 2, []float64{3, 2, 1, 4, 0, 8}) // Add a and b. d := mat.NewDense(0, 0, nil) d.Add(a, b) fd := mat.Formatted(d, mat.Prefix(" ")) fmt.Printf("d = a + b = %0.4v\n\n", fd) // Multiply a and c. f := mat.NewDense(0, 0, nil) f.Mul(a, c) ff := mat.Formatted(f, mat.Prefix(" ")) fmt.Printf("f = a c = %0.4v\n\n", ff) // Raising a matrix to a power. g := mat.NewDense(0, 0, nil) g.Pow(a, 5) fg := mat.Formatted(g, mat.Prefix(" ")) fmt.Printf("g = a^5 = %0.4v\n\n", fg) // Apply a function to each of the elements of a. h := mat.NewDense(0, 0, nil) sqrt := func(_, _ int, v float64) float64 { return math.Sqrt(v) } h.Apply(sqrt, a) fh := mat.Formatted(h, mat.Prefix(" ")) fmt.Printf("h = sqrt(a) = %0.4v\n\n", fh)
In particular, note the Apply() method above. This functionality is extremely useful as it allow you to apply any function to the elements of a matrix. You can apply the same function to all elements or make the function dependent on the indices of the matrix elements. For example, you could you this method to perform element-wise multiplications, applications of user defined functions, or applications of functions from third party packages.
Then, for all the various things, such as determinants, eigenvalue/vector solvers, and inverses, gonum.org/v1/gonum/mat has you covered. Again, I won't expand on all of the functionality, but here is a sample of some of the operations:
// Create a new matrix a.
a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
// Compute and output the transpose of the matrix.
ft := mat.Formatted(a.T(), mat.Prefix(" "))
fmt.Printf("a^T = %v\n\n", ft)
// Compute and output the determinant of a.
deta := mat.Det(a)
fmt.Printf("det(a) = %.2f\n\n", deta)
// Compute and output the inverse of a.
aInverse := mat.NewDense(0, 0, nil)
if err := aInverse.Inverse(a); err != nil {
log.Fatal(err)
}
fi := mat.Formatted(aInverse, mat.Prefix(" "))
fmt.Printf("a^-1 = %v\n\n", fi)
Note that in this example, we leverage Go's explicit error handling functionality when we need to ensure that we are maintaining integrity and readability. Matrices don't always have inverses. There are various situations like this that arise when working with matrices and large datasets, and we want to ensure that our application behaves as expected.