Source file batBigarray.ml
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
module A = struct include BatArray include BatArray.Labels end
not necessary for typing,
but they are necessary for the compatibility test in batteries_compattest.ml
which are of the form:
module _ = (BatBigarray : module type of Bigarray)
because of the somewhat strange interpretation of strengthening in (module type of),
we need to explicitly equate each type with its constructor *)
##V>=5.2##type float16_elt = Bigarray.float16_elt = Float16_elt
type float32_elt = Bigarray.float32_elt
##V>=4.2## = Float32_elt
type float64_elt = Bigarray.float64_elt
##V>=4.2## = Float64_elt
type complex32_elt = Bigarray.complex32_elt
##V>=4.2## = Complex32_elt
type complex64_elt = Bigarray.complex64_elt
##V>=4.2## = Complex64_elt
type int8_signed_elt = Bigarray.int8_signed_elt
##V>=4.2## = Int8_signed_elt
type int8_unsigned_elt = Bigarray.int8_unsigned_elt
##V>=4.2## = Int8_unsigned_elt
type int16_signed_elt = Bigarray.int16_signed_elt
##V>=4.2## = Int16_signed_elt
type int16_unsigned_elt = Bigarray.int16_unsigned_elt
##V>=4.2## = Int16_unsigned_elt
type int_elt = Bigarray.int_elt
##V>=4.2## = Int_elt
type int32_elt = Bigarray.int32_elt
##V>=4.2## = Int32_elt
type int64_elt = Bigarray.int64_elt
##V>=4.2## = Int64_elt
type nativeint_elt = Bigarray.nativeint_elt
##V>=4.2## = Nativeint_elt
type ('a, 'b) kind = ('a,'b) Bigarray.kind
##V>=4.2## = Float32 : (float, float32_elt) kind
##V>=4.2## | Float64 : (float, float64_elt) kind
##V>=4.2## | Int8_signed : (int, int8_signed_elt) kind
##V>=4.2## | Int8_unsigned : (int, int8_unsigned_elt) kind
##V>=4.2## | Int16_signed : (int, int16_signed_elt) kind
##V>=4.2## | Int16_unsigned : (int, int16_unsigned_elt) kind
##V>=4.2## | Int32 : (int32, int32_elt) kind
##V>=4.2## | Int64 : (int64, int64_elt) kind
##V>=4.2## | Int : (int, int_elt) kind
##V>=4.2## | Nativeint : (nativeint, nativeint_elt) kind
##V>=4.2## | Complex32 : (Complex.t, complex32_elt) kind
##V>=4.2## | Complex64 : (Complex.t, complex64_elt) kind
##V>=4.2## | Char : (char, int8_unsigned_elt) kind
##V>=5.2## | Float16 : (float, float16_elt) kind
##V<4.2##type untyped_kind =
##V<4.2## | Float32
##V<4.2## | Float64
##V<4.2## | Int8_signed
##V<4.2## | Int8_unsigned
##V<4.2## | Int16_signed
##V<4.2## | Int16_unsigned
##V<4.2## | Int32
##V<4.2## | Int64
##V<4.2## | Int
##V<4.2## | Nativeint
##V<4.2## | Complex32
##V<4.2## | Complex64
##V<4.2## | Char
##V<4.2##external untyped_kind_of_kind : (_, _) kind -> untyped_kind = "%identity"
type c_layout = Bigarray.c_layout
##V>=4.2## = C_layout_typ
type fortran_layout = Bigarray.fortran_layout
##V>=4.2## = Fortran_layout_typ
type 'a layout = 'a Bigarray.layout
##V>=4.2## = C_layout : c_layout layout
##V>=4.2## | Fortran_layout : fortran_layout layout
let float32 = Bigarray.float32
let float64 = Bigarray.float64
let complex32 = Bigarray.complex32
let complex64 = Bigarray.complex64
let int8_signed = Bigarray.int8_signed
let int8_unsigned = Bigarray.int8_unsigned
let int16_signed = Bigarray.int16_signed
let int16_unsigned = Bigarray.int16_unsigned
let int = Bigarray.int
let int32 = Bigarray.int32
let int64 = Bigarray.int64
let nativeint = Bigarray.nativeint
let char = Bigarray.char
##V>=4.3##let kind_size_in_bytes = Bigarray.kind_size_in_bytes
##V=4.2##let kind_size_in_bytes : type a b. (a, b) kind -> int = function
##V<4.2##let kind_size_in_bytes (kind : (_, _) kind) : int =
##V<4.2## match untyped_kind_of_kind kind with
##V<=4.2##
##V<=4.2## | Float32 -> 4
##V<=4.2## | Float64 -> 8
##V<=4.2## | Int8_signed -> 1
##V<=4.2## | Int8_unsigned -> 1
##V<=4.2## | Int16_signed -> 2
##V<=4.2## | Int16_unsigned -> 2
##V<=4.2## | Int32 -> 4
##V<=4.2## | Int64 -> 8
##V<=4.2## | Int -> Sys.word_size / 8
##V<=4.2## | Nativeint -> Sys.word_size / 8
##V<=4.2## | Complex32 -> 8
##V<=4.2## | Complex64 -> 16
##V<=4.2## | Char -> 1
let c_layout = Bigarray.c_layout
let fortran_layout = Bigarray.fortran_layout
##V<4.2##let ofs_of_layout (layout : _ Bigarray.layout) =
##V<4.2## match (Obj.magic layout : int) with
##V<4.2## | 0 -> 0
##V<4.2## | 0x100 -> 1
##V<4.2## | _ -> failwith "Unknown layout"
##V>=4.2##let ofs_of_layout : type a . a Bigarray.layout -> int = function
##V>=4.2## | Bigarray.C_layout -> 0
##V>=4.2## | Bigarray.Fortran_layout -> 1
module Genarray =
struct
include Bigarray.Genarray
##V>=4.8##let map_file = Unix.map_file
let ofs e = ofs_of_layout (layout e)
##V<4.3## let size_in_bytes arr =
##V<4.3## (kind_size_in_bytes (kind arr)) * (Array.fold_left ( * ) 1 (dims arr))
(**
Emulate multi-dimensional coordinates.
@param index The index of the element.
@param dims The dimensions of the array.
@param coor A buffer in which to write the various coordinates
*)
(**
Determine the coordinates of the item following this one.
@param coor Coordinates to increment.
@param dims The set of coordinates of the array.
@return [true] if everything happened correctly, [false] if
we've passed the last element.
*)
let inplace_next ~ofs ~dims ~coor =
let rec aux i =
if i < 0 then false
else
let new_value = coor.(i) + 1 in
if new_value = dims.(i) + ofs then
begin
coor.(i) <- ofs;
aux (i - 1)
end
else
begin
coor.(i) <- new_value;
true
end
in aux (Array.length dims - 1)
let iter f e =
let dims = dims e in
let offset = ofs e in
let coor = A.create (num_dims e) ~init:offset in
f (get e coor);
while inplace_next ~ofs:offset ~dims ~coor do
f (get e coor)
done
let iteri f e =
let dims = dims e in
let offset = ofs e in
let coor = A.create (num_dims e) ~init:offset in
f (A.Cap.of_array coor) (get e coor);
while inplace_next ~ofs:offset ~dims ~coor do
f (A.Cap.of_array coor) (get e coor)
done
let modify f e =
let dims = dims e in
let offset = ofs e in
let change c = set e c (f (get e c)) in
let coor = A.create (num_dims e) ~init:offset in
change coor;
while inplace_next ~ofs:offset ~dims ~coor do
change coor
done
let modifyi f e =
let dims = dims e in
let offset = ofs e in
let change c = set e c (f (A.Cap.of_array c) (get e c)) in
let coor = A.create (num_dims e) ~init:offset in
change coor;
while inplace_next ~ofs:offset ~dims ~coor do
change coor
done
let enum e =
let dims = dims e
and offset = ofs e in
let coor = A.create (num_dims e) ~init:offset
and status = ref `ongoing in
BatEnum.from (fun () ->
match !status with
| `ongoing ->
begin
try
let result = get e coor in
let update = inplace_next ~ofs:offset ~dims ~coor in
if not update then status := `dry;
result
with _ ->
status := `dry;
raise BatEnum.No_more_elements
end
| `dry ->
raise BatEnum.No_more_elements
)
let map f b_kind a =
let d = dims a in
let b = create b_kind (layout a) d in
iteri (fun i x -> set b (A.Cap.to_array i) (f x)) a;
b
let mapi f b_kind a =
let d = dims a in
let b = create b_kind (layout a) d in
iteri (fun i x -> set b (A.Cap.to_array i) (f (A.Cap.read_only i) x)) a;
b
end
##V>=4.5##external genarray_of_array0: ('a, 'b, 'c) Bigarray.Array0.t -> ('a, 'b, 'c) Genarray.t
##V>=4.5## = "%identity"
external genarray_of_array1: ('a, 'b, 'c) Bigarray.Array1.t -> ('a, 'b, 'c) Genarray.t
= "%identity"
external genarray_of_array2: ('a, 'b, 'c) Bigarray.Array2.t -> ('a, 'b, 'c) Genarray.t
= "%identity"
external genarray_of_array3: ('a, 'b, 'c) Bigarray.Array3.t -> ('a, 'b, 'c) Genarray.t
= "%identity"
external reshape:
('a, 'b, 'c) Genarray.t -> int array -> ('a, 'b, 'c) Genarray.t
= "caml_ba_reshape"
let reshape_3 = Bigarray.reshape_3
let reshape_2 = Bigarray.reshape_2
let reshape_1 = Bigarray.reshape_1
##V>=4.5##let reshape_0 = Bigarray.reshape_0
let array3_of_genarray = Bigarray.array3_of_genarray
let array2_of_genarray = Bigarray.array2_of_genarray
let array1_of_genarray = Bigarray.array1_of_genarray
##V>=4.5##let array0_of_genarray = Bigarray.array0_of_genarray
##V>=4.5##module Array0 = struct
##V>=4.5## include Bigarray.Array0
##V>=4.5##end
module Array1 = struct
include Bigarray.Array1
##V>=4.8##let map_file fd ?pos kind layout shared dim =
##V>=4.8## Bigarray.array1_of_genarray
##V>=4.8## (Unix.map_file fd ?pos kind layout shared [|dim|])
let ofs e = ofs_of_layout (layout e)
##V<4.3## let size_in_bytes arr =
##V<4.3## (kind_size_in_bytes (kind arr)) * (dim arr)
let enum t =
let offset = ofs t in
BatEnum.init (dim t) (fun i -> t.{offset + i})
let of_enum kind layout enum =
let b_dim = BatEnum.count enum in
let b = create kind layout b_dim in
for i = ofs b to ofs b + b_dim - 1 do
b.{i} <- BatEnum.get_exn enum
done;
b
s = String.of_enum (Array1.enum \
(Array1.of_enum char c_layout (String.enum s))))
Q.string (fun s -> s = String.of_enum (Array1.enum \
(Array1.of_enum char fortran_layout (String.enum s))))
(Q.list Q.int) (fun li -> li = List.of_enum (Array1.enum \
(Array1.of_enum int c_layout (List.enum li))))
*)
let map f b_kind a =
let b_dim = dim a in
let b = create b_kind (layout a) b_dim in
for i = ofs a to ofs a + b_dim - 1 do
b.{i} <- f a.{i}
done;
b
let mapi f b_kind a =
let b_dim = dim a in
let b = create b_kind (layout a) b_dim in
for i = ofs a to ofs a + b_dim - 1 do
b.{i} <- f i a.{i}
done;
b
let modify f a =
for i = ofs a to ofs a + dim a - 1 do
unsafe_set a i (f (unsafe_get a i))
done
let modifyi f a =
for i = ofs a to ofs a + dim a - 1 do
unsafe_set a i (f i (unsafe_get a i))
done
let to_array a = Array.init (dim a) (fun i -> a.{i+(ofs a)})
end
module Array2 = struct
include Bigarray.Array2
##V>=4.8##let map_file fd ?pos kind layout shared dim1 dim2 =
##V>=4.8## Bigarray.array2_of_genarray
##V>=4.8## (Unix.map_file fd ?pos kind layout shared [|dim1; dim2|])
let ofs e = ofs_of_layout (layout e)
##V<4.3## let size_in_bytes arr =
##V<4.3## (kind_size_in_bytes (kind arr)) * (dim1 arr) * (dim2 arr)
let enum t = Genarray.enum (genarray_of_array2 t)
let map f b_kind a =
let b_dim1 = dim1 a in
let b_dim2 = dim2 a in
let b = create b_kind (layout a) b_dim1 b_dim2 in
for i = ofs a to ofs a + b_dim1 - 1 do
for j = ofs a to ofs a + b_dim2 - 1 do
b.{i, j} <- f a.{i, j}
done
done;
b
let mapij f b_kind a =
let b_dim1 = dim1 a in
let b_dim2 = dim2 a in
let b = create b_kind (layout a) b_dim1 b_dim2 in
for i = ofs a to ofs a + b_dim1 - 1 do
for j = ofs a to ofs a + b_dim2 - 1 do
b.{i, j} <- f i j a.{i, j}
done
done;
b
let modify f a =
for i = ofs a to ofs a + dim1 a - 1 do
for j = ofs a to ofs a + dim2 a - 1 do
unsafe_set a i j (f (unsafe_get a i j))
done
done
let modifyij f a =
for i = ofs a to ofs a + dim1 a - 1 do
for j = ofs a to ofs a + dim2 a - 1 do
unsafe_set a i j (f i j (unsafe_get a i j))
done
done
let to_array a =
Array.init (dim1 a) (
fun i ->
Array.init (dim2 a) (
fun j -> a.{i + ofs a, j + ofs a}
)
)
end
module Array3 = struct
include Bigarray.Array3
##V>=4.8##let map_file fd ?pos kind layout shared dim1 dim2 dim3 =
##V>=4.8## Bigarray.array3_of_genarray
##V>=4.8## (Unix.map_file fd ?pos kind layout shared [|dim1; dim2; dim3|])
let ofs e = ofs_of_layout (layout e)
##V<4.3## let size_in_bytes arr =
##V<4.3## (kind_size_in_bytes (kind arr)) * (dim1 arr) * (dim2 arr) * (dim3 arr)
let enum t = Genarray.enum (genarray_of_array3 t)
let map f b_kind a =
let b_dim1 = dim1 a in
let b_dim2 = dim2 a in
let b_dim3 = dim3 a in
let b = create b_kind (layout a) b_dim1 b_dim2 b_dim3 in
for i = 0 to b_dim1 - 1 do
for j = 0 to b_dim2 - 1 do
for k = 0 to b_dim3 - 1 do
b.{i, j, k} <- f a.{i, j, k}
done
done
done;
b
let mapijk f b_kind a =
let b_dim1 = dim1 a in
let b_dim2 = dim2 a in
let b_dim3 = dim3 a in
let b = create b_kind (layout a) b_dim1 b_dim2 b_dim3 in
for i = 0 to b_dim1 - 1 do
for j = 0 to b_dim2 - 1 do
for k = 0 to b_dim3 - 1 do
b.{i, j, k} <- f i j k a.{i, j, k}
done
done
done;
b
let modify f a =
for i = ofs a to ofs a + dim1 a - 1 do
for j = ofs a to ofs a + dim2 a - 1 do
for k = ofs a to ofs a + dim3 a - 1 do
unsafe_set a i j k (f (unsafe_get a i j k))
done
done
done
let modifyijk f a =
for i = ofs a to ofs a + dim1 a - 1 do
for j = ofs a to ofs a + dim2 a - 1 do
for k = ofs a to ofs a + dim3 a - 1 do
unsafe_set a i j k (f i j k (unsafe_get a i j k))
done
done
done
let to_array a =
Array.init (dim1 a) (
fun i ->
Array.init (dim2 a) (
fun j ->
Array.init (dim3 a) (
fun k -> a.{i, j, k}
)
)
)
end