-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmatrix.go
65 lines (53 loc) · 1.13 KB
/
matrix.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
61
62
63
64
65
package matrix
import (
"fmt"
)
var DEBUG bool = false
type Matrix []float64
type Row []float64
type Builder []Row
/*
* Provide `true` if you want errors to panic
*/
func SetDebug(debug bool) {
DEBUG = debug
}
func generateError(message string) (err error) {
if DEBUG {
panic(message)
} else {
err = fmt.Errorf(message)
}
return
}
/*
* A matrix is valid if it has rows, columns and if all its rows have the same
* amount of columns.
*/
func (matrix Matrix) Valid() bool {
return len(matrix) > 2 && len(matrix) == int(matrix[0])*int(matrix[1])+2
}
/*
* Tells if two matrices have the same dimensions and same
* values in each cell.
*/
func (matrix Matrix) EqualTo(otherMatrix Matrix) bool {
if len(matrix) != len(otherMatrix) {
return false
}
for i := 0; i < len(matrix); i++ {
if matrix[i] != otherMatrix[i] {
return false
}
}
return true
}
/*
* True if both matrices are valid and have the same dimensions
*/
func (matrix Matrix) SameDimensions(otherMatrix Matrix) bool {
if !matrix.Valid() || !otherMatrix.Valid() {
return false
}
return matrix[0] == otherMatrix[0] && matrix[1] == otherMatrix[1]
}