-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathVectorTests.fs
881 lines (792 loc) · 25 KB
/
VectorTests.fs
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
module VectorTests
open System
open MKLNET
open MKLNET.Expression
open CsCheck
let MAX_DIM = 5
let gen1D = Gen.Int[1,MAX_DIM]
let gen2D = Gen.Select(gen1D,gen1D)
let genVector length = Gen.Double.OneTwo.Vector length
let genMatrix rows cols = Gen.Double.OneTwo.Matrix(rows, cols)
let add_vv (aS:double) (a:vector) (bS:double) (b:vector) =
let c = new vector(a.Length)
for i = 0 to a.Length-1 do
c[i] <- aS * a[i] + bS * b[i]
c
let mul_vvT (s:double) (a:vector) (b:vector) =
let C = new matrix(a.Length, b.Length)
for r = 0 to C.Rows-1 do
for c = 0 to C.Cols-1 do
C[r,c] <- s * a[r] * b[c]
C
let mul_vTv (a:vector) (b:vector) =
let mutable t = 0.0
for i = 0 to a.Length-1 do
t <- t + a[i] * b[i]
t
let mul_vTm (s:double) (a:vector) (B:matrix) =
let c = new vector(B.Cols)
for i = 0 to B.Cols - 1 do
let mutable t = 0.0
for r = 0 to a.Length - 1 do
t <- t + a[r] * B[r,i]
c[i] <- s * t
c
let mul_vTmT (s:double) (a:vector) (B:matrix) =
let c = new vector(B.Rows)
for i = 0 to B.Rows - 1 do
let mutable t = 0.0
for r = 0 to a.Length - 1 do
t <- t + a[r] * B[i,r]
c[i] <- s * t
c
let impM (m:MatrixExpression) = MatrixExpression.op_Implicit m
let impV (m:VectorExpression) = VectorExpression.op_Implicit m
let impVE (m:vector) = VectorExpression.op_Implicit m
let add1 = test "add1" {
test "vv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a 1.0 b
use actual = a + b |> impV
Check.close High expected actual
}
test "vv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a 1.0 b
use actual = (a + 0.0) + (b + 0.0) |> impV
Check.close High expected actual
}
test "vvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a s b
use actual = a + s * b |> impV
Check.close High expected actual
}
test "vvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a s b
use actual = (a + 0.0) + (s * b + 0.0) |> impV
Check.close High expected actual
}
test "vTvT" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a 1.0 b
use actual = (a.T + b.T).T |> impV
Check.close High expected actual
}
test "vTvT_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a 1.0 b
use actual = ((a.T + 0.0) + (b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTvTS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a s b
use actual = (a.T + s * b.T).T |> impV
Check.close High expected actual
}
test "vTvTS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a s b
use actual = ((a.T + 0.0) + (s * b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vSv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a 1.0 b
use actual = s * a + b |> impV
Check.close High expected actual
}
test "vSv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a 1.0 b
use actual = (s * a + 0.0) + (b + 0.0) |> impV
Check.close High expected actual
}
test "vSvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a s2 b
use actual = s1 * a + s2 * b |> impV
Check.close High expected actual
}
test "vSvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a s2 b
use actual = (s1 * a + 0.0) + (s2 * b + 0.0) |> impV
Check.close High expected actual
}
test "vTSvT" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a 1.0 b
use actual = (s * a.T + b.T).T |> impV
Check.close High expected actual
}
test "vTSvT_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a 1.0 b
use actual = ((s * a.T + 0.0) + (b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTSvTS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a s2 b
use actual = (s1 * a.T + s2 * b.T).T |> impV
Check.close High expected actual
}
test "vTSvTS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a s2 b
use actual = ((s1 * a.T + 0.0) + (s2 * b.T + 0.0)).T |> impV
Check.close High expected actual
}
}
let add2 = test "add2" {
test "vd" {
let! n = gen1D
use! A = genVector n
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- A[i] + a
R
use actual = A + a |> impV
Check.close High expected actual
}
test "vd_reuse" {
let! n = gen1D
use! A = genVector n
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- A[i] + a
R
use actual = (A + 0.0) + a |> impV
Check.close High expected actual
}
test "vTd" {
let! n = gen1D
use! A = genVector n
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- A[i] + a
R
let actual = (A.T + a).T |> impV
Check.close High expected actual
}
test "vTd_reuse" {
let! n = gen1D
use! A = genVector n
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- A[i] + a
R
let actual = ((A.T + 0.0) + a).T |> impV
Check.close High expected actual
}
test "mSd" {
let! n = gen1D
use! A = genVector n
let! s = Gen.Double.OneTwo
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- s * A[i] + a
R
use actual = (s * A) + a |> impV
Check.close High expected actual
}
test "mSd_reuse" {
let! n = gen1D
use! A = genVector n
let! s = Gen.Double.OneTwo
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- s * A[i] + a
R
use actual = (s * A + 0.0) + a |> impV
Check.close High expected actual
}
test "mTSd" {
let! n = gen1D
use! A = genVector n
let! s = Gen.Double.OneTwo
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- (s * A[i]) + a
R
let actual = ((s * A.T) + a).T |> impV
Check.close High expected actual
}
test "mTSd_reuse" {
let! n = gen1D
use! A = genVector n
let! s = Gen.Double.OneTwo
let! a = Gen.Double.OneTwo
use expected =
let R = new vector(n)
for i =0 to n-1 do
R[i] <- (s * A[i]) + a
R
let actual = ((s * A.T + 0.0) + a).T |> impV
Check.close High expected actual
}
}
let sub1 = test "sub1" {
test "vv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a -1.0 b
use actual = a - b |> impV
Check.close High expected actual
}
test "vv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a -1.0 b
use actual = (a + 0.0) - (b + 0.0) |> impV
Check.close High expected actual
}
test "vvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a -s b
use actual = a - s * b |> impV
Check.close High expected actual
}
test "vvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a -s b
use actual = (a + 0.0) - (s * b + 0.0) |> impV
Check.close High expected actual
}
test "vTvT" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a -1.0 b
use actual = (a.T - b.T).T |> impV
Check.close High expected actual
}
test "vTvT_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
use expected = add_vv 1.0 a -1.0 b
use actual = ((a.T + 0.0) - (b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTvTS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a -s b
use actual = (a.T - s * b.T).T |> impV
Check.close High expected actual
}
test "vTvTS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv 1.0 a -s b
use actual = ((a.T + 0.0) - (s * b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vSv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a -1.0 b
use actual = s * a - b |> impV
Check.close High expected actual
}
test "vSv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a -1.0 b
use actual = (s * a + 0.0) - (b + 0.0) |> impV
Check.close High expected actual
}
test "vSvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a -s2 b
use actual = s1 * a - s2 * b |> impV
Check.close High expected actual
}
test "vSvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a -s2 b
use actual = (s1 * a + 0.0) - (s2 * b + 0.0) |> impV
Check.close High expected actual
}
test "vTSvT" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a -1.0 b
use actual = (s * a.T - b.T).T |> impV
Check.close High expected actual
}
}
let sub2 = test "sub2" {
test "vTSvT_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = add_vv s a -1.0 b
use actual = ((s * a.T + 0.0) - (b.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTSvTS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a -s2 b
use actual = (s1 * a.T - s2 * b.T).T |> impV
Check.close High expected actual
}
test "vTSvTS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = add_vv s1 a -s2 b
use actual = ((s1 * a.T + 0.0) - (s2 * b.T + 0.0)).T |> impV
Check.close High expected actual
}
}
let mul1 = test "mul1" {
test "vvT" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
use expected = mul_vvT 1.0 a b
use actual = (a * b.T) |> impM
Check.close High expected actual
}
test "vvT_reuse" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
use expected = mul_vvT 1.0 a b
use actual = ((a + 0.0) * (b.T + 0.0)) |> impM
Check.close High expected actual
}
test "vvTS" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = mul_vvT s a b
use actual = a * (s * b.T) |> impM
Check.close High expected actual
}
test "vvTS_reuse" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = mul_vvT s a b
use actual = (a + 0.0) * (s * b.T + 0.0) |> impM
Check.close High expected actual
}
test "vTv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let expected = mul_vTv a b
let actual = a.T * b
Check.close High expected actual
}
test "vTv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let expected = mul_vTv a b
let actual = (a.T + 0.0) * (b + 0.0)
Check.close High expected actual
}
test "vTvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
let expected = mul_vTv a b * s
let actual = a.T * (b * s)
Check.close High expected actual
}
test "vTvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
let expected = mul_vTv a b * s
let actual = (a.T + 0.0) * (b * s + 0.0)
Check.close High expected actual
}
test "vSvT" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = mul_vvT s a b
use actual = (s * a) * b.T |> impM
Check.close High expected actual
}
test "vSvT_reuse" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s = Gen.Double.OneTwo
use expected = mul_vvT s a b
use actual = (s * a + 0.0) * (b.T + 0.0) |> impM
Check.close High expected actual
}
test "vSvTS" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = mul_vvT (s1*s2) a b
use actual = (s1 * a) * (s2 * b.T) |> impM
Check.close High expected actual
}
test "vSvTS_reuse" {
let! m,n = gen2D
use! a = genVector m
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = mul_vvT (s1*s2) a b
use actual = (s1 * a + 0.0) * (s2 * b.T + 0.0) |> impM
Check.close High expected actual
}
test "vTSv" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
let expected = s * mul_vTv a b
let actual = (s * a.T) * b
Check.close High expected actual
}
test "vTSv_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s = Gen.Double.OneTwo
let expected = s * mul_vTv a b
let actual = (s * a.T + 0.0) * (b + 0.0)
Check.close High expected actual
}
}
let mul2 = test "mul2" {
test "vTSvS" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
let expected = s1 * mul_vTv a b * s2
let actual = (s1 * a.T) * (b * s2)
Check.close High expected actual
}
test "vTSvS_reuse" {
let! n = gen1D
use! a = genVector n
use! b = genVector n
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
let expected = s1 * mul_vTv a b * s2
let actual = (s1 * a.T + 0.0) * (b * s2 + 0.0)
Check.close High expected actual
}
test "vTm" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix m n
use expected = mul_vTm 1.0 a B
use actual = (a.T * B).T |> impV
Check.close High expected actual
}
test "vTm_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix m n
use expected = mul_vTm 1.0 a B
use actual = ((a.T + 0.0) * (B + 0.0)).T |> impV
Check.close High expected actual
}
test "vTmT" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
use expected = mul_vTmT 1.0 a B
use actual = (a.T * B.T).T |> impV
Check.close High expected actual
}
test "vTmT_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
use expected = mul_vTmT 1.0 a B
use actual = ((a.T + 0.0) * (B.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTmTS" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s = Gen.Double.OneTwo
use expected = mul_vTmT s a B
use actual = (a.T * (s * B.T)).T |> impV
Check.close High expected actual
}
test "vTmTS_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s = Gen.Double.OneTwo
use expected = mul_vTmT s a B
use actual = ((a.T + 0.0) * (s * B.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTSm" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix m n
let! s = Gen.Double.OneTwo
use expected = mul_vTm s a B
use actual = ((s * a.T) * B).T |> impV
Check.close High expected actual
}
test "vTSm_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix m n
let! s = Gen.Double.OneTwo
use expected = mul_vTm s a B
use actual = ((s * a.T + 0.0) * (B + 0.0)).T |> impV
Check.close High expected actual
}
test "vTSmT" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s = Gen.Double.OneTwo
use expected = mul_vTmT s a B
use actual = ((s * a.T) * B.T).T |> impV
Check.close High expected actual
}
test "vTSmT_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s = Gen.Double.OneTwo
use expected = mul_vTmT s a B
use actual = ((s * a.T + 0.0) * (B.T + 0.0)).T |> impV
Check.close High expected actual
}
test "vTSmTS" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = mul_vTmT (s1*s2) a B
use actual = ((s1 * a.T) * (s2 * B.T)).T |> impV
Check.close High expected actual
}
test "vTSmTS_reuse" {
let! m,n = gen2D
use! a = genVector m
use! B = genMatrix n m
let! s1 = Gen.Double.OneTwo
let! s2 = Gen.Double.OneTwo
use expected = mul_vTmT (s1*s2) a B
use actual = ((s1 * a.T + 0.0) * (s2 * B.T + 0.0)).T |> impV
Check.close High expected actual
}
}
let testUnary name
(fexpected:double -> double)
(factual:VectorExpression -> VectorExpression)
(factualT:VectorTExpression -> VectorTExpression) =
let map (A:vector) =
let E = new vector(A.Length)
for i = 0 to A.Length-1 do
E[i] <- fexpected(A[i])
E
test name {
let! n = gen1D
use! A = genVector n
use expected = map A
use actual = A |> impVE |> factual |> impV
Check.close High expected actual
use actual = (factualT A.T).T |> impV
Check.close High expected actual
}
let functions1 = test "functions1" {
testUnary "Abs" abs Vector.Abs Vector.Abs
testUnary "Sqr" (fun x -> x*x) Vector.Sqr Vector.Sqr
testUnary "Inv" (fun x -> 1.0/x) Vector.Inv Vector.Inv
testUnary "Sqrt" sqrt Vector.Sqrt Vector.Sqrt
testUnary "InvSqrt" (fun x -> 1.0/sqrt x) Vector.InvSqrt Vector.InvSqrt
#if NETCOREAPP
testUnary "Cbrt" Math.Cbrt Vector.Cbrt Vector.Cbrt
testUnary "InvCbrt" ((/) 1.0 >> Math.Cbrt) Vector.InvCbrt Vector.InvCbrt
testUnary "Pow2o3" (fun a -> Math.Cbrt(a*a)) Vector.Pow2o3 Vector.Pow2o3
#endif
testUnary "Pow3o2" (fun a -> Math.Sqrt(a*a*a)) Vector.Pow3o2 Vector.Pow3o2
testUnary "Exp" exp Vector.Exp Vector.Exp
testUnary "Exp2" (fun i -> Math.Pow(2.0,i)) Vector.Exp2 Vector.Exp2
testUnary "Exp10" (fun i -> Math.Pow(10.0,i)) Vector.Exp10 Vector.Exp10
testUnary "Expm1" (fun i -> exp i-1.0) Vector.Expm1 Vector.Expm1
testUnary "Ln" log Vector.Ln Vector.Ln
#if NETCOREAPP
testUnary "Log2" Math.Log2 Vector.Log2 Vector.Log2
#endif
testUnary "Log10" log10 Vector.Log10 Vector.Log10
testUnary "Log1p" (fun i -> log(i+1.0)) Vector.Log1p Vector.Log1p
#if NETCOREAPP
testUnary "Logb" (fun i -> Math.ILogB(i) |> double) Vector.Logb Vector.Logb
#endif
}
let functions2 = test "functions2" {
testUnary "Cos" cos Vector.Cos Vector.Cos
testUnary "Sin" sin Vector.Sin Vector.Sin
testUnary "Tan" tan Vector.Tan Vector.Tan
testUnary "Cospi" (fun i -> cos(Math.PI*i)) Vector.Cospi Vector.Cospi
testUnary "Sinpi" (fun i -> sin(Math.PI*i)) Vector.Sinpi Vector.Sinpi
testUnary "Tanpi" (fun i -> tan(Math.PI*i)) Vector.Tanpi Vector.Tanpi
testUnary "Cosd" (fun i -> cos(Math.PI/180.0*i)) Vector.Cosd Vector.Cosd
testUnary "Sind" (fun i -> sin(Math.PI/180.0*i)) Vector.Sind Vector.Sind
testUnary "Tand" (fun i -> tan(Math.PI/180.0*i)) Vector.Tand Vector.Tand
testUnary "Acos" acos Vector.Acos Vector.Acos
testUnary "Asin" asin Vector.Asin Vector.Asin
testUnary "Atan" atan Vector.Atan Vector.Atan
testUnary "Acospi" (fun i -> acos i / Math.PI) Vector.Acospi Vector.Acospi
testUnary "Asinpi" (fun i -> asin i / Math.PI) Vector.Asinpi Vector.Asinpi
testUnary "Atanpi" (fun i -> atan i / Math.PI) Vector.Atanpi Vector.Atanpi
testUnary "Cosh" cosh Vector.Cosh Vector.Cosh
testUnary "Sinh" sinh Vector.Sinh Vector.Sinh
testUnary "Tanh" tanh Vector.Tanh Vector.Tanh
#if NETCOREAPP
testUnary "Acosh" Math.Acosh Vector.Acosh Vector.Acosh
testUnary "Asinh" Math.Asinh Vector.Asinh Vector.Asinh
testUnary "Atanh" Math.Atanh Vector.Atanh Vector.Atanh
#endif
testUnary "Erf" erf Vector.Erf Vector.Erf
testUnary "Erfc" erfc Vector.Erfc Vector.Erfc
testUnary "ErfInv" erfinv Vector.ErfInv Vector.ErfInv
testUnary "ErfcInv" erfcinv Vector.ErfcInv Vector.ErfcInv
testUnary "CdfNorm" normcdf Vector.CdfNorm Vector.CdfNorm
testUnary "CdfNormInv" normcdfinv Vector.CdfNormInv Vector.CdfNormInv
testUnary "LGamma" lgamma Vector.LGamma Vector.LGamma
testUnary "TGamma" gamma Vector.TGamma Vector.TGamma
testUnary "ExpInt1" (expint 1) Vector.ExpInt1 Vector.ExpInt1
testUnary "Floor" floor Vector.Floor Vector.Floor
testUnary "Ceil" ceil Vector.Ceil Vector.Ceil
testUnary "Truncate" truncate Vector.Truncate Vector.Truncate
testUnary "Round" Math.Round Vector.Round Vector.Round
testUnary "RoundAwayFromZero" (fun a -> Math.Round(a, MidpointRounding.AwayFromZero)) Vector.RoundAwayFromZero Vector.RoundAwayFromZero
testUnary "Frac" (fun a -> a - truncate a) Vector.Frac Vector.Frac
testUnary "NearbyInt" (fun a -> Math.Round(a, MidpointRounding.ToEven)) Vector.NearbyInt Vector.NearbyInt
testUnary "Rint" (fun a -> Math.Round(a, MidpointRounding.ToEven)) Vector.Rint Vector.Rint
}
let all =
test "vector" {
add1
add2
sub1
sub2
mul1
mul2
functions1
functions2
}