package lacaml

  1. Overview
  2. Docs

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

   Copyright (C) 2001-

     Markus Mottl
     email: markus.mottl@gmail.com
     WWW: http://www.ocaml.info

     Christophe Troestler
     email: Christophe.Troestler@umons.ac.be
     WWW: http://math.umh.ac.be/an/

   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 Bigarray
open Mat4_S
open Utils
open Float32

let hilbert n =
  let mat = create n n in
  for col = 1 to n do
    for row = 1 to n do
      mat.{row, col} <- 1. /. float (row + col - 1)
    done
  done;
  mat

let hankel n =
  if n = 0 then empty
  else
    let mat = make n n 0. in
    let n1 = n + 1 in
    let rec loop c r =
      mat.{r, c} <- float (c + r - 1);
      if c + r >= n1 then
        if c = n then mat
        else loop (c + 1) 1
      else loop c (r + 1) in
    loop 1 1

let pascal n =
  let mat = make n n 1. in
  for r = 2 to n do
    for c = 2 to n do
      mat.{r, c} <- mat.{r - 1, c} +. mat.{r, c - 1}
    done
  done;
  mat

let rosser_ar =
  [|
    [|  611.;  196.; -192.;  407.;   -8.;  -52.;  -49.;   29.; |];
    [|  196.;  899.;  113.; -192.;  -71.;  -43.;   -8.;  -44.; |];
    [| -192.;  113.;  899.;  196.;   61.;   49.;    8.;   52.; |];
    [|  407.; -192.;  196.;  611.;    8.;   44.;   59.;  -23.; |];
    [|   -8.;  -71.;   61.;    8.;  411.; -599.;  208.;  208.; |];
    [|  -52.;  -43.;   49.;   44.; -599.;  411.;  208.;  208.; |];
    [|  -49.;   -8.;    8.;   59.;  208.;  208.;   99.; -911.; |];
    [|   29.;  -44.;   52.;  -23.;  208.;  208.; -911.;   99.; |];
  |]

let rosser () = Array2.of_array prec fortran_layout rosser_ar

let toeplitz (v : vec) =
  let len = Array1.dim v in
  if len = 0 then empty
  else if len = 1 then make 1 1 v.{1}
  else (
    if len mod 2 <> 1 then
      invalid_arg "toeplitz: v has even number of elements";
    let n = (len + 1) / 2 in
    let mat = create n n in
    let rec loop r c i =
      mat.{r, c} <- v.{i};
      if r = n then
        if c = n then loop 1 2 (i + 1)
        else loop (n - c) 1 (i + 1)
      else
        if c = n then
          if r = 1 then mat
          else loop 1 (n - r + 2) (i + 1)
        else loop (r + 1) (c + 1) i in
    loop n 1 1)

let vandermonde (v : vec) =
  let n = Array1.dim v in
  if n = 0 then empty
  else if n = 1 then make 1 1 1.0
  else (
    let mat = create n n in
    for i = 1 to n do mat.{i, 1} <- 1.0 done;
    for i = 1 to n do mat.{i, 2} <- v.{i} done;
    for pow = 2 to n - 1 do
      let fpow = float pow in
      for i = 1 to n do mat.{i, pow + 1} <- v.{i} ** fpow done
    done;
    mat)

let wilkinson n =
  if n < 3 then invalid_arg "wilkinson: n < 3";
  if n mod 2 <> 1 then invalid_arg "wilkinson: n is an even number";
  let mat = make n n 0.0 in
  for i = 2 to n do
    let i_1 = i - 1 in
    mat.{i, i_1} <- 1.0;
    mat.{i_1, i} <- 1.0
  done;
  let n_2 = n / 2 in
  let n1_2 = n_2 + 1 in
  for i = 1 to n_2 do mat.{i, i} <- float (n1_2 - i) done;
  for i = n1_2 + 1 to n do mat.{i, i} <- float (i - n1_2) done;
  mat

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


(* Unary matrix operations *)

let unop direct loc =
  let loc = "Lacaml.S.Mat." ^ loc in
  fun ?patt ?m ?n ?(br = 1) ?(bc = 1) ?b ?(ar = 1) ?(ac = 1) a ->
    let m = get_dim1_mat loc a_str a ar m_str m in
    let n = get_dim2_mat loc a_str a ac n_str n in
    let b = get_mat loc b_str create br bc b m n in
    let pkind, pinit = Mat_patt.normalize_args ~loc ~m ~n patt in
    direct ~pkind ~pinit ~m ~n ~ar ~ac ~a ~br ~bc ~b;
    b

external direct_abs :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sabs_mat_stub_bc" "lacaml_Sabs_mat_stub"

let abs = unop direct_abs "abs"

external direct_signum :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssignum_mat_stub_bc" "lacaml_Ssignum_mat_stub"

let signum = unop direct_signum "signum"

external direct_sqr :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssqr_mat_stub_bc" "lacaml_Ssqr_mat_stub"

let sqr = unop direct_sqr "sqr"

external direct_sqrt :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssqrt_mat_stub_bc" "lacaml_Ssqrt_mat_stub"

let sqrt = unop direct_sqrt "sqrt"

external direct_cbrt :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Scbrt_mat_stub_bc" "lacaml_Scbrt_mat_stub"

let cbrt = unop direct_cbrt "cbrt"

external direct_exp :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sexp_mat_stub_bc" "lacaml_Sexp_mat_stub"

let exp = unop direct_exp "exp"

external direct_exp2 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sexp2_mat_stub_bc" "lacaml_Sexp2_mat_stub"

let exp2 = unop direct_exp2 "exp2"

external direct_expm1 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sexpm1_mat_stub_bc" "lacaml_Sexpm1_mat_stub"

let expm1 = unop direct_expm1 "expm1"

external direct_log :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Slog_mat_stub_bc" "lacaml_Slog_mat_stub"

let log = unop direct_log "log"

external direct_log10 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Slog10_mat_stub_bc" "lacaml_Slog10_mat_stub"

let log10 = unop direct_log10 "log10"

external direct_log2 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Slog2_mat_stub_bc" "lacaml_Slog2_mat_stub"

let log2 = unop direct_log2 "log2"

external direct_log1p :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Slog1p_mat_stub_bc" "lacaml_Slog1p_mat_stub"

let log1p = unop direct_log1p "log1p"

external direct_sin :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssin_mat_stub_bc" "lacaml_Ssin_mat_stub"

let sin = unop direct_sin "sin"

external direct_cos :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Scos_mat_stub_bc" "lacaml_Scos_mat_stub"

let cos = unop direct_cos "cos"

external direct_tan :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Stan_mat_stub_bc" "lacaml_Stan_mat_stub"

let tan = unop direct_tan "tan"

external direct_asin :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sasin_mat_stub_bc" "lacaml_Sasin_mat_stub"

let asin = unop direct_asin "asin"

external direct_acos :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sacos_mat_stub_bc" "lacaml_Sacos_mat_stub"

let acos = unop direct_acos "acos"

external direct_atan :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Satan_mat_stub_bc" "lacaml_Satan_mat_stub"

let atan = unop direct_atan "atan"

external direct_sinh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssinh_mat_stub_bc" "lacaml_Ssinh_mat_stub"

let sinh = unop direct_sinh "sinh"

external direct_cosh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Scosh_mat_stub_bc" "lacaml_Scosh_mat_stub"

let cosh = unop direct_cosh "cosh"

external direct_tanh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Stanh_mat_stub_bc" "lacaml_Stanh_mat_stub"

let tanh = unop direct_tanh "tanh"

external direct_asinh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sasinh_mat_stub_bc" "lacaml_Sasinh_mat_stub"

let asinh = unop direct_asinh "asinh"

external direct_acosh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sacosh_mat_stub_bc" "lacaml_Sacosh_mat_stub"

let acosh = unop direct_acosh "acosh"

external direct_atanh :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Satanh_mat_stub_bc" "lacaml_Satanh_mat_stub"

let atanh = unop direct_atanh "atanh"

external direct_floor :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sfloor_mat_stub_bc" "lacaml_Sfloor_mat_stub"

let floor = unop direct_floor "floor"

external direct_ceil :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sceil_mat_stub_bc" "lacaml_Sceil_mat_stub"

let ceil = unop direct_ceil "ceil"

external direct_round :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Sround_mat_stub_bc" "lacaml_Sround_mat_stub"

let round = unop direct_round "round"

external direct_trunc :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Strunc_mat_stub_bc" "lacaml_Strunc_mat_stub"

let trunc = unop direct_trunc "trunc"

external direct_erf :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Serf_mat_stub_bc" "lacaml_Serf_mat_stub"

let erf = unop direct_erf "erf"

external direct_erfc :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Serfc_mat_stub_bc" "lacaml_Serfc_mat_stub"

let erfc = unop direct_erfc "erfc"

external direct_logistic :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Slogistic_mat_stub_bc" "lacaml_Slogistic_mat_stub"

let logistic = unop direct_logistic "logistic"

external direct_relu :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Srelu_mat_stub_bc" "lacaml_Srelu_mat_stub"

let relu = unop direct_relu "relu"

external direct_softplus :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssoftplus_mat_stub_bc" "lacaml_Ssoftplus_mat_stub"

let softplus = unop direct_softplus "softplus"

external direct_softsign :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  unit = "lacaml_Ssoftsign_mat_stub_bc" "lacaml_Ssoftsign_mat_stub"

let softsign = unop direct_softsign "softsign"


(* Binary matrix operations *)

let binop direct loc =
  let loc = "Lacaml.S.Mat." ^ loc in
  fun ?patt ?m ?n
    ?(cr = 1) ?(cc = 1) ?c ?(ar = 1) ?(ac = 1) a ?(br = 1) ?(bc = 1) b ->
    let m = get_dim1_mat loc a_str a ar m_str m in
    let n = get_dim2_mat loc a_str a ac n_str n in
    check_dim_mat loc b_str br bc b m n;
    let c = get_mat loc c_str create cr cc c m n in
    let pkind, pinit = Mat_patt.normalize_args ~loc ~m ~n patt in
    direct ~pkind ~pinit ~m ~n ~ar ~ac ~a ~br ~bc ~b ~cr ~cc ~c;
    c

external direct_pow :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Spow_mat_stub_bc" "lacaml_Spow_mat_stub"

let pow = binop direct_pow "pow"

external direct_atan2 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Satan2_mat_stub_bc" "lacaml_Satan2_mat_stub"

let atan2 = binop direct_atan2 "atan2"

external direct_hypot :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Shypot_mat_stub_bc" "lacaml_Shypot_mat_stub"

let hypot = binop direct_hypot "hypot"

external direct_min2 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Smin2_mat_stub_bc" "lacaml_Smin2_mat_stub"

let min2 = binop direct_min2 "min2"

external direct_max2 :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Smax2_mat_stub_bc" "lacaml_Smax2_mat_stub"

let max2 = binop direct_max2 "max2"

external direct_sum_prod :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  (float [@unboxed])
  = "lacaml_Ssum_prod_mat_stub_bc" "lacaml_Ssum_prod_mat_stub"

let sum_prod ?patt ?m ?n ?(ar = 1) ?(ac = 1) a ?(br = 1) ?(bc = 1) b =
  let loc = "Lacaml.S.Mat.sum_prod" in
  let m = get_dim1_mat loc a_str a ar m_str m in
  let n = get_dim2_mat loc a_str a ac n_str n in
  check_dim_mat loc b_str br bc b m n;
  let pkind, pinit = Mat_patt.normalize_args ~loc ~m ~n patt in
  direct_sum_prod ~pkind ~pinit ~m ~n ~ar ~ac ~a ~br ~bc ~b


(* Ternary matrix operations *)

let cqab direct loc =
  let loc = "Lacaml.S.Mat." ^ loc in
  fun ?patt ?m ?n
    ?(cr = 1) ?(cc = 1) c ?(ar = 1) ?(ac = 1) a ?(br = 1) ?(bc = 1) b ->
    let m = get_dim1_mat loc a_str a ar m_str m in
    let n = get_dim2_mat loc a_str a ac n_str n in
    check_dim_mat loc b_str br bc b m n;
    check_dim_mat loc c_str cr cc c m n;
    let pkind, pinit = Mat_patt.normalize_args ~loc ~m ~n patt in
    direct ~pkind ~pinit ~m ~n ~ar ~ac ~a ~br ~bc ~b ~cr ~cc ~c

external direct_cpab :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Scpab_stub_bc" "lacaml_Scpab_stub"

let cpab = cqab direct_cpab "cpab"

external direct_cmab :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  br : (int [@untagged]) ->
  bc : (int [@untagged]) ->
  b : mat ->
  cr : (int [@untagged]) ->
  cc : (int [@untagged]) ->
  c : mat ->
  unit = "lacaml_Scmab_stub_bc" "lacaml_Scmab_stub"

let cmab = cqab direct_cmab "cmab"


(* Misc functions *)

external direct_log_sum_exp :
  pkind : Mat_patt.kind ->
  pinit : (int [@untagged]) ->
  m : (int [@untagged]) ->
  n : (int [@untagged]) ->
  ar : (int [@untagged]) ->
  ac : (int [@untagged]) ->
  a : mat ->
  (float [@unboxed])
  = "lacaml_Slog_sum_exp_mat_stub_bc" "lacaml_Slog_sum_exp_mat_stub"

let log_sum_exp ?patt ?m ?n ?(ar = 1) ?(ac = 1) a =
  let loc = "Lacaml.S.Mat.log_sum_exp" in
  let m = get_dim1_mat loc a_str a ar m_str m in
  let n = get_dim2_mat loc a_str a ac n_str n in
  let pkind, pinit = Mat_patt.normalize_args ~loc ~m ~n patt in
  direct_log_sum_exp ~pkind ~pinit ~m ~n ~ar ~ac ~a
OCaml

Innovation. Community. Security.