;; lib/apl/tests/structural.sx — Phase 3: structural primitives ;; Tests for: apl-reshape, apl-ravel, apl-transpose, apl-transpose-dyadic ;; Loaded after runtime.sx; shares apl-test / apl-test-pass / apl-test-fail. (define rv (fn (arr) (get arr :ravel))) (define sh (fn (arr) (get arr :shape))) ;; --------------------------------------------------------------------------- ;; 1. Ravel (monadic ,) ;; --------------------------------------------------------------------------- (apl-test "ravel scalar" (rv (apl-ravel (apl-scalar 5))) (list 5)) (apl-test "ravel vector" (rv (apl-ravel (make-array (list 3) (list 1 2 3)))) (list 1 2 3)) (apl-test "ravel matrix" (rv (apl-ravel (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 1 2 3 4 5 6)) (apl-test "ravel shape is rank-1" (sh (apl-ravel (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 6)) ;; --------------------------------------------------------------------------- ;; 2. Reshape (dyadic ⍴) ;; --------------------------------------------------------------------------- (apl-test "reshape 2x3 ravel" (rv (apl-reshape (make-array (list 2) (list 2 3)) (make-array (list 6) (list 1 2 3 4 5 6)))) (list 1 2 3 4 5 6)) (apl-test "reshape 2x3 shape" (sh (apl-reshape (make-array (list 2) (list 2 3)) (make-array (list 6) (list 1 2 3 4 5 6)))) (list 2 3)) (apl-test "reshape cycle 6 from 1 2" (rv (apl-reshape (make-array (list 1) (list 6)) (make-array (list 2) (list 1 2)))) (list 1 2 1 2 1 2)) (apl-test "reshape cycle 2x3 from 1 2" (rv (apl-reshape (make-array (list 2) (list 2 3)) (make-array (list 2) (list 1 2)))) (list 1 2 1 2 1 2)) (apl-test "reshape scalar fill" (rv (apl-reshape (make-array (list 1) (list 4)) (apl-scalar 7))) (list 7 7 7 7)) (apl-test "reshape truncate" (rv (apl-reshape (make-array (list 1) (list 3)) (make-array (list 6) (list 10 20 30 40 50 60)))) (list 10 20 30)) (apl-test "reshape matrix to vector" (sh (apl-reshape (make-array (list 1) (list 6)) (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 6)) (apl-test "reshape 2x2x3" (sh (apl-reshape (make-array (list 3) (list 2 2 3)) (make-array (list 12) (range 1 13)))) (list 2 2 3)) (apl-test "reshape to empty" (rv (apl-reshape (make-array (list 1) (list 0)) (make-array (list 3) (list 1 2 3)))) (list)) ;; --------------------------------------------------------------------------- ;; 3. Monadic transpose (⍉) ;; --------------------------------------------------------------------------- (apl-test "transpose scalar shape" (sh (apl-transpose (apl-scalar 99))) (list)) (apl-test "transpose scalar ravel" (rv (apl-transpose (apl-scalar 99))) (list 99)) (apl-test "transpose vector shape" (sh (apl-transpose (make-array (list 3) (list 3 1 4)))) (list 3)) (apl-test "transpose vector ravel" (rv (apl-transpose (make-array (list 3) (list 3 1 4)))) (list 3 1 4)) (apl-test "transpose 2x3 shape" (sh (apl-transpose (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 3 2)) (apl-test "transpose 2x3 ravel" (rv (apl-transpose (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 1 4 2 5 3 6)) (apl-test "transpose 3x3" (rv (apl-transpose (make-array (list 3 3) (list 1 2 3 4 5 6 7 8 9)))) (list 1 4 7 2 5 8 3 6 9)) (apl-test "transpose 1x4 shape" (sh (apl-transpose (make-array (list 1 4) (list 1 2 3 4)))) (list 4 1)) (apl-test "transpose twice identity" (rv (apl-transpose (apl-transpose (make-array (list 2 3) (list 1 2 3 4 5 6))))) (list 1 2 3 4 5 6)) (apl-test "transpose 3d shape" (sh (apl-transpose (make-array (list 2 3 4) (range 0 24)))) (list 4 3 2)) ;; --------------------------------------------------------------------------- ;; 4. Dyadic transpose (perm⍉arr) ;; --------------------------------------------------------------------------- (apl-test "dyadic-transpose identity" (rv (apl-transpose-dyadic (make-array (list 2) (list 1 2)) (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 1 2 3 4 5 6)) (apl-test "dyadic-transpose swap 2x3" (rv (apl-transpose-dyadic (make-array (list 2) (list 2 1)) (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 1 4 2 5 3 6)) (apl-test "dyadic-transpose swap shape" (sh (apl-transpose-dyadic (make-array (list 2) (list 2 1)) (make-array (list 2 3) (list 1 2 3 4 5 6)))) (list 3 2)) (apl-test "dyadic-transpose 3d shape" (sh (apl-transpose-dyadic (make-array (list 3) (list 2 1 3)) (make-array (list 2 3 4) (range 0 24)))) (list 3 2 4))