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
open Ast
(** Helper: get values from a VVector or VList *)
let to_value_array label = function
| VVector arr -> Ok arr
| VList items -> Ok (Array.of_list (List.map snd items))
| VNA _ -> Error (Error.na_value_error label)
| _ -> Error (Error.type_error (Printf.sprintf "Function `%s` expects a Vector or List." label))
let register env =
(*
--# Cumulative Sum
--#
--# Calculates the cumulative sum of a vector.
--#
--# @name cumsum
--# @param x :: Vector The input numeric vector.
--# @return :: Vector The cumulative sum.
--# @example
--# cumsum([1, 2, 3])
--# -- Returns = [1, 3, 6]
--# @family colcraft
--# @seealso sum, cummax, cummin
--# @export
*)
(* cumsum(x): cumulative sum; NA propagates to all subsequent values *)
let env = Env.add "cumsum"
(make_builtin ~name:"cumsum" 1 (fun args _env ->
match args with
| [arg] ->
(match to_value_array "cumsum" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NAFloat) in
let running = ref 0.0 in
let all_int = ref true in
let na_seen = ref false in
let had_error = ref None in
for i = 0 to n - 1 do
if !had_error = None then
if !na_seen then
result.(i) <- VNA NAFloat
else
match arr.(i) with
| VInt x ->
running := !running +. float_of_int x;
result.(i) <- if !all_int then VInt (int_of_float !running) else VFloat !running
| VFloat f ->
all_int := false;
running := !running +. f;
if i > 0 then
for j = 0 to i - 1 do
match result.(j) with
| VInt v -> result.(j) <- VFloat (float_of_int v)
| _ -> ()
done;
result.(i) <- VFloat !running
| VNA _ -> na_seen := true; result.(i) <- VNA NAFloat
| _ -> had_error := Some (Error.type_error "Function `cumsum` requires numeric values.")
done;
(match !had_error with Some e -> e | None -> VVector result))
| _ -> Error.arity_error_named "cumsum" 1 (List.length args)
))
env
in
(*
--# Cumulative Minimum
--#
--# Calculates the cumulative minimum of a vector.
--#
--# @name cummin
--# @param x :: Vector The input numeric vector.
--# @return :: Vector The cumulative minimum.
--# @example
--# cummin([3, 2, 4, 1])
--# -- Returns = [3, 2, 2, 1]
--# @family colcraft
--# @seealso min, cummax, cumsum
--# @export
*)
(* cummin(x): cumulative minimum; NA propagates to all subsequent values *)
let env = Env.add "cummin"
(make_builtin ~name:"cummin" 1 (fun args _env ->
match args with
| [arg] ->
(match to_value_array "cummin" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NAFloat) in
let running = ref Float.infinity in
let all_int = ref true in
let na_seen = ref false in
let had_error = ref None in
for i = 0 to n - 1 do
if !had_error = None then
if !na_seen then
result.(i) <- VNA NAFloat
else
match arr.(i) with
| VInt x ->
let fx = float_of_int x in
running := Float.min !running fx;
result.(i) <- if !all_int then VInt (int_of_float !running) else VFloat !running
| VFloat f ->
all_int := false;
running := Float.min !running f;
for j = 0 to i - 1 do
match result.(j) with
| VInt v -> result.(j) <- VFloat (float_of_int v)
| _ -> ()
done;
result.(i) <- VFloat !running
| VNA _ -> na_seen := true; result.(i) <- VNA NAFloat
| _ -> had_error := Some (Error.type_error "Function `cummin` requires numeric values.")
done;
(match !had_error with Some e -> e | None -> VVector result))
| _ -> Error.arity_error_named "cummin" 1 (List.length args)
))
env
in
(*
--# Cumulative Maximum
--#
--# Calculates the cumulative maximum of a vector.
--#
--# @name cummax
--# @param x :: Vector The input numeric vector.
--# @return :: Vector The cumulative maximum.
--# @example
--# cummax([1, 3, 2, 4])
--# -- Returns = [1, 3, 3, 4]
--# @family colcraft
--# @seealso max, cummin, cumsum
--# @export
*)
(* cummax(x): cumulative maximum; NA propagates to all subsequent values *)
let env = Env.add "cummax"
(make_builtin ~name:"cummax" 1 (fun args _env ->
match args with
| [arg] ->
(match to_value_array "cummax" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NAFloat) in
let running = ref Float.neg_infinity in
let all_int = ref true in
let na_seen = ref false in
let had_error = ref None in
for i = 0 to n - 1 do
if !had_error = None then
if !na_seen then
result.(i) <- VNA NAFloat
else
match arr.(i) with
| VInt x ->
let fx = float_of_int x in
running := Float.max !running fx;
result.(i) <- if !all_int then VInt (int_of_float !running) else VFloat !running
| VFloat f ->
all_int := false;
running := Float.max !running f;
for j = 0 to i - 1 do
match result.(j) with
| VInt v -> result.(j) <- VFloat (float_of_int v)
| _ -> ()
done;
result.(i) <- VFloat !running
| VNA _ -> na_seen := true; result.(i) <- VNA NAFloat
| _ -> had_error := Some (Error.type_error "Function `cummax` requires numeric values.")
done;
(match !had_error with Some e -> e | None -> VVector result))
| _ -> Error.arity_error_named "cummax" 1 (List.length args)
))
env
in
(*
--# Cumulative Mean
--#
--# Calculates the cumulative mean of a vector.
--#
--# @name cummean
--# @param x :: Vector The input numeric vector.
--# @param na_rm :: Bool = false Remove NA values before computation.
--# @return :: Vector The cumulative mean.
--# @example
--# cummean([1, 2, 3])
--# -- Returns = [1.0, 1.5, 2.0]
--#
--# cummean([1, NA, 3], na_rm = true)
--# -- Returns = [1.0, 1.0, 2.0]
--# @family colcraft
--# @seealso mean, cumsum
--# @export
*)
(* cummean(x, na_rm=false): cumulative mean *)
let env = Env.add "cummean"
(make_builtin_named ~name:"cummean" ~variadic:true 1 (fun named_args _env ->
let na_rm = List.exists (fun (name, v) ->
name = Some "na_rm" && (match v with VBool true -> true | _ -> false)
) named_args in
let args = List.filter (fun (name, _) -> name <> Some "na_rm") named_args |> List.map snd in
match args with
| [arg] ->
(match to_value_array "cummean" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NAFloat) in
let running_sum = ref 0.0 in
let running_count = ref 0 in
let na_seen = ref false in
let had_error = ref None in
for i = 0 to n - 1 do
if !had_error = None then
if not na_rm && !na_seen then
result.(i) <- VNA NAFloat
else
match arr.(i) with
| VInt x ->
running_sum := !running_sum +. float_of_int x;
incr running_count;
result.(i) <- VFloat (!running_sum /. float_of_int !running_count)
| VFloat f ->
running_sum := !running_sum +. f;
incr running_count;
result.(i) <- VFloat (!running_sum /. float_of_int !running_count)
| VNA _ ->
if na_rm then
result.(i) <- if !running_count = 0 then VNA NAFloat else VFloat (!running_sum /. float_of_int !running_count)
else (
na_seen := true;
result.(i) <- VNA NAFloat
)
| _ -> had_error := Some (Error.type_error "Function `cummean` requires numeric values.")
done;
(match !had_error with Some e -> e | None -> VVector result))
| _ -> Error.arity_error_named "cummean" 1 (List.length args)
))
env
in
(*
--# Cumulative All
--#
--# Calculates the cumulative logical AND of a vector.
--#
--# @name cumall
--# @param x :: Vector The input boolean vector.
--# @return :: Vector The cumulative AND.
--# @example
--# cumall([true, true, false, true])
--# -- Returns = [true, true, false, false]
--# @family colcraft
--# @seealso cumany
--# @export
*)
(* cumall(x): cumulative logical AND; NA propagates to all subsequent values *)
let env = Env.add "cumall"
(make_builtin ~name:"cumall" 1 (fun args _env ->
match args with
| [arg] ->
(match to_value_array "cumall" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NABool) in
let running = ref true in
let na_seen = ref false in
for i = 0 to n - 1 do
if !na_seen then
result.(i) <- VNA NABool
else
match arr.(i) with
| VNA _ -> na_seen := true; result.(i) <- VNA NABool
| v ->
running := !running && Utils.is_truthy v;
result.(i) <- VBool !running
done;
VVector result)
| _ -> Error.arity_error_named "cumall" 1 (List.length args)
))
env
in
(*
--# Cumulative Any
--#
--# Calculates the cumulative logical OR of a vector.
--#
--# @name cumany
--# @param x :: Vector The input boolean vector.
--# @return :: Vector The cumulative OR.
--# @example
--# cumany([false, false, true, false])
--# -- Returns = [false, false, true, true]
--# @family colcraft
--# @seealso cumall
--# @export
*)
(* cumany(x): cumulative logical OR; NA propagates to all subsequent values *)
let env = Env.add "cumany"
(make_builtin ~name:"cumany" 1 (fun args _env ->
match args with
| [arg] ->
(match to_value_array "cumany" arg with
| Error e -> e
| Ok arr ->
let n = Array.length arr in
if n = 0 then VVector [||]
else
let result = Array.make n (VNA NABool) in
let running = ref false in
let na_seen = ref false in
for i = 0 to n - 1 do
if !na_seen then
result.(i) <- VNA NABool
else
match arr.(i) with
| VNA _ -> na_seen := true; result.(i) <- VNA NABool
| v ->
running := !running || Utils.is_truthy v;
result.(i) <- VBool !running
done;
VVector result)
| _ -> Error.arity_error_named "cumany" 1 (List.length args)
))
env
in
env