-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathread.go
60 lines (50 loc) · 1.36 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package matrix
import (
"fmt"
)
// Rows returns the number of rows in the matrix.
func (matrix Matrix) Rows() int {
return int(matrix[0])
}
// Cols returns the number of columns in the matrix.
func (matrix Matrix) Cols() int {
return int(matrix[1])
}
// String returns a human readable representation of matrix, ready to print.
func (matrix Matrix) String() string {
output := "\n"
for i := 0; i < int(matrix[0]); i++ {
output += "{\t\t"
for j := 0; j < int(matrix[1]); j++ {
output = fmt.Sprintf("%v%v", output, matrix.At(i, j))
if j < int(matrix[1])-1 {
output += "\t\t"
}
}
output += "\t\t}\n"
}
return output
}
// At returns the value at position `row`, `col`.
//
// Just like an array, you're responsible to make sure
// you don't ask for an out of range value.
func (matrix Matrix) At(row, col int) float64 {
return matrix[matrix.IndexFor(row, col)]
}
// IndexFor computes the position of given cell in the underlying
// array representation.
func (matrix Matrix) IndexFor(row, col int) int {
return row*int(matrix[1]) + col + 2
}
// GetRow returns the given row (0-indexed) as a []float64.
func (matrix Matrix) GetRow(index int) (row []float64, err error) {
if index+1 > matrix.Rows() {
err = fmt.Errorf("Row %d is out of matrix", index)
return
}
for i := 0; i < matrix.Cols(); i++ {
row = append(row, matrix.At(index, i))
}
return
}