I was going to use Pow
but it only seems to accept powering by integer values.
For example, the element-wise square root matrix m
of matrix a
.:
a = ⎡ 4 9⎤
⎣16 25⎦
m = ⎡2 3⎤
⎣4 5⎦
For an element-wise square root of a matrix, write something like this:
package main
import (
"fmt"
"math"
"gonum.org/v1/gonum/mat"
)
func main() {
a := mat.NewDense(2, 2, []float64{
4, 9,
16, 25,
})
fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
fmt.Printf("a = %v\n\n", fa)
m := new(mat.Dense)
m.Apply(func(i, j int, v float64) float64 { return math.Sqrt(v) }, a)
fm := mat.Formatted(m, mat.Prefix(" "), mat.Squeeze())
fmt.Printf("m = %v\n\n", fm)
}
Output:
a = ⎡ 4 9⎤
⎣16 25⎦
m = ⎡2 3⎤
⎣4 5⎦