Source file utils.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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
(** General auxiliary functions *)
open Printf
open Bigarray
open Common
let empty_int32_vec = create_int32_vec 0
let get_norm_char = function `M -> 'M' | `O -> 'O' | `I -> 'I' | `F -> 'F'
let get_uplo_char up = if up then 'U' else 'L'
let get_trans_char = function `N -> 'N' | `T -> 'T' | `C -> 'C'
let get_side_char = function `L -> 'L' | `R -> 'R'
let get_diag_char = function `U -> 'U' | `N -> 'N'
let get_s_d_job_char = function `A -> 'A' | `S -> 'S' | `O -> 'O' | `N -> 'N'
let get_job_char = function true -> 'V' | _ -> 'N'
let job_char_true = get_job_char true
let job_char_false = get_job_char false
(** Preallocated strings (names) *)
let a_str = "a"
let ab_str = "ab"
let alphas_str = "alphas"
let ap_str = "ap"
let b_str = "b"
let br_str = "br"
let bc_str = "bc"
let c_str = "c"
let cr_str = "cr"
let cc_str = "cc"
let d_str = "d"
let dl_str = "dl"
let du_str = "du"
let e_str = "e"
let ipiv_str = "ipiv"
let iseed_str = "iseed"
let k_str = "k"
let ka_str = "ka"
let kb_str = "kb"
let work_str = "work"
let lwork_str = "lwork"
let liwork_str = "liwork"
let k1_str = "k1"
let k2_str = "k2"
let kd_str = "kd"
let kl_str = "kl"
let ku_str = "ku"
let m_str = "m"
let n_str = "n"
let nrhs_str = "nrhs"
let ofs_str = "ofs"
let r_str = "r"
let s_str = "s"
let tau_str = "tau"
let u_str = "u"
let um_str = "um"
let un_str = "un"
let vm_str = "vm"
let vn_str = "vn"
let vs_str = "vs"
let vsr_str = "vsr"
let vsc_str = "vsc"
let vt_str = "vt"
let w_str = "w"
let wi_str = "wi"
let wr_str = "wr"
let x_str = "x"
let y_str = "y"
let z_str = "z"
(** Range checking *)
(** [raise_var_lt0 ~loc ~name var] @raise Invalid_argument to indicate
that integer variable [var] with name [name] at location [loc] is lower
than [0]. *)
let raise_var_lt0 ~loc ~name var =
invalid_arg (sprintf "%s: %s < 0: %d" loc name var)
(** [check_var_lt0 ~loc ~name var] checks whether integer variable [var] with
name [name] at location [loc] is lower than [0]. @raise Invalid_argument
in that case. *)
let check_var_lt0 ~loc ~name var = if var < 0 then raise_var_lt0 ~loc ~name var
let check_var_within loc var_name var lb ub c =
if var < lb then
invalid_arg (sprintf "%s: %s %s < %s" loc var_name (c var) (c lb))
else if var > ub then
invalid_arg (sprintf "%s: %s %s > %s" loc var_name (c var) (c ub))
else ()
(** Valueless vector checking and allocation functions (do not require a
vector value as argument *)
(** [calc_vec_min_dim ~n ~ofs ~inc] @return minimum vector dimension given
offset [ofs], increment [inc], and operation size [n] for a vector. *)
let calc_vec_min_dim ~n ~ofs ~inc =
if n = 0 then ofs - 1 else ofs + (n - 1) * abs inc
(** [raise_vec_min_dim ~loc ~vec_name ~dim ~min_dim] @raise Invalid_argument
to indicate that dimension [dim] of a vector with name [vec_name]
exceeds the minimum [min_dim] at location [loc]. *)
let raise_vec_min_dim ~loc ~vec_name ~dim ~min_dim =
invalid_arg (
sprintf "%s: dim(%s): valid=[%d..[ got=%d" loc vec_name min_dim dim)
(** [check_vec_min_dim ~loc ~vec_name ~dim ~min_dim] checks whether vector
with name [vec_name] and dimension [dim] satisfies minimum dimension
[min_dim]. @raise Invalid_argument otherwise. *)
let check_vec_min_dim ~loc ~vec_name ~dim ~min_dim =
if dim < min_dim then raise_vec_min_dim ~loc ~vec_name ~dim ~min_dim
(** [raise_vec_bad_ofs ~loc ~vec_name ~ofs ~max_ofs] @raise Invalid_argument
to indicate that vector offset [ofs] is invalid (i.e. is outside of
[1..max_ofs]). *)
let raise_vec_bad_ofs ~loc ~vec_name ~ofs ~max_ofs =
invalid_arg (
sprintf "%s: ofs%s: valid=[1..%d] got=%d" loc vec_name max_ofs ofs)
(** [bad_n ~n ~max_n] @return [true] iff [n] is smaller than zero or larger
than [max_n]. *)
let bad_n ~n ~max_n = n < 0 || n > max_n
(** [bad_ofs ~ofs ~max_ofs] @return [true] iff [ofs] is smaller than one or
exceeds [max_ofs]. *)
let bad_ofs ~ofs ~max_ofs = ofs < 1 || ofs > max_ofs
(** [bad_inc inc] @return [true] iff [inc] is illegal. *)
let bad_inc inc = inc = 0
(** [check_vec_ofs ~loc ~vec_name ~ofs ~max_ofs] checks whether vector
offset [ofs] for vector of name [vec_name] is invalid (i.e. outside of
[1..max_ofs]). @raise Invalid_argument in that case. *)
let check_vec_ofs ~loc ~vec_name ~ofs ~max_ofs =
if bad_ofs ~ofs ~max_ofs then raise_vec_bad_ofs ~loc ~vec_name ~ofs ~max_ofs
(** [check_vec_inc ~loc ~vec_name inc] checks whether vector increment [inc]
for vector of name [vec_name] is invalid (i.e. [0]). @raise
Invalid_argument in that case. *)
let check_vec_inc ~loc ~vec_name inc =
if bad_inc inc then invalid_arg (sprintf "%s: inc%s = 0" loc vec_name)
(** [calc_vec_max_n ~dim ~ofs ~inc] @return maximum operation length [n]
for a vector given the dimension [dim] of the vector, the offset [ofs],
and increment [inc]. Assumes that the offset has already been validated
to not exceed [dim], i.e. the returned [max_n] is at least [1]. *)
let calc_vec_max_n ~dim ~ofs ~inc = 1 + (dim - ofs) / abs inc
(** [calc_vec_opt_max_n ?ofs ?inc dim] @return maximum operation length [n]
for a vector given the dimension [dim] of the vector, the optional offset
[ofs], and optional increment [inc]. Assumes that the offset has already
been validated to not exceed [dim], i.e. the returned [max_n] is at least
[1]. *)
let calc_vec_opt_max_n ?(ofs = 1) ?(inc = 1) dim = calc_vec_max_n ~dim ~ofs ~inc
(** [raise_max_len ~loc ~len_name ~len ~max_len] @raise Invalid_argument
that the maximum operation size (e.g. [m] or [n] for vectors and matrices)
has been exceeded. *)
let raise_max_len ~loc ~len_name ~len ~max_len =
invalid_arg (sprintf "%s: %s: valid=[0..%d] got=%d" loc len_name max_len len)
(** [check_vec_dim ~loc ~vec_name ~dim ~ofs ~inc ~n_name ~n] checks the vector
operation length in parameter [n] with name [n_name] at location [loc]
for vector with name [vec_name] and dimension [dim] given the operation
offset [ofs] and increment [inc]. @raise Invalid_argument if any
arguments are invalid. *)
let check_vec_dim ~loc ~vec_name ~dim ~ofs ~inc ~n_name ~n =
check_vec_inc ~loc ~vec_name inc;
check_var_lt0 ~loc ~name:n_name n;
if n = 0 then check_vec_ofs ~loc ~vec_name ~ofs ~max_ofs:(dim + 1)
else begin
check_vec_ofs ~loc ~vec_name ~ofs ~max_ofs:dim;
let max_n = calc_vec_max_n ~dim ~ofs ~inc in
if n > max_n then raise_max_len ~loc ~len_name:n_name ~len:n ~max_len:max_n
end
(** [get_vec_n ~loc ~vec_name ~dim ~ofs ~inc ~n_name n] checks or infers
the vector operation length in the option parameter [n] with name [n_name]
at location [loc] for vector with name [vec_name] and dimension [dim] given
the operation offset [ofs] and increment [inc]. @raise Invalid_argument
if any arguments are invalid. *)
let get_vec_n ~loc ~vec_name ~dim ~ofs ~inc ~n_name = function
| None when dim = 0 ->
check_vec_inc ~loc ~vec_name inc;
if ofs = 1 then dim else raise_vec_bad_ofs ~loc ~vec_name ~ofs ~max_ofs:1
| None ->
check_vec_inc ~loc ~vec_name inc;
if ofs = dim + 1 then 0
else begin
check_vec_ofs ~loc ~vec_name ~ofs ~max_ofs:dim;
calc_vec_max_n ~dim ~ofs ~inc
end
| Some n -> check_vec_dim ~loc ~vec_name ~dim ~ofs ~inc ~n_name ~n; n
(** [get_vec_min_dim ~loc ~vec_name ~ofs ~inc ~n] @return minimum vector
dimension given offset [ofs], increment [inc], and operation size [n]
for a vector named [vec_name] at location [loc]. @raise Invalid_argument
if any of the parameters are illegal. *)
let get_vec_min_dim ~loc ~vec_name ~ofs ~inc ~n =
check_vec_inc ~loc ~vec_name inc;
if ofs >= 1 then calc_vec_min_dim ~ofs ~inc ~n
else invalid_arg (sprintf "%s: ofs%s: valid=[1..] got=%d" loc vec_name ofs)
(** [get_vec_start_stop ~ofsx ~incx ~n] @return [(start, stop)] where [start]
and [stop] reflect the start and stop of an iteration respectively. *)
let get_vec_start_stop ~ofsx ~incx ~n =
if n = 0 then 0, 0
else
if incx > 0 then ofsx, ofsx + n * incx
else ofsx - (n - 1) * incx, ofsx + incx
(** Valueless matrix checking and allocation functions (do not require a
matrix value as argument *)
(** [raise_bad_mat_ofs ~loc ~name ~ofs_name ~ofs ~max_ofs] @raise
Invalid_argument to indicate that a matrix offset [ofs] named [ofs_name]
for a matrix having [name] is invalid (i.e. is outside of [1..max_ofs]). *)
let raise_bad_mat_ofs ~loc ~name ~ofs_name ~ofs ~max_ofs =
invalid_arg (
sprintf "%s: %s%s: valid=[1..%d] got=%d" loc name ofs_name max_ofs ofs)
(** [raise_mat_bad_r ~loc ~mat_name ~r ~max_r] @raise Invalid_argument
to indicate that matrix row offset [r] is invalid (i.e. is outside of
[1..max_r]). *)
let raise_mat_bad_r ~loc ~mat_name ~r ~max_r =
raise_bad_mat_ofs ~loc ~name:mat_name ~ofs_name:r_str ~ofs:r ~max_ofs:max_r
(** [raise_mat_bad_c ~loc ~mat_name ~c ~max_c] @raise Invalid_argument
to indicate that matrix column offset [c] is invalid (i.e. is outside of
[1..max_c]). *)
let raise_mat_bad_c ~loc ~mat_name ~c ~max_c =
raise_bad_mat_ofs ~loc ~name:mat_name ~ofs_name:c_str ~ofs:c ~max_ofs:max_c
(** [check_mat_r ~loc ~vec_name ~r ~max_r] checks whether matrix row
offset [r] for vector of name [vec_name] is invalid (i.e. outside of
[1..max_r]). @raise Invalid_argument in that case. *)
let check_mat_r ~loc ~mat_name ~r ~max_r =
if r < 1 || r > max_r then raise_mat_bad_r ~loc ~mat_name ~r ~max_r
(** [check_mat_c ~loc ~vec_name ~c ~max_c] checks whether matrix column
offset [c] for vector of name [vec_name] is invalid (i.e. outside of
[1..max_c]). @raise Invalid_argument in that case. *)
let check_mat_c ~loc ~mat_name ~c ~max_c =
if c < 1 || c > max_c then raise_mat_bad_c ~loc ~mat_name ~c ~max_c
(** [calc_mat_max_rows ~dim1 ~r] @return maximum row operation length [m] for a
matrix given the dimension [dim1] of the matrix and the start row [r]. *)
let calc_mat_max_rows ~dim1 ~r = dim1 - r + 1
(** [calc_mat_opt_max_rows ?r dim1] @return maximum row operation length
[m] for a matrix given the dimension [dim1] of the matrix and the optional
start row [r]. Assumes that the offset has already been validated to
not exceed [dim1], i.e. the returned [max_m] is at least [1]. *)
let calc_mat_opt_max_rows ?(r = 1) dim1 = calc_mat_max_rows ~dim1 ~r
(** [calc_mat_max_cols ~dim2 ~c] @return maximum column operation length
[n] for a matrix given the dimension [dim1] of the matrix and the start
column [c]. *)
let calc_mat_max_cols ~dim2 ~c = dim2 - c + 1
(** [calc_mat_opt_max_cols ?c dim1] @return maximum column operation length
[m] for a matrix given the dimension [dim2] of the matrix and the optional
start column [c]. Assumes that the offset has already been validated to
not exceed [dim2], i.e. the returned [max_n] is at least [1]. *)
let calc_mat_opt_max_cols ?(c = 1) dim2 = calc_mat_max_cols ~dim2 ~c
(** [check_mat_rows ~loc ~mat_name ~dim1 ~r ~p ~param_name] checks the matrix
row operation length in parameter [p] with name [param_name] at
location [loc] for matrix with name [mat_name] and dimension [dim1]
given the operation row [r]. @raise Invalid_argument if any arguments
are invalid. *)
let check_mat_rows ~loc ~mat_name ~dim1 ~r ~p ~param_name =
check_var_lt0 ~loc ~name:param_name p;
if p = 0 then check_mat_r ~loc ~mat_name ~r ~max_r:(dim1 + 1)
else begin
check_mat_r ~loc ~mat_name ~r ~max_r:dim1;
let max_rows = calc_mat_max_rows ~dim1 ~r in
if p > max_rows then
raise_max_len ~loc ~len_name:param_name ~len:p ~max_len:max_rows
end
(** [check_mat_m ~loc ~mat_name ~dim1 ~r ~m] checks the matrix row operation
length in parameter [m] at location [loc] for matrix with name [mat_name]
and dimension [dim1] given the operation row [r]. @raise Invalid_argument
if any arguments are invalid. *)
let check_mat_m ~loc ~mat_name ~dim1 ~r ~m =
check_mat_rows ~loc ~mat_name ~dim1 ~r ~p:m ~param_name:m_str
(** [check_mat_cols ~loc ~mat_name ~dim2 ~c ~p ~param_name] checks the
matrix column operation length in parameter [p] with name [param_name]
at location [loc] for matrix with name [mat_name] and dimension [dim2]
given the operation column [c]. @raise Invalid_argument if any arguments
are invalid. *)
let check_mat_cols ~loc ~mat_name ~dim2 ~c ~p ~param_name =
check_var_lt0 ~loc ~name:param_name p;
if p = 0 then check_mat_c ~loc ~mat_name ~c ~max_c:(dim2 + 1)
else begin
check_mat_c ~loc ~mat_name ~c ~max_c:dim2;
let max_cols = calc_mat_max_cols ~dim2 ~c in
if p > max_cols then
raise_max_len ~loc ~len_name:param_name ~len:p ~max_len:max_cols
end
(** [check_mat_n ~loc ~mat_name ~dim2 ~c ~n] checks the matrix column
operation length in parameter [n] at location [loc] for matrix with
name [mat_name] and dimension [dim2] given the operation column [c].
@raise Invalid_argument if any arguments are invalid. *)
let check_mat_n ~loc ~mat_name ~dim2 ~c ~n =
check_mat_cols ~loc ~mat_name ~dim2 ~c ~p:n ~param_name:n_str
(** [check_mat_mn ~loc ~mat_name ~dim1 ~dim2 ~r ~c ~m ~n] checks the matrix
operation lengths in parameters [m] and [n] at location [loc] for matrix
with name [mat_name] and dimensions [dim1] and [dim2] given the operation
row [r] and column [c]. @raise Invalid_argument if any arguments are
invalid. *)
let check_mat_mn ~loc ~mat_name ~dim1 ~dim2 ~r ~c ~m ~n =
check_mat_m ~loc ~mat_name ~dim1 ~r ~m;
check_mat_n ~loc ~mat_name ~dim2 ~c ~n
(** [get_mat_rows ~loc ~mat_name ~dim1 ~r p ~param_name] checks or infers
the matrix row operation length in the option parameter [p] with
name [param_name] at location [loc] for matrix with name [mat_name]
and dimension [dim1] given the row operation offset [r]. @raise
Invalid_argument if any arguments are invalid. *)
let get_mat_rows ~loc ~mat_name ~dim1 ~r ~p ~param_name =
match p with
| None when dim1 = 0 ->
if r = 1 then dim1 else raise_mat_bad_r ~loc ~mat_name ~r ~max_r:1
| None ->
let max_r = dim1 + 1 in
check_mat_r ~loc ~mat_name ~r ~max_r;
max_r - r
| Some p -> check_mat_rows ~loc ~mat_name ~dim1 ~r ~p ~param_name; p
(** [get_mat_dim1 ~loc ~mat_name ~dim1 ~r ~m ~m_name] checks or infers the
matrix row operation length in the option parameter [m] with name [m_name]
at location [loc] for matrix with name [mat_name] and dimension [dim1]
given the row operation offset [r]. @raise Invalid_argument if any
arguments are invalid. *)
let get_mat_dim1 ~loc ~mat_name ~dim1 ~r ~m ~m_name =
get_mat_rows ~loc ~mat_name ~dim1 ~r ~p:m ~param_name:m_name
(** [get_mat_m ~loc ~mat_name ~dim1 ~r ~m] checks or infers the matrix row
operation length in the option parameter [m] at location [loc] for matrix
with name [mat_name] and dimension [dim1] given the row operation offset
[r]. @raise Invalid_argument if any arguments are invalid. *)
let get_mat_m ~loc ~mat_name ~dim1 ~r ~m =
get_mat_dim1 ~loc ~mat_name ~dim1 ~r ~m_name:m_str ~m
(** [get_mat_cols ~loc ~mat_name ~dim2 ~c ~param_name p] checks or infers
the matrix column operation length in the option parameter [p] with
name [param_name] at location [loc] for matrix with name [mat_name]
and dimension [dim2] given the column operation offset [c]. @raise
Invalid_argument if any arguments are invalid. *)
let get_mat_cols ~loc ~mat_name ~dim2 ~c ~p ~param_name =
match p with
| None when dim2 = 0 ->
if c = 1 then dim2 else raise_mat_bad_c ~loc ~mat_name ~c ~max_c:1
| None ->
let max_c = dim2 + 1 in
check_mat_c ~loc ~mat_name ~c ~max_c;
max_c - c
| Some p -> check_mat_cols ~loc ~mat_name ~dim2 ~c ~p ~param_name; p
(** [get_mat_dim2 ~loc ~mat_name ~dim2 ~c ~n ~n_name] checks or infers the
matrix column operation length in the option parameter [n] with name
[n_name] at location [loc] for matrix with name [mat_name] and dimension
[dim2] given the column operation offset [c]. @raise Invalid_argument
if any arguments are invalid. *)
let get_mat_dim2 ~loc ~mat_name ~dim2 ~c ~n ~n_name =
get_mat_cols ~loc ~mat_name ~dim2 ~c ~p:n ~param_name:n_name
(** [get_mat_n ~loc ~mat_name ~dim2 ~c ~n] checks or infers the matrix column
operation length in the option parameter [n] at location [loc] for matrix
with name [mat_name] and dimension [dim2] given the column operation
offset [c]. @raise Invalid_argument if any arguments are invalid. *)
let get_mat_n ~loc ~mat_name ~dim2 ~c ~n =
get_mat_dim2 ~loc ~mat_name ~dim2 ~c ~n ~n_name:n_str
(** [get_mat_min_dim1 ~loc ~mat_name ~r ~m] @return the minimum row dimension
of a matrix with name [mat_name] at location [loc] given row [r] and
row operation length [m]. @raise Invalid_argument if any arguments
are invalid. *)
let get_mat_min_dim1 ~loc ~mat_name ~r ~m =
if r > 0 then r + m - 1
else invalid_arg (sprintf "%s: %sr < 1: %d" loc mat_name r)
(** [get_mat_min_dim2 ~loc ~mat_name ~c ~n] @return the minimum column
dimension of a matrix with name [mat_name] at location [loc] given column
[c] and row operation length [n]. @raise Invalid_argument if any
arguments are invalid. *)
let get_mat_min_dim2 ~loc ~mat_name ~c ~n =
if c > 0 then c + n - 1
else invalid_arg (sprintf "%s: %sc < 1: %d" loc mat_name c)
(** [check_mat_min_dim1 ~loc ~mat_name ~dim1 ~min_dim1] checks the minimum
row dimension [min_dim1] of a matrix with name [mat_name] at location
[loc] given its row dimension [dim1]. @raise Invalid_argument if
any arguments are invalid. *)
let check_mat_min_dim1 ~loc ~mat_name ~dim1 ~min_dim1 =
if dim1 < min_dim1 then
invalid_arg (
sprintf "%s: dim1(%s): valid=[%d..[ got=%d" loc mat_name min_dim1 dim1)
(** [check_mat_min_dim2 ~loc ~mat_name ~dim2 ~min_dim2] checks the minimum
column dimension [min_dim2] of a matrix with name [mat_name] at location
[loc] given its column dimension [dim2]. @raise Invalid_argument if
any arguments are invalid. *)
let check_mat_min_dim2 ~loc ~mat_name ~dim2 ~min_dim2 =
if dim2 < min_dim2 then
invalid_arg (
sprintf "%s: dim2(%s): valid=[%d..[ got=%d" loc mat_name min_dim2 dim2)
(** [check_mat_min_dim2 ~loc ~mat_name ~dim2 ~min_dim2] checks the minimum
column dimension [min_dim2] of a matrix with name [mat_name] at location
[loc] given its column dimension [dim2]. @raise Invalid_argument if
any arguments are invalid. *)
let check_mat_min_dims ~loc ~mat_name ~dim1 ~dim2 ~min_dim1 ~min_dim2 =
check_mat_min_dim1 ~loc ~mat_name ~dim1 ~min_dim1;
check_mat_min_dim2 ~loc ~mat_name ~dim2 ~min_dim2
(** (Old) Vector checking and allocation functions *)
let check_vec loc vec_name vec min_dim =
check_vec_min_dim ~loc ~vec_name ~dim:(Array1.dim vec) ~min_dim
(** [check_vec_is_perm loc vec_name vec n] checks whether [vec]
is a valid permutation vector. *)
let check_vec_is_perm loc vec_name vec n =
let dim = Array1.dim vec in
if dim <> n then
invalid_arg (sprintf "%s: dim(%s): valid=%d got=%d" loc vec_name n dim)
else
let ub = Int32.of_int n in
for i = 1 to dim do
let r = Array1.get vec i in
check_var_within loc (sprintf "%s(%d)" k_str i) r 1l ub Int32.to_string
done
let get_vec loc vec_name vec ofs inc n vec_create =
let min_dim = get_vec_min_dim ~loc ~vec_name ~ofs ~inc ~n in
match vec with
| Some vec -> check_vec loc vec_name vec min_dim; vec
| None -> vec_create min_dim
(** [get_dim_vec loc vec_name ofs inc vec n_name n] if the dimension [n]
is given, check that the vector [vec] is big enough, otherwise return
the maximal [n] for the given vector [vec]. *)
let get_dim_vec loc vec_name ofs inc vec n_name n =
get_vec_n ~loc ~vec_name ~dim:(Array1.dim vec) ~ofs ~inc ~n_name n
let check_vec_empty ~loc ~vec_name ~dim =
if dim = 0 then
invalid_arg (sprintf "%s: dimension of vector %s is zero" loc vec_name)
else ()
(** (Old) Matrix checking and allocation functions *)
let get_mat loc mat_name mat_create r c mat m n =
let min_dim1 = get_mat_min_dim1 ~loc ~mat_name ~r ~m in
let min_dim2 = get_mat_min_dim2 ~loc ~mat_name ~c ~n in
match mat with
| None -> mat_create min_dim1 min_dim2
| Some mat ->
let dim1 = Array2.dim1 mat in
let dim2 = Array2.dim2 mat in
check_mat_min_dims ~loc ~mat_name ~dim1 ~dim2 ~min_dim1 ~min_dim2;
mat
let check_dim1_mat loc mat_name mat mat_r m_name m =
let dim1 = Array2.dim1 mat in
check_mat_rows ~loc ~mat_name ~dim1 ~r:mat_r ~p:m ~param_name:m_name
let check_dim2_mat loc mat_name mat mat_c n_name n =
let dim2 = Array2.dim2 mat in
check_mat_cols ~loc ~mat_name ~dim2 ~c:mat_c ~p:n ~param_name:n_name
let check_dim_mat loc mat_name mat_r mat_c mat m n =
check_dim1_mat loc mat_name mat mat_r m_str m;
check_dim2_mat loc mat_name mat mat_c n_str n
let get_dim1_mat loc mat_name mat r m_name m =
let dim1 = Array2.dim1 mat in
get_mat_dim1 ~loc ~mat_name ~dim1 ~r ~m ~m_name
let get_dim2_mat loc mat_name mat c n_name n =
let dim2 = Array2.dim2 mat in
get_mat_dim2 ~loc ~mat_name ~dim2 ~c ~n ~n_name
let check_mat_empty ~loc ~mat_name ~dim1 ~dim2 =
if dim1 = 0 then
invalid_arg (sprintf "%s: dim1 of matrix %s is zero" loc mat_name)
else if dim2 = 0 then
invalid_arg (sprintf "%s: dim2 of matrix %s is zero" loc mat_name)
else ()
let get_vec_inc loc vec_name = function
| Some inc -> check_vec_inc ~loc ~vec_name inc; inc
| None -> 1
let get_vec_ofs loc var = function
| Some ofs when ofs < 1 -> invalid_arg (sprintf "%s: ofs%s < 1" loc var)
| Some ofs -> ofs
| None -> 1
(**)
module Mat_patt = struct
type kind = Upper | Lower
let check_upent ~loc ~l ~m =
if l <= 0 then
failwith (sprintf "%s: illegal initial rows (%d) of upper pentagon" loc l)
else if l > m then
failwith (
sprintf
"%s: initial rows (%d) of upper pentagon exceed maximum [m] (%d)"
loc l m)
let check_lpent ~loc ~l ~n =
if l <= 0 then
failwith (
sprintf "%s: illegal initial columns (%d) of lower pentagon" loc l)
else if l > n then
failwith (
sprintf
"%s: initial columns (%d) of lower pentagon exceed maximum [n] (%d)"
loc l n)
let check_args ~loc ~m ~n : Types.Mat.patt option -> unit= function
| None | Some `Full | Some `Utr | Some `Ltr -> ()
| Some `Upent l -> check_upent ~loc ~l ~m
| Some `Lpent l -> check_lpent ~loc ~l ~n
let normalize_args ~loc ~m ~n : Types.Mat.patt option -> kind * int = function
| None | Some `Full -> Lower, n
| Some `Utr -> Upper, 1
| Some `Ltr -> Lower, 1
| Some `Upent l -> check_upent ~loc ~l ~m; Upper, l
| Some `Lpent l -> check_lpent ~loc ~l ~n; Lower, l
let patt_of_uplo ~(uplo : [`U | `L] option) ~(patt : Types.Mat.patt option) =
match uplo with
| Some `U -> Some `Utr
| Some `L -> Some `Ltr
| None -> patt
let patt_of_up ~up ~(patt : Types.Mat.patt option) =
match up with
| Some true -> Some `Utr
| Some false -> Some `Ltr
| None -> patt
end
(**)
external ilaenv :
(int [@untagged]) ->
string ->
string ->
(int [@untagged]) ->
(int [@untagged]) ->
(int [@untagged]) ->
(int [@untagged]) ->
(int [@untagged])
= "lacaml_ilaenv_stub_bc" "lacaml_ilaenv_stub" [@@noalloc]
let get_work loc vec_create work min_lwork opt_lwork lwork_str =
match work with
| Some work ->
let lwork = Array1.dim work in
if lwork < min_lwork then
invalid_arg (
sprintf "%s: %s: valid=[%d..[ got=%d" loc lwork_str min_lwork lwork)
else work, lwork
| None -> vec_create opt_lwork, opt_lwork
let calc_unpacked_dim loc n_vec =
let n = truncate (sqrt (float (8 * n_vec + 1)) *. 0.5) in
if (n * n + n) / 2 <> n_vec then
failwith (sprintf "%s: illegal vector length: %d" loc n_vec)
else n
let get_unpacked_dim loc ?n n_vec =
match n with
| None -> calc_unpacked_dim loc n_vec
| Some n ->
let n_unpacked = calc_unpacked_dim loc n_vec in
if n < 0 || n > n_unpacked then
invalid_arg (sprintf "%s: n: valid=[0..%d] got=%d" loc n_unpacked n)
else n
let get_vec_geom loc var ofs inc =
get_vec_ofs loc var ofs, get_vec_inc loc var inc
let get_k_mat_sb loc mat_name mat mat_r k_name k =
let dim1 = Array2.dim1 mat in
let max_k = dim1 - mat_r in
if mat_r < 1 || max_k < 0 then
invalid_arg (
sprintf "%s: mat_r(%s): valid=[1..%d] got=%d" loc mat_name dim1 mat_r);
match k with
| None -> max_k
| Some k ->
if k < 0 || max_k < k then
invalid_arg (
sprintf "%s: %s(%s): valid=[0..%d] got=%d"
loc k_name mat_name max_k k)
else k
let get_dim_mat_packed loc mat_name ofsmat mat n_name n =
let dim = Array1.dim mat in
match n with
| Some n ->
let n1 = ofsmat + (n - 1)*(n + 2)/2 in
if n < 0 || dim < n1 then
invalid_arg (sprintf "%s: %s(%s): valid=[0..%d] got=%d"
loc n_name mat_name dim n1)
else n
| None ->
max 0 (truncate((sqrt(9. +. 8. *. float(dim - ofsmat)) -. 1.) /. 2.))
let get_n_of_square loc mat_name r c mat n =
let n = get_dim2_mat loc mat_name mat c n_str n in
check_dim1_mat loc mat_name mat r n_str n;
n
let get_n_of_a loc ar ac a n = get_n_of_square loc a_str ar ac a n
let get_nrhs_of_b loc n br bc b nrhs =
let nrhs = get_dim2_mat loc b_str b bc nrhs_str nrhs in
check_dim1_mat loc b_str b br n_str n;
nrhs
let orgqr_err ~loc ~m ~n ~k ~work ~a ~err =
let msg =
match err with
| -1 -> sprintf "m: valid=[0..[ got=%d" m
| -2 -> sprintf "n: valid=[0..%d] got=%d" m n
| -3 -> sprintf "k: valid=[0..%d] got=%d" n k
| -5 -> sprintf "dim2(a): valid=[%d..[ got=%d" n (Array2.dim2 a)
| -8 ->
sprintf "dim1(work): valid=[%d..[ got=%d" (max 1 n) (Array1.dim work)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n))
in
invalid_arg (sprintf "%s: %s" loc msg)
let orgqr_get_params loc ?m ?n ?k ~tau ~ar ~ac 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
if m < n then invalid_arg (sprintf "%s: m(%d) < n(%d)" loc m n)
else
let k = get_dim_vec loc tau_str 1 1 tau k_str k in
m, n, k
let ormqr_err ~loc ~side ~m ~n ~k ~lwork ~a ~c ~err =
let nq, nw =
match side with
| `L -> m, n
| `R -> n, m
in
let msg =
match err with
| -3 -> sprintf "m: valid=[0..[ got=%d" m
| -4 -> sprintf "n: valid=[0..[ got=%d" n
| -5 -> sprintf "k: valid=[0..%d] got=%d" k nq
| -7 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 nq) (Array2.dim1 a)
| -10 -> sprintf "dim1(c): valid=[%d..[ got=%d" (max 1 m) (Array2.dim1 c)
| -12 ->
let min_lwork = max 1 nw in
sprintf "lwork: valid=[%d..[ got=%d" min_lwork lwork
| _ -> raise (InternalError (sprintf "%s: error code %d" loc err))
in
invalid_arg (sprintf "%s: %s" loc msg)
let ormqr_get_params loc ~side ?m ?n ?k ~tau ~ar ~ac a ~cr ~cc c =
let m = get_dim1_mat loc c_str c cr m_str m in
let n = get_dim2_mat loc c_str c cc n_str n in
let k = get_dim2_mat loc a_str a ac k_str k in
begin match side with
| `L ->
if m < k then failwith (sprintf "%s: m(%d) < k(%d)" loc m k);
check_dim1_mat loc a_str a ar m_str (max 1 m)
| `R ->
if n < k then failwith (sprintf "%s: n(%d) < k(%d)" loc n k);
check_dim1_mat loc a_str a ar n_str (max 1 n)
end;
check_vec loc tau_str tau k;
m, n, k
let gelsX_err loc gelsX_min_work ar a m n lwork nrhs br b err =
if err > 0 then
failwith
(sprintf "%s: failed to converge on off-diagonal element %d" loc err)
else
let msg =
match err with
| -1 -> sprintf "m: valid=[0..[ got=%d" m
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -3 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -5 ->
sprintf "dim1(a): valid=[%d..[ got=%d"
(max 1 m + ar - 1) (Array2.dim1 a)
| -7 ->
let min_dim = max 1 (max m n) + br - 1 in
sprintf "dim1(b): valid=[%d..[ got=%d" min_dim (Array2.dim1 b)
| -12 ->
let min_lwork = gelsX_min_work ~m ~n ~nrhs in
sprintf "lwork: valid=[%d..[ got=%d" min_lwork lwork
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let gelsX_get_s vec_create loc min_dim ofss = function
| Some s ->
let dim_s = Array1.dim s in
let min_dim_ofs = ofss - 1 + min_dim in
if dim_s < min_dim_ofs then
invalid_arg (sprintf "%s: s: valid=[%d..[ got=%d" loc min_dim_ofs dim_s)
else s
| None -> vec_create min_dim
let gelsX_get_params loc ar ac a m n nrhs br bc 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
let nrhs = get_dim2_mat loc b_str b bc nrhs_str nrhs in
check_dim1_mat loc b_str b br m_str (max m n);
m, n, nrhs
let xxev_get_params loc ar ac a n vectors up =
let n = get_n_of_a loc ar ac a n in
let jobz = get_job_char vectors in
let uplo = get_uplo_char up in
n, jobz, uplo
let xxev_get_wx vec_create loc wname ofsw w n =
match w with
| None -> vec_create (ofsw - 1 + n)
| Some w -> check_vec loc wname w (ofsw - 1 + n); w
let geev_get_job_side loc mat_empty mat_create mat_name n r c mat_opt =
match mat_opt with
| None ->
if r < 1 then failwith (sprintf "%s: %sr < 1" loc mat_name)
else if c < 1 then failwith (sprintf "%s: %sc < 1" loc mat_name)
else r, c, mat_create (n + r - 1) (n + c - 1), job_char_true, true
| Some None -> 1, 1, mat_empty, job_char_false, false
| Some (Some mat) ->
check_dim1_mat loc mat_name mat r n_str n;
check_dim2_mat loc mat_name mat c n_str n;
r, c, mat, job_char_true, true
let geev_gen_get_params loc mat_empty mat_create ar ac a n
leftr leftc left rightr rightc right =
let n = get_n_of_a loc ar ac a n in
let leftr, leftc, vl, jobvl, lvs =
geev_get_job_side loc mat_empty mat_create "vl" n leftr leftc left in
let rightr, rightc, vr, jobvr, rvs =
geev_get_job_side loc mat_empty mat_create "vr" n rightr rightc right in
n, leftr, leftc, vl, jobvl, rightr, rightc, vr, jobvr, lvs || rvs
let gXmv_get_params loc vec_create m n ofsx incx x ofsy incy y trans =
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 lx, ly, trans_char =
let trans_char = get_trans_char trans in
if trans = `N then n, m, trans_char else m, n, trans_char in
check_vec loc x_str x (ofsx + (lx - 1) * abs incx);
let y = get_vec loc y_str y ofsy incy ly vec_create in
ofsx, incx, ofsy, incy, y, trans_char
let symv_get_params loc vec_create ar ac a n ofsx incx x ofsy incy y up =
let n = get_dim1_mat loc a_str a ar n_str n in
check_dim2_mat loc a_str a ac n_str n;
let ofsx, incx = get_vec_geom loc x_str ofsx incx in
let ofsy, incy = get_vec_geom loc y_str ofsy incy in
check_vec loc x_str x (ofsx + (n - 1) * abs incx);
let y = get_vec loc y_str y ofsy incy n vec_create in
check_vec loc y_str y (ofsy + (n - 1) * abs incy);
n, ofsx, incx, ofsy, incy, y, get_uplo_char up
let trXv_get_params loc ar ac a n ofsx incx x up trans unit_triangular =
let n = get_dim1_mat loc a_str a ar n_str n in
check_dim2_mat loc a_str a ac n_str n;
let trans_char = get_trans_char trans in
let diag_char = get_diag_char unit_triangular in
let ofsx, incx = get_vec_geom loc x_str ofsx incx in
check_vec loc x_str x (ofsx + (n - 1) * abs incx);
n, ofsx, incx, get_uplo_char up, trans_char, diag_char
let tpXv_get_params loc ofsap ap ?n ofsx incx x up trans unit_triangular =
let ofsap = get_vec_ofs loc ap_str ofsap in
let n = get_unpacked_dim loc ?n (Array1.dim ap - ofsap + 1) in
let trans_char = get_trans_char trans in
let diag_char = get_diag_char unit_triangular in
let ofsx, incx = get_vec_geom loc x_str ofsx incx in
check_vec loc x_str x (ofsx + (n - 1) * abs incx);
n, ofsap, ofsx, incx, get_uplo_char up, trans_char, diag_char
let get_c loc mat_create cr cc c m n = get_mat loc c_str mat_create cr cc c m n
let get_rows_mat_tr loc mat_str mat mat_r mat_c transp dim_str dim =
match transp with
| `N -> get_dim1_mat loc mat_str mat mat_r dim_str dim
| _ -> get_dim2_mat loc mat_str mat mat_c dim_str dim
let get_cols_mat_tr loc mat_str mat mat_r mat_c transp dim_str dim =
match transp with
| `N -> get_dim2_mat loc mat_str mat mat_c dim_str dim
| _ -> get_dim1_mat loc mat_str mat mat_r dim_str dim
let get_inner_dim loc mat1_str mat1 mat1_r mat1_c tr1
mat2_str mat2 mat2_r mat2_c tr2 dim_str k =
let k1 = get_cols_mat_tr loc mat1_str mat1 mat1_r mat1_c tr1 dim_str k in
let k2 = get_rows_mat_tr loc mat2_str mat2 mat2_r mat2_c tr2 dim_str k in
if k = None && k1 <> k2 then
failwith (
sprintf "%s: inner dimensions of matrices do not match (%d,%d)"
loc k1 k2)
else k1
let gemm_get_params loc mat_create ar ac a transa br bc b cr transb cc c m n k =
let m = get_rows_mat_tr loc a_str a ar ac transa m_str m in
let n = get_cols_mat_tr loc b_str b br bc transb n_str n in
let k = get_inner_dim loc a_str a ar ac transa b_str b br bc transb k_str k in
let transa = get_trans_char transa in
let transb = get_trans_char transb in
let c = get_c loc mat_create cr cc c m n in
m, n, k, transa, transb, c
let check_mat_square loc mat_str mat mat_r mat_c n =
check_dim1_mat loc mat_str mat mat_r n_str n;
check_dim2_mat loc mat_str mat mat_c n_str n
let symm_get_params loc mat_create ar ac a br bc b cr cc c m n side up =
let m = get_dim1_mat loc b_str b br m_str m in
let n = get_dim2_mat loc b_str b bc n_str n in
if side = `L then check_mat_square loc a_str a ar ac m
else check_mat_square loc a_str a ar ac n;
let side_char = get_side_char side in
let uplo_char = get_uplo_char up in
let c = get_c loc mat_create cr cc c m n in
m, n, side_char, uplo_char, c
let trXm_get_params loc ar ac a br bc b m n side up transa diag =
let m = get_dim1_mat loc b_str b br m_str m in
let n = get_dim2_mat loc b_str b bc n_str n in
if side = `L then check_mat_square loc a_str a ar ac m
else check_mat_square loc a_str a ar ac n;
let side_char = get_side_char side in
let uplo_char = get_uplo_char up in
let transa = get_trans_char transa in
let diag_char = get_diag_char diag in
m, n, side_char, uplo_char, transa, diag_char
let syrk_get_params loc mat_create ar ac a cr cc c n k up trans =
let n = get_rows_mat_tr loc a_str a ar ac trans n_str n in
let k = get_cols_mat_tr loc a_str a ar ac trans k_str k in
let trans_char = get_trans_char trans in
let uplo_char = get_uplo_char up in
let c = get_c loc mat_create cr cc c n n in
n, k, uplo_char, trans_char, c
let syr2k_get_params loc mat_create ar ac a br bc b cr cc c n k up trans =
let n = get_rows_mat_tr loc a_str a ar ac trans n_str n in
let k = get_cols_mat_tr loc a_str a ar ac trans k_str k in
begin match trans with
| `N ->
check_dim1_mat loc b_str b br n_str n;
check_dim2_mat loc b_str b bc k_str k;
| _ ->
check_dim1_mat loc b_str b br k_str k;
check_dim2_mat loc b_str b bc n_str n;
end;
let trans_char = get_trans_char trans in
let uplo_char = get_uplo_char up in
let c = get_c loc mat_create cr cc c n n in
n, k, uplo_char, trans_char, c
let xlange_get_params loc m n ar ac 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
m, n
let xxtrs_get_params loc ar ac a n br bc b nrhs =
let n = get_n_of_a loc ar ac a n in
let nrhs = get_nrhs_of_b loc n br bc b nrhs in
n, nrhs
let xxtrs_err loc n nrhs a b err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -3 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -5 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| -8 -> sprintf "dim1(b): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 b)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let xxtri_singular_err loc err =
failwith (sprintf "%s: singular on index %i" loc err)
let xxtri_err loc n a err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let xxcon_err loc n a err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let geXrf_get_params loc m n ar ac 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
m, n
let getrf_err loc m n a err =
let msg =
match err with
| -1 -> sprintf "n: valid=[0..[ got=%d" n
| -2 -> sprintf "m: valid=[0..[ got=%d" m
| -4 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 m) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let getrf_lu_err loc err =
failwith (sprintf "%s: U(%i,%i)=0 in the LU factorization" loc err err)
let getrf_get_ipiv loc ipiv m n =
match ipiv with
| None -> create_int32_vec (min m n)
| Some ipiv ->
check_vec loc ipiv_str ipiv (min m n);
ipiv
let sytrf_get_ipiv loc ipiv n =
match ipiv with
| None -> create_int32_vec n
| Some ipiv ->
check_vec loc ipiv_str ipiv n;
ipiv
let sytrf_err loc n a err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let sytrf_fact_err loc err =
failwith (sprintf "%s: D(%i,%i)=0 in the factorization" loc err err)
let potrf_chol_err loc err =
failwith (
sprintf "%s: leading minor of order %d is not positive definite" loc err)
let potrf_err loc n a err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| _ -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let potrs_err loc n nrhs a b err =
let msg =
match err with
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -3 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -5 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| -7 -> sprintf "dim1(b): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 b)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let trtrs_err loc n nrhs a b err =
let msg =
match err with
| -4 -> sprintf "n: valid=[0..[ got=%d" n
| -5 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -7 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| -9 -> sprintf "dim1(b): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 b)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let tbtrs_err loc n nrhs kd ab b err =
let msg =
match err with
| -4 -> sprintf "n: valid=[0..[ got=%d" n
| -5 -> sprintf "kd: valid=[0..[ got=%d" kd
| -6 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -8 -> sprintf "dim1(ab): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 ab)
| -10 -> sprintf "dim1(b): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 b)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let getri_err loc getri_min_lwork n a lwork err =
let msg =
match err with
| -1 -> sprintf "n: valid=[0..[ got=%d" n
| -3 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| -6 ->
let min_lwork = getri_min_lwork n in
sprintf "lwork: valid=[%d..[ got=%d" min_lwork lwork
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let trtri_err loc n a err =
let msg =
match err with
| -3 -> sprintf "n: valid=[0..[ got=%d" n
| -5 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let geqrf_err loc m n a err =
let msg =
match err with
| -1 -> sprintf "m: valid=[0..[ got=%d" m
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let gecon_err loc norm_char n a err =
let msg =
match err with
| -1 -> sprintf "norm: valid=['O', I'] got='%c'" norm_char
| -2 -> sprintf "n: valid=[0..[ got=%d" n
| -4 -> sprintf "dim1(a): valid=%d..[ got=%d" (max 1 n) (Array2.dim1 a)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let gees_err loc n err jobvs sort =
if err > 0 && err <= n then
failwith (sprintf "%s: %d eigenvalue elements did not converge" loc err)
else if err = n + 1 then
failwith (
sprintf "%s: eigenvalues not reordered, too close to separate" loc)
else if err = n + 2 then
failwith (
sprintf "%s: after reordering, roundoff changed values of some \
complex eigenvalues so that leading eigenvalues in \
the Schur form no longer satisfy SELECT" loc)
else
let msg =
match err with
| -1 -> sprintf "JOBVS: valid=['N', V'] got='%c'" jobvs
| -2 -> sprintf "SORT: valid=['N', S'] got='%c'" sort
| -4 -> sprintf "n: valid=[0..[ got=%d" n
| n -> raise (InternalError (sprintf "%s: error code %d" loc n))
in
invalid_arg (sprintf "%s: %s" loc msg)
let dummy_select_fun _ = false
let gees_get_params_generic
loc mat_create mat_empty jobvs sort n ar ac a vsr vsc vs =
let n = get_n_of_a loc ar ac a n in
let jobvs, min_ldvs =
match jobvs with
| `No_Schur_vectors -> 'N', 1
| `Compute_Schur_vectors -> 'V', n
in
let vs =
match vs with
| Some vs ->
check_dim1_mat loc vs_str vs vsr vsr_str min_ldvs;
check_dim2_mat loc vs_str vs vsc vsc_str n;
vs
| None when jobvs = 'N' -> mat_empty
| None -> mat_create min_ldvs n
in
let sort, select, select_fun =
match sort with
| `No_sort -> 'N', 0, dummy_select_fun
| `Select_left_plane -> 'S', 0, dummy_select_fun
| `Select_right_plane -> 'S', 1, dummy_select_fun
| `Select_interior_disk -> 'S', 2, dummy_select_fun
| `Select_exterior_disk -> 'S', 3, dummy_select_fun
| `Select_custom select_fun -> 'S', 4, select_fun
in
jobvs, sort, select, select_fun, n, vs
let gees_get_params_real
loc vec_create mat_create mat_empty
jobvs sort n ar ac a wr wi vsr vsc vs =
let jobvs, sort, select, select_fun, n, vs =
gees_get_params_generic
loc mat_create mat_empty jobvs sort n ar ac a vsr vsc vs
in
let wr =
match wr with
| None -> vec_create n
| Some wr -> check_vec loc wr_str wr n; wr
in
let wi =
match wi with
| None -> vec_create n
| Some wi -> check_vec loc wi_str wi n; wi
in
jobvs, sort, select, select_fun, n, vs, wr, wi
let gees_get_params_complex
loc vec_create mat_create mat_empty jobvs sort n ar ac a w vsr vsc vs =
let jobvs, sort, select, select_fun, n, vs =
gees_get_params_generic
loc mat_create mat_empty jobvs sort n ar ac a vsr vsc vs
in
let w =
match w with
| None -> vec_create n
| Some w -> check_vec loc w_str w n; w
in
jobvs, sort, select, select_fun, n, vs, w
let gesvd_err loc jobu jobvt m n a u vt lwork err =
if err > 0 then
failwith
(sprintf "%s: %d off-diagonal elements did not converge" loc err)
else
let msg =
match err with
| -3 -> sprintf "m: valid=[0..[ got=%d" m
| -4 -> sprintf "n: valid=[0..[ got=%d" n
| -6 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 m) (Array2.dim1 a)
| -9 ->
sprintf "dim1(u): valid=[%d..[ got=%d"
(match jobu with 'A' | 'S' -> max 1 m | _ -> 1)
(Array2.dim1 u)
| -11 ->
sprintf "dim1(vt): valid=[%d..[ got=%d"
(
match jobvt with
| 'A' -> max 1 n
| 'S' -> max 1 (min m n)
| _ -> 1
)
(Array2.dim1 vt)
| -13 -> sprintf "lwork: valid=[%d..[ got=%d" 1 lwork
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let gesvd_get_params
loc vec_create mat_create jobu jobvt m n ar ac a s ur uc u vtr vtc vt =
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 s = get_vec loc s_str s 1 1 (min m n) vec_create in
let um, un =
match jobu with
| `A -> m, m
| `S -> m, min m n
| `O | `N -> 1, 1 in
let u =
match u with
| Some u ->
check_dim1_mat loc u_str u ur um_str um;
check_dim2_mat loc u_str u uc un_str un;
u
| None -> mat_create um un in
let vm, vn =
match jobvt with
| `A -> n, n
| `S -> min m n, n
| `O | `N -> 1, 1 in
let vt =
match vt with
| Some vt ->
check_dim1_mat loc vt_str vt vtr vm_str vm;
check_dim2_mat loc vt_str vt vtc vn_str vn;
vt
| None -> mat_create vm vn in
let jobu_c = get_s_d_job_char jobu in
let jobvt_c = get_s_d_job_char jobvt in
jobu_c, jobvt_c, m, n, s, u, vt
let gesdd_err loc jobz m n a u vt lwork err =
if err > 0 then
failwith (
sprintf "%s: %d DBDSDC did not converge, updating process failed" loc err)
else
let msg =
match err with
| -2 -> sprintf "m: valid=[0..[ got=%d" m
| -3 -> sprintf "n: valid=[0..[ got=%d" n
| -5 -> sprintf "dim1(a): valid=[%d..[ got=%d" (max 1 m) (Array2.dim1 a)
| -8 ->
sprintf "dim1(u): valid=[%d..[ got=%d"
(
if jobz = 'A' || jobz = 'S' || (jobz = 'O' && m < n)
then max 1 m
else 1
)
(Array2.dim1 u)
| -10 ->
sprintf "dim1(vt): valid=[%d..[ got=%d"
(
if jobz = 'A' || (jobz = 'O' && m >= n) then max 1 n
else if jobz = 'S' then max 1 (min m n)
else 1
)
(Array2.dim1 vt)
| -12 -> sprintf "lwork: valid=[%d..[ got=%d" 1 lwork
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let gesdd_get_params
loc vec_create mat_create jobz m n ar ac a s ur uc u vtr vtc vt =
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 min_m_n = min m n in
let s = get_vec loc s_str s 1 1 min_m_n vec_create in
let um, un, vm, vn =
match jobz with
| `A -> m, m, n, n
| `S -> m, min_m_n, min_m_n, n
| `O -> if m >= n then 1, 1, n, n else m, m, m, n
| `N -> 1, 1, 1, 1 in
let u =
match u with
| Some u ->
check_dim1_mat loc u_str u ur um_str um;
check_dim2_mat loc u_str u uc un_str un;
u
| None -> mat_create um un in
let vt =
match vt with
| Some vt ->
check_dim1_mat loc vt_str vt vtr vm_str vm;
check_dim2_mat loc vt_str vt vtc vn_str vn;
vt
| None -> mat_create vm vn in
let jobz_c = get_s_d_job_char jobz in
jobz_c, m, n, s, u, vt
let xxsv_err loc n nrhs b err =
let msg =
match err with
| -1 -> sprintf "n: valid=[0..[ got=%d" n
| -2 -> sprintf "nrhs: valid=[0..[ got=%d" nrhs
| -7 -> sprintf "dim1(b): valid=[%d..[ got=%d" (max 1 n) (Array2.dim1 b)
| n -> raise (InternalError (sprintf "%s: error code %d" loc n)) in
invalid_arg (sprintf "%s: %s" loc msg)
let xxsv_lu_err loc err =
failwith (sprintf "%s: U(%i,%i)=0 in the LU factorization" loc err err)
let xxsv_pos_err loc err =
let msg =
sprintf
"%s: the leading minor of order %i is not positive definite" loc err in
failwith msg
let xxsv_ind_err loc err =
let msg =
sprintf
"%s: D(%i,%i)=0 in the diagonal pivoting factorization" loc err err in
failwith msg
let xxsv_a_err loc a n =
let msg =
sprintf "%s: dim1(a): valid=[%d..[ got=%d" loc (max 1 n) (Array2.dim1 a) in
invalid_arg msg
let xxsv_work_err loc lwork =
invalid_arg (sprintf "%s: dim(work): valid=[1..[ got=%d" loc lwork)
let xxsv_get_ipiv loc ipiv n =
match ipiv with
| None -> create_int32_vec n
| Some ipiv ->
check_vec loc ipiv_str ipiv n;
ipiv
let xxsv_get_params loc ar ac a n br bc b nrhs =
let n = get_n_of_a loc ar ac a n in
let nrhs = get_nrhs_of_b loc n br bc b nrhs in
n, nrhs