]> gitweb.factorcode.org Git - factor.git/blob - extra/tensors/tensors-tests.factor
calendar.format: make duration>human-readable more human readable
[factor.git] / extra / tensors / tensors-tests.factor
1 ! Copyright (C) 2019 HMC Clinic.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays kernel math math.order math.vectors
4 sequences specialized-arrays tensors tools.test ;
5 QUALIFIED-WITH: alien.c-types c
6 SPECIALIZED-ARRAY: c:float
7 IN: tensors.tests
8
9 ! Test zeros
10 { float-array{ 0.0 0.0 0.0 0.0 } } [
11     { 4 } zeros vec>>
12 ] unit-test
13
14 { { 4 } } [
15     { 4 } zeros shape>>
16 ] unit-test
17
18 { float-array{ 0.0 0.0 0.0 0.0 } } [
19     { 2 2 } zeros vec>>
20 ] unit-test
21
22 { { 2 2 } } [
23     { 2 2 } zeros shape>>
24 ] unit-test
25
26 [
27     { 0 5 } zeros
28 ]
29 [ { 0 5 } \ non-positive-shape-error boa = ] must-fail-with
30
31 [
32     { -3 5 } zeros
33 ]
34 [ { -3 5 } \ non-positive-shape-error boa = ] must-fail-with
35
36 ! Test ones
37 { float-array{ 1.0 1.0 1.0 1.0 } } [
38     { 4 } ones vec>>
39 ] unit-test
40
41 { { 4 } } [
42     { 4 } ones shape>>
43 ] unit-test
44
45 { float-array{ 1.0 1.0 1.0 1.0 } } [
46     { 2 2 } ones vec>>
47 ] unit-test
48
49 { { 2 2 } } [
50     { 2 2 } ones shape>>
51 ] unit-test
52
53 [
54     { 0 5 } ones
55 ]
56 [ { 0 5 } \ non-positive-shape-error boa = ] must-fail-with
57
58 [
59     { -3 5 } ones
60 ]
61 [ { -3 5 } \ non-positive-shape-error boa = ] must-fail-with
62
63
64 ! Test arange
65 { { 4 } float-array{ 0. 1. 2. 3. } } [
66     0 3 1 arange [ shape>> ] [ vec>> ] bi
67 ] unit-test
68
69 { { 4 } float-array{ 0. 2. 4. 6. } } [
70     0 7 2 arange [ shape>> ] [ vec>> ] bi
71 ] unit-test
72
73 { { 3 } float-array{ 1. 4. 7. } } [
74     1 9 3 arange [ shape>> ] [ vec>> ] bi
75 ] unit-test
76
77 { { 5 } float-array{ 1. 3. 5. 7. 9. } } [
78     1 9 2 arange [ shape>> ] [ vec>> ] bi
79 ] unit-test
80
81
82 ! Test naturals
83 { float-array{ 0.0 1.0 2.0 3.0 } } [
84     { 4 } naturals vec>>
85 ] unit-test
86
87 { { 4 } } [
88     { 4 } naturals shape>>
89 ] unit-test
90
91 { float-array{ 0.0 1.0 2.0 3.0 } } [
92     { 2 2 } naturals vec>>
93 ] unit-test
94
95 { { 2 2 } } [
96     { 2 2 } naturals shape>>
97 ] unit-test
98
99 [
100     { 0 5 } naturals
101 ]
102 [ { 0 5 } \ non-positive-shape-error boa = ] must-fail-with
103
104 [
105     { -3 5 } naturals
106 ]
107 [ { -3 5 } \ non-positive-shape-error boa = ] must-fail-with
108
109 ! Test (tensor)
110 { { 2 4 } } [
111     { 2 4 } (tensor) shape>>
112 ] unit-test
113
114 { { 0 } } [
115     { 0 } (tensor) shape>>
116 ] unit-test
117
118 { float-array{ } } [
119     { 0 } (tensor) vec>>
120 ] unit-test
121
122
123 ! Test reshape
124 { float-array{ 0.0 0.0 0.0 0.0 } } [
125     { 4 } zeros { 2 2 } reshape vec>>
126 ] unit-test
127
128 { { 2 2 } } [
129     { 4 } zeros { 2 2 } reshape shape>>
130 ] unit-test
131
132 [
133     { 2 2 } zeros { 2 3 } reshape
134 ]
135 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
136
137 [
138     { 2 2 } zeros { -2 -2 } reshape
139 ]
140 [ { -2 -2 } \ non-positive-shape-error boa = ] must-fail-with
141
142 ! Test flatten
143 { float-array{ 0.0 0.0 0.0 0.0 } } [
144     { 2 2 } zeros flatten vec>>
145 ] unit-test
146
147 { { 4 } } [
148     { 2 2 } zeros flatten shape>>
149 ] unit-test
150
151 { float-array{ 0.0 0.0 0.0 0.0 } } [
152     { 4 } zeros flatten vec>>
153 ] unit-test
154
155 { { 4 } } [
156     { 4 } zeros flatten shape>>
157 ] unit-test
158
159 ! Test dims
160 { 1 } [
161     { 3 } zeros dims
162 ] unit-test
163
164 { 2 } [
165     { 2 2 } ones dims
166 ] unit-test
167
168 { 3 } [
169     { 1 2 3 } zeros dims
170 ] unit-test
171
172 ! Test sequence operations
173 ! TODO: add tests for clone-like
174 ! test length
175 { 20 } [
176     { 2 2 5 } naturals length
177 ] unit-test
178
179 { 0 } [
180     t{ } length
181 ] unit-test
182
183 ! test new-sequence
184 { 10 } [
185     10 { 2 5 } ones new-sequence shape>> product
186 ] unit-test
187
188 { 2 } [
189     2 { 3 4 5 } ones new-sequence shape>> product
190 ] unit-test
191
192 { 20 } [
193     20 { 2 5 } ones new-sequence shape>> product
194 ] unit-test
195
196 ! test nth
197 { 1.0 } [
198     1 { 5 } naturals nth
199 ] unit-test
200
201 { 1.0 } [
202     { 1 } { 5 } naturals nth
203 ] unit-test
204
205 { 3.0 } [
206     { 1 1 } { 2 2 } naturals nth
207 ] unit-test
208
209 { 5.0 } [
210     { 1 0 1 } { 2 2 2 } naturals nth
211 ] unit-test
212
213 [
214     { 1 2 3 } t{ 1 2 3 } nth
215 ]
216 [ 1 3 \ dimension-mismatch-error boa = ] must-fail-with
217
218 ! test set-nth
219 { t{ 1 5 3 } } [
220     t{ 1 2 3 } dup [ 5 { 1 } ] dip set-nth
221 ] unit-test
222
223 { t{ { 0 1 } { 5 3 } } } [
224     { 2 2 } naturals dup [ 5 { 1 0 } ] dip set-nth
225 ] unit-test
226
227 { t{ { { 0 1 } { 2 3 } } { { 4 10 } { 6 7 } } } } [
228     { 2 2 2 } naturals dup [ 10 { 1 0 1 } ] dip set-nth
229 ] unit-test
230
231 [
232     { 2 2 } naturals dup [ 5 { 1 } ] dip set-nth
233 ]
234 [ 2 1 \ dimension-mismatch-error boa = ] must-fail-with
235
236 ! test clone
237 { t{ 1 2 3 }  } [
238     t{ 1 2 3 } dup clone [ 5 1 ] dip set-nth
239 ] unit-test
240
241 { t } [
242     t{ 1 2 3 } dup clone =
243 ] unit-test
244
245 { f } [
246     t{ 1 2 3 } dup clone dup [ 5 1 ] dip set-nth =
247 ] unit-test
248
249 ! Test like
250 { float-array{ 0.0 1.0 2.0 3.0 4.0 5.0 } } [
251     { 2 3 } naturals dup like vec>>
252 ] unit-test
253
254 { { 2 3 } } [
255     { 2 3 } naturals dup like shape>>
256 ] unit-test
257
258 { float-array{ 0.0 1.0 2.0 3.0 4.0 5.0 } } [
259     { 0 1 2 3 4 5 } { 2 3 } naturals like vec>>
260 ] unit-test
261
262 { { 2 3 } } [
263     { 0 1 2 3 4 5 } { 2 3 } naturals like shape>>
264 ] unit-test
265
266 { float-array{ 0.0 1.0 2.0 3.0 4.0 5.0 } } [
267     float-array{ 0 1 2 3 4 5 } { 2 3 } naturals like vec>>
268 ] unit-test
269
270 { { 2 3 } } [
271     float-array{ 0 1 2 3 4 5 } { 2 3 } naturals like shape>>
272 ] unit-test
273
274 { float-array{ 0.0 1.0 2.0 3.0 4.0 } } [
275     { 0 1 2 3 4 } { 2 3 } naturals like vec>>
276 ] unit-test
277
278 { { 5 } } [
279     { 0 1 2 3 4 } { 2 3 } naturals like shape>>
280 ] unit-test
281
282 { float-array{ 0.0 1.0 2.0 3.0 4.0 } } [
283     float-array{ 0 1 2 3 4 } { 2 3 } naturals like vec>>
284 ] unit-test
285
286 { { 5 } } [
287     float-array{ 0 1 2 3 4 } { 2 3 } naturals like shape>>
288 ] unit-test
289
290 { t{ { 0.0 1.0 } { 2.0 3.0 } } } [
291     { { 0 1 } { 2 3 } } t{ } like
292 ] unit-test
293
294 ! test clone-like
295 { float-array{ 1.0 2.0 3.0 } } [
296     { 1 2 3 } t{ } clone-like vec>>
297 ] unit-test
298
299 { f } [
300     float-array{ 1.0 2.0 3.0 } dup t{ } clone-like
301     dup [ 5 1 ] dip set-nth vec>> =
302 ] unit-test
303
304 ! Test sum
305 { 21.0 } [
306     t{ 1 2 3 4 5 6 } sum
307 ] unit-test
308
309 { 50005000.0 } [
310     { 100 100 } naturals 1 t+ sum
311 ] unit-test
312
313 ! Test tensor parsing word
314 { float-array{ 1 2 3 4 5 6 7 8 } } [
315     t{ 1 2 3 4 5 6 7 8 } vec>>
316 ] unit-test
317
318 { { 8 } } [
319     t{ 1 2 3 4 5 6 7 8 } shape>>
320 ] unit-test
321
322 { float-array{ 1 2 3 4 5 6 7 8 } } [
323     t{ { 1 2 3 4 } { 5 6 7 8 } } vec>>
324 ] unit-test
325
326 { { 2 4 } } [
327     t{ { 1 2 3 4 } { 5 6 7 8 } } shape>>
328 ] unit-test
329
330 { float-array{ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 } } [
331     t{ { { 1 2 3 4 } { 5 6 7 8 } { 9 10 11 12 } } { { 13 14 15 16 } { 17 18 19 20 } { 21 22 23 24 } } } vec>>
332 ] unit-test
333
334 { { 2 3 4 } } [
335     t{ { { 1 2 3 4 } { 5 6 7 8 } { 9 10 11 12 } } { { 13 14 15 16 } { 17 18 19 20 } { 21 22 23 24 } } } shape>>
336 ] unit-test
337
338
339 ! Test addition
340 { float-array{ 1.0 2.0 3.0 4.0 } } [
341     { 4 } naturals { 4 } ones t+ vec>>
342 ] unit-test
343
344 { { 4 } } [
345     { 4 } naturals { 4 } ones t+ shape>>
346 ] unit-test
347
348 { float-array{ 1.0 2.0 3.0 4.0 } } [
349     { 2 2 } naturals { 2 2 } ones t+ vec>>
350 ] unit-test
351
352 { { 2 2 } } [
353     { 2 2 } naturals { 2 2 } ones t+ shape>>
354 ] unit-test
355
356 [
357     { 3 } naturals { 2 2 } ones t+ vec>>
358 ]
359 [ { 3 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
360
361 [
362     { 4 } naturals { 2 2 } ones t+ vec>>
363 ]
364 [ { 4 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
365
366 ! Test scalar addition
367 { float-array{ 1.0 2.0 3.0 4.0 } } [
368     { 4 } naturals 1 t+ vec>>
369 ] unit-test
370
371 { { 4 } } [
372     { 4 } naturals 1 t+ shape>>
373 ] unit-test
374
375 { float-array{ 1.0 2.0 3.0 4.0 } } [
376     1 { 4 } naturals t+ vec>>
377 ] unit-test
378
379 { { 4 } } [
380     1 { 4 } naturals t+ shape>>
381 ] unit-test
382
383 ! Test subtraction
384 { float-array{ -1.0 0.0 1.0 2.0 } } [
385     { 4 } naturals { 4 } ones t- vec>>
386 ] unit-test
387
388 { { 4 } } [
389     { 4 } naturals { 4 } ones t- shape>>
390 ] unit-test
391
392 { float-array{ -1.0 0.0 1.0 2.0 } } [
393     { 2 2 } naturals { 2 2 } ones t- vec>>
394 ] unit-test
395
396 { { 2 2 } } [
397     { 2 2 } naturals { 2 2 } ones t- shape>>
398 ] unit-test
399
400 [
401     { 3 } naturals { 2 2 } ones t- vec>>
402 ]
403 [ { 3 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
404
405 [
406     { 4 } naturals { 2 2 } ones t- vec>>
407 ]
408 [ { 4 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
409
410 ! Test scalar subtraction
411 { float-array{ -1.0 0.0 1.0 2.0 } } [
412     { 4 } naturals 1 t- vec>>
413 ] unit-test
414
415 { { 4 } } [
416     { 4 } naturals 1 t- shape>>
417 ] unit-test
418
419 { float-array{ 1.0 0.0 -1.0 -2.0 } } [
420     1 { 4 } naturals t- vec>>
421 ] unit-test
422
423 { { 4 } } [
424     1 { 4 } naturals t- shape>>
425 ] unit-test
426
427 ! Test multiplication
428 { float-array{ 0.0 1.0 4.0 9.0 } } [
429     { 4 } naturals { 4 } naturals t* vec>>
430 ] unit-test
431
432 { { 4 } } [
433     { 4 } naturals { 4 } naturals t* shape>>
434 ] unit-test
435
436 { float-array{ 0.0 1.0 4.0 9.0 } } [
437     { 2 2 } naturals { 2 2 } naturals t* vec>>
438 ] unit-test
439
440 { { 2 2 } } [
441     { 2 2 } naturals { 2 2 } naturals t* shape>>
442 ] unit-test
443
444 [
445     { 3 } naturals { 2 2 } naturals t* vec>>
446 ]
447 [ { 3 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
448
449 [
450     { 4 } naturals { 2 2 } naturals t* vec>>
451 ]
452 [ { 4 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
453
454 ! Test division
455 { t } [
456     { 4 } ones
457     { 4 } naturals { 4 } ones t+
458     t/ vec>>
459     { 1.0 0.5 0.33333 0.25 } v-
460     [ abs ] map
461     0 [ max ] reduce 0.0001 <
462 ] unit-test
463
464 { { 4 } } [
465     { 4 } ones
466     { 4 } naturals { 4 } ones t+
467     t/ shape>>
468 ] unit-test
469
470 { t } [
471     { 2 2 } ones
472     { 2 2 } naturals { 2 2 } ones t+
473     t/ vec>>
474     { 1.0 0.5 0.33333 0.25 } v-
475     [ abs ] map
476     0 [ max ] reduce 0.0001 <
477 ] unit-test
478
479 { { 2 2 } } [
480     { 2 2 } ones
481     { 2 2 } naturals { 2 2 } ones t+
482     t/ shape>>
483 ] unit-test
484
485 [
486     { 3 } ones
487     { 2 2 } naturals { 2 2 } ones t+
488     t/ vec>>
489 ]
490 [ { 3 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
491
492 [
493     { 4 } ones
494     { 2 2 } naturals { 2 2 } ones t+
495     t/ vec>>
496 ]
497 [ { 4 } { 2 2 } \ shape-mismatch-error boa = ] must-fail-with
498
499 ! Test scalar division
500 { t } [
501     1
502     { 4 } naturals { 4 } ones t+
503     t/ vec>>
504     { 1.0 0.5 0.33333 0.25 } v-
505     [ abs ] map
506     0 [ max ] reduce 0.0001 <
507 ] unit-test
508
509 { { 4 } } [
510     1
511     { 4 } naturals { 4 } ones t+
512     t/ shape>>
513 ] unit-test
514
515 { float-array{ 0.0 0.5 1.0 1.5 } } [
516     { 4 } naturals 2 t/ vec>>
517 ] unit-test
518
519 { { 4 } } [
520     { 4 } naturals 2 t/ shape>>
521 ] unit-test
522
523 ! Test scalar multiplication
524 { float-array{ 0.0 3.0 6.0 9.0 } } [
525     { 4 } naturals 3 t* vec>>
526 ] unit-test
527
528 { { 4 } } [
529     { 4 } naturals 3 t* shape>>
530 ] unit-test
531
532 { float-array{ 0.0 3.0 6.0 9.0 } } [
533     { 2 2 } naturals 3 t* vec>>
534 ] unit-test
535
536 { { 2 2 } } [
537     { 2 2 } naturals 3 t* shape>>
538 ] unit-test
539
540 { float-array{ 0.0 3.0 6.0 9.0 } } [
541     3 { 4 } naturals t* vec>>
542 ] unit-test
543
544 { { 4 } } [
545     3 { 4 } naturals t* shape>>
546 ] unit-test
547
548 { float-array{ 0.0 3.0 6.0 9.0 } } [
549     3 { 2 2 } naturals t* vec>>
550 ] unit-test
551
552 { { 2 2 } } [
553     3 { 2 2 } naturals t* shape>>
554 ] unit-test
555
556 ! test mod
557 { float-array{ 0.0 1.0 2.0 0.0 1.0 } } [
558     { 5 } naturals
559     { 5 } ones 3 t*
560     t% vec>>
561 ] unit-test
562
563 { { 5 } } [
564     { 5 } naturals
565     { 5 } ones 3 t*
566     t% shape>>
567 ] unit-test
568
569 { float-array{ 0.0 1.0 2.0 0.0 1.0 2.0 } } [
570     { 2 3 } naturals
571     { 2 3 } ones 3 t*
572     t% vec>>
573 ] unit-test
574
575 { { 2 3 } } [
576     { 2 3 } naturals
577     { 2 3 } ones 3 t*
578     t% shape>>
579 ] unit-test
580
581 [
582     { 4 } naturals
583     { 2 3 } ones 3 t*
584     t% vec>>
585 ]
586 [ { 4 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
587
588 [
589     { 4 } naturals
590     { 2 3 } ones 3 t*
591     t% vec>>
592 ]
593 [ { 4 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
594
595 ! Test scalar mod
596 { float-array{ 0.0 1.0 2.0 0.0 1.0 } } [
597     { 5 } naturals
598     3
599     t% vec>>
600 ] unit-test
601
602 { { 5 } } [
603     { 5 } naturals
604     3
605     t% shape>>
606 ] unit-test
607
608 { float-array{ 0.0 1.0 2.0 0.0 1.0 2.0 } } [
609     { 2 3 } naturals
610     3
611     t% vec>>
612 ] unit-test
613
614 { { 2 3 } } [
615     { 2 3 } naturals
616     3
617     t% shape>>
618 ] unit-test
619
620 { float-array{ 0.0 1.0 0.0 3.0 3.0 } } [
621     3
622     { 5 } naturals 1 t+
623     t% vec>>
624 ] unit-test
625
626 { { 5 } } [
627     { 5 } naturals
628     3
629     t% shape>>
630 ] unit-test
631
632 { float-array{ 0.0 1.0 0.0 3.0 3.0 3.0 } } [
633     3
634     { 2 3 } naturals 1 t+
635     t% vec>>
636 ] unit-test
637
638 { { 2 3 } } [
639     { 2 3 } naturals
640     3
641     t% shape>>
642 ] unit-test
643
644 ! t-concat
645 { t{ { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } } } [
646     { 2 3 } naturals dup 2array t-concat
647 ] unit-test
648
649 { t{ { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } { 0.0 1.0 2.0 } { 3.0 4.0 5.0 }
650      { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } } } [
651     { 2 3 } naturals dup dup 3array t-concat
652 ] unit-test
653
654 { t{ { 0.0 1.0 2.0 }
655      { 3.0 4.0 5.0 }
656      { 0.0 1.0 2.0 }
657      { 3.0 4.0 5.0 }
658      { 6.0 7.0 8.0 }
659      { 9.0 10.0 11.0 }
660      { 12.0 13.0 14.0 } } } [
661     { 2 3 } naturals { 5 3 } naturals 2array t-concat
662 ] unit-test
663
664 { t{ { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
665      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } }
666      { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
667      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } } } [
668      { 2 3 2 } naturals dup 2array t-concat
669 ] unit-test
670
671 { t{ { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
672      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } }
673      { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
674      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } }
675      { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
676      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } } } [
677      { 2 3 2 } naturals dup dup 3array t-concat
678 ] unit-test
679
680 [
681     { 2 2 } naturals { 2 3 } naturals 2array t-concat
682 ]
683 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
684
685 [
686     { 2 2 } naturals dup { 2 3 } naturals 3array t-concat
687 ]
688 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
689
690 ! stack
691 { t{ { { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } }
692      { { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } } } } [
693     { 2 3 } naturals dup 2array stack
694 ] unit-test
695
696 { t{ { { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } }
697      { { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } }
698      { { 0.0 1.0 2.0 } { 3.0 4.0 5.0 } } } } [
699     { 2 3 } naturals dup dup 3array stack
700 ] unit-test
701
702 { t{ { { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
703        { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } }
704      { { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
705        { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } } } } [
706      { 2 3 2 } naturals dup 2array stack
707 ] unit-test
708
709 { t{ { { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
710        { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } }
711      { { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
712        { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } }
713      { { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } }
714        { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } } } } } [
715      { 2 3 2 } naturals dup dup 3array stack
716 ] unit-test
717
718 [
719     { 2 2 } naturals { 2 3 } naturals 2array stack
720 ]
721 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
722
723 [
724     { 2 2 } naturals { 3 2 } naturals 2array stack
725 ]
726 [ { 2 2 } { 3 2 } \ shape-mismatch-error boa = ] must-fail-with
727
728 [
729     { 2 2 } naturals dup { 2 3 } naturals 3array stack
730 ]
731 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
732
733 [
734     { 2 2 } naturals dup { 3 2 } naturals 3array stack
735 ]
736 [ { 2 2 } { 3 2 } \ shape-mismatch-error boa = ] must-fail-with
737
738 ! hstack
739
740 { t{ { 0.0 1.0 2.0 3.0 1.0 }
741      { 4.0 5.0 6.0 7.0 1.0 }
742      { 8.0 9.0 10.0 11.0 1.0 } } } [
743     { 3 4 } naturals { 3 1 } ones 2array hstack
744 ] unit-test
745
746 { t{ { 0.0 1.0 2.0 3.0 1.0 0.0 0.0 }
747      { 4.0 5.0 6.0 7.0 1.0 0.0 0.0 }
748      { 8.0 9.0 10.0 11.0 1.0 0.0 0.0 } } } [
749     { 3 4 } naturals { 3 1 } ones { 3 2 } zeros 3array hstack
750 ] unit-test
751
752 { t{ { { 0.0 1.0 2.0 3.0 1.0 } { 4.0 5.0 6.0 7.0 1.0 } }
753      { { 8.0 9.0 10.0 11.0 1.0 } { 12.0 13.0 14.0 15.0 1.0 } } } } [
754      { 2 2 4 } naturals { 2 2 1 } ones 2array hstack
755 ] unit-test
756
757 { t{ { { 0.0 1.0 2.0 3.0 1.0 0.0 0.0 }
758        { 4.0 5.0 6.0 7.0 1.0 0.0 0.0 } }
759      { { 8.0 9.0 10.0 11.0 1.0 0.0 0.0 }
760        { 12.0 13.0 14.0 15.0 1.0 0.0 0.0 } } } } [
761      { 2 2 4 } naturals { 2 2 1 } ones { 2 2 2 } zeros 3array hstack
762 ] unit-test
763
764 [
765     { 2 2 } naturals { 3 2 } naturals 2array hstack
766 ]
767 [ { 2 2 } { 3 2 } \ shape-mismatch-error boa = ] must-fail-with
768
769 [
770     { 2 2 } naturals dup { 3 2 } naturals 3array hstack
771 ]
772 [ { 2 2 } { 3 2 } \ shape-mismatch-error boa = ] must-fail-with
773
774 [
775     { 2 2 2 } naturals { 3 2 2 } naturals 2array hstack
776 ]
777 [ { 2 2 2 } { 3 2 2 } \ shape-mismatch-error boa = ] must-fail-with
778
779 [
780     { 2 2 2 } naturals dup { 3 2 2 } naturals 3array hstack
781 ]
782 [ { 2 2 2 } { 3 2 2 } \ shape-mismatch-error boa = ] must-fail-with
783
784 ! vstack
785 { t{ { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } { 0.0 1.0 } } } [
786     { 3 2 } naturals { 1 2 } naturals 2array vstack
787 ] unit-test
788
789 { t{ { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 }
790      { 0.0 1.0 } { 0.0 1.0 } { 2.0 3.0 } } } [
791     { 3 2 } naturals { 1 2 } naturals { 2 2 } naturals 3array vstack
792 ] unit-test
793
794 { t{ { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 } { 0.0 1.0 } }
795      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 } { 2.0 3.0 } } } } [
796     { 2 3 2 } naturals { 2 1 2 } naturals 2array vstack
797 ] unit-test
798
799 { t{ { { 0.0 1.0 } { 2.0 3.0 } { 4.0 5.0 }
800        { 0.0 1.0 } { 0.0 1.0 } { 2.0 3.0 } }
801      { { 6.0 7.0 } { 8.0 9.0 } { 10.0 11.0 }
802        { 2.0 3.0 } { 4.0 5.0 } { 6.0 7.0 } } } } [
803     { 2 3 2 } naturals { 2 1 2 } naturals { 2 2 2 } naturals 3array vstack
804 ] unit-test
805
806 [
807     { 2 2 } naturals { 2 3 } naturals 2array vstack
808 ]
809 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
810
811 [
812     { 2 2 } naturals dup { 2 3 } naturals 3array vstack
813 ]
814 [ { 2 2 } { 2 3 } \ shape-mismatch-error boa = ] must-fail-with
815
816 [
817     { 2 2 2 } naturals { 3 2 2 } naturals 2array vstack
818 ]
819 [ { 2 2 2 } { 3 2 2 } \ shape-mismatch-error boa = ] must-fail-with
820
821 [
822     { 2 2 2 } naturals dup { 3 2 2 } naturals 3array vstack
823 ]
824 [ { 2 2 2 } { 3 2 2 } \ shape-mismatch-error boa = ] must-fail-with
825
826 ! test tensor>array
827 { { 0.0 0.0 } } [
828     { 2 } zeros tensor>array
829 ] unit-test
830
831 { { { 0.0 0.0 } { 0.0 0.0 } } } [
832     { 2 2 } zeros tensor>array
833 ] unit-test
834
835 { { { { 1.0 1.0 } { 1.0 1.0 } { 1.0 1.0 } }
836     { { 1.0 1.0 } { 1.0 1.0 } { 1.0 1.0 } } } } [
837     { 2 3 2 } ones tensor>array
838 ] unit-test
839
840 ! test >tensor
841 { t } [
842     { 2 3 4 } naturals dup tensor>array >tensor =
843 ] unit-test
844
845 { t } [
846     { { { 1.0 2.0 } { 3.0 4.0 } }
847       { { 5.0 6.0 } { 7.0 8.0 } }
848       { { 9.0 10.0 } { 11.0 12.0 } } }
849     dup >tensor tensor>array =
850 ] unit-test
851
852 { t } [
853     { 2 3 } naturals
854     { { 0 1 2 } { 3 4 5 } } >tensor =
855 ] unit-test
856
857 [
858     { { 1 2 } { 3 } } >tensor
859 ]
860 [ { { 1 2 } { 3 } } \ non-uniform-seq-error boa = ] must-fail-with
861
862 { float-array{ } } [
863     t{ } vec>>
864 ] unit-test
865
866 { { 0 } } [
867     t{ } shape>>
868 ] unit-test
869
870 ! test matmul
871 { float-array{ 70.0 76.0 82.0 88.0 94.0 190.0 212.0 234.0
872                256.0 278.0 310.0 348.0 386.0 424.0 462.0 } } [
873     { 3 4 } naturals { 4 5 } naturals matmul vec>>
874 ] unit-test
875
876 { { 3 5 } } [
877     { 3 4 } naturals { 4 5 } naturals matmul shape>>
878 ] unit-test
879
880 { float-array{ 70.0 76.0 82.0 88.0 94.0 190.0 212.0 234.0 256.0
881                278.0 310.0 348.0 386.0 424.0 462.0 1510.0 1564.0
882                1618.0 1672.0 1726.0 1950.0 2020.0 2090.0 2160.0
883                2230.0 2390.0 2476.0 2562.0 2648.0 2734.0 } } [
884     { 2 3 4 } naturals { 2 4 5 } naturals matmul vec>>
885 ] unit-test
886
887 { { 2 3 5 } } [
888     { 2 3 4 } naturals { 2 4 5 } naturals matmul shape>>
889 ] unit-test
890
891 { float-array{ 70.0 76.0 82.0 88.0 94.0 190.0 212.0 234.0 256.0
892     278.0 310.0 348.0 386.0 424.0 462.0 1510.0 1564.0 1618.0
893     1672.0 1726.0 1950.0 2020.0 2090.0 2160.0 2230.0 2390.0 2476.0
894     2562.0 2648.0 2734.0 4870.0 4972.0 5074.0 5176.0 5278.0 5630.0
895     5748.0 5866.0 5984.0 6102.0 6390.0 6524.0 6658.0 6792.0 6926.0
896     10150.0 10300.0 10450.0 10600.0 10750.0 11230.0 11396.0 11562.0
897     11728.0 11894.0 12310.0 12492.0 12674.0 12856.0 13038.0 } } [
898     { 2 2 3 4 } naturals { 2 2 4 5 } naturals matmul vec>>
899 ] unit-test
900
901 { { 2 2 3 5 } } [
902     { 2 2 3 4 } naturals { 2 2 4 5 } naturals matmul shape>>
903 ] unit-test
904
905 ! where n mod 4 is not 0 and m & p have same mod 4 val
906 { float-array{ 45.0 48.0 51.0 54.0 57.0 60.0 63.0 66.0 69.0 126.0
907     138.0 150.0 162.0 174.0 186.0 198.0 210.0 222.0 207.0 228.0
908     249.0 270.0 291.0 312.0 333.0 354.0 375.0 288.0 318.0 348.0
909     378.0 408.0 438.0 468.0 498.0 528.0 369.0 408.0 447.0 486.0
910     525.0 564.0 603.0 642.0 681.0 } } [
911     { 5 3 } naturals { 3 9 } naturals matmul vec>>
912 ] unit-test
913
914 ! where n mod 4 is not 0 and m & p have same mod 4 val
915 { float-array{ 270.0 280.0 290.0 300.0 310.0 320.0 330.0 340.0 350.0
916                720.0 755.0 790.0 825.0 860.0 895.0 930.0 965.0 1000.0
917                1170.0 1230.0 1290.0 1350.0 1410.0 1470.0 1530.0 1590.0
918                1650.0 1620.0 1705.0 1790.0 1875.0 1960.0 2045.0 2130.0
919                2215.0 2300.0 2070.0 2180.0 2290.0 2400.0 2510.0 2620.0
920                2730.0 2840.0 2950.0 } } [
921     { 5 5 } naturals { 5 9 } naturals matmul vec>>
922 ] unit-test
923
924 ! where n mod 4 is not 0 and m & p have different mod 4 vals
925 { float-array{ 35.0 38.0 41.0 44.0 47.0 50.0 53.0 98.0 110.0
926     122.0 134.0 146.0 158.0 170.0 161.0 182.0 203.0 224.0
927     245.0 266.0 287.0 224.0 254.0 284.0 314.0 344.0 374.0
928     404.0 287.0 326.0 365.0 404.0 443.0 482.0 521.0 } } [
929     { 5 3 } naturals { 3 7 } naturals matmul vec>>
930 ] unit-test
931
932 { float-array{ 546.0 567.0 588.0 609.0 630.0 651.0 1428.0 1498.0 1568.0 1638.0
933     1708.0 1778.0 2310.0 2429.0 2548.0 2667.0 2786.0 2905.0 3192.0 3360.0
934     3528.0 3696.0 3864.0 4032.0 4074.0 4291.0 4508.0 4725.0 4942.0 5159.0
935     4956.0 5222.0 5488.0 5754.0 6020.0 6286.0 5838.0 6153.0 6468.0 6783.0
936     7098.0 7413.0 22008.0 22372.0 22736.0 23100.0 23464.0 23828.0 24948.0
937     25361.0 25774.0 26187.0 26600.0 27013.0 27888.0 28350.0 28812.0 29274.0
938     29736.0 30198.0 30828.0 31339.0 31850.0 32361.0 32872.0 33383.0 33768.0
939     34328.0 34888.0 35448.0 36008.0 36568.0 36708.0 37317.0 37926.0 38535.0
940     39144.0 39753.0 39648.0 40306.0 40964.0 41622.0 42280.0 42938.0 } } [
941     { 2 7 7 } naturals { 2 7 6 } naturals matmul vec>>
942 ] unit-test
943
944 ! test transpose
945 { float-array{ 0.0 2.0 1.0 3.0 } } [
946     { 2 2 } naturals transpose vec>>
947 ] unit-test
948
949 { float-array{ 0.0 12.0 4.0 16.0 8.0 20.0 1.0
950     13.0 5.0 17.0 9.0 21.0 2.0 14.0 6.0 18.0
951     10.0 22.0 3.0 15.0 7.0 19.0 11.0 23.0 } } [
952     { 2 3 4 } naturals transpose vec>>
953 ] unit-test
954
955 { { 4 3 2 } } [
956     { 2 3 4 } naturals transpose shape>>
957 ] unit-test
958
959 { t } [
960     { 2 3 4 5 6 } naturals dup transpose transpose =
961 ] unit-test