forked from andeya/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_test.go
More file actions
403 lines (355 loc) · 9.96 KB
/
vector_test.go
File metadata and controls
403 lines (355 loc) · 9.96 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Author: slowpoke <proxypoke at lavabit dot com>
// Repository: https://gist.github.com/proxypoke/vector
//
// This program is free software under the non-terms
// of the Anti-License. Do whatever the fuck you want.
package vector
import (
"math"
"math/rand"
"testing"
)
// ========================== [ Constructor Tests ] ===========================
// Creates vectors with dimension from 0 to 99, checks if they actually have
// that dimension, then checks if the values are correctly initialized to 0.
func TestNew(t *testing.T) {
var i, j uint
for i = 0; i < 100; i++ {
v := New(i)
if v.Dim() != i {
t.Errorf("Wrong dimension. Got %d, expected %d.", v.Dim(), i)
}
for j = 0; j < i; j++ {
// XXX: If the Get method errors, this test will still pass. This
// is because Get() would then return an uninitialized float64 for
// val, which is 0 and therefore what the test expects.
val, _ := v.Get(j)
if val != 0 {
t.Error("Newly initialized vector has a value != 0.")
}
}
}
}
// Creates vectors with randomized slices, then checks whether they have the
// correct dimension (len(slice)) and whether they have been correctly
// initialized.
func TestNewFrom(t *testing.T) {
var i, j uint
for i = 0; i < 100; i++ {
randslice := makeRandSlice(i)
v := NewFrom(randslice)
if v.Dim() != i {
t.Errorf("Wrong dimension. Got %d, expected %d.", v.Dim(), i)
}
for j = 0; j < i; j++ {
val, _ := v.Get(j)
if val != randslice[j] {
t.Error(
"Wrong values in vector initialized from a random slice.")
}
}
}
}
// Creates pseudo-random vectors with various dimensions, copies them and
// verifies that the new vector is equal.
func TestCopy(t *testing.T) {
var i uint
for i = 0; i < 100; i++ {
v := makeRandomVector(i)
w := v.Copy()
if !Equal(v, w) {
t.Error("Copied vector is not equal to source vector.")
}
}
}
// =================== [ General Methods/Functions Tests ] ====================
// Creates pseudo-random vectors with various dimensions, then check if Get()
// returns the correct values and errors on out-of-range indexes.
func TestGet(t *testing.T) {
var i uint
for i = 0; i < 100; i++ {
v := makeRandomVector(i)
for j, val := range v.dims {
getval, err := v.Get(uint(j))
if err != nil {
t.Error("Get() errored on a correct index.")
}
if val != getval {
t.Error("Get() returned a wrong value.")
}
}
_, err := v.Get(v.Dim())
if err == nil {
t.Error("Get didn't error on an out-of-range index.")
}
}
}
// Creates uninitialized vectors of various dimensions, then sets their values
// to pseudo-random values. It then compares those values to check if they
// were set correctly. Also verifies is Set() correctly errors on out-of-range
// indexes.
func TestSet(t *testing.T) {
var i, j uint
for i = 0; i < 100; i++ {
v := New(i)
for j = 0; j < i; j++ {
val := rand.ExpFloat64()
err := v.Set(j, val)
if err != nil {
t.Error("Set() errored on a correct index.")
}
if v.dims[j] != val {
t.Error("Set didn't correctly set a value.")
}
}
err := v.Set(v.Dim(), 0)
if err == nil {
t.Error("Set didn't error on an out-of-range index.")
}
}
}
// Creates a vector with known length, then compares the expected value with
// what Len() returns.
func TestLen(t *testing.T) {
v := New(1)
v.Set(0, 2) // has length 2
if v.Len() != 2 {
t.Error("Len returned a wrong length")
}
}
// Creates Vectors which are known to be (un)equal, then verifies that Equal()
// has correct oytput.
func TestEqual(t *testing.T) {
slc := make([]float64, 10)
for i := range slc {
slc[i] = float64(i)
}
v := NewFrom(slc)
w := NewFrom(slc)
if !Equal(v, w) {
t.Error("Equal() != true for equal vectors.")
}
w = New(10)
if Equal(v, w) {
t.Error("Equal() == true for unequal vectors.")
}
}
// =========================== [ Operation Tests ] ============================
// Creates pesudo-random vectors, then adds them first as a non-destructive,
// then as an in-place operations, checking if both operation were correct.
func TestAdd(t *testing.T) {
var i, j uint
for i = 1; i < 100; i++ {
a := makeRandomVector(i)
b := makeRandomVector(i)
c, _ := Add(a, b)
for j = 0; j < i; j++ {
if c.dims[j] != a.dims[j]+b.dims[j] {
t.Error("Addition failed, didn't get expected values.")
t.Logf("%f + %f != %f", a.dims[j], b.dims[j], c.dims[j])
}
}
// Test in-place addition.
c = a.Copy()
c.Add(b)
for j = 0; j < i; j++ {
if c.dims[j] != a.dims[j]+b.dims[j] {
t.Error(
"In-place Addition failed, didn't get expected values.")
t.Logf("%f + %f != %f", a.dims[j], b.dims[j], c.dims[j])
}
}
}
}
// Same as TestAdd, but with substraction. Heck, it's basically the same code.
func TestSubstract(t *testing.T) {
var i, j uint
for i = 1; i < 100; i++ {
a := makeRandomVector(i)
b := makeRandomVector(i)
c, _ := Substract(a, b)
for j = 0; j < i; j++ {
if c.dims[j] != a.dims[j]-b.dims[j] {
t.Error("Substraction failed, didn't get expected values.")
t.Logf("%f - %f != %f", a.dims[j], b.dims[j], c.dims[j])
}
}
// Test in-place sybstraction
c = a.Copy()
c.Substract(b)
for j = 0; j < i; j++ {
if c.dims[j] != a.dims[j]-b.dims[j] {
t.Error(
"In-place Substraction failed, didn't get expected values.")
t.Logf("%f - %f != %f", a.dims[j], b.dims[j], c.dims[j])
}
}
}
}
// Creates pseudo-random vectors, does scalar multiplication with pseudo-random
// floats, then checks if the result is correct. It checks both the in-place
// and the non-destructive version.
func TestScale(t *testing.T) {
var i, j uint
for i = 0; i < 100; i++ {
a := makeRandomVector(i)
x := rand.ExpFloat64()
b := Scale(a, x)
for j = 0; j < i; j++ {
if b.dims[j] != a.dims[j]*x {
t.Error("Scalar Multiplication failed, ",
"didn't get expected values.")
t.Logf("%f * %f != %f", a.dims[j], x, b.dims[j])
}
}
// Test in-place scalar multiplication
b = a.Copy()
b.Scale(x)
for j = 0; j < i; j++ {
if b.dims[j] != a.dims[j]*x {
t.Error("In-place Scalar Multiplication failed, ",
"didn't get expected values.")
t.Logf("%f * %f != %f", a.dims[j], x, b.dims[j])
}
}
}
}
// Creates pseudo-random vectors, normalizes them both in-place and
// non-destructive, and verifies that the result is correct.
func TestNormalize(t *testing.T) {
var i uint
// It makes no sense to normalize a zero vector, therefore we start at 1.
for i = 1; i < 100; i++ {
a := makeRandomVector(i)
b := Normalize(a)
if b.Len() != float64(1) {
t.Error("Normalization failed, vector doesn't have length 1.")
t.Logf("%f != 1", b.Len())
}
}
}
// Uses vectors with known angles to calculate their DotProduct, then verifies
// if the result is correct.
func TestDotProduct(t *testing.T) {
a := New(2)
b := New(2)
// Set the vectors as parallel.
a.Set(0, 1)
b.Set(0, 1)
dot, _ := DotProduct(a, b)
if dot != 1 {
t.Error("Dot Product of parallel vectors isn't 1.")
}
// Set the vectors as orthogonal.
b = New(2)
b.Set(1, 1)
dot, _ = DotProduct(a, b)
if dot != 0 {
t.Error("Dot Product of orthogonal vectors isn't 0.")
}
// Set the vectors as anti-parallel.
b = New(2)
b.Set(0, -1)
dot, _ = DotProduct(a, b)
if dot != -1 {
t.Error("Dot Product of anti-parallel vectors isn't -1.")
}
}
// Uses vectors with known angles to verify that Angle() is correct.
func TestAngle(t *testing.T) {
a := New(2)
b := New(2)
// Set the vectors as parallel (Θ == 0).
a.Set(0, 1)
b.Set(0, 1)
Θ, _ := Angle(a, b)
if Θ != 0 {
t.Error("Angle between parallel vectors isn't 0.")
t.Logf("%f != 0", Θ)
}
// Set the vectors as orthogonal (Θ == 0.5π).
b = New(2)
b.Set(1, 1)
Θ, _ = Angle(a, b)
if Θ != 0.5*math.Pi {
t.Error("Angle between orthonal vectors isn't 0.5π.")
t.Logf("%f != %f", Θ, 0.5*math.Pi)
}
// Set the vectors as anti-parallel (Θ == π).
b = New(2)
b.Set(0, -1)
Θ, _ = Angle(a, b)
if Θ != math.Pi {
t.Error("Angle between anti-parallel vectors isn't π.")
t.Logf("%f != %f", Θ, math.Pi)
}
}
// Calculates the cross product of two pseudo-random vectors, then checks if
// the resulting vector is orthogonal to both the original vectors. Tests both
// in-place and non-destructive versions of the operation.
func TestCrossProduct(t *testing.T) {
check := func(a, b, c *Vector) {
dot_a, _ := DotProduct(a, c)
dot_b, _ := DotProduct(b, c)
ε := 0.0000000005
if math.Abs(0-dot_a) < ε {
dot_a = 0
}
if math.Abs(0-dot_b) < ε {
dot_b = 0
}
if dot_a != 0 || dot_b != 0 {
t.Error("Either or both vectors aren't orthogonal",
"to their Cross Product.")
t.Logf("a * c = %f", dot_a)
t.Logf("b * c = %f", dot_b)
}
}
a := makeRandomVector(3)
b := makeRandomVector(3)
c, _ := CrossProduct(a, b)
check(a, b, c)
// Check in-place, too.
c = a.Copy()
c.CrossProduct(b)
check(a, b, c)
// Check if vectors ∉ ℝ³ are rejected.
d := New(2)
e := New(4)
_, err := CrossProduct(d, e)
if err == nil {
t.Error("CrossProduct() didn't error with invalid input vectors",
"(∉ ℝ³)")
}
}
// Check whether the various functions that take more than one vector error on
// being supplied with vectors of missmatched dimensions.
// It suffices to check the helper function checkDims, since every function
// must call it to verify its inputs.
func TestMissmatchedDims(t *testing.T) {
a := New(2)
b := New(3)
err := checkDims(a, b)
if err == nil {
t.Error("Missmatched dimension check succeeded on unequal dimensions.")
}
a = New(4)
b = New(4)
err = checkDims(a, b)
if err != nil {
t.Error("Missmatched dimension check failed on equal dimensions.")
}
}
// =========================== [ Helper Functions ] ===========================
// Helper function, makes pseudo-random slices.
func makeRandSlice(length uint) (randslice []float64) {
randslice = make([]float64, length)
for i := range randslice {
randslice[i] = rand.ExpFloat64()
}
return
}
// Helper function, make a pseudo-random Vector with dimension dim.
func makeRandomVector(dim uint) *Vector {
return NewFrom(makeRandSlice(dim))
}