-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRealMatrix.go
74 lines (56 loc) · 1.49 KB
/
RealMatrix.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
66
67
68
69
70
71
72
package LinearAlgebra
import "errors"
type RealMatrix struct {
vecs []*RealVector
}
func NewRealMatrix(vectors ...*RealVector) (*RealMatrix, error) {
rows := 0
for _, v := range vectors {
if rows == 0 {
rows = len(v.nums)
}
if rows != len(v.nums) {
return nil, errors.New("vectors must all have the same size")
}
}
return &RealMatrix{vectors}, nil
}
func(m RealMatrix) GetCols() int {
return len(m.vecs)
}
func(m RealMatrix) GetRows() int {
if len(m.vecs) > 0 {
return len(m.vecs[0].nums)
}
return 0
}
func(m RealMatrix) Add(other *RealMatrix) (*RealMatrix,error){
if m.GetCols() != other.GetCols() || m.GetRows() == other.GetRows() {
return nil, errors.New("matricies need to have the same dimensions")
}
newMatrix := &RealMatrix{}
for key, val := range m.vecs {
v, err := val.Add(other.vecs[key])
if err != nil {
//do something
return nil, errors.New("encountered errors when adding vectors")
}
newMatrix.vecs = append(newMatrix.vecs, v)
}
return newMatrix, nil
}
func(m RealMatrix) Minus(other *RealMatrix) (*RealMatrix,error){
if m.GetCols() != other.GetCols() || m.GetRows() == other.GetRows() {
return nil, errors.New("matricies need to have the same dimensions")
}
newMatrix := &RealMatrix{}
for key, val := range m.vecs {
v, err := val.Minus(other.vecs[key])
if err != nil {
//do something
return nil, errors.New("encountered errors when subtracting vectors")
}
newMatrix.vecs = append(newMatrix.vecs, v)
}
return newMatrix, nil
}