test-slices.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ; Tests for (ploy slices)
  2. ; (c) Daniel Llorens - 2012-2014
  3. ; This library is free software; you can redistribute it and/or modify it under
  4. ; the terms of the GNU General Public License as published by the Free
  5. ; Software Foundation; either version 3 of the License, or (at your option) any
  6. ; later version.
  7. (import (srfi srfi-1) (srfi srfi-9) (srfi srfi-26) (ploy basic) (ploy slices)
  8. (ploy test) (ploy as-array))
  9. ; this might need to be elsewhere, but test-reduce.scm doesn't know cant.
  10. (define +/ (verb (cut over (lambda (x y) (ply + x y)) <>) #f '_))
  11. (define +/a (verb (cut folda + 0 <>) #f '_))
  12. (define +/b (verb (cut foldb + 0 <>) #f '_))
  13. ;; @TODO An example that is too slow b/c +/ results in many (ply + scalar scalar).
  14. (ply (w/rank +/ 1) (cant (i. 200) 21))
  15. (ply (w/rank +/a 1) (cant (i. 200) 21))
  16. (ply (w/rank +/b 1) (cant (i. 200) 21))
  17. ; ------------------------
  18. ; index
  19. ; ------------------------
  20. (T 0 (index (lambda (v) (= (v-norm v) 1)) #2((1 0 0) (1 2 0) (-.1 0 .3))))
  21. (T 1 (index (lambda (v) (> (v-norm v) 1)) #2((1 0 0) (1 2 0) (-.1 0 .3))))
  22. (T 2 (index (lambda (v) (< (v-norm v) 1)) #2((1 0 0) (1 2 0) (-.1 0 .3))))
  23. (ply (lambda (i) (index (cut = i <>) (i. 10))) #(0 1 2))
  24. ; -----------------------------
  25. ; max-by min-by sort. sort-by.
  26. ; -----------------------------
  27. (assert (= -2 (max-by '(-1 -2 2 1) (lambda (a b) (< (magnitude a) (magnitude b)))))
  28. "unstable sort")
  29. (assert (= -1 (min-by '(-2 -1 1 2) (lambda (a b) (< (magnitude a) (magnitude b)))))
  30. "unstable sort")
  31. (T (sort. (i. 3) >) #(2 1 0))
  32. (T (sort. #(2 1 0)) (i. 3))
  33. (T (sort-by. (i. 3 3) #(1 0 2)) #2((3 4 5) (0 1 2) (6 7 8)))
  34. (T (sort-by. (i. 3 3) #(1 0 2) >) #2((6 7 8) (0 1 2) (3 4 5)))
  35. (T (sort-indices-by. #(1 0 0 1 2) #(1.3 1.9 0.3) <)
  36. (sort-by. #(1 0 0 1 2) (from #(1.3 1.9 0.3) #(1 0 0 1 2)) <)
  37. #(2 0 0 1 1))
  38. (T (sort-indices-by. #(1 0 0 1 2) #(1.3 1.9 0.3) >)
  39. (sort-by. #(1 0 0 1 2) (from #(1.3 1.9 0.3) #(1 0 0 1 2)) >)
  40. #(1 1 0 0 2))
  41. ; -----------------------------
  42. ; ply with other functions.
  43. ; -----------------------------
  44. (define zxy (verb (cut from <> #(2 0 1)) '(3) 1))
  45. (T (ply zxy #2((0 1+1i 1) (1-1i 0 1))) #2((1 0 1+1i) (1 1-1i 0)))
  46. (T (ply zxy #2f64((0 1 2) (1 0 2))) #2f64((2 0 1) (2 1 0)))
  47. (T (ply zxy #2c64((0 1+1i 1) (1-1i 0 1))) #2c64((1 0 1+1i) (1 1-1i 0)))
  48. ; -----------------------------
  49. ; pile
  50. ; -----------------------------
  51. ; @TODO type output according to types of a.
  52. (define (pile a)
  53. "(pile a) Make array whose items are the elements of list a"
  54. (assert (not (null? a)))
  55. (let ((rank (rank (car a)))
  56. (type (array-type* (car a))))
  57. (if (zero? rank)
  58. (list->typed-array type 1 a)
  59. (let ((b (apply make-typed-array type *unspecified* (length a) ($ (car a)))))
  60. (let loop ((a a) (i 0))
  61. (cond ((null? a) b)
  62. (else
  63. (array-copy! (car a) (from b i))
  64. (loop (cdr a) (+ 1 i)))))))))
  65. (T (pile '(1 2 3)) #(1 2 3))
  66. (T (pile '(#(1 2) #(3 4) #(5 6))) #2((1 2) (3 4) (5 6)))
  67. ; ---------------------------------------------------------------------
  68. ; joining arrays.
  69. ; ---------------------------------------------------------------------
  70. ; @TODO signal arity error proc->verb
  71. ; (folda (verb (lambda (n a) (+ n (tally a))) '() '_) a)
  72. (T (raze #(#(1 2 3) #(a b) #(5 6 7 8) #() #(x) #(s t)))
  73. #(1 2 3 a b 5 6 7 8 x s t))
  74. (T (raze `#(,(i. 3 2) ,(i. 1 2) ,(i. 5 2) ,(i. 0 2)))
  75. #2((0 1) (2 3) (4 5) (0 1) (0 1) (2 3) (4 5) (6 7) (8 9)))
  76. ; -------------------------
  77. ; axis operations.
  78. ; -------------------------
  79. (T (reverse. (i. 3 2) 0) #2((4 5) (2 3) (0 1)))
  80. (T (reverse. (i. 3 2) 1) #2((1 0) (3 2) (5 4)))
  81. ; -----------------------------
  82. ; cant
  83. ; -----------------------------
  84. (T (cant (i. 9) 3 3)
  85. #2((0 1 2) (3 4 5) (6 7 8)))
  86. (T (cant (i. 9) 3 2)
  87. #2((0 1 2) (2 3 4) (4 5 6) (6 7 8)))
  88. (T (cant (i. 9) 3 1)
  89. #2((0 1 2) (1 2 3) (2 3 4) (3 4 5) (4 5 6) (5 6 7) (6 7 8)))
  90. (T (cant (i. 9) 2 2)
  91. #2((0 1) (2 3) (4 5) (6 7)))
  92. (T (cant (i. 9) 2 1)
  93. #2((0 1) (1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8)))
  94. ; -----------------------------
  95. ; roll
  96. ; -----------------------------
  97. (T (ply (w/rank (verb (cut roll 1 <>) #f '_) 1) (i. 5 5))
  98. #2((4 0 1 2 3) (9 5 6 7 8) (14 10 11 12 13) (19 15 16 17 18) (24 20 21 22 23)))
  99. ; -----------------------------
  100. ; index-of count. copy. copy-i. drop. remove-i. filter. indices
  101. ; -----------------------------
  102. ; J dyad (i.). This definition is irregular; not sure if I want to adopt it.
  103. (define (index-of a b)
  104. (if (> (rank a) (rank b))
  105. (if (array? b)
  106. (ply (verb (lambda (a) (index (cut array-equal? <> b) a)) #f (+ 1 (array-rank b))) a)
  107. (ply (verb (lambda (a) (index (cut equal? <> b) a)) #f 1) a))
  108. (if (> (rank a) 1)
  109. (ply (verb (lambda (a b) (index (cut array-equal? <> b) a)) #f '_ (+ -1 (array-rank a))) a b)
  110. (ply (verb (lambda (a b) (index (cut equal? <> b) a)) #f '_ (+ -1 (array-rank a))) a b))))
  111. ; direct cases.
  112. ; 3 1 4 1 5 9 i. 5
  113. (T 4 (index-of #(3 1 4 1 5 9) 5))
  114. ; (i. 4 3) i. 6 7 8
  115. (T 2 (index-of (i. 4 3) #(6 7 8)))
  116. ; reverse cases
  117. ; 3 1 4 1 5 9 i. 1 5
  118. (T #(1 4) (index-of #(3 1 4 1 5 9) #(1 5)))
  119. ; (i. 3 3) i. (i. 2 3)
  120. (T #(0 1) (index-of (i. 3 3) (i. 2 3)))
  121. ; 3 1 4 1 5 9 i. 8 4 _1
  122. (T #(#f 2 #f) (index-of #(3 1 4 1 5 9) #(8 4 -1)))
  123. ; cf., although wasteful (way faster to modify (make-array #f 10)
  124. (T (index-of (i. 10) #(3 4 7)) #(3 4 7))
  125. (T (index-of #(3 4 7) (i. 10)) #(#f #f #f 0 1 #f #f 2 #f #f))
  126. (T 0 (count. values #()))
  127. (T 2 (count. values #(#f #f #t #f #t #f)))
  128. (T 3 (count. (lambda (a) (apply < (vector->list a))) #2((0 1) (1 2) (3 2) (1 3) (9 2))))
  129. (T (copy. #(#f #f #t #f #t #f) (i. 6 2)) #2((4 5) (8 9)))
  130. (T (copy-i. #(0 1 0) (i. 6 3)) #2((0 1 2) (3 4 5)))
  131. (T (ply (verb array-cell-ref #f '_ 0) (i. 6 3) #(0 1 0)) #2((0 1 2) (3 4 5) (0 1 2)))
  132. (T (remove-i. #(0 2 4) (i. 6 3)) #2((3 4 5) (9 10 11) (15 16 17)))
  133. (T (drop. (i. 3 3)) #2((3 4 5) (6 7 8)))
  134. (T (drop. (i. 3 3) 2) #2((6 7 8)))
  135. ; filter.
  136. (T (filter. (lambda (v) (odd? (array-cell-ref v (- (tally v) 1))))
  137. (i. 4 3))
  138. #2((3 4 5) (9 10 11)))
  139. (T (filter. (lambda (v) (negative? (array-cell-ref v (- (tally v) 1))))
  140. (i. 4 3))
  141. (make-array 0 0 0)) ; @TODO #2() is buggy in my Guile branch
  142. (T (filter. positive? #(1 2 3 -4 5))
  143. #(1 2 3 5))
  144. ; filter-map.
  145. (T #(-3 -5) (filter-map. (lambda (x) (if (positive? x) (- x) #f)) #(-1 -1 +3 0 -4 5)))
  146. ; invert-index
  147. (T (invert-index #(3 7 1)) #(#f 2 #f 0 #f #f #f 1))
  148. (T (invert-index #(1 0 2)) #(1 0 2))
  149. (T (invert-index #(1 2 0)) #(2 0 1))
  150. ; @TODO More tests, argument should be verb
  151. (T #(2) (indices (lambda (a b) (positive? (+ a b))) #(1 2 3) #(-2 -2 -2)))
  152. ; -----------------------------
  153. ; deal @TODO the argument to ply is spurious.
  154. ; -----------------------------
  155. (define (deal w y)
  156. (ply (lambda (w) (array-cell-ref y (random (tally y)))) (reshape 1 w)))
  157. (deal 4 (i. 4))
  158. (define x (deal 1000 (i. 360)))
  159. ; -----------------------------
  160. ; conversion or casting.
  161. ; -----------------------------
  162. (T-eps 0. #c64(1+2i 3+1i 2+3i) (real->c64 #f64(1 2 3 1 2 3)))
  163. (T-eps 0. (complex->f64 #c64(1+2i 3+1i 2+3i)) #f64(1 2 3 1 2 3))
  164. ; -----------------------
  165. ; array-map-rows-1!
  166. ; -----------------------
  167. ; deprecated test; array-map-rows-1! isn't used anymore.
  168. ; only to check performance of ply against manual looping.
  169. (define-syntax repeat-i
  170. (syntax-rules ()
  171. ((_ (i n) e0 ...) (do ((i 0 (+ i 1))) ((= i n)) e0 ...))))
  172. (define (array-map-rows-1! dst f src0)
  173. "(array-map-rows-1! dst f src) - map over rows of src0 to rows of dst."
  174. (case (array-rank dst)
  175. ((2) (repeat-i (i (tally dst))
  176. (array-copy! (f (array-cell-ref src0 i)) (array-cell-ref dst i))))
  177. ((1) (array-index-map! dst (lambda (i) (f (array-cell-ref src0 i)))))
  178. (else (error (format #f "bad rank of destination ~a" (array-rank dst))))))
  179. (define (sphere-points . dims)
  180. (let ((a (make-random-array dims)))
  181. (ply / a (ply vnorm. a)))) ; @TODO typical need of fusion
  182. (define r (sphere-points 100 3))
  183. (define r0 (array-copy 'f64 r))
  184. (define r1 (array-copy 'f64 r))
  185. (define r2 (array-copy 'f64 r))
  186. (define r3 (array-copy 'f64 r))
  187. (array-map! r0 (cut * 2 <>) r0)
  188. (array-map-rows-1! r1 (cut array-map #t (cut * <> 2) <>) r1)
  189. (assert (zero? (max (compare-arrays r0 r1))) "bad array-map-rows-1!")
  190. ; array-map-rows-1! when the destination has rank 1.
  191. (define r0 #(1.73205080756888 3.46410161513775 5.19615242270663))
  192. (define r1 (array-copy #f64(0 0 0)))
  193. (array-map-rows-1! r1 v-norm #2f64((1 1 1) (2 2 2) (3 3 3)))
  194. (assert (> 5e-15 (max (compare-arrays r0 r1))) "bad array-map-rows-1!")
  195. (define r0 (as-array r #:type 'f64))
  196. (define vr0 (make-typed-array 'f64 0. ($. r0 0)))
  197. (array-map-rows-1! vr0 v-norm r)
  198. (assert (> 6e-15 (max (compare-arrays (reshape 1. (tally r0)) vr0))))
  199. (define r (sphere-points 1000 3))
  200. (define r0 (as-array r #:type 'f64))
  201. (define vr1 (make-typed-array 'f64 0. (tally r0)))
  202. (array-map-rows-1! vr1 v-norm r)
  203. (define vr2 (ply (verb v-norm '() 1) r))
  204. (T-eps 1e-15 (reshape 1. (tally r0)) vr1 vr2)
  205. ; -----------------------------
  206. ; reductions, @TODO to be filed in (ploy reduce).
  207. ; -----------------------------
  208. (T 4 (every. values #(1 2 3 4)))
  209. (T #t (every. positive? #(1 2 3 4)))
  210. (T 1 (any. values #(1 2 3 4)))
  211. (T #t (any. negative? #(1 2 -3 4)))
  212. ; @wish ((w/rank (cut every. positive? <>) 1) (i. 10 3))
  213. (T (ply (verb (cut every. positive? <>) '() 1) (i. 10 3))
  214. #(#f #t #t #t #t #t #t #t #t #t))
  215. (T #t (every. values #(#t #t)))
  216. (T #f (every. values #(#t #f)))
  217. (T #f (every. values #(#f #t)))
  218. (T #t (every. values #(#t)))
  219. (T #f (every. values #(#f)))
  220. (T #t (every. values #()))
  221. ; -----------------------------
  222. ; tile
  223. ; -----------------------------
  224. (T (tile (i. 2 3) 3 1) #2((0 1 2) (3 4 5) (0 1 2) (3 4 5) (0 1 2) (3 4 5)))
  225. (T (tile (i. 2 3) 1 3) #2((0 1 2 0 1 2 0 1 2) (3 4 5 3 4 5 3 4 5)))
  226. (T (tile (i. 2 3) 0 3) #2:0:9())
  227. (T (tile (i. 2 3) 3 0) #2(() () () () () ()))
  228. (T (tile #(#(1 2) #(3)) 3) #(#(1 2) #(3) #(1 2) #(3) #(1 2) #(3)))
  229. (T (tile #2((#(1 2) #(3)) (#(4) #(5 6))) 2 3)
  230. #2((#(1 2) #(3) #(1 2) #(3) #(1 2) #(3))
  231. (#(4) #(5 6) #(4) #(5 6) #(4) #(5 6))
  232. (#(1 2) #(3) #(1 2) #(3) #(1 2) #(3))
  233. (#(4) #(5 6) #(4) #(5 6) #(4) #(5 6))))
  234. (T (tile (cat 1 (i. 3 1 2 2) (i. 3 1 2 2)) 1 1 1 1)
  235. (tile (i. 3 1 2 2) 1 2 1 1))
  236. (T (tile (cat 2 (i. 1 3 1 2) (i. 1 3 1 2)) 1 1 1 1)
  237. (tile (i. 1 3 1 2) 1 1 2 1))
  238. (T (tile (cat 2 (i. 1 3 1 2 2) (i. 1 3 1 2 2)) 1 1 1 1 1)
  239. (tile (i. 1 3 1 2 2) 1 1 2 1 1))
  240. (T (tile (i. 2 3) 2)
  241. (tile (i. 2 3) 2 1)
  242. (cat 0 (i. 2 3) (i. 2 3))
  243. #2((0 1 2) (3 4 5) (0 1 2) (3 4 5)))
  244. ; -----------------------------
  245. ; bits
  246. ; -----------------------------
  247. (display "\ndone.\n") (force-output)