Вот кусок кода:
package main
import (
"fmt"
"gonum.org/v1/gonum/mat"
)
func main() {
// Matrix and Vector
// Initialize a Matrix A
row1 := []float64{1,2,3}
row2 := []float64{4,5,6}
row3 := []float64{7,8,9}
row4 := []float64{10,11,12}
A := mat.NewDense(4,3,nil)
A.SetRow(0, row1)
A.SetRow(1, row2)
A.SetRow(2, row3)
A.SetRow(3, row4)
fmt.Printf("A :\n%v\n\n", mat.Formatted(A, mat.Prefix(""), mat.Excerpt(0)))
// Initialize a Vector v
v := mat.NewDense(3,1, []float64{1,2,3})
fmt.Printf("v :\n%v\n\n", mat.Formatted(v, mat.Prefix(""), mat.Excerpt(0)))
//Get the dimension of the matrix A where m = rows and n = cols
row, col := len(A)
// row, col := size(A)
fmt.Println("row: ", row)
fmt.Println("col: ", col)
}
Error:
invalid argument A (type *mat.Dense) for len
Когда я использую size для определения размеров матрицы A. Потом выдает ошибку undefined: size.
Как я могу получить размеры матрицы A?

len используется для встроенных типов, таких как массив, срез и т. д.
В документе пакета вы должны использовать Dims() для доступа к размеру строки и размеру столбца.
попробуйте godoc gonum.org/v1/gonum/mat и найдите следующие разделы:
func (m *Dense) Dims() (r, c int)
Dims returns the number of rows and columns in the matrix.
Вы можете использовать метод Dims(), чтобы получить количество строк и столбцов в матрице.
import "gonum.org/v1/gonum/mat"func (m *Dense) Dims() (r, c int)Dims returns the number of rows and columns in the matrix.
Например,
package main
import (
"fmt"
"gonum.org/v1/gonum/mat"
)
func main() {
row1 := []float64{1, 2, 3}
row2 := []float64{4, 5, 6}
row3 := []float64{7, 8, 9}
row4 := []float64{10, 11, 12}
A := mat.NewDense(4, 3, nil)
A.SetRow(0, row1)
A.SetRow(1, row2)
A.SetRow(2, row3)
A.SetRow(3, row4)
fmt.Printf("A matrix:\n%v\n\n", mat.Formatted(A, mat.Prefix(""), mat.Excerpt(0)))
//Get the dimensions of the matrix A
rows, cols := A.Dims()
fmt.Println("A: rows: ", rows)
fmt.Println("A: cols: ", cols)
}
Вывод:
A matrix:
⎡ 1 2 3⎤
⎢ 4 5 6⎥
⎢ 7 8 9⎥
⎣10 11 12⎦
A: rows: 4
A: cols: 3