| 1 |
;;; |
| 2 |
;;; Array operations. This is superset of SRFI-25. |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2002-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; Copyright(C) 2004 by Alex Shinn ([email protected]) |
| 6 |
;;; |
| 7 |
;;; Permission to use, copy, modify, distribute this software and |
| 8 |
;;; accompanying documentation for any purpose is hereby granted, |
| 9 |
;;; provided that existing copyright notices are retained in all |
| 10 |
;;; copies and that this notice is included verbatim in all |
| 11 |
;;; distributions. |
| 12 |
;;; This software is provided as is, without express or implied |
| 13 |
;;; warranty. In no circumstances the author(s) shall be liable |
| 14 |
;;; for any damages arising out of the use of this software. |
| 15 |
;;; |
| 16 |
|
| 17 |
;; Conceptually, an array is a backing storage and a procedure to |
| 18 |
;; map n-dimensional indices to an index of the backing storage. |
| 19 |
|
| 20 |
(define-module gauche.array |
| 21 |
(use scheme.list) |
| 22 |
(use gauche.uvector) |
| 23 |
(use gauche.collection) |
| 24 |
(use gauche.sequence) |
| 25 |
(export <array-meta> <array> |
| 26 |
<u8array> <s8array> <u16array> <s16array> <u32array> <s32array> |
| 27 |
<u64array> <s64array> <f16array> <f32array> <f64array> |
| 28 |
array? make-array array-copy shape array array-rank |
| 29 |
array-start array-end array-ref array-set! |
| 30 |
share-array subarray array-equal? |
| 31 |
array-valid-index? shape-valid-index? |
| 32 |
array-shape array-length array-size |
| 33 |
array-for-each-index shape-for-each |
| 34 |
array-for-each-index-by-dimension |
| 35 |
array-for-each array-every array-any |
| 36 |
tabulate-array array-retabulate! |
| 37 |
array-map array-map! array->vector array->list |
| 38 |
make-u8array make-s8array make-u16array make-s16array |
| 39 |
make-u32array make-s32array make-u64array make-s64array |
| 40 |
make-f16array make-f32array make-f64array |
| 41 |
u8array s8array u16array s16array u32array s32array |
| 42 |
u64array s64array f16array f32array f64array |
| 43 |
array-concatenate array-transpose array-rotate-90 |
| 44 |
array-flip array-flip! |
| 45 |
identity-array array-inverse determinant determinant! |
| 46 |
array-mul array-vector-mul vector-array-mul array-expt |
| 47 |
array-div-left array-div-right |
| 48 |
array-add-elements array-add-elements! |
| 49 |
array-sub-elements array-sub-elements! |
| 50 |
array-negate-elements array-negate-elements! |
| 51 |
array-mul-elements array-mul-elements! |
| 52 |
array-div-elements array-div-elements! |
| 53 |
array-reciprocate-elements array-reciprocate-elements! |
| 54 |
pretty-print-array |
| 55 |
)) |
| 56 |
(select-module gauche.array) |
| 57 |
|
| 58 |
(autoload "gauche/matrix" |
| 59 |
array-concatenate array-transpose array-rotate-90 array-flip array-flip! |
| 60 |
identity-array array-inverse determinant determinant! |
| 61 |
array-mul array-vector-mul vector-array-mul array-expt |
| 62 |
array-div-left array-div-right |
| 63 |
array-add-elements array-add-elements! |
| 64 |
array-sub-elements array-sub-elements! |
| 65 |
array-negate-elements array-negate-elements! |
| 66 |
array-mul-elements array-mul-elements! |
| 67 |
array-div-elements array-div-elements! |
| 68 |
array-reciprocate-elements array-reciprocate-elements! |
| 69 |
pretty-print-array) |
| 70 |
|
| 71 |
(define-class <array-meta> (<class>) |
| 72 |
((backing-storage-creator :init-keyword :backing-storage-creator |
| 73 |
:getter backing-storage-creator-of) |
| 74 |
(backing-storage-getter :init-keyword :backing-storage-getter |
| 75 |
:getter backing-storage-getter-of) |
| 76 |
(backing-storage-setter :init-keyword :backing-storage-setter |
| 77 |
:getter backing-storage-setter-of) |
| 78 |
(backing-storage-length :init-keyword :backing-storage-length |
| 79 |
:getter backing-storage-length-of)) |
| 80 |
) |
| 81 |
|
| 82 |
;; auto reader and writer for array-meta |
| 83 |
|
| 84 |
(define-method initialize ((class <array-meta>) initargs) |
| 85 |
(next-method) |
| 86 |
(let ((name (class-name class))) |
| 87 |
(when name |
| 88 |
(define-reader-ctor name |
| 89 |
(^[sh . inits] |
| 90 |
(list-fill-array! (make-array-internal class (apply shape sh)) inits)))))) |
| 91 |
|
| 92 |
(define-class <array-base> () |
| 93 |
((start-vector :init-keyword :start-vector :getter start-vector-of) |
| 94 |
(end-vector :init-keyword :end-vector :getter end-vector-of) |
| 95 |
(mapper :init-keyword :mapper :getter mapper-of) |
| 96 |
(getter :getter getter-of) |
| 97 |
(setter :getter setter-of) |
| 98 |
(backing-storage :init-keyword :backing-storage |
| 99 |
:getter backing-storage-of)) |
| 100 |
:metaclass <array-meta>) |
| 101 |
|
| 102 |
(define-method initialize ((self <array-base>) initargs) |
| 103 |
(next-method) |
| 104 |
(let ([get (backing-storage-getter-of (class-of self))] |
| 105 |
[set (backing-storage-setter-of (class-of self))] |
| 106 |
[store (backing-storage-of self)]) |
| 107 |
(set! (slot-ref self 'getter) (^[index] (get store index))) |
| 108 |
(set! (slot-ref self 'setter) (^[index value] (set store index value))))) |
| 109 |
|
| 110 |
(define-method write-object ((self <array-base>) port) |
| 111 |
(format port "#,(~A ~S" (class-name (class-of self)) |
| 112 |
(array->list (array-shape self))) |
| 113 |
(array-for-each (cut format port " ~S" <>) self) |
| 114 |
(format port ")")) |
| 115 |
|
| 116 |
(define-class <array> (<array-base>) |
| 117 |
() |
| 118 |
:backing-storage-creator make-vector |
| 119 |
:backing-storage-getter vector-ref |
| 120 |
:backing-storage-setter vector-set! |
| 121 |
:backing-storage-length vector-length) |
| 122 |
|
| 123 |
(define-class <u8array> (<array-base>) |
| 124 |
() |
| 125 |
:backing-storage-creator make-u8vector |
| 126 |
:backing-storage-getter u8vector-ref |
| 127 |
:backing-storage-setter u8vector-set! |
| 128 |
:backing-storage-length u8vector-length) |
| 129 |
|
| 130 |
(define-class <s8array> (<array-base>) |
| 131 |
() |
| 132 |
:backing-storage-creator make-s8vector |
| 133 |
:backing-storage-getter s8vector-ref |
| 134 |
:backing-storage-setter s8vector-set! |
| 135 |
:backing-storage-length s8vector-length) |
| 136 |
|
| 137 |
(define-class <u16array> (<array-base>) |
| 138 |
() |
| 139 |
:backing-storage-creator make-u16vector |
| 140 |
:backing-storage-getter u16vector-ref |
| 141 |
:backing-storage-setter u16vector-set! |
| 142 |
:backing-storage-length u16vector-length) |
| 143 |
|
| 144 |
(define-class <s16array> (<array-base>) |
| 145 |
() |
| 146 |
:backing-storage-creator make-s16vector |
| 147 |
:backing-storage-getter s16vector-ref |
| 148 |
:backing-storage-setter s16vector-set! |
| 149 |
:backing-storage-length s16vector-length) |
| 150 |
|
| 151 |
(define-class <u32array> (<array-base>) |
| 152 |
() |
| 153 |
:backing-storage-creator make-u32vector |
| 154 |
:backing-storage-getter u32vector-ref |
| 155 |
:backing-storage-setter u32vector-set! |
| 156 |
:backing-storage-length u32vector-length) |
| 157 |
|
| 158 |
(define-class <s32array> (<array-base>) |
| 159 |
() |
| 160 |
:backing-storage-creator make-s32vector |
| 161 |
:backing-storage-getter s32vector-ref |
| 162 |
:backing-storage-setter s32vector-set! |
| 163 |
:backing-storage-length s32vector-length) |
| 164 |
|
| 165 |
(define-class <u64array> (<array-base>) |
| 166 |
() |
| 167 |
:backing-storage-creator make-u64vector |
| 168 |
:backing-storage-getter u64vector-ref |
| 169 |
:backing-storage-setter u64vector-set! |
| 170 |
:backing-storage-length u64vector-length) |
| 171 |
|
| 172 |
(define-class <s64array> (<array-base>) |
| 173 |
() |
| 174 |
:backing-storage-creator make-s64vector |
| 175 |
:backing-storage-getter s64vector-ref |
| 176 |
:backing-storage-setter s64vector-set! |
| 177 |
:backing-storage-length s64vector-length) |
| 178 |
|
| 179 |
(define-class <f16array> (<array-base>) |
| 180 |
() |
| 181 |
:backing-storage-creator make-f16vector |
| 182 |
:backing-storage-getter f16vector-ref |
| 183 |
:backing-storage-setter f16vector-set! |
| 184 |
:backing-storage-length f16vector-length) |
| 185 |
|
| 186 |
(define-class <f32array> (<array-base>) |
| 187 |
() |
| 188 |
:backing-storage-creator make-f32vector |
| 189 |
:backing-storage-getter f32vector-ref |
| 190 |
:backing-storage-setter f32vector-set! |
| 191 |
:backing-storage-length f32vector-length) |
| 192 |
|
| 193 |
(define-class <f64array> (<array-base>) |
| 194 |
() |
| 195 |
:backing-storage-creator make-f64vector |
| 196 |
:backing-storage-getter f64vector-ref |
| 197 |
:backing-storage-setter f64vector-set! |
| 198 |
:backing-storage-length f64vector-length) |
| 199 |
|
| 200 |
;; internal |
| 201 |
(define-inline (%xvector-copy v) |
| 202 |
(cond [(vector? v) (vector-copy v)] |
| 203 |
[(uvector? v) (uvector-copy v)] |
| 204 |
[else (error "Vector or uvector required, but got:" v)])) |
| 205 |
|
| 206 |
(define (array-copy a) |
| 207 |
(assume-type a <array-base>) |
| 208 |
(make (class-of a) |
| 209 |
:start-vector (start-vector-of a) |
| 210 |
:end-vector (end-vector-of a) |
| 211 |
:mapper (mapper-of a) |
| 212 |
:backing-storage (%xvector-copy (backing-storage-of a)))) |
| 213 |
|
| 214 |
;;------------------------------------------------------------- |
| 215 |
;; Affine mapper |
| 216 |
;; |
| 217 |
;; Given begin-vector Vb = #s32(b0 b1 ... bN) |
| 218 |
;; end-vector Ve = #s32(e0 e1 ... eN) |
| 219 |
;; where b0 <= e0, ..., bN <= eN |
| 220 |
;; Returns a procedure, |
| 221 |
;; which calculates 1-dimentional offset off to the backing storage, |
| 222 |
;; from given index vector #s32(i0 i1 ... iN) |
| 223 |
;; |
| 224 |
;; Pre-calculation: |
| 225 |
;; sizes s0 = e0 - b0, s1 = e1 - b1 ... |
| 226 |
;; coefficients c0 = s1 * s2 * ... * sN |
| 227 |
;; c1 = s2 * ... * sN |
| 228 |
;; cN-1 = sN |
| 229 |
;; cN = 1 |
| 230 |
;; |
| 231 |
;; Mapping |
| 232 |
;; off = c0*(i0-b0) + c1*(i1-b1) + .. + cN*(iN-bN) |
| 233 |
;; |
| 234 |
;; |
| 235 |
|
| 236 |
(define (zero-vector? vec) |
| 237 |
(not (s32vector-range-check vec 0 0))) |
| 238 |
|
| 239 |
(define (generate-amap Vb Ve) |
| 240 |
(let* ([rank (s32vector-length Vb)] |
| 241 |
[indices (iota rank)] |
| 242 |
[Vs (s32vector-sub Ve Vb)] |
| 243 |
[-Vb (map-to <s32vector> - Vb)] |
| 244 |
[Ve-1 (s32vector-sub Ve 1)] |
| 245 |
[vcl (fold-right (^[sN l] (cons (* sN (car l)) l)) |
| 246 |
'(1) |
| 247 |
(s32vector->list Vs))] |
| 248 |
[Vc (coerce-to <s32vector> (cdr vcl))]) |
| 249 |
(^[Vi] |
| 250 |
(cond [(s32vector-range-check Ve-1 Vi #f) |
| 251 |
=> (^i (errorf "index of dimension ~s is too big: ~s" |
| 252 |
i (ref Vi i)))] |
| 253 |
[(s32vector-range-check Vb #f Vi) |
| 254 |
=> (^i (errorf "index of dimension ~s is too small: ~s" |
| 255 |
i (ref Vi i)))] |
| 256 |
[else (s32vector-dot Vc (s32vector-add -Vb Vi))])))) |
| 257 |
|
| 258 |
;; shape index tests |
| 259 |
|
| 260 |
(define-method shape-valid-index? ((sh <array-base>) (ind <s32vector>)) |
| 261 |
(receive (Vb Ve) (shape->start/end-vector sh) |
| 262 |
(and |
| 263 |
(= (s32vector-length ind) (s32vector-length Vb)) |
| 264 |
(not (s32vector-range-check ind Vb (s32vector-sub Ve 1)))))) |
| 265 |
|
| 266 |
(define-method shape-valid-index? ((sh <array-base>) (ind <vector>)) |
| 267 |
(shape-valid-index? sh (vector->s32vector ind))) |
| 268 |
(define-method shape-valid-index? ((sh <array-base>) (ind <pair>)) |
| 269 |
(shape-valid-index? sh (list->s32vector ind))) |
| 270 |
(define-method shape-valid-index? ((sh <array-base>) (ind <array>)) |
| 271 |
(shape-valid-index? sh (vector->s32vector (array->vector ind)))) |
| 272 |
(define-method shape-valid-index? ((sh <array-base>) (ind <integer>) . more-index) |
| 273 |
(shape-valid-index? sh (list->s32vector (cons ind more-index)))) |
| 274 |
(define-method shape-valid-index? ((sh <array-base>)) |
| 275 |
;; special case - zero dimensional array |
| 276 |
(null? (array->list sh))) |
| 277 |
|
| 278 |
;; array index tests |
| 279 |
|
| 280 |
(define (array-valid-index? ar . args) |
| 281 |
(apply shape-valid-index? (array-shape ar) args)) |
| 282 |
|
| 283 |
;;--------------------------------------------------------------- |
| 284 |
;; Shape |
| 285 |
;; ... is a special array that has a few constraints; |
| 286 |
;; that is, suppose S is a shape, then: |
| 287 |
;; (1) the shape of S is [0, d, 0, 2], where d is the rank |
| 288 |
;; of the array S represents. |
| 289 |
;; (2) all the elements of S are exact integers. |
| 290 |
;; (3) (array-ref S n 0) <= (array-ref S n 1) where 0 <= n <= d |
| 291 |
;; |
| 292 |
|
| 293 |
(define (shape . args) |
| 294 |
(let1 arglen (length args) |
| 295 |
(unless (even? arglen) |
| 296 |
(error "shape arguments not even" args)) |
| 297 |
(let* ([rank (quotient arglen 2)] |
| 298 |
[back (make-vector arglen)]) |
| 299 |
;; check vailidity of the shape |
| 300 |
(do ([l args (cddr l)]) |
| 301 |
[(null? l)] |
| 302 |
(unless (and (exact? (car l)) (integer? (car l))) |
| 303 |
(error "exact integer required for shape, but got" (car l))) |
| 304 |
(unless (and (exact? (cadr l)) (integer? (cadr l))) |
| 305 |
(error "exact integer required for shape, but got" (cadr l))) |
| 306 |
(unless (<= (car l) (cadr l)) |
| 307 |
(errorf "beginning index ~s is larger than ending index ~s in shape argument: ~s" (car l) (cadr l) args))) |
| 308 |
;; make array. |
| 309 |
(make <array> |
| 310 |
:start-vector (s32vector 0 0) |
| 311 |
:end-vector (s32vector rank 2) |
| 312 |
:mapper (^[Vi] (s32vector-dot (s32vector 2 1) Vi)) |
| 313 |
:backing-storage (list->vector args))))) |
| 314 |
|
| 315 |
(define (shape->start/end-vector shape) |
| 316 |
(let* ([rank (array-end shape 0)] |
| 317 |
[cnt (iota rank)]) |
| 318 |
(values (map-to <s32vector> (^i (array-ref shape i 0)) cnt) |
| 319 |
(map-to <s32vector> (^i (array-ref shape i 1)) cnt)))) |
| 320 |
|
| 321 |
(define (start/end-vector->shape Vb Ve) |
| 322 |
(define (interleave a b) |
| 323 |
(cond [(null? a) b] |
| 324 |
[(null? b) a] |
| 325 |
[else (cons (car a) (interleave b (cdr a)))])) |
| 326 |
(apply shape (interleave (s32vector->list Vb) (s32vector->list Ve)))) |
| 327 |
|
| 328 |
;;--------------------------------------------------------------- |
| 329 |
;; Make general array |
| 330 |
;; |
| 331 |
|
| 332 |
(define (make-array-internal class shape . maybe-init) |
| 333 |
(receive (Vb Ve) (shape->start/end-vector shape) |
| 334 |
(make class |
| 335 |
:start-vector Vb |
| 336 |
:end-vector Ve |
| 337 |
:mapper (generate-amap Vb Ve) |
| 338 |
:backing-storage (apply (backing-storage-creator-of class) |
| 339 |
(fold * 1 (s32vector-sub Ve Vb)) |
| 340 |
maybe-init)))) |
| 341 |
|
| 342 |
(define (list-fill-array! a inits) |
| 343 |
(let* ([bv (backing-storage-of a)] |
| 344 |
[set (backing-storage-setter-of (class-of a))] |
| 345 |
[len ((backing-storage-length-of (class-of a)) bv)]) |
| 346 |
(unless (= (length inits) len) |
| 347 |
(errorf "array element initializer doesn't match the shape ~s [~s]: ~s" |
| 348 |
(array->list (array-shape a)) len inits)) |
| 349 |
(do ([i 0 (+ i 1)] |
| 350 |
[inits inits (cdr inits)]) |
| 351 |
[(= i len) a] |
| 352 |
(set bv i (car inits))))) |
| 353 |
|
| 354 |
(define-macro (define-array-ctors . pfxs) |
| 355 |
(define (build-ctor-def pfx) |
| 356 |
(define maker (symbol-append 'make- pfx 'array)) |
| 357 |
(define ctor (symbol-append pfx 'array)) |
| 358 |
(define class (symbol-append '< pfx 'array>)) |
| 359 |
`((define (,maker shape . opt) |
| 360 |
(apply make-array-internal ,class shape opt)) |
| 361 |
(define (,ctor shape . inits) |
| 362 |
(list-fill-array! (,maker shape) inits)))) |
| 363 |
`(begin ,@(append-map build-ctor-def pfxs))) |
| 364 |
|
| 365 |
(define-array-ctors || u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64) |
| 366 |
|
| 367 |
(define (subarray ar sh) |
| 368 |
(receive (Vb Ve) (shape->start/end-vector sh) |
| 369 |
(let* ([rank (s32vector-length Vb)] |
| 370 |
[Vb2 (make-s32vector rank 0)] |
| 371 |
[Ve2 (s32vector-sub Ve Vb)] |
| 372 |
[new-shape (start/end-vector->shape Vb2 Ve2)] |
| 373 |
[res (make-array-internal (class-of ar) new-shape)]) |
| 374 |
(array-retabulate! res new-shape |
| 375 |
(^[ind] (array-ref ar (s32vector->vector |
| 376 |
(s32vector-add Vb ind)))) |
| 377 |
(make-vector rank)) |
| 378 |
res))) |
| 379 |
|
| 380 |
(define (array? obj) |
| 381 |
(is-a? obj <array-base>)) |
| 382 |
|
| 383 |
(define (array-rank array) |
| 384 |
(unless (array? array) (error "array required, but got" array)) |
| 385 |
(s32vector-length (start-vector-of array))) |
| 386 |
|
| 387 |
(define (array-start array k) |
| 388 |
(unless (array? array) (error "array required, but got" array)) |
| 389 |
(s32vector-ref (start-vector-of array) k)) |
| 390 |
|
| 391 |
(define (array-end array k) |
| 392 |
(unless (array? array) (error "array required, but got" array)) |
| 393 |
(s32vector-ref (end-vector-of array) k)) |
| 394 |
|
| 395 |
;;--------------------------------------------------------------- |
| 396 |
;; Array ref and set! |
| 397 |
;; |
| 398 |
|
| 399 |
(define-method array-ref ((array <array-base>) (index <integer>) . more-index) |
| 400 |
((getter-of array) |
| 401 |
((mapper-of array) (cons index more-index)))) |
| 402 |
|
| 403 |
(define-method array-ref ((array <array-base>) (index <vector>)) |
| 404 |
((getter-of array) ((mapper-of array) index))) |
| 405 |
|
| 406 |
(define-method array-ref ((array <array-base>) (index <array>)) |
| 407 |
(unless (= (array-rank index) 1) |
| 408 |
(error "index array must be rank 1" index)) |
| 409 |
;; This is slow, but we don't know if the index array using |
| 410 |
;; standard mapping. |
| 411 |
((getter-of array) |
| 412 |
((mapper-of array) |
| 413 |
(map (pa$ array-ref index) |
| 414 |
(iota (- (array-end index 0) (array-start index 0)) |
| 415 |
(array-start index 0)))))) |
| 416 |
|
| 417 |
(define-method array-ref ((array <array-base>)) |
| 418 |
;; special case - zero dimensional array |
| 419 |
((getter-of array) ((mapper-of array) '()))) |
| 420 |
|
| 421 |
(define-method array-set! ((array <array-base>) (index <integer>) . more-index) |
| 422 |
(receive (indices value) |
| 423 |
(split-at! (cons index more-index) (length more-index)) |
| 424 |
((setter-of array) ((mapper-of array) indices) (car value)))) |
| 425 |
|
| 426 |
(define-method array-set! ((array <array-base>) (index <vector>) value) |
| 427 |
((setter-of array) ((mapper-of array) index) value)) |
| 428 |
|
| 429 |
(define-method array-set! ((array <array-base>) (index <array>) value) |
| 430 |
(unless (= (array-rank index) 1) |
| 431 |
(error "index array must be rank 1" index)) |
| 432 |
;; This is slow, but we don't know if the index array using |
| 433 |
;; standard mapping. |
| 434 |
((setter-of array) |
| 435 |
((mapper-of array) |
| 436 |
(map (pa$ array-ref index) |
| 437 |
(iota (- (array-end index 0) (array-start index 0)) |
| 438 |
(array-start index 0)))) |
| 439 |
value)) |
| 440 |
|
| 441 |
(define-method array-set! ((array <array-base>) value) |
| 442 |
;; special case - zero dimensional array |
| 443 |
((setter-of array) ((mapper-of array) '()) value)) |
| 444 |
|
| 445 |
;;--------------------------------------------------------------- |
| 446 |
;; Share array |
| 447 |
;; |
| 448 |
|
| 449 |
;; Given proc, calculates coefficients of affine mapper |
| 450 |
;; Rank is the rank of the new array returned by share-array, which is |
| 451 |
;; the same as the arity of proc. |
| 452 |
|
| 453 |
(define (affine-proc->coeffs proc rank) |
| 454 |
(receive Cs (apply proc (make-list rank 0)) |
| 455 |
(let ([cvecs (map (^_ (make-s32vector rank)) Cs)] |
| 456 |
[cnt (iota rank)]) |
| 457 |
(dotimes [i rank] |
| 458 |
(receive Ks (apply proc (map (^j (if (= j i) 1 0)) cnt)) |
| 459 |
(for-each (^[v k c] (set! (ref v i) (- k c))) cvecs Ks Cs))) |
| 460 |
(values Cs cvecs)))) |
| 461 |
|
| 462 |
;; given calculated coefficients vectors and constants for affine mapper, |
| 463 |
;; and the original mapping function, creates the new afiine mapper. |
| 464 |
(define (generate-shared-map omap constants coeffs) |
| 465 |
(^[Vi] (omap (map (^[ci cvec] (+ ci (s32vector-dot cvec Vi))) |
| 466 |
constants coeffs)))) |
| 467 |
|
| 468 |
(define (share-array array shape proc) |
| 469 |
(receive (Vb Ve) (shape->start/end-vector shape) |
| 470 |
(receive (constants coeffs) (affine-proc->coeffs proc (size-of Vb)) |
| 471 |
(make (class-of array) |
| 472 |
:start-vector Vb |
| 473 |
:end-vector Ve |
| 474 |
:mapper (generate-shared-map (mapper-of array) constants coeffs) |
| 475 |
:backing-storage (backing-storage-of array))))) |
| 476 |
|
| 477 |
;;--------------------------------------------------------------- |
| 478 |
;; Array utilities |
| 479 |
;; |
| 480 |
|
| 481 |
(define (array-shape ar) |
| 482 |
(let1 r (array-rank ar) |
| 483 |
(apply array (shape 0 r 0 2) |
| 484 |
(append-map! (^k (list (array-start ar k) (array-end ar k))) |
| 485 |
(iota r))))) |
| 486 |
|
| 487 |
(define (array-length ar dim) |
| 488 |
(- (array-end ar dim) (array-start ar dim))) |
| 489 |
|
| 490 |
(define (array-size ar) |
| 491 |
(reduce * 1 (map (cute array-length ar <>) (iota (array-rank ar))))) |
| 492 |
|
| 493 |
(define (array-equal? a b :optional (eq equal?)) |
| 494 |
(let1 r (array-rank a) |
| 495 |
(and (= r (array-rank b)) |
| 496 |
(every (^[dim] (and (= (array-start a dim) (array-start b dim)) |
| 497 |
(= (array-end a dim) (array-end b dim)))) |
| 498 |
(iota r)) |
| 499 |
(let/cc break |
| 500 |
(array-for-each-index a |
| 501 |
(^[index] (unless (eq (array-ref a index) |
| 502 |
(array-ref b index)) |
| 503 |
(break #f))) |
| 504 |
(make-vector r)) |
| 505 |
#t)))) |
| 506 |
|
| 507 |
(define-method object-equal? ((a <array-base>) (b <array-base>)) |
| 508 |
(array-equal? a b equal?)) |
| 509 |
|
| 510 |
;; returns a proc that applies proc to indices that is given by a vector. |
| 511 |
|
| 512 |
(define (array-index-applier rank) |
| 513 |
(case rank |
| 514 |
[(0) (^[proc vec] (proc))] |
| 515 |
[(1) (^[proc vec] (proc (vector-ref vec 0)))] |
| 516 |
[(2) (^[proc vec] (proc (vector-ref vec 0) (vector-ref vec 1)))] |
| 517 |
[(3) (^[proc vec] |
| 518 |
(proc (vector-ref vec 0) (vector-ref vec 1) (vector-ref vec 2)))] |
| 519 |
[else (^[proc vec] (apply proc (vector->list vec)))])) |
| 520 |
|
| 521 |
(define (array-for-each proc ar) |
| 522 |
(for-each proc (backing-storage-of ar))) |
| 523 |
|
| 524 |
(define (array-any pred ar) |
| 525 |
(let/cc found |
| 526 |
(for-each (^x (if (pred x) (found #t))) (backing-storage-of ar)) |
| 527 |
#f)) |
| 528 |
|
| 529 |
(define (array-every pred ar) |
| 530 |
(let/cc found |
| 531 |
(for-each (^x (if (not (pred x)) (found #f))) (backing-storage-of ar)) |
| 532 |
#t)) |
| 533 |
|
| 534 |
;; repeat construct |
| 535 |
|
| 536 |
(define (array-for-each-int proc keep Vb Ve ind) |
| 537 |
|
| 538 |
(define i (if (pair? ind) (car ind) (make-vector (s32vector-length Vb)))) |
| 539 |
(define applier #f) |
| 540 |
|
| 541 |
(define (list-loop dim k) |
| 542 |
(if (= dim (car k)) |
| 543 |
(let ([e (s32vector-ref Ve dim)] |
| 544 |
[rest (cdr k)]) |
| 545 |
(if (null? rest) |
| 546 |
(do ([k (s32vector-ref Vb dim) (+ k 1)]) |
| 547 |
[(= k e)] |
| 548 |
(vector-set! i dim k) |
| 549 |
;; use an applier |
| 550 |
(applier proc i)) |
| 551 |
(do ([k (s32vector-ref Vb dim) (+ k 1)]) |
| 552 |
[(= k e)] |
| 553 |
(vector-set! i dim k) |
| 554 |
(list-loop (+ dim 1) rest)))) |
| 555 |
(list-loop (+ dim 1) k))) |
| 556 |
|
| 557 |
(define (helper-loop setter dimensions keep-ls) |
| 558 |
(let loop ([dim dimensions] |
| 559 |
[k keep-ls]) |
| 560 |
(if (= dim (car k)) |
| 561 |
;; we loop over this dimension |
| 562 |
(let ([e (s32vector-ref Ve dim)] |
| 563 |
[rest (cdr k)]) |
| 564 |
(if (null? rest) |
| 565 |
;; inline last loop to avoid excess procedure calls |
| 566 |
(do ([k (s32vector-ref Vb dim) (+ k 1)]) |
| 567 |
[(= k e)] |
| 568 |
(setter i dim k) |
| 569 |
(proc i)) |
| 570 |
;; set the index for this dimension and loop |
| 571 |
(do ([k (s32vector-ref Vb dim) (+ k 1)]) |
| 572 |
[(= k e)] |
| 573 |
(setter i dim k) |
| 574 |
(loop (+ dim 1) rest)))) |
| 575 |
;; skip this dimension |
| 576 |
(loop (+ dim 1) k)))) |
| 577 |
|
| 578 |
(unless (null? keep) |
| 579 |
(cond |
| 580 |
[(null? ind) |
| 581 |
(set! applier (array-index-applier (s32vector-length Vb))) |
| 582 |
(list-loop 0 keep)] |
| 583 |
[(vector? i) (helper-loop vector-set! 0 keep)] |
| 584 |
[(array? i) (helper-loop array-set! 0 keep)] |
| 585 |
[(s8vector? i) (helper-loop s8vector-set! 0 keep)] |
| 586 |
[(s16vector? i) (helper-loop s16vector-set! 0 keep)] |
| 587 |
[(s32vector? i) (helper-loop s32vector-set! 0 keep)] |
| 588 |
[else "bad index object (vector or array required)" (car ind)]))) |
| 589 |
|
| 590 |
(define (array-for-each-index ar proc . o) |
| 591 |
(array-for-each-int |
| 592 |
proc |
| 593 |
(iota (array-rank ar)) |
| 594 |
(start-vector-of ar) |
| 595 |
(end-vector-of ar) |
| 596 |
o)) |
| 597 |
|
| 598 |
(define (array-for-each-index-by-dimension ar keep proc . o) |
| 599 |
(array-for-each-int |
| 600 |
proc |
| 601 |
keep |
| 602 |
(start-vector-of ar) |
| 603 |
(end-vector-of ar) |
| 604 |
o)) |
| 605 |
|
| 606 |
(define (shape-for-each sh proc . o) |
| 607 |
(let* ([rank (array-end sh 0)] |
| 608 |
[ser (iota rank)]) |
| 609 |
(array-for-each-int |
| 610 |
proc |
| 611 |
ser |
| 612 |
(map-to <s32vector> (cut array-ref sh <> 0) ser) |
| 613 |
(map-to <s32vector> (cut array-ref sh <> 1) ser) |
| 614 |
o))) |
| 615 |
|
| 616 |
(define (tabulate-array sh . args) |
| 617 |
(rlet1 res (make-array sh) |
| 618 |
(apply array-retabulate! res args))) |
| 619 |
|
| 620 |
;; Mapping onto array. |
| 621 |
;; array-retabulate! |
| 622 |
;; array-map! |
| 623 |
;; array-map |
| 624 |
;; These may take optional shape argument. It is redundant, for the shape |
| 625 |
;; has to match the target array's shape anyway. In Jussi's reference |
| 626 |
;; implementation, giving the shape allows some optimization. In my |
| 627 |
;; implementation it's not much of use. I keep it just for compatibility |
| 628 |
;; to Jussi's. |
| 629 |
|
| 630 |
(define-method array-retabulate! ((ar <array-base>) (sh <array-base>) (proc <procedure>) . o) |
| 631 |
;; need to check the shape sh matches the ar's shape. |
| 632 |
(apply array-retabulate! ar proc o)) |
| 633 |
|
| 634 |
(define-method array-retabulate! ((ar <array-base>) (proc <procedure>) . o) |
| 635 |
(let ([set (backing-storage-setter-of (class-of ar))] |
| 636 |
[store (backing-storage-of ar)] |
| 637 |
[mapper (mapper-of ar)]) |
| 638 |
(cond [(null? o) |
| 639 |
(let1 applier (array-index-applier (array-rank ar)) |
| 640 |
(array-for-each-index ar |
| 641 |
(^[ind] (set store (mapper ind) (applier proc ind))) |
| 642 |
(make-vector (array-rank ar))))] |
| 643 |
[(or (vector? (car o)) (array? (car o))) |
| 644 |
(array-for-each-index ar |
| 645 |
(^[ind] (set store (mapper ind) (proc ind))) |
| 646 |
(car o))] |
| 647 |
[else "bad index object (vector or array required)" (car o)]))) |
| 648 |
|
| 649 |
(define-method array-map! ((ar <array-base>) (sh <array-base>) (proc <procedure>) ar0 . more-arrays) |
| 650 |
;; need to check the shape sh matches the ar's shape. |
| 651 |
(apply array-map! ar proc ar0 more-arrays)) |
| 652 |
|
| 653 |
(define-method array-map! ((ar <array-base>) (proc <procedure>) ar0) |
| 654 |
(let ([set (backing-storage-setter-of (class-of ar))] |
| 655 |
[store (backing-storage-of ar)] |
| 656 |
[mapper (mapper-of ar)] |
| 657 |
[get0 (backing-storage-getter-of (class-of ar0))] |
| 658 |
[store0 (backing-storage-of ar0)] |
| 659 |
[mapper0 (mapper-of ar0)]) |
| 660 |
(array-for-each-index ar0 |
| 661 |
(^[ind] (set store (mapper ind) (proc (get0 store0 (mapper0 ind))))) |
| 662 |
(make-vector (array-rank ar))))) |
| 663 |
|
| 664 |
(define-method array-map! ((ar <array-base>) (proc <procedure>) ar0 ar1) |
| 665 |
(let ([set (backing-storage-setter-of (class-of ar))] |
| 666 |
[store (backing-storage-of ar)] |
| 667 |
[mapper (mapper-of ar)] |
| 668 |
[get0 (backing-storage-getter-of (class-of ar0))] |
| 669 |
[store0 (backing-storage-of ar0)] |
| 670 |
[mapper0 (mapper-of ar0)] |
| 671 |
[get1 (backing-storage-getter-of (class-of ar1))] |
| 672 |
[store1 (backing-storage-of ar1)] |
| 673 |
[mapper1 (mapper-of ar1)]) |
| 674 |
(array-for-each-index ar0 |
| 675 |
(^[ind] (set store (mapper ind) (proc (get0 store0 (mapper0 ind)) |
| 676 |
(get1 store1 (mapper1 ind))))) |
| 677 |
(make-vector (array-rank ar))))) |
| 678 |
|
| 679 |
(define-method array-map! ((ar <array-base>) (proc <procedure>) ar0 ar1 ar2 . more) |
| 680 |
(let1 arlist (list* ar0 ar1 ar2 more) |
| 681 |
(array-for-each-index ar |
| 682 |
(^[ind] (array-set! ar ind |
| 683 |
(apply proc |
| 684 |
(map (cut array-ref <> ind) arlist)))) |
| 685 |
(make-vector (array-rank ar))))) |
| 686 |
|
| 687 |
(define-method array-map ((sh <array-base>) (proc <procedure>) ar0 . more) |
| 688 |
(apply array-map proc ar0 more)) |
| 689 |
|
| 690 |
(define-method array-map ((proc <procedure>) ar0 . more) |
| 691 |
(rlet1 target (make-array (array-shape ar0)) |
| 692 |
(apply array-map! target proc ar0 more))) |
| 693 |
|
| 694 |
(define (array->vector ar) |
| 695 |
(with-builder (<vector> add! get :size (array-size ar)) |
| 696 |
(array-for-each-index ar |
| 697 |
(^[ind] (add! (array-ref ar ind))) |
| 698 |
(make-vector (array-rank ar))) |
| 699 |
(get))) |
| 700 |
|
| 701 |
(define (array->list ar) |
| 702 |
(with-builder (<list> add! get) |
| 703 |
(array-for-each-index ar |
| 704 |
(^[ind] (add! (array-ref ar ind))) |
| 705 |
(make-vector (array-rank ar))) |
| 706 |
(get))) |