-
Notifications
You must be signed in to change notification settings - Fork 42
/
generalized-arrays.lisp
340 lines (309 loc) · 10.8 KB
/
generalized-arrays.lisp
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
(defpackage :serapeum/generalized-arrays
(:use :cl :alexandria :serapeum)
(:export
:tally
:valence
:each
:each-left
:each-right
:tell
:array=)
(:documentation "Implementation of generalized arrays.")
#+sb-package-locks (:implement :serapeum :serapeum/dispatch-case))
(in-package :serapeum/generalized-arrays)
(defsubst tally (array)
"Return the total size of ARRAY, a generalized array.
For a true array this is equivalent to `array-total-size'."
;; (reduce #'* (shape array))
(typecase array
(sequence (length array))
(array (array-total-size array))
(t 0)))
(defsubst shape (array)
"Return the shape of ARRAY, a generalized array.
For a true array this is equivalent to `array-dimensions'."
(typecase array
(sequence (list (length array)))
(array (array-dimensions array))
;; An array with no axes.
(otherwise nil)))
(defsubst valence (array)
"Return the number of dimensions of ARRAY, a generalized array.
For a true array this is equivalent to `array-rank'."
;; (tally (shape array))
(typecase array
(sequence 1)
(array (array-rank array))
(t 0)))
(defsubst shape= (array1 array2)
"Return true if ARRAY1 and ARRAY2 have the same shape."
;; (equal (shape array1) (shape array2))
(typecase array1
(sequence
(typecase array2
(sequence
(length= array1 array2))
(otherwise nil)))
(array
(typecase array2
(vector nil)
(array
(equal (shape array1) (shape array2)))
(otherwise nil)))
(otherwise
(typecase array2
(sequence nil)
(array nil)
(otherwise t)))))
(defsubst ensure-shape (x)
(etypecase x
(array-length (list x))
(list x)))
(defsubst replace* (out in)
"Like `replace' with no keyword arguments, but if IN is shorter than
OUT, extend it cyclically.
In the base case, if IN is empty, leave OUT alone."
(if (emptyp in) out
(loop for start from 0 below (length out) by (length in)
do (replace out in :start1 start)
finally (return out))))
(defsubst %flatten (array)
(make-array (array-total-size array)
:displaced-to array
:displaced-index-offset 0
:element-type (array-element-type array)))
(defsubst shrink-wrap (object shape)
"Make an array of shape SHAPE containing OBJECT as its initial element.
The array will have the smallest element type sufficient to contain
OBJECT."
(make-array shape
:initial-element object
:element-type (upgraded-array-element-type `(eql ,object))))
(defsubst void (x)
(shrink-wrap x 0))
(defsubst displace (array shape
&optional (offset 0))
"Shorthand function for displacing an array."
(make-array (ensure-shape shape)
:displaced-to array
:displaced-index-offset offset
:element-type (array-element-type array)))
(defun reshape (shape array &key (element-type t) (displace t))
"Return an array that has the same items as ARRAY, a generalized
array, but whose shape is SHAPE.
If the resulting array is smaller than ARRAY, then discard the excess
items.
If the resulting array is larger than ARRAY, fill it with the items of
ARRAY cyclically.
ELEMENT-TYPE specifies an element type to use for the resulting array
if one cannot be inferred from the array itself."
(setf shape (ensure-shape shape))
;; (when (arrayp array)
;; (setf array (undisplace-array array)))
(cond
((equal shape (shape array))
array)
((null shape)
(assure (or null (vector * 0))
(typecase array
(array
(make-array 0 :element-type (array-element-type array)))
(number (void array))
(t nil))))
((null (cdr shape))
(assure sequence
(let ((len (car shape)))
(typecase array
(sequence
(let ((array-len (length array)))
(if (<= len array-len)
(if displace
(nsubseq array 0 len)
(subseq array 0 len))
(lret ((out (serapeum::make-sequence-like array len)))
(replace* out array)))))
(array
(let ((element-type (array-element-type array)))
(or (and (<= len (array-total-size array))
(if displace
(displace array len)
(and (= len (array-total-size array))
(make-array len
:element-type element-type
:initial-contents (%flatten array)))))
(lret ((out (make-array
len
:element-type (array-element-type array))))
(replace* out (%flatten array))))))
(t (shrink-wrap array shape))))))
(t
(assure (and array (not vector))
(let ((size (apply #'* shape)))
(typecase array
(vector
(or (and (<= size (length array))
(and displace
(displace array shape)))
(lret ((out (make-array
shape
:element-type (array-element-type array))))
(replace* (%flatten out) array))))
(sequence
(lret ((out (make-array shape :element-type element-type)))
(replace* (%flatten out) array)))
(array
(let ((element-type (array-element-type array)))
(or (and (<= size (array-total-size array))
(and displace
(displace array shape)))
(lret ((out (make-array shape
:element-type element-type)))
(replace* (%flatten out)
(%flatten array))))))
(t (shrink-wrap array shape))))))))
(defun ravel (array &key (displace t))
"Return the items of ARRAY as a sequence.
Array theory calls this operation `list', but the MOA operation is
identical and has a more distinctive name."
;; (reshape (tally array) array)
(typecase array
(sequence (copy-seq array))
(array (reshape (tally array) array :displace displace))
(t (list array))))
(defun tell (shape)
(etypecase shape
(array-index (range shape))
(sequence
(lret* ((shape (ensure-shape shape))
(array (make-array shape)))
(loop for i from 0 below (array-total-size array)
do (setf (row-major-aref array i)
(array-index-row-major array i)))))))
(defun array= (x y)
#.+merge-tail-calls+
(and (shape= x y)
(typecase x
(sequence
(typecase y
(sequence
(every #'array= x y))
(otherwise nil)))
(array
(typecase y
(array
(loop with size = (array-total-size x)
for i below size
always (array= (row-major-aref x i)
(row-major-aref y i))))
(otherwise nil)))
(otherwise (equal x y)))))
(defun each (fn array &key (element-type t))
(let ((fn (ensure-function fn)))
(typecase array
(list (mapcar fn array))
(vector (map-into
(make-array (length array) :element-type element-type)
fn
array))
(sequence (map-into
(serapeum::make-sequence-like array (length array))
fn array))
(array
(lret ((out (make-array
(array-dimensions array)
:element-type element-type)))
(map-into (%flatten out)
fn
(%flatten array))))
(otherwise (funcall fn array)))))
(defun each-left (array fn fixed &key (element-type t))
"The left refers to the position of the array."
(fbind (fn)
(each (op (fn _ fixed))
array
:element-type element-type)))
(defun each-right (fixed fn array &key (element-type t))
(fbind (fn)
(each (op (fn fixed _))
array
:element-type element-type)))
(defun mutual-element-type (arrays)
(upgraded-array-element-type
(cons 'or
(map 'list
(lambda (array)
(if (arrayp array)
(array-element-type array)
t))
arrays))))
(defun link (arrays)
"Return a list of all of the items in ARRAYS."
(cond
((nor (arrayp arrays)
(typep arrays 'sequence))
(list arrays))
((notevery #'arrayp arrays)
(collecting
(do-each (a arrays)
(typecase a
(sequence
(do-each (x a)
(collect x)))
(array
(loop for i from 0 below (array-total-size a) do
(collect (row-major-aref a i))))
(otherwise (collect a)))
arrays)))
(t
(let* ((size (reduce #'+ arrays :key #'array-total-size))
(element-type (mutual-element-type arrays))
(offset 0)
(array-out (make-array size :element-type element-type)))
(do-each (a arrays array-out)
(replace array-out a :start1 offset))))))
;;; TODO Experiment with value.
(defconst seq-cutoff 128
"Max length above which to operate pairwise.")
(defun reduce-between (fn xs start end)
(fbind fn
(let ((first-time? t)
(result nil))
(loop for i from start below end
do (if first-time?
(setf first-time? nil
result (aref xs i))
(setf result (fn result (aref xs i))))
finally (return result)))))
(defun reduce-vector-pairwise (fun xs)
(fbindrec (fun
(pairwise
(lambda (start end)
(let ((len (- end start)))
(if (<= len seq-cutoff)
(reduce-between fun xs start end)
(let ((split (+ start (ceiling len 2))))
(fun (pairwise start split)
(pairwise split end))))))))
(pairwise 0 (length xs))))
(defun pairwise (fn xs)
(reduce-vector-pairwise fn (coerce xs 'vector)))
(defun sum (array)
"Return the sum of all of the elements of ARRAY, a generalized array.
Operates pairwise for numerical stability."
(etypecase array
(bit-vector
(with-type-dispatch (simple-bit-vector bit-vector) array
(count 1 array)))
(sequence (or (pairwise #'+ array) 0))
(array (sum (%flatten array)))
(number array)))
(defun prod (array)
"Return the product of all of the elements of ARRAY, a generalized array.
Operates pairwise for numerical stability."
(etypecase array
(bit-vector
(with-type-dispatch (simple-bit-vector bit-vector) array
(if (find 0 array) 0 1)))
(sequence (or (pairwise #'* array) 1))
(array (prod (%flatten array)))
(number array)))