package lacaml

  1. Overview
  2. Docs

Source file vec2_S.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
(* File: vec_SD.ml

   Copyright © 2001-

   Markus Mottl <markus.mottl@gmail.com>

   Christophe Troestler <Christophe.Troestler@umons.ac.be>

   This library is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the Free
   Software Foundation; either version 2.1 of the License, or (at your option)
   any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public License
   along with this library; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *)

open Vec4_S
open Utils
open Float32

let random ?rnd_state ?(from = -1.) ?(range = 2.) n =
  let vec = create n in
  let state =
    match rnd_state with None -> Random.get_state () | Some state -> state
  in
  for row = 1 to n do
    vec.{row} <- Random.State.float state range +. from
  done;
  if rnd_state = None then Random.set_state state;
  vec

let get_y_vec ~loc ~ofsy ~incy ~n y = get_vec loc y_str y ofsy incy n create
let get_z_vec ~loc ~ofsz ~incz ~n z = get_vec loc z_str z ofsz incz n create

(* Unary vector operations *)

let int_abs = abs

let unop direct loc =
  let loc = "Lacaml.S.Vec." ^ loc in
  fun ?n ?ofsy ?incy ?y ?ofsx ?incx x ->
    let ofsx, incx = get_vec_geom loc x_str ofsx incx in
    let ofsy, incy = get_vec_geom loc y_str ofsy incy in
    let n = get_dim_vec loc x_str ofsx incx x n_str n in
    let y = get_y_vec ~loc ~ofsy ~incy ~n y in
    direct ~n ~ofsy ~incy ~y ~ofsx ~incx ~x;
    y

external direct_abs :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sabs_stub_bc" "lacaml_Sabs_stub"

let abs = unop direct_abs "abs"

external direct_signum :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssignum_stub_bc" "lacaml_Ssignum_stub"

let signum = unop direct_signum "signum"

external direct_sqr :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssqr_stub_bc" "lacaml_Ssqr_stub"

let sqr = unop direct_sqr "sqr"

external direct_sqrt :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssqrt_stub_bc" "lacaml_Ssqrt_stub"

let sqrt = unop direct_sqrt "sqrt"

external direct_cbrt :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Scbrt_stub_bc" "lacaml_Scbrt_stub"

let cbrt = unop direct_cbrt "cbrt"

external direct_exp :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sexp_stub_bc" "lacaml_Sexp_stub"

let exp = unop direct_exp "exp"

external direct_exp2 :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sexp2_stub_bc" "lacaml_Sexp2_stub"

let exp2 = unop direct_exp2 "exp2"

external direct_expm1 :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sexpm1_stub_bc" "lacaml_Sexpm1_stub"

let expm1 = unop direct_expm1 "expm1"

external direct_log :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Slog_stub_bc" "lacaml_Slog_stub"

let log = unop direct_log "log"

external direct_log10 :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Slog10_stub_bc" "lacaml_Slog10_stub"

let log10 = unop direct_log10 "log10"

external direct_log2 :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Slog2_stub_bc" "lacaml_Slog2_stub"

let log2 = unop direct_log2 "log2"

external direct_log1p :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Slog1p_stub_bc" "lacaml_Slog1p_stub"

let log1p = unop direct_log1p "log1p"

external direct_sin :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssin_stub_bc" "lacaml_Ssin_stub"

let sin = unop direct_sin "sin"

external direct_cos :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Scos_stub_bc" "lacaml_Scos_stub"

let cos = unop direct_cos "cos"

external direct_tan :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Stan_stub_bc" "lacaml_Stan_stub"

let tan = unop direct_tan "tan"

external direct_asin :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sasin_stub_bc" "lacaml_Sasin_stub"

let asin = unop direct_asin "asin"

external direct_acos :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sacos_stub_bc" "lacaml_Sacos_stub"

let acos = unop direct_acos "acos"

external direct_atan :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Satan_stub_bc" "lacaml_Satan_stub"

let atan = unop direct_atan "atan"

external direct_sinh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssinh_stub_bc" "lacaml_Ssinh_stub"

let sinh = unop direct_sinh "sinh"

external direct_cosh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Scosh_stub_bc" "lacaml_Scosh_stub"

let cosh = unop direct_cosh "cosh"

external direct_tanh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Stanh_stub_bc" "lacaml_Stanh_stub"

let tanh = unop direct_tanh "tanh"

external direct_asinh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sasinh_stub_bc" "lacaml_Sasinh_stub"

let asinh = unop direct_asinh "asinh"

external direct_acosh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sacosh_stub_bc" "lacaml_Sacosh_stub"

let acosh = unop direct_acosh "acosh"

external direct_atanh :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Satanh_stub_bc" "lacaml_Satanh_stub"

let atanh = unop direct_atanh "atanh"

external direct_floor :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sfloor_stub_bc" "lacaml_Sfloor_stub"

let floor = unop direct_floor "floor"

external direct_ceil :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sceil_stub_bc" "lacaml_Sceil_stub"

let ceil = unop direct_ceil "ceil"

external direct_round :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Sround_stub_bc" "lacaml_Sround_stub"

let round = unop direct_round "round"

external direct_trunc :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Strunc_stub_bc" "lacaml_Strunc_stub"

let trunc = unop direct_trunc "trunc"

external direct_erf :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Serf_stub_bc" "lacaml_Serf_stub"

let erf = unop direct_erf "erf"

external direct_erfc :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Serfc_stub_bc" "lacaml_Serfc_stub"

let erfc = unop direct_erfc "erfc"

external direct_logistic :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Slogistic_stub_bc" "lacaml_Slogistic_stub"

let logistic = unop direct_logistic "logistic"

external direct_relu :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Srelu_stub_bc" "lacaml_Srelu_stub"

let relu = unop direct_relu "relu"

external direct_softplus :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssoftplus_stub_bc" "lacaml_Ssoftplus_stub"

let softplus = unop direct_softplus "softplus"

external direct_softsign :
  n:(int[@untagged]) ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  unit = "lacaml_Ssoftsign_stub_bc" "lacaml_Ssoftsign_stub"

let softsign = unop direct_softsign "softsign"

(* Binary vector operations *)

let binop direct loc =
  let loc = "Lacaml.S.Vec." ^ loc in
  fun ?n ?ofsz ?incz ?z ?ofsx ?incx x ?ofsy ?incy y ->
    let ofsz, incz = get_vec_geom loc z_str ofsz incz in
    let ofsx, incx = get_vec_geom loc x_str ofsx incx in
    let ofsy, incy = get_vec_geom loc y_str ofsy incy in
    let n = get_dim_vec loc x_str ofsx incx x n_str n in
    check_vec loc y_str y (ofsy + ((n - 1) * int_abs incy));
    let z = get_z_vec ~loc ~ofsz ~incz ~n z in
    direct ~n ~ofsz ~incz ~z ~ofsx ~incx ~x ~ofsy ~incy ~y;
    z

external direct_pow :
  n:(int[@untagged]) ->
  ofsz:(int[@untagged]) ->
  incz:(int[@untagged]) ->
  z:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  unit = "lacaml_Spow_stub_bc" "lacaml_Spow_stub"

let pow = binop direct_pow "pow"

external direct_atan2 :
  n:(int[@untagged]) ->
  ofsz:(int[@untagged]) ->
  incz:(int[@untagged]) ->
  z:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  unit = "lacaml_Satan2_stub_bc" "lacaml_Satan2_stub"

let atan2 = binop direct_atan2 "atan2"

external direct_hypot :
  n:(int[@untagged]) ->
  ofsz:(int[@untagged]) ->
  incz:(int[@untagged]) ->
  z:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  unit = "lacaml_Shypot_stub_bc" "lacaml_Shypot_stub"

let hypot = binop direct_hypot "hypot"

external direct_min2 :
  n:(int[@untagged]) ->
  ofsz:(int[@untagged]) ->
  incz:(int[@untagged]) ->
  z:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  unit = "lacaml_Smin2_stub_bc" "lacaml_Smin2_stub"

let min2 = binop direct_min2 "min2"

external direct_max2 :
  n:(int[@untagged]) ->
  ofsz:(int[@untagged]) ->
  incz:(int[@untagged]) ->
  z:vec ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  ofsy:(int[@untagged]) ->
  incy:(int[@untagged]) ->
  y:vec ->
  unit = "lacaml_Smax2_stub_bc" "lacaml_Smax2_stub"

let max2 = binop direct_max2 "max2"

(* Misc functions *)

external direct_log_sum_exp :
  n:(int[@untagged]) ->
  ofsx:(int[@untagged]) ->
  incx:(int[@untagged]) ->
  x:vec ->
  (float[@unboxed])
  = "lacaml_Slog_sum_exp_vec_stub_bc" "lacaml_Slog_sum_exp_vec_stub"

let log_sum_exp =
  let loc = "Lacaml.D.Vec.log_sum_exp" in
  fun ?n ?ofsx ?incx x ->
    let ofsx, incx = get_vec_geom loc x_str ofsx incx in
    let n = get_dim_vec loc x_str ofsx incx x n_str n in
    direct_log_sum_exp ~n ~ofsx ~incx ~x
OCaml

Innovation. Community. Security.