Debian Codegraph @

%3a25/arlib.scm

raw symbols SCIP
1 ;;; array arlib
2
3 ;;; 2001 Jussi Piitulainen
4
5 ;;; This is a high level implementation of some generally useful
6 ;;; array procedures. In addition to R5RS and SRFI-25, only one
7 ;;; tool is used, namely array:apply-to-vector and friends. Thus
8 ;;; this library serves to prove that the primitives really are
9 ;;; primitives. - A lower level implementation would access some
10 ;;; implementation details to bypass redundant checking and such.
11
12 ;;; Note that these procedures are not necessarily designed with
13 ;;; full care. Think of them as examples of what can be done.
14 ;;; Important tools are also missing, including scans and reduces
15 ;;; and many thinks that I have not even heard of yet.
16
17 ;;; (array-shape arr) (array-length arr dim) (array-size arr)
18 ;;; (array-equal? arr1 arr2)
19 ;;; (shape-for-each shp proc [ind])
20 ;;; (array-for-each-index arr proc [ind])
21 ;;; (tabulate-array shp proc) (tabulate-array! shp proc ind)
22 ;;; (array-retabulate! arr shp proc [ind])
23 ;;; (array-map [shp] proc arr0 arr1 ...)
24 ;;; (array-map! arr [shp] proc arr0 arr1 ...)
25 ;;; (array->vector arr) (array->list arr)
26 ;;; (share-array/prefix arr k ...) (share-row arr k) (share-column arr k)
27 ;;; (share-array/origin arr k ...) (share-array/origin arr ind)
28 ;;; (array-append dim arr0 arr1 ...)
29 ;;; (transpose arr dim ...)
30 ;;; (share-nths arr dim n)
31
32 ;;; Naming problem: should all those index-object using procedures
33 ;;; bang? The main argument, like shape, is not mutated.
34
35 ;;; (array-shape arr)
36
37 (define (array-shape arr)
38 (let ((r (array-rank arr)))
39 (let ((m (make-array (shape 0 r 0 2))))
40 (do ((d 0 (+ d 1)))
41 ((= d r)
42 m)
43 (array-set! m d 0 (array-start arr d))
44 (array-set! m d 1 (array-end arr d))))))
45
46 ;;; (array-length arr dim)
47
48 (define (array-length arr dim)
49 (- (array-end arr dim)
50 (array-start arr dim)))
51
52 ;;; (array-size arr)
53
54 (define (array-size arr)
55 (let ((r (array-rank arr)))
56 (do ((k 0 (+ k 1))
57 (p 1 (* p (array-length arr k))))
58 ((= k r) p))))
59
60 ;;; (array-equal? a b)
61 ;;; compares elements with equal? so elements better not contain
62 ;;; arrays.
63
64 (define (array-equal? a b)
65 (let ((r (array-rank a)))
66 (and (= r (array-rank b))
67 (and (do ((k 0 (+ k 1))
68 (true #t (and true
69 (= (array-start a k)
70 (array-start b k))
71 (= (array-end a k)
72 (array-end b k)))))
73 ((= k r) true))
74 (let ((ks (make-vector r 0)))
75 (let wok ((d 0))
76 (if (< d r)
77 (let ((e (array-end a d)))
78 (do ((k (array-start a d) (+ k 1))
79 (true #t (and true (wok (+ d 1)))))
80 ((= k e) true)
81 (vector-set! ks d k)))
82 (equal? (array-ref a ks)
83 (array-ref b ks)))))))))
84
85 ;;; (shape-for-each shp proc [index-object])
86 ;;; passes each index in shape to proc in row-major orderd, using
87 ;;; index-object if provided.
88
89 (define (shape-for-each shp proc . o)
90 (if (null? o)
91 (array:arlib:shape-for-each/arguments shp proc)
92 (if (vector? (car o))
93 (array:arlib:shape-for-each/vector shp proc (car o))
94 (array:arlib:shape-for-each/array shp proc (car o)))))
95
96 (define (array:arlib:shape-for-each/arguments shp proc)
97 (let ((r (array-end shp 0)))
98 (let ((vec (make-vector r)))
99 (let do-dim ((d 0))
100 (if (= d r)
101 (array:apply-to-vector r proc vec)
102 (let ((e (array-ref shp d 1)))
103 (do ((k (array-ref shp d 0) (+ k 1)))
104 ((= k e))
105 (vector-set! vec d k)
106 (do-dim (+ d 1)))))))))
107
108 (define (array:arlib:shape-for-each/vector shp proc vec)
109 (let ((r (array-end shp 0)))
110 (let do-dim ((d 0))
111 (if (= d r)
112 (proc vec)
113 (let ((e (array-ref shp d 1)))
114 (do ((k (array-ref shp d 0) (+ k 1)))
115 ((= k e))
116 (vector-set! vec d k)
117 (do-dim (+ d 1))))))))
118
119 (define (array:arlib:shape-for-each/array shp proc arr)
120 ;; arr is not vector
121 (let ((r (array-end shp 0)))
122 (let do-dim ((d 0))
123 (if (= d r)
124 (proc arr)
125 (let ((e (array-ref shp d 1)))
126 (do ((k (array-ref shp d 0) (+ k 1)))
127 ((= k e))
128 (array-set! arr d k)
129 (do-dim (+ d 1))))))))
130
131 ;;; (array-for-each-index arr proc [ind])
132 ;;; is equivalent to
133 ;;;
134 ;;; (shape-for-each-index (array-shape arr) proc [ind])
135 ;;;
136 ;;; but is implemented without allocation of the shape, to prove
137 ;;; that it can be so implemented.
138
139 (define (array-for-each-index arr proc . o)
140 (if (null? o)
141 (array:arlib:array-for-each-index/arguments arr proc)
142 (if (vector? (car o))
143 (array:arlib:array-for-each-index/vector arr proc (car o))
144 (array:arlib:array-for-each-index/array arr proc (car o)))))
145
146 (define (array:arlib:array-for-each-index/arguments arr proc)
147 (let ((r (array-rank arr)))
148 (let ((vec (make-vector r))
149 (apply (array:applier-to-vector r)))
150 (let do-dim ((d 0))
151 (if (= d r)
152 (apply proc vec)
153 (let ((e (array-end arr d)))
154 (do ((k (array-start arr d) (+ k 1)))
155 ((= k e))
156 (vector-set! vec d k)
157 (do-dim (+ d 1)))))))))
158
159 (define (array:arlib:array-for-each-index/vector arr proc ind)
160 ;; ind is a vector
161 (let ((r (array-rank arr)))
162 (let do-dim ((d 0))
163 (if (= d r)
164 (proc ind)
165 (let ((e (array-end arr d)))
166 (do ((k (array-start arr d) (+ k 1)))
167 ((= k e))
168 (vector-set! ind d k)
169 (do-dim (+ d 1))))))))
170
171 (define (array:arlib:array-for-each-index/array arr proc ind)
172 ;; ind is an array but not a vector
173 (let ((r (array-rank arr)))
174 (let do-dim ((d 0))
175 (if (= d r)
176 (proc ind)
177 (let ((e (array-end arr d)))
178 (do ((k (array-start arr d) (+ k 1)))
179 ((= k e))
180 (array-set! ind d k)
181 (do-dim (+ d 1))))))))
182
183 ;;; (tabulate-array shp proc)
184 ;;; (tabulate-array! shp proc ind)
185 ;;; returns a newly allocated array of the given shape with initial
186 ;;; contents at each index whatever proc returns given the indices.
187 ;;; The latter procedure reuses ind for package of indices.
188
189 (define (tabulate-array shp proc)
190 (let ((arr (make-array shp)))
191 (array:arlib:shape-for-each/vector
192 shp
193 (let ((apply (array:applier-to-vector (array-end shp 0))))
194 (lambda (ix) (array-set! arr ix (apply proc ix))))
195 (make-vector (array-end shp 0)))
196 arr))
197
198 (define (tabulate-array! shp proc ind)
199 (let ((arr (make-array shp)))
200 (if (vector? ind)
201 (array:arlib:shape-for-each/vector
202 shp
203 (lambda (ix) (array-set! arr ix (proc ix)))
204 ind)
205 (array:arlib:shape-for-each/array
206 shp
207 (lambda (ix) (array-set! arr ix (proc ix)))
208 ind))
209 arr))
210
211 ;;; (array-retabulate! arr shp proc [index-object])
212 ;;; sets the elements of arr in shape to the value of proc at that
213 ;;; index, using index-object if provided.
214
215 (define (array-retabulate! arr shp proc . o)
216 (if (null? o)
217 (array:arlib:shape-for-each/vector
218 shp
219 (let ((apply (array:applier-to-vector (array-end shp 0))))
220 (lambda (ix)
221 (array-set! arr ix (apply proc ix))))
222 (make-vector (array-end shp 0)))
223 (if (vector? (car o))
224 (array:arlib:shape-for-each/vector
225 shp
226 (lambda (ix)
227 (array-set! arr ix (proc ix)))
228 (car o))
229 (array:arlib:shape-for-each/array
230 shp
231 (lambda (ix)
232 (array-set! arr ix (proc ix)))
233 (car o)))))
234
235 ;;; (array-map! array [shape] proc array0 array1...)
236 ;;; stores to the elements of array (in shape) the values of proc at
237 ;;; the contents of arrayk at corresponding index.
238
239 (define (array-map! arr x y . o)
240 (if (array:array? x)
241 (array:arlib:map! arr x y (apply vector o))
242 (array:arlib:map! arr (array-shape arr) x (apply vector y o))))
243
244 (define (array:arlib:map! arr shp proc args)
245 (let ((rank (vector-length args)))
246 (let ((argv (make-vector rank)))
247 (array:arlib:shape-for-each/vector
248 shp
249 (let ((apply (array:applier-to-vector rank)))
250 (lambda (ix)
251 (do ((k 0 (+ k 1)))
252 ((= k rank))
253 (vector-set! argv k (array-ref (vector-ref args k) ix)))
254 (array-set! arr ix (apply proc argv))))
255 (make-vector (array-end shp 0))))))
256
257 ;;; (array-map [shape] proc array0 array1 ...)
258 ;;; creates a new array with elements initialized to the values of
259 ;;; proc at contents of arrayk (in shape).
260
261 (define (array-map x y . o)
262 (if (array:array? x)
263 (let ((arr (make-array x)))
264 (array:arlib:map! arr x y (apply vector o))
265 arr)
266 (let ((shp (array-shape y)))
267 (let ((arr (make-array shp)))
268 (array:arlib:map! arr shp x (apply vector y o))
269 arr))))
270
271 ;;; SRFI-25 mailing list requested array->vector; they also requested the
272 ;;; ability to use an array as an index of an element, and array->list is
273 ;;; an attempt to provide for that.
274
275 (define (array->vector arr)
276 (let ((vec (make-vector (array-size arr))))
277 (let ((k 0))
278 (shape-for-each
279 (array-shape arr)
280 (lambda (index)
281 (vector-set! vec k (array-ref arr index))
282 (set! k (+ k 1)))
283 (make-vector (array-rank arr)))
284 vec)))
285
286 ;;; It needs to be said that more efficient implementations are
287 ;;; possible, even within SRFI-25.
288
289 (define (array->list arr)
290 (vector->list (array->vector arr)))
291
292 ;;; (share-row arr k)
293 ;;; shares whatever the first index is about.
294 ;;; The result has one dimension less.
295
296 (define (share-row arr k)
297 (share-array
298 arr
299 (let ((bounds (array->list (array-shape arr))))
300 (apply shape (cddr bounds)))
301 (lambda ks
302 (apply values k ks))))
303
304 ;;; (share-array/prefix arr k ...)
305
306 (define (share-array/prefix arr . js)
307 (if (or (null? js)
308 (integer? (car js)))
309 (share-array
310 arr
311 (let ((bounds (array->list (array-shape arr))))
312 (apply shape (list-tail bounds (* 2 (length js)))))
313 (lambda ks
314 (apply values (append js ks))))
315 (apply (lambda (fix)
316 (share-array/prefix!
317 arr
318 fix
319 (make-vector (- (array-rank arr)
320 (if (vector? fix)
321 (vector-length fix)
322 (array-end fix 0))))))
323 js)))
324
325 (define (share-array/prefix! arr fix in . out)
326 (let* ((out (if (pair? out)
327 ((lambda (out) out) out)
328 (make-vector (array-rank arr))))
329 (fix-ref (if (vector? fix) vector-ref array-ref))
330 (in-ref (if (vector? in) vector-ref array-ref))
331 (out-set! (if (vector? out) vector-set! array-set!))
332 (m (if (vector? fix)
333 (vector-length fix)
334 (array-end fix 0)))
335 (n (if (vector? out)
336 (vector-length out)
337 (array-end out 0))))
338 (do ((k 0 (+ k 1)))
339 ((= k m))
340 (out-set! out k (fix-ref fix k)))
341 (share-array/index!
342 arr
343 (let ((bounds (array->list (array-shape arr))))
344 (apply shape (list-tail bounds (if (vector? fix)
345 (* 2 (vector-length fix))
346 (* 2 (array-end fix 0))))))
347 (lambda (in)
348 (do ((k m (+ k 1)))
349 ((= k n))
350 (out-set! out k (in-ref in (- k m))))
351 out)
352 in)))
353
354 ;;; (share-column arr k)
355 ;;; shares whatever the second index is about.
356 ;;; The result has one dimension less.
357
358 (define (share-column arr k)
359 (share-array
360 arr
361 (let ((bounds (array->list (array-shape arr))))
362 (apply shape
363 (car bounds) (cadr bounds)
364 (cddddr bounds)))
365 (lambda ks
366 (apply values (car ks) k (cdr ks)))))
367
368 ;;; (share-array/origin arr k ...)
369 ;;; (share-array/origin arr index)
370 ;;; change origin to k ..., with index a vector or zero-based
371 ;;; one-dimensional array that contains k ...
372 ;;;
373 ;;; This is useful for writing array-append. Maybe for something
374 ;;; else too - who knows.
375
376 (define (share-array/origin arr . xs)
377 (let ((new (if (or (null? xs)
378 (integer? (car xs)))
379 xs
380 (apply (lambda (x)
381 (if (vector? x)
382 (vector->list x)
383 (if (array? x)
384 (array->list x)
385 (error "share-array/origin: bad thing"))))
386 xs))))
387 (do ((k (array-rank arr) (- k 1))
388 (old '() (cons (array-start arr (- k 1)) old)))
389 ((= k 0)
390 (let ((ds (map - new old)))
391 (share-array
392 arr
393 (tabulate-array
394 (shape 0 (array-rank arr) 0 2)
395 (lambda (r k)
396 (case k
397 ((0) (+ (array-start arr r) (list-ref ds r)))
398 ((1) (+ (array-end arr r) (list-ref ds r))))))
399 (lambda ks
400 (apply values (map - ks ds)))))))))
401
402 ;;; SRFI-25 mailing list requested making shapes their own type. Here's
403 ;;; an example of how manipulating shapes as arrays can be useful. The
404 ;;; example also tests that higher level libraries are indeed easy to
405 ;;; write on top of this SRFI.
406
407 ;;; (array-append arr1 arr2 dim)
408 ;;; appends two arrays along a specified dimension. The arrays must
409 ;;; have equally many dimensions and all other dimensions equally long.
410 ;;;
411 ;;; Generalize to more arrays and maybe rewrite with shape-for-each or
412 ;;; what have you.
413
414 (define (array-append dim arr . ars)
415 (let* ((total (do ((m (array-length arr dim)
416 (+ m (array-length (car r) dim)))
417 (r ars (cdr r)))
418 ((null? r) m)))
419 (common (array-shape arr))
420 (origin (array->vector (share-column common 0)))
421 (index (make-vector (array-rank arr))))
422 (array-set! common dim 1 (+ (array-start arr dim) total))
423 (let ((result (make-array common)))
424 (array-set! common dim 1 (array-start arr dim))
425 (let wok ((arr arr)
426 (ars ars))
427 (vector-set! origin dim (array-ref common dim 1))
428 (let ((arr1 (share-array/origin arr origin)))
429 (array-set! common dim 0 (array-start arr1 dim))
430 (array-set! common dim 1 (array-end arr1 dim))
431 (shape-for-each
432 common
433 (lambda (index)
434 (array-set! result index (array-ref arr1 index)))
435 index))
436 (if (pair? ars)
437 (wok (car ars) (cdr ars))))
438 result)))
439
440 ;;; Transpose, as permutation of dimensions, is applicable to all
441 ;;; arrays. The default is reversal.
442
443 ;;; The implementation uses multiplication by permutation
444 ;;; matrix but matrix multiplication is not exported.
445
446 (define (array:arlib:matrix-times a b)
447 (or (and (= (array-rank a) 2)
448 (= (array-rank b) 2))
449 (error "times: arrays are not matrices"))
450 (let ((r0 (array-start a 0)) (rn (array-end a 0))
451 (t0 (array-start a 1)) (tn (array-end a 1))
452 (u0 (array-start b 0)) (un (array-end b 0))
453 (k0 (array-start b 1)) (kn (array-end b 1)))
454 (or (= (- tn t0) (- un u0))
455 (error "times: matrices are not compatible"))
456 (let ((ab (make-array (shape r0 rn k0 kn))))
457 (do ((r r0 (+ r 1)))
458 ((= r rn))
459 (do ((k k0 (+ k 1)))
460 ((= k kn))
461 (do ((t t0 (+ t 1))
462 (u u0 (+ u 1))
463 (s 0 (+ s (* (array-ref a r t)
464 (array-ref b u k)))))
465 ((and (= t tn)
466 (= u un))
467 (array-set! ab r k s)))))
468 ab)))
469
470 ; This is a generalized transpose. It can permute the dimensions any which
471 ; way. The permutation is provided by a permutation matrix: a square matrix
472 ; of zeros and ones, with exactly one one in each row and column, or a
473 ; permutation of the rows of an identity matrix; the size of the matrix
474 ; must match the number of dimensions of the array.
475 ;
476 ; The default permutation is [ 0 1 | 1 0 ] of course, but any permutation
477 ; array can be specified, and the shape array of the original array is then
478 ; multiplied with it, and index column vectors of the new array with its
479 ; inverse, from left, to permute the rows appropriately.
480
481 (define (array:arlib:permutation-matrix . ds)
482 (let* ((n (length ds))
483 (arr (make-array (shape 0 n 0 n) 0)))
484 (do ((k 0 (+ k 1))
485 (ds ds (cdr ds)))
486 ((= k n))
487 (array-set! arr k (car ds) 1))
488 arr))
489
490 ;;; (transpose arr k ...)
491 ;;; shares arr with permuted dimensions. Each dimension from 0
492 ;;; inclusive to rank exclusive must appear once in k ...
493
494 (define (transpose a . p0)
495 (let* ((r (array-rank a))
496 (permutation (apply array:arlib:permutation-matrix
497 (if (pair? p0)
498 p0
499 (do ((ds '() (cons d ds))
500 (d 0 (+ d 1)))
501 ((= d r)
502 ;; reverse dimensions
503 ds)))))
504 (inverse-permutation (share-array permutation
505 (array-shape permutation)
506 (lambda (r k)
507 ;; transpose
508 (values k r)))))
509 (share-array
510 a
511 (array:arlib:matrix-times permutation (array-shape a))
512 (lambda ks0
513 (apply values
514 (array->list
515 (array:arlib:matrix-times
516 inverse-permutation
517 (apply array (shape 0 r 0 1) ks0))))))))
518
519 ;;; (share-array/index! array subshape proc index)
520
521 (define (share-array/index! array subshape proc index)
522 (array:share/index! array subshape proc index))
523
524 ;;; Take every nth slice along dimension d into a shared array. This
525 ;;; preserves the origin.
526
527 (define (share-nths arr d n)
528 (let* ((bounds (array->vector (array-shape arr)))
529 (b (vector-ref bounds (* 2 d)))
530 (e (vector-ref bounds (+ (* 2 d) 1))))
531 (vector-set! bounds (+ (* 2 d) 1) (+ b (quotient (+ n (- e b 1)) n)))
532 (share-array
533 arr
534 (apply shape (vector->list bounds))
535 (lambda ks
536 (apply values
537 (let d/nk ((u 0) (ks ks))
538 (if (= u d)
539 (cons (+ b (* n (- (car ks) b))) (cdr ks))
540 (cons (car ks) (d/nk (+ u 1) (cdr ks))))))))))