everything.scm 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. ; -*- mode: scheme; coding: utf-8 -*-
  2. ; Replacement for Guile C-based array system - Tests
  3. ; (c) Daniel Llorens - 2016-2021
  4. ; This library is free software; you can redistribute it and/or modify it under
  5. ; the terms of the GNU General Public License as published by the Free
  6. ; Software Foundation; either version 3 of the License, or (at your option) any
  7. ; later version.
  8. ; Run with $GUILE -L mod test/test.scm
  9. (import (newra) (newra tools) (newra read) (test misc) (test map-ladder)
  10. (srfi srfi-64) (srfi srfi-26) (srfi srfi-8)
  11. (only (srfi srfi-1) fold iota drop)
  12. (ice-9 format) (ice-9 match) (only (rnrs base) vector-map vector-for-each))
  13. (define (ra->string ra) (call-with-output-string (cut display ra <>)))
  14. (define (string->ra s) (call-with-input-string s read))
  15. (set! test-log-to-file #f)
  16. (test-begin "newra")
  17. ; -----------------------
  18. ; auxiliary functions
  19. ; -----------------------
  20. (test-equal
  21. 6 ((@ (newra vector) vector-fold) (lambda (a c) (+ c (car a))) 0 #((1) (2) (3))))
  22. (test-equal
  23. #(2 3) (vector-copy #(1 2 3 4) 1 3)) ; in Guile 3.0.8
  24. ; loop-fun from (test misc) FIXME may become ra-index-map!
  25. (define ra0 (make-ra-root (make-aseq) (c-dims)))
  26. (define ra1 (make-ra-root (make-aseq) (c-dims 2)))
  27. (define ra2 (make-ra-root (make-aseq) (c-dims 2 3)))
  28. (define ra3 (make-ra-root (make-aseq) (c-dims 2 3 4)))
  29. (test-assert (ra-equal? (make-ra-root #(1 2 3)) (list->ra 1 '(1 2 3))))
  30. (test-assert (not (ra-equal? (make-ra 0 3 4) (make-ra 0 3)))) ; ra-equal? doesn't rank-extend
  31. (test-equal "0"
  32. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra0)) 0 () (display (ra0))))))
  33. (test-equal "01"
  34. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra1)) 1 (i) (display (+ i))))))
  35. (test-equal "012123"
  36. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra2)) 2 (i j) (display (+ i j))))))
  37. (test-equal "012312342345123423453456"
  38. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra3)) 3 (i j k) (display (+ i j k))))))
  39. (define a0 (ra->array (as-ra ra0 #:type #t)))
  40. (define a1 (ra->array (as-ra ra1 #:type #t)))
  41. (define a2 (ra->array (as-ra ra2 #:type #t)))
  42. (define a3 (ra->array (as-ra ra3 #:type #t)))
  43. (test-equal "(0)"
  44. (call-with-output-string (lambda (o) (ra-loop ra0 (lambda () (format o "(~a)" (ra0)))))))
  45. (test-equal "(0)(1)"
  46. (call-with-output-string (lambda (o) (ra-loop ra1 (lambda (i) (format o "(~a)" (ra1 i)))))))
  47. (test-equal "(0)(1)(2)(3)(4)(5)"
  48. (call-with-output-string (lambda (o) (ra-loop ra2 (lambda (i j) (format o "(~a)" (ra2 i j)))))))
  49. (test-equal "(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)(21)(22)(23)"
  50. (call-with-output-string (lambda (o) (ra-loop ra3 (lambda (i j k) (format o "(~a)" (ra3 i j k)))))))
  51. (test-equal "(0)"
  52. (call-with-output-string (lambda (o) (array-loop a0 (lambda () (format o "(~a)" (array-ref a0)))))))
  53. (test-equal "(0)(1)"
  54. (call-with-output-string (lambda (o) (array-loop a1 (lambda (i) (format o "(~a)" (array-ref a1 i)))))))
  55. (test-equal "(0)(1)(2)(3)(4)(5)"
  56. (call-with-output-string (lambda (o) (array-loop a2 (lambda (i j) (format o "(~a)" (array-ref a2 i j)))))))
  57. (test-equal "(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)(21)(22)(23)"
  58. (call-with-output-string (lambda (o) (array-loop a3 (lambda (i j k) (format o "(~a)" (array-ref a3 i j k)))))))
  59. ; -----------------------
  60. ; array->ra, ra->array, printers
  61. ; -----------------------
  62. (let* ((ra0 (array->ra #(1 2 3)))
  63. (ra1 (array->ra #@1(1 2 3)))
  64. (ra2 (array->ra #2((1 2) (3 4))))
  65. (ra3 (array->ra #2@1@1((1 2) (3 4))))
  66. (ra4 (array->ra #0(99)))
  67. (ra5 (array->ra #0s64(99)))
  68. (ra6 (array->ra #0((a)))))
  69. (test-equal (ra->string ra0) "#%1:3(1 2 3)")
  70. (test-equal (ra->string ra1) "#%1@1:3(1 2 3)")
  71. (test-equal (ra->string ra2) "#%2:2:2((1 2) (3 4))")
  72. (test-equal (ra->string ra3) "#%2@1:2@1:2((1 2) (3 4))")
  73. (parameterize ((*ra-parenthesized-rank-zero* #t))
  74. (test-equal (ra->string ra4) "#%0(99)")
  75. (test-equal (ra->string ra5) "#%0s64(99)")
  76. (test-equal (ra->string ra6) "#%0((a))"))
  77. (parameterize ((*ra-parenthesized-rank-zero* #f))
  78. (test-equal (ra->string ra4) "#%0 99")
  79. (test-equal (ra->string ra5) "#%0s64 99")
  80. (test-equal (ra->string ra6) "#%0 (a)"))
  81. (test-equal #(1 2 3) (ra->array ra0))
  82. (test-equal #@1(1 2 3) (ra->array ra1))
  83. (test-equal #2((1 2) (3 4)) (ra->array ra2))
  84. (test-equal #2@1@1((1 2) (3 4)) (ra->array ra3))
  85. (test-equal #0(99) (ra->array ra4))
  86. (test-equal #0s64(99) (ra->array ra5))
  87. (test-equal #0((a)) (ra->array ra6)))
  88. ; dead axes
  89. (test-equal "#%2d:d:10((0 1 2 3 4 5 6 7 8 9))" (ra->string (ra-transpose (ra-i 10) 1)))
  90. ; printing of string elements (for example).
  91. (test-equal "#%0(\"hello\")" (ra->string (make-ra "hello")))
  92. (test-equal "#%1:2(\"hello\" \"bye\")" (ra->string (make-ra-root #("hello" "bye") (c-dims 2))))
  93. ; -----------------------
  94. ; ra-iota, ra-i
  95. ; -----------------------
  96. (test-equal "#%3d:2:3:4(((0 1 2 3) (4 5 6 7) (8 9 10 11)) ((12 13 14 15) (16 17 18 19) (20 21 22 23)))"
  97. (ra->string (ra-i 2 3 4)))
  98. (test-equal "#%3d:2:0:4(() ())"
  99. (ra->string (ra-i 2 0 4)))
  100. (test-equal "#%0d(0)"
  101. (ra->string (ra-i)))
  102. (test-equal "#%1d:4(9 8 7 6)"
  103. (ra->string (ra-iota 4 9 -1)))
  104. (test-equal "#%1d:4(9 10 11 12)"
  105. (ra->string (ra-iota 4 9)))
  106. (test-equal "#%1d:0()"
  107. (ra->string (ra-iota 0)))
  108. ; default ra-iota has lo #f so it will match any lower bound and act as index placeholder.
  109. (test-equal "#%1@2:3(2 3 4)"
  110. (ra->string (ra-copy! (make-ra-new #t 'o (c-dims '(2 4))) (ra-iota))))
  111. ; mixing dead and unbounded axes
  112. (let ((a (ra-i #f #t #f 2 3)))
  113. (test-equal 5 (a 0 0 0 1 2))
  114. (test-equal 5 (a 0 0 1 1 2))
  115. (test-equal 11 (a 0 1 0 1 2))
  116. ; reducing the unbounded axis
  117. (test-equal "#%4d:d:d:2:3((((6 7 8) (9 10 11))))"
  118. (ra->string (ra-from a #t 1))))
  119. ; unbounded both ways
  120. (let ((a (ra-i #f '(#f #t) #f 2 3)))
  121. (test-equal "#%4d:d:d:2:3((((-6 -5 -4) (-3 -2 -1))))"
  122. (ra->string (ra-from a #t -1 #t))))
  123. ; 2/3/4 use moving slice but 1 uses direct slicing in the lo..hi range.
  124. (define (test-lof rsfe)
  125. (test-equal
  126. "(#%0(o) #%0d(2))(#%0(o) #%0d(3))(#%0(o) #%0d(4))"
  127. (call-with-output-string
  128. (lambda (o)
  129. (rsfe 1 (lambda a (display a o)) (make-ra-new #t 'o (c-dims '(2 4))) (ra-iota))))))
  130. (test-lof ra-slice-for-each-1)
  131. (test-lof ra-slice-for-each-2)
  132. (test-lof ra-slice-for-each-3)
  133. (test-lof ra-slice-for-each-4)
  134. ; -----------------------
  135. ; make-ra-shared
  136. ; -----------------------
  137. (let* ((ra2 (array->ra #2((1 2) (3 4))))
  138. (ra3 (array->ra #2@1@1((1 2) (3 4))))
  139. (ra4 (array->ra #0(99))))
  140. (test-equal "#%0(99)" (ra->string (make-ra-shared ra4 (lambda () '()))))
  141. (test-equal "#%1:2(1 4)" (ra->string (make-ra-shared ra2 (lambda (i) (list i i)) 2)))
  142. (test-equal "#%1:2(1 3)" (ra->string (make-ra-shared ra2 (lambda (i) (list i 0)) 2)))
  143. (test-equal "#%1:2(2 4)" (ra->string (make-ra-shared ra2 (lambda (i) (list i 1)) 2)))
  144. (test-equal "#%1:2(1 2)" (ra->string (make-ra-shared ra2 (lambda (j) (list 0 j)) 2)))
  145. (test-equal "#%1:2(3 4)" (ra->string (make-ra-shared ra2 (lambda (j) (list 1 j)) 2)))
  146. (test-equal "#%1@1:2(1 4)" (ra->string (make-ra-shared ra3 (lambda (i) (list i i)) '(1 2))))
  147. (test-equal "#%1@1:2(1 3)" (ra->string (make-ra-shared ra3 (lambda (i) (list i 1)) '(1 2))))
  148. (test-equal "#%1@1:2(2 4)" (ra->string (make-ra-shared ra3 (lambda (i) (list i 2)) '(1 2))))
  149. (test-equal "#%1@1:2(1 2)" (ra->string (make-ra-shared ra3 (lambda (j) (list 1 j)) '(1 2))))
  150. (test-equal "#%1@1:2(3 4)" (ra->string (make-ra-shared ra3 (lambda (j) (list 2 j)) '(1 2)))))
  151. ; -----------------------
  152. ; make-ra-new, make-ra
  153. ; -----------------------
  154. (let* ((ra5 (make-ra-new #t 0 (c-dims '(1 3) '(1 2)))))
  155. (array-index-map! (ra-root ra5) (lambda i i))
  156. (test-equal (ra->string ra5) "#%2@1:3@1:2(((0) (1)) ((2) (3)) ((4) (5)))")
  157. (test-equal 3 (ra-len ra5)))
  158. (let* ((ra5 (make-typed-ra 's64 0 '(1 3) '(1 2))))
  159. (array-index-map! (ra-root ra5) (lambda i (car i)))
  160. (test-equal (ra->string ra5) "#%2s64@1:3@1:2((0 1) (2 3) (4 5))")
  161. (test-equal 3 (ra-len ra5)))
  162. ; -----------------------
  163. ; make-ra-root
  164. ; -----------------------
  165. (define ra6 (make-ra-root #(1 2 3 4 5 6) (c-dims '(1 2) '(1 3))))
  166. (test-equal (ra->string ra6) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  167. (define ra7a (make-ra-root (make-aseq 1) (c-dims '(1 2) '(1 3))))
  168. (test-equal (ra->string ra7a) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  169. (define ra7b (make-ra-root #(1 4 2 5 3 6) `#(,(make-dim 2 1 1) ,(make-dim 3 1 2)) -3))
  170. (test-equal (ra->string ra7b) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  171. (test-equal 2 (ra-len ra6))
  172. (test-equal 2 (ra-len ra7a))
  173. (test-equal 2 (ra-len ra7b))
  174. ; -----------------------
  175. ; ra-slice, ra-ref, ra-cell
  176. ; -----------------------
  177. (define ra8 (make-ra-root (make-aseq 1) (c-dims '(1 2) '(1 3))))
  178. (test-equal 2 (ra-len ra8))
  179. (test-equal (ra->string (ra-cell ra8)) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  180. (test-equal (ra->string (ra-cell ra8 1)) "#%1d@1:3(1 2 3)")
  181. (test-equal (ra->string (ra-cell ra8 2)) "#%1d@1:3(4 5 6)")
  182. (test-equal 5 (ra-cell ra8 2 2))
  183. ; applicable!
  184. (test-equal (ra->string (ra8)) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  185. (test-equal (ra->string (ra8 1)) "#%1d@1:3(1 2 3)")
  186. (test-equal (ra->string (ra8 2)) "#%1d@1:3(4 5 6)")
  187. (test-equal 5 (ra8 2 2))
  188. (test-equal (ra->string (ra-slice ra8)) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  189. (test-equal (ra->string (ra-slice ra8 1)) "#%1d@1:3(1 2 3)")
  190. (test-equal (ra->string (ra-slice ra8 2)) "#%1d@1:3(4 5 6)")
  191. (test-equal (ra->string (ra-slice ra8 2 2)) "#%0d(5)")
  192. (test-equal 5 (ra-ref (ra-slice ra8 2 2)))
  193. (test-assert (throws-exception? 'bad-number-of-indices (lambda () (ra-ref ra8))))
  194. (test-assert (throws-exception? 'bad-number-of-indices (lambda () (ra-ref ra8 1))))
  195. (test-assert (throws-exception? 'dim-check-out-of-range (lambda () (ra-ref ra8 0 0))))
  196. (test-assert (throws-exception? 'dim-check-out-of-range (lambda () (ra-ref ra8 3 1))))
  197. (test-equal 1 (ra-ref ra8 1 1))
  198. (test-equal 2 (ra-ref ra8 1 2))
  199. (test-equal 3 (ra-ref ra8 1 3))
  200. (test-equal 4 (ra-ref ra8 2 1))
  201. (test-equal 5 (ra-ref ra8 2 2))
  202. (test-equal 6 (ra-ref ra8 2 3))
  203. ; -----------------------
  204. ; ra-transpose (see below for ra-transpose with dead axes)
  205. ; -----------------------
  206. (test-equal (ra->string (ra-transpose ra7a 1 0)) "#%2d@1:3@1:2((1 4) (2 5) (3 6))")
  207. (test-equal (ra->string (ra-transpose ra7b 1 0)) "#%2@1:3@1:2((1 4) (2 5) (3 6))")
  208. (test-equal (ra->string (ra-transpose ra7a 0 1)) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  209. (test-equal (ra->string (ra-transpose ra7b 0 1)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  210. (test-equal (ra->string (ra-transpose ra7a 0 0)) "#%1d@1:2(1 5)")
  211. (test-equal (ra->string (ra-transpose ra7b 0 0)) "#%1@1:2(1 5)")
  212. (test-assert (throws-exception? 'bad-number-of-axes (lambda () (ra-transpose (ra-iota 3) 0 1))))
  213. (let ((ra0 (make-ra 99))) (test-assert (ra-equal? ra0 (ra-transpose ra0))))
  214. ; other cases
  215. (test-equal (ra->string (ra-transpose ra7a)) "#%2d@1:2@1:3((1 2 3) (4 5 6))")
  216. (test-equal (ra->string (ra-transpose ra7b)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  217. (let ((ra1 (ra-i 3)))
  218. (test-assert (ra-equal? (ra-transpose ra1) (ra-transpose ra1 0)))
  219. (test-eq (ra-root ra1) (ra-root (ra-transpose ra1 0))))
  220. (let ((ra2 (ra-i 3 4)))
  221. (test-assert (ra-equal? (ra-transpose ra2) (ra-transpose ra2 0 1)))
  222. (test-eq (ra-root ra2) (ra-root (ra-transpose ra2))))
  223. (let ((ra3 (ra-i 3 4 5)))
  224. (test-assert (ra-equal? (ra-transpose ra3) (ra-transpose ra3 0 1 2)))
  225. (test-eq (ra-root ra3) (ra-root (ra-transpose ra3)))
  226. (test-assert (ra-equal? (ra-transpose ra3 1 0) (ra-transpose ra3 1 0 2)))
  227. ; these two have a dead axes so the cannot be matched one to the other.
  228. (test-equal (ra-dims (ra-transpose ra3 1)) (ra-dims (ra-transpose ra3 1 2 3))))
  229. ; untranspose
  230. (let* ((a (ra-copy #t (ra-i 2 3 4)))
  231. (b (ra-transpose a 2 0 1))
  232. (c (ra-untranspose b 2 0 1)))
  233. (test-eq (ra-root a) (ra-root c))
  234. (test-assert (ra-equal? a c))
  235. (test-equal (ra-dims a) (ra-dims c)))
  236. (let* ((a (ra-copy #t (ra-i 2 3 4 5)))
  237. (b (ra-transpose a 2 0 1))
  238. (c (ra-untranspose b 2 0 1)))
  239. (test-eq (ra-root a) (ra-root c))
  240. (test-assert (ra-equal? a c))
  241. (test-equal (ra-dims a) (ra-dims c)))
  242. ; -----------------------
  243. ; ra-reverse
  244. ; -----------------------
  245. (let ((ra (ra-i 2 3 2)))
  246. (test-assert (ra-equal? ra (ra-reverse ra)))
  247. (test-equal "#%3d:2:3:2(((6 7) (8 9) (10 11)) ((0 1) (2 3) (4 5)))" (ra->string (ra-reverse ra 0)))
  248. (test-equal "#%3d:2:3:2(((4 5) (2 3) (0 1)) ((10 11) (8 9) (6 7)))" (ra->string (ra-reverse ra 1)))
  249. (test-equal "#%3d:2:3:2(((1 0) (3 2) (5 4)) ((7 6) (9 8) (11 10)))" (ra->string (ra-reverse ra 2)))
  250. (test-equal "#%3d:2:3:2(((5 4) (3 2) (1 0)) ((11 10) (9 8) (7 6)))" (ra->string (ra-reverse ra 1 2)))
  251. (test-equal "#%3d:2:3:2(((7 6) (9 8) (11 10)) ((1 0) (3 2) (5 4)))" (ra->string (ra-reverse ra 2 0)))
  252. (test-assert (ra-equal? (ra-reverse ra 2 0) (ra-reverse ra 0 2)))
  253. (test-assert (ra-equal? (ra-reverse ra 2 0 1) (ra-reverse (ra-reverse (ra-reverse ra 0) 1) 2)))
  254. (test-eq (ra-root ra) (ra-root (ra-reverse ra 2 0))))
  255. (let ((ra (make-ra-root #(1 2 3 4) (vector (make-dim 4 -1)))))
  256. (test-equal "#%1@-1:4(4 3 2 1)" (ra->string (ra-reverse ra 0))))
  257. ; -----------------------
  258. ; ra-slice-for-each
  259. ; -----------------------
  260. (define ra-empty0 (make-ra-root (make-aseq 1) (c-dims '(1 0) '(2 1))))
  261. (define ra-empty1 (make-ra-root (make-aseq 1) (c-dims '(1 1) '(2 1))))
  262. (define ra-empty2 (make-ra-root (make-aseq 1) (c-dims '(1 0) '(2 2))))
  263. (test-equal 0 (ra-len ra-empty0))
  264. (test-equal 1 (ra-len ra-empty1))
  265. (test-equal 0 (ra-len ra-empty2))
  266. (for-each
  267. (lambda (ra-slice-for-each)
  268. (test-begin (procedure-name ra-slice-for-each))
  269. (test-equal "#%2d@1:0@2:0()\n"
  270. (call-with-output-string
  271. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty0))))
  272. (test-equal "#%2d@1:1@2:0(())\n"
  273. (call-with-output-string
  274. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty1))))
  275. (test-equal "#%2d@1:0@2:1()\n"
  276. (call-with-output-string
  277. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty2))))
  278. (test-equal "#%2@1:2@1:3((1 2 3) (4 5 6))\n"
  279. (call-with-output-string
  280. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra6))))
  281. (test-equal "#%1@1:3(1 2 3)\n#%1@1:3(4 5 6)\n"
  282. (call-with-output-string
  283. (lambda (s) (ra-slice-for-each 1 (lambda (o) (format s "~a\n" o)) ra6))))
  284. (test-equal "1\n2\n3\n4\n5\n6\n"
  285. (call-with-output-string
  286. (lambda (s) (ra-slice-for-each 2 (lambda (o) (format s "~a\n" (ra-ref o))) ra6))))
  287. (test-equal "2\n4\n6\n8\n10\n12\n"
  288. (call-with-output-string
  289. (lambda (s) (ra-slice-for-each 2 (lambda (a b) (format s "~a\n" (+ (ra-ref a) (ra-ref b)))) ra6 ra7a))))
  290. (test-equal "2\n4\n6\n8\n10\n12\n"
  291. (call-with-output-string
  292. (lambda (s) (ra-slice-for-each 2 (lambda (a b) (format s "~a\n" (+ (ra-ref a) (ra-ref b)))) ra6 ra7b))))
  293. (test-equal "(#%2@1:2@1:3((1 2 3) (4 5 6)) #%2d@1:2@1:3((1 2 3) (4 5 6)) #%2@1:2@1:3((1 2 3) (4 5 6)))\n"
  294. (call-with-output-string
  295. (lambda (s) (ra-slice-for-each 0 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  296. (test-equal "(#%1@1:3(1 2 3) #%1d@1:3(1 2 3) #%1@1:3(1 2 3))\n(#%1@1:3(4 5 6) #%1d@1:3(4 5 6) #%1@1:3(4 5 6))\n"
  297. (call-with-output-string
  298. (lambda (s) (ra-slice-for-each 1 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  299. (test-equal "(#%0(1) #%0d(1) #%0(1))\n(#%0(2) #%0d(2) #%0(2))\n(#%0(3) #%0d(3) #%0(3))\n(#%0(4) #%0d(4) #%0(4))\n(#%0(5) #%0d(5) #%0(5))\n(#%0(6) #%0d(6) #%0(6))\n"
  300. (call-with-output-string
  301. (lambda (s) (ra-slice-for-each 2 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  302. (test-end (procedure-name ra-slice-for-each)))
  303. (list ra-slice-for-each-1 ra-slice-for-each-2
  304. ra-slice-for-each-3 ra-slice-for-each-4))
  305. ; -----------------------
  306. ; ra-offset - from guile/test-suite/tests/arrays.test
  307. ; -----------------------
  308. (test-begin "ra-offset")
  309. ; plain vector
  310. (test-equal 0 (ra-offset (make-ra 0 4)))
  311. ; plain array rank 2
  312. (test-equal 0 (ra-offset (make-ra 0 4 4)))
  313. ; row of rank-2 array, I
  314. (test-equal 0 (ra-offset ((make-ra 0 5 3) 0)))
  315. ; row of rank-2 array, II
  316. (test-equal 4 (ra-offset ((make-ra 0 6 4) 1)))
  317. ; col of rank-2 array, I
  318. (test-equal 0 (ra-offset ((ra-transpose (make-ra 0 5 3) 1 0) 0)))
  319. ; col of rank-2 array, II
  320. (test-equal 1 (ra-offset ((ra-transpose (make-ra 0 6 4) 1 0) 1)))
  321. ; of inverted array; shared-array-root's doc is unclear as 'first' is unclear. But we do the same.
  322. (test-equal 2 (shared-array-offset (make-shared-array #(1 2 3) (lambda (i) (list (- 2 i))) 3)))
  323. (test-equal 2 (ra-offset (make-ra-shared (array->ra #(1 2 3)) (lambda (i) (list (- 2 i))) 3)))
  324. (test-end "ra-offset")
  325. ; -----------------------
  326. ; ra-slice-for-each compatibility - from guile/test-suite/tests/array-map.test
  327. ; -----------------------
  328. (for-each
  329. (match-lambda
  330. ((name ra-slice-for-each list->ra list->typed-ra make-ra ra-copy!
  331. make-ra-shared array->ra ra->array ra-set! ra-ref)
  332. (let ((test-name (format #f "~a" name)))
  333. (test-begin test-name)
  334. ; 1 argument frame rank 1
  335. (test-equal
  336. #2((1 3 9) (2 7 8))
  337. (ra->array
  338. (let* ((a (list->ra 2 '((9 1 3) (7 8 2)))))
  339. (ra-slice-for-each 1 (lambda (a) (sort! (ra->array a) <)) a)
  340. a)))
  341. ; 1 argument frame rank 1, non-zero base indices
  342. (test-equal
  343. #2@1@1((1 3 9) (2 7 8))
  344. (ra->array
  345. (let* ((a (make-ra *unspecified* '(1 2) '(1 3)))
  346. (b (array->ra #2@1@1((9 1 3) (7 8 2)))))
  347. (ra-copy! a b)
  348. (ra-slice-for-each 1 (lambda (a) (sort! (ra->array a) <)) a)
  349. a)))
  350. ; 2 arguments frame rank 1
  351. (test-equal
  352. #f64(8 -1)
  353. (ra->array
  354. (let* ((x (list->typed-ra 'f64 2 '((9 1) (7 8))))
  355. (y (array->ra (f64vector 99 99))))
  356. (ra-slice-for-each 1 (lambda (y x) (ra-set! y (- (ra-ref x 0) (ra-ref x 1)))) y x)
  357. y)))
  358. ; regression: zero-sized frame loop without unrolling
  359. (test-equal
  360. 99
  361. (let* ((x 99)
  362. (o (make-ra 0. 0 3 2)))
  363. (ra-slice-for-each 2
  364. (lambda (o a0 a1)
  365. (set! x 0))
  366. o
  367. (make-ra-shared (make-ra 1. 0 1) (const '(0 0)) 0 3)
  368. (make-ra 2. 0 3))
  369. x))
  370. (test-end test-name))))
  371. `(("oldra" ,array-slice-for-each ,list->array ,list->typed-array
  372. ,make-array ,(lambda (a b) (array-copy! b a)) ,make-shared-array
  373. ,(lambda (x) x) ,(lambda (x) x) ,array-set! ,array-ref)
  374. ("newra" ,ra-slice-for-each ,list->ra ,list->typed-ra
  375. ,make-ra ,ra-copy! ,make-ra-shared
  376. ,array->ra ,ra->array ,ra-set! ,ra-ref)
  377. ))
  378. ; -----------------------
  379. ; setter
  380. ; -----------------------
  381. (define ra9 (make-ra-root (make-vector 6) (c-dims '(-1 0) '(1 3))))
  382. (set! (ra9 -1 1) 99)
  383. (set! (ra9 -1 2) 77)
  384. (set! (ra9 -1 3) 88)
  385. (set! (ra9 0 1) 33)
  386. (set! (ra9 0 2) 11)
  387. (set! (ra9 0 3) 22)
  388. (test-equal (ra->string ra9) "#%2@-1:2@1:3((99 77 88) (33 11 22))")
  389. (test-equal 2 (ra-len ra9))
  390. (test-equal #2@-1:2@1:3((99 77 88) (33 44 22)) (ra->array (set! (ra9 0 2) 44)))
  391. ; -----------------------
  392. ; test through the ra-map! interface, also ra-copy!, ra-fill!, ra-swap! ra-equal?
  393. ; -----------------------
  394. (define ra11 (make-ra-new #t 0 (c-dims 10)))
  395. (define ra12 (make-ra-root (make-aseq) (c-dims 10)))
  396. (define ra13 (make-ra-root (make-aseq 10 -1) (c-dims 10)))
  397. (test-equal 10 (ra-len ra11))
  398. (test-equal 10 (ra-len ra12))
  399. (test-equal 10 (ra-len ra13))
  400. (test-equal "#%1:10(0 1 2 3 4 5 6 7 8 9)" (ra->string (ra-copy! ra11 ra12)))
  401. (test-equal "#%1:10(0 1 2 3 4 5 6 7 8 9)" (ra->string (ra-copy! ra11 (ra-copy #t ra12))))
  402. (let ((ra5a (make-ra-new #t 0 (c-dims '(1 2) '(1 3))))
  403. (ra6a (make-ra-root (vector-copy #(1 2 3 4 5 6)) (c-dims '(1 2) '(1 3)))))
  404. (test-equal "#%2@1:2@1:3((1 2 3) (4 5 6))" (ra->string (ra-copy! ra5a ra6a)))
  405. (test-assert (ra-equal? ra6a ra5a))
  406. (test-assert (ra-equal? ra6a ra5a ra6a))
  407. (set! (ra5a 1 1) 99)
  408. (test-assert (not (ra-equal? ra5a ra6a)))
  409. (set! (ra6a 1 1) 99)
  410. (test-assert (ra-equal? ra5a ra6a))
  411. (test-equal "#%2@1:2@1:3((x x x) (x x x))" (ra->string (ra-fill! ra5a 'x)))
  412. (test-equal "#%2@1:2@1:3((x x x) (x x x))" (ra->string ra5a))
  413. (test-assert (not (ra-equal? ra6a ra5a)))
  414. (test-assert (not (ra-equal? (string->ra "#%(1 2 3)") (string->ra "#%(1 2 3 4)"))))
  415. (test-assert (not (ra-equal? (string->ra "#%1f64(1 2 3)") (string->ra "#%(1 2 3)"))))
  416. (test-assert (ra-equal? (make-ra-root #(99 99 99 99) (c-dims '(1 2) '(1 2)))
  417. (ra-fill! (make-ra-root (vector 1 2 3 4) (c-dims '(1 2) '(1 2))) 99))))
  418. (test-begin "ra-for-each")
  419. (test-equal "(10 0 10)(9 1 9)(8 2 8)(7 3 7)(6 4 6)(5 5 5)(4 6 4)(3 7 3)(2 8 2)(1 9 1)"
  420. (call-with-output-string (lambda (s) (ra-for-each (lambda x (display x s)) ra13 ra12 ra13))))
  421. (test-end "ra-for-each")
  422. (ra-map! ra11 (lambda () (random 100)))
  423. (for-each
  424. (match-lambda
  425. ((ra-map! name)
  426. (test-begin (string-append "3 args - " name))
  427. (test-equal "#%1:10(-10 -8 -6 -4 -2 0 2 4 6 8)"
  428. (call-with-output-string (cute display (ra-map! ra11 - ra12 ra13) <>)))
  429. (test-end (string-append "3 args - " name))))
  430. `((,(cut ra-map*! ra-slice-for-each-1 <...>) "fe1")
  431. (,(cut ra-map*! ra-slice-for-each-2 <...>) "fe2")
  432. (,(cut ra-map*! ra-slice-for-each-3 <...>) "fe3")
  433. (,(cut ra-map*! ra-slice-for-each-4 <...>) "fe4")
  434. (,ra-map! "ra-map!")))
  435. ; test with enough args to hit the rest list version.
  436. (for-each
  437. (match-lambda
  438. ((ra-map! name)
  439. (test-begin (string-append "4 args - " name))
  440. (test-equal "#%1:10(-10 -9 -8 -7 -6 -5 -4 -3 -2 -1)"
  441. (call-with-output-string (cute display (ra-map! ra11 - ra12 ra13 ra12) <>)))
  442. (test-end (string-append "4 args - " name))))
  443. `((,(cut ra-map*! ra-slice-for-each-1 <...>) "fe1")
  444. (,(cut ra-map*! ra-slice-for-each-2 <...>) "fe2")
  445. (,(cut ra-map*! ra-slice-for-each-3 <...>) "fe3")
  446. (,(cut ra-map*! ra-slice-for-each-4 <...>) "fe4")
  447. (,ra-map! "ra-map!")))
  448. (test-begin "ra-for-each")
  449. (test-equal "(10 0 10 0)(9 1 9 1)(8 2 8 2)(7 3 7 3)(6 4 6 4)(5 5 5 5)(4 6 4 6)(3 7 3 7)(2 8 2 8)(1 9 1 9)"
  450. (call-with-output-string (lambda (s) (ra-for-each (lambda x (display x s)) ra13 ra12 ra13 ra12))))
  451. (test-end "ra-for-each")
  452. (test-begin "ra-swap!")
  453. (let* ((a (ra-copy #t (ra-i 3 2)))
  454. (root-a (ra-root a))
  455. (ca (ra-copy a))
  456. (b (list->ra 2 '((a b) (c d) (e f))))
  457. (root-b (ra-root b))
  458. (cb (ra-copy b)))
  459. (ra-swap! a b)
  460. (test-eq root-a (ra-root a))
  461. (test-eq root-b (ra-root b))
  462. (test-assert (ra-equal? ca b) (ra-equal? cb a)))
  463. (test-end "ra-swap!")
  464. ; -----------------------
  465. ; ra->list
  466. ; -----------------------
  467. (test-equal '(1 2 3) (ra->list (string->ra "#%(1 2 3)")))
  468. (test-equal '(1 2 3) (ra->list (string->ra "#%@1(1 2 3)")))
  469. (test-equal '((1 2) (3 4)) (ra->list (string->ra "#%2((1 2) (3 4))")))
  470. (test-equal '((1 2) (3 4)) (ra->list (string->ra "#%2@1@1((1 2) (3 4))")))
  471. (test-equal '(((1 2 3) (3 4 5)) ((4 5 6) (6 7 8))) (ra->list (string->ra "#%3@1@1@-1(((1 2 3) (3 4 5)) ((4 5 6) (6 7 8)))")))
  472. (test-equal 99 (ra->list (string->ra "#%0(99)")))
  473. ; -----------------------
  474. ; reader
  475. ; -----------------------
  476. (for-each
  477. (lambda (str)
  478. (test-equal (format #f "#%3~a:2:2:2(((1 2) (3 4)) ((5 6) (7 8)))" str)
  479. (ra->string (string->ra (format #f "#%3~a(((1 2) (3 4)) ((5 6) (7 8)))" str))))
  480. (test-equal (format #f "#%3~a:2:2:2(((1 2) (3 4)) ((5 6) (7 8)))" str)
  481. (ra->string (string->ra (format #f "#%3~a[((1 2) [3 4]) [[5 6] (7 8)]]" str))))
  482. (test-equal (format #f "#%2~a:2:2((1 2) (3 4))" str)
  483. (ra->string (string->ra (format #f "#%2~a((1 2) (3 4))" str))))
  484. (test-equal (format #f "#%1~a:4(1 2 3 4)" str)
  485. (ra->string (string->ra (format #f "#%1~a(1 2 3 4)" str))))
  486. (test-equal (format #f "#%1~a:12(1 2 3 4 5 6 7 8 9 10 11 12)" str)
  487. (ra->string (string->ra (format #f "#%1~a(1 2 3 4 5 6 7 8 9 10 11 12)" str))))
  488. (test-equal (format #f "#%2~a@1:2@1:2((1 2) (3 4))" str)
  489. (ra->string (string->ra (format #f "#%2~a@1@1((1 2) (3 4))" str))))
  490. (test-equal (format #f "#%2~a@1:2@1:2((1 2) (3 4))" str)
  491. (ra->string (string->ra (format #f "#%2~a@1@1:2((1 2) (3 4))" str))))
  492. (test-equal (format #f "#%2~a@-1:2@1:2((1 2) (3 4))" str)
  493. (ra->string (string->ra (format #f "#%2~a@-1@1((1 2) (3 4))" str)))))
  494. '("s32" ""))
  495. (test-equal "#%0(#(1 2 3))" (ra->string (string->ra "#%0(#(1 2 3))")))
  496. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (string->ra "#%1f64(1 2 3)")))
  497. (test-equal "#%0(#%1:3(1 2 3))" (ra->string (string->ra "#%0[#%[1 2 3]]")))
  498. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (string->ra "#%1f64[1 2 3]")))
  499. (test-equal "#%3:0:0:0()" (ra->string (string->ra "#%3()")))
  500. (test-equal "#%3:2:0:0(() ())" (ra->string (string->ra "#%3(() ())")))
  501. (test-equal "#%3:1:1:0((()))" (ra->string (string->ra "#%3((())))")))
  502. ; -----------------------
  503. ; list->ra, list->typed-ra
  504. ; -----------------------
  505. (test-equal "#%0(99)" (ra->string (list->ra 0 99)))
  506. (test-equal "#%1:3(1 2 3)" (ra->string (list->ra 1 '(1 2 3))))
  507. (test-equal "#%0((1 2 3))" (ra->string (list->ra 0 '(1 2 3))))
  508. (test-equal "#%2:2:2((1 2) (3 4))" (ra->string (list->ra 2 '((1 2) (3 4)))))
  509. (test-equal "#%2:2:3((1 2 3) (4 5 6))" (ra->string (list->ra 2 '((1 2 3) (4 5 6)))))
  510. (test-equal "#%2:3:2((1 2) (3 4) (5 6))" (ra->string (list->ra 2 '((1 2) (3 4) (5 6)))))
  511. (test-equal "#%1:2((1 2) (3 4))" (ra->string (list->ra 1 '((1 2) (3 4)))))
  512. (test-equal "#%0(((1 2) (3 4)))" (ra->string (list->ra 0 '((1 2) (3 4)))))
  513. (test-equal "#%0f64(99.0)" (ra->string (list->typed-ra 'f64 0 99)))
  514. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 1 '(1 2 3))))
  515. (test-equal "#%2f64:2:2((1.0 2.0) (3.0 4.0))" (ra->string (list->typed-ra 'f64 2 '((1 2) (3 4)))))
  516. (test-equal "#%1f64@1:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 '(1) '(1 2 3))))
  517. (test-equal "#%1f64@1:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 '((1 3)) '(1 2 3))))
  518. (test-equal "#%1f64@-1:3(1.0 2.0 3.0)" (ra->string (list->ra 'f64 '(-1) '(1 2 3))))
  519. (test-equal "#%1f64@-1:3(1.0 2.0 3.0)" (ra->string (list->ra 'f64 '((-1 1)) '(1 2 3))))
  520. ; -----------------------
  521. ; ra-index-map!
  522. ; -----------------------
  523. (test-equal "#%2:2:3(((0 0) (0 1) (0 2)) ((1 0) (1 1) (1 2)))"
  524. (ra->string (let ((x (make-ra 0 2 3))) (ra-index-map! x (lambda x x)))))
  525. (test-equal "#%0(99)"
  526. (ra->string (let ((x (make-ra 0))) (ra-index-map! x (lambda x 99)))))
  527. ; -----------------------
  528. ; ra-dimensions, ra-shape
  529. ; -----------------------
  530. (test-equal '((-1 3) (0 4))
  531. (ra-shape (make-ra 'foo '(-1 3) 5)))
  532. (test-equal '((-1 3) 5)
  533. (ra-dimensions (make-ra 'foo '(-1 3) 5)))
  534. ; -----------------------
  535. ; raw prefix match
  536. ; -----------------------
  537. (let ((ra0 (ra-i 2 3))
  538. (ra1 (ra-i 2)))
  539. (test-equal
  540. "#%2d:2:3((0 1 2) (3 4 5))-#%1d:2(0 1)|"
  541. (call-with-output-string
  542. (lambda (o) (ra-slice-for-each 0 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  543. (test-equal
  544. "#%1d:3(0 1 2)-#%0d(0)|#%1d:3(3 4 5)-#%0d(1)|"
  545. (call-with-output-string
  546. (lambda (o) (ra-slice-for-each 1 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  547. (test-equal
  548. "#%0d(0)-#%0d(0)|#%0d(1)-#%0d(0)|#%0d(2)-#%0d(0)|#%0d(3)-#%0d(1)|#%0d(4)-#%0d(1)|#%0d(5)-#%0d(1)|"
  549. (call-with-output-string
  550. (lambda (o) (ra-slice-for-each 2 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  551. (test-assert
  552. (throws-exception? 'unset-len-for-dim
  553. (lambda () (ra-slice-for-each 3 (lambda (ra0 ra1) 0) ra0 ra1)))))
  554. ; -----------------------
  555. ; prefix match on item (not cell) variants
  556. ; -----------------------
  557. (test-assert
  558. (ra-equal?
  559. (string->ra "#%1:2(-2 -5)")
  560. (let ((ra0 (ra-copy #t (ra-i 2 3)))
  561. (ra1 (ra-copy #t (ra-i 2))))
  562. (ra-map! ra1 - ra0))))
  563. (test-assert
  564. (ra-equal?
  565. (string->ra "#%2:2:3((0 0 0) (-1 -1 -1))")
  566. (let ((ra0 (ra-copy #t (ra-i 2 3)))
  567. (ra1 (ra-copy #t (ra-i 2))))
  568. (ra-map! ra0 - ra1))))
  569. (test-assert
  570. (ra-equal?
  571. (string->ra "#%3:4:3:2(((0 0) (1 1) (2 2)) ((3 3) (4 4) (5 5)) ((6 6) (7 7) (8 8)) ((9 9) (10 10) (11 11)))")
  572. (ra-copy! (make-ra 99 4 3 2) (ra-i 4 3))))
  573. (test-assert
  574. (ra-equal?
  575. (string->ra "#%2:3:2((77 77) (77 77) (77 77))")
  576. (ra-copy! (make-ra 99 3 2) (make-ra 77))))
  577. ; -----------------------
  578. ; ra-fold
  579. ; -----------------------
  580. ; these are slow :-/
  581. ; numpy (1.16.2) - time(np.sum(np.arange(1e8))) ~ 0.15 s
  582. ; octave (4.4.1) - a = time(); b=sum(0:999999999); time()-a ~ 3.1 s
  583. ; newra - ,time (ra-fold + 0 (ra-iota #e1e8)) ~ 4.7 s
  584. (test-equal (* #e1e6 (- #e1e6 1) 1/2) (ra-fold + 0 (ra-iota #e1e6)))
  585. (test-equal (* #e1e6 (- #e1e6 1) 1/2) (ra-fold + 0 (ra-i 100 100 100)))
  586. ; -----------------------
  587. ; ra-any ra-every
  588. ; -----------------------
  589. (test-assert (ra-every positive? (list->ra 1 '(1 2 3 4 5))))
  590. (test-equal -3 (ra-any (lambda (x) (and (negative? x) x)) (list->ra 1 '(1 2 -3 4 5))))
  591. (test-equal #(0 2) (ra-any (lambda (a i j) (and (zero? a) (vector i j)))
  592. (list->ra 2 '((3 4 0) (1 2 9)))
  593. (ra-iota #f)
  594. (ra-transpose (ra-iota #f) 1)))
  595. ; -----------------------
  596. ; inf dims I - (make-dim #f) = (make-dim #f 0 1)
  597. ; -----------------------
  598. (test-assert (ra-equal? (ra-i 3) (make-ra-root (make-aseq) (c-dims 3))))
  599. ; inf dimension
  600. (test-assert (not (dim-len (make-dim #f))))
  601. ; make an array out of inf root
  602. (test-assert (ra-equal? (ra-i 3) (make-ra-root (make-aseq) (c-dims 3))))
  603. ; make an inf array
  604. (let ((rainf (make-ra-root (make-aseq) (c-dims #t))))
  605. (throws-exception? 'unset-len-for-dim (lambda () (ra-for-each pk rainf)))
  606. (test-equal #(10 8 6) (ra->array (ra-map! (make-ra #f 3) - (ra-iota 3 10 -1) rainf)))
  607. (test-equal #(-10 -8 -6) (ra->array (ra-map! (make-ra #f 3) - rainf (ra-iota 3 10 -1)))))
  608. (let ((rainf (ra-i #t 3 4)))
  609. (test-equal #(1204 1205 1206 1207) (ra->array (ra-copy #t (rainf 100 1))))
  610. (test-equal '(#f 3 4) (ra-dimensions rainf)))
  611. ; properties of inf arrays
  612. (test-equal '((#f #f)) (ra-shape (ra-iota)))
  613. ; -----------------------
  614. ; inf dims II - (make-dim #f 0 0) = dead axis
  615. ; -----------------------
  616. (test-equal
  617. #2((11 12 13) (21 22 23))
  618. (ra->array
  619. (ra-map! (make-ra #f 2 3) +
  620. (make-ra-root #(10 20) (vector (make-dim 2 0 1) (make-dim #f 0 0)))
  621. (make-ra-root #(1 2 3) (vector (make-dim #f 0 0) (make-dim 3 0 1))))))
  622. ; using generalized transpose
  623. (test-equal
  624. #2((11 12 13) (21 22 23))
  625. (ra->array
  626. (ra-map! (make-ra #f 2 3) +
  627. (ra-iota 2 10 10)
  628. (ra-transpose (ra-iota 3 1 1) 1))))
  629. ; copying arrays with dead axes
  630. (let ((ra (ra-copy #t (ra-transpose (ra-i 3) 1))))
  631. (test-equal "#%2:d:3((0 1 2))" (ra->string ra))
  632. (test-equal #(0 1 2) (ra-root ra)))
  633. ; -----------------------
  634. ; inf dims III - dimensions can be unbounded in both directions. For these the origin is 0.
  635. ; -----------------------
  636. (let ((ii (make-ra-root (make-aseq) (vector (make-dim #f #f 1)))))
  637. (test-equal 99 (ra-ref ii 99))
  638. (test-equal -99 (ra-ref ii -99)))
  639. ; -----------------------
  640. ; from
  641. ; -----------------------
  642. ; all the tests below pass with fromu = fromb set to ra-from.
  643. (define fromu (@ (newra from) fromu))
  644. (define fromb (@ (newra from) fromb))
  645. (define A (ra-map! (make-ra 0 10 10) + (ra-transpose (ra-iota 10) 1) (ra-iota 10 0 10)))
  646. (define b (ra-i 2))
  647. (define c (make-ra-root (make-aseq 10 2) (vector (make-dim 3 1))))
  648. (define d (ra-i 2 2))
  649. (define iz (ra-i 3))
  650. (ra-from A iz)
  651. (ra-from A 0)
  652. (test-equal "#%2:3:10((0 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))"
  653. (ra->string (ra-from A (ra-copy #t (ra-iota 3)))))
  654. (test-equal "#%2:3:2((0 1) (10 11) (20 21))" (ra->string (ra-from A (ra-iota 3) (ra-iota 2))))
  655. ; manual examples
  656. (let* ((a (list->ra 2 '((1 2 3) (a b c))))
  657. (b (ra-from a #t (ra-iota 3 2 -1)))
  658. (bc (ra-from-copy a #t (ra-iota 3 2 -1)))
  659. (c (ra-from a #t (list->ra 1 '(2 1 0)))))
  660. (test-equal "#%2:2:3((3 2 1) (c b a))" (ra->string b) (ra->string c))
  661. (test-assert (eq? (ra-root a) (ra-root b)))
  662. (test-assert (not (eq? (ra-root a) (ra-root bc))))
  663. (test-assert (not (eq? (ra-root a) (ra-root c)))))
  664. ; ------------------------
  665. ; tests
  666. ; ------------------------
  667. (define (test-from id A . i)
  668. ; to be able to test integers on fromu
  669. (let ((ii (map (match-lambda ((? ra? x) x) ((? integer? x) (make-ra x))) i)))
  670. (test-begin id)
  671. ; beatable vs unbeatable
  672. (let ((X (apply fromb A i))
  673. (Y (apply fromu A ii)))
  674. (test-assert (ra-equal? X Y)))
  675. ; general as unbeatable (except integers), vs unbeatable
  676. (let ((X (apply ra-from A (map (lambda (x) (if (ra? x) (ra-copy #t x) x)) i)))
  677. (Y (apply fromu A ii))
  678. (Z (apply A i)))
  679. (test-assert (ra-equal? Y X))
  680. (test-assert (ra-equal? Y (if (zero? (ra-rank Y)) (make-ra Z) Z))))
  681. ; general as beatable, vs beatable
  682. (let ((X (apply ra-from A i))
  683. (Y (apply fromb A i))
  684. (Z (apply A i)))
  685. (test-assert (ra-equal? Y X))
  686. (test-assert (ra-equal? Y (if (zero? (ra-rank Y)) (make-ra Z) Z)))
  687. ; purely beatable indices preserve the root.
  688. (test-eq (ra-root A) (ra-root Y))
  689. (test-eq (ra-root A) (ra-root X))
  690. (unless (zero? (ra-rank Y))
  691. (test-assert (ra-root A) (ra-root Z))))
  692. ; general as beatable, vs unbeatable
  693. (let ((X (apply ra-from A i))
  694. (Y (apply fromu A ii))
  695. (Z (apply A i)))
  696. (test-assert (ra-equal? Y X))
  697. (test-assert (ra-equal? Y (if (zero? (ra-rank Y)) (make-ra Z) Z))))
  698. ; general as unbeatable/beatable (2 args), vs unbeatable
  699. (match i
  700. ((i j)
  701. (match ii
  702. ((ii jj)
  703. (let ((X1 (ra-from A (if (ra? i) (ra-copy #t i) i) j))
  704. (X2 (ra-from A i (if (ra? j) (ra-copy #t j) j)))
  705. (Y (fromu A ii jj))
  706. (Z1 (A (if (ra? i) (ra-copy #t i) i) j))
  707. (Z2 (A i (if (ra? j) (ra-copy #t j) j))))
  708. (test-assert (ra-equal? Y X1))
  709. (test-assert (ra-equal? Y X2))
  710. (test-assert (ra-equal? Y (if (zero? (ra-rank Y)) (make-ra Z1) Z1)))
  711. (test-assert (ra-equal? Y (if (zero? (ra-rank Y)) (make-ra Z2) Z2)))))))
  712. (else #f))
  713. (test-end id)))
  714. ; ------------------------
  715. ; one arg
  716. ; rank 0
  717. (test-from "from00" A (make-ra-root (make-aseq 3) (vector)))
  718. (test-from "from01" A 2)
  719. (test-from "from02" A (make-ra 2))
  720. ; rank 1
  721. (test-from "from03" A (ra-iota 3))
  722. (test-from "from04" A (make-ra-root (make-aseq) (vector (make-dim 3 1 1))))
  723. (test-from "from05" A (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 1))))
  724. (test-from "from06" A (make-ra-root (make-aseq 1 2) (vector (make-dim 3 1 1))))
  725. (test-from "from07" A (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 2))))
  726. (test-from "from08" A (make-ra-root (make-aseq 3 2) (vector (make-dim 2 1 3))))
  727. ; rank 2
  728. (test-from "from09" A (ra-i 2 2))
  729. (test-from "from10" A (make-ra-root (make-aseq 3) (vector (make-dim 2 1 2) (make-dim 2 1 3))))
  730. ; ------------------------
  731. ; two args
  732. ; rank 0 0
  733. (test-from "from11" A (make-ra-root (make-aseq 3) (vector))
  734. (make-ra-root (make-aseq 2) (vector)))
  735. (test-from "from12" A 3 (make-ra-root (make-aseq 2) (vector)))
  736. (test-from "from13" A (make-ra-root (make-aseq 3) (vector)) 2)
  737. (test-from "from14" A 3 2)
  738. ; rank 1 1
  739. (test-from "from15" A (ra-iota 3) (ra-iota 2 4))
  740. (test-from "from16" A (make-ra-root (make-aseq) (vector (make-dim 3 1 1)))
  741. (make-ra-root (make-aseq) (vector (make-dim 3 1 2))))
  742. (test-from "from17" A (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 1)))
  743. (make-ra-root (make-aseq 1 2) (vector (make-dim 3 1 1))))
  744. (test-from "from18" A (make-ra-root (make-aseq 1 2) (vector (make-dim 3 1 1)))
  745. (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 2))))
  746. (test-from "from19" A (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 2)))
  747. (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 2))))
  748. (test-from "from20" A (make-ra-root (make-aseq 3 2) (vector (make-dim 2 1 3)))
  749. (make-ra-root (make-aseq 1 1) (vector (make-dim 3 1 2))))
  750. ; rank 2 2
  751. (test-from "from21" A (ra-i 3 3) (ra-i 2 2))
  752. (test-from "from22" A (make-ra-root (make-aseq 3) (vector (make-dim 2 1 2) (make-dim 2 1 3)))
  753. (make-ra-root (make-aseq 3) (vector (make-dim 2 1 2) (make-dim 2 1 3))))
  754. ; FIXME if ra-from is used in ra application, then these must give the same result.
  755. (ra-from (ra-i 2 3) (make-ra 0) (make-ra 1))
  756. (ra-cell (ra-i 2 3) 0 1)
  757. ; placeholders
  758. (test-begin "from, placeholders")
  759. (let* ((a (ra-i 2 3 4 5 6))
  760. (ref0 (ra-from a 0 (ra-iota 3)))
  761. (ref1 (ra-from a 0 (ra-iota 3) 1))
  762. (ref2 (ra-from a 0 (ra-iota 3) (ra-iota 4) (ra-iota 5) 2)))
  763. (test-assert (ra-equal? a (ra-from a #t)))
  764. (test-assert (ra-equal? a (ra-from a (dots 1))))
  765. (test-assert (ra-equal? a (ra-from a #t #t #t #t #t)))
  766. (test-assert (ra-equal? a (ra-from a (dots))))
  767. (test-assert (ra-equal? ref0 (ra-from a 0 #t)))
  768. (test-assert (ra-equal? ref0 (ra-from a 0 (dots 1))))
  769. (test-assert (ra-equal? ref0 (ra-from a 0 (dots 0))))
  770. (test-assert (ra-equal? ref1 (ra-from a 0 #t 1)))
  771. (test-assert (ra-equal? ref1 (ra-from a 0 (dots 1) 1)))
  772. (test-assert (ra-equal? ref2 (ra-from a 0 #t #t #t 2)))
  773. (test-assert (ra-equal? ref2 (ra-from a 0 (dots 3) 2)))
  774. (test-assert (ra-equal? ref2 (ra-from a 0 (dots) 2))))
  775. (test-end "from, placeholders")
  776. ; ------------------------
  777. ; other A
  778. (test-begin "from, other cases")
  779. (throws-exception? 'dim-check-out-of-range (lambda () (ra-from (ra-i 4) (ra-iota 6))))
  780. (throws-exception? 'dim-check-out-of-range (lambda () (ra-from (ra-i 4) 4)))
  781. (let ((A (make-ra-root (make-aseq) (vector (make-dim 4 -3 1)))))
  782. (test-equal 0 (ra-ref (fromu A (make-ra -3))))
  783. (test-equal 0 (ra-ref (fromb A -3)))
  784. (test-equal 0 (ra-ref (ra-from A -3)))
  785. (test-equal 0 (ra-ref (fromu A (make-ra -3))))
  786. (test-equal 0 (ra-ref (fromb A (make-ra -3))))
  787. (test-equal 0 (ra-ref (ra-from A (make-ra -3)))))
  788. (let ((A (make-ra-root #(0 1 2 3 4 5 6 7) (vector (make-dim 4 -3 1)))))
  789. (test-equal 0 (ra-ref (fromu A (make-ra -3))))
  790. (test-equal 0 (ra-ref (fromb A -3)))
  791. (test-equal 0 (ra-ref (ra-from A -3)))
  792. (test-equal 0 (ra-ref (fromu A (make-ra -3))))
  793. (test-equal 0 (ra-ref (fromb A (make-ra -3))))
  794. (test-equal 0 (ra-ref (ra-from A (make-ra -3)))))
  795. (test-equal "#%1d:2(0 1)" (ra->string (ra-from (ra-i 4) (ra-iota 2))))
  796. (test-equal "#%1d:4(0 1 2 3)" (ra->string (ra-from (ra-i #t) (ra-iota 4))))
  797. (test-equal "#%1d:4(0 -1 -2 -3)" (ra->string (ra-from (make-ra-root (make-aseq) (vector (make-dim 4 -3 -1))) (ra-iota 4 -3))))
  798. ; note that arrays are always indexed upwards, so you cannot have (lo hi) be (#f number).
  799. (throws-exception? 'dim-check-out-of-range (lambda () (ra-from (ra-i #f) (ra-iota 4 -3)))) ; error: -3 < 0
  800. (throws-exception? 'dim-check-out-of-range
  801. (lambda () (ra-from (make-ra-root (make-aseq) (vector (make-dim 4 -3 -1)))
  802. (ra-iota 5 -3)))) ; -3+5 > -3+4
  803. (throws-exception? 'dim-check-out-of-range (lambda () (ra-from (ra-i 4) (ra-iota #f))))
  804. (throws-exception? 'dim-check-out-of-range (lambda () (ra-from (ra-copy #t (ra-i 4)) (ra-iota #f))))
  805. (let ((A (make-ra-root (make-aseq) (vector (make-dim 4 1 3) (make-dim 3 1)))))
  806. (test-equal "#%1d@1:4(1 4 7 10)" (ra->string (ra-from A #t 2)))
  807. (test-equal "#%1d@1:3(3 4 5)" (ra->string (ra-from A 2 #t))))
  808. (test-end "from, other cases")
  809. ; -----------------------
  810. ; ra-amend! FIXME needs more tests
  811. ; -----------------------
  812. (test-begin "amend!")
  813. (define (ra-I . i) (ra-copy #t (apply ra-i i)))
  814. (define (amend-case A . i) (ra->string (apply ra-amend! A i)))
  815. (test-equal "#%2:2:3((a b c) (3 4 5))" (amend-case (ra-I 2 3) (array->ra #(a b c)) 0))
  816. (test-equal "#%2:2:3((0 1 2) (x y z))" (amend-case (ra-I 2 3) (array->ra #(x y z)) 1))
  817. (test-equal "#%2:2:3((a 1 2) (b 4 5))" (amend-case (ra-I 2 3) (array->ra #(a b)) #t 0))
  818. (test-equal "#%2:2:3((0 x 2) (3 y 5))" (amend-case (ra-I 2 3) (array->ra #(x y)) #t 1))
  819. (test-equal "#%2:2:3((x x x) (x x x))" (amend-case (ra-I 2 3) 'x))
  820. (test-equal "#%2:2:3((x x x) (x x x))" (amend-case (ra-I 2 3) (make-ra 'x)))
  821. (test-equal "#%2:2:3((a a a) (b b b))" (amend-case (ra-I 2 3) (array->ra #(a b))))
  822. (test-equal "#%2:2:3((x y z) (x y z))" (amend-case (ra-I 2 3) (ra-transpose (array->ra #(x y z)) 1)))
  823. (test-equal "#%2:2:3((0 x 2) (3 x 5))" (amend-case (ra-I 2 3) 'x (array->ra #(0 1)) 1))
  824. (test-equal "#%2:2:3((0 x 2) (3 x 5))" (amend-case (ra-I 2 3) 'x (ra-i 2) 1))
  825. (test-equal "#%2:2:3((a b c) (3 4 5))" (amend-case (ra-I 2 3) (array->ra #2((a b c))) (array->ra #(0))))
  826. (test-equal "#%2:2:3((0 1 2) (x y z))" (amend-case (ra-I 2 3) (array->ra #2((x y z))) (array->ra #(1))))
  827. (test-equal "#%2:2:3((a 1 2) (b 4 5))" (amend-case (ra-I 2 3) (array->ra #(a b)) #t 0))
  828. (test-equal "#%2:2:3((0 x 2) (3 y 5))" (amend-case (ra-I 2 3) (array->ra #(x y)) #t 1))
  829. (test-equal "#%2:2:3((a 1 2) (b 4 5))" (amend-case (ra-I 2 3) (array->ra #2((a) (b))) #t (array->ra #(0))))
  830. (test-equal "#%2:2:3((0 x 2) (3 y 5))" (amend-case (ra-I 2 3) (array->ra #2((x) (y))) #t (array->ra #(1))))
  831. (test-equal "#%2:2:3((x y 2) (j k 5))" (amend-case (ra-I 2 3) (array->ra #2((x y) (j k))) #t (array->ra #(0 1))))
  832. (test-equal "#%2:2:3((a 1 b) (3 4 5))" (amend-case (ra-I 2 3) (array->ra #(a b)) 0 (array->ra #(0 2))))
  833. (test-equal "#%2:2:3((0 x 2) (3 y 5))" (amend-case (ra-I 2 3) (array->ra #(x y)) #t 1))
  834. (test-equal "#%1:3(b 2 a)" (amend-case (list->ra 1 '(1 2 3)) (array->ra #(a b)) (array->ra #(2 0))))
  835. (test-equal "#%2:2:3((k 1 j) (y 4 x))" (amend-case (ra-I 2 3) (array->ra #2((x y) (j k))) (array->ra #(1 0)) (array->ra #(2 0))))
  836. (test-equal "#%2:2:3((0 x 2) (3 4 5))" (amend-case (ra-I 2 3) 'x 0 1))
  837. ; adapted from guile-ploy
  838. (test-equal "#%1:20(#f a a b b #f c c #f d #f d #f #f #f #f #f #f #f #f)"
  839. (amend-case (make-ra #f 20) (array->ra #(a b c d)) (array->ra #2((1 2) (3 4) (6 7) (9 11)))))
  840. ; with placeholders
  841. (test-equal "#%3:4:3:2(((0 x) (2 x) (4 x)) ((6 x) (8 x) (10 x)) ((12 x) (14 x) (16 x)) ((18 x) (20 x) (22 x)))"
  842. (amend-case (ra-I 4 3 2) 'x (dots) 1))
  843. (test-end "amend!")
  844. ; -----------------------
  845. ; ra-ravel
  846. ; -----------------------
  847. (test-begin "ravel")
  848. (test-assert (ra-order-c? (ra-i)))
  849. (test-assert (ra-order-c? (ra-i 2)))
  850. (test-assert (ra-order-c? (ra-i 2 3)))
  851. (test-assert (ra-order-c? (ra-i 2 3 4)))
  852. (test-assert (not (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 0 1 3 2))))
  853. (test-assert (not (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 0 1 3 2) 3)))
  854. (test-assert (ra-order-c? (ra-transpose (ra-i 1 2 3 4) 0 1 3 2) 2))
  855. (test-assert (ra-order-c? (ra-transpose (ra-i 1 2 3 4) 0 1 3 2) 1))
  856. (test-assert (not (ra-order-c? (ra-from (ra-i 1 2 3 4) #t #t #t (ra-iota 2 0 2))))) ; last step 1, packed
  857. (test-assert (ra-order-c? (ra-from (ra-i 1 2 3 4) #t #t #t (ra-iota 2 0 2)) 4))
  858. (test-assert (ra-order-c? (ra-tile (ra-i 1 2 3 4) 0 1)))
  859. (test-assert (ra-order-c? (ra-transpose (ra-i 5 1 3 1 4) 0 3 2 1)))
  860. (test-assert (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 1 0 2 3) 2 2))
  861. (test-assert (not (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 1 0 2 3) 2 1)))
  862. (test-assert (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 3 1 2 0) 2 1))
  863. (test-assert (not (ra-order-c? (ra-transpose (ra-i 2 3 4 5) 3 1 2 0) 2 2)))
  864. (test-equal "#%1:18(1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18)"
  865. (ra->string
  866. (ra-ravel
  867. (ra-transpose
  868. (make-ra-root (list->vector (iota 18 1)) (c-dims '(1 3) '(4 9)))
  869. 1 0))))
  870. (let ((ra (make-ra-root (list->vector (iota 18 1)) (c-dims '(1 3) '(4 9)))))
  871. (test-eq (ra-root ra) (ra-root (ra-ravel ra)))
  872. (test-equal "#%1:18(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)"
  873. (ra->string (ra-ravel ra))))
  874. (test-equal "#%1:1(3)" (ra->string (ra-ravel (make-ra 3))))
  875. ; partial ravel
  876. (define (test-ravel-n ra keep-root?)
  877. (let loop ((i (ra-rank ra)))
  878. (when (>= i 0)
  879. (let ((ri (ra-ravel ra i)))
  880. (when keep-root?
  881. (test-assert (ra-equal? (ra-ravel ri) (ra-ravel ra)))
  882. (test-eq (ra-root ra) (ra-root ri)))
  883. (test-equal (ra->list (ra-ravel ri)) (ra->list (ra-ravel ra)))
  884. (test-equal (+ (ra-rank ra) 1 (- i)) (ra-rank ri)))
  885. (loop (- i 1)))))
  886. (let* ((ra (ra-i 2 3 4 5 6))
  887. (rb (ra-from (ra-i 2 3 4 5 6) #t #t #t #t (ra-iota 3 0 2))))
  888. (test-ravel-n ra #t)
  889. (test-ravel-n rb #t)
  890. (test-ravel-n (ra-transpose ra 1 0) #f)
  891. (test-ravel-n (ra-transpose rb 1 0) #f))
  892. ; not prefix, free ravel
  893. (test-equal "#%2d:2:12((0 1 2 3 4 5 6 7 8 9 10 11) (12 13 14 15 16 17 18 19 20 21 22 23))"
  894. (ra->string (ra-ravel (ra-i 2 3 4) 2 1)))
  895. ; not prefix, copy required
  896. (test-assert "#%2:2:12((0 4 8 1 5 9 2 6 10 3 7 11) (12 16 20 13 17 21 14 18 22 15 19 23))"
  897. (ra->string (ra-ravel (ra-transpose (ra-i 2 3 4) 0 2 1) 2 1)))
  898. (test-end "ravel")
  899. ; -----------------------
  900. ; ra-reshape
  901. ; -----------------------
  902. (test-begin "reshape")
  903. (define (test-reshape k a shape check-shape)
  904. (let* ((b (apply ra-reshape a k shape)))
  905. (test-assert (ra-equal? (ra-ravel a) (ra-ravel b)))
  906. (test-eq (ra-root a) (ra-root b))
  907. (test-equal check-shape (ra-shape b))))
  908. (test-reshape 0 (ra-i 4 4)
  909. '(2 2) '((0 1) (0 1) (0 3)))
  910. (test-reshape 1 (ra-i 4 4)
  911. '(2 2) '((0 3) (0 1) (0 1)))
  912. (test-reshape 0 (ra-i 6)
  913. '(2 3) '((0 1) (0 2)))
  914. (test-reshape 0 (ra-i 6)
  915. '((2 4) (3 4)) '((2 4) (3 4)))
  916. (test-reshape 0 (ra-transpose (ra-i 3 2 4) 1 2 0)
  917. '(2 2) '((0 1) (0 1) (0 2) (0 1)))
  918. (test-reshape 0 (ra-from (make-ra-root (make-aseq) (c-dims '(4 9) '(1 4))) (ra-iota 4 5) (ra-iota 2 1))
  919. '((1 2) 2) '((1 2) (0 1) (0 1)))
  920. (test-reshape 0 (ra-transpose (make-ra-root (list->vector (iota 18 1)) (c-dims '(1 3) '(4 9))) 1 0)
  921. '(2 3) '((0 1) (0 2) (1 3)))
  922. (test-reshape 0 (ra-transpose (make-ra-root (make-aseq) (c-dims '(4 9) '(1 4))) 1 0)
  923. '((1 2) 2) '((1 2) (0 1) (4 9)))
  924. ; reshape of inf length - but lo must be set
  925. (let* ((a (ra-iota #f 9))
  926. (b (ra-reshape a 0 '(2 3))))
  927. (test-equal '((2 3)) (ra-shape b))
  928. (test-eq (ra-root a) (ra-root b))
  929. (test-equal 9 (ra-ref b 2))
  930. (test-equal 10 (ra-ref b 3)))
  931. ; reshape with placeholders
  932. (test-reshape 0 (ra-iota 8) '(#f 2) '((0 3) (0 1)))
  933. (test-reshape 0 (ra-iota 8) '(2 #f) '((0 1) (0 3)))
  934. (test-reshape 0 (ra-iota 8) '((1 4) #f) '((1 4) (0 1)))
  935. (test-reshape 1 (ra-i 5 12) '(2 #f 2) '((0 4) (0 1) (0 2) (0 1)))
  936. (test-reshape 0 (ra-i 12 5) '(2 #f 2) '((0 1) (0 2) (0 1) (0 4)))
  937. (test-end "reshape")
  938. ; -----------------------
  939. ; ra-tile
  940. ; -----------------------
  941. (test-begin "tile")
  942. (define (test-tile a shape check-shape)
  943. (let* ((b (apply ra-tile a 0 shape)))
  944. (ra-slice-for-each 2 (lambda (b) (test-assert (ra-equal? a b)))
  945. ; enable loop in the dead axis case since there's only that argument.
  946. (ra-singletonize b))
  947. (test-assert (eq? (ra-root a) (ra-root b)))
  948. (test-equal check-shape (ra-shape b))))
  949. (test-tile (ra-i 5)
  950. '(2 3) '((0 1) (0 2) (0 4)))
  951. (test-tile (ra-transpose (make-ra-root (make-aseq) (c-dims '(4 9) '(1 4))) 1 0)
  952. '(2 3) '((0 1) (0 2) (1 4) (4 9)))
  953. (test-tile (ra-transpose (make-ra-root (make-aseq) (c-dims '(4 9) '(1 4))) 1 0)
  954. '((2 4) (-1 3)) '((2 4) (-1 3) (1 4) (4 9)))
  955. (test-tile (ra-i 5)
  956. '(2 #f) '((0 1) (0 #f) (0 4)))
  957. (test-end "tile")
  958. ; -----------------------
  959. ; ra-rotate!
  960. ; -----------------------
  961. (test-begin "rotate")
  962. (let ((a (ra-copy #t (ra-i 4 3))))
  963. (ra-rotate! 0 a)
  964. (test-assert (ra-equal? (string->ra "#%2:4:3((0 1 2) (3 4 5) (6 7 8) (9 10 11))") a))
  965. (ra-rotate! 1 a)
  966. (test-assert (ra-equal? (string->ra "#%2:4:3((3 4 5) (6 7 8) (9 10 11) (0 1 2))") a))
  967. (ra-rotate! 2 a)
  968. (test-assert (ra-equal? (string->ra "#%2:4:3((9 10 11) (0 1 2) (3 4 5) (6 7 8))") a))
  969. (ra-rotate! 3 a)
  970. (test-assert (ra-equal? (string->ra "#%2:4:3((6 7 8) (9 10 11) (0 1 2) (3 4 5))") a))
  971. (ra-rotate! -1 a)
  972. (test-assert (ra-equal? (string->ra "#%2:4:3((3 4 5) (6 7 8) (9 10 11) (0 1 2))") a)))
  973. (let ((a (ra-copy #t (ra-i 4 3))))
  974. (test-assert (ra-equal? (string->ra "#%2:4:3((0 1 2) (3 4 5) (6 7 8) (9 10 11))") (ra-rotate! 0 a)))
  975. (test-assert (ra-equal? (string->ra "#%2:4:3((3 4 5) (6 7 8) (9 10 11) (0 1 2))") (ra-rotate! 1 a)))
  976. (test-assert (ra-equal? (string->ra "#%2:4:3((9 10 11) (0 1 2) (3 4 5) (6 7 8))") (ra-rotate! 2 a)))
  977. (test-assert (ra-equal? (string->ra "#%2:4:3((6 7 8) (9 10 11) (0 1 2) (3 4 5))") (ra-rotate! 3 a)))
  978. (test-assert (ra-equal? (string->ra "#%2:4:3((3 4 5) (6 7 8) (9 10 11) (0 1 2))") (ra-rotate! -1 a))))
  979. (let ((a (ra-i 4 3)))
  980. (test-assert (ra-equal? (string->ra "#%2:4:3((0 1 2) (3 4 5) (6 7 8) (9 10 11))") (ra-rotate 0 a)))
  981. (test-assert (ra-equal? (string->ra "#%2:4:3((3 4 5) (6 7 8) (9 10 11) (0 1 2))") (ra-rotate 1 a)))
  982. (test-assert (ra-equal? (string->ra "#%2:4:3((6 7 8) (9 10 11) (0 1 2) (3 4 5))") (ra-rotate 2 a)))
  983. (test-assert (ra-equal? (string->ra "#%2:4:3((9 10 11) (0 1 2) (3 4 5) (6 7 8))") (ra-rotate 3 a)))
  984. (test-assert (ra-equal? (string->ra "#%2:4:3((9 10 11) (0 1 2) (3 4 5) (6 7 8))") (ra-rotate -1 a))))
  985. (test-end "rotate")
  986. ; -----------------------
  987. ; ra-singletonize
  988. ; -----------------------
  989. (test-equal '(1 2 1 3) (ra-dimensions (ra-singletonize (ra-transpose (ra-i 2 3) 1 3))))
  990. ; -----------------------
  991. ; ra-clip
  992. ; -----------------------
  993. (test-begin "clip")
  994. (let* ((i0 (ra-transpose (ra-iota) 0))
  995. (i1 (ra-transpose (ra-iota) 1))
  996. (i2 (ra-transpose (ra-iota) 2))
  997. (f (lambda a (format #f "~{~a~}" a)))
  998. (b (ra-clip (ra-map! (make-ra-new #t 'a (c-dims '(4 6) '(0 2) '(0 4))) f i0 i1 i2)
  999. (ra-map! (make-ra-new #t 'b (c-dims '(2 9) '(1 4))) f i0 i1))))
  1000. (test-equal '((4 6) (1 2) (0 4)) (ra-shape b))
  1001. (test-equal
  1002. "#%3@4:3@1:2:5(((\"410\" \"411\" \"412\" \"413\" \"414\") (\"420\" \"421\" \"422\" \"423\" \"424\")) ((\"510\" \"511\" \"512\" \"513\" \"514\") (\"520\" \"521\" \"522\" \"523\" \"524\")) ((\"610\" \"611\" \"612\" \"613\" \"614\") (\"620\" \"621\" \"622\" \"623\" \"624\")))"
  1003. (ra->string b)))
  1004. (test-end "clip")
  1005. ; -----------------------
  1006. ; a bug in c0f60b1b76118a601715d331c265d1126b0d1e7c & before
  1007. ; -----------------------
  1008. (test-begin "bug in c0f60b1b76118a601715d331c265d1126b0d1e7c")
  1009. (let ()
  1010. (define ra0 (make-ra-root #f64(0 1 2 3 4)))
  1011. (define ra1 (ra-copy! (make-typed-ra 'f64 0 5) ra0))
  1012. (test-assert (ra-equal? ra0 ra1))
  1013. (test-equal (ra-ref ra0 4) (ra-ref ra1 4)))
  1014. (let ()
  1015. (define ra0 (make-ra-root #u8(0 1 2 3 4)))
  1016. (define ra1 (ra-copy! (make-typed-ra 'u8 0 5) ra0))
  1017. (test-assert (ra-equal? ra0 ra1))
  1018. (test-equal (ra-ref ra0 4) (ra-ref ra1 4)))
  1019. (test-end "bug in c0f60b1b76118a601715d331c265d1126b0d1e7c")
  1020. ; -----------------------
  1021. ; ra-slice creates a new descriptor with no shared dim vector
  1022. ; -----------------------
  1023. (let ()
  1024. (define ra0 (ra-i 2 3 4))
  1025. (define v0 (vector-copy (ra-dims ra0)))
  1026. (define ra1 (ra-slice ra0))
  1027. (test-assert (not (eq? (ra-dims ra0) (ra-dims ra1))))
  1028. (vector-for-each (lambda (a b) (test-equal a b))
  1029. v0 (ra-dims ra1))
  1030. (vector-fill! (ra-dims ra1) #f)
  1031. (vector-for-each (lambda (a b) (test-equal a b))
  1032. v0 (ra-dims ra0)))
  1033. ; -----------------------
  1034. ; concatenation
  1035. ; -----------------------
  1036. (test-begin "cat")
  1037. (test-assert (ra-equal? (ra-cat #t 0 (ra-i 1 2) (ra-i 2 2)) (array->ra #2((0 1) (0 1) (2 3)))))
  1038. (test-assert (ra-equal? (ra-cat #t 0 (ra-iota 2 1) (ra-iota 3 3)) (array->ra #1(1 2 3 4 5))))
  1039. (test-assert (ra-equal? (ra-cat #t -1 (ra-iota 2 1) (ra-iota 2 4)) (array->ra #2((1 2) (4 5)))))
  1040. (test-assert (ra-equal? (ra-cat #t 1 (ra-iota 2 1) (ra-iota 2 4)) (array->ra #2((1 4) (2 5)))))
  1041. (test-assert (ra-equal? (ra-cat #t 0 (make-ra 'a) (ra-iota 2)) (array->ra #1(a 0 1))))
  1042. (test-assert (ra-equal? (ra-cat #t 1 (make-ra 'a) (ra-iota 2)) (array->ra #2((a 0) (a 1)))))
  1043. (test-assert (ra-equal? (ra-cat #t -1 (make-ra 'a) (ra-iota 2)) (array->ra #2((a a) (0 1)))))
  1044. (test-assert (ra-equal? (ra-cat #t 1 (array->ra #(a b)) (ra-i 2 2)) (array->ra #2((a 0 1) (b 2 3)))))
  1045. (test-assert (ra-equal? (ra-cat #t 0 (array->ra #(a b)) (ra-i 2 2)) (array->ra #2((a a) (b b) (0 1) (2 3)))))
  1046. (test-assert (ra-equal? (ra-cats #t 0 (make-ra 'a) (make-ra 'b) (make-ra 'c)) (array->ra #1(a b c))))
  1047. (test-assert (ra-equal? (ra-cats #t 1 (make-ra 'a) (make-ra 'b) (make-ra 'c)) (array->ra #2((a) (b) (c)))))
  1048. (test-assert (ra-equal? (ra-cats #t 0 (array->ra #(1 2 3)) (make-ra 4) (array->ra #(5 6))) (array->ra #1(1 2 3 4 5 6))))
  1049. (test-assert (ra-equal? (ra-cats #t 0 (array->ra #2((0 1) (2 3))) (array->ra #(a b))) (array->ra #2((0 1 a b) (2 3 a b)))))
  1050. (test-assert (ra-equal? (ra-cats #t 1 (array->ra #2((0 1) (2 3))) (array->ra #(a b))) (array->ra #2((0 1) (2 3) (a b)))))
  1051. (test-assert (ra-equal? (ra-cats #t 0 (array->ra #2((0 1))) (array->ra #(a))) (array->ra #2((0 1 a)))))
  1052. (test-assert (ra-equal? (ra-cats #t -1 (array->ra #(1 2 3)) (array->ra #(a b c))) (array->ra #2((1 a) (2 b) (3 c)))))
  1053. (test-assert (ra-equal? (ra-cats #t -1 (make-ra 'a) (array->ra #(x y z))) (array->ra #2((a x) (a y) (a z)))))
  1054. ; non-zero base indices on the concat axis
  1055. (let* ((a0 (ra-i 2 4))
  1056. (a1 (ra-reshape a0 0 '(1 2)))
  1057. (b (ra-i 1 4)))
  1058. (test-assert (ra-equal? (ra-cat #t 0 a0 b) (ra-cat #t 0 a1 b))))
  1059. (let* ((a0 (ra-i 4 2))
  1060. (a1 (ra-untranspose (ra-reshape (ra-transpose a0 1 0) 0 '(1 2)) 1 0))
  1061. (b (ra-i 4 1)))
  1062. (test-assert (ra-equal? (ra-cats #t 0 a0 b) (ra-cats #t 0 a1 b))))
  1063. ; non-zero base indices on a non-concat axis
  1064. (let ((a (make-ra 'a '(1 2) '(1 4)))
  1065. (b (make-ra 'b '(1 3) '(1 4))))
  1066. (test-equal "#%2:5@1:4((a a a a) (a a a a) (b b b b) (b b b b) (b b b b))"
  1067. (ra->string (ra-cat #t 0 a b)))
  1068. (test-equal "#%2:5@1:4((a a a a) (a a a a) (b b b b) (b b b b) (b b b b))"
  1069. (ra->string (ra-cats #t 1 a b))))
  1070. (test-end "cat")
  1071. ; -----------------------
  1072. ; ra-map
  1073. ; -----------------------
  1074. ; this requires ra-slice-for-each-check to fix axes where largest rank argument has dead
  1075. (test-equal "#%2f64:3:4((1.0 2.0 3.0 4.0) (2.0 4.0 6.0 8.0) (3.0 6.0 9.0 12.0))"
  1076. (ra->string (ra-map 'f64 * (ra-iota 3 1) (ra-transpose (ra-iota 4 1) 1))))
  1077. ; lower bounds are respected
  1078. (test-equal "#%2:2@1:3((9 9 9) (18 18 18))"
  1079. (ra->string (ra-map #t * (ra-iota 2 1) (ra-transpose (make-ra 9 '(1 3)) 1))))
  1080. (test-equal "#%2:3:4((1 2 3 4) (6 7 8 9) (11 12 13 14))"
  1081. (ra->string (ra-map #f + (ra-iota 3 1) (ra-i 3 4))))
  1082. ; -----------------------
  1083. ; ra-format
  1084. ; -----------------------
  1085. (test-begin "format")
  1086. (define port #f) ; no real test, but at least check that they run. Use #t to demo
  1087. (let* ((ra (ra-i 1 8 9 3))
  1088. (ra (ra-map! (ra-copy #t ra) sqrt ra)))
  1089. (ra-format ra port #:fmt "~4,2f"))
  1090. (let ((ra (ra-set! (ra-set! (ra-copy #t (ra-i 3 4 3))
  1091. (ra-i 2 3) 1 2 1)
  1092. (make-ra-root #(hello world of ras) (c-dims 2 2))
  1093. 2 1 2)))
  1094. (ra-format ra port #:fmt "~a"))
  1095. (let ((ra (ra-i 2 2 2 2 2 2 2 2)))
  1096. (ra-format ra port #:compact 1))
  1097. (let* ((ra (ra-set! (ra-copy #t (ra-i 7)) (make-ra-root #(hello world of ras) (c-dims 2 2)) 3))
  1098. (ra (ra-tile ra (ra-rank ra) 1)))
  1099. (ra-format ra port))
  1100. ; example from srfi-163.
  1101. (let* ((arr
  1102. #2@1:2@1:3((#2((1 2) (3 4)) 9 #2((3 4) (5 6)))
  1103. (#(42 43) #2((8 7 6)) #2((90 91) (100 101)))))
  1104. (ra (array->ra arr))
  1105. (ra (ra-map! (ra-copy ra) (lambda (x) (if (array? x) (array->ra x) x)) ra)))
  1106. (ra-format ra port #:prefix? #t #:compact 1)
  1107. (ra-format ra port #:prefix? #t #:compact 0))
  1108. (let ((ra (array->ra #2@1:2@1:3((1 2 3) (4 5 6)))))
  1109. (ra-format ra port))
  1110. (let ((ra (make-ra 'zero)))
  1111. (ra-format ra port))
  1112. ; shorter than the prefix; was a bug
  1113. (let ((ra (make-ra 'x)))
  1114. (ra-format ra port))
  1115. ; when size is 0, just print the prefix
  1116. (ra-format (ra-i 3 0 4) port)
  1117. ; when size is unbounded, just print the prefix. Not ideal...
  1118. (ra-format (ra-i #t 3 4) port)
  1119. ; when there are dead axes, print the result of ra-singletonize, except for the prefix. Not ideal...
  1120. (ra-format (ra-i 3 #f 4) port)
  1121. (test-end "format")
  1122. ; -----------------------
  1123. ; the end.
  1124. ; -----------------------
  1125. (define error-count (test-runner-fail-count (test-runner-current)))
  1126. (test-end "newra")
  1127. (exit error-count)