forked from scheme-requests-for-implementation/srfi-122
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srfi-179.scm
2173 lines (1984 loc) · 120 KB
/
srfi-179.scm
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(include "html-lib.scm")
(define (format-lambda-list lst)
(let ((name (car lst))
(arguments (cdr lst)))
(<p> (<b> "Procedure: ")
(<code> (<a> name: name name)
(map (lambda (arg)
(list " " (cond ((symbol? arg)
(<var> arg))
((keyword? arg)
arg)
(else
arg))))
arguments)))))
(define (format-global-variable name)
(<p> (<b> "Variable: ")
(<code> (<a> name: name name))))
(with-output-to-file
"srfi-179.html"
(lambda()
(html-display
(list
(<unprotected> "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">")
(<html>
(<head>
(<meta> charset: "utf-8")
(<meta> name: 'viewport
content: "width=device-width, initial-scale=1")
(<title> "Nonempty Intervals and Generalized Arrays")
(<link> href: "http://srfi.schemers.org/srfi.css"
rel: "stylesheet"
type: "test/css")
(<script> type: "text/x-mathjax-config" "
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}
});")
(<script> crossorigin: "anonymous"
integrity:"sha384-Ra6zh6uYMmH5ydwCqqMoykyf1T/+ZcnOQfFPhDrp2kI4OIxadnhsvvA2vv9A7xYv"
type: "text/javascript"
src: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML")
)
(<body>
(<h1> "Title")
(<p> "Nonempty Intervals and Generalized Arrays")
(<h2> "Author")
(<p> "Bradley J. Lucier")
(<h2> "Status")
(<p> "This SRFI is currently in " (<em> "draft") " status. Here is "
(<a> href: "http://srfi.schemers.org/srfi-process.html" "an explanation")
" of each status that a SRFI can hold. To provide input on this SRFI, please send email to "
(<code> (<a> href: "mailto:srfi minus 179 at srfi dot schemers dot org"
"srfi-179@" (<span> class: "antispam" "nospam") "srfi.schemers.org"))
". To subscribe to the list, follow "
(<a> href: "http://srfi.schemers.org/srfi-list-subscribe.html" "these instructions")
". You can access previous messages via the mailing list "
(<a> href: "http://srfi-email.schemers.org/srfi-179" "archive")". There is a "(<a> href: "https://github.com/scheme-requests-for-implementation/srfi-179" "git repository")" of this document, a reference implementation, a test file, and other materials.")
(<ul> (<li> "Received: 2020/1/11"))
(<h2> "Abstract")
(<p>
"This SRFI specifies an array mechanism for Scheme. Arrays as defined here are quite general; at their most basic, an array is simply a "
"mapping, or function, from multi-indices of exact integers $i_0,\\ldots,i_{d-1}$ to Scheme values. The set of multi-indices "
"$i_0,\\ldots,i_{d-1}$ that are valid for a given array form the "(<i>'domain)" of the array. In this SRFI, each array's domain consists "
" of the cross product of nonempty intervals of exact integers $[l_0,u_0)\\times[l_1,u_1)\\times\\cdots\\times[l_{d-1},u_{d-1})$ of $\\mathbb Z^d$, $d$-tuples of "
"integers. Thus, we introduce a data type "
"called $d$-"(<i> 'intervals)", or more briefly "(<i>'intervals)", that encapsulates this notion. (We borrow this terminology from, e.g., "
" Elias Zakon's "(<a> href: "http://www.trillia.com/zakon1.html" "Basic Concepts of Mathematics")".) "
"Specialized variants of arrays are specified to provide portable programs with efficient representations for common use cases.")
(<h2> "Rationale")
(<p> "This SRFI was motivated by a number of somewhat independent notions, which we outline here and which are explained below.")
(<ul>
(<li> "Provide a "(<b> "general API")" (Application Program Interface) that specifies the minimal required properties of any given array, without requiring any specific implementation strategy from the programmer for that array.")
(<li> "Provide a "(<b> "single, efficient implementation for dense arrays")" (which we call "(<i>"specialized arrays")").")
(<li> "Provide "(<b> "useful array transformations")" by exploiting the algebraic structure of affine one-to-one mappings on multi-indices.")
(<li> "Separate "(<b>"the routines that specify the work to be done")" ("(<code>'array-map)", "(<code>'array-outer-product)", etc.) from "(<b>"the routines that actually do the work")" ("(<code>'array->specialized-array)", "(<code>'array-assign!)", "(<code>'array-fold)", etc.). This approach "(<b> "avoids temporary intermediate arrays")" in computations.")
(<li> "Encourage " (<b> "bulk processing of arrays")" rather than word-by-word operations.")
)
(<p> "This SRFI differs from the finalized " (<a> href: "https://srfi.schemers.org/srfi-122/" "SRFI-122")" in the following ways:")
(<ul>
(<li> "The procedures "(<code>'interval-for-each)", "(<code>'interval-cartesian-product)", "(<code>'array-outer-product)", "(<code>'array-tile)", "(<code>'array-assign!)", and "(<code>'array-swap!)" have been added.")
(<li> "The discussion of Haar transforms as examples of separable transforms has been corrected.")
(<li> "The documentation has a few more examples of image processing algorithms.")
(<li> "Some matrix examples have been added to this document and test-arrays.scm."))
(<h2> "Overview")
(<h3> "Bawden-style arrays")
(<p> "In a "(<a> href: "https://groups.google.com/forum/?hl=en#!msg/comp.lang.scheme/7nkx58Kv6RI/a5hdsduFL2wJ" "1993 post")
" to the news group comp.lang.scheme, Alan Bawden gave a simple implementation of multi-dimensional arrays in R4RS scheme. "
"The only constructor of new arrays required specifying an initial value, and he provided the three low-level primitives "
(<code>'array-ref)", "(<code>'array-set!)", and "(<code>'array?)". His arrays were defined on rectangular intervals in "
"$\\mathbb Z^d$ of the form $[0,u_0)\\times\\cdots\\times [0,u_{d-1})$. I'll note that his function "(<code>'array-set!)
" put the value to be entered into the array at the front of the variable-length list of indices that indicate where to "
"place the new value. He offered an intriguing way to \"share\" arrays "
"in the form of a routine "(<code>'make-shared-array)" that took a mapping from a new interval of indices into the domain "
"of the array to be shared. His implementation incorporated what he called an "(<i>'indexer)", which was a function from "
"the interval $[0,u_0)\\times\\cdots\\times [0,u_{d-1})$ to an interval $[0,N)$, where the "(<i>'body)" of the array consisted of "
"a single Scheme vector of length $N$. Bawden called the mapping specified in "(<code>'make-shared-array)" "
(<i>'linear)", but I prefer the term "(<i>'affine)", as I explain later.")
(<p> "Mathematically, Bawden's arrays can be described as follows. We'll use the vector notation $\\vec i$ for a multi-index "
"$i_0,\\ldots,i_{d-1}$. (Multi-indices correspond to Scheme "(<code>'values)".) Arrays will be denoted by capital letters "
"$A,B,\\ldots$, the domain of the array $A$ (in Bawden's case $[0,u_0)\\times \\cdots\\times [0,u_{d-1})$) will be denoted by $D_A$, "
"and the indexer of $A$, mapping $D_A$ to the interval $[0,N)$ will be denoted by $I_A$. Initially, Bawden constructs "
"$I_A$ such that $I_A(\\vec i)$ steps consecutively through the values $0,1,\\ldots,N-1$ as $\\vec i$ steps through the "
"multi-indices $(0,\\ldots,0,0)$, $(0,\\ldots,0,1)$, $\\ldots$, $(0,\\ldots,1,0)$, etc., in lexicographical order, which means "
"that if $\\vec i$ and $\\vec j$ are two multi-indices, then $\\vec i<\\vec j$ iff the first coordinate $k$ where $\\vec i$ and $\\vec j$ "
"differ satisfies $\\vec i_k<\\vec j_k$. In fact, $I_A(\\vec i)=\\vec v\\cdot\\vec i$ for some specially-constructed vector $\\vec v$ "
"that depends only on $D_A$, the domain of $A$, where $\\vec v\\cdot\\vec i$ is the dot product of $\\vec v$ and $\\vec i$.")
(<p> "In "(<code>'make-shared-array)", Bawden allows you to specify a new $r$-dimensional interval $D_B$ as the domain of a new array $B$, and a "
"mapping $T_{BA}:D_B\\to D_A$ of the form $T_{BA}(\\vec i)=M\\vec i+\\vec b$; here $M$ is a $d\\times r$ matrix of integer values and "
"$\\vec b$ is a $d$-vector. So this mapping $T_{BA}$ is "(<i>'affine)", in that $T_{BA}(\\vec i)-T_{BA}(\\vec j)=M(\\vec i-\\vec j)$ is "
(<i>'linear)" (in a linear algebra sense) in $\\vec i-\\vec j$. The new indexer of $B$ satisfies $I_B(\\vec i)=I_A(T_{BA}(\\vec i))$.")
(<p> "A fact Bawden exploits in the code, but doesn't point out in the short post, is that $I_B$ is again an affine map, and indeed, the composition "
"of "(<i>'any)" two affine maps is again affine.")
(<h3> "Our extensions of Bawden-style arrays")
(<p> "We incorporate Bawden-style arrays into this SRFI, but extend them in two relatively minor ways that we find quite useful.")
(<p> "First, we allow the intervals of multi-indices that form the domains of arrays to have nonzero lower bounds as "
"well as upper bounds, so domains are rectangular, $d$-dimensional intervals $[l_0,u_0)\\times\\cdots\\times[l_{d-1},u_{d-1})$.")
(<p> "Second, we introduce the notion of a "(<i>"storage class")", an object that contains functions that manipulate, store, check, etc., different types of values. "
"A "(<code>'generic-storage-class)" can manipulate any Scheme value, "
"whereas, e.g., a "(<code>'u1-storage-class)" can store only the values 0 and 1 in each element of a body.")
(<p> "We also require that our affine maps be one-to-one, so that if $\\vec i\\neq\\vec j$ then $T(\\vec i)\\neq T(\\vec j)$. Without this property, modifying "
"the $\\vec i$th component of $A$ would cause the $\\vec j$th component to change.")
(<h3> "Common transformations on Bawden-style arrays")
(<p> "Requiring the transformations $T_{BA}:D_B\\to D_A$ to be affine may seem esoteric and restricting, but in fact many common and useful array transformations "
"can be expressed in this way. We give several examples below: ")
(<ul>
(<li> (<b> "Restricting the domain of an array: ")
" If the domain of $B$, $D_B$, is a subset of the domain of $A$, then $T_{BA}(\\vec i)=\\vec i$ is a one-to-one affine mapping. We define "
(<code>'array-extract)" to define this common operation; it's like looking at a rectangular sub-part of a spreadsheet. We use it to extract the common part of overlapping domains of three arrays in an image processing example below. ")
(<li> (<b> "Tiling an array: ")
"For various reasons (parallel processing, optimizing cache localization, GPU programming, etc.) one may wish to process a large array as a number of subarrays of the same dimensions, which we call "(<i>'tiling)" the array. The routine "(<code>'array-tile)" returns a new array, each entry of which is a subarray extracted (in the sense of "(<code>'array-extract)") from the input array.")
(<li> (<b> "Translating the domain of an array: ")
"If $\\vec d$ is a vector of integers, then $T_{BA}(\\vec i)=\\vec i-\\vec d$ is a one-to-one affine map of $D_B=\\{\\vec i+\\vec d\\mid \\vec i\\in D_A\\}$ onto $D_A$. "
"We call $D_B$ the "(<i>'translate)" of $D_A$, and we define "(<code>'array-translate)" to provide this operation.")
(<li> (<b> "Permuting the coordinates of an array: ")
"If $\\pi$ "(<a> href: "https://en.wikipedia.org/wiki/Permutation" 'permutes)" the coordinates of a multi-index $\\vec i$, and $\\pi^{-1}$ is the inverse of $\\pi$, then "
"$T_{BA}(\\vec i)=\\pi (\\vec i)$ is a one-to-one affine map from $D_B=\\{\\pi^{-1}(\\vec i)\\mid \\vec i\\in D_A\\}$ onto $D_A$. We provide "(<code>'array-permute)" for this operation. "
"The only nonidentity permutation of a two-dimensional spreadsheet turns rows into columns and vice versa.")
(<li> (<b> "Currying an array: ")
"Let's denote the cross product of two intervals $\\text{Int}_1$ and $\\text{Int}_2$ by $\\text{Int}_1\\times\\text{Int}_2$; "
"if $\\vec j=(j_0,\\ldots,j_{r-1})\\in \\text{Int}_1$ and $\\vec i=(i_0,\\ldots,i_{s-1})\\in \\text{Int}_2$, then "
"$\\vec j\\times\\vec i$, which we define to be $(j_0,\\ldots,j_{r-1},i_0,\\ldots,i_{s-1})$, is in $\\text{Int}_1\\times\\text{Int}_2$. "
"If $D_A=\\text{Int}_1\\times\\text{Int}_2$ and $\\vec j\\in\\text{Int}_1$, then $T_{BA}(\\vec i)=\\vec j\\times\\vec i$ "
"is a one-to-one affine mapping from $D_B=\\text{Int}_2$ into $D_A$. For each vector $\\vec j$ we can compute a new array in this way; we provide "
(<code>'array-curry)" for this operation, which returns an array whose domain is $\\text{Int}_1$ and whose elements are themselves arrays, each of which is defined on $\\text{Int}_2$. "
"Currying a two-dimensional array would be like organizing a spreadsheet into a one-dimensional array of rows of the spreadsheet.")
(<li> (<b> "Traversing some indices in a multi-index in reverse order: ")
"Consider an array $A$ with domain $D_A=[l_0,u_0)\\times\\cdots\\times[l_{d-1},u_{d-1})$. Fix $D_B=D_A$ and assume we're given a vector of booleans $F$ ($F$ for \"flip?\"). "
"Then define $T_{BA}:D_B\\to D_A$ by $i_j\\to i_j$ if $F_j$ is "(<code>'#f)" and $i_j\\to u_j+l_j-1-i_j$ if $F_j$ is "(<code>'#t)"."
"In other words, we reverse the ordering of the $j$th coordinate of $\\vec i$ if and only if $F_j$ is true. "
"$T_{BA}$ is an affine mapping from $D_B\\to D_A$, which defines a new array $B$, and we can provide "(<code>'array-reverse)" for this operation. "
"Applying "(<code>'array-reverse)" to a two-dimensional spreadsheet might reverse the order of the rows or columns (or both).")
(<li> (<b> "Uniformly sampling an array: ")
"Assume that $A$ is an array with domain $[0,u_1)\\times\\cdots\\times[0,u_{d-1})$ (i.e., an interval all of whose lower bounds are zero). "
"We'll also assume the existence of vector $S$ of scale factors, which are positive exact integers. "
"Let $D_B$ be a new interval with $j$th lower bound equal to zero and $j$th upper bound equal to $\\operatorname{ceiling}(u_j/S_j)$ and let "
"$T_{BA}(\\vec i)_j=i_j\\times S_j$, i.e., the $j$th coordinate is scaled by $S_j$. ($D_B$ contains precisely those multi-indices that $T_{BA}$ maps into $D_A$.) "
" Then $T_{BA}$ is an affine one-to-one mapping, and we provide "(<code>'interval-scale)" and "(<code>'array-sample)" for these operations.")
)
(<p> "We make several remarks. First, all these operations could have been computed by specifying the particular mapping $T_{BA}$ explicitly, so that these routines are simply "
"\"convenience\" procedures. Second, because the composition of any number of affine mappings are again affine, accessing or changing the elements of a "
"restricted, translated, curried, permuted array is no slower than accessing or changing the elements of the original array itself. "
"Finally, we note that by combining array currying and permuting, say, one can come up with simple expressions of powerful algorithms, such as extending "
"one-dimensional transforms to multi-dimensional separable transforms, or quickly generating two-dimensional slices of three-dimensional image data. "
"Examples are given below.")
(<h3> "Generalized arrays")
(<p> "Bawden-style arrays are clearly useful as a programming construct, but they do not fulfill all our needs in this area. "
"An array, as commonly understood, provides a mapping from multi-indices $(i_0,\\ldots,i_{d-1})$ of exact integers
in a nonempty, rectangular, $d$-dimensional interval $[l_0,u_0)\\times[l_1,u_1)\\times\\cdots\\times[l_{d-1},u_{d-1})$ (the "(<i>'domain)" of the array) to Scheme objects.
Thus, two things are necessary to specify an array: an interval and a mapping that has that interval as its domain.")
(<p> "Since these two things are often sufficient for certain algorithms, we introduce in this SRFI a minimal set of interfaces for dealing with such arrays.")
(<p> "Specifically, an array specifies a nonempty, multi-dimensional interval, called its "(<i> "domain")", and a mapping from this domain to Scheme objects. This mapping is called the "(<i> 'getter)" of the array, accessed with the procedure "(<code>'array-getter)"; the domain of the array (more precisely, the domain of the array's getter) is accessed with the procedure "(<code>'array-domain)".")
(<p> "If this mapping can be changed, the array is said to be "(<i> 'mutable)" and the mutation is effected
by the array's "(<i> 'setter)", accessed by the procedure "(<code>'array-setter)". We call an object of this type a mutable array. Note: If an array does not have a setter, then we call it immutable even though the array's getter might not be a \"pure\" function, i.e., the value it returns may not depend solely on the arguments passed to the getter.")
(<p> "In general, we leave the implementation of generalized arrays completely open. They may be defined simply by closures, or
they may have hash tables or databases behind an implementation, one may read the values from a file, etc.")
(<p> "In this SRFI, Bawden-style arrays are called "(<i> 'specialized)". A specialized array is an example of a mutable array.")
(<h3> "Sharing generalized arrays")
(<p> "Even if an array $A$ is not a specialized array, then it could be \"shared\" by specifying a new interval $D_B$ as the domain of "
"a new array $B$ and an affine map $T_{BA}:D_B\\to D_A$. Each call to $B$ would then be computed as $B(\\vec i)=A(T_{BA}(\\vec i))$.")
(<p> "One could again \"share\" $B$, given a new interval $D_C$ as the domain of a new array $C$ and an affine transform $T_{CB}:D_C\\to D_B$, and then each access $C(\\vec i)=A(T_{BA}(T_{CB}(\\vec i)))$. The composition $T_{BA}\\circ T_{CB}:D_C\\to D_A$, being itself affine, could be precomputed and stored as $T_{CA}:D_C\\to D_A$, and $C(\\vec i)=A(T_{CA}(\\vec i))$ can be computed with the overhead of computing a single affine transformation.")
(<p> "So, if we wanted, we could share generalized arrays with constant overhead by adding a single layer of (multi-valued) affine transformations on top of evaluating generalized arrays. Even though this could be done transparently to the user, we do not do that here; it would be a compatible extension of this SRFI to do so. We provide only the routine "(<code>'specialized-array-share)", not a more general "(<code>'array-share)".")
(<p> "Certain ways of sharing generalized arrays, however, are relatively easy to code and not that expensive. If we denote "(<code>"(array-getter A)")" by "(<code>'A-getter)", then if B is the result of "(<code>'array-extract)" applied to A, then "
(<code>"(array-getter B)")" is simply "(<code>'A-getter)". Similarly, if A is a two-dimensional array, and B is derived from A by applying the permutation $\\pi((i,j))=(j,i)$, then "(<code>"(array-getter B)")" is "
(<code>"(lambda (i j) (A-getter j i))")". Translation and currying also lead to transformed arrays whose getters are relatively efficiently derived from "(<code>'A-getter)", at least for arrays of small dimension.")
(<p> "Thus, while we do not provide for sharing of generalized arrays for general one-to-one affine maps $T$, we do allow it for the specific functions "(<code>'array-extract)", "(<code>'array-translate)", "(<code>'array-permute)", "
(<code>'array-curry)", "(<code>'array-reverse)", and "(<code>'array-sample)", and we provide relatively efficient implementations of these functions for arrays of dimension no greater than four.")
(<h3> "Array-map does not produce a specialized array")
(<p> "Daniel Friedman and David Wise wrote a famous paper "(<a> href: "http://www.cs.indiana.edu/cgi-bin/techreports/TRNNN.cgi?trnum=TR44" "CONS should not Evaluate its Arguments")". "
"In the spirit of that paper, our procedure "(<code>'array-map)" does not immediately produce a specialized array, but a simple immutable array, whose elements are recomputed from the arguments of "(<code>'array-map)
" each time they are accessed. This immutable array can be passed on to further applications of "(<code>'array-map)" for further processing, without generating the storage bodies for intermediate arrays.")
(<p> "We provide the procedure "(<code>'array->specialized-array)" to transform a generalized array (like that returned by "(<code>'array-map)
") to a specialized, Bawden-style array, for which accessing each element again takes $O(1)$ operations.")
(<h3> "Notational convention")
(<p> "If "(<code>(<var>'A))" is an array, then we generally define "(<code>(<var>'A_))" to be "(<code>"(array-getter "(<var>'A)")")" and "(<code>(<var>'A!))" to be "(<code>"(array-setter "(<var>'A)")")". The latter notation is motivated by the general Scheme convention that the names of functions that modify the contents of data structures end in "(<code>(<var>"!"))", while the notation for the getter of an array is motivated by the TeX notation for subscripts. See particularly the "(<a> href: "#Haar" "Haar transform")" example.")
(<h2> "Issues and Notes")
(<ul>
(<li> (<b> "Relationship to "(<a> href: "http://docs.racket-lang.org/math/array_nonstrict.html#%28tech._nonstrict%29" "nonstrict arrays")" in Racket. ")
"It appears that what we call simply arrays in this SRFI are called nonstrict arrays in the math/array library of Racket, which in turn was influenced by an "(<a> href: "http://research.microsoft.com/en-us/um/people/simonpj/papers/ndp/RArrays.pdf" "array proposal for Haskell")". Our \"specialized\" arrays are related to Racket's \"strict\" arrays.")
(<li> (<b> "Indexers. ")"The argument new-domain->old-domain to "(<code> 'specialized-array-share)" is, conceptually, a multi-valued array.")
(<li> (<b> "Source of function names. ")"The function "(<code> 'array-curry)" gets its name from the " #\newline
(<a> href: "http://en.wikipedia.org/wiki/Currying" "curry operator")
" in programming---we are currying the getter of the array and keeping careful track of the domains. " #\newline
(<code>'interval-projections)" can be thought of as currying the " #\newline
"characteristic function of the interval, encapsulated here as "(<code> 'interval-contains-multi-index?)".")
(<li> (<b> "Choice of functions on intervals. ")"The choice of functions for both arrays and intervals was motivated almost solely by what I needed for arrays.")
(<li> (<b> "No empty intervals. ")"This SRFI considers arrays over only nonempty intervals of positive dimension. The author of this proposal acknowledges that other languages and array systems allow either zero-dimensional intervals or empty intervals of positive dimension, but prefers to leave such empty intervals as possibly compatible extensions to the current proposal.")
(<li> (<b> "Multi-valued arrays. ")"While this SRFI restricts attention to single-valued arrays, wherein the getter of each array returns a single value, allowing multi-valued immutable arrays would a compatible extension of this SRFI.")
(<li> (<b> "No low-level specialized-array constructor. ")
"While the author of the SRFI uses mainly "(<code>"(make-array ...)")", "(<code>'array-map)", and "(<code>'array->specialized-array)" to construct arrays, and while there are several other ways to construct arrays, there is no really low-level interface given for constructing specialized arrays (where one specifies a body, an indexer, etc.). It was felt that certain difficulties, some surmountable (such as checking that a given body is compatible with a given storage class) and some not (such as checking that an indexer is indeed affine), made a low-level interface less useful. At the same time, the simple "(<code>"(make-array ...)")" mechanism is so general, allowing one to specify getters and setters as general functions, as to cover nearly all needs.")
)
(<h2> "Specification")
(let ((END ",\n"))
(<p> "Names defined in this SRFI:")
(<dl>
(<dt> "Miscellaneous Functions")
(<dd> (<a> href: "#translation?" "translation?") END
(<a> href: "#permutation?" "permutation?")
".")
(<dt> "Intervals")
(<dd> (<a> href: "#make-interval" "make-interval")END
(<a> href: "#interval?" "interval?")END
(<a> href: "#interval-dimension" "interval-dimension")END
(<a> href: "#interval-lower-bound" "interval-lower-bound")END
(<a> href: "#interval-upper-bound" "interval-upper-bound")END
(<a> href: "#interval-lower-bounds->list" "interval-lower-bounds->list")END
(<a> href: "#interval-upper-bounds->list" "interval-upper-bounds->list")END
(<a> href: "#interval-lower-bounds->vector" "interval-lower-bounds->vector")END
(<a> href: "#interval-upper-bounds->vector" "interval-upper-bounds->vector")END
(<a> href: "#interval=" "interval=")END
(<a> href: "#interval-volume" "interval-volume")END
(<a> href: "#interval-subset?" "interval-subset?")END
(<a> href: "#interval-contains-multi-index?" "interval-contains-multi-index?")END
(<a> href: "#interval-projections" "interval-projections")END
(<a> href: "#interval-for-each" "interval-for-each")END
(<a> href: "#interval-dilate" "interval-dilate")END
(<a> href: "#interval-intersect" "interval-intersect")END
(<a> href: "#interval-translate" "interval-translate")END
(<a> href: "#interval-permute" "interval-permute") END
(<a> href: "#interval-scale" "interval-scale") END
(<a> href: "#interval-cartesian-product" "interval-cartesian-product")
".")
(<dt> "Storage Classes")
(<dd> (<a> href: "#make-storage-class" "make-storage-class") END
(<a> href: "#storage-class?" "storage-class?") END
(<a> href: "#storage-class-getter" "storage-class-getter") END
(<a> href: "#storage-class-setter" "storage-class-setter") END
(<a> href: "#storage-class-checker" "storage-class-checker") END
(<a> href: "#storage-class-maker" "storage-class-maker") END
(<a> href: "#storage-class-length" "storage-class-length") END
(<a> href: "#storage-class-default" "storage-class-default") END
(<a> href: "#generic-storage-class" "generic-storage-class") END
(<a> href: "#s8-storage-class" "s8-storage-class") END
(<a> href: "#s16-storage-class" "s16-storage-class") END
(<a> href: "#s32-storage-class" "s32-storage-class") END
(<a> href: "#s64-storage-class" "s64-storage-class") END
(<a> href: "#u1-storage-class" "u1-storage-class") END
(<a> href: "#u8-storage-class" "u8-storage-class") END
(<a> href: "#u16-storage-class" "u16-storage-class") END
(<a> href: "#u32-storage-class" "u32-storage-class") END
(<a> href: "#u64-storage-class" "u64-storage-class") END
(<a> href: "#f32-storage-class" "f32-storage-class") END
(<a> href: "#f64-storage-class" "f64-storage-class") END
(<a> href: "#c64-storage-class" "c64-storage-class") END
(<a> href: "#c128-storage-class" "c128-storage-class")
".")
(<dt> "Arrays")
(<dd> (<a> href: "#make-array" "make-array")END
(<a> href: "#array?" "array?")END
(<a> href: "#array-domain" "array-domain")END
(<a> href: "#array-getter" "array-getter")END
(<a> href: "#array-dimension" "array-dimension")END
(<a> href: "#mutable-array?" "mutable-array?")END
(<a> href: "#array-setter" "array-setter")END
(<a> href: "#specialized-array-default-safe?" "specialized-array-default-safe?") END
(<a> href: "#make-specialized-array" "make-specialized-array")END
(<a> href: "#specialized-array?" "specialized-array?")END
(<a> href: "#array-storage-class" "array-storage-class")END
(<a> href: "#array-indexer" "array-indexer")END
(<a> href: "#array-body" "array-body")END
(<a> href: "#array-safe?" "array-safe?") END
(<a> href: "#specialized-array-share" "specialized-array-share")END
(<a> href: "#array->specialized-array" "array->specialized-array")END
(<a> href: "#array-curry" "array-curry")END
(<a> href: "#array-extract" "array-extract") END
(<a> href: "#array-tile" "array-tile") END
(<a> href: "#array-translate" "array-translate")END
(<a> href: "#array-permute" "array-permute")END
(<a> href: "#array-reverse" "array-reverse")END
(<a> href: "#array-sample" "array-sample")END
(<a> href: "#array-outer-product" "array-outer-product") END
(<a> href: "#array-map" "array-map")END
(<a> href: "#array-for-each" "array-for-each")END
(<a> href: "#array-fold" "array-fold")END
(<a> href: "#array-fold-right" "array-fold-right")END
(<a> href: "#array-any" "array-any")END
(<a> href: "#array-every" "array-every")END
(<a> href: "#array->list" "array->list") END
(<a> href: "#list->specialized-array" "list->specialized-array") END
(<a> href: "#array-assign!" "array-assign!") END
(<a> href: "#array-swap!" "array-swap!")
"."
)))
(<h2> "Miscellaneous Functions")
(<p> "This document refers to "(<i> 'translations)" and "(<i> 'permutations)".
A translation is a vector of exact integers. A permutation of dimension $n$
is a vector whose entries are the exact integers $0,1,\\ldots,n-1$, each occurring once, in any order.")
(<h3> "Procedures")
(format-lambda-list '(translation? object))
(<p> "Returns "(<code> '#t)" if "(<code>(<var>'object))" is a translation, and "(<code> '#f)" otherwise.")
(format-lambda-list '(permutation? object))
(<p> "Returns "(<code> '#t)" if "(<code>(<var>'object))" is a permutation, and "(<code> '#f)" otherwise.")
(<h2> "Intervals")
(<p> "An interval represents the set of all multi-indices of exact integers
$i_0,\\ldots,i_{d-1}$
satisfying
$l_0\\leq i_0<u_0,\\ldots,l_{d-1}\\leq i_{d-1}<u_{d-1}$,
where the "(<i>"lower bounds")"
$l_0,\\ldots,l_{d-1}$
and the "(<i>"upper bounds")"
$u_0,\\ldots,u_{d-1}$
are specified multi-indices of exact integers. The positive integer $d$ is the "(<i>"dimension")"
of the interval. It is required that
$l_0<u_0,\\ldots,l_{d-1}<u_{d-1}$.")
(<p> "Intervals are a data type distinct from other Scheme data types.")
(<h3> "Procedures")
(format-lambda-list '(make-interval lower-bounds upper-bounds))
(<p> "Create a new interval; "(<code> (<var>"lower-bounds"))" and "(<code> (<var>"upper-bounds"))"
are nonempty vectors (of the same length) of exact integers that satisfy")
(<pre>
(<code>" (< (vector-ref "(<var>"lower-bounds")" i) (vector-ref "(<var>"upper-bounds")" i))"))
(<p> " for
$0\\leq i<{}$"(<code>"(vector-length "(<var>"lower-bounds")")")". It is an error if
"(<code>(<var>"lower-bounds"))" and "(<code>(<var>"upper-bounds"))" do not satisfy these conditions.")
(format-lambda-list '(interval? obj))
(<p> "Returns "(<code> "#t")" if "(<code> (<var>"obj"))" is an interval, and "(<code>"#f")" otherwise.")
(format-lambda-list '(interval-dimension interval))
(<p> "If "(<code>(<var>"interval"))" is an interval built with ")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds")" "(<var>"upper-bounds")")"))
(<p> "then "(<code> 'interval-dimension)" returns "(<code>"(vector-length "(<var>"lower-bounds")")")". It is an error to call "(<code> 'interval-dimension)"
if "(<code>(<var>"interval"))" is not an interval.")
(format-lambda-list '(interval-lower-bound interval i))
(format-lambda-list '(interval-upper-bound interval i))
(<p> "If "(<code>(<var>"interval"))" is an interval built with ")
(<blockquote>
(<code>"(make-interval "(<var>"lower-bounds")" "(<var>"upper-bounds")")"))
(<p> "and "(<code>(<var>"i"))" is an exact integer that satisfies")
(<blockquote>
"$0 \\leq i<$ "(<code>"(vector-length "(<var>"lower-bounds")")")",")
(<p> " then "(<code> 'interval-lower-bound)" returns
"(<code>"(vector-ref "(<var>"lower-bounds")" "(<var>"i")")")" and "(<code> 'interval-upper-bound)" returns
"(<code>"(vector-ref "(<var>"upper-bounds")" "(<var>"i")")")". It is an error to call "(<code> 'interval-lower-bound)" or "(<code> 'interval-upper-bound)"
if "(<code>(<var>"interval"))" and "(<code>(<var>"i"))" do not satisfy these conditions.")
(format-lambda-list '(interval-lower-bounds->list interval))
(format-lambda-list '(interval-upper-bounds->list interval))
(<p> "If "(<code>(<var>"interval"))" is an interval built with ")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds")" "(<var>"upper-bounds")")"))
(<p> " then "(<code> 'interval-lower-bounds->list)" returns "(<code> "(vector->list "(<var>"lower-bounds")")")
" and "(<code> 'interval-upper-bounds->list)" returns "(<code> "(vector->list "(<var>"upper-bounds")")")". It is an error to call
"(<code> 'interval-lower-bounds->list)" or "(<code> 'interval-upper-bounds->list)" if "(<code>(<var>"interval"))" does not satisfy these conditions.")
(format-lambda-list '(interval-lower-bounds->vector interval))
(format-lambda-list '(interval-upper-bounds->vector interval))
(<p> "If "(<code>(<var>"interval"))" is an interval built with ")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds")" "(<var>"upper-bounds")")"))
(<p> " then "(<code> 'interval-lower-bounds->vector)" returns a copy of "(<code> (<var>"lower-bounds"))
" and "(<code> 'interval-upper-bounds->vector)" returns a copy of "(<code> (<var>"upper-bounds"))". It is an error to call
"(<code> 'interval-lower-bounds->vector)" or "(<code> 'interval-upper-bounds->vector)" if "(<code>(<var>"interval"))" does not satisfy these conditions.")
(format-lambda-list '(interval-volume interval))
(<p> "If "(<code>(<var>"interval"))" is an interval built with ")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds")" "(<var>"upper-bounds")")"))
(<p> "then, assuming the existence of "(<code>'vector-map)", "(<code> 'interval-volume)" returns ")
(<pre>
(<code> "(apply * (vector->list (vector-map - "(<var>"upper-bounds")" "(<var>"lower-bounds")")))"))
(<p> "It is an error to call "(<code> 'interval-volume)" if "(<code>(<var> 'interval))" does not satisfy this condition.")
(format-lambda-list '(interval= interval1 interval2))
(<p> "If "(<code>(<var>"interval1"))" and "(<code>(<var>"interval2"))" are intervals built with ")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds1")" "(<var>"upper-bounds1")")"))
(<p> "and")
(<pre>
(<code>"(make-interval "(<var>"lower-bounds2")" "(<var>"upper-bounds2")")"))
(<p> "respectively, then "(<code> 'interval=)" returns")
(<pre>
(<code> "(and (equal? "(<var> 'lower-bounds1)" "(<var> 'lower-bounds2)") (equal? "(<var> 'upper-bounds1)" "(<var> 'upper-bounds2)"))"))
(<p> "It is an error to call "(<code> 'interval=)" if "(<code>(<var> 'interval1))" or "(<code>(<var> 'interval2))" do not satisfy this condition.")
(format-lambda-list '(interval-subset? interval1 interval2))
(<p> "If "(<code>(<var>"interval1"))" and "(<code>(<var>"interval2"))" are intervals of the same dimension $d$, "
"then "(<code>'interval-subset?)" returns "(<code>'#t)" if ")
(<pre>
(<code>"(<= (interval-lower-bound "(<var>'interval1)" j) (interval-lower-bound "(<var>'interval2)" j))"))
(<p> "and")
(<pre>
(<code>"(<= (interval-upper-bound "(<var>'interval1)" j) (interval-upper-bound "(<var>'interval2)" j))"))
(<p> "for all $0\\leq j<d$, otherwise it returns "(<code>'#f)". It is an error if the arguments do not satisfy these conditions.")
(format-lambda-list '(interval-contains-multi-index? interval index-0 index-1 ...))
(<p> "If "(<code>(<var> 'interval))" is an interval with dimension $d$ and "(<code>(<var> 'index-0))", "(<code>(<var> 'index-1))", ..., is a multi-index of length $d$,
then "(<code> 'interval-contains-multi-index?)" returns "(<code> #t)" if ")
(<blockquote>
(<code> "(interval-lower-bound "(<var> 'interval)" j)")" $\\leq$ "(<code> (<var> 'index-j))" $<$ "(<code> "(interval-upper-bound "(<var> 'interval)" j)"))
(<p>"for $0\\leq j < d$, and "(<code>'#f)" otherwise.")
(<p> "It is an error to call "(<code> 'interval-contains-multi-index?)" if "(<code>(<var> 'interval))" and "(<code>(<var> 'index-0))",..., do not satisfy this condition.")
(format-lambda-list '(interval-projections interval right-dimension))
(<p> "Conceptually, "(<code> 'interval-projections)" takes a $d$-dimensional interval
$[l_0,u_0)\\times [l_1,u_1)\\times\\cdots\\times[l_{d-1},u_{d-1})$\n"
"and splits it into two parts")
(<blockquote> "$[l_0,u_0)\\times\\cdots\\times[l_{d-\\text{right-dimension}-1},u_{d-\\text{right-dimension}-1})$")
(<p> "and")
(<blockquote> "$[l_{d-\\text{right-dimension}},u_{d-\\text{right-dimension}})\\times\\cdots\\times[l_{d-1},u_{d-1})$")
(<p> "This function, the inverse of Cartesian products or cross products of intervals, is used to keep track of the domains of curried arrays.")
(<p> "More precisely, if "(<code>(<var> 'interval))" is an interval and "(<code>(<var> 'right-dimension))" is an exact integer that satisfies "
(<code> "0 < "(<var> 'right-dimension)" < "(<var>'d))" then "(<code> 'interval-projections)" returns two intervals:")
(<pre>
(<code> "
(values
(make-interval
(vector (interval-lower-bound "(<var>'interval)" 0)
...
(interval-lower-bound "(<var>'interval)"
(- "(<var>'d)" "(<var>'right-dimension)" 1)))
(vector (interval-upper-bound "(<var>'interval)" 0)
...
(interval-upper-bound "(<var>'interval)"
(- "(<var>'d)" "(<var>'right-dimension)" 1))))
(make-interval
(vector (interval-lower-bound "(<var>'interval)"
(- "(<var>'d)" "(<var>'right-dimension)"))
...
(interval-lower-bound "(<var>'interval)"
(- "(<var>'d)" 1)))
(vector (interval-upper-bound "(<var>'interval)"
(- "(<var>'d)" "(<var>'right-dimension)"))
...
(interval-upper-bound "(<var>'interval)"
(- "(<var>'d)" 1)))))"))
(<p> "It is an error to call "(<code> 'interval-projections)" if its arguments do not satisfy these conditions.")
(format-lambda-list '(interval-for-each f interval))
(<p> "This routine assumes that "(<code>(<var> 'interval))" is an interval and "(<code>(<var> 'f))" is a routine whose domain includes elements of "(<code>(<var> 'interval))". It is an error to call
"(<code> 'interval-for-each)" if "(<code>(<var> 'interval))" and "(<code>(<var> 'f))" do not satisfy these conditions.")
(<p> (<code> 'interval-for-each)" calls "(<code>(<var> 'f))" with each multi-index of "(<code>(<var> 'interval))" as arguments, all in lexicographical order.")
(format-lambda-list '(interval-dilate interval lower-diffs upper-diffs))
(<p> "If "(<code>(<var> 'interval))" is an interval with
lower bounds l"(<sub>"0")", ..., l"(<sub>"d-1")" and
upper bounds u"(<sub>"0")", ..., u"(<sub>"d-1")", and "
(<code>(<var> "lower-diffs"))" is a vector of exact integers L"(<sub>"0")", ..., L"(<sub>"d-1")" and "
(<code>(<var> "upper-diffs"))" is a vector of exact integers U"(<sub>"0")", ..., U"(<sub>"d-1")", then "
(<code>"interval-dilate")" returns a new interval with
lower bounds l"(<sub>"0")"+L"(<sub>"0")", ..., l"(<sub>"d-1")"+L"(<sub>"d-1")" and
upper bounds u"(<sub>"0")"+U"(<sub>"0")", ..., u"(<sub>"d-1")"+U"(<sub>"d-1")", as long as this is a
nonempty interval. It is an error if the arguments do not satisfy these conditions.")
(<p> "Examples:")
(<pre>(<code>"
(interval=
(interval-dilate (make-interval '#(0 0) '#(100 100))
'#(1 1) '#(1 1))
(make-interval '#(1 1) '#(101 101))) => #t
(interval=
(interval-dilate (make-interval '#(0 0) '#(100 100))
'#(-1 -1) '#(1 1))
(make-interval '#(-1 -1) '#(101 101))) => #t
(interval=
(interval-dilate (make-interval '#(0 0) '#(100 100))
'#(0 0) '#(-50 -50))
(make-interval '#(0 0) '#(50 50))) => #t
(interval-dilate
(make-interval '#(0 0) '#(100 100))
'#(0 0) '#(-500 -50)) => error
"))
(format-lambda-list '(interval-intersect interval-1 interval-2 ...))
(<p> "If all the arguments are intervals of the same dimension and they have a nonempty intersection,
the "(<code> 'interval-intersect)" returns that intersection; otherwise it returns "(<code>'#f)".")
(<p> "It is an error if the arguments are not all intervals with the same dimension.")
(format-lambda-list '(interval-translate interval translation))
(<p> "If "(<code>(<var> 'interval))" is an interval with
lower bounds l"(<sub>"0")", ..., l"(<sub>"d-1")" and
upper bounds u"(<sub>"0")", ..., u"(<sub>"d-1")", and "
(<code>(<var> "translation"))" is a translation with entries T"(<sub>"0")", ..., T"(<sub>"d-1")
", then "
(<code>"interval-translate")" returns a new interval with
lower bounds l"(<sub>"0")"+T"(<sub>"0")", ..., l"(<sub>"d-1")"+T"(<sub>"d-1")" and
upper bounds u"(<sub>"0")"+T"(<sub>"0")", ..., u"(<sub>"d-1")"+T"(<sub>"d-1")".
It is an error if the arguments do not satisfy these conditions.")
(<p> "One could define "(<code> "(interval-translate interval translation)")" by "(<code> "(interval-dilate interval translation translation)")".")
(format-lambda-list '(interval-permute interval permutation))
(<p> "The argument "(<code>(<var>'interval))" must be an interval, and the argument "(<code>(<var>'permutation))" must be a valid permutation with the same dimension as "(<code>(<var>'interval))". It is an error if the arguments do not satisfy these conditions.")
(<p> "Heuristically, this function returns a new interval whose axes have been permuted in a way consistent with "(<code>(<var>'permutation))".
But we have to say how the entries of "(<code>(<var>'permutation))" are associated with the new interval.")
(<p> "We have chosen the following convention: If the permutation is $(\\pi_0,\\ldots,\\pi_{d-1})$, and the argument interval
represents the cross product
$[l_0,u_0)\\times[l_1,u_1)\\times\\cdots\\times[l_{d-1},u_{d-1})$,
then the result represents the cross product
$[l_{\\pi_0},u_{\\pi_0})\\times[l_{\\pi_1},u_{\\pi_1})\\times\\cdots\\times[l_{\\pi_{d-1}},u_{\\pi_{d-1}})$.")
(<p> "For example, if the argument interval represents $[0,4)\\times[0,8)\\times[0,21)\\times [0,16)$ and the
permutation is "(<code>'#(3 0 1 2))", then the result of "(<code> "(interval-permute "(<var>'interval)" "(<var>' permutation)")")" will be
the representation of $[0,16)\\times [0,4)\\times[0,8)\\times[0,21)$.")
(format-lambda-list '(interval-scale interval scales))
(<p> "If "(<code>(<var>'interval))" is a $d$-dimensional interval $[0,u_1)\\times\\cdots\\times[0,u_{d-1})$ with all lower bounds zero, "
"and "(<code>(<var>'scales))" is a length-$d$ vector of positive exact integers, which we'll denote by $\\vec s$, then "(<code>'interval-scale)
" returns the interval $[0,\\operatorname{ceiling}(u_1/s_1))\\times\\cdots\\times[0,\\operatorname{ceiling}(u_{d-1}/s_{d-1})$.")
(<p> "It is an error if "(<code>(<var>'interval))" and "(<code>(<var>'scales))" do not satisfy this condition.")
(format-lambda-list '(interval-cartesian-product interval #\. intervals))
(<p> "Implements the Cartesian product of the intervals in "
(<code>
"(cons "
(<var>'interval)
" "
(<var>'intervals)
")")". Returns")
(<pre>(<code>"
(make-interval (list->vector (apply append (map array-lower-bounds->list (cons interval intervals))))
(list->vector (apply append (map array-upper-bounds->list (cons interval intervals)))))"))
(<p> "It is an error if any argument is not an interval.")
(<h2> "Storage classes")
(<p> "Conceptually, a storage-class is a set of functions to manage the backing store of a specialized-array.
The functions allow one to make a backing store, to get values from the store and to set new values, to return the length of the store, and to specify a default value for initial elements of the backing store. Typically, a backing store is a (heterogeneous or homogeneous) vector. A storage-class has a type distinct from other Scheme types.")
(<h3> "Procedures")
(format-lambda-list '(make-storage-class getter setter checker maker length default))
(<p> "Here we assume the following relationships between the arguments of "(<code> 'make-storage-class)". Assume that the \"elements\" of
the backing store are of some \"type\", either heterogeneous (all Scheme types) or homogeneous (of some restricted type).")
(<ul>
(<li> (<code> "("(<var>"maker n")" "(<var> 'value)")")" returns an object containing "(<code>(<var> 'n))" elements of value "(<code>(<var> 'value))".")
(<li> "If "(<code>(<var> 'v))" is an object created by "
(<code>"("(<var> "maker n value")")")
" and 0 <= "(<code>(<var> 'i))" < "(<code>(<var> 'n))", then "(<code> "("(<var>"getter v i")")")" returns the current value of the "(<code>(<var> 'i))"'th element of "(<code>(<var> 'v))", and "(<code> "("(<var> 'checker)" ("(<var>"getter v i")")) => #t")".")
(<li> "If "(<code>(<var> 'v))" is an object created by "
(<code>"("(<var> "maker n value")")")
", 0 <= "(<code>(<var> 'i))" < "(<code>(<var> 'n))", and "(<code>"("(<var> 'checker)" "(<var> 'val)") => #t")", then "(<code> "("(<var>"setter v i val")")")" sets the value of the "(<code>(<var> 'i))"'th element of "(<code>(<var> 'v))" to "(<code>(<var> 'val))".")
(<li> "If "(<code>(<var> 'v))" is an object created by "
(<code>"("(<var> "maker n value")")")
" then "(<code> "("(<var>"length v")")")" returns "(<code>(<var> 'n))"."))
(<p> "If the arguments do not satisfy these conditions, then it is an error to call "(<code> 'make-storage-class)".")
(<p> "Note that we assume that "(<code>(<var> 'getter))" and "(<code>(<var> 'setter))" generally take "(<i> 'O)"(1) time to execute.")
(format-lambda-list '(storage-class? m))
(<p> "Returns "(<code>'#t)" if "(<code>(<var>'m))" is a storage class, and "(<code>'#f)" otherwise.")
(format-lambda-list '(storage-class-getter m))
(format-lambda-list '(storage-class-setter m))
(format-lambda-list '(storage-class-checker m))
(format-lambda-list '(storage-class-maker m))
(format-lambda-list '(storage-class-length m))
(format-lambda-list '(storage-class-default m))
(<p> "If "(<code>(<var> 'm))" is an object created by")
(<blockquote>
(<code>"(make-storage-class "(<var> "getter setter checker maker length default")")"))
(<p> " then "
(<code> 'storage-class-getter)" returns "(<code>(<var> 'getter))", "
(<code> 'storage-class-setter)" returns "(<code>(<var> 'setter))", "
(<code> 'storage-class-checker)" returns "(<code>(<var> 'checker))", "
(<code> 'storage-class-maker)" returns "(<code>(<var> 'maker))", and "
(<code> 'storage-class-length)" returns "(<code>(<var> 'length))", and "
(<code> 'storage-class-default)" returns "(<code>(<var> 'default))". Otherwise, it is an error to call any of these routines.")
(<h3> "Global Variables")
(format-global-variable 'generic-storage-class)
(format-global-variable 's8-storage-class)
(format-global-variable 's16-storage-class)
(format-global-variable 's32-storage-class)
(format-global-variable 's64-storage-class)
(format-global-variable 'u1-storage-class)
(format-global-variable 'u8-storage-class)
(format-global-variable 'u16-storage-class)
(format-global-variable 'u32-storage-class)
(format-global-variable 'u64-storage-class)
(format-global-variable 'f32-storage-class)
(format-global-variable 'f64-storage-class)
(format-global-variable 'c64-storage-class)
(format-global-variable 'c128-storage-class)
(<p> (<code> 'generic-storage-class)" is defined as if by")
(<pre>
(<code> "
(define generic-storage-class
(make-storage-class vector-ref
vector-set!
(lambda (arg) #t)
make-vector
vector-length
#f))"))
"Furthermore, "(<code> "s"(<var> 'X)"-storage-class")" is defined for "(<code>(<var> 'X))"=8, 16, 32, and 64 (which have default values 0 and
manipulate exact integer values between -2"(<sup>(<var> 'X)"-1")" and
2"(<sup> (<var> 'X)"-1")"-1 inclusive),
"(<code> "u"(<var> 'X)"-storage-class")" is defined for "(<code>(<var> 'X))"=1, 8, 16, 32, and 64 (which have default values 0 and manipulate exact integer values between 0 and
2"(<sup> (<var> 'X))"-1 inclusive),
"(<code> "f"(<var> 'X)"-storage-class")" is defined for "(<code>(<var> 'X))"= 32 and 64 (which have default value 0.0 and manipulate 32- and 64-bit floating-point numbers), and
"(<code> "c"(<var> 'X)"-storage-class")" is defined for "(<code>(<var> 'X))"= 64 and 128 (which have default value 0.0+0.0i and manipulate complex numbers with, respectively, 32- and 64-bit floating-point numbers as real and imaginary parts). Each of these
could be defined simply as "(<code>'generic-storage-class)", but it is assumed that implementations with homogeneous vectors will give definitions
that either save space, avoid boxing, etc., for the specialized arrays."
(<h2> "Arrays")
(<p> "Arrays are a data type distinct from other Scheme data types.")
(<h3> "Procedures")
(format-lambda-list '(make-array interval getter #\[ setter #\]))
(<p> "Assume first that the optional argument "(<code>'setter)" is not given.")
(<p> "If "(<code>(<var> 'interval))" is an interval and "(<code>(<var> 'getter))" is a function from
"(<code>(<var> 'interval))" to Scheme objects, then "(<code> 'make-array)" returns an array with domain "(<code>(<var> 'interval))"
and getter "(<code>(<var> 'getter))".")
(<p> "It is an error to call "(<code> 'make-array)" if "(<code>(<var> 'interval))" and "(<code>(<var> 'getter))"
do not satisfy these conditions.")
(<p> "If now "(<code>(<var> 'setter))" is specified, assume that it is a procedure such that getter and setter satisfy: If")
(<blockquote>
(<code>"("(<var> 'i)(<sub> '1)",...,"(<var> 'i)(<sub> 'n)")")" $\\neq$ "(<code> "("(<var> 'j)(<sub> '1)",...,"(<var> 'j)(<sub> 'n)")"))
(<p> "are elements of "(<code>(<var> 'interval))" and ")
(<blockquote>
(<code> "(getter "(<var> 'j)(<sub> '1)" ... "(<var> 'j)(<sub> 'n)") => x"))
(<p> "then \"after\"")
(<blockquote>
(<code> "(setter v "(<var> 'i)(<sub> '1)" ... "(<var> 'i)(<sub> 'n)")"))
(<p> "we have")
(<blockquote>
(<code> "(getter "(<var> 'j)(<sub> '1)" ... "(<var> 'j)(<sub> 'n)") => x"))
(<p> "and")
(<blockquote>
(<code> "(getter "(<var> 'i)(<sub> '1)",...,"(<var> 'i)(<sub> 'n)") => v"))
(<p> "Then "(<code> 'make-array)" builds a mutable array with domain "(<code>(<var> 'interval))", getter "(<code>(<var> 'getter))", and
setter "(<code>(<var> 'setter))". It is an error to call "(<code> 'make-array)" if its arguments do not satisfy these conditions.")
(<p> "Example: ")
(<pre>
(<code>"
(define a (make-array (make-interval '#(1 1) '#(11 11))
(lambda (i j)
(if (= i j)
1
0))))"))
(<p> "defines an array for which "(<code> "(array-getter a)")" returns 1 when i=j and 0 otherwise.")
(<p> "Example: ")
(<pre>
(<code>"
(define a ;; a sparse array
(let ((domain
(make-interval '#(0 0)
'#(1000000 1000000)))
(sparse-rows
(make-vector 1000000 '())))
(make-array
domain
(lambda (i j)
(cond ((assv j (vector-ref sparse-rows i))
=> cdr)
(else
0.0)))
(lambda (v i j)
(cond ((assv j (vector-ref sparse-rows i))
=> (lambda (pair)
(set-cdr! pair v)))
(else
(vector-set!
sparse-rows
i
(cons (cons j v)
(vector-ref sparse-rows i)))))))))
(define a_ (array-getter a))
(define a! (array-setter a))
(a_ 12345 6789) => 0.
(a_ 0 0) => 0.
(a! 1.0 0 0) => undefined
(a_ 12345 6789) => 0.
(a_ 0 0) => 1."))
(format-lambda-list '(array? obj))
(<p> "Returns "(<code> "#t")" if "(<code>(<var> 'obj))" is an array and "(<code> '#f)" otherwise.")
(format-lambda-list '(array-domain array))
(format-lambda-list '(array-getter array))
(<p> "If "(<code>(<var> 'array))" is an array built by")
(<pre>
(<code> "(make-array "(<var> 'interval)" "(<var> 'getter)" ["(<var> 'setter)"])"))
(<p> "(with or without the optional "(<code>(<var> 'setter))" argument) then "(<code> 'array-domain)" returns "(<code>(<var> 'interval))
" and "(<code> 'array-getter)" returns "(<code>(<var> 'getter))".
It is an error to call "(<code> 'array-domain)" or "(<code> 'array-getter)" if "(<code>(<var> 'array))" is not an array.")
(<p> "Example: ")
(<pre>
(<code>"
(define a (make-array (make-interval '#(1 1) '#(11 11))
(lambda (i j)
(if (= i j)
1
0))))
(defina a_ (array-getter a))
(a_ 3 3) => 1
(a_ 2 3) => 0
(a_ 11 0) => is an error"))
(format-lambda-list '(array-dimension array))
(<p> "Shorthand for "(<code>"(interval-dimension (array-domain "(<var>'array)"))")". It is an error to call this function if "(<code>(<var>'array))" is not an array.")
(format-lambda-list '(mutable-array? obj))
(<p> "Returns "(<code>"#t")" if "(<code>(<var> 'obj))" is a mutable array and "(<code> '#f)" otherwise.")
(format-lambda-list '(array-setter array))
(<p> "If "(<code>(<var> 'array))" is an array built by")
(<pre>
(<code> "(make-array "(<var> 'interval)" "(<var> 'getter)" "(<var> 'setter)")"))
(<p> "then "(<code> 'array-setter)" returns "(<code>(<var> 'setter))". It is an error to call "(<code> 'array-setter)"
if "(<code>(<var> 'array))" is not a mutable array.")
(format-lambda-list '(specialized-array-default-safe? #\[ bool #\]))
(<p> "With no argument, returns "(<code>'#t)" if newly-constructed specialized arrays check the arguments of setters and getters by default, and "(<code>'#f)" otherwise.")
(<p> "If "(<code>(<var>'bool))" is "(<code>'#t)" then the next call to "(<code>'specialized-array-default-safe?)" will return "(<code>'#t)";
if "(<code>(<var>'bool))" is "(<code>'#f)" then the next call to "(<code>'specialized-array-default-safe?)" will return "(<code>'#f)";
otherwise it is an error.")
(format-lambda-list '(make-specialized-array interval #\[ storage-class "generic-storage-class" #\] #\[ safe? "(specialized-array-default-safe?)"#\]))
(<p> "Constructs a specialized-array from its arguments.")
(<p> (<code>(<var>'interval))" must be given as a nonempty interval. If given, "(<code>(<var>'storage-class))" must be a storage class; if it is not given it defaults to "(<code>'generic-storage-class)". If given, "(<code>(<var>'safe?))" must be a boolean; if it is not given it defaults to the current value of "(<code>"(specialized-array-default-safe?)")".")
(<p>"The body of the result is constructed as ")
(<pre>
(<code>"
((storage-class-maker "(<var>'storage-class)")
(interval-volume "(<var>'interval)")
(storage-class-default "(<var>'storage-class)"))
"))
(<p> "The indexer of the resulting array is constructed as the lexicographical mapping of "(<code>(<var>'interval))" onto the interval "(<code> "[0,(interval-volume "(<var>'interval)"))")".")
(<p> "If "(<code>(<var>'safe))" is "(<code>'#t)", then the arguments of the getter and setter (including the value to be stored) of the resulting array are always checked for correctness.")
(<p> "After correctness checking (if needed), "(<code>"(array-getter "(<var>'array)")")" is defined simply as ")
(<pre>
(<code>"
(lambda multi-index
((storage-class-getter "(<var>'storage-class)")
(array-body "(<var>'array)")
(apply (array-indexer "(<var>'array)") multi-index)))
"))
(<p> " and "(<code>"(array-setter "(<var>'array)")")" is defined as ")
(<pre>
(<code>"
(lambda (val . multi-index)
((storage-class-setter "(<var>'storage-class)")
(array-body "(<var>'array)")
(apply (array-indexer "(<var>'array)") multi-index)
val))
"
))
(<p> "It is an error if the arguments of "(<code>'make-specialized-array)" do not satisfy these conditions.")
(<p> (<b> "Examples. ")"A simple array that can hold any type of element can be defined with "(<code>"(make-specialized-array (make-interval '#(0 0) '#(3 3)))")". If you find that you're using a lot of unsafe arrays of unsigned 16-bit integers, one could define ")
(<pre>
(<code>"
(define (make-u16-array interval)
(make-specialized-array interval u16-storage-class #f))
"))
(<p> "and then simply call, e.g., "(<code>"(make-u16-array (make-interval '#(0 0) '#(3 3)))")".")
(format-lambda-list '(specialized-array? obj))
(<p> "Returns "(<code>"#t")" if "(<code>(<var> 'obj))" is a specialized-array, and "(<code>"#f")" otherwise. A specialized-array is an array.")
(format-lambda-list '(array-storage-class array))
(format-lambda-list '(array-indexer array))
(format-lambda-list '(array-body array))
(format-lambda-list '(array-safe? array))
(<p> (<code>'array-storage-class)" returns the storage-class of "(<code>(<var> 'array))". "
(<code>'array-safe?)" is true if and only if the arguments of "(<code> "(array-getter "(<var> 'array)")")" and "(<code> "(array-setter "(<var> 'array)")")" (including the value to be stored in the array) are checked for correctness.")
(<p> (<code>"(array-indexer "(<var> 'array)")")" is assumed to be a one-to-one, but not necessarily onto, affine mapping from "(<code> "(array-domain "(<var> 'array)")")" into "(<code>"(array-body "(<var> 'array)")")".")
(<p> "It is an error to call any of these routines if "(<code>(<var> 'array))" is not a specialized-array.")
(format-lambda-list '(specialized-array-share array new-domain new-domain->old-domain))
(<p> "Constructs a new specialized-array that shares the body of the specialized-array "(<code>(<var> 'array))".
Returns an object that is behaviorally equivalent to a specialized array with the following fields:")
(<pre>
(<code>"
domain: new-domain
storage-class: (array-storage-class "(<var> 'array)")
body: (array-body "(<var> 'array)")
indexer: (lambda multi-index
(call-with-values
(lambda ()
(apply "(<var>'new-domain->old-domain)"
multi-index))
(array-indexer "(<var> 'array)")))"))
(<p> (<code>(<var> 'new-domain->old-domain))" must be an affine one-to-one mapping from "(<code>(<var> 'new-domain))" to
"(<code>"(array-domain "(<var> 'array)")")".")
(<p> "Note: It is assumed that affine structure of the composition of "(<code>(<var> 'new-domain->old-domain))" and "(<code>"(array-indexer "(<var> 'array))" will be used to simplify:")
(<pre>
(<code>"
(lambda multi-index
(call-with-values
(lambda ()
(apply "(<var>'new-domain->old-domain)" multi-index))
(array-indexer "(<var> 'array)")))"
))
(<p> "It is an error if "(<code>(<var>'array))" is not a specialized array, or if "(<code>(<var>'new-domain))" is not an interval, or if "(<code>(<var>'new-domain->old-domain))" is not a one-to-one affine mapping with the appropriate domain and range.")
(<p> (<b> "Example: ")
"One can apply a \"shearing\" operation to an array as follows: ")
(<pre>
(<code>"
(define a
(array->specialized-array
(make-array (make-interval '#(0 0) '#(5 10))
list)))
(define b
(specialized-array-share
a
(make-interval '#(0 0) '#(5 5))
(lambda (i j)
(values i (+ i j)))))
;; Print the \"rows\" of b
(array-for-each (lambda (row)
(pretty-print (array->list row)))
(array-curry b 1))
;; which prints
;; ((0 0) (0 1) (0 2) (0 3) (0 4))
;; ((1 1) (1 2) (1 3) (1 4) (1 5))
;; ((2 2) (2 3) (2 4) (2 5) (2 6))
;; ((3 3) (3 4) (3 5) (3 6) (3 7))
;; ((4 4) (4 5) (4 6) (4 7) (4 8))
"
))
(<p> "This \"shearing\" operation cannot be achieved by combining the procedures "(<code>'array-extract)", "(<code>'array-translate)", "(<code>'array-permute)", "(<code>'array-translate)", "(<code>'array-curry)", "(<code>'array-reverse)", and "(<code>'array-sample)".")
(format-lambda-list '(array->specialized-array array #\[ result-storage-class "generic-storage-class" #\] #\[ safe? "(specialized-array-default-safe?)" #\]))
(<p> "If "(<code>(<var> 'array))" is an array whose elements can be manipulated by the storage-class
"(<code>(<var> 'result-storage-class))", then the specialized-array returned by "(<code> 'array->specialized-array)" can be defined conceptually by:")
(<pre>
(<code>"
(let* ((domain
(array-domain "(<var>'array)"))
(result
(make-specialized-array domain
"(<var>'result-storage-class)"
"(<var>'safe?)")))
(array-assign! result "(<var>'array)")
result)"))
(<p> "It is guaranteed that "(<code>"(array-getter "(<var>'array)")")" is called precisely once for each multi-index in "(<code>"(array-domain "(<var>'array)")")" in lexicographical order.")
(<p> "It is an error if "(<code>(<var>'result-storage-class))" does not satisfy these conditions, or if "(<code>(<var>'safe?))" is not a boolean.")
(format-lambda-list '(array-curry array inner-dimension))
(<p> "If "
(<code>(<var> 'array))
" is an array whose domain is an interval $[l_0,u_0)\\times\\cdots\\times[l_{d-1},u_{d-1})$, and "
(<code>(<var> 'inner-dimension))
" is an exact integer strictly between $0$ and $d$, then "(<code>'array-curry)" returns an immutable array with domain "
"$[l_0,u_0)\\times\\cdots\\times[l_{d-\\text{inner-dimension}-1},u_{d-\\text{inner-dimension}-1})$"
", each of whose entries is in itself an array with domain $[l_{d-\\text{inner-dimension}},u_{d-\\text{inner-dimension}})\\times\\cdots\\times[l_{d-1},u_{d-1})$.")
(<p> "For example, if "(<code>'A)" and "(<code> 'B)" are defined by ")
(<pre>
(<code>"
(define interval (make-interval '#(0 0 0 0)
'#(10 10 10 10)))
(define A (make-array interval list))
(define B (array-curry A 1))
(define A_ (array-getter A))
(define B_ (array-getter B))
"))
(<p> "so")
(<pre>
(<code>"
(A_ i j k l) => (list i j k l)"))
(<p> "then "(<code>'B)" is an immutable array with domain "(<code>"(make-interval '#(0 0 0) '#(10 10 10))")", each
of whose elements is itself an (immutable) array and ")
(<pre>
(<code>"
(equal?
(A_ i j k l)
((array-getter (B_ i j k)) l)) => #t
"))
(<p> "for all multi-indices "(<code> "i j k l")" in "(<code> 'interval)".")
(<p> "The subarrays are immutable, mutable, or specialized according to whether the array argument is immutable, mutable, or specialized.")
(<p> "More precisely, if ")
(<pre>
(<code> "0 < "(<var> 'inner-dimension)" < (interval-dimension (array-domain "(<var> 'array)"))"))
(<p> "then "(<code> 'array-curry)" returns a result as follows.")
(<p> "If the input array is specialized, then array-curry returns")
(<pre>
(<code>"
(call-with-values
(lambda () (interval-projections (array-domain "(<var> 'array)")
"(<var> 'inner-dimension)"))
(lambda (outer-interval inner-interval)
(make-array
outer-interval
(lambda outer-multi-index
(specialized-array-share
"(<var> 'array)"
inner-interval
(lambda inner-multi-index
(apply values
(append outer-multi-index
inner-multi-index))))))))"))
(<p> "Otherwise, if the input array is mutable, then array-curry returns")
(<pre>
(<code>"
(call-with-values
(lambda () (interval-projections (array-domain "(<var> 'array)")
"(<var> 'inner-dimension)"))
(lambda (outer-interval inner-interval)
(make-array
outer-interval
(lambda outer-multi-index
(make-array
inner-interval
(lambda inner-multi-index
(apply (array-getter "(<var> 'array)")
(append outer-multi-index
inner-multi-index)))
(lambda (v . inner-multi-index)
(apply (array-setter "(<var> 'array)")
v
(append outer-multi-index
inner-multi-index))))))))"))
(<p> "Otherwise, array-curry returns")
(<pre>
(<code>"
(call-with-values
(lambda () (interval-projections (array-domain "(<var> 'array)")
"(<var> 'inner-dimension)"))
(lambda (outer-interval inner-interval)
(make-array
outer-interval
(lambda outer-multi-index
(make-array
inner-interval
(lambda inner-multi-index
(apply (array-getter "(<var> 'array)")
(append outer-multi-index
inner-multi-index))))))))"))
(<p> "It is an error to call "(<code> 'array-curry)" if its arguments do not satisfy these conditions.")
(<p> "Please see the note in the discussion of "(<a> href: "#array-tile" "array-tile")".")
(<p>"Example:")
(<pre>
(<code>"
(define a (make-array (make-interval '#(0 0)
'#(10 10))
list))
(define a_ (array-getter a))
(a_ 3 4) => (3 4)
(define curried-a (array-curry a 1))
(define curried-a_ (array-getter curried-a))
((array-getter (curried-a_ 3)) 4)
=> (3 4)"))
(format-lambda-list '(array-extract array new-domain))
(<p> "Returns a new array with the same getter (and setter, if appropriate) of the first argument, defined on the second argument.")
(<p> "Assumes that "(<code>(<var> 'array))" is an array and "(<code>(<var> 'new-domain))" is an interval that is a sub-interval of "(<code> "(array-domain "(<var> 'array)")")". If "(<code>(<var>'array))" is a specialized array, then returns ")
(<pre>
(<code>"
(specialized-array-share "(<var> 'array)"
"(<var> 'new-domain)"
values)
"))