-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_vector.nim
293 lines (251 loc) · 8.4 KB
/
test_vector.nim
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
import unittest, math
import arraymancer
import numericalnim
test "Vector creation from array":
let c = [0.0, 1.0, 2.0, 3.3]
let v = newVector(c)
check v.components == @c
test "Vector creation from seq":
let c = @[0.0, 1.0, 2.0, 3.3]
let v = newVector(c)
check v.components == c
test "Vector equality ==":
let v1 = newVector([0.0, 1.0, 2.2, 100.0])
let v2 = newVector([0.0, 1.0, 2.2, 100.0])
check (v1 == v2) == true
check (v1 == v1) == true
test "Vector negate (-Vector)":
let v1 = newVector([1.0, 2.5, -3.34])
let correct = newVector([-1.0, -2.5, 3.34])
check correct == -v1
test "Vector addition":
let v1 = newVector([1.1, 2.2, 3.3])
let v2 = newVector([3.3, 2.2, 1.0])
let v3 = v1 + v2
check v3.components == @[1.1+3.3, 2.2+2.2, 3.3+1.0]
test "Vector-scalar addition":
let v1 = newVector([1.1, 2.2, 3.3])
let d = 8.98
let v2 = v1 + d
let v3 = d + v1
let correct = newVector([1.1+d, 2.2+d, 3.3+d])
check v2 == v3
check v2 == correct
test "Vectors of different sizes":
let v1 = newVector([1.0, 2.0, 4.0, 1.34, 9.9])
let v2 = newVector([3.3, 2.2, 1.1, 5.67])
expect(ValueError):
discard v1 + v2
test "Vector subtraction":
let v1 = newVector([1.1, 2.2, 3.3])
let v2 = newVector([3.3, 2.2, 1.0])
let v3 = v1 - v2
let correct = newVector([1.1-3.3, 2.2-2.2, 3.3-1.0])
check v3 == correct
test "Vector-scalar subtraction":
let v1 = newVector([1.1, 2.2, 3.3])
let d = 8.98
let v2 = v1 - d
let v3 = d - v1
let correct2 = newVector([1.1-d, 2.2-d, 3.3-d])
let correct3 = newVector([d-1.1, d-2.2, d-3.3])
check v2 == correct2
check v3 == correct3
test "Vector +=":
var v1 = newVector([0.0, 0.0, 0.0])
let v2 = newVector([1.0, 2.0, 3.0])
v1 += v2
check v1 == v2
test "Vector-scalar +=":
var v1 = newVector([1.1, 2.2, 3.3])
let d = 6.78
v1 += d
let correct = newVector([1.1+d, 2.2+d, 3.3+d])
check v1 == correct
test "Vector -=":
var v1 = newVector([0.0, 0.0, 0.0])
let v2 = newVector([1.0, 2.0, 3.0])
v1 -= v2
check v1 == -v2
test "Vector-scalar -=":
var v1 = newVector([1.1, 2.2, 3.3])
let d = 6.78
v1 -= d
let correct = newVector([1.1-d, 2.2-d, 3.3-d])
check v1 == correct
test "Vector-scalar division":
let v1 = newVector([0.0, 1.0, 2.0, 3.0])
let d = 2.0
let v2 = v1 / d
let correct = newVector([0.0/d, 1.0/d, 2.0/d, 3.0/d])
check v2 == correct
test "Vector-scalar multiplication":
let v1 = newVector([0.0, 1.0, 2.0, 3.0])
let d = 2.0
let v2 = v1 * d
let correct = newVector([0.0*d, 1.0*d, 2.0*d, 3.0*d])
check v2 == correct
test "Vector-scalar multiplication reverse order":
let v1 = newVector([0.0, 1.0, 2.0, 3.0])
let d = 2.0
let v2 = d * v1
let correct = newVector([0.0*d, 1.0*d, 2.0*d, 3.0*d])
check v2 == correct
test "Vector-Vector dot product":
let v1 = newVector([0.0, 1.0, 2.0, 3.0])
let v2 = newVector([1.0, 2.2, 6.3, 0.0])
let p1 = v1 * v2
let p2 = v1.dot(v2)
let correct = 0.0*1.0 + 1.0*2.2 + 2.0*6.3 + 3.0*0.0
check p1 == p2
check p1 == correct
test "Vector elemetwise multiplication":
let v1 = newVector([1.0, 2.0, 4.0, 1.34])
let v2 = newVector([3.3, 2.2, 1.1, 5.67])
let v3 = v1 *. v2
let correct = newVector([1.0*3.3, 2.0*2.2, 4.0*1.1, 1.34*5.67])
check v3 == correct
test "Vector elemetwise division":
let v1 = newVector([1.0, 2.0, 4.0, 1.34])
let v2 = newVector([3.3, 2.2, 1.1, 5.67])
let v3 = v1 /. v2
let correct = newVector([1.0/3.3, 2.0/2.2, 4.0/1.1, 1.34/5.67])
check v3 == correct
test "Vector *=":
var v1 = newVector([2.0, 3.0, 5.0])
let d = 1.25
v1 *= d
let correct = newVector([2.0*1.25, 3.0*1.25, 5.0*1.25])
check v1 == correct
test "Vector /=":
var v1 = newVector([2.0, 3.0, 5.0])
let d = 1.25
v1 /= d
let correct = newVector([2.0/1.25, 3.0/1.25, 5.0/1.25])
check v1 == correct
test "Vector *.=":
var v1 = newVector([2.0, 3.0, 5.0])
let v2 = newVector([3.3, 2.2, 1.0])
v1 *.= v2
let correct = newVector([2.0*3.3, 3.0*2.2, 5.0*1.0])
check v1 == correct
test "Vector *.=":
var v1 = newVector([2.0, 3.0, 5.0])
let v2 = newVector([3.3, 2.2, 1.0])
v1 /.= v2
let correct = newVector([2.0/3.3, 3.0/2.2, 5.0/1.0])
check v1 == correct
test "Vector nomr default (former abs)":
let v1 = newVector([1.0, 2.0, 3.0, 4.0])
let v1_abs = norm(v1)
let correct = sqrt(1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 4.0*4.0)
check v1_abs == correct
test "Vector non-nested @ unpacking":
let correct = @[1.0, 2.0, 4.0, 3.0]
let v = newVector(correct)
let vSeq = @v
check correct == vSeq
test "Vector 1 nested @ unpacking":
let c1 = @[1.0, 2.0, 3.0]
let c2 = @[4.0, 5.0, 6.0]
let c3 = @[7.0, 8.0, 9.0]
let correct = @[c1, c2, c3]
let v1 = newVector(c1)
let v2 = newVector(c2)
let v3 = newVector(c3)
let vNested = newVector([v1, v2, v3])
let vSeq = @vNested
check correct == vSeq
test "Vector 2 nested @ unpacking":
let c1 = @[1.0, 2.0, 3.0]
var c2 = @[4.0, 5.0, 6.0]
let c3 = @[7.0, 8.0, 9.0]
let c = @[c1, c2, c3]
let correct = @[c, c, c]
var v1 = newVector(c1)
var v2 = newVector(c2)
let v3 = newVector(c3)
let v = newVector([v1, v2, v3])
let vNested = newVector([v, v, v])
let vSeq = @vNested
check correct == vSeq
test "Vector iterator":
let c = @[0.0, 0.2, 0.4, 0.6, 0.9]
let v1 = newVector(c)
for i, val in v1:
check val == c[i]
test "Vector Powers (integer power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 2
let correct = newVector(@[1.0, 4.0, 9.0, 16.0])
check correct == (a ^ p)
test "Vector Powers (zero power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 0
let correct = newVector(@[1.0, 1.0, 1.0, 1.0])
check correct == (a ^ p)
test "Vector Powers (large integer power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 511
let correct = newVector(@[1.0, 6.703903964971299e+153, 6.44111661076297e+243, 4.49423283715579e+307])
let value = a ^ p
check isClose(value, correct, tol = 1e-4)
test "Vector Powers (float power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 2.0
let correct = newVector(@[1.0, 4.0, 9.0, 16.0])
check correct == (a ^ p)
test "Vector Powers (float power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 2.5
let correct = newVector(@[1.0, 5.656854249492381, 15.5884572681199, 32.0])
let value = a ^ p
check isClose(value, correct, tol = 1e-4)
test "Vector Powers (negative power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = -2.5
let correct = newVector(@[pow(1.0, -2.5), pow(2.0, -2.5), pow(3.0, -2.5), pow(4.0, -2.5)])
let value = a ^ p
check isClose(value, correct, tol = 1e-4)
test "Vector Powers (float power)":
let a = newVector([1.0, 2.0, 3.0, 4.0])
let p = 0.5
let correct = newVector(@[1.0, 1.414213562373095, 1.732050807568877, 2.0])
let value = a ^ p
check isClose(value, correct, tol = 1e-4)
test "Vector Norms (infinity norm)":
let vec = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
let value = norm(vec, 0)
let correct = 5.0
check correct == norm(vec, 0)
check isClose(value, correct, tol = 1e-4)
test "Vector Norms (1 norm)":
let vec = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
let value = norm(vec, 1)
let correct = 25.0
check isClose(value, correct, tol = 1e-4)
test "Vector Norms (2 norm)":
let vec = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
let value = norm(vec, 2)
let correct = 10.2469507659596
check isClose(value, correct, tol = 1e-4)
test "Vector Norms (p norm)":
let vec = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
let value = norm(vec, 4)
let correct = 6.871119744004721
check isClose(value, correct, tol = 1e-4)
test "Vector Norms (p norm)":
let vec = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
let value = norm(vec, 15)
let correct = 5.384187447546214
check isClose(value, correct, tol = 1e-4)
test "Vector abs (element-wise)":
let vec = newVector([-1.0,-2.0,3.0,-4.0,5.0,-5.0,5.0])
let abs_vec = abs(vec)
let correct = newVector([1.0,2.0,3.0,4.0,5.0,5.0,5.0])
check abs_vec == correct
test "Vector sum":
let vec = newVector([-1.0,-2.0,3.0,-4.0,5.0,-5.0,5.0])
let vec_sum = vec.sum()
let correct = sum([-1.0,-2.0,3.0,-4.0,5.0,-5.0,5.0])
check vec_sum == correct