File: util.hh

package info (click to toggle)
lapackpp 2024.10.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,500 kB
  • sloc: cpp: 80,181; ansic: 27,660; python: 4,838; xml: 182; perl: 99; makefile: 53; sh: 23
file content (1595 lines) | stat: -rw-r--r-- 41,000 bytes parent folder | download
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
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.

#ifndef LAPACK_UTIL_HH
#define LAPACK_UTIL_HH

#include <exception>
#include <complex>
#include <ctype.h>

#include <assert.h>

#include "blas.hh"

// from config, we need only lapack_logical for callback functions lapack_*_select*
#include "lapack/config.h"

namespace lapack {

// -----------------------------------------------------------------------------
/// Exception class for LAPACK errors.
class Error: public std::exception {
public:
    /// Constructs LAPACK error
    Error():
        std::exception()
    {}

    /// Constructs BLAS error with message
    Error( std::string const& msg ):
        std::exception(),
        msg_( msg )
    {}

    /// Constructs LAPACK error with message: "func: msg"
    Error( const char* msg, const char* func ):
        std::exception(),
        msg_( std::string(msg) + ", in function " + func )
    {}

    /// Returns LAPACK error message
    virtual const char* what() const noexcept override
        { return msg_.c_str(); }

private:
    std::string msg_;
};

// =============================================================================
namespace internal {

// -----------------------------------------------------------------------------
// internal helper function; throws Error if cond is true
// called by blas_error_if macro
inline void throw_if( bool cond, const char* condstr, const char* func )
{
    if (cond) {
        throw Error( condstr, func );
    }
}

#if defined(_MSC_VER)
    #define LAPACKPP_ATTR_FORMAT(I, F)
#else
    #define LAPACKPP_ATTR_FORMAT(I, F) __attribute__((format( printf, I, F )))
#endif

// -----------------------------------------------------------------------------
// internal helper function; throws Error if cond is true
// uses printf-style format for error message
// called by lapack_error_if_msg macro
// condstr is ignored, but differentiates this from other version.
inline void throw_if( bool cond, const char* condstr, const char* func, const char* format, ... )
    LAPACKPP_ATTR_FORMAT(4, 5);

inline void throw_if( bool cond, const char* condstr, const char* func, const char* format, ... )
{
    if (cond) {
        char buf[80];
        va_list va;
        va_start( va, format );
        vsnprintf( buf, sizeof(buf), format, va );
        throw Error( buf, func );
    }
}

// -----------------------------------------------------------------------------
// internal helper function; aborts if cond is true
// uses printf-style format for error message
// called by lapack_error_if_msg macro
inline void abort_if( bool cond, const char* func,  const char* format, ... )
    LAPACKPP_ATTR_FORMAT(3, 4);

inline void abort_if( bool cond, const char* func,  const char* format, ... )
{
    if (cond) {
        char buf[80];
        va_list va;
        va_start( va, format );
        vsnprintf( buf, sizeof(buf), format, va );

        fprintf( stderr, "Error: %s, in function %s\n", buf, func );
        abort();
    }
}

#undef LAPACKPP_ATTR_FORMAT

} // namespace internal

// -----------------------------------------------------------------------------
// internal macros to handle error checks
#if defined(LAPACK_ERROR_NDEBUG) || (defined(LAPACK_ERROR_ASSERT) && defined(NDEBUG))

    // lapackpp does no error checking;
    // lower level LAPACK may still handle errors via xerbla
    #define lapack_error_if( cond ) \
        ((void)0)

    #define lapack_error_if_msg( cond, ... ) \
        ((void)0)

#elif defined(LAPACK_ERROR_ASSERT)

    // lapackpp aborts on error
    #define lapack_error_if( cond ) \
        lapack::internal::abort_if( cond, __func__, "%s", #cond )

    #define lapack_error_if_msg( cond, ... ) \
        lapack::internal::abort_if( cond, __func__, __VA_ARGS__ )

#else

    // lapackpp throws errors (default)
    // internal macro to get string #cond; throws Error if cond is true
    // ex: lapack_error_if( a < b );
    #define lapack_error_if( cond ) \
        lapack::internal::throw_if( cond, #cond, __func__ )

    // internal macro takes cond and printf-style format for error message.
    // throws Error if cond is true.
    // ex: lapack_error_if_msg( a < b, "a %d < b %d", a, b );
    #define lapack_error_if_msg( cond, ... ) \
        lapack::internal::throw_if( cond, #cond, __func__, __VA_ARGS__ )

#endif

// =============================================================================
// Callback logical functions of one, two, or three arguments are used
// to select eigenvalues to sort to the top left of the Schur form in gees and gges.
// The value is selected if function returns TRUE (non-zero).

typedef lapack_logical (*lapack_s_select2) ( float const* omega_real, float const* omega_imag );
typedef lapack_logical (*lapack_s_select3) ( float const* alpha_real, float const* alpha_imag, float const* beta );

typedef lapack_logical (*lapack_d_select2) ( double const* omega_real, double const* omega_imag );
typedef lapack_logical (*lapack_d_select3) ( double const* alpha_real, double const* alpha_imag, double const* beta );

typedef lapack_logical (*lapack_c_select1) ( std::complex<float> const* omega );
typedef lapack_logical (*lapack_c_select2) ( std::complex<float> const* alpha, std::complex<float> const* beta );

typedef lapack_logical (*lapack_z_select1) ( std::complex<double> const* omega );
typedef lapack_logical (*lapack_z_select2) ( std::complex<double> const* alpha, std::complex<double> const* beta );

// =============================================================================
using blas::Layout;
using blas::Op;
using blas::Uplo;
using blas::Diag;
using blas::Side;

// -----------------------------------------------------------------------------
// like blas::side, but adds Both for trevc
enum class Sides : char {
    Left  = 'L',  L = 'L',
    Right = 'R',  R = 'R',
    Both  = 'B',  B = 'B',
};

extern const char* Sides_help;

inline char to_char( Sides value )
{
    return char( value );
}

inline const char* to_c_string( Sides value )
{
    switch (value) {
        case Sides::Left:  return "left";
        case Sides::Right: return "right";
        case Sides::Both:  return "both";
    }
    return "?";
}

inline std::string to_string( Sides value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Sides* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "l" || str_ == "left")
        *val = Sides::Left;
    else if (str_ == "r" || str_ == "right")
        *val = Sides::Right;
    else if (str_ == "b" || str_ == "both")
        *val = Sides::Both;
    else
        throw Error( "unknown Sides: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char sides2char( Sides value )
{
    return char( value );
}

[[deprecated("use to_string or to_c_string. To be removed 2025-05.")]]
inline const char* sides2str( Sides value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Sides char2sides( char ch )
{
    Sides val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
enum class Norm : char {
    One = '1',  // or 'O'
    Two = '2',
    Inf = 'I',
    Fro = 'F',  // or 'E'
    Max = 'M',
};

extern const char* Norm_help;

inline char to_char( Norm value )
{
    return char( value );
}

inline const char* to_c_string( Norm value )
{
    switch (value) {
        case Norm::One: return "1";
        case Norm::Two: return "2";
        case Norm::Inf: return "inf";
        case Norm::Fro: return "fro";
        case Norm::Max: return "max";
    }
    return "?";
}

inline std::string to_string( Norm value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Norm* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "1" || str_ == "o" || str_ == "one")
        *val = Norm::One;
    else if (str_ == "2" || str_ == "two")
        *val = Norm::Two;
    else if (str_ == "i" || str_ == "inf")
        *val = Norm::Inf;
    else if (str_ == "f" || str_ == "fro")
        *val = Norm::Fro;
    else if (str_ == "m" || str_ == "max")
        *val = Norm::Max;
    else
        throw Error( "unknown Norm: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char norm2char( Norm value )
{
    return char( value );
}

[[deprecated("use to_string or to_c_string. To be removed 2025-05.")]]
inline const char* norm2str( Norm value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Norm char2norm( char ch )
{
    Norm val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

//------------------------------------------------------------------------------
// itype is integer, 1, 2, 3.
// sygv
extern const char* itype_help;

// -----------------------------------------------------------------------------
// Job for computing eigenvectors and singular vectors
// # needs custom map
enum class Job : char {
    NoVec        = 'N',
    Vec          = 'V',  // geev, syev, ...
    UpdateVec    = 'U',  // gghrd#, hbtrd, hgeqz#, hseqr#, ... (many compq or compz)

    AllVec       = 'A',  // gesvd, gesdd, gejsv#
    SomeVec      = 'S',  // gesvd, gesdd, gejsv#, gesvj#
    OverwriteVec = 'O',  // gesvd, gesdd

    CompactVec   = 'P',  // bdsdc
    SomeVecTol   = 'C',  // gesvj
    VecJacobi    = 'J',  // gejsv
    Workspace    = 'W',  // gejsv
};

extern const char* Job_eig_help;
extern const char* Job_eig_left_help;
extern const char* Job_eig_right_help;
extern const char* Job_svd_left_help;
extern const char* Job_svd_right_help;

inline char to_char( Job value )
{
    return char( value );
}

// custom maps
// bbcsd, orcsd2by1
inline char to_char_csd( Job value )
{
    switch (value) {
        case Job::Vec:          return 'Y';  // orcsd
        case Job::UpdateVec:    return 'Y';  // bbcsd
        default: return char( value );
    }
}

// bdsdc, gghrd, hgeqz, hseqr, pteqr, stedc, steqr, tgsja, trexc, trsen
inline char to_char_comp( Job value )
{
    switch (value) {
        case Job::Vec:          return 'I';
        case Job::UpdateVec:    return 'V';
        default: return char( value );
    }
}

// tgsja
inline char to_char_compu( Job value )
{
    switch (value) {
        case Job::Vec:          return 'I';
        case Job::UpdateVec:    return 'U';
        default: return char( value );
    }
}

// tgsja
inline char to_char_compq( Job value )
{
    switch (value) {
        case Job::Vec:          return 'I';
        case Job::UpdateVec:    return 'Q';
        default: return char( value );
    }
}

// ggsvd3, ggsvp3
inline char to_char_jobu( Job value )
{
    switch (value) {
        case Job::Vec:          return 'U';
        default: return char( value );
    }
}

// ggsvd3, ggsvp3
inline char to_char_jobq( Job value )
{
    switch (value) {
        case Job::Vec:          return 'Q';
        default: return char( value );
    }
}

// gejsv
inline char to_char_gejsv( Job value )
{
    switch (value) {
        case Job::SomeVec:      return 'U';
        case Job::AllVec:       return 'F';
        default: return char( value );
    }
}

// gesvj
inline char to_char_gesvj( Job value )
{
    switch (value) {
        case Job::SomeVec:      return 'U';  // jobu
        case Job::SomeVecTol:   return 'C';  // jobu
        case Job::UpdateVec:    return 'U';  // jobv
        default: return char( value );
    }
}

inline const char* to_c_string( Job value )
{
    switch (value) {
        case Job::NoVec:        return "novec";
        case Job::Vec:          return "vec";
        case Job::UpdateVec:    return "update";

        case Job::AllVec:       return "all";
        case Job::SomeVec:      return "some";
        case Job::OverwriteVec: return "overwrite";

        case Job::CompactVec:   return "compact";
        case Job::SomeVecTol:   return "sometol";
        case Job::VecJacobi:    return "jacobi";
        case Job::Workspace:    return "work";
    }
    return "?";
}

inline std::string to_string( Job value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Job* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "n" || str_ == "novec")
        *val = Job::NoVec;
    else if (str_ == "v" || str_ == "vec")
        *val = Job::Vec;
    else if (str_ == "u" || str_ == "update" || str_ == "updatevec")
        *val = Job::UpdateVec;

    else if (str_ == "a" || str_ == "all" || str_ == "allvec")
        *val = Job::AllVec;
    else if (str_ == "s" || str_ == "some" || str_ == "somevec")
        *val = Job::SomeVec;
    else if (str_ == "o" || str_ == "overwrite" || str_ == "overwritevec")
        *val = Job::OverwriteVec;

    else if (str_ == "p" || str_ == "compact" || str_ == "compactvec")
        *val = Job::CompactVec;
    else if (str_ == "c" || str_ == "somevectol")
        *val = Job::SomeVecTol;
    else if (str_ == "j" || str_ == "jacobi")
        *val = Job::VecJacobi;
    else if (str_ == "w" || str_ == "workspace")
        *val = Job::Workspace;
    else
        throw Error( "unknown Job: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job2char       ( Job value ) { return to_char      ( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job_csd2char   ( Job value ) { return to_char_csd  ( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job_comp2char  ( Job value ) { return to_char_comp ( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job_compu2char ( Job value ) { return to_char_compu( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job_compq2char ( Job value ) { return to_char_compq( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char jobu2char      ( Job value ) { return to_char_jobu ( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char jobq2char      ( Job value ) { return to_char_jobq ( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char jobu_gejsv2char( Job value ) { return to_char_gejsv( value ); }

[[deprecated("use to_char. To be removed 2025-05.")]]
inline char job_gesvj2char ( Job value ) { return to_char_gesvj( value ); }

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* job2str( Job value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Job char2job( char ch )
{
    Job val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// hseqr
enum class JobSchur : char {
    Eigenvalues  = 'E',
    Schur        = 'S',
};

extern const char* JobSchur_help;

inline char to_char( JobSchur value )
{
    return char( value );
}

inline const char* to_c_string( JobSchur value )
{
    switch (value) {
        case JobSchur::Eigenvalues: return "eigval";
        case JobSchur::Schur:       return "schur";
    }
    return "?";
}

inline std::string to_string( JobSchur value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, JobSchur* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "e" || str_ == "eigval")
        *val = JobSchur::Eigenvalues;
    else if (str_ == "s" || str_ == "schur")
        *val = JobSchur::Schur;
    else
        throw Error( "unknown JobSchur: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char jobschur2char( JobSchur value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* jobschur2str( JobSchur value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline JobSchur char2jobschur( char ch )
{
    JobSchur val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// gees
// todo: generic yes/no
enum class Sort : char {
    NotSorted   = 'N',
    Sorted      = 'S',
};

extern const char* Sort_help;

//--------------------
inline char to_char( Sort value )
{
    return char( value );
}

inline const char* to_c_string( Sort value )
{
    switch (value) {
        case Sort::NotSorted: return "notsorted";
        case Sort::Sorted:    return "sorted";
    }
    return "?";
}

inline std::string to_string( Sort value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Sort* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "n" || str_ == "notsorted")
        *val = Sort::NotSorted;
    else if (str_ == "s" || str_ == "sorted")
        *val = Sort::Sorted;
    else
        throw Error( "unknown Sort: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char sort2char( Sort sort )
{
    return char( sort );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* sort2str( Sort value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Sort char2sort( char ch )
{
    Sort val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// syevx
enum class Range : char {
    All         = 'A',
    Value       = 'V',
    Index       = 'I',
};

extern const char* Range_help;

//--------------------
inline char to_char( Range value )
{
    return char( value );
}

inline const char* to_c_string( Range value )
{
    switch (value) {
        case Range::All:   return "all";
        case Range::Value: return "value";
        case Range::Index: return "index";
    }
    return "?";
}

inline std::string to_string( Range value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Range* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "a" || str_ == "all")
        *val = Range::All;
    else if (str_ == "v" || str_ == "value")
        *val = Range::Value;
    else if (str_ == "i" || str_ == "index")
        *val = Range::Index;
    else
        throw Error( "unknown Range: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char range2char( Range range )
{
    return char( range );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* range2str( Range value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Range char2range( char ch )
{
    Range val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
enum class Vect : char {
    Q           = 'Q',  // orgbr, ormbr
    P           = 'P',  // orgbr, ormbr
    None        = 'N',  // orgbr, ormbr, gbbrd
    Both        = 'B',  // orgbr, ormbr, gbbrd
};

extern const char* Vect_help;

//--------------------
inline char to_char( Vect value )
{
    return char( value );
}

inline const char* to_c_string( Vect value )
{
    switch (value) {
        case Vect::P:    return "p";
        case Vect::Q:    return "q";
        case Vect::None: return "none";
        case Vect::Both: return "both";
    }
    return "?";
}

inline std::string to_string( Vect value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Vect* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "q")
        *val = Vect::Q;
    else if (str_ == "p")
        *val = Vect::P;
    else if (str_ == "n" || str_ == "none")
        *val = Vect::None;
    else if (str_ == "b" || str_ == "both")
        *val = Vect::Both;
    else
        throw Error( "unknown Vect: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char vect2char( Vect value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* vect2str( Vect value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Vect char2vect( char ch )
{
    Vect val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// larfb
enum class Direction : char {
    Forward     = 'F',
    Backward    = 'B',
};

extern const char* Direction_help;

//--------------------
inline char to_char( Direction value )
{
    return char( value );
}

inline const char* to_c_string( Direction value )
{
    switch (value) {
        case Direction::Forward:  return "forward";
        case Direction::Backward: return "backward";
    }
    return "?";
}

inline std::string to_string( Direction value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Direction* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "f" || str_ == "forward")
        *val = Direction::Forward;
    else if (str_ == "b" || str_ == "backward")
        *val = Direction::Backward;
    else
        throw Error( "unknown Direction: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char direction2char( Direction value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* direction2str( Direction value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Direction char2direction( char ch )
{
    Direction val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// larfb
enum class StoreV : char {
    Columnwise  = 'C',
    Rowwise     = 'R',
};

extern const char* StoreV_help;

//--------------------
inline char to_char( StoreV value )
{
    return char( value );
}

inline const char* to_c_string( StoreV value )
{
    switch (value) {
        case StoreV::Columnwise: return "colwise";
        case StoreV::Rowwise:    return "rowwise";
    }
    return "?";
}

inline std::string to_string( StoreV value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, StoreV* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "c" || str_ == "col" || str_ == "colwise"
        || str_ == "columnwise")
        *val = StoreV::Columnwise;
    else if (str_ == "r" || str_ == "row" || str_ == "rowwise")
        *val = StoreV::Rowwise;
    else
        throw Error( "unknown StoreV: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char storev2char( StoreV value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* storev2str( StoreV value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline StoreV char2storev( char ch )
{
    StoreV val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// lascl, laset
enum class MatrixType : char {
    General     = 'G',
    Lower       = 'L',
    Upper       = 'U',
    Hessenberg  = 'H',
    LowerBand   = 'B',
    UpperBand   = 'Q',
    Band        = 'Z',
};

extern const char* MatrixType_help;

//--------------------
inline char to_char( MatrixType value )
{
    return char( value );
}

// This string can't be passed to LAPACK since "band" is reused.
inline const char* to_c_string( MatrixType value )
{
    switch (value) {
        case MatrixType::General:    return "general";
        case MatrixType::Lower:      return "lower";
        case MatrixType::Upper:      return "upper";
        case MatrixType::Hessenberg: return "hessenberg";
        case MatrixType::LowerBand:  return "band-lower";
        case MatrixType::UpperBand:  return "band-upper";
        case MatrixType::Band:       return "band";
    }
    return "?";
}

inline std::string to_string( MatrixType value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, MatrixType* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "g" || str_ == "general")
        *val = MatrixType::General;
    else if (str_ == "l" || str_ == "lower")
        *val = MatrixType::Lower;
    else if (str_ == "u" || str_ == "upper")
        *val = MatrixType::Upper;
    else if (str_ == "h" || str_ == "hessenberg")
        *val = MatrixType::Hessenberg;
    else if (str_ == "b" || str_ == "band-lower")
        *val = MatrixType::LowerBand;
    else if (str_ == "q" || str_ == "band-upper")
        *val = MatrixType::UpperBand;
    else if (str_ == "z" || str_ == "band")
        *val = MatrixType::Band;
    else
        throw Error( "unknown MatrixType: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char matrixtype2char( MatrixType value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* matrixtype2str( MatrixType value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline MatrixType char2matrixtype( char ch )
{
    MatrixType val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// trevc
enum class HowMany : char {
    All           = 'A',
    Backtransform = 'B',
    Select        = 'S',
};

extern const char* HowMany_help;

//--------------------
inline char to_char( HowMany value )
{
    return char( value );
}

inline const char* to_c_string( HowMany value )
{
    switch (value) {
        case HowMany::All:           return "all";
        case HowMany::Backtransform: return "backtransform";
        case HowMany::Select:        return "select";
    }
    return "?";
}

inline std::string to_string( HowMany value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, HowMany* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "a" || str_ == "all")
        *val = HowMany::All;
    else if (str_ == "b" || str_ == "backtransform")
        *val = HowMany::Backtransform;
    else if (str_ == "s" || str_ == "select")
        *val = HowMany::Select;
    else
        throw Error( "unknown HowMany: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char howmany2char( HowMany value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* howmany2str( HowMany value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline HowMany char2howmany( char ch )
{
    HowMany val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// *svx, *rfsx
enum class Equed : char {
    None        = 'N',
    Row         = 'R',
    Col         = 'C',
    Both        = 'B',
    Yes         = 'Y',  // porfsx
};

extern const char* Equed_help;

//--------------------
inline char to_char( Equed value )
{
    return char( value );
}

inline const char* to_c_string( Equed value )
{
    switch (value) {
        case Equed::None: return "none";
        case Equed::Row:  return "row";
        case Equed::Col:  return "col";
        case Equed::Both: return "both";
        case Equed::Yes:  return "yes";
    }
    return "?";
}

inline std::string to_string( Equed value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Equed* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "n" || str_ == "none")
        *val = Equed::None;
    else if (str_ == "r" || str_ == "row")
        *val = Equed::Row;
    else if (str_ == "c" || str_ == "col")
        *val = Equed::Col;
    else if (str_ == "b" || str_ == "both")
        *val = Equed::Both;
    else if (str_ == "y" || str_ == "yes")
        *val = Equed::Yes;
    else
        throw Error( "unknown Equed: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char equed2char( Equed value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* equed2str( Equed value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Equed char2equed( char ch )
{
    Equed val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// *svx
// todo: what's good name for this?
enum class Factored : char {
    Factored    = 'F',
    NotFactored = 'N',
    Equilibrate = 'E',
};

extern const char* Factored_help;

//--------------------
inline char to_char( Factored value )
{
    return char( value );
}

inline const char* to_c_string( Factored value )
{
    switch (value) {
        case Factored::Factored:    return "factored";
        case Factored::NotFactored: return "notfactored";
        case Factored::Equilibrate: return "equilibrate";
    }
    return "?";
}

inline std::string to_string( Factored value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Factored* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "f" || str_ == "factored")
        *val = Factored::Factored;
    else if (str_ == "n" || str_ == "notfactored")
        *val = Factored::NotFactored;
    else if (str_ == "e" || str_ == "equilibrate")
        *val = Factored::Equilibrate;
    else
        throw Error( "unknown Factored: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char factored2char( Factored value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* factored2str( Factored value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Factored char2factored( char ch )
{
    Factored val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// geesx, trsen (job)
enum class Sense : char {
    None        = 'N',
    Eigenvalues = 'E',
    Subspace    = 'V',
    Both        = 'B',
};

extern const char* Sense_help;

//--------------------
inline char to_char( Sense value )
{
    return char( value );
}

// This string can't be passed to LAPACK since it uses "subspace" instead of "V".
inline const char* to_c_string( Sense value )
{
    switch (value) {
        case Sense::None:        return "none";
        case Sense::Eigenvalues: return "eigval";
        case Sense::Subspace:    return "subspace";
        case Sense::Both:        return "both";
    }
    return "?";
}

inline std::string to_string( Sense value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Sense* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "n" || str_ == "none")
        *val = Sense::None;
    else if (str_ == "e" || str_ == "eigval")
        *val = Sense::Eigenvalues;
    else if (str_ == "v" || str_ == "s" || str_ == "subspace")
        *val = Sense::Subspace;
    else if (str_ == "b" || str_ == "both")
        *val = Sense::Both;
    else
        throw Error( "unknown Sense: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char sense2char( Sense value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* sense2str( Sense value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Sense char2sense( char ch )
{
    Sense val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// disna
enum class JobCond : char {
    EigenVec         = 'E',
    LeftSingularVec  = 'L',
    RightSingularVec = 'R',
};

extern const char* JobCond_help;

//--------------------
inline char to_char( JobCond value )
{
    return char( value );
}

inline const char* to_c_string( JobCond value )
{
    switch (value) {
        case JobCond::EigenVec:         return "eigvec";
        case JobCond::LeftSingularVec:  return "left";
        case JobCond::RightSingularVec: return "right";
    }
    return "?";
}

inline std::string to_string( JobCond value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, JobCond* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "e" || str_ == "eigvec")
        *val = JobCond::EigenVec;
    else if (str_ == "l" || str_ == "left")
        *val = JobCond::LeftSingularVec;
    else if (str_ == "r" || str_ == "right")
        *val = JobCond::RightSingularVec;
    else
        throw Error( "unknown JobCond: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char jobcond2char( JobCond value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* jobcond2str( JobCond value )
{
    return to_c_string( value );
}

inline JobCond char2jobcond( char ch )
{
    JobCond val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// {ge,gg}{bak,bal}
enum class Balance : char {
    None        = 'N',
    Permute     = 'P',
    Scale       = 'S',
    Both        = 'B',
};

extern const char* Balance_help;

//--------------------
inline char to_char( Balance value )
{
    return char( value );
}

inline const char* to_c_string( Balance value )
{
    switch (value) {
        case Balance::None:    return "none";
        case Balance::Permute: return "permute";
        case Balance::Scale:   return "scale";
        case Balance::Both:    return "both";
    }
    return "?";
}

inline std::string to_string( Balance value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Balance* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "n" || str_ == "none")
        *val = Balance::None;
    else if (str_ == "p" || str_ == "permute")
        *val = Balance::Permute;
    else if (str_ == "s" || str_ == "scale")
        *val = Balance::Scale;
    else if (str_ == "b" || str_ == "both")
        *val = Balance::Both;
    else
        throw Error( "unknown Balance: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char balance2char( Balance value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* balance2str( Balance value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Balance char2balance( char ch )
{
    Balance val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// stebz, larrd, stein docs
enum class Order : char {
    Block       = 'B',
    Entire      = 'E',
};

extern const char* Order_help;

//--------------------
inline char to_char( Order value )
{
    return char( value );
}

inline const char* to_c_string( Order value )
{
    switch (value) {
        case Order::Block:  return "block";
        case Order::Entire: return "entire";
    }
    return "?";
}

inline std::string to_string( Order value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Order* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "b" || str_ == "block")
        *val = Order::Block;
    else if (str_ == "e" || str_ == "entire")
        *val = Order::Entire;
    else
        throw Error( "unknown Order: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char order2char( Order value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* order2str( Order value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline Order char2order( char ch )
{
    Order val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// check_ortho (LAPACK testing zunt01)
enum class RowCol : char {
    Col = 'C',
    Row = 'R',
};

extern const char* RowCol_help;

//--------------------
inline char to_char( RowCol value )
{
    return char( value );
}

inline const char* to_c_string( RowCol value )
{
    switch (value) {
        case RowCol::Col: return "col";
        case RowCol::Row: return "row";
    }
    return "?";
}

inline std::string to_string( RowCol value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, RowCol* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "c" || str_ == "col")
        *val = RowCol::Col;
    else if (str_ == "r" || str_ == "row")
        *val = RowCol::Row;
    else
        throw Error( "unknown RowCol: " + str );
}

//--------------------
[[deprecated("use to_char. To be removed 2025-05.")]]
inline char rowcol2char( RowCol value )
{
    return char( value );
}

[[deprecated("use to_c_string or to_string. To be removed 2025-05.")]]
inline const char* rowcol2str( RowCol value )
{
    return to_c_string( value );
}

[[deprecated("use from_string. To be removed 2025-05.")]]
inline RowCol char2rowcol( char ch )
{
    RowCol val;
    from_string( std::string( 1, ch ), &val );
    return val;
}

// -----------------------------------------------------------------------------
// check_ortho (LAPACK testing zunt01)
enum class Pivot : char {
    Variable = 'V',
    Top      = 'T',
    Bottom   = 'B',
};

extern const char* Pivot_help;

//--------------------
inline char to_char( Pivot value )
{
    return char( value );
}

inline const char* to_c_string( Pivot value )
{
    switch (value) {
        case Pivot::Variable: return "variable";
        case Pivot::Top:      return "top";
        case Pivot::Bottom:   return "bottom";
    }
    return "?";
}

inline std::string to_string( Pivot value )
{
    return to_c_string( value );
}

inline void from_string( std::string const& str, Pivot* val )
{
    std::string str_ = str;
    std::transform( str_.begin(), str_.end(), str_.begin(), ::tolower );

    if (str_ == "v" || str_ == "variable")
        *val = Pivot::Variable;
    else if (str_ == "t" || str_ == "top")
        *val = Pivot::Top;
    else if (str_ == "b" || str_ == "bottom")
        *val = Pivot::Bottom;
    else
        throw Error( "unknown Pivot: " + str );
}

//------------------------------------------------------------------------------
// For %lld printf-style printing, cast to llong; guaranteed >= 64 bits.
using llong = long long;

}  // namespace lapack

#endif  // LAPACK_UTIL_HH