File: LinearAlgebra.pm

package info (click to toggle)
libpdl-linearalgebra-perl 0.433-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,052 kB
  • sloc: perl: 2,421; ansic: 168; makefile: 6
file content (3326 lines) | stat: -rw-r--r-- 100,489 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
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
package PDL::LinearAlgebra;
use PDL::Ops;
use PDL::Core;
use PDL::Basic qw/sequence/;
use PDL::Primitive qw/which which_both/;
use PDL::Ufunc qw/sumover/;
use PDL::NiceSlice;
use PDL::Slices;
use PDL::LinearAlgebra::Real;
use PDL::LinearAlgebra::Complex;
use PDL::LinearAlgebra::Special qw//;
use PDL::Exporter;
no warnings 'uninitialized';
use constant {
	NO => 0,
	WARN => 1,
	BARF => 2,
};

use strict;

our $VERSION = '0.433';
$VERSION =~ tr/_//d;

@PDL::LinearAlgebra::ISA = qw/PDL::Exporter/;
@PDL::LinearAlgebra::EXPORT_OK = qw/diag issym minv mtriinv msyminv mposinv mdet mposdet mrcond positivise
				mdsvd msvd mgsvd mpinv mlu mhessen mchol mqr mql mlq mrq meigen meigenx
				mgeigen  mgeigenx msymeigen msymeigenx msymgeigen msymgeigenx
				msolve mtrisolve msymsolve mpossolve msolvex msymsolvex mpossolvex
				mrank mlls mllsy mllss mglm mlse tritosym mnorm mgschur mgschurx
				mcrossprod mcond morth mschur mschurx
				NO WARN BARF setlaerror getlaerorr laerror/;
%PDL::LinearAlgebra::EXPORT_TAGS = (Func=>[@PDL::LinearAlgebra::EXPORT_OK]);

my $_laerror = BARF;

########################################################################

=encoding utf8

=head1 NAME

PDL::LinearAlgebra - Linear Algebra utils for PDL

=head1 SYNOPSIS

 use PDL::LinearAlgebra;

 $a = random (100,100);
 ($U, $s, $V) = mdsvd($a);

=head1 DESCRIPTION

This module provides a convenient interface to L<PDL::LinearAlgebra::Real>
and L<PDL::LinearAlgebra::Complex>. Since
Blas and Lapack use a column major ordering scheme some routines here need to transpose matrices before
calling fortran routines and transpose back (see the documentation of each routine). If you need
optimized code use directly  L<PDL::LinearAlgebra::Real> and
L<PDL::LinearAlgebra::Complex>.

=cut

=head1 FUNCTIONS

=head2 setlaerror

=for ref

Sets action type when an error is encountered, returns previous type. Available values are NO, WARN and BARF (predefined constants).
If, for example, in computation of the inverse, singularity is detected,
the routine can silently return values from computation (see manuals),
warn about singularity or barf. BARF is the default value.

=for example

 # h : x -> g(f(x))

 $a = sequence(5,5);
 $err = setlaerror(NO);
 ($b, $info)= f($a);
 setlaerror($err);
 $info ? barf "can't compute h" : return g($b);


=cut

sub setlaerror($){
	my $err = $_laerror;
	$_laerror = shift;
	$err;
}

=head2 getlaerror

=for ref

Gets action type when an error is encountered.

	0 => NO,
	1 => WARN,
	2 => BARF

=cut

sub getlaerror{
	$_laerror;
}

sub laerror{
	return unless $_laerror;
	if ($_laerror < 2){
		warn "$_[0]\n";
	}
	else{
		barf "$_[0]\n";
	}
}

=head2 t

=for usage

 PDL = t(PDL, SCALAR(conj))
 conj : Conjugate Transpose = 1 | Transpose = 0, default = 0;

=for ref

Convenient function for transposing real or complex 2D array(s).
For complex data, if conj is true returns conjugate transposed array(s) and doesn't support dataflow.
Supports threading.

=cut

sub PDL::dims_internal {0}
sub PDL::dims_internal_values {()}
sub PDL::_similar {
  my @di_vals = $_[0]->dims_internal_values;
  my ($m, @vdims) = @_;
  ref($m)->new_from_specification($m->type, @di_vals, @vdims);
}
sub PDL::_similar_null { ref($_[0])->null }
sub _complex_null {
  PDL->null
}
sub _ecplx {
  my ($re, $im) = @_;
  $re = PDL->topdl($re);
  return $re if !$re->type->real;
  Carp::confess("Usage: _ecplx(re,im) or (complex)") if !defined $im;
  $im = PDL->topdl($im);
  $re->czip($im);
}
sub PDL::_is_complex { !$_[0]->type->real }
sub PDL::_norm {
	my ($m, $real, $trans) = @_;
	# If trans == true => transpose output matrix
	# If real == true => rotate (complex as a vector)
	#		     such that max abs will be real
	my $ret = PDL::LinearAlgebra::Complex::cnrm2($m);
	return ($trans ? $m->t/$ret->dummy(0)->t : $m/$ret->dummy(0))->reshape(-1) if !$real;
	$m = ($m/$ret->dummy(0))->reshape(-1);
	my $index = $m->abs->maximum_ind;
	my $scale = $m->mv(0,-1)->index($index)->mv(-1,0);
	$scale = $scale->conj/$scale->abs;
	return $trans ? $m->t*$scale->dummy(2) : $m*$scale->dummy(2)->t;
}

=head2 issym

=for usage

 PDL = issym(PDL, SCALAR|PDL(tol),SCALAR(hermitian))
 tol : tolerance value, default: 1e-8 for double else 1e-5
 hermitian : Hermitian = 1 | Symmetric = 0, default = 0;

=for ref

Checks symmetricity/Hermitianicity of matrix.
Supports threading.

=cut

sub _2d_array {
  my @dims = $_[0]->dims;
  my $d = $_[0]->dims_internal;
  barf("Require 2D array(s)") unless @dims >= 2+$d;
}
sub _square {
  &_2d_array;
  my @dims = $_[0]->dims;
  my $d = $_[0]->dims_internal;
  barf("Require square array(s)") unless $dims[$d] == $dims[$d+1];
}
sub _square_same {
  my $d = $_[0]->dims_internal;
  my @adims = $_[0]->dims;
  my @bdims = $_[1]->dims;
  barf("Require square matrices of same order")
    unless( $adims[$d] == $adims[$d+1] && $bdims[$d] == $bdims[$d+1] && $adims[$d] == $bdims[$d]);
}
sub _matrices_match {
  my $d = $_[0]->dims_internal;
  my @adims = $_[0]->dims;
  my @bdims = $_[1]->dims;
  barf("Require right hand side array(s) B with number".
    " of row equal to number of columns of A")
    unless @adims >= 2+$d && @bdims >= 2+$d && $bdims[1+$d] == $adims[$d];
}
sub _matrices_matchcolumns {
  my $di = $_[0]->dims_internal;
  my @adims = $_[0]->dims;
  my @bdims = $_[1]->dims;
  barf("Require 2 matrices with equal number of columns")
    unless( ((@adims >= 2+$di && @bdims >= 2+$di)) &&
    $adims[$di] == $bdims[$di]);
}
sub _matrices_matchrows {
  my $d = $_[0]->dims_internal;
  my @adims = $_[0]->dims;
  my @bdims = $_[1]->dims;
  barf("Require a 2D right hand side matrix B with number".
    " of rows equal to number of rows of A")
    unless @adims >= 2+$d && @bdims >= 2+$d && $bdims[1+$d] == $adims[1+$d];
}
sub _same_dims {
  my $d = $_[0]->dims_internal;
  my @adims = $_[0]->dims;
  my @bdims = $_[1]->dims;
  barf("Require arrays with equal number of dimensions") if @adims != @bdims;
}
sub _error {
  my ($info, $msg) = @_;
  return unless $info->max > 0 && $_laerror;
  my @list = (which($info > 0)+1)->list;
  laerror(sprintf $msg . ": \$info = $info", "@list");
}
sub _error_schur {
  my ($info, $select_func, $N, $func, $algo) = @_;
  return unless $info->max > 0 && $_laerror;
  my $index = which((($info > 0)+($info <=$N))==2);
  if (!$index->isempty) {
    laerror("$func: The $algo algorithm failed to converge for matrix (PDL(s) @{[$index->list]}): \$info = $info");
    print "Returning converged eigenvalues\n";
  }
  return if !$select_func;
  if (!($index = which($info == $N+1))->isempty) {
    if ($algo eq 'QR') {
      laerror("$func: The eigenvalues could not be reordered because some\n".
	"eigenvalues were too close to separate (the problem".
	" is very ill-conditioned) for PDL(s) @{[$index->list]}: \$info = $info");
    } else {
      laerror("$func: Error in hgeqz for matrix (PDL(s) @{[$index->list]}): \$info = $info");
    }
  }
  if (!($index = which($info == $N+2))->isempty) {
    warn("$func: The Schur form no longer satisfy select_func = 1\n because of roundoff".
      " or underflow (PDL(s) @{[$index->list]})\n");
  }
}
sub PDL::_wrap_select_func {
  my ($m, $select_func) = @_;
  return $select_func if !defined $select_func or $m->_is_complex;
  sub { &$select_func(_ecplx(@_[0,1]), @_>2 ? PDL->topdl($_[2]) : ()) };
}

*issym = \&PDL::issym;
sub PDL::issym {
	&_square;
	my ($m, $tol, $conj) = @_;
	$tol //= ($m->type >= double) ? 1e-8 : 1e-5;
	$m = $m - $m->t($conj);
	my ($min,$max) = PDL::Ufunc::minmaximum($m);
	$min = $min->minimum;
	$max = $max->maximum;
	return  (((abs($max) > $tol) + (abs($min) > $tol)) == 0);
}

=head2 diag

=for ref

Returns i-th diagonal if matrix in entry or matrix with i-th diagonal
with entry. I-th diagonal returned flows data back&forth.
Can be used as lvalue subs if your perl supports it.
Supports threading.

=for usage

 PDL = diag(PDL, SCALAR(i), SCALAR(vector)))
 i	: i-th diagonal, default = 0
 vector	: create diagonal matrices by threading over row vectors, default = 0

=for example

 my $a = random(5,5);
 my $diag  = diag($a,2);
 # If your perl support lvaluable subroutines.
 $a->diag(-2) .= pdl(1,2,3);
 # Construct a (5,5,5) PDL (5 matrices) with
 # diagonals from row vectors of $a
 $a->diag(0,1)

=cut

*diag = \&PDL::diag;
sub PDL::diag {
	my $di = $_[0]->dims_internal;
	my @diag_args = ($di, $di+1);
	my $slice_prefix = ',' x $di;
	my ($a,$i, $vec) = @_;
	my $z;
	my @dims = $a->dims;
	my $diag = ($i < 0) ? -$i : $i ;
	if (@dims == $di+1 || $vec){
		my $dim = $dims[0];
		my $zz = $dim + $diag;
		my $v = $z = $a->_similar($zz,$zz,@dims[$di+1..$#dims]);
		$v = ($i < 0) ? $v->slice("$slice_prefix:@{[$dim-1]},$diag:") : $v->slice("$slice_prefix$diag:,:@{[$dim-1]}") if $i;
		$v->diagonal(@diag_args) .= $a;
	}
	elsif($i < 0){
		$z = $a->slice("$slice_prefix:-@{[$diag+1]} , $diag:")->diagonal(@diag_args);
	}
	elsif($i){
		$z = $a->slice("$slice_prefix$diag:, :-@{[$diag+1]}")->diagonal(@diag_args);
	}
	else{$z = $a->diagonal(@diag_args);}
	$z;
}
use attributes 'PDL', \&PDL::diag, 'lvalue';

=head2 tritosym

=for ref

Returns symmetric or Hermitian matrix from lower or upper triangular matrix.
Supports inplace and threading.

=for usage

 PDL = tritosym(PDL, SCALAR(uplo), SCALAR(conj))
 uplo : UPPER = 0 | LOWER = 1, default = 0
 conj : Hermitian = 1 | Symmetric = 0, default = 0;

=for example

 # Assume $a is symmetric triangular
 my $a = random(10,10);
 my $b = tritosym($a);

=cut

*tritosym = \&PDL::tritosym;
sub PDL::tritosym {
	&_square;
	my ($m, $upper, $conj) = @_;
	my $b = $m->is_inplace ? $m->t : $m->_similar_null;
	($conj ? $m->conj : $m)->tricpy($upper, $b);
	$m->tricpy($upper, $b->t) unless (!$conj && $m->is_inplace(0));
	$b->im->diagonal(0,1) .= 0 if $conj;
	$b;
}

=head2 positivise

=for ref

Returns entry pdl with changed sign by row so that average of positive sign > 0.
In other words threads among dimension 1 and row  =  -row if sum(sign(row)) < 0.
Only makes sense for real ndarrays.
Works inplace.

=for example

 my $a = random(10,10);
 $a -= 0.5;
 $a->xchg(0,1)->inplace->positivise;

=cut

*positivise = \&PDL::positivise;
sub PDL::positivise{
	my $m = shift->new_or_inplace;
	my $tmp;
	$tmp = $m->dice('X', which(($m->lt(0,0)->sumover > ($m->dim(0)/2))>0));
	$tmp->inplace->mult(-1,0);
	$m;
}

=head2 mcrossprod

=for ref

Computes the cross-product of two matrix: A' x  B.
If only one matrix is given, takes B to be the same as A.
Supports threading.
Uses L<crossprod|PDL::LinearAlgebra::Real/crossprod> or L<ccrossprod|PDL::LinearAlgebra::Complex/ccrossprod>.

=for usage

 PDL = mcrossprod(PDL(A), (PDL(B))

=for example

 my $a = random(10,10);
 my $crossproduct = mcrossprod($a);

=cut

sub PDL::_call_method {
  my ($m, $method, @args) = @_;
  $method = [$method, "c$method"] if !ref $method;
  $method = $method->[!$m->type->real ? 1 : 0];
  $m->$method(@args);
}
*mcrossprod = \&PDL::mcrossprod;
sub PDL::mcrossprod {
	&_2d_array;
	my($a, $b) = @_;
	$b = $a unless defined $b;
	$a->_call_method('crossprod', $b);
}

=head2 mrank

=for ref

Computes the rank of a matrix, using a singular value decomposition,
returning a Perl scalar.
from Lapack.

=for usage

 SCALAR = mrank(PDL, SCALAR(TOL))
 TOL:	tolerance value, default : mnorm(dims(PDL),'inf') * mnorm(PDL) * EPS

=for example

 my $a = random(10,10);
 my $b = mrank($a, 1e-5);

=cut

*mrank = \&PDL::mrank;

sub PDL::mrank {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my($m, $tol) = @_;
	my(@dims) = $m->dims;
	my $err = setlaerror(NO);
	# Sometimes mdsvd bugs for  float (SGEBRD)
	# ($sv, $info) = $m->msvd(0, 0);
	my ($sv, $info) = $m->mdsvd(0);
	setlaerror($err);
	barf("mrank: SVD algorithm did not converge\n") if $info;
	unless (defined $tol){
		$tol =  ($dims[$di+1] > $dims[$di] ? $dims[$di+1] : $dims[$di]) * $sv((0)) * lamch(3);
	}
	(which($sv > $tol))->dim(0);
}

=head2 mnorm

=for ref

Computes norm of real or complex matrix
Supports threading.

=for usage

 PDL(norm) = mnorm(PDL, SCALAR(ord));
 ord :
	0|'inf' : Infinity norm
	1|'one' : One norm
	2|'two'	: norm 2 (default)
	3|'fro' : frobenius norm

=for example

 my $a = random(10,10);
 my $norm = mnorm($a);

=cut

my %norms = (inf=>0, one=>1, two=>2, fro=>3);
my %norm2arg = (0=>1, 1=>2, 3=>3);
*mnorm = \&PDL::mnorm;
sub PDL::mnorm {
	my ($m, $ord) = @_;
	$ord //= 2;
	$ord = $norms{$ord} if exists $norms{$ord};
	return $m->_call_method('lange', $norm2arg{$ord}) if exists $norm2arg{$ord};
	my $err = setlaerror(NO);
	my ($sv, $info) = $m->msvd(0, 0);
	setlaerror($err);
	_error($info, "mnorm: SVD algorithm did not converge for matrix (PDL(s) %s");
	$sv->slice('(0)')->reshape(-1)->sever;
}

=head2 mdet

=for ref

Computes determinant of a general square matrix using LU factorization.
Supports threading.
Uses L<getrf|PDL::LinearAlgebra::Real/getrf> or L<cgetrf|PDL::LinearAlgebra::Complex/cgetrf>
from Lapack.

=for usage

 PDL(determinant) = mdet(PDL);

=for example

 my $a = random(10,10);
 my $det = mdet($a);

=cut

*mdet = \&PDL::mdet;
sub PDL::mdet {
	&_square;
	my $di = $_[0]->dims_internal;
	my $m_orig = my $m = shift->copy;
	$m->_call_method('getrf', my $ipiv = null, my $info = null);
	$m = $m->diagonal($di,$di+1);
	$m = $m->prodover;
	$m = $m * ((PDL::Ufunc::sumover(sequence($ipiv->dim(0))->plus(1,0) != $ipiv)%2)*(-2)+1);
	$info = which($info != 0);
	$m->flat->index($info) .= 0 if !$info->isempty;
	$m;
}

=head2 mposdet

=for ref

Compute determinant of a symmetric or Hermitian positive definite square matrix using Cholesky factorization.
Supports threading.
Uses L<potrf|PDL::LinearAlgebra::Real/potrf> or L<cpotrf|PDL::LinearAlgebra::Complex/cpotrf> from Lapack.

=for usage

 (PDL, PDL) = mposdet(PDL, SCALAR)
 SCALAR : UPPER = 0 | LOWER = 1, default = 0

=for example

 my $a = random(10,10);
 my $det = mposdet($a);

=cut

*mposdet = \&PDL::mposdet;
sub PDL::mposdet {
	&_square;
	my ($m, $upper) = @_;
	$m = $m->copy;
	$m->_call_method('potrf', $upper, my $info = null);
	_error($info, "mposdet: Matrix (PDL(s) %s) is/are not positive definite (after potrf factorization)");
	$m = $m->re if $m->_is_complex;
	$m = $m->diagonal(0,1)->prodover->pow(2);
	return wantarray ? ($m, $info) : $m;
}

=head2 mcond

=for ref

Computes the condition number (two-norm) of a general matrix.

The condition number in two-n is defined:

	norm (a) * norm (inv (a)).

Uses a singular value decomposition.
Supports threading.

=for usage

 PDL = mcond(PDL)

=for example

 my $a = random(10,10);
 my $cond = mcond($a);

=cut

*mcond = \&PDL::mcond;
sub PDL::mcond {
	&_2d_array;
	my $m = shift;
	my @dims = $m->dims;
	my $err = setlaerror(NO);
	my ($sv, $info) = $m->msvd(0, 0);
	setlaerror($err);
	_error($info, "mcond: Algorithm did not converge for matrix (PDL(s) %s)");
	my $temp = $sv->slice('(0)');
        my $ret = $temp/$sv->((-1));
	$info = $ret->flat->index(which($temp == 0));
	$info .= inf() unless $info->isempty;
	return $ret;
}

=head2 mrcond

=for ref

Estimates the reciprocal condition number of a
general square matrix using LU factorization
in either the 1-norm or the infinity-norm.

The reciprocal condition number is defined:

	1/(norm (a) * norm (inv (a)))

Supports threading.
Works on transposed array(s)

=for usage

 PDL = mrcond(PDL, SCALAR(ord))
 ord :
	0 : Infinity norm (default)
	1 : One norm

=for example

 my $a = random(10,10);
 my $rcond = mrcond($a,1);

=cut

*mrcond = \&PDL::mrcond;
sub PDL::mrcond {
	&_square;
	my ($m,$anorm) = @_;
	$anorm = 0 unless defined $anorm;
	$_ = null for my ($ipiv, $info, $rcond);
	my $norm = $m->mnorm($anorm);
	$m = $m->t->copy();
	$m->_call_method('getrf', $ipiv, $info);
	_error($info, "mrcond: Factor(s) U (PDL(s) %s) is/are singular (after getrf factorization)");
	$m->_call_method('gecon',$anorm,$norm,$rcond,$info);
	return wantarray ? ($rcond, $info) : $rcond;
}

=head2 morth

=for ref

Returns an orthonormal basis of the range space of matrix A.

=for usage

 PDL = morth(PDL(A), SCALAR(tol))
 tol : tolerance for determining rank, default: 1e-8 for double else 1e-5

=for example

 my $a = sequence(10,10);
 my $ortho = morth($a, 1e-8);

=cut

*morth = \&PDL::morth;

sub PDL::morth {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my ($m, $tol) = @_;
	$tol =  (defined $tol) ? $tol  : ($m->type == double) ? 1e-8 : 1e-5;
	(my $u, my $s, undef, my $info) = $m->mdsvd;
	barf("morth: SVD algorithm did not converge\n") if $info;
	my $rank = (which($s > $tol))->dim(0) - 1;
	$rank < 0 ? $m->_similar_null : $u->slice("$slice_prefix:$rank,")->sever;
}

=head2 mnull

=for ref

Returns an orthonormal basis of the null space of matrix A.
Works on transposed array.

=for usage

 PDL = mnull(PDL(A), SCALAR(tol))
 tol : tolerance for determining rank, default: 1e-8 for double else 1e-5

=for example

 my $a = sequence(10,10);
 my $null = mnull($a, 1e-8);

=cut

*mnull = \&PDL::mnull;

sub PDL::mnull {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my ($m, $tol) = @_;
	my @dims = $m->dims;
	$tol //= ($m->type == double) ? 1e-8 : 1e-5;
	(undef, my $s, my $v, my $info) = $m->mdsvd;
	barf("mnull: SVD algorithm did not converge\n") if $info;
	#TODO: USE TRANSPOSED A
	my $rank = (which($s > $tol))->dim(0);
	$rank < $dims[$di] ? $v->t->slice("$slice_prefix$rank:")->sever : $m->_similar_null;
}

=head2 minv

=for ref

Computes inverse of a general square matrix using LU factorization. Supports inplace and threading.
Uses L<getrf|PDL::LinearAlgebra::Real/getrf> and L<getri|PDL::LinearAlgebra::Real/getri>
or L<cgetrf|PDL::LinearAlgebra::Complex/cgetrf> and L<cgetri|PDL::LinearAlgebra::Complex/cgetri>
from Lapack and returns C<inverse, info> in array context.

=for usage

 PDL(inv)  = minv(PDL)

=for example

 my $a = random(10,10);
 my $inv = minv($a);

=cut

*minv = \&PDL::minv;
sub PDL::minv {
	&_square;
	my $m = shift->new_or_inplace;
	$_ = null for my ($ipiv, $info);
	$m->_call_method('getrf', $ipiv, $info);
	_error($info, "minv: Factor(s) U (PDL(s) %s) is/are singular (after getrf factorization)");
	$m->_call_method('getri', $ipiv, $info);
	return wantarray ? ($m, $info) : $m;
}

=head2 mtriinv

=for ref

Computes inverse of a triangular matrix. Supports inplace and threading.
Uses L<trtri|PDL::LinearAlgebra::Real/trtri> or L<ctrtri|PDL::LinearAlgebra::Complex/ctrtri> from Lapack.
Returns C<inverse, info> in array context.

=for usage

 (PDL, PDL(info))) = mtriinv(PDL, SCALAR(uplo), SCALAR|PDL(diag))
 uplo : UPPER = 0 | LOWER = 1, default = 0
 diag : UNITARY DIAGONAL = 1, default = 0

=for example

 # Assume $a is upper triangular
 my $a = random(10,10);
 my $inv = mtriinv($a);

=cut

*mtriinv = \&PDL::mtriinv;
sub PDL::mtriinv{
	&_square;
	my $m = shift->new_or_inplace;
	my $upper = @_ ? (1 - shift)  : pdl (long,1);
	my $diag = shift;
	my $info = PDL->null;
	$m->_call_method('trtri', $upper, $diag, $info);
	_error($info, "mtriinv: Matrix (PDL(s) %s) is/are singular");
	return wantarray ? ($m, $info) : $m;
}

=head2 msyminv

=for ref

Computes inverse of a symmetric square matrix using the Bunch-Kaufman diagonal pivoting method.
Supports inplace and threading.
Uses L<sytrf|PDL::LinearAlgebra::Real/sytrf> and L<sytri|PDL::LinearAlgebra::Real/sytri> or
L<csytrf|PDL::LinearAlgebra::Complex/csytrf> and L<csytri|PDL::LinearAlgebra::Complex/csytri>
from Lapack and returns C<inverse, info> in array context.

=for usage

 (PDL, (PDL(info))) = msyminv(PDL, SCALAR|PDL(uplo))
 uplo : UPPER = 0 | LOWER = 1, default = 0

=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my $inv = msyminv($a);

=cut

*msyminv = \&PDL::msyminv;
sub PDL::msyminv {
	&_square;
	my $di = $_[0]->dims_internal;
	my $m = shift->new_or_inplace;
	my $upper = @_ ? (1 - shift)  : pdl (long,1);
	$m->_call_method('sytrf', $upper, my $ipiv=null, my $info=null);
	_error($info, "msyminv: Block diagonal matrix D (PDL(s) %s) is/are singular (after sytrf factorization)");
	$m->_call_method('sytri',$upper,$ipiv,$info);
	$m = $m->t->tritosym($upper, 0);
	return wantarray ? ($m, $info) : $m;
}

=head2 mposinv

=for ref

Computes inverse of a symmetric positive definite square matrix using Cholesky factorization.
Supports inplace and threading.
Uses L<potrf|PDL::LinearAlgebra::Real/potrf> and L<potri|PDL::LinearAlgebra::Real/potri> or
L<cpotrf|PDL::LinearAlgebra::Complex/cpotrf> and L<cpotri|PDL::LinearAlgebra::Complex/cpotri>
from Lapack and returns C<inverse, info> in array context.

=for usage

 (PDL, (PDL(info))) = mposinv(PDL, SCALAR|PDL(uplo))
 uplo : UPPER = 0 | LOWER = 1, default = 0

=for example

 # Assume $a is symmetric positive definite
 my $a = random(10,10);
 $a = $a->crossprod($a);
 my $inv = mposinv($a);

=cut

*mposinv = \&PDL::mposinv;
sub PDL::mposinv {
	&_square;
	my $di = $_[0]->dims_internal;
	my $m = shift->new_or_inplace;
	my $upper = @_ ? (1 - shift)  : pdl (long,1);
	$m->_call_method('potrf', $upper, my $info=null);
	_error($info, "mposinv: matrix (PDL(s) %s) is/are not positive definite (after potrf factorization)");
	$m->_call_method('potri', $upper, $info);
	return wantarray ? ($m, $info) : $m;
}

=head2 mpinv

=for ref

Computes pseudo-inverse (Moore-Penrose) of a general matrix.
Works on transposed array.

=for usage

 PDL(pseudo-inv)  = mpinv(PDL, SCALAR(tol))
 TOL:	tolerance value, default : mnorm(dims(PDL),'inf') * mnorm(PDL) * EPS

=for example

 my $a = random(5,10);
 my $inv = mpinv($a);

=cut

*mpinv = \&PDL::mpinv;
sub PDL::mpinv{
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my ($m, $tol) = @_;
	my @dims = $m->dims;
	my $err = setlaerror(NO);
	#TODO: don't transpose
	my ($u, $s, $v, $info) = $m->mdsvd(2);
	setlaerror($err);
	_error($info, "mpinv: SVD algorithm did not converge (PDL %s)");
	unless (defined $tol){
		$tol =  ($dims[$di+1] > $dims[$di] ? $dims[$di+1] : $dims[$di]) * $s((0)) * lamch(3);
	}
	my ($ind, $cind) = which_both( $s > $tol );
	$s->index($cind) .= 0 if defined $cind;
	$s->index($ind)  .= 1/$s->index($ind) ;
	$ind = ($v->t * ($m->_is_complex ? $s->r2C : $s)) x $u->t;
	return wantarray ? ($ind, $info) : $ind;
}

=head2 mlu

=for ref

Computes LU factorization.
Uses L<getrf|PDL::LinearAlgebra::Real/getrf> or L<cgetrf|PDL::LinearAlgebra::Complex/cgetrf>
from Lapack and returns L, U, pivot and info.
Works on transposed array.

=for usage

 (PDL(l), PDL(u), PDL(pivot), PDL(info)) = mlu(PDL)

=for example

 my $a = random(10,10);
 ($l, $u, $pivot, $info) = mlu($a);

=cut

*mlu = \&PDL::mlu;

sub PDL::mlu {
	my $di = $_[0]->dims_internal;
	&_2d_array;
	my $m = shift;
	my @dims = $m->dims;
        $m = $m->copy;
	$m->t->_call_method('getrf',my $ipiv=null,my $info = null);
	if($info > 0) {
		$info--;
		laerror("mlu: Factor U is singular: U($info,$info) = 0 (after cgetrf factorization)");
		return $m, $m, $ipiv, $info;
	}
	my $u = $m->mtri;
	my $l = $m->mtri(1);
	my $slice_prefix = ',' x $di;
	my $smallerm1 = ($dims[$di] < $dims[$di+1] ? $dims[$di] : $dims[$di+1]) - 1;
	if ($dims[$di+1] > $dims[$di]) {
		$u = $u->slice("$slice_prefix,:$smallerm1")->sever;
		$l->slice("$slice_prefix :$smallerm1, :$smallerm1")->diagonal($di,$di+1) .= 1;
	} else {
		$l = $l->slice("$slice_prefix:$smallerm1")->sever if $dims[$di+1] < $dims[$di];
		$l->diagonal($di,$di+1) .= 1;
	}
	$l, $u, $ipiv, $info;
}

=head2 mchol

=for ref

Computes Cholesky decomposition of a symmetric matrix also known as symmetric square root.
If inplace flag is set, overwrite  the leading upper or lower triangular part of A else returns
triangular matrix. Returns C<cholesky, info> in array context.
Supports threading.
Uses L<potrf|PDL::LinearAlgebra::Real/potrf> or L<cpotrf|PDL::LinearAlgebra::Complex/cpotrf> from Lapack.

=for usage

 PDL(Cholesky) = mchol(PDL, SCALAR)
 SCALAR : UPPER = 0 | LOWER = 1, default = 0

=for example

 my $a = random(10,10);
 $a = crossprod($a, $a);
 my $u  = mchol($a);

=cut

*mchol = \&PDL::mchol;
sub PDL::mchol {
	&_square;
	my $di = $_[0]->dims_internal;
	my($m, $upper) = @_;
	my(@dims) = $m->dims;
	$m = $m->mtri($upper) unless $m->is_inplace(0);
	@dims = @dims[2+$di..$#dims];
	my $uplo =  1 - $upper;
	$m->_call_method('potrf',$uplo,my $info=null);
	_error($info, "mchol: matrix (PDL(s) %s) is/are not positive definite (after potrf factorization)");
	return wantarray ? ($m, $info) : $m;
}

=head2 mhessen

=for ref

Reduces a square matrix to Hessenberg form H and orthogonal matrix Q.

It reduces a general matrix A to upper Hessenberg form H by an orthogonal
similarity transformation:

	Q' x A x Q = H

or

	A = Q x H x Q'

Uses L<gehrd|PDL::LinearAlgebra::Real/gehrd> and L<orghr|PDL::LinearAlgebra::Real/orghr> or
L<cgehrd|PDL::LinearAlgebra::Complex/cgehrd> and L<cunghr|PDL::LinearAlgebra::Complex/cunghr>
from Lapack and returns C<H> in scalar context else C<H> and C<Q>.
Works on transposed array.

=for usage

 (PDL(h), (PDL(q))) = mhessen(PDL)

=for example

 my $a = random(10,10);
 ($h, $q) = mhessen($a);

=cut

*mhessen = \&PDL::mhessen;
sub PDL::mhessen {
	&_square;
	my $di = $_[0]->dims_internal;
	my @diag_args = ($di, $di+1);
	my $slice_arg = (',' x $di) . ":-2, 1:";
	my $m = shift;
	my(@dims) = $m->dims;
	$m = $m->t->copy;
	$m->_call_method('gehrd',1,$dims[$di], my $tau = $m->_similar($dims[$di]-1),my $info = null);
	(my $q = $m->copy)->_call_method(['orghr','cunghr'], 1, $dims[$di], $tau, $info) if wantarray;
	my $h = ($m = $m->t)->mtri;
	$h->slice($slice_arg)->diagonal(@diag_args) .= $m->slice($slice_arg)->diagonal(@diag_args);
	wantarray ? return ($h, $q->t->sever) : $h;
}

=head2 mschur

=for ref

Computes Schur form, works inplace.

	A = Z x T x Z'

Supports threading for unordered eigenvalues.
Uses L<gees|PDL::LinearAlgebra::Real/gees> or L<cgees|PDL::LinearAlgebra::Complex/cgees>
from Lapack and returns schur(T) in scalar context.
Works on transposed array(s).

=for usage

 ( PDL(schur), (PDL(eigenvalues), (PDL(left schur vectors), PDL(right schur vectors), $sdim), $info) ) = mschur(PDL(A), SCALAR(schur vector),SCALAR(left eigenvector), SCALAR(right eigenvector),SCALAR(select_func), SCALAR(backtransform), SCALAR(norm))
 schur vector	     : Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 left eigenvector    : Left eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right eigenvector   : Right eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 select_func	     : Select_func is used to select eigenvalues to sort
		       to the top left of the Schur form.
		       An eigenvalue is selected if PerlInt select_func(w) is true;
		       (the inputs are converted to complex ndarrays for you)
		       Note that a selected complex eigenvalue may no longer
		       satisfy select_func(w) = 1 after ordering, since
		       ordering may change the value of complex eigenvalues
		       (especially if the eigenvalue is ill-conditioned).
		       All eigenvalues/vectors are selected if select_func is undefined.
 backtransform	     : Whether or not backtransforms eigenvectors to those of A.
		       Only supported if schur vectors are computed, default = 1.
 norm                : Whether or not computed eigenvectors are normalized to have Euclidean norm equal to
		       1 and largest component real, default = 1

 Returned values     :
		       Schur form T (SCALAR CONTEXT),
		       eigenvalues,
		       Schur vectors (Z) if requested,
		       left eigenvectors if requested
		       right eigenvectors if requested
		       sdim: Number of eigenvalues selected if select_func is defined.
		       info: Info output from gees/cgees.

=for example

 my $a = random(10,10);
 my $schur  = mschur($a);
 sub select{
	my $w = shift;
	# select "discrete time" eigenspace
	return $w->Cabs < 1 ? 1 : 0;
 }
 my ($schur,$eigen,$svectors,$evectors)  = mschur($a,1,1,0,\&select);

=cut

sub _eigen_extract {
  my ($jobvl, $jobvr, $vl, $vr, @w) = @_;
  return ($w[0], $vl, $vr) if @w == 1;
  my $w;
  ($w, $vl) = cplx_eigen(@w, $vl, 1) if $jobvl;
  ($w, $vr) = cplx_eigen(@w, $vr, 1) if $jobvr;
  $w = _ecplx(@w) if !defined $w;
  ($w, $vl, $vr);
}

sub _eigen_one {
  my ($mm, $select_func, $jobv, $jobvl, $jobvr, $mult, $norm, $mdim, $sdim, @w) = @_;
  my $job = $jobvr && $jobvl ? undef : $jobvl ? 2 : 1;
  my $is_mult = $jobvl == 1 || $jobvr == 1 || $mult;
  $_ = $mm->_similar_null for my ($vl, $vr);
  $mult = ($select_func && !$is_mult) ? 2 : !$jobv ? 0 : $mult;
  my $sel = ($select_func && !$is_mult) ? zeroes($mdim) : undef;
  $sel(:($sdim-1)) .= 1 if defined $sel;
  $mm->_call_method('trevc', $job, $mult, $sel, $vl, $vr, $sdim, my $infos=null);
  @w = map $is_mult || !$select_func ? $_ : $_(:($sdim-1)), @w if @w == 2;
  my @ret;
  for ([$jobvr,$vr], [$jobvl,$vl]) {
    unshift(@ret, undef), next if !$_->[0];
    my $val;
    if (@w == 2) {
      (undef,$val) = cplx_eigen(@w,$norm?($_->[1],1):($_->[1]->t,0));
    } else {
      $val = $_->[1];
    }
    unshift(@ret, $norm ? $val->_norm(1,1) : $val), next if !$is_mult or !$select_func;
    my $di = $val->dims_internal;
    my $slice_prefix = ',' x $di;
    $val = $val->slice("$slice_prefix,:@{[$sdim-1]}")->sever if $_->[0] == 2;
    $val = $val->_norm(1,1) if $norm;
    unshift @ret, $val;
  }
  @ret;
}

*mschur = \&PDL::mschur;
sub PDL::mschur {
	&_square;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my ($m, $jobv, $jobvl, $jobvr, $select_func, $mult, $norm) = @_;
	my @dims = $m->dims;
	barf("mschur: threading not supported for selected vectors")
		if $select_func && @dims > 2+$di
		  && (grep $_ == 2, $jobv, $jobvl, $jobvr);
	$mult //= 1;
	$norm //= 1;
	$jobv = $jobvl = $jobvr = 0 unless wantarray;
	my $mm = $m->new_or_inplace->t;
	my $v = $m->_similar_null;
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	my $select_f = $m->_wrap_select_func($select_func);
	$mm->_call_method('gees',
		$jobv, $select_func ? 1 : 0, @w,
		$v, my $sdim = null, my $info = null, $select_f
	);
	_error_schur($info, $select_func, $dims[$di], 'mschur', 'QR');
	my @ret = !$select_func || $sdim ? () : map _complex_null(), grep $_ == 2, $jobvl, $jobvr;
	push @ret, $sdim if $select_func;
	$_ = 0 for grep $select_func && $_ == 2 && !$sdim, $jobvl, $jobvr;
	my $w = @w == 2 ? _ecplx(@w) : @w[0];
	if ($jobvl || $jobvr){
		unshift @ret, grep defined, _eigen_one(
		  $mm, $select_func, $jobv, $jobvl, $jobvr,
		  $mult, $norm, $dims[$di+1], $sdim, @w
		);
	}
	if ($jobv == 2 && $select_func) {
		unshift @ret, $sdim > 0 ? $v->t->slice("$slice_prefix:@{[$sdim-1]}")->sever : $m->_similar_null;
	}
	elsif($jobv){
		unshift @ret, $v->t->sever;
	}
	$m = $mm->t->sever unless $m->is_inplace(0);
	return wantarray ? ($m, $w, @ret, $info) : $m;
}

=head2 mschurx

=for ref

Computes Schur form, works inplace.
Uses L<geesx|PDL::LinearAlgebra::Real/geesx> or L<cgeesx|PDL::LinearAlgebra::Complex/cgeesx>
from Lapack and returns schur(T) in scalar context.
Works on transposed array.

=for usage

 ( PDL(schur) (,PDL(eigenvalues))  (, PDL(schur vectors), HASH(result)) ) = mschurx(PDL, SCALAR(schur vector), SCALAR(left eigenvector), SCALAR(right eigenvector),SCALAR(select_func), SCALAR(sense), SCALAR(backtransform), SCALAR(norm))
 schur vector	     : Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 left eigenvector    : Left eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right eigenvector   : Right eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 select_func         : Select_func is used to select eigenvalues to sort
		       to the top left of the Schur form.
		       An eigenvalue is selected if PerlInt select_func(w) is true;
		       (the inputs are converted to complex ndarrays for you)
		       Note that a selected complex eigenvalue may no longer
		       satisfy select_func(w) = 1 after ordering, since
		       ordering may change the value of complex eigenvalues
		       (especially if the eigenvalue is ill-conditioned).
		       All  eigenvalues/vectors are selected if select_func is undefined.
 sense		     : Determines which reciprocal condition numbers will be computed.
			0: None are computed
			1: Computed for average of selected eigenvalues only
			2: Computed for selected right invariant subspace only
			3: Computed for both
			If select_func is undefined, sense is not used.
 backtransform	     : Whether or not backtransforms eigenvectors to those of A.
		       Only supported if schur vector are computed, default = 1
 norm                : Whether or not computed eigenvectors are normalized to have Euclidean norm equal to
		       1 and largest component real, default = 1

 Returned values     :
		       Schur form T (SCALAR CONTEXT),
		       eigenvalues,
		       Schur vectors if requested,
		       HASH{VL}: left eigenvectors if requested
		       HASH{VR}: right eigenvectors if requested
		       HASH{info}: info output from gees/cgees.
		       if select_func is defined:
			HASH{n}: number of eigenvalues selected,
			HASH{rconde}: reciprocal condition numbers for the average of
			the selected eigenvalues if requested,
			HASH{rcondv}: reciprocal condition numbers for the selected
			right invariant subspace if requested.

=for example

 my $a = random(10,10);
 my $schur  = mschurx($a);
 sub select{
	my $m = shift;
	# select "discrete time" eigenspace
	return $m->Cabs < 1 ? 1 : 0;
 }
 my ($schur,$eigen, $vectors,%ret)  = mschurx($a,1,0,0,\&select);

=cut

*mschurx = \&PDL::mschurx;
sub PDL::mschurx {
	&_square;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my($m, $jobv, $jobvl, $jobvr, $select_func, $sense, $mult,$norm) = @_;
	my(@dims) = $m->dims;
	$mult //= 1;
	$norm //= 1;
	$jobv = $jobvl = $jobvr = 0 unless wantarray;
	my $select = long($select_func ? 1 : 0);
	$sense = pdl(long,0) if !$select_func;
	$_ = null for my ($info, $sdim, $rconde, $rcondv);
	my $mm = $m->new_or_inplace->t;
	my $v = $m->_similar_null;
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	my $select_f = $m->_wrap_select_func($select_func);
	$mm->_call_method('geesx', $jobv, $select, $sense, @w, $v, $sdim, $rconde, $rcondv,$info, $select_f);
	_error_schur($info, $select_func, $dims[$di], 'mschurx', 'QR');
	my @vl = ('VL', $jobvl);
	my @vr = ('VR', $jobvr);
	my %ret;
	$ret{$_->[0]} = _complex_null(), $_->[1]=0 for grep $select_func && !$sdim && $_->[1] == 2, \@vl, \@vr;
	$ret{n} = $sdim if $select_func;
	if ($vl[1] || $vr[1]) {
		my ($vl, $vr) = _eigen_one(
		  $mm, $select_func, $jobv, $vl[1], $vr[1],
		  $mult, $norm, $dims[$di+1], $sdim, @w
		);
		$ret{$_->[0]} = $_->[1] for grep defined $_->[1], ['VL',$vl], ['VR',$vr];
	}
	my $w = _ecplx(@w);
	if ($jobv == 2 && $select_func) {
		$v = $sdim > 0 ? $v->t->slice("$slice_prefix:@{[$sdim-1]},")->sever : $m->_similar_null;
	}
	elsif($jobv){
		$v =  $v->t->sever;
	}
	$ret{info} = $info;
	$ret{rconde} = $rconde if $sense & 1;
	$ret{rcondv} = $rcondv if $sense & 2;
	$m = $mm->t->sever unless $m->is_inplace(0);
	!wantarray ? $m : ($m, $w, ($jobv ? $v : ()), %ret);
}

# scale by max(abs(real)+abs(imag))
sub magn_norm {
	my ($m, $trans) = @_;
	# If trans == true => transpose output matrix
	my $ret = PDL::cat(map $m->$_, qw(re im))->mv(-1,0)->abs->sumover->maxover->dummy(0);
	$m = $m->t, $ret = $ret->t if $trans;
	($m/$ret)->reshape(-1);
}

#TODO: inplace ?

=head2 mgschur

=for ref

Computes generalized Schur decomposition of the pair (A,B).

	A = Q x S x Z'
	B = Q x T x Z'

Uses L<gges|PDL::LinearAlgebra::Real/gges> or L<cgges|PDL::LinearAlgebra::Complex/cgges>
from Lapack.
Works on transposed array.

=for usage

 ( PDL(schur S), PDL(schur T), PDL(alpha), PDL(beta), HASH{result}) = mgschur(PDL(A), PDL(B), SCALAR(left schur vector),SCALAR(right schur vector),SCALAR(left eigenvector), SCALAR(right eigenvector), SCALAR(select_func), SCALAR(backtransform), SCALAR(scale))
 left schur vector   : Left Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right schur vector  : Right Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 left eigenvector    : Left eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right eigenvector   : Right eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 select_func	     : Select_func is used to select eigenvalues to sort.
		       to the top left of the Schur form.
		       An eigenvalue w = wr(j)+sqrt(-1)*wi(j) is selected if
		       PerlInt select_func(alpha,beta) is true;
		       (the inputs are converted to complex ndarrays for you)
		       Note that a selected complex eigenvalue may no longer
		       satisfy select_func = 1 after ordering, since
		       ordering may change the value of complex eigenvalues
		       (especially if the eigenvalue is ill-conditioned).
		       All eigenvalues/vectors are selected if select_func is undefined.
 backtransform	     : Whether or not backtransforms eigenvectors to those of (A,B).
		       Only supported if right and/or left schur vector are computed,
 scale               : Whether or not computed eigenvectors are scaled so the largest component
		       will have abs(real part) + abs(imag. part) = 1, default = 1

 Returned values     :
		       Schur form S,
		       Schur form T,
		       alpha,
		       beta (eigenvalues = alpha/beta),
		       HASH{info}: info output from gges/cgges.
		       HASH{SL}: left Schur vectors if requested
		       HASH{SR}: right Schur vectors if requested
		       HASH{VL}: left eigenvectors if requested
		       HASH{VR}: right eigenvectors if requested
		       HASH{n} : Number of eigenvalues selected if select_func is defined.

=for example

 my $a = random(10,10);
 my $b = random(10,10);
 my ($S,$T) = mgschur($a,$b);
 sub select{
	my ($alpha,$beta) = @_;
	return $alpha->Cabs < abs($beta) ? 1 : 0;
 }
 my ($S, $T, $alpha, $beta, %res)  = mgschur( $a, $b, 1, 1, 1, 1,\&select);

=cut

sub _eigen_pair {
  my (
    $mm, $pp, $select_func, $jobvl, $jobvr, $jobvsl, $jobvsr, $vsl, $vsr,
    $mult, $norm, $mdim, $sdim, @w
  ) = @_;
  my $job = !($jobvr && $jobvl) ? $jobvl ? 2 : 1 : undef;
  $_ = $mm->_similar_null for my ($vl, $vr);
  $mult = 0 if ($jobvl && !$jobvsl) || ($jobvr && !$jobvsr);
  if (!$select_func || ($jobvl == 1 || $jobvr == 1 || $mult)) {
    $vl .= $vsl if $jobvl && $jobvsl;
    $vr .= $vsr if $jobvr && $jobvsr;
  }
  my ($sdim_in, $sel) = $sdim;
  if ($select_func) {
    if ($jobvl == 1 || $jobvr == 1 || $mult) {
      $sdim_in = null;
    } else {
      $sel = zeroes($mdim);
      $sel(:($sdim-1)) .= 1;
      $mult = 2;
      @w = map $_(:($sdim-1)), @w;
    }
  }
  $mm->_call_method('tgevc', $job, $mult, $pp, $sel, $vl, $vr, $sdim_in, my $infos=null);
  if (@w == 2) {
    (undef,$vl) = cplx_eigen(@w,$vl->t,0) if $jobvl;
    (undef,$vr) = cplx_eigen(@w,$vr->t,0) if $jobvr;
  }
  if ($select_func && ($jobvl == 1 || $jobvr == 1 || $mult)) {
    $vr = $vr(,:($sdim-1))->sever if $jobvr == 2;
    $vl = $vl(,:($sdim-1))->sever if $jobvl == 2;
  }
  $vl = magn_norm($vl,1) if $jobvl and $norm;
  $vr = magn_norm($vr,1) if $jobvr and $norm;
  ($vl, $vr);
}

*mgschur = \&PDL::mgschur;
sub PDL::mgschur {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	&_square_same;
	my($m, $p, $jobvsl, $jobvsr, $jobvl, $jobvr, $select_func, $mult, $norm) = @_;
	my @mdims  = $m->dims;
	$_ = $_->new_or_inplace->t for $m, $p;
	barf("mgschur: threading isn't supported for selected vectors")
		if ($select_func && ((@mdims > 2+$di) || ($p->ndims > 2+$di)) &&
			($jobvsl == 2 || $jobvsr == 2 || $jobvl == 2 || $jobvr == 2));
	$mult //= 1;
	$norm //= 1;
	my $select = $select_func ? pdl(long,1) : pdl(long,0);
	$_ = null for my ($info, $sdim);
	my $select_f = $m->_wrap_select_func($select_func);
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	$_ = $m->_similar_null for my ($beta, $vsl, $vsr);
	$m->_call_method('gges', $jobvsl, $jobvsr, $select, $p, @w, $beta, $vsl, $vsr, $sdim, $info, $select_f);
	_error_schur($info, $select_func, $mdims[$di], 'mgschur', 'QZ');
	my @vl = ('VL', $jobvl);
	my @vr = ('VR', $jobvr);
	my %ret;
	$ret{$_->[0]} = _complex_null(), $_->[1]=0 for grep $select_func && !$sdim && $_->[1] == 2, \@vl, \@vr;
	$ret{n} = $sdim if $select_func;
	if ($vl[1] || $vr[1]) {
		my ($vl, $vr) = _eigen_pair(
		  $m, $p, $select_func, $vl[1], $vr[1], $jobvsl, $jobvsr, $vsl, $vsr,
		  $mult, $norm, $mdims[$di+1], $sdim, @w
		);
		$ret{$_->[0]} = $_->[1] for grep defined $_->[1], ['VL',$vl], ['VR',$vr];
	}
	my $w = @w == 2 ? _ecplx(@w) : @w[0];
	if ($jobvsl == 2 && $select_func) {
		$ret{SL} = $sdim ? $vsl->t->slice("$slice_prefix:@{[$sdim-1]},")->sever : $m->_similar_null;
	}
	elsif($jobvsl){
		$ret{SL} = $vsl->t->sever;
	}
	if ($jobvsr == 2 && $select_func) {
		$ret{SR} = $sdim ? $vsr->t->slice("$slice_prefix:@{[$sdim-1]},")->sever : $m->_similar_null;
	}
	elsif($jobvsr){
		$ret{SR} = $vsr->t->sever;
	}
	$ret{info} = $info;
	return ($m->t, $p->t, $w, $beta, %ret);
}

=head2 mgschurx

=for ref

Computes generalized Schur decomposition of the pair (A,B).

	A = Q x S x Z'
	B = Q x T x Z'

Uses L<ggesx|PDL::LinearAlgebra::Real/ggesx> or L<cggesx|PDL::LinearAlgebra::Complex/cggesx>
from Lapack. Works on transposed array.

=for usage

 ( PDL(schur S), PDL(schur T), PDL(alpha), PDL(beta), HASH{result}) = mgschurx(PDL(A), PDL(B), SCALAR(left schur vector),SCALAR(right schur vector),SCALAR(left eigenvector), SCALAR(right eigenvector), SCALAR(select_func), SCALAR(sense), SCALAR(backtransform), SCALAR(scale))
 left schur vector   : Left Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right schur vector  : Right Schur vectors returned, none = 0 | all = 1 | selected = 2, default = 0
 left eigenvector    : Left eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 right eigenvector   : Right eigenvectors returned, none = 0 | all = 1 | selected = 2, default = 0
 select_func	     : Select_func is used to select eigenvalues to sort.
		       to the top left of the Schur form.
		       An eigenvalue w = wr(j)+sqrt(-1)*wi(j) is selected if
		       PerlInt select_func(alpha,beta) is true;
		       (the inputs are converted to complex ndarrays for you)
		       Note that a selected complex eigenvalue may no longer
		       satisfy select_func = 1 after ordering, since
		       ordering may change the value of complex eigenvalues
		       (especially if the eigenvalue is ill-conditioned).
		       All eigenvalues/vectors are selected if select_func is undefined.
 sense		     : Determines which reciprocal condition numbers will be computed.
			0: None are computed
			1: Computed for average of selected eigenvalues only
			2: Computed for selected deflating subspaces only
			3: Computed for both
			If select_func is undefined, sense is not used.

 backtransform	     : Whether or not backtransforms eigenvectors to those of (A,B).
		       Only supported if right and/or left schur vector are computed, default = 1
 scale               : Whether or not computed eigenvectors are scaled so the largest component
		       will have abs(real part) + abs(imag. part) = 1, default = 1

 Returned values     :
		       Schur form S,
		       Schur form T,
		       alpha,
		       beta (eigenvalues = alpha/beta),
		       HASH{info}: info output from gges/cgges.
		       HASH{SL}: left Schur vectors if requested
		       HASH{SR}: right Schur vectors if requested
		       HASH{VL}: left eigenvectors if requested
		       HASH{VR}: right eigenvectors if requested
		       HASH{rconde}: reciprocal condition numbers for average of selected eigenvalues if requested
		       HASH{rcondv}: reciprocal condition numbers for selected deflating subspaces if requested
		       HASH{n} : Number of eigenvalues selected if select_func is defined.

=for example

 my $a = random(10,10);
 my $b = random(10,10);
 my ($S,$T) = mgschurx($a,$b);
 sub select{
	my ($alpha,$beta) = @_;
	return $alpha->Cabs < abs($beta) ? 1 : 0;
 }
 my ($S, $T, $alpha, $beta, %res)  = mgschurx( $a, $b, 1, 1, 1, 1,\&select,3);

=cut

*mgschurx = \&PDL::mgschurx;
sub PDL::mgschurx {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	&_square_same;
	my($m, $p, $jobvsl, $jobvsr, $jobvl, $jobvr, $select_func, $sense, $mult, $norm) = @_;
	my (@mdims) = $m->dims;
	$_ = $_->new_or_inplace->t for $m, $p;
	$mult //= 1;
	$norm //= 1;
	my $select = $select_func ? 1 : 0;
	$sense = 0 if !$select_func;
	$_ = null for my ($info, $rconde, $rcondv, $sdim);
	$_ = $m->_similar_null for my ($beta, $vsl, $vsr);
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	my $select_f = $m->_wrap_select_func($select_func);
	$m->_call_method('ggesx', $jobvsl, $jobvsr, $select, $sense, $p, @w, $beta, $vsl, $vsr, $sdim, $rconde, $rcondv,$info, $select_f);
	_error_schur($info, $select_func, $mdims[$di], 'mgschurx', 'QZ');
	my @vl = ('VL', $jobvl);
	my @vr = ('VR', $jobvr);
	my %ret;
	$ret{$_->[0]} = _complex_null(), $_->[1]=0 for grep $select_func && !$sdim && $_->[1] == 2, \@vl, \@vr;
	$ret{n} = $sdim if $select_func;
	if ($vl[1] || $vr[1]) {
		my ($vl, $vr) = _eigen_pair(
		  $m, $p, $select_func, $vl[1], $vr[1], $jobvsl, $jobvsr, $vsl, $vsr,
		  $mult, $norm, $mdims[$di+1], $sdim, @w
		);
		$ret{$_->[0]} = $_->[1] for grep defined $_->[1], ['VL',$vl], ['VR',$vr];
	}
	if ($jobvsl == 2 && $select_func) {
		$ret{SL} = $sdim ? $vsl->t->slice("$slice_prefix:@{[$sdim-1]},")->sever : $m->_similar_null;
	}
	elsif($jobvsl){
		$ret{SL} = $vsl->t->sever;
	}
	if ($jobvsr == 2 && $select_func) {
		$ret{SR} = $sdim ? $vsr->t->slice("$slice_prefix:@{[$sdim-1]},")->sever : $m->_similar_null;
	}
	elsif($jobvsr){
		$ret{SR} = $vsr->t->sever;
	}
	my $w = @w == 2 ? _ecplx(@w) : @w[0];
	$ret{info} = $info;
	$ret{rconde} = $rconde if $sense & 1;
	$ret{rcondv} = $rcondv if $sense & 2;
	return ($m->t, $p->t, $w, $beta, %ret);
}

=head2 mqr

=for ref

Computes QR decomposition.
Handles complex data.
Uses L<geqrf|PDL::LinearAlgebra::Real/geqrf> and L<orgqr|PDL::LinearAlgebra::Real/orgqr>
or L<cgeqrf|PDL::LinearAlgebra::Complex/cgeqrf> and L<cungqr|PDL::LinearAlgebra::Complex/cungqr>
from Lapack and returns C<Q> in scalar context. Works on transposed array.

=for usage

 (PDL(Q), PDL(R), PDL(info)) = mqr(PDL, SCALAR)
 SCALAR : ECONOMIC = 0 | FULL = 1, default = 0

=for example

 my $a = random(10,10);
 my ( $q, $r )  = mqr($a);
 # Can compute full decomposition if nrow > ncol
 $a = random(5,7);
 ( $q, $r )  = $a->mqr(1);

=cut

*mqr = \&PDL::mqr;
sub PDL::mqr {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my @di_vals = $_[0]->dims_internal_values;
	my($m, $full) = @_;
	my(@dims) = $m->dims;
	$m = $m->t->copy;
	my $min = $dims[$di] < $dims[$di+1] ? $dims[$di] : $dims[$di+1];
	my $slice_arg = (',' x $di) . ",:@{[$min-1]}";
	my $tau = $m->_similar($min);
	$m->_call_method('geqrf', $tau, my $info = null);
	if ($info){
		laerror ("mqr: Error $info in geqrf\n");
		return ($m->t->sever, $m, $info);
	}
	my $q = ($dims[$di] > $dims[$di+1] ? $m->slice($slice_arg) : $m)->copy;
	$q->reshape(@di_vals, @dims[$di+1,$di+1]) if $full && $dims[$di] < $dims[$di+1];
	$q->_call_method(['orgqr','cungqr'], $tau, $info);
	return $q->t->sever unless wantarray;
	my $r = (($dims[$di] < $dims[$di+1] && !$full) ? $m->t->slice($slice_arg) : $m->t)->tricpy(0);
	return ($q->t->sever, $r, $info);
}

=head2 mrq

=for ref

Computes RQ decomposition.
Handles complex data.
Uses L<gerqf|PDL::LinearAlgebra::Real/gerqf> and L<orgrq|PDL::LinearAlgebra::Real/orgrq>
or L<cgerqf|PDL::LinearAlgebra::Complex/cgerqf> and L<cungrq|PDL::LinearAlgebra::Complex/cungrq>
from Lapack and returns C<Q> in scalar context. Works on transposed array.

=for usage

 (PDL(R), PDL(Q), PDL(info)) = mrq(PDL, SCALAR)
 SCALAR : ECONOMIC = 0 | FULL = 1, default = 0

=for example

 my $a = random(10,10);
 my ( $r, $q )  = mrq($a);
 # Can compute full decomposition if nrow < ncol
 $a = random(5,7);
 ( $r, $q )  = $a->mrq(1);

=cut

*mrq = \&PDL::mrq;
sub PDL::mrq {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my @diag_args = ($di, $di+1);
	my($m, $full) = @_;
	my(@dims) = $m->dims;
	my ($q, $r);
        $m = $m->t->copy;
	my $min = $dims[$di] < $dims[$di+1] ? $dims[$di] : $dims[$di+1];
	my $tau = $m->_similar($min);
	$m->_call_method('gerqf', $tau, my $info = null);
	if ($info){
		laerror ("mrq: Error $info in gerqf\n");
		return ($m, $m->t->sever, $info);
	}
	if ($dims[$di] > $dims[$di+1] && $full){
		$q = $m->_similar(@dims[$di,$di]);
		$q->slice("$slice_prefix@{[$dims[$di] - $dims[$di+1]]}:") .= $m;
	}
	elsif ($dims[$di] < $dims[$di+1]){
		$q = $m->slice("$slice_prefix@{[$dims[$di+1] - $dims[$di]]}:")->copy;
	}
	else{
		$q = $m->copy;
	}
	$q->_call_method(['orgrq','cungrq'], $tau, $info);
	return $q->t->sever unless wantarray;
	if ($dims[$di] > $dims[$di+1] && $full){
		$r = $m->t->tricpy(0);
		$r->slice("$slice_prefix:@{[$min-1]},:@{[$min-1]}")->diagonal(@diag_args) .= 0;
	}
	elsif ($dims[$di] < $dims[$di+1]){
		my $temp = $m->_similar(@dims[$di+1,$di+1]);
		$temp->slice("$slice_prefix-$min:") .= $m->t;
		$r = $temp->tricpy(0);
		$r = $r->slice("$slice_prefix-$min:")->sever;
	}
	else{
		$r = $m->t->slice("$slice_prefix@{[$dims[$di] - $dims[$di+1]]}:")->tricpy(0);
	}
	return ($r, $q->t->sever, $info);
}

=head2 mql

=for ref

Computes QL decomposition.
Handles complex data.
Uses L<geqlf|PDL::LinearAlgebra::Real/geqlf> and L<orgql|PDL::LinearAlgebra::Real/orgql>
or L<cgeqlf|PDL::LinearAlgebra::Complex/cgeqlf> and L<cungql|PDL::LinearAlgebra::Complex/cungql>
from Lapack and returns C<Q> in scalar context. Works on transposed array.

=for usage

 (PDL(Q), PDL(L), PDL(info)) = mql(PDL, SCALAR)
 SCALAR : ECONOMIC = 0 | FULL = 1, default = 0

=for example

 my $a = random(10,10);
 my ( $q, $l )  = mql($a);
 # Can compute full decomposition if nrow > ncol
 $a = random(5,7);
 ( $q, $l )  = $a->mql(1);

=cut

*mql = \&PDL::mql;
sub PDL::mql {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my @diag_args = ($di, $di+1);
	my($m, $full) = @_;
	my(@dims) = $m->dims;
	my ($q, $l);
        $m = $m->t->copy;
	my $min = $dims[$di] < $dims[$di+1] ? $dims[$di] : $dims[$di+1];
	my $tau = $m->_similar($min);
	$m->_call_method('geqlf', $tau, my $info = null);
	if ($info){
		laerror("mql: Error $info in geqlf\n");
		return ($m->t->sever, $m, $info);
	}
	if ($dims[$di] < $dims[$di+1] && $full){
		$q = $m->_similar(@dims[$di+1,$di+1]);
		$q->slice("$slice_prefix:,-$dims[$di]:") .= $m;
	}
	elsif ($dims[$di] > $dims[$di+1]){
		$q = $m->slice("$slice_prefix:,-$min:")->copy;
	}
	else{
		$q = $m->copy;
	}
	$q->_call_method(['orgql','cungql'], $tau, $info);
	return $q->t->sever unless wantarray;
	if ($dims[$di] < $dims[$di+1] && $full){
		$l = $m->t->tricpy(1);
		$l->slice("$slice_prefix:@{[$min-1]},:@{[$min-1]}")->diagonal(@diag_args) .= 0;
	}
	elsif ($dims[$di] > $dims[$di+1]){
		my $temp = $m->_similar(@dims[$di,$di]);
		$temp->slice("$slice_prefix:,-$dims[$di+1]:") .= $m->t;
		$l = $temp->tricpy(0);
		$l = $l->slice("$slice_prefix:,-$dims[$di+1]:");
	}
	else{
		$l = $m->t->slice("$slice_prefix:,@{[$dims[$di+1] - $min]}:")->tricpy(1);
	}
	return ($q->t->sever, $l, $info);
}

=head2 mlq

=for ref

Computes LQ decomposition.
Handles complex data.
Uses L<gelqf|PDL::LinearAlgebra::Real/gelqf> and L<orglq|PDL::LinearAlgebra::Real/orglq>
or L<cgelqf|PDL::LinearAlgebra::Complex/cgelqf> and L<cunglq|PDL::LinearAlgebra::Complex/cunglq>
from Lapack and returns C<Q> in scalar context. Works on transposed array.

=for usage

 ( PDL(L), PDL(Q), PDL(info) ) = mlq(PDL, SCALAR)
 SCALAR : ECONOMIC = 0 | FULL = 1, default = 0

=for example

 my $a = random(10,10);
 my ( $l, $q )  = mlq($a);
 # Can compute full decomposition if nrow < ncol
 $a = random(5,7);
 ( $l, $q )  = $a->mlq(1);

=cut

*mlq = \&PDL::mlq;
sub PDL::mlq {
	&_2d_array;
	my $di = $_[0]->dims_internal;
	my($m, $full) = @_;
	my(@dims) = $m->dims;
	my ($q);
	$m = $m->t->copy;
	my $min = $dims[$di] < $dims[$di+1] ? $dims[$di] : $dims[$di+1];
	my $slice_arg = (',' x $di) . ":@{[$min-1]}";
	my $tau = $m->_similar($min);
	$m->_call_method('gelqf', $tau, my $info = null);
	if ($info){
		laerror("mlq: Error $info in gelqf\n");
		return ($m, $m->t->sever, $info);
	}
	if ($dims[$di] > $dims[$di+1] && $full){
		$q = $m->_similar(@dims[$di,$di]);
		$q->slice($slice_arg) .= $m;
	}
	elsif ($dims[$di] < $dims[$di+1]){
		$q = $m->slice($slice_arg)->copy;
	}
	else{
		$q = $m->copy;
	}
	$q->_call_method(['orglq','cunglq'], $tau, $info);
	return $q->t->sever unless wantarray;
	my $l = (($dims[$di] > $dims[$di+1] && !$full) ? $m->t->slice($slice_arg) : $m->t)->tricpy(1);
	return ($l, $q->t->sever, $info);
}

=head2 msolve

=for ref

Solves linear system of equations using LU decomposition.

	A * X = B

Returns X in scalar context else X, LU, pivot vector and info.
B is overwritten by X if its inplace flag is set.
Supports threading.
Uses L<gesv|PDL::LinearAlgebra::Real/gesv> or L<cgesv|PDL::LinearAlgebra::Complex/cgesv> from Lapack.
Works on transposed arrays.

=for usage

 (PDL(X), (PDL(LU), PDL(pivot), PDL(info))) = msolve(PDL(A), PDL(B) )

=for example

 my $a = random(5,5);
 my $b = random(10,5);
 my $X = msolve($a, $b);

=cut

*msolve = \&PDL::msolve;
sub PDL::msolve {
	&_square;
	&_matrices_match;
	&_same_dims;
	my($a, $b) = @_;
	$a = $a->t->copy;
	my $c = $b->new_or_inplace->t;
	$a->_call_method('gesv', $c, my $ipiv = null, my $info = null);
	_error($info, "msolve: Can't solve system of linear equations (after getrf factorization): matrix (PDL(s) %s) is/are singular");
	$b = $c->t->sever if !$b->is_inplace(0);
	wantarray ? ($b, $a->t->sever, $ipiv, $info) : $b;
}

=head2 msolvex

=for ref

Solves linear system of equations using LU decomposition.

	A * X = B

Can optionally equilibrate the matrix.
Uses L<gesvx|PDL::LinearAlgebra::Real/gesvx> or L<cgesvx|PDL::LinearAlgebra::Complex/cgesvx> from Lapack.
Works on transposed arrays.

=for usage

 (PDL, (HASH(result))) = msolvex(PDL(A), PDL(B), HASH(options))
 where options are:
 transpose:	solves A' * X = B
		0: false
		1: true
 equilibrate:	equilibrates A if necessary.
		form equilibration is returned in HASH{'equilibration'}:
			0: no equilibration
			1: row equilibration
			2: column equilibration
		row scale factors are returned in HASH{'row'}
		column scale factors are returned in HASH{'column'}
		0: false
		1: true
 LU:		returns lu decomposition in HASH{LU}
		0: false
		1: true
 A:		returns scaled A if equilibration was done in HASH{A}
		0: false
		1: true
 B:		returns scaled B if equilibration was done in HASH{B}
		0: false
		1: true
 Returned values:
		X (SCALAR CONTEXT),
		HASH{'pivot'}:
		 Pivot indice from LU factorization
		HASH{'rcondition'}:
		 Reciprocal condition of the matrix
		HASH{'ferror'}:
		 Forward error bound
		HASH{'berror'}:
		 Componentwise relative backward error
		HASH{'rpvgrw'}:
		 Reciprocal pivot growth factor
		HASH{'info'}:
		 Info: output from gesvx

=for example

 my $a = random(10,10);
 my $b = random(5,10);
 my %options = (
		LU=>1,
		equilibrate => 1,
		);
 my( $X, %result) = msolvex($a,$b,%options);

=cut


*msolvex = \&PDL::msolvex;

sub PDL::msolvex {
	&_square;
	&_matrices_match;
	my $di = $_[0]->dims_internal;
	my($a, $b, %opt) = @_;
	my(@adims) = $a->dims;
	$a = $a->t->copy;
	$b = $b->t->copy;
	my $x = $a->_similar_null;
	my $af = PDL::zeroes $a;
	$_ = null for my ($info, $rcond, $rpvgrw, $ferr, $berr);
	my $equed = pdl(long, 0);
	my $ipiv = zeroes(long, $adims[$di]);
	$a->_call_method('gesvx', $opt{transpose}, $opt{equilibrate} ? 2 : 1, $b, $af, $ipiv, $equed, my $r = null, my $c = null, $x, $rcond, $ferr, $berr, $rpvgrw,$info);
	if( $info < $adims[$di] && $info > 0){
		$info--;
		laerror("msolvex: Can't solve system of linear equations:\nfactor U($info,$info)".
		" of coefficient matrix is exactly 0");
	}
	elsif ($info != 0 and $_laerror){
		warn ("msolvex: The matrix is singular to working precision");
	}
	return $x->t->sever unless wantarray;
	my %result = (rcondition => $rcond, ferror => $ferr, berror => $berr);
	if ($opt{equilibrate}){
		$result{equilibration} = $equed;
		$result{row} = $r if $equed & 1;
		$result{column} = $c if $equed & 2;
		if ($equed){
			$result{A} = $a->t->sever if $opt{A};
			$result{B} = $b->t->sever if $opt{B};
		}
	}
	@result{qw(pivot rpvgrw info)} = ($ipiv, $rpvgrw, $info);
        $result{LU} = $af->t->sever if $opt{LU};
	return ($x->t->sever, %result);
}

=head2 mtrisolve

=for ref

Solves linear system of equations with triangular matrix A.

	A * X = B  or A' * X = B

B is overwritten by X if its inplace flag is set.
Supports threading.
Uses L<trtrs|PDL::LinearAlgebra::Real/trtrs> or L<ctrtrs|PDL::LinearAlgebra::Complex/ctrtrs> from Lapack.
Work on transposed array(s).

=for usage

 (PDL(X), (PDL(info)) = mtrisolve(PDL(A), SCALAR(uplo), PDL(B), SCALAR(trans), SCALAR(diag))
 uplo	: UPPER  = 0 | LOWER = 1
 trans	: NOTRANSPOSE  = 0 | TRANSPOSE = 1, default = 0
 uplo	: UNITARY DIAGONAL = 1, default = 0

=for example

 # Assume $a is upper triagonal
 my $a = random(5,5);
 my $b = random(5,10);
 my $X = mtrisolve($a, 0, $b);

=cut

*mtrisolve = \&PDL::mtrisolve;
sub PDL::mtrisolve{
	&_square;
	my $uplo = splice @_, 1, 1;
	&_matrices_match;
	&_same_dims;
	my($a, $b, $trans, $diag) = @_;
	$uplo = 1 - $uplo;
	$trans = 1 - $trans;
	my $c = $b->new_or_inplace->t;
	$a->_call_method('trtrs', $uplo, $trans, $diag, $c, my $info = null);
	_error($info, "mtrisolve: Can't solve system of linear equations: matrix (PDL(s) %s) is/are singular");
	$b = $c->t->sever if !$b->is_inplace(0);
	wantarray ? ($b, $info) : $b;
}

=head2 msymsolve

=for ref

Solves linear system of equations using diagonal pivoting method with symmetric matrix A.

	A * X = B

Returns X in scalar context else X, block diagonal matrix D (and the
multipliers), pivot vector an info. B is overwritten by X if its inplace flag is set.
Supports threading.
Uses L<sysv|PDL::LinearAlgebra::Real/sysv> or L<csysv|PDL::LinearAlgebra::Complex/csysv> from Lapack.
Works on transposed array(s).

=for usage

 (PDL(X), ( PDL(D), PDL(pivot), PDL(info) ) ) = msymsolve(PDL(A), SCALAR(uplo), PDL(B) )
 uplo : UPPER  = 0 | LOWER = 1, default = 0

=for example

 # Assume $a is symmetric
 my $a = random(5,5);
 my $b = random(5,10);
 my $X = msymsolve($a, 0, $b);

=cut

*msymsolve = \&PDL::msymsolve;
sub PDL::msymsolve {
	&_square;
	my $uplo = splice @_, 1, 1;
	&_matrices_match;
	&_same_dims;
	my($a, $b) = @_;
	$uplo = 1 - $uplo;
	$a = $a->copy;
	my $c = $b->new_or_inplace->t;
	$a->_call_method('sysv', $uplo, $c, my $ipiv = null, my $info = null);
	_error($info, "msymsolve: Can't solve system of linear equations (after sytrf factorization): matrix (PDL(s) %s) is/are singular");
	$b = $c->t->sever if !$b->is_inplace(0);
	wantarray ? ($b, $a, $ipiv, $info) : $b;
}

=head2 msymsolvex

=for ref

Solves linear system of equations using diagonal pivoting method with symmetric matrix A.

	A * X = B

Uses L<sysvx|PDL::LinearAlgebra::Real/sysvx> or L<csysvx|PDL::LinearAlgebra::Complex/csysvx>
from Lapack. Works on transposed array.

=for usage

 (PDL, (HASH(result))) = msymsolvex(PDL(A), SCALAR (uplo), PDL(B), SCALAR(d))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 d    : whether return diagonal matrix d and pivot vector
	FALSE  = 0 | TRUE = 1, default = 0
 Returned values:
		X (SCALAR CONTEXT),
		HASH{'D'}:
		 Block diagonal matrix D (and the multipliers) (if requested)
		HASH{'pivot'}:
		 Pivot indice from LU factorization (if requested)
		HASH{'rcondition'}:
		 Reciprocal condition of the matrix
		HASH{'ferror'}:
		 Forward error bound
		HASH{'berror'}:
		 Componentwise relative backward error
		HASH{'info'}:
		 Info: output from sysvx

=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my $b = random(5,10);
 my ($X, %result) = msolvex($a, 0, $b);


=cut


*msymsolvex = \&PDL::msymsolvex;
sub PDL::msymsolvex {
	&_square;
	my $uplo = splice @_, 1, 1;
	&_matrices_match;
	my $di = $_[0]->dims_internal;
	my($a, $b, $d) = @_;
	my(@adims) = $a->dims;
	$uplo = 1 - $uplo;
	$b = $b->t;
	my $x = $a->_similar_null;
	my $af =  PDL::zeroes $a;
	$_ = null for my ($info, $rcond, $ferr, $berr);
	my $ipiv = zeroes(long, $adims[$di]);
	$a->_call_method('sysvx', $uplo, 0, $b, $af, $ipiv, $x, $rcond, $ferr, $berr, $info);
	if( $info < $adims[$di] && $info > 0){
		$info--;
		laerror("msymsolvex: Can't solve system of linear equations:\nfactor D($info,$info)".
		" of coefficient matrix is exactly 0");
	}
	elsif ($info != 0 and $_laerror){
		warn("msymsolvex: The matrix is singular to working precision");
	}
	my %result = (rcondition => $rcond, ferror => $ferr, berror => $berr, info => $info);
	@result{qw(pivot D)} = ($ipiv, $af) if $d;
	wantarray ? ($x->t->sever, %result): $x->t->sever;
}

=head2 mpossolve

=for ref

Solves linear system of equations using Cholesky decomposition with
symmetric positive definite matrix A.

	A * X = B

Returns X in scalar context else X, U or L and info.
B is overwritten by X if its inplace flag is set.
Supports threading.
Uses L<posv|PDL::LinearAlgebra::Real/posv> or L<cposv|PDL::LinearAlgebra::Complex/cposv> from Lapack.
Works on transposed array(s).

=for usage

 (PDL, (PDL, PDL, PDL)) = mpossolve(PDL(A), SCALAR(uplo), PDL(B) )
 uplo : UPPER  = 0 | LOWER = 1, default = 0

=for example

 # asume $a is symmetric positive definite
 my $a = random(5,5);
 my $b = random(5,10);
 my $X = mpossolve($a, 0, $b);

=cut


*mpossolve = \&PDL::mpossolve;
sub PDL::mpossolve {
	&_square;
	my $uplo = splice @_, 1, 1;
	&_matrices_match;
	&_same_dims;
	my($a, $b) = @_;
	$uplo = 1 - $uplo;
	$a = $a->copy;
	my $c = $b->new_or_inplace->t;
	$a->_call_method('posv', $uplo, $c, my $info=null);
	_error($info, "mpossolve: Can't solve system of linear equations: matrix (PDL(s) %s) is/are not positive definite");
	wantarray ? $b->is_inplace(0) ? ($b, $a,$info) : ($c->t->sever , $a,$info) : $b->is_inplace(0) ? $b : $c->t->sever;
}

=head2 mpossolvex

=for ref

Solves linear system of equations using Cholesky decomposition with
symmetric positive definite matrix A

	A * X = B

Can optionally equilibrate the matrix.
Uses L<posvx|PDL::LinearAlgebra::Real/posvx> or
L<cposvx|PDL::LinearAlgebra::Complex/cposvx> from Lapack.
Works on transposed array(s).

=for usage

 (PDL, (HASH(result))) = mpossolvex(PDL(A), SCARA(uplo), PDL(B), HASH(options))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 where options are:
 equilibrate:	equilibrates A if necessary.
		form equilibration is returned in HASH{'equilibration'}:
			0: no equilibration
			1: equilibration
		scale factors are returned in HASH{'scale'}
		0: false
		1: true
 U|L:		returns Cholesky factorization in HASH{U} or HASH{L}
		0: false
		1: true
 A:		returns scaled A if equilibration was done in HASH{A}
		0: false
		1: true
 B:		returns scaled B if equilibration was done in HASH{B}
		0: false
		1: true
 Returned values:
		X (SCALAR CONTEXT),
		HASH{'rcondition'}:
		 Reciprocal condition of the matrix
		HASH{'ferror'}:
		 Forward error bound
		HASH{'berror'}:
		 Componentwise relative backward error
		HASH{'info'}:
		 Info: output from gesvx

=for example

 # Assume $a is symmetric positive definite
 my $a = random(10,10);
 my $b = random(5,10);
 my %options = (U=>1,
		equilibrate => 1,
		);
 my ($X, %result) = msolvex($a, 0, $b,%opt);

=cut


*mpossolvex = \&PDL::mpossolvex;

sub PDL::mpossolvex {
	&_square;
	my $uplo = splice(@_, 1, 1) ? 0 : 1;
	&_matrices_match;
	my $di = $_[0]->dims_internal;
	my($a, $b, %opt) = @_;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	my $equilibrate = $opt{'equilibrate'} ? 2: 1;
	$a = $a->copy;
	$b = $b->t->copy;
	my $x = $a->_similar_null;
	my $af = PDL::zeroes $a;
	my $equed = pdl(long, 0);
	$a->_call_method('posvx', $uplo, $equilibrate, $b, $af, $equed, my $s = null, $x, my $rcond=null, my $ferr=null, my $berr=null, my $info=null);
	if( $info < $adims[$di] && $info > 0){
		$info--;
		barf("mpossolvex: Can't solve system of linear equations:\n".
			"the leading minor of order $info of A is".
                         " not positive definite");
		return;
	}
	elsif ( $info  and $_laerror){
		warn("mpossolvex: The matrix is singular to working precision");
	}
	my %result = (rcondition=>$rcond, ferror=>$ferr, berror=>$berr);
	if ($opt{equilibrate}){
		$result{equilibration} = $equed;
		if ($equed){
			$result{scale} = $s if $equed;
			$result{A} = $a if $opt{A};
			$result{B} = $b->t->sever if $opt{B};
		}
	}
	$result{info} = $info;
        $result{L} = $af if $opt{L};
        $result{U} = $af if $opt{U};
	wantarray ? ($x->t->sever, %result): $x->t->sever;
}

=head2 mlls

=for ref

Solves overdetermined or underdetermined real linear systems using QR or LQ factorization.

If M > N in the M-by-N matrix A, returns the residual sum of squares too.
Uses L<gels|PDL::LinearAlgebra::Real/gels> or L<cgels|PDL::LinearAlgebra::Complex/cgels> from Lapack.
Works on transposed arrays.

=for usage

 PDL(X) = mlls(PDL(A), PDL(B), SCALAR(trans))
 trans : NOTRANSPOSE  = 0 | TRANSPOSE/CONJUGATE = 1, default = 0

=for example

 $a = random(4,5);
 $b = random(3,5);
 ($x, $res) = mlls($a, $b);

=cut

*mlls = \&PDL::mlls;

sub PDL::mlls {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	&_matrices_matchrows;
	my($a, $b, $trans) = @_;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	my $x;
	$a = $a->copy;
	if ( $adims[$di+1] < $adims[$di]) {
		$x = $a->_similar($adims[$di], $bdims[$di]);
		$x->slice("$slice_prefix:@{[$bdims[$di+1]-1]}, :@{[$bdims[$di]-1]}") .= $b->t;
	} else {
		$x = $b->t->copy;
	}
	$a->_call_method('gels', $trans ? 0 : 1, $x, my $info = null);
	$x = $x->t;
	return $x->sever if $adims[$di+1] <= $adims[$di];
	my $sliced = $x->slice("$slice_prefix, :@{[$adims[$di]-1]}")->sever;
	return $sliced if !wantarray;
	my $power = $a->_similar(1); $power .= 2;
	($sliced, ($x->slice("$slice_prefix, $adims[$di]:")->t ** $power)->sumover);
}

=head2 mllsy

=for ref

Computes the minimum-norm solution to a real linear least squares problem
using a complete orthogonal factorization.

Uses L<gelsy|PDL::LinearAlgebra::Real/gelsy> or L<cgelsy|PDL::LinearAlgebra::Complex/cgelsy>
from Lapack. Works on transposed arrays.

=for usage

 ( PDL(X), ( HASH(result) ) ) = mllsy(PDL(A), PDL(B))
 Returned values:
		X (SCALAR CONTEXT),
		HASH{'A'}:
		 complete orthogonal factorization of A
		HASH{'jpvt'}:
		 details of columns interchanges
		HASH{'rank'}:
		 effective rank of A

=for example

 my $a = random(10,10);
 my $b = random(10,10);
 $X = mllsy($a, $b);

=cut

*mllsy = \&PDL::mllsy;
sub PDL::mllsy {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	&_matrices_matchrows;
	my($a, $b) = @_;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	my $rcond = lamch(0);
	$rcond = $rcond->sqrt - ($rcond->sqrt - $rcond) / 2;
	$a = $a->t->copy;
	my ($x);
	if ( $adims[1+$di] < $adims[0+$di]){
		$x = $a->_similar($adims[$di], $bdims[$di]);
		$x->slice("$slice_prefix:@{[$bdims[$di+1]-1]}, :@{[$bdims[$di]-1]}") .= $b->t;
	}
	else{
		$x = $b->t->copy;
	}
	my $info = null;
	my $rank = null;
	my $jpvt = zeroes(long, $adims[$di]);
	$a->_call_method('gelsy', $x,  $rcond, $jpvt, $rank, $info);
	my %ret = !wantarray ? () : ('A'=> $a->t->sever, 'rank' => $rank, 'jpvt'=>$jpvt);
	return wantarray ? ($x->t->sever, %ret) : $x->t->sever if $adims[$di+1] <= $adims[$di];
	$x = $x->t->slice("$slice_prefix, :@{[$adims[$di]-1]}")->sever;
	wantarray ? ($x, %ret) : $x;
}

=head2 mllss

=for ref

Computes the minimum-norm solution to a real linear least squares problem
using a singular value decomposition.

Uses L<gelss|PDL::LinearAlgebra::Real/gelss> or L<gelsd|PDL::LinearAlgebra::Real/gelsd> from Lapack.
Works on transposed arrays.

=for usage

 ( PDL(X), ( HASH(result) ) )= mllss(PDL(A), PDL(B), SCALAR(method))
 method: specifies which method to use (see Lapack for further details)
	'(c)gelss' or '(c)gelsd', default = '(c)gelsd'
 Returned values:
		X (SCALAR CONTEXT),
		HASH{'V'}:
		 if method = (c)gelss, the right singular vectors, stored columnwise
		HASH{'s'}:
		 singular values from SVD
		HASH{'res'}:
		 if A has full rank the residual sum-of-squares for the solution
		HASH{'rank'}:
		 effective rank of A
		HASH{'info'}:
		 info output from method

=for example

 my $a = random(10,10);
 my $b = random(10,10);
 $X = mllss($a, $b);

=cut

*mllss = \&PDL::mllss;

sub PDL::mllss {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	&_matrices_matchrows;
	my($a, $b, $method) = @_;
	my @adims = $a->dims;
	my @bdims = $b->dims;
	#TODO: Add this in option
	my $rcond = lamch(0);
	$rcond = $rcond->sqrt - ($rcond->sqrt - $rcond) / 2;
	$a = $a->t->copy;
	my $x;
	if ($adims[1+$di] < $adims[0+$di]){
		$x = $a->_similar($adims[$di], $bdims[$di]);
		$x->slice("$slice_prefix:@{[$bdims[$di+1]-1]}, :@{[$bdims[$di]-1]}") .= $b->t;
	}
	else{
		$x = $b->t->copy;
	}
	$_ = null for my ($info, $rank, $s);
	my $min = ($adims[$di] > $adims[$di+1]) ? $adims[$di+1] : $adims[$di];
	$method ||= 'gelsd';
	$a->_call_method($method, $x,  $rcond, $s, $rank, $info);
	laerror("mllss: The algorithm for computing the SVD failed to converge\n") if $info;
	$x = $x->t;
	my %ret = !wantarray ? () : (rank => $rank, s=>$s, info=>$info);
	$ret{V} = $a if wantarray and $method =~ /gelss/;
	return wantarray ? ($x->sever, %ret) : $x->sever if $adims[1+$di] <= $adims[0+$di];
	if (wantarray and $rank == $min) {
		my $power = $a->_similar(1); $power .= 2;
		$ret{res} = ($x->slice("$slice_prefix, $adims[$di]:")->t ** $power)->sumover;
	}
	$x = $x->slice("$slice_prefix, :@{[$adims[$di]-1]}")->sever;
	wantarray ? ($x->sever, %ret) : $x->sever;
}

=head2 mglm

=for ref

Solves a general Gauss-Markov Linear Model (GLM) problem.
Supports threading.
Uses L<ggglm|PDL::LinearAlgebra::Real/ggglm> or L<cggglm|PDL::LinearAlgebra::Complex/cggglm>
from Lapack. Works on transposed arrays.

=for usage

 (PDL(x), PDL(y)) = mglm(PDL(a), PDL(b), PDL(d))
 where d is the left hand side of the GLM equation

=for example

 my $a = random(8,10);
 my $b = random(7,10);
 my $d = random(10);
 my ($x, $y) = mglm($a, $b, $d);

=cut

*mglm = \&PDL::mglm;
sub PDL::mglm{
	my($a, $b, $d) = @_;
	my $di = $_[0]->dims_internal;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	my(@ddims) = $d->dims;
	barf("mglm: Require arrays with equal number of rows")
		unless( @adims >= 2+$di && @bdims >= 2+$di && $adims[1+$di] == $bdims[1+$di]);
	barf "mglm: Require that column(A) <= row(A) <= column(A) + column(B)" unless
		( ($adims[0+$di] <= $adims[1+$di] ) && ($adims[1+$di] <= ($adims[0+$di] + $bdims[0+$di])) );
	barf("mglm: Require vector(s) with size equal to number of rows of A")
		unless( @ddims >= 1+$di  && $adims[1+$di] == $ddims[0+$di]);
	$a = $a->t->copy;
	$b = $b->t->copy;
	$d = $d->copy;
	my ($x, $y, $info) = $a->_call_method('ggglm', $b, $d);
	$x, $y;
}

=head2 mlse

=for ref

Solves a linear equality-constrained least squares (LSE) problem.
Uses L<gglse|PDL::LinearAlgebra::Real/gglse> or L<cgglse|PDL::LinearAlgebra::Complex/cgglse>
from Lapack. Works on transposed arrays.

=for usage

 (PDL(x), PDL(res2)) = mlse(PDL(a), PDL(b), PDL(c), PDL(d))
 where
 c	: The right hand side vector for the
	  least squares part of the LSE problem.
 d	: The right hand side vector for the
	  constrained equation.
 x	: The solution of the LSE problem.
 res2	: The residual sum of squares for the solution
	  (returned only in array context)


=for example

 my $a = random(5,4);
 my $b = random(5,3);
 my $c = random(4);
 my $d = random(3);
 my ($x, $res2) = mlse($a, $b, $c, $d);

=cut

*mlse = \&PDL::mlse;

sub PDL::mlse {
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my($a, $b, $c, $d) = @_;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	my(@cdims) = $c->dims;
	my(@ddims) = $d->dims;
	&_matrices_matchcolumns;
	barf("mlse: Require 1D vector C with size equal to number of A rows")
		unless( (@cdims == $di+1)&& $adims[$di+2] == $cdims[$di+2]);
	barf("mlse: Require 1D vector D with size equal to number of B rows")
		unless( (@ddims == $di+1)&& $bdims[$di+2] == $ddims[$di+2]);
	barf "mlse: Require that row(B) <= column(A) <= row(A) + row(B)" unless
		( ($bdims[$di+1] <= $adims[$di] ) && ($adims[$di] <= ($adims[$di+1]+ $bdims[$di+1])) );
	$a = $a->t->copy;
	$b = $b->t->copy;
	$c = $c->copy;
	$d = $d->copy;
	my ($x, $info) = $a->_call_method('gglse', $b, $c, $d);
	my $power = $a->_similar(1); $power .= 2;
	my $sumsq = ($c->slice("$slice_prefix @{[$adims[$di]-$bdims[$di+1]]}:@{[$adims[$di+1]-1]}") ** $power)->sumover;
	wantarray ? ($x, $sumsq) : $x;
}

=head2 meigen

=for ref

Computes eigenvalues and, optionally, the left and/or right eigenvectors of a general square matrix
(spectral decomposition).
Eigenvectors are normalized (Euclidean norm = 1) and largest component real.
The eigenvalues and eigenvectors returned are complex ndarrays.
If only eigenvalues are requested, info is returned in array context.
Supports threading.
Uses L<geev|PDL::LinearAlgebra::Real/geev> or L<cgeev|PDL::LinearAlgebra::Complex/cgeev> from Lapack.
Works on transposed arrays.

=for usage

 (PDL(values), (PDL(LV),  (PDL(RV)), (PDL(info)))) = meigen(PDL, SCALAR(left vector), SCALAR(right vector))
 left vector  : FALSE = 0 | TRUE = 1, default = 0
 right vector : FALSE = 0 | TRUE = 1, default = 0

=for example

 my $a = random(10,10);
 my ( $eigenvalues, $left_eigenvectors, $right_eigenvectors )  = meigen($a,1,1);

=cut

*meigen = \&PDL::meigen;
sub PDL::meigen {
	&_square;
	my ($m,$jobvl,$jobvr) = @_;
	$_ = null for my ($info, $sdim);
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	$_ = $m->_similar_null for my ($vl, $vr);
	$m->t->_call_method('geev', $jobvl, $jobvr, @w, $vl, $vr, $info);
	_error($info, "meigen: The QR algorithm failed to converge for PDL(s) %s");
	(my $w, $vl, $vr) = _eigen_extract($jobvl, $jobvr, $vl, $vr, @w);
	return $w if !wantarray;
	($w, ($jobvl?$vl->t->sever:()), ($jobvr?$vr->t->sever:()), $info);
}

=head2 meigenx

=for ref

Computes eigenvalues, one-norm and, optionally, the left and/or right eigenvectors of a general square matrix
(spectral decomposition).
Eigenvectors are normalized (Euclidean norm = 1) and largest component real.
The eigenvalues and eigenvectors returned are complex ndarrays.
Uses L<geevx|PDL::LinearAlgebra::Real/geevx> or
L<cgeevx|PDL::LinearAlgebra::Complex/cgeevx> from Lapack.
Works on transposed arrays.

=for usage

 (PDL(value), (PDL(lv),  (PDL(rv)), HASH(result)), HASH(result)) = meigenx(PDL, HASH(options))
 where options are:
 vector:     eigenvectors to compute
		'left':  computes left eigenvectors
		'right': computes right eigenvectors
		'all':   computes left and right eigenvectors
		 0:     doesn't compute (default)
 rcondition: reciprocal condition numbers to compute (returned in HASH{'rconde'} for eigenvalues and HASH{'rcondv'} for eigenvectors)
		'value':  computes reciprocal condition numbers for eigenvalues
		'vector': computes reciprocal condition numbers for eigenvectors
		'all':    computes reciprocal condition numbers for eigenvalues and eigenvectors
		 0:      doesn't compute (default)
 error:      specifies whether or not it computes the error bounds (returned in HASH{'eerror'} and HASH{'verror'})
	     error bound = EPS * One-norm / rcond(e|v)
	     (reciprocal condition numbers for eigenvalues or eigenvectors must be computed).
		1: returns error bounds
		0: not computed
 scale:      specifies whether or not it diagonaly scales the entry matrix
	     (scale details returned in HASH : 'scale')
		1: scales
		0: Doesn't scale (default)
 permute:    specifies whether or not it permutes row and columns
	     (permute details returned in HASH{'balance'})
		1: permutes
		0: Doesn't permute (default)
 schur:      specifies whether or not it returns the Schur form (returned in HASH{'schur'})
		1: returns Schur form
		0: not returned
 Returned values:
	    eigenvalues (SCALAR CONTEXT),
	    left eigenvectors if requested,
	    right eigenvectors if requested,
	    HASH{'norm'}:
		One-norm of the matrix
	    HASH{'info'}:
		Info: if > 0, the QR algorithm failed to compute all the eigenvalues
		(see syevx for further details)

=for example

 my $a = random(10,10);
 my %options = ( rcondition => 'all',
             vector => 'all',
             error => 1,
             scale => 1,
             permute=>1,
             schur => 1
             );
 my ( $eigenvalues, $left_eigenvectors, $right_eigenvectors, %result)  = meigenx($a,%options);
 print "Error bounds for eigenvalues:\n $eigenvalues\n are:\n". transpose($result{'eerror'}) unless $info;

=cut

my %rcondition2sense = (value => 1, vector => 2, all => 3);
my %vector2jobvl = (left => 1, all => 1);
my %vector2jobvr = (right => 1, all => 1);
*meigenx = \&PDL::meigenx;
sub PDL::meigenx {
	&_square;
	my($m, %opt) = @_;
	my (%result);
	$m = $m->copy;
	$_ = null for my ($info, $ilo, $ihi, $abnrm, $scale, $rconde, $rcondv);
	$_ = $m->_similar_null for my ($vl, $vr);
	my @w = map $m->_similar_null, $m->_is_complex ? 1 : 1..2;
	my $balanc = ($opt{'scale'}?2:0) | ($opt{permute}?1:0);
	my $jobvl = $vector2jobvl{$opt{vector}} || $opt{rcondition} ? 1 : 0;
	my $jobvr = $vector2jobvr{$opt{vector}} || $opt{rcondition} ? 1 : 0;
	my $sense = $rcondition2sense{$opt{rcondition}} || 0;
	$m->t->_call_method('geevx', $jobvl, $jobvr, $balanc, $sense, @w, $vl, $vr, $ilo, $ihi, $scale, $abnrm, $rconde, $rcondv, $info);
	(my $w, $vl, $vr) = _eigen_extract($jobvl, $jobvr, $vl, $vr, @w);
	if ($info){
		laerror("meigenx: The QR algorithm failed to converge");
		print "Returning converged eigenvalues\n" if $_laerror;
	}
	$result{'schur'} = $m if $opt{'schur'};
	$result{'balance'} = cat $ilo, $ihi if $opt{'permute'};
	@result{qw(info norm)} = ($info, $abnrm);
	$result{'scale'} =  $scale if $opt{'scale'};
	if ($sense & 2) {
		$result{'rcondv'} =  $rcondv;
		$result{'verror'} = (lamch(0)* $abnrm /$rcondv  ) if $opt{'error'};
	}
	if ($sense & 1) {
		$result{'rconde'} =  $rconde;
		$result{'eerror'} = (lamch(0)* $abnrm /$rconde  ) if $opt{'error'};
	}
	($w, ($vector2jobvl{$opt{vector}}?$vl->t->sever:()), ($vector2jobvr{$opt{vector}}?$vr->t->sever:()), %result);
}

=head2 mgeigen

=for ref

Computes generalized eigenvalues and, optionally, the left and/or right generalized eigenvectors
for a pair of N-by-N real nonsymmetric matrices (A,B) .
The alpha from ratio alpha/beta is a complex ndarray.
Supports threading. Uses L<ggev|PDL::LinearAlgebra::Real/ggev> or
L<cggev|PDL::LinearAlgebra::Complex/cggev> from Lapack.
Works on transposed arrays.

=for usage

 ( PDL(alpha), PDL(beta), ( PDL(LV),  (PDL(RV) ), PDL(info)) = mgeigen(PDL(A),PDL(B) SCALAR(left vector), SCALAR(right vector))
 left vector  : FALSE = 0 | TRUE = 1, default = 0
 right vector : FALSE = 0 | TRUE = 1, default = 0

=for example

 my $a = random(10,10);
 my $b = random(10,10);
 my ( $alpha, $beta, $left_eigenvectors, $right_eigenvectors )  = mgeigen($a, $b,1, 1);

=cut

*mgeigen = \&PDL::mgeigen;
sub PDL::mgeigen {
	&_square_same;
	&_same_dims;
	my ($a,$b,$jobvl,$jobvr) = @_;
	$_ = $_->new_or_inplace->t for $a, $b;
	$_ = null for my ($info, $sdim);
	my @w = map $a->_similar_null, $a->_is_complex ? 1 : 1..2;
	$_ = $a->_similar_null for my ($vl, $vr, $beta);
	$a->_call_method('ggev', $jobvl, $jobvr, $b, @w, $beta, $vl, $vr, $info);
	_error($info, "mgeigen: Can't compute eigenvalues/vectors for PDL(s) %s");
	(my $w, $vl, $vr) = _eigen_extract($jobvl, $jobvr, $vl, $vr, @w);
	($w, $beta, ($jobvl?$vl->t->sever:()), ($jobvr?$vr->t->sever:()), $info);
}

=head2 mgeigenx

=for ref

Computes generalized eigenvalues, one-norms and, optionally, the left and/or right generalized
eigenvectors for a pair of N-by-N real nonsymmetric matrices (A,B).
The alpha from ratio alpha/beta is a complex ndarray.
Uses L<ggevx|PDL::LinearAlgebra::Real/ggevx> or
L<cggevx|PDL::LinearAlgebra::Complex/cggevx> from Lapack.
Works on transposed arrays.

=for usage

 (PDL(alpha), PDL(beta), PDL(lv),  PDL(rv), HASH(result) ) = mgeigenx(PDL(a), PDL(b), HASH(options))
 where options are:
 vector:     eigenvectors to compute
		'left':  computes left eigenvectors
		'right': computes right eigenvectors
		'all':   computes left and right eigenvectors
		 0:     doesn't compute (default)
 rcondition: reciprocal condition numbers to compute (returned in HASH{'rconde'} for eigenvalues and HASH{'rcondv'} for eigenvectors)
		'value':  computes reciprocal condition numbers for eigenvalues
		'vector': computes reciprocal condition numbers for eigenvectors
		'all':    computes reciprocal condition numbers for eigenvalues and eigenvectors
		 0:      doesn't compute (default)
 error:      specifies whether or not it computes the error bounds (returned in HASH{'eerror'} and HASH{'verror'})
	     error bound = EPS * sqrt(one-norm(a)**2 + one-norm(b)**2) / rcond(e|v)
	     (reciprocal condition numbers for eigenvalues or eigenvectors must be computed).
		1: returns error bounds
		0: not computed
 scale:      specifies whether or not it diagonaly scales the entry matrix
	     (scale details returned in HASH : 'lscale' and 'rscale')
		1: scales
		0: doesn't scale (default)
 permute:    specifies whether or not it permutes row and columns
	     (permute details returned in HASH{'balance'})
		1: permutes
		0: Doesn't permute (default)
 schur:      specifies whether or not it returns the Schur forms (returned in HASH{'aschur'} and HASH{'bschur'})
	     (right or left eigenvectors must be computed).
		1: returns Schur forms
		0: not returned
 Returned values:
	    alpha,
	    beta,
	    left eigenvectors if requested,
	    right eigenvectors if requested,
	    HASH{'anorm'}, HASH{'bnorm'}:
		One-norm of the matrix A and B
	    HASH{'info'}:
		Info: if > 0, the QR algorithm failed to compute all the eigenvalues
		(see syevx for further details)

=for example

 $a = random(10,10);
 $b = random(10,10);
 %options = (rcondition => 'all',
             vector => 'all',
             error => 1,
             scale => 1,
             permute=>1,
             schur => 1
             );
 ($alpha, $beta, $left_eigenvectors, $right_eigenvectors, %result)  = mgeigenx($a, $b,%options);
 print "Error bounds for eigenvalues:\n $eigenvalues\n are:\n". transpose($result{'eerror'}) unless $info;

=cut

*mgeigenx = \&PDL::mgeigenx;
sub PDL::mgeigenx {
	&_square_same;
	my $di = $_[0]->dims_internal;
	my ($a, $b, %opt) = @_;
	my @adims = $a->dims;
	$_ = $_->new_or_inplace->t for $a, $b;
	my (%result);
	$_ = $a->_similar_null for my ($vl, $vr, $beta);
	my @w = map $a->_similar_null, $a->_is_complex ? 1 : 1..2;
	$_ = null for my ($rconde, $rcondv, $info, $ilo, $ihi, $rscale, $lscale, $abnrm, $bbnrm);
	my $jobvl = $vector2jobvl{$opt{vector}} || $opt{rcondition} ? 1 : 0;
	my $jobvr = $vector2jobvr{$opt{vector}} || $opt{rcondition} ? 1 : 0;
	my $sense = $rcondition2sense{$opt{rcondition}} || 0;
	my $balanc = ($opt{scale}?2:0) | ($opt{permute}?1:0);
	$a->_call_method('ggevx', $balanc, $jobvl, $jobvr, $sense, $b, @w,
	  $beta, $vl, $vr, $ilo, $ihi, $lscale, $rscale,
	  $abnrm, $bbnrm, $rconde, $rcondv, $info);
	(my $w, $vl, $vr) = _eigen_extract($jobvl, $jobvr, $vl, $vr, @w);
	if ($info > 0 && $info < $adims[$di+1]) {
		laerror("mgeigenx: The QZ algorithm failed to converge");
		print "Returning converged eigenvalues\n" if $_laerror;
	} elsif ($info) {
		laerror("mgeigenx: Error from hgeqz or tgevc");
	}
	$result{qw(aschur bschur)} = map $_->t, $a, $b if $opt{schur};
	$result{balance} = cat $ilo, $ihi if $opt{permute};
	@result{qw(info anorm bnorm)} = ($info, $abnrm, $bbnrm);
	@result{qw(lscale rscale)} =  ($lscale, $rscale) if $opt{scale};
	# Doesn't use lacpy2 =(sqrt **2 , **2) without unnecessary overflow
	if ($sense & 2) {
		$result{rcondv} =  $rcondv;
		$result{verror} = lamch(0) * sqrt($abnrm->pow(2) + $bbnrm->pow(2)) /$rcondv if $opt{error};
	}
	if ($sense & 1) {
		$result{rconde} =  $rconde;
		$result{eerror} = lamch(0) * sqrt($abnrm->pow(2) + $bbnrm->pow(2)) /$rconde if $opt{error};
	}
	($w, $beta, ($vector2jobvl{$opt{vector}}?$vl->t:()), ($vector2jobvr{$opt{vector}}?$vr->t:()), %result);
}

=head2 msymeigen

=for ref

Computes eigenvalues and, optionally eigenvectors of a real symmetric square or
complex Hermitian matrix (spectral decomposition).
The eigenvalues are computed from lower or upper triangular matrix.
If only eigenvalues are requested, info is returned in array context.
Supports threading and works inplace if eigenvectors are requested.
From Lapack, uses L<syev|PDL::LinearAlgebra::Real/syev> or L<syevd|PDL::LinearAlgebra::Real/syevd> for real
and L<cheev|PDL::LinearAlgebra::Complex/cheev> or L<cheevd|PDL::LinearAlgebra::Complex/cheevd> for complex.
Works on transposed array(s).

=for usage

 (PDL(values), (PDL(VECTORS)), PDL(info)) = msymeigen(PDL, SCALAR(uplo), SCALAR(vector), SCALAR(method))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 vector : FALSE = 0 | TRUE = 1, default = 0
 method : 'syev' | 'syevd' | 'cheev' | 'cheevd', default = 'syevd'|'cheevd'

=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my ( $eigenvalues, $eigenvectors )  = msymeigen($a,0,1, 'syev');

=cut

*msymeigen = \&PDL::msymeigen;
sub PDL::msymeigen {
	&_square;
	my($m, $upper, $jobv, $method) = @_;
	my ($w, $info) = (null, null);
	$method //= [ 'syevd', 'cheevd' ];
	$m = $m->copy unless ($m->is_inplace(0) and $jobv);
	$m->t->_call_method($method, $jobv, $upper, $w, $info);
	_error($info, "msymeigen: The algorithm failed to converge for PDL(s) %s");
	!wantarray ? $w : ($w, ($jobv?$m:()), $info);
}

=head2 msymeigenx

=for ref

Computes eigenvalues and, optionally eigenvectors of a symmetric square matrix (spectral decomposition).
The eigenvalues are computed from lower or upper triangular matrix and can be selected by specifying a
range. From Lapack, uses L<syevx|PDL::LinearAlgebra::Real/syevx> or
L<syevr|PDL::LinearAlgebra::Real/syevr> for real and L<cheevx|PDL::LinearAlgebra::Complex/cheevx>
or L<cheevr|PDL::LinearAlgebra::Complex/cheevr> for complex. Works on transposed arrays.

=for usage

 (PDL(value), (PDL(vector)), PDL(n), PDL(info), (PDL(support)) ) = msymeigenx(PDL, SCALAR(uplo), SCALAR(vector), HASH(options))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 vector : FALSE = 0 | TRUE = 1, default = 0
 where options are:
 range_type:    method for selecting eigenvalues
		indice:  range of indices
		interval: range of values
		0: find all eigenvalues and optionally all vectors
 range:		PDL(2), lower and upper bounds interval or smallest and largest indices
		1<=range<=N for indice
 abstol:        specifies error tolerance for eigenvalues
 method:        specifies which method to use (see Lapack for further details)
		'syevx' (default)
		'syevr'
		'cheevx' (default)
		'cheevr'
 Returned values:
		eigenvalues (SCALAR CONTEXT),
		eigenvectors if requested,
		total number of eigenvalues found (n),
		info
		issupz or ifail (support) according to method used and returned info,
		for (sy|che)evx returns support only if info != 0


=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my $overflow = lamch(9);
 my $range = cat pdl(0),$overflow;
 my $abstol = pdl(1.e-5);
 my %options = (range_type=>'interval',
		range => $range,
		abstol => $abstol,
		method=>'syevd');
 my ( $eigenvalues, $eigenvectors, $n, $isuppz )  = msymeigenx($a,0,1, %options);

=cut

*msymeigenx = \&PDL::msymeigenx;

my %range_type2range = (interval => 1, indice => 2);
sub PDL::msymeigenx {
	&_square;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my($m, $upper, $jobz, %opt) = @_;
	my(@dims) = $m->dims;
	my $range = $range_type2range{$opt{range_type}} || 0;
	if ((ref $opt{range}) ne 'PDL'){
		$opt{range} = pdl([0,0]);
		$range = 0;
	}
	elsif ($range == 2){
		barf "msymeigenx: Indices must be > 0" unless $opt{range}->(0) > 0;
		barf "msymeigenx: Indices must be <= $dims[$di+1]" unless $opt{range}->(1) <= $dims[$di+1];
	}
	elsif ($range == 1){
		barf "msymeigenx: Interval limits must be different" unless ($opt{range}->(0) !=  $opt{range}->(1));
	}
	$_ = null for my ($w, $n, $support, $info);
	my $z = $m->_similar_null;
	if (!defined $opt{'abstol'})
	{
		my $unfl = lamch(1);
		$unfl->labad(lamch(9));
		$opt{'abstol'} = $unfl + $unfl;
	}
	my $method = $opt{'method'} || ['syevx','cheevx'];
	$upper = $upper ? 0 : 1;
	$m = $m->copy;
	$m->_call_method($method, $jobz, $range, $upper, $opt{range}->(0), $opt{range}->(1),$opt{range}->(0),$opt{range}->(1),
		 $opt{'abstol'}, $n, $w, $z , $support, $info);
	if ($info){
		laerror("msymeigenx: The algorithm failed to converge.");
		print ("See support for details.\n") if $_laerror;
	}
	if ($jobz){
		return ($w, $z->t->sever, $n, $info, $support) if $info;
		return (undef,undef,$n,$info,$method =~ qr/evr/?$support:()) if $n == 0;
		return ($w(:$n-1)->sever, $z->t->slice("$slice_prefix:@{[$n->sclr-1]}")->sever, $n, $info, $method =~ qr/evr/?$support:());
	}
	else{
		return $w if !wantarray;
		if ($info){
			($w, $n, $info, $support);
		}
		else{
			($w(:$n-1)->sever, $n, $info, $method =~ qr/evr/?$support:());
		}
	}
}

=head2 msymgeigen

=for ref

Computes eigenvalues and, optionally eigenvectors of a real generalized
symmetric-definite or Hermitian-definite eigenproblem.
The eigenvalues are computed from lower or upper triangular matrix
If only eigenvalues are requested, info is returned in array context.
Supports threading. From Lapack, uses L<sygv|PDL::LinearAlgebra::Real/sygv> or L<sygvd|PDL::LinearAlgebra::Real/sygvd> for real
or L<chegv|PDL::LinearAlgebra::Complex/chegv> or L<chegvd|PDL::LinearAlgebra::Complex/chegvd> for complex.
Works on transposed array(s).

=for usage

 (PDL(values), (PDL(vectors)), PDL(info)) = msymgeigen(PDL(a), PDL(b),SCALAR(uplo), SCALAR(vector), SCALAR(type), SCALAR(method))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 vector : FALSE = 0 | TRUE = 1, default = 0
 type :
	1: A * x = (lambda) * B * x
	2: A * B * x = (lambda) * x
	3: B * A * x = (lambda) * x
	default = 1
 method : 'sygv' | 'sygvd' for real or  ,'chegv' | 'chegvd' for complex,  default = 'sygvd' | 'chegvd'

=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my $b = random(10,10);
 $b = $b->crossprod($b);
 my ( $eigenvalues, $eigenvectors )  = msymgeigen($a, $b, 0, 1, 1, 'sygv');

=cut

*msymgeigen = \&PDL::msymgeigen;
sub PDL::msymgeigen {
	&_square_same;
	&_same_dims;
	my($a, $b, $upper, $jobv, $type, $method) = @_;
	$type ||= 1;
	$method //= [ 'sygvd', 'chegvd' ];
	$upper = 1-$upper;
	$a = $a->copy;
	$b = $b->copy;
	$a->_call_method($method, $type, $jobv, $upper, $b, my $w = null, my $info = null);
	_error($info, "msymgeigen: Can't compute eigenvalues/vectors: matrix (PDL(s) %s) is/are not positive definite or the algorithm failed to converge");
	!wantarray ? $w : ($w, $jobv?$a->t->sever:(), $info);
}

=head2 msymgeigenx

=for ref

Computes eigenvalues and, optionally eigenvectors of a real generalized
symmetric-definite or Hermitian eigenproblem.
The eigenvalues are computed from lower or upper triangular matrix and can be selected by specifying a
range. Uses L<sygvx|PDL::LinearAlgebra::Real/syevx> or L<cheevx|PDL::LinearAlgebra::Complex/cheevx>
from Lapack. Works on transposed arrays.

=for usage

 (PDL(value), (PDL(vector)), PDL(info), PDL(n), (PDL(support)) ) = msymeigenx(PDL(a), PDL(b), SCALAR(uplo), SCALAR(vector), HASH(options))
 uplo : UPPER  = 0 | LOWER = 1, default = 0
 vector : FALSE = 0 | TRUE = 1, default = 0
 where options are:
 type :         Specifies the problem type to be solved
		1: A * x = (lambda) * B * x
		2: A * B * x = (lambda) * x
		3: B * A * x = (lambda) * x
		default = 1
 range_type:    method for selecting eigenvalues
		indice:  range of indices
		interval: range of values
		0: find all eigenvalues and optionally all vectors
 range:		PDL(2), lower and upper bounds interval or smallest and largest indices
		1<=range<=N for indice
 abstol:        specifies error tolerance for eigenvalues
 Returned values:
		eigenvalues (SCALAR CONTEXT),
		eigenvectors if requested,
		total number of eigenvalues found (n),
		info
		ifail according to returned info (support).

=for example

 # Assume $a is symmetric
 my $a = random(10,10);
 my $b = random(10,10);
 $b = $b->crossprod($b);
 my $overflow = lamch(9);
 my $range = cat pdl(0),$overflow;
 my $abstol = pdl(1.e-5);
 my %options = (range_type=>'interval',
		range => $range,
		abstol => $abstol,
		type => 1);
 my ( $eigenvalues, $eigenvectors, $n, $isuppz )  = msymgeigenx($a, $b, 0,1, %options);

=cut

*msymgeigenx = \&PDL::msymgeigenx;

sub PDL::msymgeigenx {
	&_square_same;
	my $di = $_[0]->dims_internal;
	my($a, $b, $upper, $jobv, %opt) = @_;
	my(@adims) = $a->dims;
	my $range = $range_type2range{$opt{range_type}} || 0;
	if (!UNIVERSAL::isa($opt{range},'PDL')){
		$opt{range} = pdl([0,0]);
		$range = 0;
	}
	$opt{type} //= 1;
	$_ = null for my ($w, $n, $support, $info);
	if (!defined $opt{'abstol'}){
		my $unfl = lamch(1);
		my $ovfl = lamch(9);
		$unfl->labad($ovfl);
		$opt{'abstol'} = $unfl + $unfl;
	}
	my $z = $a->_similar_null;
	$upper = $upper ? 0 : 1;
	$a = $a->copy;
	$b = $b->copy;
	$a->_call_method(['sygvx','chegvx'], $opt{type}, $jobv, $range, $upper,
	  $b, $opt{range}->(0), $opt{range}->(1),$opt{range}->(0),$opt{range}->(1),
	  $opt{'abstol'}, $n, $w, $z ,$support, $info);
	if ( ($info > 0) && ($info < $adims[$di+1])){
		laerror("msymgeigenx: The algorithm failed to converge");
		print("see support for details\n") if $_laerror;
	}
	elsif($info){
		$info = $info - $adims[$di+1] - 1;
		barf("msymgeigenx: The leading minor of order $info of B is not positive definite\n");
	}
	return $w if !wantarray;
	($w, $jobv?$z->t->sever:(), $n, $info, $info?$support:());
}


=head2 mdsvd

=for ref

Computes SVD using Coppen's divide and conquer algorithm.
Return singular values in scalar context else left (U),
singular values, right (V' (hermitian for complex)) singular vectors and info.
Supports threading.
If only singulars values are requested, info is only returned in array context.
Uses L<gesdd|PDL::LinearAlgebra::Real/gesdd> or L<cgesdd|PDL::LinearAlgebra::Complex/cgesdd> from Lapack.

=for usage

 (PDL(U), (PDL(s), PDL(V)), PDL(info)) = mdsvd(PDL, SCALAR(job))
 job :  0 = computes only singular values
	1 = computes full SVD (square U and V)
	2 = computes SVD (singular values, right and left singular vectors)
	default = 1

=for example

 my $a = random(5,10);
 my ($u, $s, $v) = mdsvd($a);

=cut

*mdsvd = \&PDL::mdsvd;
sub PDL::mdsvd {
	my $di = $_[0]->dims_internal;
	my($m, $jobz) = @_;
	my(@dims) = $m->dims;
	$jobz = !wantarray ? 0 : $jobz // 1;
	my $min = $dims[$di] > $dims[1+$di] ? $dims[1+$di]: $dims[$di];
	$m = $m->copy;
	$_ = $m->_similar_null for my ($u, $v);
	$m->_call_method('gesdd', $jobz, my $s = null, $u, $v, my $info = null);
	_error($info, "mdsvd: Matrix (PDL(s) %s) is/are singular");
	return ($u, $s, $v, $info) if $jobz;
	wantarray ? ($s, $info) : $s;
}

=head2 msvd

=for ref

Computes SVD.
Can compute singular values, either U or V or neither.
Return singular values in scalar context else left (U),
singular values, right (V' (hermitian for complex) singular vector and info.
Supports threading.
If only singular values are requested, info is returned in array context.
Uses L<gesvd|PDL::LinearAlgebra::Real/gesvd> or L<cgesvd|PDL::LinearAlgebra::Complex/cgesvd> from Lapack.

=for usage

 ( (PDL(U)), PDL(s), (PDL(V), PDL(info)) = msvd(PDL, SCALAR(jobu), SCALAR(jobv))
 jobu : 0 = Doesn't compute U
	1 = computes full SVD (square U)
	2 = computes right singular vectors
	default = 1
 jobv : 0 = Doesn't compute V
	1 = computes full SVD (square V)
	2 = computes left singular vectors
	default = 1

=for example

 my $a = random(10,10);
 my ($u, $s, $v) = msvd($a);

=cut

*msvd = \&PDL::msvd;
sub PDL::msvd {
	my ($m, $jobu, $jobv) = @_;
	$jobu = !wantarray ? 0 : $jobu // 1;
	$jobv = !wantarray ? 0 : $jobv // 1;
	$m = $m->copy;
	$_ = $m->_similar_null for my ($u, $v);
	$m->_call_method('gesvd', $jobv, $jobu,my $s = null, $u, $v, my $info = null);
	_error($info, "msvd: Matrix (PDL(s) %s) is/are singular");
	wantarray ? ($jobu?$u:(), $s, $jobv?$v:(), $info) : $s;
}

=head2 mgsvd

=for ref

Computes generalized (or quotient) singular value decomposition.
If the effective rank of (A',B')' is 0 return only unitary V, U, Q.
Handles complex data.
Uses L<ggsvd|PDL::LinearAlgebra::Real/ggsvd> or
L<cggsvd|PDL::LinearAlgebra::Complex/cggsvd> from Lapack. Works on transposed arrays.

=for usage

 (PDL(sa), PDL(sb), %ret) = mgsvd(PDL(a), PDL(b), %HASH(options))
 where options are:
 V:    whether or not computes V (boolean, returned in HASH{'V'})
 U:    whether or not computes U (boolean, returned in HASH{'U'})
 Q:    whether or not computes Q (boolean, returned in HASH{'Q'})
 D1:   whether or not computes D1 (boolean, returned in HASH{'D1'})
 D2:   whether or not computes D2 (boolean, returned in HASH{'D2'})
 0R:   whether or not computes 0R (boolean, returned in HASH{'0R'})
 R:    whether or not computes R (boolean, returned in HASH{'R'})
 X:    whether or not computes X (boolean, returned in HASH{'X'})
 all:  whether or not computes all the above.
 Returned value:
	 sa,sb		: singular value pairs of A and B (generalized singular values = sa/sb)
	 $ret{'rank'}   : effective numerical rank of (A',B')'
	 $ret{'info'}   : info from (c)ggsvd

=for example

 my $a = random(5,5);
 my $b = random(5,7);
 my ($c, $s, %ret) = mgsvd($a, $b, X => 1);

=cut

my @gsvd_opts = qw(V U Q D1 D2 0R R X);
*mgsvd = \&PDL::mgsvd;
sub PDL::mgsvd {
	&_matrices_matchcolumns;
	my $di = $_[0]->dims_internal;
	my $slice_prefix = ',' x $di;
	my @diag_args = ($di, $di+1);
	my($a, $b, %opt) = @_;
	my(@adims) = $a->dims;
	my(@bdims) = $b->dims;
	@opt{@gsvd_opts} = (1) x @gsvd_opts if $opt{all};
	my $type = $a->type;
	my $jobqx = ($opt{Q} || $opt{X}) ? 1 : 0;
	$a = $a->copy;
	$b = $b->t->copy;
	$_ = null for my ($k, $l, $alpha, $beta, $iwork, $info);
	$_ = $a->_similar_null for my ($U, $V, $Q);
	$a->t->_call_method('ggsvd', $opt{U}, $opt{V}, $jobqx, $b, $k, $l, $alpha, $beta, $U, $V, $Q, $iwork, $info);
	laerror("mgsvd: The Jacobi procedure fails to converge") if $info;
	my %ret = (rank=>$k + $l, info=>$info);
	warn "mgsvd: Effective rank of 0 in mgsvd" if (!$ret{rank} and $_laerror);
	if (%opt) {
		$Q = $Q->t->sever if $jobqx;
		if (($adims[$di+1] - $k - $l)  < 0  && $ret{rank}){
			if ( $opt{'0R'} || $opt{R} || $opt{X}){
				$a->reshape((map $a->dim($_-1), 1..$di), $adims[$di], ($k + $l));
				# Slice $a ???  => always square ??
				$a->slice("$slice_prefix@{[$adims[$di] - ($k+$l-$adims[$di+1])]} : , $adims[$di+1]:") .=
						$b->slice("$slice_prefix@{[$adims[$di+1]-$k]}:@{[$l-1]},@[[$adims[$di]+$adims[$di+1]-$k - $l]}:@{[$adims[$di]-1]}")->t;
				$ret{'0R'} = $a if $opt{'0R'};
			}
			if ($opt{'D1'}){
				my $D1 = zeroes($type, $adims[$di+1], $adims[$di+1]);
				$D1->diagonal(0,1) .= $alpha(:($adims[$di+1]-1));
				$D1 = $D1->t->reshape($adims[$di+1] , ($k+$l))->t->sever;
				$ret{'D1'} = $D1;
			}
		}
		elsif ($ret{rank}){
			if ( $opt{'0R'} || $opt{R} || $opt{X}){
				$a->reshape((map $a->dim($_-1), 1..$di), $adims[$di], ($k + $l));
				$ret{'0R'} = $a if $opt{'0R'};
			}
			if ($opt{'D1'}){
				my $D1 = zeroes($type, ($k + $l), ($k + $l));
				$D1->diagonal(0,1) .=  $alpha(:($k+$l-1));
				$D1->reshape(($k + $l), $adims[$di+1]);
				$ret{'D1'} = $D1;
			}
		}
		if ($opt{'D2'} && $ret{rank}){
			my $work = zeroes($b->type, $l, $l);
			$work->diagonal(0,1) .=  $beta($k:($k+$l-1));
			my $D2 = zeroes($b->type, ($k + $l), $bdims[$di+1]);
			$D2( $k:, :($l-1)  ) .= $work;
			$ret{'D2'} = $D2;
		}
		if ( $ret{rank} && ($opt{X} || $opt{R}) ){
			my $work = $a->slice("$slice_prefix@{[-($k + $l)]}:");
			$ret{R} = $work if $opt{R};
			if ($opt{X}){
				my $X = $a->_similar(@adims[$di,$di]);
				$X->diagonal(@diag_args) .= 1 if ($adims[$di] > ($k + $l));
				$X->slice("$slice_prefix@{[-($k + $l)]}:, @{[-($k + $l)]}:") .= mtriinv($work);
				$ret{X} = $Q x $X;
			}
		}
		$ret{U} = $U->t->sever if $opt{U};
		$ret{V} = $V->t->sever if $opt{V};
		$ret{Q} = $Q if $opt{Q};
	}
	$ret{rank} ? return ($alpha($k:($k+$l-1))->sever, $beta($k:($k+$l-1))->sever, %ret ) : (undef, undef, %ret);
}

#TODO

# Others things

#	rectangular diag
#	usage
#	is_inplace and function which modify entry matrix
#	threading support
#	automatically create PDL
#	inplace operation and memory
#	PDL type, verify float/double
#	eig_det qr_det
#	(g)schur(x):
#		if conjugate pair
#			non generalized pb: $seldim ?? (cf: generalized)
#			return conjugate pair if only selected?
#	port to PDL::Matrix

=head1 AUTHOR

Copyright (C) Grégory Vanuxem 2005-2018.

This library is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License as in the file Artistic_2
in this distribution.

=cut

1;