-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmatrix_test.go
178 lines (173 loc) · 2.98 KB
/
matrix_test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package blas
import (
"testing"
)
func TestSparseMatrixSet(t *testing.T) {
var tests = []struct {
r, c int
data []float64
i, j int
v float64
result []float64
}{
{ // 0 at start of matrix set to non-zero
r: 3, c: 4,
data: []float64{
0, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 6,
},
i: 0, j: 0,
v: 5,
result: []float64{
5, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 6,
},
},
{ // 0 as first element of row set to non-zero
r: 3, c: 4,
data: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 6,
},
i: 2, j: 0,
v: 5,
result: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
5, 0, 3, 6,
},
},
{ // 0 as first non-zero element of row set to non-zero
r: 3, c: 4,
data: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 6,
},
i: 2, j: 1,
v: 5,
result: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
0, 5, 3, 6,
},
},
{ // 0 as non-zero element in middle of row/col set to non-zero
r: 3, c: 4,
data: []float64{
1, 0, 0, 0,
0, 2, 0, 7,
0, 0, 3, 6,
},
i: 1, j: 2,
v: 5,
result: []float64{
1, 0, 0, 0,
0, 2, 5, 7,
0, 0, 3, 6,
},
},
{ // non-zero value updated
r: 3, c: 4,
data: []float64{
1, 0, 0, 0,
0, 2, 0, 0,
0, 0, 3, 6,
},
i: 2, j: 2,
v: 5,
result: []float64{
1, 0, 0, 0,
0, 2, 0, 0,
0, 0, 5, 6,
},
},
{ // 0 at end of row set to non-zero
r: 3, c: 4,
data: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 0,
},
i: 2, j: 3,
v: 5,
result: []float64{
1, 0, 0, 0,
0, 2, 1, 0,
0, 0, 3, 5,
},
},
{ // 0 on all zero row/column set to non-zero
r: 3, c: 4,
data: []float64{
1, 0, 2, 0,
0, 0, 0, 0,
0, 0, 3, 6,
},
i: 1, j: 1,
v: 5,
result: []float64{
1, 0, 2, 0,
0, 5, 0, 0,
0, 0, 3, 6,
},
},
{ // non-zero on otherwise all zero row/column set to zero
r: 3, c: 4,
data: []float64{
1, 0, 2, 0,
0, 5, 0, 0,
0, 0, 3, 6,
},
i: 1, j: 1,
v: 0,
result: []float64{
1, 0, 2, 0,
0, 0, 0, 0,
0, 0, 3, 6,
},
},
{ // zero on otherwise all zero row/column set to zero
r: 3, c: 4,
data: []float64{
1, 0, 2, 0,
0, 0, 0, 0,
0, 0, 3, 6,
},
i: 1, j: 1,
v: 0,
result: []float64{
1, 0, 2, 0,
0, 0, 0, 0,
0, 0, 3, 6,
},
},
}
for ti, test := range tests {
matrix := SparseMatrix{
I: test.r, J: test.c,
}
matrix.Indptr = make([]int, test.r+1)
for i := 0; i < test.r; i++ {
for j := 0; j < test.c; j++ {
v := test.data[i*test.c+j]
if v != 0 {
matrix.Ind = append(matrix.Ind, j)
matrix.Data = append(matrix.Data, v)
}
}
matrix.Indptr[i+1] = len(matrix.Data)
}
matrix.Set(test.i, test.j, test.v)
for i := 0; i < test.r; i++ {
for j := 0; j < test.c; j++ {
if matrix.At(i, j) != test.result[i*test.c+j] {
t.Errorf("Test %d: Expected %f at %d,%d but found %f", ti+1, test.result[i*test.c+j], i, j, matrix.At(i, j))
}
}
}
}
}