common.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ; Test helpers.
  2. ; (c) Daniel Llorens - 2014, 2020
  3. ; This library is free software; you can redistribute it and/or modify it under
  4. ; the terms of the GNU Lesser 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-11))
  8. ; ---------------------------------
  9. ; Various sorts of arrays.
  10. ; ---------------------------------
  11. (define (make-v-compact type)
  12. (make-typed-array type *unspecified* 10))
  13. (define (make-v-strided type)
  14. (make-shared-array (make-typed-array type *unspecified* 20) (lambda (i) (list (* i 2))) 10))
  15. (define (make-v-offset type)
  16. (make-shared-array (make-typed-array type *unspecified* 12) (lambda (i) (list (+ i 2))) 10))
  17. (define (make-v-strided-reversed type)
  18. (make-shared-array (make-typed-array type *unspecified* 20) (lambda (i) (list (- 19 (* i 2)))) 10))
  19. (define (make-v-z type)
  20. (make-shared-array (make-typed-array type *unspecified*) (lambda (i) (list)) 10))
  21. (define (fill-A1! A)
  22. (case (array-type A)
  23. ((f32 f64) (array-index-map! A (lambda (i) (- i 4))))
  24. ((c32 c64) (array-index-map! A (lambda (i) (make-rectangular (+ i 1) (- i 5)))))
  25. (else (throw 'bad-array-type (array-type A))))
  26. A)
  27. (define (fill-B1! A)
  28. (case (array-type A)
  29. ((f32 f64) (array-index-map! A (lambda (i) (+ i 3))))
  30. ((c32 c64) (array-index-map! A (lambda (i) (make-rectangular (+ i 2) (- 5 i)))))
  31. (else (throw 'bad-array-type (array-type A))))
  32. A)
  33. (define (make-M-c-order type)
  34. (make-typed-array type *unspecified* 10 10))
  35. (define (make-M-fortran-order type)
  36. (transpose-array (make-typed-array type *unspecified* 10 10) 1 0))
  37. (define (make-M-strided type)
  38. (make-shared-array (make-typed-array type *unspecified* 20 10) (lambda (i j) (list (* i 2) j)) 10 10))
  39. (define (make-M-strided-both type)
  40. (make-shared-array (make-typed-array type *unspecified* 20 20) (lambda (i j) (list (* i 2) (* j 2))) 10 10))
  41. (define (make-M-strided-reversed type)
  42. (make-shared-array (make-typed-array type *unspecified* 20 20) (lambda (i j) (list (- 19 (* i 2)) (- 19 (* j 2)))) 10 10))
  43. (define (make-M-offset type)
  44. (make-shared-array (make-typed-array type *unspecified* 12 13) (lambda (i j) (list (+ i 2) (+ j 3))) 10 10))
  45. (define (make-M-z0 type)
  46. (make-shared-array (make-typed-array type *unspecified* 12) (lambda (i j) (list (+ 2 j))) 10 10))
  47. (define (make-M-z1 type)
  48. (make-shared-array (make-typed-array type *unspecified* 12) (lambda (i j) (list (+ 2 i))) 10 10))
  49. (define (make-M-z00 type)
  50. (make-shared-array (make-typed-array type *unspecified*) (lambda (i j) (list)) 10 10))
  51. (define (make-M-overlap type)
  52. (make-shared-array (make-typed-array type *unspecified* 20) (lambda (i j) (list (+ i j))) 10 10))
  53. (define (make-M-overlap-reversed type)
  54. (make-shared-array (make-typed-array type *unspecified* 20) (lambda (i j) (list (- 18 i j))) 10 10))
  55. (define (fill-A2! A)
  56. (case (array-type A)
  57. ((f32 f64) (array-index-map!
  58. A (lambda (i j) (+ 4 (* i 1) (* j j 2)))))
  59. ((c32 c64) (array-index-map!
  60. A (lambda (i j) (make-rectangular (+ 4 (* i 1) (* j j 2)) (+ -3 (* i i 1) (* j 2) -4)))))
  61. (else (throw 'bad-array-type (array-type A))))
  62. A)
  63. ; ---------------------------------
  64. ; Utilities
  65. ; ---------------------------------
  66. (define (conj a) (make-rectangular (real-part a) (- (imag-part a))))
  67. (define (array-copy A)
  68. (let ((B (apply make-typed-array (array-type A) *unspecified* (array-dimensions A))))
  69. (array-copy! A B)
  70. B))
  71. (define (array-map op A)
  72. (let ((B (apply make-typed-array (array-type A) *unspecified* (array-dimensions A))))
  73. (array-map! B op A)
  74. B))
  75. (define (list-product . rest)
  76. "make a list of all lists with 1st element from 1st arg, 2nd element from
  77. (list-product '(1 2)) => ((1) (2))
  78. (list-product '(1 2) '(3 4)) => ((1 3) (1 4) (2 3) (2 4))
  79. (list-product '(1 2) '(3 4) '(5 6)) => ((1 3 5) (1 3 6) etc.)"
  80. (cond
  81. ((null? rest)
  82. '())
  83. ((null? (cdr rest))
  84. (map list (car rest)))
  85. (else
  86. (let ((list-product-rest (apply list-product (cdr rest))))
  87. (append-map!
  88. (lambda (p)
  89. (map
  90. (lambda (v) (cons p v))
  91. list-product-rest))
  92. (car rest))))))