1 ! Copyright (C) 2005, 2010, 2018, 2020 Slava Pestov, Joe Groff, and Cat Stevens.
2 USING: arrays assocs combinators.short-circuit grouping kernel
3 math math.statistics math.vectors sequences sequences.deep
8 : call-eq? ( obj quots -- ? )
9 [ call( x -- x ) ] with map all-eq? ; ! inline
11 ! ------------------------
14 { t } [ { } regular-matrix? ] unit-test
15 { t } [ { { } } regular-matrix? ] unit-test
16 { t } [ { { 1 2 } } regular-matrix? ] unit-test
17 { t } [ { { 1 2 } { 3 4 } } regular-matrix? ] unit-test
18 { t } [ { { 1 } { 3 } } regular-matrix? ] unit-test
19 { f } [ { { 1 2 } { 3 } } regular-matrix? ] unit-test
20 { f } [ { { 1 } { 3 2 } } regular-matrix? ] unit-test
23 { t } [ { } square-matrix? ] unit-test
24 { t } [ { { 1 } } square-matrix? ] unit-test
25 { t } [ { { 1 2 } { 3 4 } } square-matrix? ] unit-test
26 { f } [ { { 1 } { 2 3 } } square-matrix? ] unit-test
27 { f } [ { { 1 2 } } square-matrix? ] unit-test
29 ! any deep-empty matrix is null
30 ! it doesn't make any sense for { } to be null while { { } } to be considered nonnull
36 { { { } } { { { } } } }
37 } [ null-matrix? ] all?
44 { { { 1 } } { 2 } { } }
45 } [ null-matrix? ] any?
48 { t } [ 10 dup <zero-matrix> zero-matrix? ] unit-test
49 { t } [ 10 10 15 <simple-eye> zero-matrix? ] unit-test
50 { t } [ 0 dup <zero-matrix> null-matrix? ] unit-test
51 { f } [ 0 dup <zero-matrix> zero-matrix? ] unit-test
52 { f } [ 4 <identity-matrix> zero-matrix? ] unit-test
53 ! make sure we're not using the sum-to-zero strategy
54 { f } [ { { 0 -2 } { 1 -1 } } zero-matrix? ] unit-test
55 { f } [ { { 0 0 } { 1 -1 } } zero-matrix? ] unit-test
56 { f } [ { { 0 1 } { 0 -1 } } zero-matrix? ] unit-test
60 { 3 } [ { 1 2 3 } 0 swap nth-end ] unit-test
61 { 2 } [ { 1 2 3 } 1 swap nth-end ] unit-test
62 { 1 } [ { 1 2 3 } 2 swap nth-end ] unit-test
64 [ { 1 2 3 } -1 swap nth-end ] [ bounds-error? ] must-fail-with
65 [ { 1 2 3 } 3 swap nth-end ] [ bounds-error? ] must-fail-with
66 [ { 1 2 3 } 4 swap nth-end ] [ bounds-error? ] must-fail-with
68 { { 0 0 1 } } [ { 0 0 0 } dup 1 0 rot set-nth-end ] unit-test
69 { { 0 2 0 } } [ { 0 0 0 } dup 2 1 rot set-nth-end ] unit-test
70 { { 3 0 0 } } [ { 0 0 0 } dup 3 2 rot set-nth-end ] unit-test
72 [ { 0 0 0 } dup 1 -1 rot set-nth-end ] [ bounds-error? ] must-fail-with
73 [ { 0 0 0 } dup 2 3 rot set-nth-end ] [ bounds-error? ] must-fail-with
74 [ { 0 0 0 } dup 3 4 rot set-nth-end ] [ bounds-error? ] must-fail-with
81 } } [ 2 2 5 <matrix> ] unit-test
84 { { -1 -1 } { -1 -1 } }
85 { { -1 -1 } { -1 -1 } }
86 { { -1 -1 } { -1 -1 } }
88 { { -1 -1 } { -1 -1 } }
89 { { -1 -1 } { -1 -1 } }
90 { { -1 -1 } { -1 -1 } }
91 } } } [ 2 3 2 2 -1 <matrix> <matrix> ] unit-test
96 } } [ 2 2 [ 5 ] <matrix-by> ] unit-test
100 } } [ 2 2 [ 3 2 * ] <matrix-by> ] unit-test
105 } } [ 2 3 [ + ] <matrix-by-indices> ] unit-test
110 } } [ 3 3 [ * ] <matrix-by-indices> ] unit-test
112 { t } [ 3 3 <zero-matrix> zero-square-matrix? ] unit-test
113 { t } [ 3 <zero-square-matrix> zero-square-matrix? ] unit-test
114 { t f } [ 3 1 <zero-matrix> [ zero-matrix? ] [ square-matrix? ] bi ] unit-test
121 { 1 2 3 } <diagonal-matrix>
129 } } [ { -11 -12 -33 -14 } <diagonal-matrix> ] unit-test
135 } } [ { 1 2 3 } <anti-diagonal-matrix> ] unit-test
142 } } [ { -11 -12 -33 -14 } <anti-diagonal-matrix> ] unit-test
228 { { 0 0 } { 0 1 } { 0 2 } }
229 { { 1 0 } { 1 1 } { 1 2 } }
230 { { 2 0 } { 2 1 } { 2 2 } }
231 { { 3 0 } { 3 1 } { 3 2 } }
232 } } [ { 4 3 } <coordinate-matrix> ] unit-test
237 } } [ 2 <square-rows> ] unit-test
242 } } [ 2 <square-cols> ] unit-test
247 } } [ { 5 6 } <square-rows> ] unit-test
252 } } [ { 5 6 } <square-cols> ] unit-test
258 } <square-rows> ] unit-test
266 } <square-rows> ] unit-test
275 } <square-rows> ] unit-test
309 { { 3 4 } } [ { { 1 0 } { 0 1 } } { 3 4 } mdotv ] unit-test
310 { { 4 3 } } [ { { 0 1 } { 1 0 } } { 3 4 } mdotv ] unit-test
312 { { { 6 } } } [ { { 3 } } { { 2 } } mdot ] unit-test
313 { { { 11 } } } [ { { 1 3 } } { { 5 } { 2 } } mdot ] unit-test
317 { { 1 } { 2 } { 3 } }
322 [ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l1-norm ] unit-test
325 [ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l-infinity-norm ] unit-test
328 [ { { 1 1 } { 1 1 } } matrix-l2-norm ] unit-test
333 { { 1 2 } { 3 4 } } matrix-l2-norm
339 { { 1 2 } { 4 8 } { 16 32 } } matrix-l2-norm
342 ! equivalent to frobenius for p = q = 2
344 [ { { 1 1 } { 1 1 } } 2 2 matrix-p-q-norm ] unit-test
349 { { 1 2 } { 4 8 } { 16 32 } } 3 matrix-p-norm-entrywise
352 { { { -1 0 } { 0 0 } } }
353 [ { { -2 0 } { 0 0 } } matrix-normalize ] unit-test
355 { { { -1 0 } { 0 1/2 } } }
356 [ { { -2 0 } { 0 1 } } matrix-normalize ] unit-test
359 [ 3 3 <zero-matrix> dup matrix-normalize = ] unit-test
364 { { 1 1 1 1 } } [ 4 <identity-matrix> main-diagonal ] unit-test
365 { { 0 0 0 0 } } [ 4 <identity-matrix> anti-diagonal ] unit-test
366 { { 4 8 } } [ { { 4 6 } { 3 8 } } main-diagonal ] unit-test
367 { { 6 3 } } [ { { 4 6 } { 3 8 } } anti-diagonal ] unit-test
368 { { 1 2 3 } } [ { { 0 0 1 } { 0 2 0 } { 3 0 0 } } anti-diagonal ] unit-test
369 { { 1 2 3 4 } } [ { 1 2 3 4 } <diagonal-matrix> main-diagonal ] unit-test
372 { { 1 2 3 4 } } [ { 1 2 3 4 } <diagonal-matrix> transpose main-diagonal ] unit-test
373 { t } [ 50 <identity-matrix> dup transpose = ] unit-test
374 { { 4 3 2 1 } } [ { 1 2 3 4 } <anti-diagonal-matrix> transpose anti-diagonal ] unit-test
384 } transpose ] unit-test
387 { { 1 2 3 4 } } [ { 1 2 3 4 } <anti-diagonal-matrix> anti-transpose anti-diagonal ] unit-test
388 { t } [ 50 <iota> <anti-diagonal-matrix> dup anti-transpose = ] unit-test
389 { { 4 3 2 1 } } [ { 1 2 3 4 } <diagonal-matrix> anti-transpose main-diagonal ] unit-test
399 } anti-transpose ] unit-test
402 SYMBOLS: A B C D E F G H I J K L M N O P ;
427 [ rows-except ] map-index
430 { { { 2 } } } [ { { 1 } { 2 } } 0 rows-except ] unit-test
431 { { { 1 } } } [ { { 1 } { 2 } } 1 rows-except ] unit-test
432 { { } } [ { { 1 } } 0 rows-except ] unit-test
433 { { { 1 } } } [ { { 1 } } 1 rows-except ] unit-test
442 } { 1 3 } rows-except ] unit-test
471 [ cols-except ] map-index
474 { { } } [ { { 1 } { 2 } } 0 cols-except ] unit-test
475 { { { 1 } { 2 } } } [ { { 1 } { 2 } } 1 cols-except ] unit-test
476 { { } } [ { { 1 } } 0 cols-except ] unit-test
477 { { { 1 } } } [ { { 1 } } 1 cols-except ] unit-test
478 { { { 2 } { 4 } } } [ { { 1 2 } { 3 4 } } 0 cols-except ] unit-test
479 { { { 1 } { 3 } } } [ { { 1 2 } { 3 4 } } 1 cols-except ] unit-test
490 } { 1 3 } cols-except ] unit-test
515 [ dup 2array matrix-except ] map-index
518 ! prepare for bracket hell
519 ! going to test the Matrix of Minors permutation strategy
521 ! going to test 1x2 inputs
522 ! the input had 2 elements, the output has 2 0-matrices across 2 arrays ;)
523 { { { { } { } } } } [ { { 1 2 } } matrix-except-all ] unit-test
525 ! any matrix with a 1 in its dimensions will give a void matrix output
526 { t } [ { { 1 2 } } matrix-except-all null-matrix? ] unit-test
527 { t } [ { { 1 } { 2 } } matrix-except-all null-matrix? ] unit-test
529 ! going to test 2x2 inputs
530 ! these 1x1 output matrices have omitted a row and column from the 2x2 input
532 ! the input had 4 elements, the output has 4 1-matrices across 2 arrays
533 ! the permutations of indices 0 1 are: 0 0, 0 1, 1 0, 1 1
536 { ! item #1: excluding row 0...
537 { { 3 } } ! and col 0 = 0 0
538 { { 2 } } ! and col 1 = 0 1
540 { ! item #2: excluding row 1...
541 { { 1 } } ! and col 0 = 1 0
542 { { 0 } } ! and col 1 = 1 1
546 ! the input to the function is a simple 2x2
547 { { 0 1 } { 2 3 } } matrix-except-all
550 ! we are going to ensure that "duplicate" matrices are not omitted in the output
562 } [ { { 0 0 } { 0 0 } } matrix-except-all ] unit-test
563 ! the output only has elements from the input
564 { t } [ 44 <zero-square-matrix> matrix-except-all zero-matrix? ] unit-test
566 ! going to test 2x3 and 3x2 inputs
570 { { 2 } { 3 } } ! and col 0
571 { { 1 } { 2 } } ! and col 1
574 { { 1 } { 3 } } ! and col 0
575 { { 0 } { 2 } } ! and col 1
578 { { 1 } { 2 } } ! col 0
579 { { 0 } { 1 } } ! col 1
586 } matrix-except-all ] unit-test
604 } matrix-except-all ] unit-test
606 ! going to test 3x3 inputs
608 ! the input had 9 elements, the output has 9 2-matrices across 3 arrays
609 ! every element from the input is represented 4 times in the output
610 ! the number of copies of each element found in the output is the side length of the next smaller square matrix
611 ! 3x3 input gives 4 copies of each element; (N-1) ^ 2 = 4 where N=3
612 ! the permutations of indices 0 1 2 are: 0 0, 0 1, 0 2; 1 0, 1 1, 1 2; 2 0, 2 1, 2 2
615 { ! item #1: excluding row 0...
630 { ! item #2: excluding row 1...
645 { ! item #2: excluding row 2...
665 } matrix-except-all dup flatten sorted-histogram values
666 { [ length 9 = ] [ [ 4 = ] all? ] }
670 ! going to test 4x4 inputs
672 ! don't feel like handwriting this right now, so a sanity check test instead
673 ! the input contains 4 rows and 4 columns for 16 elements
674 ! 4x4 input gives 9 copies of each element; (N-1) ^ 2 = 9 where N = 4
680 } matrix-except-all flatten sorted-histogram values
681 { [ length 16 = ] [ [ 9 = ] all? ] }