File: lbfgsb.c

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (6666 lines) | stat: -rw-r--r-- 231,788 bytes parent folder | download | duplicates (3)
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
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
/* opt/lbfgsb.f -- translated by f2c (version 20050501).
   You must link the resulting object file with libf2c:
        on Microsoft Windows system, link with libf2c.lib;
        on Linux or Unix systems, link with .../path/to/libf2c.a -lm
        or, if you install libf2c.a in a standard place, with -lf2c -lm
        -- in that order, at the end of the command line, as in
                cc *.o -lf2c -lm
        Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,

                http://www.netlib.org/f2c/libf2c.zip
*/

#ifdef __cplusplus
extern "C" {
#endif
#include "v3p_netlib.h"

#undef abs
#undef min
#undef max
#include <math.h>
#include <stdio.h>
#define abs(x) ((x) >= 0 ? (x) : -(x))
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define max(a,b) ((a) >= (b) ? (a) : (b))

/* Code below must be fixed to pass a real FILE* value to fprintf if
   this macro is defined.  */
#undef LBFGSB_ENABLE_ITERATE_FILE

static void lbfgsb_printf_vec(const char* name,
                              double const* v,
                              integer len)
{
/*<             write (6,1004) 'G =',(g(i), i = 1, n) >*/
/*
  1004 format (/,a4, 1p, 6(1x,d11.4),/,(4x,1p,6(1x,d11.4)))
*/
  integer i;
  printf("%s =", name);
  for(i = 1; i <= len; ++i)
    {
    printf(" %11.4g", v[i]);
    }
  printf("\n");
}

/* Table of constant values */

static doublereal c_b9 = 0.;
static integer c__1 = 1;
static integer c__11 = 11;
static doublereal c_b275 = .001;
static doublereal c_b276 = .9;
static doublereal c_b277 = .1;

/* ================    L-BFGS-B (version 2.1)   ========================== */
/*<    >*/
/* Subroutine */ int setulb_(integer *n, integer *m, doublereal *x, 
        doublereal *l, doublereal *u, integer *nbd, doublereal *f, doublereal 
        *g, doublereal *factr, doublereal *pgtol, doublereal *wa, integer *
        iwa, char *task, integer *iprint, char *csave, logical *lsave, 
        integer *isave, doublereal *dsave)
{
    /* System generated locals */
    integer i__1;

    /* Builtin functions */
    integer s_cmp(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    integer l1, l2, l3, ld, lr, lt, lz, lwa, lsg, lyg, lwn, lss, lws, lwt, 
            lsy, lwy, lyy, lsnd, lsgo, lygo;
    extern /* Subroutine */ int mainlb_(integer *, integer *, doublereal *, 
            doublereal *, doublereal *, integer *, doublereal *, doublereal *,
             doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, integer *, integer *, 
            integer *, char *, integer *, char *, logical *, integer *, 
            doublereal *, ftnlen, ftnlen);

/*<       character*60     task, csave >*/
/*<       logical          lsave(4) >*/
/*<    >*/
/*<    >*/
/*     ************ */

/*     Subroutine setulb */

/*     This subroutine partitions the working arrays wa and iwa, and */
/*       then uses the limited memory BFGS method to solve the bound */
/*       constrained optimization problem by calling mainlb. */
/*       (The direct method will be used in the subspace minimization.) */

/*     n is an integer variable. */
/*       On entry n is the dimension of the problem. */
/*       On exit n is unchanged. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric corrections */
/*         used to define the limited memory matrix. */
/*       On exit m is unchanged. */

/*     x is a double precision array of dimension n. */
/*       On entry x is an approximation to the solution. */
/*       On exit x is the current approximation. */

/*     l is a double precision array of dimension n. */
/*       On entry l is the lower bound on x. */
/*       On exit l is unchanged. */

/*     u is a double precision array of dimension n. */
/*       On entry u is the upper bound on x. */
/*       On exit u is unchanged. */

/*     nbd is an integer array of dimension n. */
/*       On entry nbd represents the type of bounds imposed on the */
/*         variables, and must be specified as follows: */
/*         nbd(i)=0 if x(i) is unbounded, */
/*                1 if x(i) has only a lower bound, */
/*                2 if x(i) has both lower and upper bounds, and */
/*                3 if x(i) has only an upper bound. */
/*       On exit nbd is unchanged. */

/*     f is a double precision variable. */
/*       On first entry f is unspecified. */
/*       On final exit f is the value of the function at x. */

/*     g is a double precision array of dimension n. */
/*       On first entry g is unspecified. */
/*       On final exit g is the value of the gradient at x. */

/*     factr is a double precision variable. */
/*       On entry factr >= 0 is specified by the user.  The iteration */
/*         will stop when */

/*         (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr*epsmch */

/*         where epsmch is the machine precision, which is automatically */
/*         generated by the code. Typical values for factr: 1.d+12 for */
/*         low accuracy; 1.d+7 for moderate accuracy; 1.d+1 for extremely */
/*         high accuracy. */
/*       On exit factr is unchanged. */

/*     pgtol is a double precision variable. */
/*       On entry pgtol >= 0 is specified by the user.  The iteration */
/*         will stop when */

/*                 max{|proj g_i | i = 1, ..., n} <= pgtol */

/*         where pg_i is the ith component of the projected gradient. */
/*       On exit pgtol is unchanged. */

/*     wa is a double precision working array of length */
/*       (2mmax + 4)nmax + 12mmax^2 + 12mmax. */

/*     iwa is an integer working array of length 3nmax. */

/*     task is a working string of characters of length 60 indicating */
/*       the current job when entering and quitting this subroutine. */

/*     iprint is an integer variable that must be set by the user. */
/*       It controls the frequency and type of output generated: */
/*        iprint<0    no output is generated; */
/*        iprint=0    print only one line at the last iteration; */
/*        0<iprint<99 print also f and |proj g| every iprint iterations; */
/*        iprint=99   print details of every iteration except n-vectors; */
/*        iprint=100  print also the changes of active set and final x; */
/*        iprint>100  print details of every iteration including x and g; */
/*       When iprint > 0, the file iterate.dat will be created to */
/*                        summarize the iteration. */

/*     csave is a working string of characters of length 60. */

/*     lsave is a logical working array of dimension 4. */
/*       On exit with 'task' = NEW_X, the following information is */
/*                                                             available: */
/*         If lsave(1) = .true.  then  the initial X has been replaced by */
/*                                     its projection in the feasible set; */
/*         If lsave(2) = .true.  then  the problem is constrained; */
/*         If lsave(3) = .true.  then  each variable has upper and lower */
/*                                     bounds; */

/*     isave is an integer working array of dimension 44. */
/*       On exit with 'task' = NEW_X, the following information is */
/*                                                             available: */
/*         isave(22) = the total number of intervals explored in the */
/*                         search of Cauchy points; */
/*         isave(26) = the total number of skipped BFGS updates before */
/*                         the current iteration; */
/*         isave(30) = the number of current iteration; */
/*         isave(31) = the total number of BFGS updates prior the current */
/*                         iteration; */
/*         isave(33) = the number of intervals explored in the search of */
/*                         Cauchy point in the current iteration; */
/*         isave(34) = the total number of function and gradient */
/*                         evaluations; */
/*         isave(36) = the number of function value or gradient */
/*                                  evaluations in the current iteration; */
/*         if isave(37) = 0  then the subspace argmin is within the box; */
/*         if isave(37) = 1  then the subspace argmin is beyond the box; */
/*         isave(38) = the number of free variables in the current */
/*                         iteration; */
/*         isave(39) = the number of active constraints in the current */
/*                         iteration; */
/*         n + 1 - isave(40) = the number of variables leaving the set of */
/*                           active constraints in the current iteration; */
/*         isave(41) = the number of variables entering the set of active */
/*                         constraints in the current iteration. */

/*     dsave is a double precision working array of dimension 29. */
/*       On exit with 'task' = NEW_X, the following information is */
/*                                                             available: */
/*         dsave(1) = current 'theta' in the BFGS matrix; */
/*         dsave(2) = f(x) in the previous iteration; */
/*         dsave(3) = factr*epsmch; */
/*         dsave(4) = 2-norm of the line search direction vector; */
/*         dsave(5) = the machine precision epsmch generated by the code; */
/*         dsave(7) = the accumulated time spent on searching for */
/*                                                         Cauchy points; */
/*         dsave(8) = the accumulated time spent on */
/*                                                 subspace minimization; */
/*         dsave(9) = the accumulated time spent on line search; */
/*         dsave(11) = the slope of the line search function at */
/*                                  the current point of line search; */
/*         dsave(12) = the maximum relative step length imposed in */
/*                                                           line search; */
/*         dsave(13) = the infinity norm of the projected gradient; */
/*         dsave(14) = the relative step length in the line search; */
/*         dsave(15) = the slope of the line search function at */
/*                                 the starting point of the line search; */
/*         dsave(16) = the square of the 2-norm of the line search */
/*                                                      direction vector. */

/*     Subprograms called: */

/*       L-BFGS-B Library ... mainlb. */


/*     References: */

/*       [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*       memory algorithm for bound constrained optimization'', */
/*       SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */

/*       [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: a */
/*       limited memory FORTRAN code for solving bound constrained */
/*       optimization problems'', Tech. Report, NAM-11, EECS Department, */
/*       Northwestern University, 1994. */

/*       (Postscript files of these papers are available via anonymous */
/*        ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.) */

/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<    >*/
/*<       if (task .eq. 'START') then >*/
    /* Parameter adjustments */
    --iwa;
    --g;
    --nbd;
    --u;
    --l;
    --x;
    --wa;
    --lsave;
    --isave;
    --dsave;

    /* Function Body */
    if (s_cmp(task, "START", (ftnlen)60, (ftnlen)5) == 0) {
/*<          isave(1)  = m*n >*/
        isave[1] = *m * *n;
/*<          isave(2)  = m**2 >*/
/* Computing 2nd power */
        i__1 = *m;
        isave[2] = i__1 * i__1;
/*<          isave(3)  = 4*m**2 >*/
/* Computing 2nd power */
        i__1 = *m;
        isave[3] = i__1 * i__1 << 2;
/*<          isave(4)  = 1 >*/
        isave[4] = 1;
/*<          isave(5)  = isave(4)  + isave(1) >*/
        isave[5] = isave[4] + isave[1];
/*<          isave(6)  = isave(5)  + isave(1) >*/
        isave[6] = isave[5] + isave[1];
/*<          isave(7)  = isave(6)  + isave(2) >*/
        isave[7] = isave[6] + isave[2];
/*<          isave(8)  = isave(7)  + isave(2) >*/
        isave[8] = isave[7] + isave[2];
/*<          isave(9)  = isave(8)  + isave(2) >*/
        isave[9] = isave[8] + isave[2];
/*<          isave(10) = isave(9)  + isave(2) >*/
        isave[10] = isave[9] + isave[2];
/*<          isave(11) = isave(10) + isave(3) >*/
        isave[11] = isave[10] + isave[3];
/*<          isave(12) = isave(11) + isave(3) >*/
        isave[12] = isave[11] + isave[3];
/*<          isave(13) = isave(12) + n >*/
        isave[13] = isave[12] + *n;
/*<          isave(14) = isave(13) + n >*/
        isave[14] = isave[13] + *n;
/*<          isave(15) = isave(14) + n >*/
        isave[15] = isave[14] + *n;
/*<          isave(16) = isave(15) + n >*/
        isave[16] = isave[15] + *n;
/*<          isave(17) = isave(16) + 8*m >*/
        isave[17] = isave[16] + (*m << 3);
/*<          isave(18) = isave(17) + m >*/
        isave[18] = isave[17] + *m;
/*<          isave(19) = isave(18) + m >*/
        isave[19] = isave[18] + *m;
/*<          isave(20) = isave(19) + m    >*/
        isave[20] = isave[19] + *m;
/*<       endif >*/
    }
/*<       l1   = isave(1) >*/
    l1 = isave[1];
/*<       l2   = isave(2) >*/
    l2 = isave[2];
/*<       l3   = isave(3) >*/
    l3 = isave[3];
/*<       lws  = isave(4) >*/
    lws = isave[4];
/*<       lwy  = isave(5) >*/
    lwy = isave[5];
/*<       lsy  = isave(6) >*/
    lsy = isave[6];
/*<       lss  = isave(7) >*/
    lss = isave[7];
/*<       lyy  = isave(8) >*/
    lyy = isave[8];
/*<       lwt  = isave(9) >*/
    lwt = isave[9];
/*<       lwn  = isave(10) >*/
    lwn = isave[10];
/*<       lsnd = isave(11) >*/
    lsnd = isave[11];
/*<       lz   = isave(12) >*/
    lz = isave[12];
/*<       lr   = isave(13) >*/
    lr = isave[13];
/*<       ld   = isave(14) >*/
    ld = isave[14];
/*<       lt   = isave(15) >*/
    lt = isave[15];
/*<       lwa  = isave(16) >*/
    lwa = isave[16];
/*<       lsg  = isave(17) >*/
    lsg = isave[17];
/*<       lsgo = isave(18) >*/
    lsgo = isave[18];
/*<       lyg  = isave(19) >*/
    lyg = isave[19];
/*<       lygo = isave(20) >*/
    lygo = isave[20];
/*<    >*/
    mainlb_(n, m, &x[1], &l[1], &u[1], &nbd[1], f, &g[1], factr, pgtol, &wa[
            lws], &wa[lwy], &wa[lsy], &wa[lss], &wa[lyy], &wa[lwt], &wa[lwn], 
            &wa[lsnd], &wa[lz], &wa[lr], &wa[ld], &wa[lt], &wa[lwa], &wa[lsg],
             &wa[lsgo], &wa[lyg], &wa[lygo], &iwa[1], &iwa[*n + 1], &iwa[(*n 
            << 1) + 1], task, iprint, csave, &lsave[1], &isave[22], &dsave[1],
             (ftnlen)60, (ftnlen)60);
/*<       return >*/
    return 0;
/*<       end >*/
} /* setulb_ */

/* ======================= The end of setulb ============================= */
/*<    >*/
/* Subroutine */ int mainlb_(integer *n, integer *m, doublereal *x, 
        doublereal *l, doublereal *u, integer *nbd, doublereal *f, doublereal 
        *g, doublereal *factr, doublereal *pgtol, doublereal *ws, doublereal *
        wy, doublereal *sy, doublereal *ss, doublereal *yy, doublereal *wt, 
        doublereal *wn, doublereal *snd, doublereal *z__, doublereal *r__, 
        doublereal *d__, doublereal *t, doublereal *wa, doublereal *sg, 
        doublereal *sgo, doublereal *yg, doublereal *ygo, integer *index, 
        integer *iwhere, integer *indx2, char *task, integer *iprint, char *
        csave, logical *lsave, integer *isave, doublereal *dsave, ftnlen 
        task_len, ftnlen csave_len)
{
    /* System generated locals */
    integer ws_dim1, ws_offset, wy_dim1, wy_offset, sy_dim1, sy_offset, 
            ss_dim1, ss_offset, yy_dim1, yy_offset, wt_dim1, wt_offset, 
            wn_dim1, wn_offset, snd_dim1, snd_offset, i__1;
    doublereal d__1, d__2;

    /* Builtin functions */
    integer s_cmp(char *, char *, ftnlen, ftnlen);
    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    integer i__, k;
    doublereal gd, dr, rr, dtd;
    integer col;
    doublereal tol;
    logical wrk;
    doublereal stp;
    doublereal cpu1;
    doublereal cpu2;
    integer head;
    doublereal fold;
    integer nact;
    doublereal ddum;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    integer info;
    doublereal time;
    integer nfgv, ifun, iter, nint;
    char word[3];
    doublereal time1, time2;
    integer iback;
    extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
            integer *);
    doublereal gdold;
    integer nfree;
    logical boxed;
    integer itail;
    doublereal theta;
    extern /* Subroutine */ int freev_(integer *, integer *, integer *, 
            integer *, integer *, integer *, integer *, logical *, logical *, 
            logical *, integer *, integer *), dcopy_(integer *, doublereal *, 
            integer *, doublereal *, integer *);
    doublereal dnorm;
    extern /* Subroutine */ int timer_(doublereal *), formk_(integer *, 
            integer *, integer *, integer *, integer *, integer *, integer *, 
            logical *, doublereal *, doublereal *, integer *, doublereal *, 
            doublereal *, doublereal *, doublereal *, integer *, integer *, 
            integer *);
    integer nskip, iword;
    extern /* Subroutine */ int formt_(integer *, doublereal *, doublereal *, 
            doublereal *, integer *, doublereal *, integer *), subsm_(integer 
            *, integer *, integer *, integer *, doublereal *, doublereal *, 
            integer *, doublereal *, doublereal *, doublereal *, doublereal *,
             doublereal *, integer *, integer *, integer *, doublereal *, 
            doublereal *, integer *, integer *);
    doublereal xstep, stpmx;
    extern /* Subroutine */ int prn1lb_(integer *, integer *, doublereal *, 
            doublereal *, doublereal *, integer *, integer *, doublereal *), 
            prn2lb_(integer *, doublereal *, doublereal *, doublereal *, 
            integer *, integer *, integer *, integer *, integer *, doublereal 
            *, integer *, char *, integer *, integer *, doublereal *, 
            doublereal *, ftnlen), prn3lb_(integer *, doublereal *, 
            doublereal *, char *, integer *, integer *, integer *, integer *, 
            integer *, integer *, integer *, integer *, doublereal *, 
            doublereal *, integer *, char *, integer *, doublereal *, 
            doublereal *, integer *, doublereal *, doublereal *, doublereal *,
             ftnlen, ftnlen);
    integer ileave;
    extern /* Subroutine */ int errclb_(integer *, integer *, doublereal *, 
            doublereal *, doublereal *, integer *, char *, integer *, integer 
            *, ftnlen);
    doublereal cachyt;
    integer itfile;
    extern /* Subroutine */ int active_(integer *, doublereal *, doublereal *,
             integer *, doublereal *, integer *, integer *, logical *, 
            logical *, logical *), cauchy_(integer *, doublereal *, 
            doublereal *, doublereal *, integer *, doublereal *, integer *, 
            integer *, doublereal *, doublereal *, doublereal *, integer *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, integer *, integer *, doublereal *, doublereal *, 
            doublereal *, doublereal *, integer *, doublereal *, doublereal *,
             integer *, doublereal *, integer *, doublereal *);
    doublereal epsmch;
    extern /* Subroutine */ int cmprlb_(integer *, integer *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, integer *,
             doublereal *, integer *, integer *, integer *, logical *, 
            integer *);
    logical updatd;
    doublereal sbtime;
    logical prjctd;
    integer iupdat;
    extern doublereal dpmeps_();
    logical cnstnd;
    doublereal sbgnrm;
    integer nenter;
    doublereal lnscht;
    extern /* Subroutine */ int lnsrlb_(integer *, doublereal *, doublereal *,
             integer *, doublereal *, doublereal *, doublereal *, doublereal *
            , doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, integer *, integer *, 
            integer *, integer *, integer *, char *, logical *, logical *, 
            char *, integer *, doublereal *, ftnlen, ftnlen), matupd_(integer 
            *, integer *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, integer *, integer *, 
            integer *, integer *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *);
    integer nintol;
    extern /* Subroutine */ int projgr_(integer *, doublereal *, doublereal *,
             integer *, doublereal *, doublereal *, doublereal *);

    (void)task_len;
    (void)csave_len;
    
    cpu1 = 0;
    cpu2 = 0;

/*<       character*60     task, csave >*/
/*<       logical          lsave(4) >*/
/*<    >*/
/*<    >*/
/*     ************ */

/*     Subroutine mainlb */

/*     This subroutine solves bound constrained optimization problems by */
/*       using the compact formula of the limited memory BFGS updates. */

/*     n is an integer variable. */
/*       On entry n is the number of variables. */
/*       On exit n is unchanged. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric */
/*          corrections allowed in the limited memory matrix. */
/*       On exit m is unchanged. */

/*     x is a double precision array of dimension n. */
/*       On entry x is an approximation to the solution. */
/*       On exit x is the current approximation. */

/*     l is a double precision array of dimension n. */
/*       On entry l is the lower bound of x. */
/*       On exit l is unchanged. */

/*     u is a double precision array of dimension n. */
/*       On entry u is the upper bound of x. */
/*       On exit u is unchanged. */

/*     nbd is an integer array of dimension n. */
/*       On entry nbd represents the type of bounds imposed on the */
/*         variables, and must be specified as follows: */
/*         nbd(i)=0 if x(i) is unbounded, */
/*                1 if x(i) has only a lower bound, */
/*                2 if x(i) has both lower and upper bounds, */
/*                3 if x(i) has only an upper bound. */
/*       On exit nbd is unchanged. */

/*     f is a double precision variable. */
/*       On first entry f is unspecified. */
/*       On final exit f is the value of the function at x. */

/*     g is a double precision array of dimension n. */
/*       On first entry g is unspecified. */
/*       On final exit g is the value of the gradient at x. */

/*     factr is a double precision variable. */
/*       On entry factr >= 0 is specified by the user.  The iteration */
/*         will stop when */

/*         (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr*epsmch */

/*         where epsmch is the machine precision, which is automatically */
/*         generated by the code. */
/*       On exit factr is unchanged. */

/*     pgtol is a double precision variable. */
/*       On entry pgtol >= 0 is specified by the user.  The iteration */
/*         will stop when */

/*                 max{|proj g_i | i = 1, ..., n} <= pgtol */

/*         where pg_i is the ith component of the projected gradient. */
/*       On exit pgtol is unchanged. */

/*     ws, wy, sy, and wt are double precision working arrays used to */
/*       store the following information defining the limited memory */
/*          BFGS matrix: */
/*          ws, of dimension n x m, stores S, the matrix of s-vectors; */
/*          wy, of dimension n x m, stores Y, the matrix of y-vectors; */
/*          sy, of dimension m x m, stores S'Y; */
/*          ss, of dimension m x m, stores S'S; */
/*         yy, of dimension m x m, stores Y'Y; */
/*          wt, of dimension m x m, stores the Cholesky factorization */
/*                                  of (theta*S'S+LD^(-1)L'); see eq. */
/*                                  (2.26) in [3]. */

/*     wn is a double precision working array of dimension 2m x 2m */
/*       used to store the LEL^T factorization of the indefinite matrix */
/*                 K = [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                     [L_a -R_z           theta*S'AA'S ] */

/*       where     E = [-I  0] */
/*                     [ 0  I] */

/*     snd is a double precision working array of dimension 2m x 2m */
/*       used to store the lower triangular part of */
/*                 N = [Y' ZZ'Y   L_a'+R_z'] */
/*                     [L_a +R_z  S'AA'S   ] */

/*     z(n),r(n),d(n),t(n),wa(8*m) are double precision working arrays. */
/*       z is used at different times to store the Cauchy point and */
/*       the Newton point. */

/*     sg(m),sgo(m),yg(m),ygo(m) are double precision working arrays. */

/*     index is an integer working array of dimension n. */
/*       In subroutine freev, index is used to store the free and fixed */
/*          variables at the Generalized Cauchy Point (GCP). */

/*     iwhere is an integer working array of dimension n used to record */
/*       the status of the vector x for GCP computation. */
/*       iwhere(i)=0 or -3 if x(i) is free and has bounds, */
/*                 1       if x(i) is fixed at l(i), and l(i) .ne. u(i) */
/*                 2       if x(i) is fixed at u(i), and u(i) .ne. l(i) */
/*                 3       if x(i) is always fixed, i.e.,  u(i)=x(i)=l(i) */
/*                -1       if x(i) is always free, i.e., no bounds on it. */

/*     indx2 is an integer working array of dimension n. */
/*       Within subroutine cauchy, indx2 corresponds to the array iorder. */
/*       In subroutine freev, a list of variables entering and leaving */
/*       the free set is stored in indx2, and it is passed on to */
/*       subroutine formk with this information. */

/*     task is a working string of characters of length 60 indicating */
/*       the current job when entering and leaving this subroutine. */

/*     iprint is an INTEGER variable that must be set by the user. */
/*       It controls the frequency and type of output generated: */
/*        iprint<0    no output is generated; */
/*        iprint=0    print only one line at the last iteration; */
/*        0<iprint<99 print also f and |proj g| every iprint iterations; */
/*        iprint=99   print details of every iteration except n-vectors; */
/*        iprint=100  print also the changes of active set and final x; */
/*        iprint>100  print details of every iteration including x and g; */
/*       When iprint > 0, the file iterate.dat will be created to */
/*                        summarize the iteration. */

/*     csave is a working string of characters of length 60. */

/*     lsave is a logical working array of dimension 4. */

/*     isave is an integer working array of dimension 23. */

/*     dsave is a double precision working array of dimension 29. */


/*     Subprograms called */

/*       L-BFGS-B Library ... cauchy, subsm, lnsrlb, formk, */

/*        errclb, prn1lb, prn2lb, prn3lb, active, projgr, */

/*        freev, cmprlb, matupd, formt. */

/*       Minpack2 Library ... timer, dpmeps. */

/*       Linpack Library ... dcopy, ddot. */


/*     References: */

/*       [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*       memory algorithm for bound constrained optimization'', */
/*       SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */

/*       [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN */
/*       Subroutines for Large Scale Bound Constrained Optimization'' */
/*       Tech. Report, NAM-11, EECS Department, Northwestern University, */
/*       1994. */

/*       [3] R. Byrd, J. Nocedal and R. Schnabel "Representations of */
/*       Quasi-Newton Matrices and their use in Limited Memory Methods'', */
/*       Mathematical Programming 63 (1994), no. 4, pp. 129-156. */

/*       (Postscript files of these papers are available via anonymous */
/*        ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.) */

/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       logical          prjctd,cnstnd,boxed,updatd,wrk >*/
/*<       character*3      word >*/
/*<    >*/
/*<    >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*<       if (task .eq. 'START') then >*/
    /* Parameter adjustments */
    --indx2;
    --iwhere;
    --index;
    --t;
    --d__;
    --r__;
    --z__;
    --g;
    --nbd;
    --u;
    --l;
    --x;
    --ygo;
    --yg;
    --sgo;
    --sg;
    --wa;
    snd_dim1 = 2 * *m;
    snd_offset = 1 + snd_dim1;
    snd -= snd_offset;
    wn_dim1 = 2 * *m;
    wn_offset = 1 + wn_dim1;
    wn -= wn_offset;
    wt_dim1 = *m;
    wt_offset = 1 + wt_dim1;
    wt -= wt_offset;
    yy_dim1 = *m;
    yy_offset = 1 + yy_dim1;
    yy -= yy_offset;
    ss_dim1 = *m;
    ss_offset = 1 + ss_dim1;
    ss -= ss_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;
    --lsave;
    --isave;
    --dsave;

    /* Function Body */
    if (s_cmp(task, "START", (ftnlen)60, (ftnlen)5) == 0) {
/*<          call timer(time1) >*/
        timer_(&time1);
/*        Generate the current machine precision. */
/*<          epsmch = dpmeps() >*/
        epsmch = dpmeps_();
/*        Initialize counters and scalars when task='START'. */
/*           for the limited memory BFGS matrices: */
/*<          col    = 0 >*/
        col = 0;
/*<          head   = 1 >*/
        head = 1;
/*<          theta  = one >*/
        theta = 1.;
/*<          iupdat = 0 >*/
        iupdat = 0;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*           for operation counts: */
/*<          iter   = 0 >*/
        iter = 0;
/*<          nfgv   = 0 >*/
        nfgv = 0;
/*<          nint   = 0 >*/
        nint = 0;
/*<          nintol = 0 >*/
        nintol = 0;
/*<          nskip  = 0 >*/
        nskip = 0;
/*<          nfree  = n >*/
        nfree = *n;
/*           for stopping tolerance: */
/*<          tol = factr*epsmch >*/
        tol = *factr * epsmch;
/*           for measuring running time: */
/*<          cachyt = 0 >*/
        cachyt = 0.;
/*<          sbtime = 0 >*/
        sbtime = 0.;
/*<          lnscht = 0 >*/
        lnscht = 0.;
/*           'word' records the status of subspace solutions. */
/*<          word = '---' >*/
        s_copy(word, "---", (ftnlen)3, (ftnlen)3);
/*           'info' records the termination information. */
/*<          info = 0 >*/
        info = 0;
/*<          if (iprint .ge. 1) then >*/
        if (*iprint >= 1) {
/*                                open a summary file 'iterate.dat' */
/*<             open (8, file = 'iterate.dat', status = 'unknown') >*/
/*
            o__1.oerr = 0;
            o__1.ounit = 8;
            o__1.ofnmlen = 11;
            o__1.ofnm = "iterate.dat";
            o__1.orl = 0;
            o__1.osta = "unknown";
            o__1.oacc = 0;
            o__1.ofm = 0;
            o__1.oblnk = 0;
            f_open(&o__1);
*/
/*<             itfile = 8 >*/
            itfile = 8;
/*<          endif             >*/
        }
/*        Check the input arguments for errors. */
/*<      call errclb(n,m,factr,l,u,nbd,task,info,k) >*/
        errclb_(n, m, factr, &l[1], &u[1], &nbd[1], task, &info, &k, (ftnlen)
                60);
/*<          if (task(1:5) .eq. 'ERROR') then >*/
        if (s_cmp(task, "ERROR", (ftnlen)5, (ftnlen)5) == 0) {
/*<    >*/
            prn3lb_(n, &x[1], f, task, iprint, &info, &itfile, &iter, &nfgv, &
                    nintol, &nskip, &nact, &sbgnrm, &c_b9, &nint, word, &
                    iback, &stp, &xstep, &k, &cachyt, &sbtime, &lnscht, (
                    ftnlen)60, (ftnlen)3);
/*<             return >*/
            return 0;
/*<          endif >*/
        }
/*<          call prn1lb(n,m,l,u,x,iprint,itfile,epsmch) >*/
        prn1lb_(n, m, &l[1], &u[1], &x[1], iprint, &itfile, &epsmch);
/*        Initialize iwhere & project x onto the feasible set. */
/*<          call active(n,l,u,nbd,x,iwhere,iprint,prjctd,cnstnd,boxed)  >*/
        active_(n, &l[1], &u[1], &nbd[1], &x[1], &iwhere[1], iprint, &prjctd, 
                &cnstnd, &boxed);
/*        The end of the initialization. */
/*<       else >*/
    } else {
/*          restore local variables. */
/*<          prjctd = lsave(1) >*/
        prjctd = lsave[1];
/*<          cnstnd = lsave(2) >*/
        cnstnd = lsave[2];
/*<          boxed  = lsave(3) >*/
        boxed = lsave[3];
/*<          updatd = lsave(4) >*/
        updatd = lsave[4];
/*<          nintol = isave(1) >*/
        nintol = isave[1];
/*<          itfile = isave(3) >*/
        itfile = isave[3];
/*<          iback  = isave(4) >*/
        iback = isave[4];
/*<          nskip  = isave(5) >*/
        nskip = isave[5];
/*<          head   = isave(6) >*/
        head = isave[6];
/*<          col    = isave(7) >*/
        col = isave[7];
/*<          itail  = isave(8) >*/
        itail = isave[8];
/*<          iter   = isave(9) >*/
        iter = isave[9];
/*<          iupdat = isave(10) >*/
        iupdat = isave[10];
/*<          nint   = isave(12) >*/
        nint = isave[12];
/*<          nfgv   = isave(13) >*/
        nfgv = isave[13];
/*<          info   = isave(14) >*/
        info = isave[14];
/*<          ifun   = isave(15) >*/
        ifun = isave[15];
/*<          iword  = isave(16) >*/
        iword = isave[16];
/*<          nfree  = isave(17) >*/
        nfree = isave[17];
/*<          nact   = isave(18) >*/
        nact = isave[18];
/*<          ileave = isave(19) >*/
        ileave = isave[19];
/*<          nenter = isave(20) >*/
        nenter = isave[20];
/*<          theta  = dsave(1) >*/
        theta = dsave[1];
/*<          fold   = dsave(2) >*/
        fold = dsave[2];
/*<          tol    = dsave(3) >*/
        tol = dsave[3];
/*<          dnorm  = dsave(4) >*/
        dnorm = dsave[4];
/*<          epsmch = dsave(5) >*/
        epsmch = dsave[5];
/*<          cpu1   = dsave(6) >*/
        cpu1 = dsave[6];
/*<          cachyt = dsave(7) >*/
        cachyt = dsave[7];
/*<          sbtime = dsave(8) >*/
        sbtime = dsave[8];
/*<          lnscht = dsave(9) >*/
        lnscht = dsave[9];
/*<          time1  = dsave(10) >*/
        time1 = dsave[10];
/*<          gd     = dsave(11) >*/
        gd = dsave[11];
/*<          stpmx  = dsave(12) >*/
        stpmx = dsave[12];
/*<          sbgnrm = dsave(13) >*/
        sbgnrm = dsave[13];
/*<          stp    = dsave(14) >*/
        stp = dsave[14];
/*<          gdold  = dsave(15) >*/
        gdold = dsave[15];
/*<          dtd    = dsave(16) >*/
        dtd = dsave[16];
/*        After returning from the driver go to the point where execution */
/*        is to resume. */
/*<          if (task(1:5) .eq. 'FG_LN') goto 666 >*/
        if (s_cmp(task, "FG_LN", (ftnlen)5, (ftnlen)5) == 0) {
            goto L666;
        }
/*<          if (task(1:5) .eq. 'NEW_X') goto 777 >*/
        if (s_cmp(task, "NEW_X", (ftnlen)5, (ftnlen)5) == 0) {
            goto L777;
        }
/*<          if (task(1:5) .eq. 'FG_ST') goto 111 >*/
        if (s_cmp(task, "FG_ST", (ftnlen)5, (ftnlen)5) == 0) {
            goto L111;
        }
/*<          if (task(1:4) .eq. 'STOP') then >*/
        if (s_cmp(task, "STOP", (ftnlen)4, (ftnlen)4) == 0) {
/*<             if (task(7:9) .eq. 'CPU') then >*/
            if (s_cmp(task + 6, "CPU", (ftnlen)3, (ftnlen)3) == 0) {
/*                                          restore the previous iterate. */
/*<                call dcopy(n,t,1,x,1) >*/
                dcopy_(n, &t[1], &c__1, &x[1], &c__1);
/*<                call dcopy(n,r,1,g,1) >*/
                dcopy_(n, &r__[1], &c__1, &g[1], &c__1);
/*<                f = fold >*/
                *f = fold;
/*<             endif >*/
            }
/*<             goto 999 >*/
            goto L999;
/*<          endif >*/
        }
/*<       endif  >*/
    }
/*     Compute f0 and g0. */
/*<       task = 'FG_START'  >*/
    s_copy(task, "FG_START", (ftnlen)60, (ftnlen)8);
/*          return to the driver to calculate f and g; reenter at 111. */
/*<       goto 1000 >*/
    goto L1000;
/*<  111  continue >*/
L111:
/*<       nfgv = 1 >*/
    nfgv = 1;
/*     Compute the infinity norm of the (-) projected gradient. */
/*<       call projgr(n,l,u,nbd,x,g,sbgnrm) >*/
    projgr_(n, &l[1], &u[1], &nbd[1], &x[1], &g[1], &sbgnrm);
/*<       if (iprint .ge. 1) then >*/
    if (*iprint >= 1) {
/*<          write (6,1002) iter,f,sbgnrm >*/
/*
 1002 format
     +  (/,'At iterate',i5,4x,'f= ',1p,d12.5,4x,'|proj g|= ',1p,d12.5)
*/
        printf("At iterate %5ld    f= %12.5g    |proj g|= %12.5g\n",
               iter, *f, sbgnrm);
/*<          write (itfile,1003) iter,nfgv,sbgnrm,f >*/
/*
 1003 format (2(1x,i4),5x,'-',5x,'-',3x,'-',5x,'-',5x,'-',8x,'-',3x,
     +        1p,2(1x,d10.3))
*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
        fprintf(0,
                " %4ld %4ld     -     -   -     -     -        -    %10.3g %10.3g\n",
                iter, nfgv, sbgnrm, *f);
#endif
/*<       endif >*/
    }
/*<       if (sbgnrm .le. pgtol) then >*/
    if (sbgnrm <= *pgtol) {
/*                                terminate the algorithm. */
/*<          task = 'CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL' >*/
        s_copy(task, "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL", (
                ftnlen)60, (ftnlen)48);
/*<          goto 999 >*/
        goto L999;
/*<       endif  >*/
    }
/* ----------------- the beginning of the loop -------------------------- */
/*<  222  continue >*/
L222:
/*<       if (iprint .ge. 99) write (6,1001) iter + 1 >*/
/*
 1001 format (//,'ITERATION ',i5)
*/
    if (*iprint >= 99) {
        i__1 = iter + 1;
        printf("ITERATION %5ld\n", i__1);
    }
/*<       iword = -1 >*/
    iword = -1;

/*<       if (.not. cnstnd .and. col .gt. 0) then  >*/
    if (! cnstnd && col > 0) {
/*                                            skip the search for GCP. */
/*<          call dcopy(n,x,1,z,1) >*/
        dcopy_(n, &x[1], &c__1, &z__[1], &c__1);
/*<      wrk = updatd >*/
        wrk = updatd;
/*<          nint = 0 >*/
        nint = 0;
/*<          goto 333 >*/
        goto L333;
/*<       endif >*/
    }
/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */

/*     Compute the Generalized Cauchy Point (GCP). */

/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */
/*<       call timer(cpu1)  >*/
    timer_(&cpu1);
/*<    >*/
    cauchy_(n, &x[1], &l[1], &u[1], &nbd[1], &g[1], &indx2[1], &iwhere[1], &t[
            1], &d__[1], &z__[1], m, &wy[wy_offset], &ws[ws_offset], &sy[
            sy_offset], &wt[wt_offset], &theta, &col, &head, &wa[1], &wa[(*m 
            << 1) + 1], &wa[(*m << 2) + 1], &wa[*m * 6 + 1], &nint, &sg[1], &
            yg[1], iprint, &sbgnrm, &info, &epsmch);
/*<       if (info .ne. 0) then  >*/
    if (info != 0) {
/*         singular triangular system detected; refresh the lbfgs memory. */
/*<          if(iprint .ge. 1) write (6, 1005) >*/
/*
 1005 format (/, 
     +' Singular triangular system detected;',/,
     +'   refresh the lbfgs memory and restart the iteration.')
*/
        if (*iprint >= 1) {
            printf(" Singular triangular system detected;\n"
                   "   refresh the lbfgs memory and restart the iteration.\n");
        }
/*<          info   = 0 >*/
        info = 0;
/*<          col    = 0 >*/
        col = 0;
/*<          head   = 1 >*/
        head = 1;
/*<          theta  = one >*/
        theta = 1.;
/*<          iupdat = 0 >*/
        iupdat = 0;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*<          call timer(cpu2)  >*/
        timer_(&cpu2);
/*<          cachyt = cachyt + cpu2 - cpu1 >*/
        cachyt = cachyt + cpu2 - cpu1;
/*<          goto 222 >*/
        goto L222;
/*<       endif >*/
    }
/*<       call timer(cpu2)  >*/
    timer_(&cpu2);
/*<       cachyt = cachyt + cpu2 - cpu1 >*/
    cachyt = cachyt + cpu2 - cpu1;
/*<       nintol = nintol + nint >*/
    nintol += nint;
/*     Count the entering and leaving variables for iter > 0; */
/*     find the index set of free and active variables at the GCP. */
/*<    >*/
    freev_(n, &nfree, &index[1], &nenter, &ileave, &indx2[1], &iwhere[1], &
            wrk, &updatd, &cnstnd, iprint, &iter);
/*<       nact = n - nfree >*/
    nact = *n - nfree;
/*<  333  continue >*/
L333:
/*     If there are no free variables or B=theta*I, then */
/*                                        skip the subspace minimization. */
/*<       if (nfree .eq. 0 .or. col .eq. 0) goto 555 >*/
    if (nfree == 0 || col == 0) {
        goto L555;
    }
/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */

/*     Subspace minimization. */

/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */
/*<       call timer(cpu1)  >*/
    timer_(&cpu1);
/*     Form  the LEL^T factorization of the indefinite */
/*       matrix    K = [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                     [L_a -R_z           theta*S'AA'S ] */
/*       where     E = [-I  0] */
/*                     [ 0  I] */
/*<    >*/
    if (wrk) {
        formk_(n, &nfree, &index[1], &nenter, &ileave, &indx2[1], &iupdat, &
                updatd, &wn[wn_offset], &snd[snd_offset], m, &ws[ws_offset], &
                wy[wy_offset], &sy[sy_offset], &theta, &col, &head, &info);
    }
/*<       if (info .ne. 0) then >*/
    if (info != 0) {
/*          nonpositive definiteness in Cholesky factorization; */
/*          refresh the lbfgs memory and restart the iteration. */
/*<          if(iprint .ge. 1) write (6, 1006) >*/
/*
 1006 format (/, 
     +' Nonpositive definiteness in Cholesky factorization in formk;',/,
     +'   refresh the lbfgs memory and restart the iteration.')
 */
        if (*iprint >= 1) {
            printf(" Nonpositive definiteness in Cholesky factorization in formk;\n"
                   "   refresh the lbfgs memory and restart the iteration.\n");
        }
/*<          info   = 0 >*/
        info = 0;
/*<          col    = 0 >*/
        col = 0;
/*<          head   = 1 >*/
        head = 1;
/*<          theta  = one >*/
        theta = 1.;
/*<          iupdat = 0 >*/
        iupdat = 0;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*<          call timer(cpu2)  >*/
        timer_(&cpu2);
/*<          sbtime = sbtime + cpu2 - cpu1  >*/
        sbtime = sbtime + cpu2 - cpu1;
/*<          goto 222 >*/
        goto L222;
/*<       endif  >*/
    }
/*        compute r=-Z'B(xcp-xk)-Z'g (using wa(2m+1)=W'(xcp-x) */
/*                                                   from 'cauchy'). */
/*<    >*/
    cmprlb_(n, m, &x[1], &g[1], &ws[ws_offset], &wy[wy_offset], &sy[sy_offset]
            , &wt[wt_offset], &z__[1], &r__[1], &wa[1], &index[1], &theta, &
            col, &head, &nfree, &cnstnd, &info);
/*<       if (info .ne. 0) goto 444 >*/
    if (info != 0) {
        goto L444;
    }
/*       call the direct method. */
/*<    >*/
    subsm_(n, m, &nfree, &index[1], &l[1], &u[1], &nbd[1], &z__[1], &r__[1], &
            ws[ws_offset], &wy[wy_offset], &theta, &col, &head, &iword, &wa[1]
            , &wn[wn_offset], iprint, &info);
/*<  444  continue >*/
L444:
/*<       if (info .ne. 0) then  >*/
    if (info != 0) {
/*          singular triangular system detected; */
/*          refresh the lbfgs memory and restart the iteration. */
/*<          if(iprint .ge. 1) write (6, 1005) >*/
        if (*iprint >= 1) {
            printf(" Singular triangular system detected;\n"
                   "   refresh the lbfgs memory and restart the iteration.\n");
        }
/*<          info   = 0 >*/
        info = 0;
/*<          col    = 0 >*/
        col = 0;
/*<          head   = 1 >*/
        head = 1;
/*<          theta  = one >*/
        theta = 1.;
/*<          iupdat = 0 >*/
        iupdat = 0;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*<          call timer(cpu2)  >*/
        timer_(&cpu2);
/*<          sbtime = sbtime + cpu2 - cpu1  >*/
        sbtime = sbtime + cpu2 - cpu1;
/*<          goto 222 >*/
        goto L222;
/*<       endif >*/
    }
/*<       call timer(cpu2)  >*/
    timer_(&cpu2);
/*<       sbtime = sbtime + cpu2 - cpu1  >*/
    sbtime = sbtime + cpu2 - cpu1;
/*<  555  continue >*/
L555:
/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */

/*     Line search and optimality tests. */

/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */
/*     Generate the search direction d:=z-x. */
/*<       do 40 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      d(i) = z(i) - x(i) >*/
        d__[i__] = z__[i__] - x[i__];
/*<   40  continue >*/
/* L40: */
    }
/*<       call timer(cpu1)  >*/
    timer_(&cpu1);
/*<  666  continue >*/
L666:
/*<    >*/
    lnsrlb_(n, &l[1], &u[1], &nbd[1], &x[1], f, &fold, &gd, &gdold, &g[1], &
            d__[1], &r__[1], &t[1], &z__[1], &stp, &dnorm, &dtd, &xstep, &
            stpmx, &iter, &ifun, &iback, &nfgv, &info, task, &boxed, &cnstnd, 
            csave, &isave[22], &dsave[17], (ftnlen)60, (ftnlen)60);
/*<       if (info .ne. 0 .or. iback .ge. 20) then >*/
    if (info != 0 || iback >= 20) {
/*          restore the previous iterate. */
/*<          call dcopy(n,t,1,x,1) >*/
        dcopy_(n, &t[1], &c__1, &x[1], &c__1);
/*<          call dcopy(n,r,1,g,1) >*/
        dcopy_(n, &r__[1], &c__1, &g[1], &c__1);
/*<          f = fold >*/
        *f = fold;
/*<          if (col .eq. 0) then >*/
        if (col == 0) {
/*             abnormal termination. */
/*<             if (info .eq. 0) then >*/
            if (info == 0) {
/*<                info = -9 >*/
                info = -9;
/*                restore the actual number of f and g evaluations etc. */
/*<                nfgv = nfgv - 1 >*/
                --nfgv;
/*<                ifun = ifun - 1 >*/
                --ifun;
/*<                iback = iback - 1 >*/
                --iback;
/*<             endif >*/
            }
/*<             task = 'ABNORMAL_TERMINATION_IN_LNSRCH' >*/
            s_copy(task, "ABNORMAL_TERMINATION_IN_LNSRCH", (ftnlen)60, (
                    ftnlen)30);
/*<             iter = iter + 1 >*/
            ++iter;
/*<             goto 999 >*/
            goto L999;
/*<          else >*/
        } else {
/*             refresh the lbfgs memory and restart the iteration. */
/*<             if(iprint .ge. 1) write (6, 1008) >*/
/*
 1008 format (/,
     +' Bad direction in the line search;',/,
     +'   refresh the lbfgs memory and restart the iteration.')
*/
            if (*iprint >= 1) {
                printf(" Bad direction in the line search;\n"
                       "   refresh the lbfgs memory and restart the iteration.\n");
            }
/*<             if (info .eq. 0) nfgv = nfgv - 1 >*/
            if (info == 0) {
                --nfgv;
            }
/*<             info   = 0 >*/
            info = 0;
/*<             col    = 0 >*/
            col = 0;
/*<             head   = 1 >*/
            head = 1;
/*<             theta  = one >*/
            theta = 1.;
/*<             iupdat = 0 >*/
            iupdat = 0;
/*<             updatd = .false. >*/
            updatd = FALSE_;
/*<             task   = 'RESTART_FROM_LNSRCH' >*/
            s_copy(task, "RESTART_FROM_LNSRCH", (ftnlen)60, (ftnlen)19);
/*<             call timer(cpu2) >*/
            timer_(&cpu2);
/*<             lnscht = lnscht + cpu2 - cpu1 >*/
            lnscht = lnscht + cpu2 - cpu1;
/*<             goto 222 >*/
            goto L222;
/*<          endif >*/
        }
/*<       else if (task(1:5) .eq. 'FG_LN') then >*/
    } else if (s_cmp(task, "FG_LN", (ftnlen)5, (ftnlen)5) == 0) {
/*          return to the driver for calculating f and g; reenter at 666. */
/*<      goto 1000 >*/
        goto L1000;
/*<       else  >*/
    } else {
/*          calculate and print out the quantities related to the new X. */
/*<          call timer(cpu2)  >*/
        timer_(&cpu2);
/*<          lnscht = lnscht + cpu2 - cpu1 >*/
        lnscht = lnscht + cpu2 - cpu1;
/*<          iter = iter + 1 >*/
        ++iter;
/*        Compute the infinity norm of the projected (-)gradient. */
/*<          call projgr(n,l,u,nbd,x,g,sbgnrm) >*/
        projgr_(n, &l[1], &u[1], &nbd[1], &x[1], &g[1], &sbgnrm);
/*        Print iteration information. */
/*<    >*/
        prn2lb_(n, &x[1], f, &g[1], iprint, &itfile, &iter, &nfgv, &nact, &
                sbgnrm, &nint, word, &iword, &iback, &stp, &xstep, (ftnlen)3);
/*<          goto 1000 >*/
        goto L1000;
/*<       endif >*/
    }
/*<  777  continue >*/
L777:
/*     Test for termination. */
/*<       if (sbgnrm .le. pgtol) then >*/
    if (sbgnrm <= *pgtol) {
/*                                terminate the algorithm. */
/*<          task = 'CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL' >*/
        s_copy(task, "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL", (
                ftnlen)60, (ftnlen)48);
/*<          goto 999 >*/
        goto L999;
/*<       endif  >*/
    }
/*<       ddum = max(abs(fold), abs(f), one) >*/
/* Computing MAX */
    d__1 = abs(fold), d__2 = abs(*f), d__1 = max(d__1,d__2);
    ddum = max(d__1,1.);
/*<       if ((fold - f) .le. tol*ddum) then >*/
    if (fold - *f <= tol * ddum) {
/*                                        terminate the algorithm. */
/*<          task = 'CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH' >*/
        s_copy(task, "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH", (
                ftnlen)60, (ftnlen)47);
/*<          if (iback .ge. 10) info = -5 >*/
        if (iback >= 10) {
            info = -5;
        }
/*           i.e., to issue a warning if iback>10 in the line search. */
/*<          goto 999 >*/
        goto L999;
/*<       endif  >*/
    }
/*     Compute d=newx-oldx, r=newg-oldg, rr=y'y and dr=y's. */
/*<       do 42 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          r(i) = g(i) - r(i) >*/
        r__[i__] = g[i__] - r__[i__];
/*<   42  continue >*/
/* L42: */
    }
/*<       rr = ddot(n,r,1,r,1) >*/
    rr = ddot_(n, &r__[1], &c__1, &r__[1], &c__1);
/*<       if (stp .eq. one) then   >*/
    if (stp == 1.) {
/*<          dr = gd - gdold >*/
        dr = gd - gdold;
/*<          ddum = -gdold >*/
        ddum = -gdold;
/*<       else >*/
    } else {
/*<          dr = (gd - gdold)*stp >*/
        dr = (gd - gdold) * stp;
/*<      call dscal(n,stp,d,1) >*/
        dscal_(n, &stp, &d__[1], &c__1);
/*<          ddum = -gdold*stp >*/
        ddum = -gdold * stp;
/*<       endif >*/
    }
/*<       if (dr .le. epsmch*ddum) then >*/
    if (dr <= epsmch * ddum) {
/*                            skip the L-BFGS update. */
/*<          nskip = nskip + 1 >*/
        ++nskip;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*<          if (iprint .ge. 1) write (6,1004) dr, ddum >*/
/*
 1004 format ('  ys=',1p,e10.3,'  -gs=',1p,e10.3,' BFGS update SKIPPED')
 */
        if (*iprint >= 1) {
            printf("  ys=%10.3g  -gs=%10.3g BFGS update SKIPPED\n",
                   dr, ddum);
        }
/*<          goto 888 >*/
        goto L888;
/*<       endif  >*/
    }
/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */

/*     Update the L-BFGS matrix. */

/* ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc */
/*<       updatd = .true. >*/
    updatd = TRUE_;
/*<       iupdat = iupdat + 1 >*/
    ++iupdat;
/*     Update matrices WS and WY and form the middle matrix in B. */
/*<    >*/
    matupd_(n, m, &ws[ws_offset], &wy[wy_offset], &sy[sy_offset], &ss[
            ss_offset], &d__[1], &r__[1], &itail, &iupdat, &col, &head, &
            theta, &rr, &dr, &stp, &dtd);
/*     Form the upper half of the pds T = theta*SS + L*D^(-1)*L'; */
/*        Store T in the upper triangular of the array wt; */
/*        Cholesky factorize T to J*J' with */
/*           J' stored in the upper triangular of wt. */
/*<       call formt(m,wt,sy,ss,col,theta,info) >*/
    formt_(m, &wt[wt_offset], &sy[sy_offset], &ss[ss_offset], &col, &theta, &
            info);
/*<       if (info .ne. 0) then  >*/
    if (info != 0) {
/*          nonpositive definiteness in Cholesky factorization; */
/*          refresh the lbfgs memory and restart the iteration. */
/*<          if(iprint .ge. 1) write (6, 1007) >*/
/*
 1007 format (/,
     +' Nonpositive definiteness in Cholesky factorization in formt;',/,
     +'   refresh the lbfgs memory and restart the iteration.')
*/
        if (*iprint >= 1) {
            printf(" Nonpositive definiteness in Cholesky factorization in formt;\n"
                   "   refresh the lbfgs memory and restart the iteration.\n");
        }
/*<          info = 0 >*/
        info = 0;
/*<          col = 0 >*/
        col = 0;
/*<          head = 1 >*/
        head = 1;
/*<          theta = one >*/
        theta = 1.;
/*<          iupdat = 0 >*/
        iupdat = 0;
/*<          updatd = .false. >*/
        updatd = FALSE_;
/*<          goto 222 >*/
        goto L222;
/*<       endif >*/
    }
/*     Now the inverse of the middle matrix in B is */
/*       [  D^(1/2)      O ] [ -D^(1/2)  D^(-1/2)*L' ] */
/*       [ -L*D^(-1/2)   J ] [  0        J'          ] */
/*<  888  continue >*/
L888:
/* -------------------- the end of the loop ----------------------------- */
/*<       goto 222 >*/
    goto L222;
/*<  999  continue >*/
L999:
/*<       call timer(time2) >*/
    timer_(&time2);
/*<       time = time2 - time1 >*/
    time = time2 - time1;
/*<    >*/
    prn3lb_(n, &x[1], f, task, iprint, &info, &itfile, &iter, &nfgv, &nintol, 
            &nskip, &nact, &sbgnrm, &time, &nint, word, &iback, &stp, &xstep, 
            &k, &cachyt, &sbtime, &lnscht, (ftnlen)60, (ftnlen)3);
/*<  1000 continue >*/
L1000:
/*     Save local variables. */
/*<       lsave(1)  = prjctd >*/
    lsave[1] = prjctd;
/*<       lsave(2)  = cnstnd >*/
    lsave[2] = cnstnd;
/*<       lsave(3)  = boxed >*/
    lsave[3] = boxed;
/*<       lsave(4)  = updatd >*/
    lsave[4] = updatd;
/*<       isave(1)  = nintol  >*/
    isave[1] = nintol;
/*<       isave(3)  = itfile  >*/
    isave[3] = itfile;
/*<       isave(4)  = iback  >*/
    isave[4] = iback;
/*<       isave(5)  = nskip  >*/
    isave[5] = nskip;
/*<       isave(6)  = head  >*/
    isave[6] = head;
/*<       isave(7)  = col  >*/
    isave[7] = col;
/*<       isave(8)  = itail  >*/
    isave[8] = itail;
/*<       isave(9)  = iter  >*/
    isave[9] = iter;
/*<       isave(10) = iupdat  >*/
    isave[10] = iupdat;
/*<       isave(12) = nint  >*/
    isave[12] = nint;
/*<       isave(13) = nfgv  >*/
    isave[13] = nfgv;
/*<       isave(14) = info  >*/
    isave[14] = info;
/*<       isave(15) = ifun  >*/
    isave[15] = ifun;
/*<       isave(16) = iword  >*/
    isave[16] = iword;
/*<       isave(17) = nfree  >*/
    isave[17] = nfree;
/*<       isave(18) = nact  >*/
    isave[18] = nact;
/*<       isave(19) = ileave  >*/
    isave[19] = ileave;
/*<       isave(20) = nenter  >*/
    isave[20] = nenter;
/*<       dsave(1)  = theta  >*/
    dsave[1] = theta;
/*<       dsave(2)  = fold  >*/
    dsave[2] = fold;
/*<       dsave(3)  = tol  >*/
    dsave[3] = tol;
/*<       dsave(4)  = dnorm  >*/
    dsave[4] = dnorm;
/*<       dsave(5)  = epsmch  >*/
    dsave[5] = epsmch;
/*<       dsave(6)  = cpu1  >*/
    dsave[6] = cpu1;
/*<       dsave(7)  = cachyt  >*/
    dsave[7] = cachyt;
/*<       dsave(8)  = sbtime  >*/
    dsave[8] = sbtime;
/*<       dsave(9)  = lnscht  >*/
    dsave[9] = lnscht;
/*<       dsave(10) = time1  >*/
    dsave[10] = time1;
/*<       dsave(11) = gd  >*/
    dsave[11] = gd;
/*<       dsave(12) = stpmx  >*/
    dsave[12] = stpmx;
/*<       dsave(13) = sbgnrm >*/
    dsave[13] = sbgnrm;
/*<       dsave(14) = stp >*/
    dsave[14] = stp;
/*<       dsave(15) = gdold >*/
    dsave[15] = gdold;
/*<       dsave(16) = dtd   >*/
    dsave[16] = dtd;
/*<  1001 format (//,'ITERATION ',i5) >*/
/*<  1 >*/
/*<  1 >*/
/*<  1004 format ('  ys=',1p,e10.3,'  -gs=',1p,e10.3,' BFGS update SKIPPED') >*/
/*<  1 >*/
/*<  1 >*/
/*<  1 >*/
/*<  1 >*/
/*<       return    >*/
    return 0;
/*<       end >*/
} /* mainlb_ */

/* ======================= The end of mainlb ============================= */
/*<    >*/
/* Subroutine */ int active_(integer *n, doublereal *l, doublereal *u, 
        integer *nbd, doublereal *x, integer *iwhere, integer *iprint, 
        logical *prjctd, logical *cnstnd, logical *boxed)
{
    /* System generated locals */
    integer i__1;

    /* Local variables */
    integer i__, nbdd;

/*<       logical          prjctd, cnstnd, boxed >*/
/*<       integer          n, iprint, nbd(n), iwhere(n) >*/
/*<       double precision x(n), l(n), u(n) >*/
/*     ************ */

/*     Subroutine active */

/*     This subroutine initializes iwhere and projects the initial x to */
/*       the feasible set if necessary. */

/*     iwhere is an integer array of dimension n. */
/*       On entry iwhere is unspecified. */
/*       On exit iwhere(i)=-1  if x(i) has no bounds */
/*                         3   if l(i)=u(i) */
/*                         0   otherwise. */
/*       In cauchy, iwhere is given finer gradations. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          nbdd,i >*/
/*<       double precision zero >*/
/*<       parameter        (zero=0.0d0) >*/
/*     Initialize nbdd, prjctd, cnstnd and boxed. */
/*<       nbdd = 0 >*/
    /* Parameter adjustments */
    --iwhere;
    --x;
    --nbd;
    --u;
    --l;

    /* Function Body */
    nbdd = 0;
/*<       prjctd = .false. >*/
    *prjctd = FALSE_;
/*<       cnstnd = .false. >*/
    *cnstnd = FALSE_;
/*<       boxed = .true. >*/
    *boxed = TRUE_;
/*     Project the initial x to the easible set if necessary. */
/*<       do 10 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          if (nbd(i) .gt. 0) then >*/
        if (nbd[i__] > 0) {
/*<             if (nbd(i) .le. 2 .and. x(i) .le. l(i)) then >*/
            if (nbd[i__] <= 2 && x[i__] <= l[i__]) {
/*<            if (x(i) .lt. l(i)) then >*/
                if (x[i__] < l[i__]) {
/*<                   prjctd = .true. >*/
                    *prjctd = TRUE_;
/*<               x(i) = l(i) >*/
                    x[i__] = l[i__];
/*<                endif >*/
                }
/*<                nbdd = nbdd + 1 >*/
                ++nbdd;
/*<             else if (nbd(i) .ge. 2 .and. x(i) .ge. u(i)) then >*/
            } else if (nbd[i__] >= 2 && x[i__] >= u[i__]) {
/*<            if (x(i) .gt. u(i)) then >*/
                if (x[i__] > u[i__]) {
/*<                   prjctd = .true. >*/
                    *prjctd = TRUE_;
/*<               x(i) = u(i) >*/
                    x[i__] = u[i__];
/*<                endif >*/
                }
/*<                nbdd = nbdd + 1 >*/
                ++nbdd;
/*<             endif >*/
            }
/*<          endif >*/
        }
/*<   10  continue >*/
/* L10: */
    }
/*     Initialize iwhere and assign values to cnstnd and boxed. */
/*<       do 20 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          if (nbd(i) .ne. 2) boxed = .false. >*/
        if (nbd[i__] != 2) {
            *boxed = FALSE_;
        }
/*<          if (nbd(i) .eq. 0) then >*/
        if (nbd[i__] == 0) {
/*                                this variable is always free */
/*<         iwhere(i) = -1 >*/
            iwhere[i__] = -1;
/*           otherwise set x(i)=mid(x(i), u(i), l(i)). */
/*<          else >*/
        } else {
/*<         cnstnd = .true. >*/
            *cnstnd = TRUE_;
/*<             if (nbd(i) .eq. 2 .and. u(i) - l(i) .le. zero) then >*/
            if (nbd[i__] == 2 && u[i__] - l[i__] <= 0.) {
/*                   this variable is always fixed */
/*<            iwhere(i) = 3 >*/
                iwhere[i__] = 3;
/*<             else  >*/
            } else {
/*<                iwhere(i) = 0 >*/
                iwhere[i__] = 0;
/*<             endif >*/
            }
/*<          endif >*/
        }
/*<   20  continue >*/
/* L20: */
    }
/*<       if (iprint .ge. 0) then >*/
    if (*iprint >= 0) {
/*<    >*/
        if (*prjctd) {
            printf("The initial X is infeasible.  Restart with its projection.\n");
        }
/*<    >*/
        if (! (*cnstnd)) {
            printf("This problem is unconstrained.\n");
        }
/*<       endif >*/
    }
/*<       if (iprint .gt. 0) write (6,1001) nbdd >*/
    if (*iprint > 0) {
        printf("At X0 %9ld variables are exactly at the bounds\n", nbdd);
    }
/*<  1001 format (/,'At X0 ',i9,' variables are exactly at the bounds')  >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* active_ */

/* ======================= The end of active ============================= */
/*<       subroutine bmv(m, sy, wt, col, v, p, info) >*/
/* Subroutine */ int bmv_(integer *m, doublereal *sy, doublereal *wt, integer 
        *col, doublereal *v, doublereal *p, integer *info)
{
    /* System generated locals */
    integer sy_dim1, sy_offset, wt_dim1, wt_offset, i__1, i__2;

    /* Builtin functions */
    double sqrt(doublereal);

    /* Local variables */
    integer i__, k, i2;
    doublereal sum;
    extern /* Subroutine */ int dtrsl_(doublereal *, integer *, integer *, 
            doublereal *, integer *, integer *);

/*<       integer m, col, info >*/
/*<       double precision sy(m, m), wt(m, m), v(2*col), p(2*col) >*/
/*     ************ */

/*     Subroutine bmv */

/*     This subroutine computes the product of the 2m x 2m middle matrix */
/*      in the compact L-BFGS formula of B and a 2m vector v; */
/*      it returns the product in p. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric corrections */
/*         used to define the limited memory matrix. */
/*       On exit m is unchanged. */

/*     sy is a double precision array of dimension m x m. */
/*       On entry sy specifies the matrix S'Y. */
/*       On exit sy is unchanged. */

/*     wt is a double precision array of dimension m x m. */
/*       On entry wt specifies the upper triangular matrix J' which is */
/*         the Cholesky factor of (thetaS'S+LD^(-1)L'). */
/*       On exit wt is unchanged. */

/*     col is an integer variable. */
/*       On entry col specifies the number of s-vectors (or y-vectors) */
/*         stored in the compact L-BFGS formula. */
/*       On exit col is unchanged. */

/*     v is a double precision array of dimension 2col. */
/*       On entry v specifies vector v. */
/*       On exit v is unchanged. */

/*     p is a double precision array of dimension 2col. */
/*       On entry p is unspecified. */
/*       On exit p is the product Mv. */

/*     info is an integer variable. */
/*       On entry info is unspecified. */
/*       On exit info = 0       for normal return, */
/*                    = nonzero for abnormal return when the system */
/*                                to be solved by dtrsl is singular. */

/*     Subprograms called: */

/*       Linpack ... dtrsl. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          i,k,i2 >*/
/*<       double precision sum >*/
/*<       if (col .eq. 0) return >*/
    /* Parameter adjustments */
    wt_dim1 = *m;
    wt_offset = 1 + wt_dim1;
    wt -= wt_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    --p;
    --v;

    /* Function Body */
    if (*col == 0) {
        return 0;
    }
/*     PART I: solve [  D^(1/2)      O ] [ p1 ] = [ v1 ] */
/*                   [ -L*D^(-1/2)   J ] [ p2 ]   [ v2 ]. */
/*      solve Jp2=v2+LD^(-1)v1. */
/*<       p(col + 1) = v(col + 1) >*/
    p[*col + 1] = v[*col + 1];
/*<       do 20 i = 2, col >*/
    i__1 = *col;
    for (i__ = 2; i__ <= i__1; ++i__) {
/*<          i2 = col + i >*/
        i2 = *col + i__;
/*<          sum = 0.0d0 >*/
        sum = 0.;
/*<          do 10 k = 1, i - 1 >*/
        i__2 = i__ - 1;
        for (k = 1; k <= i__2; ++k) {
/*<             sum = sum + sy(i,k)*v(k)/sy(k,k) >*/
            sum += sy[i__ + k * sy_dim1] * v[k] / sy[k + k * sy_dim1];
/*<   10     continue >*/
/* L10: */
        }
/*<          p(i2) = v(i2) + sum >*/
        p[i2] = v[i2] + sum;
/*<   20  continue   >*/
/* L20: */
    }
/*     Solve the triangular system */
/*<       call dtrsl(wt,m,col,p(col+1),11,info) >*/
    dtrsl_(&wt[wt_offset], m, col, &p[*col + 1], &c__11, info);
/*<       if (info .ne. 0) return >*/
    if (*info != 0) {
        return 0;
    }
/*      solve D^(1/2)p1=v1. */
/*<       do 30 i = 1, col >*/
    i__1 = *col;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          p(i) = v(i)/sqrt(sy(i,i)) >*/
        p[i__] = v[i__] / sqrt(sy[i__ + i__ * sy_dim1]);
/*<   30  continue  >*/
/* L30: */
    }
/*     PART II: solve [ -D^(1/2)   D^(-1/2)*L'  ] [ p1 ] = [ p1 ] */
/*                    [  0         J'           ] [ p2 ]   [ p2 ]. */
/*       solve J^Tp2=p2. */
/*<       call dtrsl(wt,m,col,p(col+1),01,info) >*/
    dtrsl_(&wt[wt_offset], m, col, &p[*col + 1], &c__1, info);
/*<       if (info .ne. 0) return >*/
    if (*info != 0) {
        return 0;
    }
/*       compute p1=-D^(-1/2)(p1-D^(-1/2)L'p2) */
/*                 =-D^(-1/2)p1+D^(-1)L'p2. */
/*<       do 40 i = 1, col >*/
    i__1 = *col;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          p(i) = -p(i)/sqrt(sy(i,i)) >*/
        p[i__] = -p[i__] / sqrt(sy[i__ + i__ * sy_dim1]);
/*<   40  continue >*/
/* L40: */
    }
/*<       do 60 i = 1, col >*/
    i__1 = *col;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          sum = 0.d0 >*/
        sum = 0.;
/*<          do 50 k = i + 1, col >*/
        i__2 = *col;
        for (k = i__ + 1; k <= i__2; ++k) {
/*<             sum = sum + sy(k,i)*p(col+k)/sy(i,i) >*/
            sum += sy[k + i__ * sy_dim1] * p[*col + k] / sy[i__ + i__ * 
                    sy_dim1];
/*<   50     continue >*/
/* L50: */
        }
/*<          p(i) = p(i) + sum >*/
        p[i__] += sum;
/*<   60  continue >*/
/* L60: */
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* bmv_ */

/* ======================== The end of bmv =============================== */
/*<    >*/
/* Subroutine */ int cauchy_(integer *n, doublereal *x, doublereal *l, 
        doublereal *u, integer *nbd, doublereal *g, integer *iorder, integer *
        iwhere, doublereal *t, doublereal *d__, doublereal *xcp, integer *m, 
        doublereal *wy, doublereal *ws, doublereal *sy, doublereal *wt, 
        doublereal *theta, integer *col, integer *head, doublereal *p, 
        doublereal *c__, doublereal *wbp, doublereal *v, integer *nint, 
        doublereal *sg, doublereal *yg, integer *iprint, doublereal *sbgnrm, 
        integer *info, doublereal *epsmch)
{
    /* System generated locals */
    integer wy_dim1, wy_offset, ws_dim1, ws_offset, sy_dim1, sy_offset, 
            wt_dim1, wt_offset, i__1, i__2;
    doublereal d__1;

    /* Local variables */
    integer i__, j;
    doublereal f1, f2, dt, tj, tl=0, tu=0, tj0;
    integer ibp;
    doublereal dtm;
    extern /* Subroutine */ int bmv_(integer *, doublereal *, doublereal *, 
            integer *, doublereal *, doublereal *, integer *);
    doublereal wmc, wmp, wmw;
    integer col2;
    doublereal dibp;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    integer iter;
    doublereal zibp, tsum, dibp2;
    logical bnded;
    extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
            integer *);
    doublereal neggi;
    integer nfree;
    doublereal bkmin;
    integer nleft;
    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
            doublereal *, integer *), daxpy_(integer *, doublereal *, 
            doublereal *, integer *, doublereal *, integer *);
    doublereal f2_org__;
    integer nbreak, ibkmin;
    extern /* Subroutine */ int hpsolb_(integer *, doublereal *, integer *, 
            integer *);
    integer pointr;
    logical xlower, xupper;

/*<    >*/
/*<    >*/
/*     ************ */

/*     Subroutine cauchy */

/*     For given x, l, u, g (with sbgnrm > 0), and a limited memory */
/*       BFGS matrix B defined in terms of matrices WY, WS, WT, and */
/*       scalars head, col, and theta, this subroutine computes the */
/*       generalized Cauchy point (GCP), defined as the first local */
/*       minimizer of the quadratic */

/*                  Q(x + s) = g's + 1/2 s'Bs */

/*       along the projected gradient direction P(x-tg,l,u). */
/*       The routine returns the GCP in xcp. */

/*     n is an integer variable. */
/*       On entry n is the dimension of the problem. */
/*       On exit n is unchanged. */

/*     x is a double precision array of dimension n. */
/*       On entry x is the starting point for the GCP computation. */
/*       On exit x is unchanged. */

/*     l is a double precision array of dimension n. */
/*       On entry l is the lower bound of x. */
/*       On exit l is unchanged. */

/*     u is a double precision array of dimension n. */
/*       On entry u is the upper bound of x. */
/*       On exit u is unchanged. */

/*     nbd is an integer array of dimension n. */
/*       On entry nbd represents the type of bounds imposed on the */
/*         variables, and must be specified as follows: */
/*         nbd(i)=0 if x(i) is unbounded, */
/*                1 if x(i) has only a lower bound, */
/*                2 if x(i) has both lower and upper bounds, and */
/*                3 if x(i) has only an upper bound. */
/*       On exit nbd is unchanged. */

/*     g is a double precision array of dimension n. */
/*       On entry g is the gradient of f(x).  g must be a nonzero vector. */
/*       On exit g is unchanged. */

/*     iorder is an integer working array of dimension n. */
/*       iorder will be used to store the breakpoints in the piecewise */
/*       linear path and free variables encountered. On exit, */
/*         iorder(1),...,iorder(nleft) are indices of breakpoints */
/*                                which have not been encountered; */
/*         iorder(nleft+1),...,iorder(nbreak) are indices of */
/*                                     encountered breakpoints; and */
/*         iorder(nfree),...,iorder(n) are indices of variables which */
/*                 have no bound constraits along the search direction. */

/*     iwhere is an integer array of dimension n. */
/*       On entry iwhere indicates only the permanently fixed (iwhere=3) */
/*       or free (iwhere= -1) components of x. */
/*       On exit iwhere records the status of the current x variables. */
/*       iwhere(i)=-3  if x(i) is free and has bounds, but is not moved */
/*                 0   if x(i) is free and has bounds, and is moved */
/*                 1   if x(i) is fixed at l(i), and l(i) .ne. u(i) */
/*                 2   if x(i) is fixed at u(i), and u(i) .ne. l(i) */
/*                 3   if x(i) is always fixed, i.e.,  u(i)=x(i)=l(i) */
/*                 -1  if x(i) is always free, i.e., it has no bounds. */

/*     t is a double precision working array of dimension n. */
/*       t will be used to store the break points. */

/*     d is a double precision array of dimension n used to store */
/*       the Cauchy direction P(x-tg)-x. */

/*     xcp is a double precision array of dimension n used to return the */
/*       GCP on exit. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric corrections */
/*         used to define the limited memory matrix. */
/*       On exit m is unchanged. */

/*     ws, wy, sy, and wt are double precision arrays. */
/*       On entry they store information that defines the */
/*                             limited memory BFGS matrix: */
/*         ws(n,m) stores S, a set of s-vectors; */
/*         wy(n,m) stores Y, a set of y-vectors; */
/*         sy(m,m) stores S'Y; */
/*         wt(m,m) stores the */
/*                 Cholesky factorization of (theta*S'S+LD^(-1)L'). */
/*       On exit these arrays are unchanged. */

/*     theta is a double precision variable. */
/*       On entry theta is the scaling factor specifying B_0 = theta I. */
/*       On exit theta is unchanged. */

/*     col is an integer variable. */
/*       On entry col is the actual number of variable metric */
/*         corrections stored so far. */
/*       On exit col is unchanged. */

/*     head is an integer variable. */
/*       On entry head is the location of the first s-vector (or y-vector) */
/*         in S (or Y). */
/*       On exit col is unchanged. */

/*     p is a double precision working array of dimension 2m. */
/*       p will be used to store the vector p = W^(T)d. */

/*     c is a double precision working array of dimension 2m. */
/*       c will be used to store the vector c = W^(T)(xcp-x). */

/*     wbp is a double precision working array of dimension 2m. */
/*       wbp will be used to store the row of W corresponding */
/*         to a breakpoint. */

/*     v is a double precision working array of dimension 2m. */

/*     nint is an integer variable. */
/*       On exit nint records the number of quadratic segments explored */
/*         in searching for the GCP. */

/*     sg and yg are double precision arrays of dimension m. */
/*       On entry sg  and yg store S'g and Y'g correspondingly. */
/*       On exit they are unchanged. */

/*     iprint is an INTEGER variable that must be set by the user. */
/*       It controls the frequency and type of output generated: */
/*        iprint<0    no output is generated; */
/*        iprint=0    print only one line at the last iteration; */
/*        0<iprint<99 print also f and |proj g| every iprint iterations; */
/*        iprint=99   print details of every iteration except n-vectors; */
/*        iprint=100  print also the changes of active set and final x; */
/*        iprint>100  print details of every iteration including x and g; */
/*       When iprint > 0, the file iterate.dat will be created to */
/*                        summarize the iteration. */

/*     sbgnrm is a double precision variable. */
/*       On entry sbgnrm is the norm of the projected gradient at x. */
/*       On exit sbgnrm is unchanged. */

/*     info is an integer variable. */
/*       On entry info is 0. */
/*       On exit info = 0       for normal return, */
/*                    = nonzero for abnormal return when the the system */
/*                              used in routine bmv is singular. */

/*     Subprograms called: */

/*       L-BFGS-B Library ... hpsolb, bmv. */

/*       Linpack ... dscal dcopy, daxpy. */


/*     References: */

/*       [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*       memory algorithm for bound constrained optimization'', */
/*       SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */

/*       [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN */
/*       Subroutines for Large Scale Bound Constrained Optimization'' */
/*       Tech. Report, NAM-11, EECS Department, Northwestern University, */
/*       1994. */

/*       (Postscript files of these papers are available via anonymous */
/*        ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.) */

/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       logical          xlower,xupper,bnded >*/
/*<    >*/
/*<    >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*     Check the status of the variables, reset iwhere(i) if necessary; */
/*       compute the Cauchy direction d and the breakpoints t; initialize */
/*       the derivative f1 and the vector p = W'd (for theta = 1). */
/*<       if (sbgnrm .le. zero) then >*/
    /* Parameter adjustments */
    --xcp;
    --d__;
    --t;
    --iwhere;
    --iorder;
    --g;
    --nbd;
    --u;
    --l;
    --x;
    --yg;
    --sg;
    --v;
    --wbp;
    --c__;
    --p;
    wt_dim1 = *m;
    wt_offset = 1 + wt_dim1;
    wt -= wt_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;

    /* Function Body */
    if (*sbgnrm <= 0.) {
/*<          if (iprint .ge. 0) write (6,*) 'Subgnorm = 0.  GCP = X.' >*/
        if (*iprint >= 0) {
            printf("Subgnorm = 0.  GCP = X.\n");
        }
/*<          call dcopy(n,x,1,xcp,1) >*/
        dcopy_(n, &x[1], &c__1, &xcp[1], &c__1);
/*<      return >*/
        return 0;
/*<       endif  >*/
    }
/*<       bnded = .true. >*/
    bnded = TRUE_;
/*<       nfree = n + 1 >*/
    nfree = *n + 1;
/*<       nbreak = 0 >*/
    nbreak = 0;
/*<       ibkmin = 0 >*/
    ibkmin = 0;
/*<       bkmin = zero >*/
    bkmin = 0.;
/*<       col2 = 2*col >*/
    col2 = *col << 1;
/*<       f1 = zero >*/
    f1 = 0.;
/*<       if (iprint .ge. 99) write (6,3010) >*/
/*
 3010 format (/,'---------------- CAUCHY entered-------------------')
 */
    if (*iprint >= 99) {
        printf("---------------- CAUCHY entered-------------------\n");
    }
/*     We set p to zero and build it up as we determine d. */
/*<       do 20 i = 1, col2 >*/
    i__1 = col2;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          p(i) = zero >*/
        p[i__] = 0.;
/*<   20  continue  >*/
/* L20: */
    }
/*     In the following loop we determine for each variable its bound */
/*        status and its breakpoint, and update p accordingly. */
/*        Smallest breakpoint is identified. */
/*<       do 50 i = 1, n  >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          neggi = -g(i)       >*/
        neggi = -g[i__];
/*<          if (iwhere(i) .ne. 3 .and. iwhere(i) .ne. -1) then >*/
        if (iwhere[i__] != 3 && iwhere[i__] != -1) {
/*             if x(i) is not a constant and has bounds, */
/*             compute the difference between x(i) and its bounds. */
/*<             if (nbd(i) .le. 2) tl = x(i) - l(i) >*/
            if (nbd[i__] <= 2) {
                tl = x[i__] - l[i__];
            }
/*<             if (nbd(i) .ge. 2) tu = u(i) - x(i) >*/
            if (nbd[i__] >= 2) {
                tu = u[i__] - x[i__];
            }
/*           If a variable is close enough to a bound */
/*             we treat it as at bound. */
/*<             xlower = nbd(i) .le. 2 .and. tl .le. zero >*/
            xlower = nbd[i__] <= 2 && tl <= 0.;
/*<             xupper = nbd(i) .ge. 2 .and. tu .le. zero >*/
            xupper = nbd[i__] >= 2 && tu <= 0.;
/*              reset iwhere(i). */
/*<             iwhere(i) = 0 >*/
            iwhere[i__] = 0;
/*<             if (xlower) then >*/
            if (xlower) {
/*<                if (neggi .le. zero) iwhere(i) = 1 >*/
                if (neggi <= 0.) {
                    iwhere[i__] = 1;
                }
/*<             else if (xupper) then >*/
            } else if (xupper) {
/*<                if (neggi .ge. zero) iwhere(i) = 2 >*/
                if (neggi >= 0.) {
                    iwhere[i__] = 2;
                }
/*<             else >*/
            } else {
/*<                if (abs(neggi) .le. zero) iwhere(i) = -3 >*/
                if (abs(neggi) <= 0.) {
                    iwhere[i__] = -3;
                }
/*<             endif >*/
            }
/*<          endif  >*/
        }
/*<          pointr = head >*/
        pointr = *head;
/*<          if (iwhere(i) .ne. 0 .and. iwhere(i) .ne. -1) then >*/
        if (iwhere[i__] != 0 && iwhere[i__] != -1) {
/*<             d(i) = zero >*/
            d__[i__] = 0.;
/*<          else >*/
        } else {
/*<             d(i) = neggi >*/
            d__[i__] = neggi;
/*<             f1 = f1 - neggi*neggi >*/
            f1 -= neggi * neggi;
/*             calculate p := p - W'e_i* (g_i). */
/*<             do 40 j = 1, col >*/
            i__2 = *col;
            for (j = 1; j <= i__2; ++j) {
/*<                p(j) = p(j) +  wy(i,pointr)* neggi >*/
                p[j] += wy[i__ + pointr * wy_dim1] * neggi;
/*<                p(col + j) = p(col + j) + ws(i,pointr)*neggi >*/
                p[*col + j] += ws[i__ + pointr * ws_dim1] * neggi;
/*<                pointr = mod(pointr,m) + 1 >*/
                pointr = pointr % *m + 1;
/*<   40        continue  >*/
/* L40: */
            }
/*<    >*/
            if (nbd[i__] <= 2 && nbd[i__] != 0 && neggi < 0.) {
/*                                 x(i) + d(i) is bounded; compute t(i). */
/*<                nbreak = nbreak + 1 >*/
                ++nbreak;
/*<                iorder(nbreak) = i >*/
                iorder[nbreak] = i__;
/*<                t(nbreak) = tl/(-neggi) >*/
                t[nbreak] = tl / (-neggi);
/*<            if (nbreak .eq. 1 .or. t(nbreak) .lt. bkmin) then >*/
                if (nbreak == 1 || t[nbreak] < bkmin) {
/*<               bkmin = t(nbreak) >*/
                    bkmin = t[nbreak];
/*<               ibkmin = nbreak >*/
                    ibkmin = nbreak;
/*<                endif >*/
                }
/*<             else if (nbd(i) .ge. 2 .and. neggi .gt. zero) then >*/
            } else if (nbd[i__] >= 2 && neggi > 0.) {
/*                                 x(i) + d(i) is bounded; compute t(i). */
/*<                nbreak = nbreak + 1 >*/
                ++nbreak;
/*<                iorder(nbreak) = i >*/
                iorder[nbreak] = i__;
/*<                t(nbreak) = tu/neggi >*/
                t[nbreak] = tu / neggi;
/*<            if (nbreak .eq. 1 .or. t(nbreak) .lt. bkmin) then >*/
                if (nbreak == 1 || t[nbreak] < bkmin) {
/*<               bkmin = t(nbreak) >*/
                    bkmin = t[nbreak];
/*<               ibkmin = nbreak >*/
                    ibkmin = nbreak;
/*<                endif >*/
                }
/*<             else >*/
            } else {
/*                x(i) + d(i) is not bounded. */
/*<                nfree = nfree - 1 >*/
                --nfree;
/*<                iorder(nfree) = i >*/
                iorder[nfree] = i__;
/*<                if (abs(neggi) .gt. zero) bnded = .false. >*/
                if (abs(neggi) > 0.) {
                    bnded = FALSE_;
                }
/*<             endif >*/
            }
/*<          endif >*/
        }
/*<   50  continue  >*/
/* L50: */
    }
/*     The indices of the nonzero components of d are now stored */
/*       in iorder(1),...,iorder(nbreak) and iorder(nfree),...,iorder(n). */
/*       The smallest of the nbreak breakpoints is in t(ibkmin)=bkmin. */
/*<       if (theta .ne. one) then >*/
    if (*theta != 1.) {
/*                   complete the initialization of p for theta not= one. */
/*<          call dscal(col,theta,p(col+1),1) >*/
        dscal_(col, theta, &p[*col + 1], &c__1);
/*<       endif >*/
    }
/*     Initialize GCP xcp = x. */
/*<       call dcopy(n,x,1,xcp,1) >*/
    dcopy_(n, &x[1], &c__1, &xcp[1], &c__1);
/*<       if (nbreak .eq. 0 .and. nfree .eq. n + 1) then >*/
    if (nbreak == 0 && nfree == *n + 1) {
/*                  is a zero vector, return with the initial xcp as GCP. */
/*<          if (iprint .gt. 100) write (6,1010) (xcp(i), i = 1, n) >*/
/*
 1010 format ('Cauchy X =  ',/,(4x,1p,6(1x,d11.4)))
*/
        if (*iprint > 100) {
            i__1 = *n;
            lbfgsb_printf_vec("Cauchy X", xcp, i__1);
        }
/*<          return >*/
        return 0;
/*<       endif     >*/
    }
/*     Initialize c = W'(xcp - x) = 0. */
/*<       do 60 j = 1, col2 >*/
    i__1 = col2;
    for (j = 1; j <= i__1; ++j) {
/*<          c(j) = zero >*/
        c__[j] = 0.;
/*<   60  continue  >*/
/* L60: */
    }
/*     Initialize derivative f2. */
/*<       f2 =  -theta*f1  >*/
    f2 = -(*theta) * f1;
/*<       f2_org  =  f2 >*/
    f2_org__ = f2;
/*<       if (col .gt. 0) then >*/
    if (*col > 0) {
/*<              call bmv(m,sy,wt,col,p,v,info) >*/
        bmv_(m, &sy[sy_offset], &wt[wt_offset], col, &p[1], &v[1], info);
/*<      if (info .ne. 0) return >*/
        if (*info != 0) {
            return 0;
        }
/*<              f2 = f2 - ddot(col2,v,1,p,1) >*/
        f2 -= ddot_(&col2, &v[1], &c__1, &p[1], &c__1);
/*<       endif >*/
    }
/*<       dtm = -f1/f2 >*/
    dtm = -f1 / f2;
/*<       tsum = zero >*/
    tsum = 0.;
/*<       nint = 1 >*/
    *nint = 1;
/*<    >*/
    if (*iprint >= 99) {
        printf("There are %ld  breakpoints.\n", nbreak);
    }
/*     If there are no breakpoints, locate the GCP and return. */
/*<       if (nbreak .eq. 0) goto 888 >*/
    if (nbreak == 0) {
        goto L888;
    }
/*<       nleft = nbreak >*/
    nleft = nbreak;
/*<       iter = 1 >*/
    iter = 1;
/*<       tj = zero >*/
    tj = 0.;
/* ------------------- the beginning of the loop ------------------------- */
/*<  777  continue >*/
L777:
/*     Find the next smallest breakpoint; */
/*       compute dt = t(nleft) - t(nleft + 1). */
/*<       tj0 = tj >*/
    tj0 = tj;
/*<       if (iter .eq. 1) then >*/
    if (iter == 1) {
/*         Since we already have the smallest breakpoint we need not do */
/*         heapsort yet. Often only one breakpoint is used and the */
/*         cost of heapsort is avoided. */
/*<      tj = bkmin >*/
        tj = bkmin;
/*<      ibp = iorder(ibkmin) >*/
        ibp = iorder[ibkmin];
/*<       else >*/
    } else {
/*<          if (iter .eq. 2) then >*/
        if (iter == 2) {
/*             Replace the already used smallest breakpoint with the */
/*             breakpoint numbered nbreak > nlast, before heapsort call. */
/*<             if (ibkmin .ne. nbreak) then >*/
            if (ibkmin != nbreak) {
/*<                t(ibkmin) = t(nbreak) >*/
                t[ibkmin] = t[nbreak];
/*<            iorder(ibkmin) = iorder(nbreak) >*/
                iorder[ibkmin] = iorder[nbreak];
/*<             endif  >*/
            }
/*        Update heap structure of breakpoints */
/*           (if iter=2, initialize heap). */
/*<          endif >*/
        }
/*<          call hpsolb(nleft,t,iorder,iter-2) >*/
        i__1 = iter - 2;
        hpsolb_(&nleft, &t[1], &iorder[1], &i__1);
/*<          tj = t(nleft) >*/
        tj = t[nleft];
/*<          ibp = iorder(nleft)   >*/
        ibp = iorder[nleft];
/*<       endif  >*/
    }
/*<       dt = tj - tj0 >*/
    dt = tj - tj0;
/*<       if (dt .ne. zero .and. iprint .ge. 100) then >*/
    if (dt != 0. && *iprint >= 100) {
/*<          write (6,4011) nint,f1,f2 >*/
/*
 4010 format ('Piece    ',i3,' --f1, f2 at start point ',1p,2(1x,d11.4))
*/
        printf("Piece    %3ld --f1, f2 at start point  %11.4g %11.5g\n",
               *nint, f1, f2);
/*<          write (6,5010) dt >*/
/*
 5010 format ('Distance to the next break point =  ',1p,d11.4)
 */
        printf("Distance to the next break point =  %11.4g", dt);
/*<          write (6,6010) dtm >*/
/*
 6010 format ('Distance to the stationary point =  ',1p,d11.4) 
 */
        printf("Distance to the stationary point =  %11.4g", dtm);
/*<       endif       >*/
    }
/*     If a minimizer is within this interval, locate the GCP and return. */
/*<       if (dtm .lt. dt) goto 888 >*/
    if (dtm < dt) {
        goto L888;
    }
/*     Otherwise fix one variable and */
/*       reset the corresponding component of d to zero. */
/*<       tsum = tsum + dt >*/
    tsum += dt;
/*<       nleft = nleft - 1 >*/
    --nleft;
/*<       iter = iter + 1 >*/
    ++iter;
/*<       dibp = d(ibp) >*/
    dibp = d__[ibp];
/*<       d(ibp) = zero >*/
    d__[ibp] = 0.;
/*<       if (dibp .gt. zero) then >*/
    if (dibp > 0.) {
/*<      zibp = u(ibp) - x(ibp) >*/
        zibp = u[ibp] - x[ibp];
/*<      xcp(ibp) = u(ibp) >*/
        xcp[ibp] = u[ibp];
/*<          iwhere(ibp) = 2 >*/
        iwhere[ibp] = 2;
/*<       else >*/
    } else {
/*<      zibp = l(ibp) - x(ibp) >*/
        zibp = l[ibp] - x[ibp];
/*<      xcp(ibp) = l(ibp) >*/
        xcp[ibp] = l[ibp];
/*<          iwhere(ibp) = 1 >*/
        iwhere[ibp] = 1;
/*<       endif >*/
    }
/*<       if (iprint .ge. 100) write (6,*) 'Variable  ',ibp,'  is fixed.' >*/
    if (*iprint >= 100) {
        printf("Variable  %ld  is fixed.\n", ibp);
    }
/*<       if (nleft .eq. 0 .and. nbreak .eq. n) then >*/
    if (nleft == 0 && nbreak == *n) {
/*                                             all n variables are fixed, */
/*                                                return with xcp as GCP. */
/*<      dtm = dt >*/
        dtm = dt;
/*<      goto 999 >*/
        goto L999;
/*<       endif >*/
    }
/*     Update the derivative information. */
/*<       nint = nint + 1 >*/
    ++(*nint);
/*<       dibp2 = dibp**2 >*/
/* Computing 2nd power */
    d__1 = dibp;
    dibp2 = d__1 * d__1;
/*     Update f1 and f2. */
/*        temporarily set f1 and f2 for col=0. */
/*<       f1 = f1 + dt*f2 + dibp2 - theta*dibp*zibp >*/
    f1 = f1 + dt * f2 + dibp2 - *theta * dibp * zibp;
/*<       f2 = f2 - theta*dibp2 >*/
    f2 -= *theta * dibp2;
/*<       if (col .gt. 0) then >*/
    if (*col > 0) {
/*                          update c = c + dt*p. */
/*<      call daxpy(col2,dt,p,1,c,1) >*/
        daxpy_(&col2, &dt, &p[1], &c__1, &c__[1], &c__1);
/*           choose wbp, */
/*           the row of W corresponding to the breakpoint encountered. */
/*<              pointr = head >*/
        pointr = *head;
/*<          do 70 j = 1,col >*/
        i__1 = *col;
        for (j = 1; j <= i__1; ++j) {
/*<         wbp(j) = wy(ibp,pointr) >*/
            wbp[j] = wy[ibp + pointr * wy_dim1];
/*<         wbp(col + j) = theta*ws(ibp,pointr) >*/
            wbp[*col + j] = *theta * ws[ibp + pointr * ws_dim1];
/*<             pointr = mod(pointr,m) + 1 >*/
            pointr = pointr % *m + 1;
/*<   70     continue  >*/
/* L70: */
        }
/*           compute (wbp)Mc, (wbp)Mp, and (wbp)M(wbp)'. */
/*<          call bmv(m,sy,wt,col,wbp,v,info) >*/
        bmv_(m, &sy[sy_offset], &wt[wt_offset], col, &wbp[1], &v[1], info);
/*<      if (info .ne. 0) return >*/
        if (*info != 0) {
            return 0;
        }
/*<      wmc = ddot(col2,c,1,v,1) >*/
        wmc = ddot_(&col2, &c__[1], &c__1, &v[1], &c__1);
/*<      wmp = ddot(col2,p,1,v,1)  >*/
        wmp = ddot_(&col2, &p[1], &c__1, &v[1], &c__1);
/*<      wmw = ddot(col2,wbp,1,v,1) >*/
        wmw = ddot_(&col2, &wbp[1], &c__1, &v[1], &c__1);
/*           update p = p - dibp*wbp. */
/*<              call daxpy(col2,-dibp,wbp,1,p,1) >*/
        d__1 = -dibp;
        daxpy_(&col2, &d__1, &wbp[1], &c__1, &p[1], &c__1);
/*           complete updating f1 and f2 while col > 0. */
/*<              f1 = f1 + dibp*wmc >*/
        f1 += dibp * wmc;
/*<              f2 = f2 + 2.0d0*dibp*wmp - dibp2*wmw >*/
        f2 = f2 + dibp * 2. * wmp - dibp2 * wmw;
/*<       endif >*/
    }
/*<       f2 = max(epsmch*f2_org,f2) >*/
/* Computing MAX */
    d__1 = *epsmch * f2_org__;
    f2 = max(d__1,f2);
/*<       if (nleft .gt. 0) then >*/
    if (nleft > 0) {
/*<          dtm = -f1/f2 >*/
        dtm = -f1 / f2;
/*<          goto 777 >*/
        goto L777;
/*                 to repeat the loop for unsearched intervals. */
/*<       else if(bnded) then >*/
    } else if (bnded) {
/*<              f1 = zero >*/
        f1 = 0.;
/*<              f2 = zero >*/
        f2 = 0.;
/*<      dtm = zero >*/
        dtm = 0.;
/*<       else >*/
    } else {
/*<          dtm = -f1/f2 >*/
        dtm = -f1 / f2;
/*<       endif  >*/
    }
/* ------------------- the end of the loop ------------------------------- */
/*<  888  continue >*/
L888:
/*<       if (iprint .ge. 99) then >*/
    if (*iprint >= 99) {
/*<          write (6,*) >*/
        printf("\n");
/*<          write (6,*) 'GCP found in this segment' >*/
        printf("GCP found in this segment\n");
/*<          write (6,4010) nint,f1,f2 >*/
/*
 4010 format ('Piece    ',i3,' --f1, f2 at start point ',1p,2(1x,d11.4))
*/
        printf("Piece    %3ld --f1, f2 at start point  %11.4g %11.4g\n",
               *nint, f1, f2);
/*<          write (6,6010) dtm >*/
/*
 6010 format ('Distance to the stationary point =  ',1p,d11.4)
*/
        printf("Distance to the stationary point =  %11.4g\n", dtm);
/*<       endif  >*/
    }
/*<       if (dtm .le. zero) dtm = zero >*/
    if (dtm <= 0.) {
        dtm = 0.;
    }
/*<       tsum = tsum + dtm >*/
    tsum += dtm;
/*     Move free variables (i.e., the ones w/o breakpoints) and */
/*       the variables whose breakpoints haven't been reached. */
/*<       call daxpy(n,tsum,d,1,xcp,1) >*/
    daxpy_(n, &tsum, &d__[1], &c__1, &xcp[1], &c__1);
/*<  999  continue >*/
L999:
/*     Update c = c + dtm*p = W'(x^c - x) */
/*       which will be used in computing r = Z'(B(x^c - x) + g). */
/*<       if (col .gt. 0) call daxpy(col2,dtm,p,1,c,1) >*/
    if (*col > 0) {
        daxpy_(&col2, &dtm, &p[1], &c__1, &c__[1], &c__1);
    }
/*<       if (iprint .gt. 100) write (6,1010) (xcp(i),i = 1,n) >*/
    if (*iprint > 100) {
        i__1 = *n;
        lbfgsb_printf_vec("Cauchy X", xcp, i__1);
    }
/*<       if (iprint .ge. 99) write (6,2010) >*/
/*
 2010 format (/,'---------------- exit CAUCHY----------------------',/)
 */
    if (*iprint >= 99) {
        printf("---------------- exit CAUCHY----------------------\n");
    }
/*<  1010 format ('Cauchy X =  ',/,(4x,1p,6(1x,d11.4))) >*/
/*<  2010 format (/,'---------------- exit CAUCHY----------------------',/) >*/
/*<  3010 format (/,'---------------- CAUCHY entered-------------------') >*/
/*<  4010 format ('Piece    ',i3,' --f1, f2 at start point ',1p,2(1x,d11.4)) >*/
/*<  4 >*/
/*<  5010 format ('Distance to the next break point =  ',1p,d11.4) >*/
/*<  6010 format ('Distance to the stationary point =  ',1p,d11.4)  >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* cauchy_ */

/* ====================== The end of cauchy ============================== */
/*<    >*/
/* Subroutine */ int cmprlb_(integer *n, integer *m, doublereal *x, 
        doublereal *g, doublereal *ws, doublereal *wy, doublereal *sy, 
        doublereal *wt, doublereal *z__, doublereal *r__, doublereal *wa, 
        integer *index, doublereal *theta, integer *col, integer *head, 
        integer *nfree, logical *cnstnd, integer *info)
{
    /* System generated locals */
    integer ws_dim1, ws_offset, wy_dim1, wy_offset, sy_dim1, sy_offset, 
            wt_dim1, wt_offset, i__1, i__2;

    /* Local variables */
    integer i__, j, k;
    doublereal a1, a2;
    extern /* Subroutine */ int bmv_(integer *, doublereal *, doublereal *, 
            integer *, doublereal *, doublereal *, integer *);
    integer pointr;

/*<       logical          cnstnd >*/
/*<       integer          n, m, col, head, nfree, info, index(n) >*/
/*<    >*/
/*     ************ */

/*     Subroutine cmprlb */

/*       This subroutine computes r=-Z'B(xcp-xk)-Z'g by using */
/*         wa(2m+1)=W'(xcp-x) from subroutine cauchy. */

/*     Subprograms called: */

/*       L-BFGS-B Library ... bmv. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          i,j,k,pointr >*/
/*<       double precision a1,a2 >*/
/*<       if (.not. cnstnd .and. col .gt. 0) then  >*/
    /* Parameter adjustments */
    --index;
    --r__;
    --z__;
    --g;
    --x;
    --wa;
    wt_dim1 = *m;
    wt_offset = 1 + wt_dim1;
    wt -= wt_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;

    /* Function Body */
    if (! (*cnstnd) && *col > 0) {
/*<          do 26 i = 1, n >*/
        i__1 = *n;
        for (i__ = 1; i__ <= i__1; ++i__) {
/*<         r(i) = -g(i) >*/
            r__[i__] = -g[i__];
/*<   26     continue >*/
/* L26: */
        }
/*<       else >*/
    } else {
/*<          do 30 i = 1, nfree >*/
        i__1 = *nfree;
        for (i__ = 1; i__ <= i__1; ++i__) {
/*<             k = index(i) >*/
            k = index[i__];
/*<         r(i) = -theta*(z(k) - x(k)) - g(k) >*/
            r__[i__] = -(*theta) * (z__[k] - x[k]) - g[k];
/*<   30     continue >*/
/* L30: */
        }
/*<              call bmv(m,sy,wt,col,wa(2*m+1),wa(1),info) >*/
        bmv_(m, &sy[sy_offset], &wt[wt_offset], col, &wa[(*m << 1) + 1], &wa[
                1], info);
/*<          if (info .ne. 0) then >*/
        if (*info != 0) {
/*<             info = -8 >*/
            *info = -8;
/*<         return >*/
            return 0;
/*<          endif >*/
        }
/*<              pointr = head  >*/
        pointr = *head;
/*<              do 34 j = 1, col >*/
        i__1 = *col;
        for (j = 1; j <= i__1; ++j) {
/*<                 a1 = wa(j) >*/
            a1 = wa[j];
/*<             a2 = theta*wa(col + j) >*/
            a2 = *theta * wa[*col + j];
/*<         do 32 i = 1, nfree >*/
            i__2 = *nfree;
            for (i__ = 1; i__ <= i__2; ++i__) {
/*<            k = index(i) >*/
                k = index[i__];
/*<            r(i) = r(i) + wy(k,pointr)*a1 + ws(k,pointr)*a2 >*/
                r__[i__] = r__[i__] + wy[k + pointr * wy_dim1] * a1 + ws[k + 
                        pointr * ws_dim1] * a2;
/*<   32        continue >*/
/* L32: */
            }
/*<         pointr = mod(pointr,m) + 1 >*/
            pointr = pointr % *m + 1;
/*<   34     continue >*/
/* L34: */
        }
/*<       endif >*/
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* cmprlb_ */

/* ======================= The end of cmprlb ============================= */
/*<       subroutine errclb(n, m, factr, l, u, nbd, task, info, k) >*/
/* Subroutine */ int errclb_(integer *n, integer *m, doublereal *factr, 
        doublereal *l, doublereal *u, integer *nbd, char *task, integer *info,
         integer *k, ftnlen task_len)
{
    /* System generated locals */
    integer i__1;

    /* Builtin functions */
    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    integer i__;

    (void)task_len;

/*<       character*60     task >*/
/*<       integer          n, m, info, k, nbd(n) >*/
/*<       double precision factr, l(n), u(n) >*/
/*     ************ */

/*     Subroutine errclb */

/*     This subroutine checks the validity of the input data. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          i >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*     Check the input arguments for errors. */
/*<       if (n .le. 0) task = 'ERROR: N .LE. 0' >*/
    /* Parameter adjustments */
    --nbd;
    --u;
    --l;

    /* Function Body */
    if (*n <= 0) {
        s_copy(task, "ERROR: N .LE. 0", (ftnlen)60, (ftnlen)15);
    }
/*<       if (m .le. 0) task = 'ERROR: M .LE. 0' >*/
    if (*m <= 0) {
        s_copy(task, "ERROR: M .LE. 0", (ftnlen)60, (ftnlen)15);
    }
/*<       if (factr .lt. zero) task = 'ERROR: FACTR .LT. 0' >*/
    if (*factr < 0.) {
        s_copy(task, "ERROR: FACTR .LT. 0", (ftnlen)60, (ftnlen)19);
    }
/*     Check the validity of the arrays nbd(i), u(i), and l(i). */
/*<       do 10 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          if (nbd(i) .lt. 0 .or. nbd(i) .gt. 3) then >*/
        if (nbd[i__] < 0 || nbd[i__] > 3) {
/*                                                   return */
/*<             task = 'ERROR: INVALID NBD' >*/
            s_copy(task, "ERROR: INVALID NBD", (ftnlen)60, (ftnlen)18);
/*<         info = -6 >*/
            *info = -6;
/*<         k = i >*/
            *k = i__;
/*<          endif >*/
        }
/*<      if (nbd(i) .eq. 2) then >*/
        if (nbd[i__] == 2) {
/*<         if (l(i) .gt. u(i)) then >*/
            if (l[i__] > u[i__]) {
/*                                    return */
/*<                task = 'ERROR: NO FEASIBLE SOLUTION' >*/
                s_copy(task, "ERROR: NO FEASIBLE SOLUTION", (ftnlen)60, (
                        ftnlen)27);
/*<            info = -7 >*/
                *info = -7;
/*<            k = i >*/
                *k = i__;
/*<             endif >*/
            }
/*<          endif >*/
        }
/*<   10  continue >*/
/* L10: */
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* errclb_ */

/* ======================= The end of errclb ============================= */
/*<    >*/
/* Subroutine */ int formk_(integer *n, integer *nsub, integer *ind, integer *
        nenter, integer *ileave, integer *indx2, integer *iupdat, logical *
        updatd, doublereal *wn, doublereal *wn1, integer *m, doublereal *ws, 
        doublereal *wy, doublereal *sy, doublereal *theta, integer *col, 
        integer *head, integer *info)
{
    /* System generated locals */
    integer wn_dim1, wn_offset, wn1_dim1, wn1_offset, ws_dim1, ws_offset, 
            wy_dim1, wy_offset, sy_dim1, sy_offset, i__1, i__2, i__3;

    /* Local variables */
    integer i__, k, k1, m2, is, js, iy, jy, is1, js1, col2, dend, pend;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    integer upcl;
    doublereal temp1, temp2, temp3, temp4;
    extern /* Subroutine */ int dpofa_(doublereal *, integer *, integer *, 
            integer *), dcopy_(integer *, doublereal *, integer *, doublereal 
            *, integer *), dtrsl_(doublereal *, integer *, integer *, 
            doublereal *, integer *, integer *);
    integer ipntr, jpntr, dbegin, pbegin;

/*<    >*/
/*<    >*/
/*<       logical          updatd >*/
/*     ************ */

/*     Subroutine formk */

/*     This subroutine forms  the LEL^T factorization of the indefinite */

/*       matrix    K = [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                     [L_a -R_z           theta*S'AA'S ] */
/*                                                    where E = [-I  0] */
/*                                                              [ 0  I] */
/*     The matrix K can be shown to be equal to the matrix M^[-1]N */
/*       occurring in section 5.1 of [1], as well as to the matrix */
/*       Mbar^[-1] Nbar in section 5.3. */

/*     n is an integer variable. */
/*       On entry n is the dimension of the problem. */
/*       On exit n is unchanged. */

/*     nsub is an integer variable */
/*       On entry nsub is the number of subspace variables in free set. */
/*       On exit nsub is not changed. */

/*     ind is an integer array of dimension nsub. */
/*       On entry ind specifies the indices of subspace variables. */
/*       On exit ind is unchanged. */

/*     nenter is an integer variable. */
/*       On entry nenter is the number of variables entering the */
/*         free set. */
/*       On exit nenter is unchanged. */

/*     ileave is an integer variable. */
/*       On entry indx2(ileave),...,indx2(n) are the variables leaving */
/*         the free set. */
/*       On exit ileave is unchanged. */

/*     indx2 is an integer array of dimension n. */
/*       On entry indx2(1),...,indx2(nenter) are the variables entering */
/*         the free set, while indx2(ileave),...,indx2(n) are the */
/*         variables leaving the free set. */
/*       On exit indx2 is unchanged. */

/*     iupdat is an integer variable. */
/*       On entry iupdat is the total number of BFGS updates made so far. */
/*       On exit iupdat is unchanged. */

/*     updatd is a logical variable. */
/*       On entry 'updatd' is true if the L-BFGS matrix is updatd. */
/*       On exit 'updatd' is unchanged. */

/*     wn is a double precision array of dimension 2m x 2m. */
/*       On entry wn is unspecified. */
/*       On exit the upper triangle of wn stores the LEL^T factorization */
/*         of the 2*col x 2*col indefinite matrix */
/*                     [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                     [L_a -R_z           theta*S'AA'S ] */

/*     wn1 is a double precision array of dimension 2m x 2m. */
/*       On entry wn1 stores the lower triangular part of */
/*                     [Y' ZZ'Y   L_a'+R_z'] */
/*                     [L_a+R_z   S'AA'S   ] */
/*         in the previous iteration. */
/*       On exit wn1 stores the corresponding updated matrices. */
/*       The purpose of wn1 is just to store these inner products */
/*       so they can be easily updated and inserted into wn. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric corrections */
/*         used to define the limited memory matrix. */
/*       On exit m is unchanged. */

/*     ws, wy, sy, and wtyy are double precision arrays; */
/*     theta is a double precision variable; */
/*     col is an integer variable; */
/*     head is an integer variable. */
/*       On entry they store the information defining the */
/*                                          limited memory BFGS matrix: */
/*         ws(n,m) stores S, a set of s-vectors; */
/*         wy(n,m) stores Y, a set of y-vectors; */
/*         sy(m,m) stores S'Y; */
/*         wtyy(m,m) stores the Cholesky factorization */
/*                                   of (theta*S'S+LD^(-1)L') */
/*         theta is the scaling factor specifying B_0 = theta I; */
/*         col is the number of variable metric corrections stored; */
/*         head is the location of the 1st s- (or y-) vector in S (or Y). */
/*       On exit they are unchanged. */

/*     info is an integer variable. */
/*       On entry info is unspecified. */
/*       On exit info =  0 for normal return; */
/*                    = -1 when the 1st Cholesky factorization failed; */
/*                    = -2 when the 2st Cholesky factorization failed. */

/*     Subprograms called: */

/*       Linpack ... dcopy, dpofa, dtrsl. */


/*     References: */
/*       [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*       memory algorithm for bound constrained optimization'', */
/*       SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */

/*       [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: a */
/*       limited memory FORTRAN code for solving bound constrained */
/*       optimization problems'', Tech. Report, NAM-11, EECS Department, */
/*       Northwestern University, 1994. */

/*       (Postscript files of these papers are available via anonymous */
/*        ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.) */

/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<    >*/
/*<       double precision ddot,temp1,temp2,temp3,temp4 >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*     Form the lower triangular part of */
/*               WN1 = [Y' ZZ'Y   L_a'+R_z'] */
/*                     [L_a+R_z   S'AA'S   ] */
/*        where L_a is the strictly lower triangular part of S'AA'Y */
/*              R_z is the upper triangular part of S'ZZ'Y. */
/*<       if (updatd) then >*/
    /* Parameter adjustments */
    --indx2;
    --ind;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;
    wn1_dim1 = 2 * *m;
    wn1_offset = 1 + wn1_dim1;
    wn1 -= wn1_offset;
    wn_dim1 = 2 * *m;
    wn_offset = 1 + wn_dim1;
    wn -= wn_offset;

    /* Function Body */
    if (*updatd) {
/*<          if (iupdat .gt. m) then  >*/
        if (*iupdat > *m) {
/*                                 shift old part of WN1. */
/*<             do 10 jy = 1, m - 1 >*/
            i__1 = *m - 1;
            for (jy = 1; jy <= i__1; ++jy) {
/*<                js = m + jy >*/
                js = *m + jy;
/*<            call dcopy(m-jy,wn1(jy+1,jy+1),1,wn1(jy,jy),1) >*/
                i__2 = *m - jy;
                dcopy_(&i__2, &wn1[jy + 1 + (jy + 1) * wn1_dim1], &c__1, &wn1[
                        jy + jy * wn1_dim1], &c__1);
/*<            call dcopy(m-jy,wn1(js+1,js+1),1,wn1(js,js),1) >*/
                i__2 = *m - jy;
                dcopy_(&i__2, &wn1[js + 1 + (js + 1) * wn1_dim1], &c__1, &wn1[
                        js + js * wn1_dim1], &c__1);
/*<            call dcopy(m-1,wn1(m+2,jy+1),1,wn1(m+1,jy),1) >*/
                i__2 = *m - 1;
                dcopy_(&i__2, &wn1[*m + 2 + (jy + 1) * wn1_dim1], &c__1, &wn1[
                        *m + 1 + jy * wn1_dim1], &c__1);
/*<   10        continue >*/
/* L10: */
            }
/*<          endif >*/
        }
/*          put new rows in blocks (1,1), (2,1) and (2,2). */
/*<          pbegin = 1 >*/
        pbegin = 1;
/*<      pend = nsub >*/
        pend = *nsub;
/*<          dbegin = nsub + 1 >*/
        dbegin = *nsub + 1;
/*<      dend = n >*/
        dend = *n;
/*<          iy = col >*/
        iy = *col;
/*<          is = m + col >*/
        is = *m + *col;
/*<          ipntr = head + col - 1 >*/
        ipntr = *head + *col - 1;
/*<          if (ipntr .gt. m) ipntr = ipntr - m         >*/
        if (ipntr > *m) {
            ipntr -= *m;
        }
/*<          jpntr = head >*/
        jpntr = *head;
/*<          do 20 jy = 1, col >*/
        i__1 = *col;
        for (jy = 1; jy <= i__1; ++jy) {
/*<             js = m + jy >*/
            js = *m + jy;
/*<             temp1 = zero >*/
            temp1 = 0.;
/*<         temp2 = zero >*/
            temp2 = 0.;
/*<         temp3 = zero >*/
            temp3 = 0.;
/*             compute element jy of row 'col' of Y'ZZ'Y */
/*<         do 15 k = pbegin, pend >*/
            i__2 = pend;
            for (k = pbegin; k <= i__2; ++k) {
/*<            k1 = ind(k) >*/
                k1 = ind[k];
/*<            temp1 = temp1 + wy(k1,ipntr)*wy(k1,jpntr) >*/
                temp1 += wy[k1 + ipntr * wy_dim1] * wy[k1 + jpntr * wy_dim1];
/*<   15        continue >*/
/* L15: */
            }
/*             compute elements jy of row 'col' of L_a and S'AA'S */
/*<         do 16 k = dbegin, dend >*/
            i__2 = dend;
            for (k = dbegin; k <= i__2; ++k) {
/*<            k1 = ind(k) >*/
                k1 = ind[k];
/*<            temp2 = temp2 + ws(k1,ipntr)*ws(k1,jpntr) >*/
                temp2 += ws[k1 + ipntr * ws_dim1] * ws[k1 + jpntr * ws_dim1];
/*<            temp3 = temp3 + ws(k1,ipntr)*wy(k1,jpntr) >*/
                temp3 += ws[k1 + ipntr * ws_dim1] * wy[k1 + jpntr * wy_dim1];
/*<   16        continue >*/
/* L16: */
            }
/*<         wn1(iy,jy) = temp1 >*/
            wn1[iy + jy * wn1_dim1] = temp1;
/*<         wn1(is,js) = temp2 >*/
            wn1[is + js * wn1_dim1] = temp2;
/*<         wn1(is,jy) = temp3 >*/
            wn1[is + jy * wn1_dim1] = temp3;
/*<             jpntr = mod(jpntr,m) + 1 >*/
            jpntr = jpntr % *m + 1;
/*<   20     continue >*/
/* L20: */
        }
/*          put new column in block (2,1). */
/*<          jy = col    >*/
        jy = *col;
/*<          jpntr = head + col - 1 >*/
        jpntr = *head + *col - 1;
/*<          if (jpntr .gt. m) jpntr = jpntr - m >*/
        if (jpntr > *m) {
            jpntr -= *m;
        }
/*<          ipntr = head >*/
        ipntr = *head;
/*<          do 30 i = 1, col >*/
        i__1 = *col;
        for (i__ = 1; i__ <= i__1; ++i__) {
/*<             is = m + i >*/
            is = *m + i__;
/*<         temp3 = zero >*/
            temp3 = 0.;
/*             compute element i of column 'col' of R_z */
/*<         do 25 k = pbegin, pend >*/
            i__2 = pend;
            for (k = pbegin; k <= i__2; ++k) {
/*<            k1 = ind(k) >*/
                k1 = ind[k];
/*<            temp3 = temp3 + ws(k1,ipntr)*wy(k1,jpntr) >*/
                temp3 += ws[k1 + ipntr * ws_dim1] * wy[k1 + jpntr * wy_dim1];
/*<   25        continue  >*/
/* L25: */
            }
/*<         ipntr = mod(ipntr,m) + 1 >*/
            ipntr = ipntr % *m + 1;
/*<             wn1(is,jy) = temp3 >*/
            wn1[is + jy * wn1_dim1] = temp3;
/*<   30     continue >*/
/* L30: */
        }
/*<      upcl = col - 1 >*/
        upcl = *col - 1;
/*<       else >*/
    } else {
/*<          upcl = col >*/
        upcl = *col;
/*<       endif >*/
    }
/*       modify the old parts in blocks (1,1) and (2,2) due to changes */
/*       in the set of free variables. */
/*<       ipntr = head   >*/
    ipntr = *head;
/*<       do 45 iy = 1, upcl >*/
    i__1 = upcl;
    for (iy = 1; iy <= i__1; ++iy) {
/*<          is = m + iy >*/
        is = *m + iy;
/*<      jpntr = head >*/
        jpntr = *head;
/*<              do 40 jy = 1, iy >*/
        i__2 = iy;
        for (jy = 1; jy <= i__2; ++jy) {
/*<         js = m + jy >*/
            js = *m + jy;
/*<         temp1 = zero >*/
            temp1 = 0.;
/*<         temp2 = zero >*/
            temp2 = 0.;
/*<         temp3 = zero >*/
            temp3 = 0.;
/*<         temp4 = zero >*/
            temp4 = 0.;
/*<         do 35 k = 1, nenter >*/
            i__3 = *nenter;
            for (k = 1; k <= i__3; ++k) {
/*<            k1 = indx2(k) >*/
                k1 = indx2[k];
/*<            temp1 = temp1 + wy(k1,ipntr)*wy(k1,jpntr) >*/
                temp1 += wy[k1 + ipntr * wy_dim1] * wy[k1 + jpntr * wy_dim1];
/*<            temp2 = temp2 + ws(k1,ipntr)*ws(k1,jpntr) >*/
                temp2 += ws[k1 + ipntr * ws_dim1] * ws[k1 + jpntr * ws_dim1];
/*<   35        continue >*/
/* L35: */
            }
/*<         do 36 k = ileave, n >*/
            i__3 = *n;
            for (k = *ileave; k <= i__3; ++k) {
/*<            k1 = indx2(k) >*/
                k1 = indx2[k];
/*<            temp3 = temp3 + wy(k1,ipntr)*wy(k1,jpntr) >*/
                temp3 += wy[k1 + ipntr * wy_dim1] * wy[k1 + jpntr * wy_dim1];
/*<            temp4 = temp4 + ws(k1,ipntr)*ws(k1,jpntr) >*/
                temp4 += ws[k1 + ipntr * ws_dim1] * ws[k1 + jpntr * ws_dim1];
/*<   36        continue >*/
/* L36: */
            }
/*<         wn1(iy,jy) = wn1(iy,jy) + temp1 - temp3  >*/
            wn1[iy + jy * wn1_dim1] = wn1[iy + jy * wn1_dim1] + temp1 - temp3;
/*<         wn1(is,js) = wn1(is,js) - temp2 + temp4  >*/
            wn1[is + js * wn1_dim1] = wn1[is + js * wn1_dim1] - temp2 + temp4;
/*<         jpntr = mod(jpntr,m) + 1 >*/
            jpntr = jpntr % *m + 1;
/*<   40     continue >*/
/* L40: */
        }
/*<          ipntr = mod(ipntr,m) + 1 >*/
        ipntr = ipntr % *m + 1;
/*<   45  continue >*/
/* L45: */
    }
/*       modify the old parts in block (2,1). */
/*<       ipntr = head       >*/
    ipntr = *head;
/*<       do 60 is = m + 1, m + upcl >*/
    i__1 = *m + upcl;
    for (is = *m + 1; is <= i__1; ++is) {
/*<          jpntr = head  >*/
        jpntr = *head;
/*<          do 55 jy = 1, upcl >*/
        i__2 = upcl;
        for (jy = 1; jy <= i__2; ++jy) {
/*<             temp1 = zero >*/
            temp1 = 0.;
/*<         temp3 = zero >*/
            temp3 = 0.;
/*<         do 50 k = 1, nenter >*/
            i__3 = *nenter;
            for (k = 1; k <= i__3; ++k) {
/*<            k1 = indx2(k) >*/
                k1 = indx2[k];
/*<            temp1 = temp1 + ws(k1,ipntr)*wy(k1,jpntr) >*/
                temp1 += ws[k1 + ipntr * ws_dim1] * wy[k1 + jpntr * wy_dim1];
/*<   50            continue >*/
/* L50: */
            }
/*<         do 51 k = ileave, n >*/
            i__3 = *n;
            for (k = *ileave; k <= i__3; ++k) {
/*<            k1 = indx2(k) >*/
                k1 = indx2[k];
/*<            temp3 = temp3 + ws(k1,ipntr)*wy(k1,jpntr) >*/
                temp3 += ws[k1 + ipntr * ws_dim1] * wy[k1 + jpntr * wy_dim1];
/*<   51            continue >*/
/* L51: */
            }
/*<          if (is .le. jy + m) then >*/
            if (is <= jy + *m) {
/*<            wn1(is,jy) = wn1(is,jy) + temp1 - temp3   >*/
                wn1[is + jy * wn1_dim1] = wn1[is + jy * wn1_dim1] + temp1 - 
                        temp3;
/*<         else >*/
            } else {
/*<            wn1(is,jy) = wn1(is,jy) - temp1 + temp3   >*/
                wn1[is + jy * wn1_dim1] = wn1[is + jy * wn1_dim1] - temp1 + 
                        temp3;
/*<         endif >*/
            }
/*<         jpntr = mod(jpntr,m) + 1 >*/
            jpntr = jpntr % *m + 1;
/*<   55     continue >*/
/* L55: */
        }
/*<          ipntr = mod(ipntr,m) + 1 >*/
        ipntr = ipntr % *m + 1;
/*<   60  continue >*/
/* L60: */
    }
/*     Form the upper triangle of WN = [D+Y' ZZ'Y/theta   -L_a'+R_z' ] */
/*                                     [-L_a +R_z        S'AA'S*theta] */
/*<       m2 = 2*m >*/
    m2 = *m << 1;
/*<       do 70 iy = 1, col >*/
    i__1 = *col;
    for (iy = 1; iy <= i__1; ++iy) {
/*<      is = col + iy >*/
        is = *col + iy;
/*<      is1 = m + iy >*/
        is1 = *m + iy;
/*<              do 65 jy = 1, iy >*/
        i__2 = iy;
        for (jy = 1; jy <= i__2; ++jy) {
/*<         js = col + jy >*/
            js = *col + jy;
/*<             js1 = m + jy >*/
            js1 = *m + jy;
/*<         wn(jy,iy) = wn1(iy,jy)/theta >*/
            wn[jy + iy * wn_dim1] = wn1[iy + jy * wn1_dim1] / *theta;
/*<         wn(js,is) = wn1(is1,js1)*theta >*/
            wn[js + is * wn_dim1] = wn1[is1 + js1 * wn1_dim1] * *theta;
/*<   65     continue >*/
/* L65: */
        }
/*<              do 66 jy = 1, iy - 1 >*/
        i__2 = iy - 1;
        for (jy = 1; jy <= i__2; ++jy) {
/*<         wn(jy,is) = -wn1(is1,jy) >*/
            wn[jy + is * wn_dim1] = -wn1[is1 + jy * wn1_dim1];
/*<   66     continue >*/
/* L66: */
        }
/*<              do 67 jy = iy, col >*/
        i__2 = *col;
        for (jy = iy; jy <= i__2; ++jy) {
/*<         wn(jy,is) = wn1(is1,jy) >*/
            wn[jy + is * wn_dim1] = wn1[is1 + jy * wn1_dim1];
/*<   67     continue >*/
/* L67: */
        }
/*<      wn(iy,iy) = wn(iy,iy) + sy(iy,iy) >*/
        wn[iy + iy * wn_dim1] += sy[iy + iy * sy_dim1];
/*<   70  continue >*/
/* L70: */
    }
/*     Form the upper triangle of WN= [  LL'            L^-1(-L_a'+R_z')] */
/*                                    [(-L_a +R_z)L'^-1   S'AA'S*theta  ] */
/*        first Cholesky factor (1,1) block of wn to get LL' */
/*                          with L' stored in the upper triangle of wn. */
/*<       call dpofa(wn,m2,col,info) >*/
    dpofa_(&wn[wn_offset], &m2, col, info);
/*<       if (info .ne. 0) then >*/
    if (*info != 0) {
/*<      info = -1 >*/
        *info = -1;
/*<      return >*/
        return 0;
/*<       endif >*/
    }
/*        then form L^-1(-L_a'+R_z') in the (1,2) block. */
/*<       col2 = 2*col >*/
    col2 = *col << 1;
/*<       do 71 js = col+1 ,col2 >*/
    i__1 = col2;
    for (js = *col + 1; js <= i__1; ++js) {
/*<          call dtrsl(wn,m2,col,wn(1,js),11,info) >*/
        dtrsl_(&wn[wn_offset], &m2, col, &wn[js * wn_dim1 + 1], &c__11, info);
/*<   71  continue >*/
/* L71: */
    }
/*     Form S'AA'S*theta + (L^-1(-L_a'+R_z'))'L^-1(-L_a'+R_z') in the */
/*        upper triangle of (2,2) block of wn. */
/*<       do 72 is = col+1, col2 >*/
    i__1 = col2;
    for (is = *col + 1; is <= i__1; ++is) {
/*<          do 74 js = is, col2 >*/
        i__2 = col2;
        for (js = is; js <= i__2; ++js) {
/*<            wn(is,js) = wn(is,js) + ddot(col,wn(1,is),1,wn(1,js),1) >*/
            wn[is + js * wn_dim1] += ddot_(col, &wn[is * wn_dim1 + 1], &c__1, 
                    &wn[js * wn_dim1 + 1], &c__1);
/*<   74        continue >*/
/* L74: */
        }
/*<   72     continue >*/
/* L72: */
    }
/*     Cholesky factorization of (2,2) block of wn. */
/*<       call dpofa(wn(col+1,col+1),m2,col,info) >*/
    dpofa_(&wn[*col + 1 + (*col + 1) * wn_dim1], &m2, col, info);
/*<       if (info .ne. 0) then >*/
    if (*info != 0) {
/*<      info = -2 >*/
        *info = -2;
/*<      return >*/
        return 0;
/*<       endif >*/
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* formk_ */

/* ======================= The end of formk ============================== */
/*<       subroutine formt(m, wt, sy, ss, col, theta, info) >*/
/* Subroutine */ int formt_(integer *m, doublereal *wt, doublereal *sy, 
        doublereal *ss, integer *col, doublereal *theta, integer *info)
{
    /* System generated locals */
    integer wt_dim1, wt_offset, sy_dim1, sy_offset, ss_dim1, ss_offset, i__1, 
            i__2, i__3;

    /* Local variables */
    integer i__, j, k, k1;
    doublereal ddum;
    extern /* Subroutine */ int dpofa_(doublereal *, integer *, integer *, 
            integer *);

/*<       integer          m, col, info >*/
/*<       double precision theta, wt(m, m), sy(m, m), ss(m, m) >*/
/*     ************ */

/*     Subroutine formt */

/*       This subroutine forms the upper half of the pos. def. and symm. */
/*         T = theta*SS + L*D^(-1)*L', stores T in the upper triangle */
/*         of the array wt, and performs the Cholesky factorization of T */
/*         to produce J*J', with J' stored in the upper triangle of wt. */

/*     Subprograms called: */

/*       Linpack ... dpofa. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          i,j,k,k1 >*/
/*<       double precision ddum >*/
/*<       double precision zero >*/
/*<       parameter        (zero=0.0d0) >*/
/*     Form the upper half of  T = theta*SS + L*D^(-1)*L', */
/*        store T in the upper triangle of the array wt. */
/*<       do 52 j = 1, col >*/
    /* Parameter adjustments */
    ss_dim1 = *m;
    ss_offset = 1 + ss_dim1;
    ss -= ss_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    wt_dim1 = *m;
    wt_offset = 1 + wt_dim1;
    wt -= wt_offset;

    /* Function Body */
    i__1 = *col;
    for (j = 1; j <= i__1; ++j) {
/*<              wt(1,j) = theta*ss(1,j) >*/
        wt[j * wt_dim1 + 1] = *theta * ss[j * ss_dim1 + 1];
/*<   52  continue >*/
/* L52: */
    }
/*<       do 55 i = 2, col >*/
    i__1 = *col;
    for (i__ = 2; i__ <= i__1; ++i__) {
/*<      do 54 j = i, col >*/
        i__2 = *col;
        for (j = i__; j <= i__2; ++j) {
/*<             k1 = min(i,j) - 1 >*/
            k1 = min(i__,j) - 1;
/*<             ddum  = zero >*/
            ddum = 0.;
/*<             do 53 k = 1, k1 >*/
            i__3 = k1;
            for (k = 1; k <= i__3; ++k) {
/*<                ddum  = ddum + sy(i,k)*sy(j,k)/sy(k,k) >*/
                ddum += sy[i__ + k * sy_dim1] * sy[j + k * sy_dim1] / sy[k + 
                        k * sy_dim1];
/*<   53        continue >*/
/* L53: */
            }
/*<             wt(i,j) = ddum + theta*ss(i,j) >*/
            wt[i__ + j * wt_dim1] = ddum + *theta * ss[i__ + j * ss_dim1];
/*<   54     continue >*/
/* L54: */
        }
/*<   55  continue >*/
/* L55: */
    }
/*     Cholesky factorize T to J*J' with */
/*        J' stored in the upper triangle of wt. */
/*<       call dpofa(wt,m,col,info) >*/
    dpofa_(&wt[wt_offset], m, col, info);
/*<       if (info .ne. 0) then >*/
    if (*info != 0) {
/*<          info = -3 >*/
        *info = -3;
/*<       endif >*/
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* formt_ */

/* ======================= The end of formt ============================== */
/*<    >*/
/* Subroutine */ int freev_(integer *n, integer *nfree, integer *index, 
        integer *nenter, integer *ileave, integer *indx2, integer *iwhere, 
        logical *wrk, logical *updatd, logical *cnstnd, integer *iprint, 
        integer *iter)
{
    /* System generated locals */
    integer i__1;

    /* Local variables */
    integer i__, k, iact;

/*<    >*/
/*<       logical wrk, updatd, cnstnd >*/
/*     ************ */

/*     Subroutine freev */

/*     This subroutine counts the entering and leaving variables when */
/*       iter > 0, and finds the index set of free and active variables */
/*       at the GCP. */

/*     cnstnd is a logical variable indicating whether bounds are present */

/*     index is an integer array of dimension n */
/*       for i=1,...,nfree, index(i) are the indices of free variables */
/*       for i=nfree+1,...,n, index(i) are the indices of bound variables */
/*       On entry after the first iteration, index gives */
/*         the free variables at the previous iteration. */
/*       On exit it gives the free variables based on the determination */
/*         in cauchy using the array iwhere. */

/*     indx2 is an integer array of dimension n */
/*       On entry indx2 is unspecified. */
/*       On exit with iter>0, indx2 indicates which variables */
/*          have changed status since the previous iteration. */
/*       For i= 1,...,nenter, indx2(i) have changed from bound to free. */
/*       For i= ileave+1,...,n, indx2(i) have changed from free to bound. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer iact,i,k >*/
/*<       nenter = 0 >*/
    /* Parameter adjustments */
    --iwhere;
    --indx2;
    --index;

    /* Function Body */
    *nenter = 0;
/*<       ileave = n + 1 >*/
    *ileave = *n + 1;
/*<       if (iter .gt. 0 .and. cnstnd) then >*/
    if (*iter > 0 && *cnstnd) {
/*                           count the entering and leaving variables. */
/*<      do 20 i = 1, nfree >*/
        i__1 = *nfree;
        for (i__ = 1; i__ <= i__1; ++i__) {
/*<         k = index(i) >*/
            k = index[i__];
/*<         if (iwhere(k) .gt. 0) then >*/
            if (iwhere[k] > 0) {
/*<            ileave = ileave - 1 >*/
                --(*ileave);
/*<            indx2(ileave) = k >*/
                indx2[*ileave] = k;
/*<       >*/
                if (*iprint >= 100) {
                    printf("Variable %ld leaves the set of free variables\n", k);
                }
/*<             endif >*/
            }
/*<   20     continue >*/
/* L20: */
        }
/*<      do 22 i = 1 + nfree, n >*/
        i__1 = *n;
        for (i__ = *nfree + 1; i__ <= i__1; ++i__) {
/*<         k = index(i) >*/
            k = index[i__];
/*<         if (iwhere(k) .le. 0) then >*/
            if (iwhere[k] <= 0) {
/*<            nenter = nenter + 1 >*/
                ++(*nenter);
/*<            indx2(nenter) = k >*/
                indx2[*nenter] = k;
/*<       >*/
                if (*iprint >= 100) {
                    printf("Variable %ld enters the set of free variables\n", k);
                }
/*<             endif >*/
            }
/*<   22     continue >*/
/* L22: */
        }
/*<    >*/
        if (*iprint >= 99) {
            i__1 = *n + 1 - *ileave;
            printf("%ld variables leave; %ld variables enter\n", i__1, *nenter);
        }
/*<       endif >*/
    }
/*<       wrk = (ileave .lt. n+1) .or. (nenter .gt. 0) .or. updatd >*/
    *wrk = *ileave < *n + 1 || *nenter > 0 || *updatd;
/*     Find the index set of free and active variables at the GCP. */
/*<       nfree = 0  >*/
    *nfree = 0;
/*<       iact = n + 1 >*/
    iact = *n + 1;
/*<       do 24 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      if (iwhere(i) .le. 0) then >*/
        if (iwhere[i__] <= 0) {
/*<         nfree = nfree + 1 >*/
            ++(*nfree);
/*<         index(nfree) = i >*/
            index[*nfree] = i__;
/*<          else >*/
        } else {
/*<             iact = iact - 1 >*/
            --iact;
/*<             index(iact) = i >*/
            index[iact] = i__;
/*<          endif >*/
        }
/*<   24  continue >*/
/* L24: */
    }
/*<    >*/
    if (*iprint >= 99) {
        i__1 = *iter + 1;
        printf("%ld variables are free at GCP %ld\n", *nfree, i__1);
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* freev_ */

/* ======================= The end of freev ============================== */
/*<       subroutine hpsolb(n, t, iorder, iheap) >*/
/* Subroutine */ int hpsolb_(integer *n, doublereal *t, integer *iorder, 
        integer *iheap)
{
    /* System generated locals */
    integer i__1;

    /* Local variables */
    integer i__, j, k;
    doublereal out, ddum;
    integer indxin, indxou;

/*<       integer          iheap, n, iorder(n) >*/
/*<       double precision t(n) >*/
/*     ************ */

/*     Subroutine hpsolb */

/*     This subroutine sorts out the least element of t, and puts the */
/*       remaining elements of t in a heap. */

/*     n is an integer variable. */
/*       On entry n is the dimension of the arrays t and iorder. */
/*       On exit n is unchanged. */

/*     t is a double precision array of dimension n. */
/*       On entry t stores the elements to be sorted, */
/*       On exit t(n) stores the least elements of t, and t(1) to t(n-1) */
/*         stores the remaining elements in the form of a heap. */

/*     iorder is an integer array of dimension n. */
/*       On entry iorder(i) is the index of t(i). */
/*       On exit iorder(i) is still the index of t(i), but iorder may be */
/*         permuted in accordance with t. */

/*     iheap is an integer variable specifying the task. */
/*       On entry iheap should be set as follows: */
/*         iheap .eq. 0 if t(1) to t(n) is not in the form of a heap, */
/*         iheap .ne. 0 if otherwise. */
/*       On exit iheap is unchanged. */


/*     References: */
/*       Algorithm 232 of CACM (J. W. J. Williams): HEAPSORT. */

/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */

/*     ************ */
/*<       integer          i,j,k,indxin,indxou >*/
/*<       double precision ddum,out >*/
/*<       if (iheap .eq. 0) then >*/
    /* Parameter adjustments */
    --iorder;
    --t;

    /* Function Body */
    if (*iheap == 0) {
/*        Rearrange the elements t(1) to t(n) to form a heap. */
/*<          do 20 k = 2, n >*/
        i__1 = *n;
        for (k = 2; k <= i__1; ++k) {
/*<             ddum  = t(k) >*/
            ddum = t[k];
/*<             indxin = iorder(k) >*/
            indxin = iorder[k];
/*           Add ddum to the heap. */
/*<             i = k >*/
            i__ = k;
/*<    10       continue >*/
L10:
/*<             if (i.gt.1) then >*/
            if (i__ > 1) {
/*<                j = i/2 >*/
                j = i__ / 2;
/*<                if (ddum .lt. t(j)) then >*/
                if (ddum < t[j]) {
/*<                   t(i) = t(j) >*/
                    t[i__] = t[j];
/*<                   iorder(i) = iorder(j) >*/
                    iorder[i__] = iorder[j];
/*<                   i = j >*/
                    i__ = j;
/*<                   goto 10  >*/
                    goto L10;
/*<                endif   >*/
                }
/*<             endif   >*/
            }
/*<             t(i) = ddum >*/
            t[i__] = ddum;
/*<             iorder(i) = indxin >*/
            iorder[i__] = indxin;
/*<    20    continue >*/
/* L20: */
        }
/*<       endif >*/
    }
/*     Assign to 'out' the value of t(1), the least member of the heap, */
/*        and rearrange the remaining members to form a heap as */
/*        elements 1 to n-1 of t. */
/*<       if (n .gt. 1) then >*/
    if (*n > 1) {
/*<          i = 1 >*/
        i__ = 1;
/*<          out = t(1) >*/
        out = t[1];
/*<          indxou = iorder(1) >*/
        indxou = iorder[1];
/*<          ddum  = t(n) >*/
        ddum = t[*n];
/*<          indxin  = iorder(n) >*/
        indxin = iorder[*n];
/*        Restore the heap */
/*<    30    continue >*/
L30:
/*<          j = i+i >*/
        j = i__ + i__;
/*<          if (j .le. n-1) then >*/
        if (j <= *n - 1) {
/*<             if (t(j+1) .lt. t(j)) j = j+1 >*/
            if (t[j + 1] < t[j]) {
                ++j;
            }
/*<             if (t(j) .lt. ddum ) then >*/
            if (t[j] < ddum) {
/*<                t(i) = t(j) >*/
                t[i__] = t[j];
/*<                iorder(i) = iorder(j) >*/
                iorder[i__] = iorder[j];
/*<                i = j >*/
                i__ = j;
/*<                goto 30 >*/
                goto L30;
/*<             endif  >*/
            }
/*<          endif  >*/
        }
/*<          t(i) = ddum >*/
        t[i__] = ddum;
/*<          iorder(i) = indxin >*/
        iorder[i__] = indxin;
/*     Put the least member in t(n). */
/*<          t(n) = out >*/
        t[*n] = out;
/*<          iorder(n) = indxou >*/
        iorder[*n] = indxou;
/*<       endif  >*/
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* hpsolb_ */

/* ====================== The end of hpsolb ============================== */
/*<    >*/
/* Subroutine */ int lnsrlb_(integer *n, doublereal *l, doublereal *u, 
        integer *nbd, doublereal *x, doublereal *f, doublereal *fold, 
        doublereal *gd, doublereal *gdold, doublereal *g, doublereal *d__, 
        doublereal *r__, doublereal *t, doublereal *z__, doublereal *stp, 
        doublereal *dnorm, doublereal *dtd, doublereal *xstep, doublereal *
        stpmx, integer *iter, integer *ifun, integer *iback, integer *nfgv, 
        integer *info, char *task, logical *boxed, logical *cnstnd, char *
        csave, integer *isave, doublereal *dsave, ftnlen task_len, ftnlen 
        csave_len)
{
    /* System generated locals */
    integer i__1;
    doublereal d__1;

    /* Builtin functions */
    integer s_cmp(char *, char *, ftnlen, ftnlen);
    double sqrt(doublereal);
    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    integer i__;
    doublereal a1, a2;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
            doublereal *, integer *), dcsrch_(doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, char *, integer *, doublereal *, 
            ftnlen);
    (void)task_len;
    (void)csave_len;

/*<       character*60     task, csave >*/
/*<       logical          boxed, cnstnd >*/
/*<    >*/
/*<    >*/
/*     ********** */

/*     Subroutine lnsrlb */

/*     This subroutine calls subroutine dcsrch from the Minpack2 library */
/*       to perform the line search.  Subroutine dscrch is safeguarded so */
/*       that all trial points lie within the feasible region. */

/*     Subprograms called: */

/*       Minpack2 Library ... dcsrch. */

/*       Linpack ... dtrsl, ddot. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ********** */
/*<       integer          i >*/
/*<       double           precision ddot,a1,a2 >*/
/*<       double precision one,zero,big >*/
/*<       parameter        (one=1.0d0,zero=0.0d0,big=1.0d+10) >*/
/*<       double precision ftol,gtol,xtol >*/
/*<       parameter        (ftol=1.0d-3,gtol=0.9d0,xtol=0.1d0) >*/
/*<       if (task(1:5) .eq. 'FG_LN') goto 556 >*/
    /* Parameter adjustments */
    --z__;
    --t;
    --r__;
    --d__;
    --g;
    --x;
    --nbd;
    --u;
    --l;
    --isave;
    --dsave;

    /* Function Body */
    if (s_cmp(task, "FG_LN", (ftnlen)5, (ftnlen)5) == 0) {
        goto L556;
    }
/*<       dtd = ddot(n,d,1,d,1) >*/
    *dtd = ddot_(n, &d__[1], &c__1, &d__[1], &c__1);
/*<       dnorm = sqrt(dtd) >*/
    *dnorm = sqrt(*dtd);
/*     Determine the maximum step length. */
/*<       stpmx = big >*/
    *stpmx = 1e10;
/*<       if (cnstnd) then >*/
    if (*cnstnd) {
/*<          if (iter .eq. 0) then >*/
        if (*iter == 0) {
/*<             stpmx = one >*/
            *stpmx = 1.;
/*<          else >*/
        } else {
/*<             do 43 i = 1, n >*/
            i__1 = *n;
            for (i__ = 1; i__ <= i__1; ++i__) {
/*<                a1 = d(i) >*/
                a1 = d__[i__];
/*<                if (nbd(i) .ne. 0) then >*/
                if (nbd[i__] != 0) {
/*<                   if (a1 .lt. zero .and. nbd(i) .le. 2) then >*/
                    if (a1 < 0. && nbd[i__] <= 2) {
/*<                      a2 = l(i) - x(i) >*/
                        a2 = l[i__] - x[i__];
/*<                      if (a2 .ge. zero) then >*/
                        if (a2 >= 0.) {
/*<                         stpmx = zero >*/
                            *stpmx = 0.;
/*<                      else if (a1*stpmx .lt. a2) then >*/
                        } else if (a1 * *stpmx < a2) {
/*<                         stpmx = a2/a1 >*/
                            *stpmx = a2 / a1;
/*<                      endif >*/
                        }
/*<                   else if (a1 .gt. zero .and. nbd(i) .ge. 2) then >*/
                    } else if (a1 > 0. && nbd[i__] >= 2) {
/*<                      a2 = u(i) - x(i) >*/
                        a2 = u[i__] - x[i__];
/*<                      if (a2 .le. zero) then >*/
                        if (a2 <= 0.) {
/*<                         stpmx = zero >*/
                            *stpmx = 0.;
/*<                      else if (a1*stpmx .gt. a2) then >*/
                        } else if (a1 * *stpmx > a2) {
/*<                         stpmx = a2/a1 >*/
                            *stpmx = a2 / a1;
/*<                      endif >*/
                        }
/*<                   endif >*/
                    }
/*<                endif >*/
                }
/*<   43        continue >*/
/* L43: */
            }
/*<          endif >*/
        }
/*<       endif >*/
    }
/*<       if (iter .eq. 0 .and. .not. boxed) then >*/
    if (*iter == 0 && ! (*boxed)) {
/*<          stp = min(one/dnorm, stpmx) >*/
/* Computing MIN */
        d__1 = 1. / *dnorm;
        *stp = min(d__1,*stpmx);
/*<       else >*/
    } else {
/*<      stp = one >*/
        *stp = 1.;
/*<       endif  >*/
    }
/*<       call dcopy(n,x,1,t,1) >*/
    dcopy_(n, &x[1], &c__1, &t[1], &c__1);
/*<       call dcopy(n,g,1,r,1) >*/
    dcopy_(n, &g[1], &c__1, &r__[1], &c__1);
/*<       fold = f >*/
    *fold = *f;
/*<       ifun = 0 >*/
    *ifun = 0;
/*<       iback = 0 >*/
    *iback = 0;
/*<       csave = 'START' >*/
    s_copy(csave, "START", (ftnlen)60, (ftnlen)5);
/*<  556  continue >*/
L556:
/*<       gd = ddot(n,g,1,d,1) >*/
    *gd = ddot_(n, &g[1], &c__1, &d__[1], &c__1);
/*<       if (ifun .eq. 0) then >*/
    if (*ifun == 0) {
/*<      gdold=gd >*/
        *gdold = *gd;
/*<          if (gd .ge. zero) then >*/
        if (*gd >= 0.) {
/*                               the directional derivative >=0. */
/*                               Line search is impossible. */
/*<             info = -4 >*/
            *info = -4;
/*<             return >*/
            return 0;
/*<          endif >*/
        }
/*<       endif >*/
    }
/*<       call dcsrch(f,gd,stp,ftol,gtol,xtol,zero,stpmx,csave,isave,dsave) >*/
    dcsrch_(f, gd, stp, &c_b275, &c_b276, &c_b277, &c_b9, stpmx, csave, &
            isave[1], &dsave[1], (ftnlen)60);
/*<       xstep = stp*dnorm >*/
    *xstep = *stp * *dnorm;
/*<       if (csave(1:4) .ne. 'CONV' .and. csave(1:4) .ne. 'WARN') then >*/
    if (s_cmp(csave, "CONV", (ftnlen)4, (ftnlen)4) != 0 && s_cmp(csave, "WARN"
            , (ftnlen)4, (ftnlen)4) != 0) {
/*<      task = 'FG_LNSRCH' >*/
        s_copy(task, "FG_LNSRCH", (ftnlen)60, (ftnlen)9);
/*<      ifun = ifun + 1 >*/
        ++(*ifun);
/*<          nfgv = nfgv + 1 >*/
        ++(*nfgv);
/*<          iback = ifun - 1  >*/
        *iback = *ifun - 1;
/*<          if (stp .eq. one) then >*/
        if (*stp == 1.) {
/*<             call dcopy(n,z,1,x,1) >*/
            dcopy_(n, &z__[1], &c__1, &x[1], &c__1);
/*<          else >*/
        } else {
/*<             do 41 i = 1, n >*/
            i__1 = *n;
            for (i__ = 1; i__ <= i__1; ++i__) {
/*<            x(i) = stp*d(i) + t(i) >*/
                x[i__] = *stp * d__[i__] + t[i__];
/*<   41        continue >*/
/* L41: */
            }
/*<          endif >*/
        }
/*<       else >*/
    } else {
/*<          task = 'NEW_X' >*/
        s_copy(task, "NEW_X", (ftnlen)60, (ftnlen)5);
/*<       endif >*/
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* lnsrlb_ */

/* ======================= The end of lnsrlb ============================= */
/*<    >*/
/* Subroutine */ int matupd_(integer *n, integer *m, doublereal *ws, 
        doublereal *wy, doublereal *sy, doublereal *ss, doublereal *d__, 
        doublereal *r__, integer *itail, integer *iupdat, integer *col, 
        integer *head, doublereal *theta, doublereal *rr, doublereal *dr, 
        doublereal *stp, doublereal *dtd)
{
    /* System generated locals */
    integer ws_dim1, ws_offset, wy_dim1, wy_offset, sy_dim1, sy_offset, 
            ss_dim1, ss_offset, i__1, i__2;

    /* Local variables */
    integer j;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
            doublereal *, integer *);
    integer pointr;

/*<       integer          n, m, itail, iupdat, col, head >*/
/*<    >*/
/*     ************ */

/*     Subroutine matupd */

/*       This subroutine updates matrices WS and WY, and forms the */
/*         middle matrix in B. */

/*     Subprograms called: */

/*       Linpack ... dcopy, ddot. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          j,pointr >*/
/*<       double precision ddot >*/
/*<       double precision one >*/
/*<       parameter        (one=1.0d0) >*/
/*     Set pointers for matrices WS and WY. */
/*<       if (iupdat .le. m) then >*/
    /* Parameter adjustments */
    --r__;
    --d__;
    ss_dim1 = *m;
    ss_offset = 1 + ss_dim1;
    ss -= ss_offset;
    sy_dim1 = *m;
    sy_offset = 1 + sy_dim1;
    sy -= sy_offset;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;

    /* Function Body */
    if (*iupdat <= *m) {
/*<      col = iupdat >*/
        *col = *iupdat;
/*<      itail = mod(head+iupdat-2,m) + 1 >*/
        *itail = (*head + *iupdat - 2) % *m + 1;
/*<       else >*/
    } else {
/*<      itail = mod(itail,m) + 1 >*/
        *itail = *itail % *m + 1;
/*<      head = mod(head,m) + 1 >*/
        *head = *head % *m + 1;
/*<       endif >*/
    }
/*     Update matrices WS and WY. */
/*<       call dcopy(n,d,1,ws(1,itail),1) >*/
    dcopy_(n, &d__[1], &c__1, &ws[*itail * ws_dim1 + 1], &c__1);
/*<       call dcopy(n,r,1,wy(1,itail),1) >*/
    dcopy_(n, &r__[1], &c__1, &wy[*itail * wy_dim1 + 1], &c__1);
/*     Set theta=yy/ys. */
/*<       theta = rr/dr >*/
    *theta = *rr / *dr;
/*     Form the middle matrix in B. */
/*        update the upper triangle of SS, */
/*                                         and the lower triangle of SY: */
/*<       if (iupdat .gt. m) then >*/
    if (*iupdat > *m) {
/*                              move old information */
/*<          do 50 j = 1, col - 1 >*/
        i__1 = *col - 1;
        for (j = 1; j <= i__1; ++j) {
/*<             call dcopy(j,ss(2,j+1),1,ss(1,j),1) >*/
            dcopy_(&j, &ss[(j + 1) * ss_dim1 + 2], &c__1, &ss[j * ss_dim1 + 1]
                    , &c__1);
/*<             call dcopy(col-j,sy(j+1,j+1),1,sy(j,j),1) >*/
            i__2 = *col - j;
            dcopy_(&i__2, &sy[j + 1 + (j + 1) * sy_dim1], &c__1, &sy[j + j * 
                    sy_dim1], &c__1);
/*<   50     continue >*/
/* L50: */
        }
/*<       endif >*/
    }
/*        add new information: the last row of SY */
/*                                             and the last column of SS: */
/*<       pointr = head >*/
    pointr = *head;
/*<       do 51 j = 1, col - 1 >*/
    i__1 = *col - 1;
    for (j = 1; j <= i__1; ++j) {
/*<      sy(col,j) = ddot(n,d,1,wy(1,pointr),1) >*/
        sy[*col + j * sy_dim1] = ddot_(n, &d__[1], &c__1, &wy[pointr * 
                wy_dim1 + 1], &c__1);
/*<      ss(j,col) = ddot(n,ws(1,pointr),1,d,1) >*/
        ss[j + *col * ss_dim1] = ddot_(n, &ws[pointr * ws_dim1 + 1], &c__1, &
                d__[1], &c__1);
/*<          pointr = mod(pointr,m) + 1 >*/
        pointr = pointr % *m + 1;
/*<   51  continue >*/
/* L51: */
    }
/*<       if (stp .eq. one) then >*/
    if (*stp == 1.) {
/*<          ss(col,col) = dtd >*/
        ss[*col + *col * ss_dim1] = *dtd;
/*<       else >*/
    } else {
/*<          ss(col,col) = stp*stp*dtd >*/
        ss[*col + *col * ss_dim1] = *stp * *stp * *dtd;
/*<       endif >*/
    }
/*<       sy(col,col) = dr >*/
    sy[*col + *col * sy_dim1] = *dr;
/*<       return >*/
    return 0;
/*<       end >*/
} /* matupd_ */

/* ======================= The end of matupd ============================= */
/*<       subroutine prn1lb(n, m, l, u, x, iprint, itfile, epsmch) >*/
/* Subroutine */ int prn1lb_(integer *n, integer *m, doublereal *l, 
        doublereal *u, doublereal *x, integer *iprint, integer *itfile, 
        doublereal *epsmch)
{
  (void)itfile;
  (void)m;
  (void)epsmch;
/*<       integer n, m, iprint, itfile >*/
/*<       double precision epsmch, x(n), l(n), u(n) >*/
/*     ************ */

/*     Subroutine prn1lb */

/*     This subroutine prints the input data, initial point, upper and */
/*       lower bounds of each variable, machine precision, as well as */
/*       the headings of the output. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer i >*/
/*<       if (iprint .ge. 0) then >*/
    /* Parameter adjustments */
    --x;
    --u;
    --l;

    /* Function Body */
    if (*iprint >= 0) {
/*
 7001 format ('RUNNING THE L-BFGS-B CODE',/,/,
     + '           * * *',/,/,
     + 'Machine precision =',1p,d10.3)
*/
/*<          write (6,7001) epsmch >*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
        fprintf(0,
                "RUNNING THE L-BFGS-B CODE\n"
                "\n"
                "           * * *\n"
                "\n"
                "Machine precision = %10.3g\n", *epsmch);
#endif
/*<          write (6,*) 'N = ',n,'    M = ',m >*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
        fprintf(0, "N = %ld    M = %ld\n", *n, *m);
#endif
/*<          if (iprint .ge. 1) then >*/
        if (*iprint >= 1) {
/*<             write (itfile,2001) epsmch >*/
/*
 2001 format ('RUNNING THE L-BFGS-B CODE',/,/,
     + 'it    = iteration number',/,
     + 'nf    = number of function evaluations',/,
     + 'nint  = number of segments explored during the Cauchy search',/,
     + 'nact  = number of active bounds at the generalized Cauchy point'
     + ,/,
     + 'sub   = manner in which the subspace minimization terminated:'
     + ,/,'        con = converged, bnd = a bound was reached',/,
     + 'itls  = number of iterations performed in the line search',/,
     + 'stepl = step length used',/,
     + 'tstep = norm of the displacement (total step)',/,
     + 'projg = norm of the projected gradient',/,
     + 'f     = function value',/,/,
     + '           * * *',/,/,
     + 'Machine precision =',1p,d10.3)
*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
            fprintf(0,
                    "RUNNING THE L-BFGS-B CODE\n"
                    "\n"
                    "it    = iteration number\n"
                    "nf    = number of function evaluations\n"
                    "nint  = number of segments explored during the Cauchy search\n"
                    "nact  = number of active bounds at the generalized Cauchy point\n"
                    "sub   = manner in which the subspace minimization terminated:\n"
                    "        con = converged, bnd = a bound was reached\n"
                    "itls  = number of iterations performed in the line search\n"
                    "stepl = step length used\n"
                    "tstep = norm of the displacement (total step)\n"
                    "projg = norm of the projected gradient\n"
                    "f     = function value\n"
                    "\n"
                    "           * * *\n"
                    "\n"
                    "Machine precision = %10.3g\n", *epsmch);
#endif
/*<             write (itfile,*)'N = ',n,'    M = ',m >*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
            fprintf(0, "N = %ld    M = %ld\n", *n, *m);
#endif
/*<         write (itfile,9001) >*/
/*
 9001 format (/,3x,'it',3x,'nf',2x,'nint',2x,'nact',2x,'sub',2x,'itls',
     +        2x,'stepl',4x,'tstep',5x,'projg',8x,'f')
*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
            fprintf(0, "   it   nf  nint  nact  sub  itls  stepl    tstep     projg        f\n");
#endif
/*<             if (iprint .gt. 100) then >*/
            if (*iprint > 100) {
/*<                write (6,1004) 'L =',(l(i),i = 1,n) >*/
                lbfgsb_printf_vec("L", l, *n);
/*<                write (6,1004) 'X0 =',(x(i),i = 1,n) >*/
                lbfgsb_printf_vec("X0", x, *n);
/*<                write (6,1004) 'U =',(u(i),i = 1,n) >*/
                lbfgsb_printf_vec("U", u, *n);
/*<             endif  >*/
            }
/*<          endif >*/
        }
/*<       endif  >*/
    }
/*<  1004 format (/,a4, 1p, 6(1x,d11.4),/,(4x,1p,6(1x,d11.4))) >*/
/*<  2 >*/
/*<  7 >*/
/*<  9 >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* prn1lb_ */

/* ======================= The end of prn1lb ============================= */
/*<    >*/
/* Subroutine */ int prn2lb_(integer *n, doublereal *x, doublereal *f, 
        doublereal *g, integer *iprint, integer *itfile, integer *iter, 
        integer *nfgv, integer *nact, doublereal *sbgnrm, integer *nint, char 
        *word, integer *iword, integer *iback, doublereal *stp, doublereal *
        xstep, ftnlen word_len)
{
    /* Builtin functions */
    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    integer imod;

    (void)itfile;
    (void)word_len;
    (void)nfgv;
    (void)nact;
    (void)nint;
    (void)stp;

/*<       character*3      word >*/
/*<    >*/
/*<       double precision f, sbgnrm, stp, xstep, x(n), g(n) >*/
/*     ************ */

/*     Subroutine prn2lb */

/*     This subroutine prints out new information after a successful */
/*       line search. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer i,imod >*/
/*           'word' records the status of subspace solutions. */
/*<       if (iword .eq. 0) then >*/
    /* Parameter adjustments */
    --g;
    --x;

    /* Function Body */
    if (*iword == 0) {
/*                            the subspace minimization converged. */
/*<      word = 'con' >*/
        s_copy(word, "con", (ftnlen)3, (ftnlen)3);
/*<       else if (iword .eq. 1) then >*/
    } else if (*iword == 1) {
/*                          the subspace minimization stopped at a bound. */
/*<          word = 'bnd' >*/
        s_copy(word, "bnd", (ftnlen)3, (ftnlen)3);
/*<       else if (iword .eq. 5) then >*/
    } else if (*iword == 5) {
/*                             the truncated Newton step has been used. */
/*<      word = 'TNT' >*/
        s_copy(word, "TNT", (ftnlen)3, (ftnlen)3);
/*<       else >*/
    } else {
/*<          word = '---' >*/
        s_copy(word, "---", (ftnlen)3, (ftnlen)3);
/*<       endif >*/
    }
/*<       if (iprint .ge. 99) then >*/
    if (*iprint >= 99) {
/*<          write (6,*) 'LINE SEARCH',iback,' times; norm of step = ',xstep >*/
        printf("LINE SEARCH %ld times; norm of step = %g\n", *iback, *xstep);
/*<          write (6,2001) iter,f,sbgnrm >*/
/*
 2001 format
     +  (/,'At iterate',i5,4x,'f= ',1p,d12.5,4x,'|proj g|= ',1p,d12.5)
*/
        printf("At iterate %5ld    f= %12.5g    |proj g|= %12.5g\n",
               *iter, *f, *sbgnrm);
/*<          if (iprint .gt. 100) then   >*/
        if (*iprint > 100) {
/*<             write (6,1004) 'X =',(x(i), i = 1, n) >*/
            lbfgsb_printf_vec("X", x, *n);
/*<             write (6,1004) 'G =',(g(i), i = 1, n) >*/
            lbfgsb_printf_vec("G", g, *n);
/*<          endif >*/
        }
/*<       else if (iprint .gt. 0) then  >*/
    } else if (*iprint > 0) {
/*<          imod = mod(iter,iprint) >*/
        imod = *iter % *iprint;
/*<          if (imod .eq. 0) write (6,2001) iter,f,sbgnrm >*/
        if (imod == 0) {
/*
  2001 format
  +  (/,'At iterate',i5,4x,'f= ',1p,d12.5,4x,'|proj g|= ',1p,d12.5)
*/
            printf("At iterate %5ld    f= %12.5g    |proj g|= %12.5g\n",
                   *iter, *f, *sbgnrm);
        }
/*<       endif >*/
    }
/*<    >*/
    if (*iprint >= 1) {
/*
<  3001 format(2(1x,i4),2(1x,i5),2x,a3,1x,i4,1p,2(2x,d7.1),1p,2(1x,d10.3)) >
*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
        fprintf(0, " %4ld %4ld %5ld %5ld  %3s %4ld  %7.1g %7.1g<1p> %10.3g %10.3g\n",
                *iter, *nfgv, *nint, *nact, word, *iback, *stp, *xstep,
                *sbgnrm, *f);
#endif
    }
/*<  1004 format (/,a4, 1p, 6(1x,d11.4),/,(4x,1p,6(1x,d11.4))) >*/
/*<  2 >*/
/*<  3001 format(2(1x,i4),2(1x,i5),2x,a3,1x,i4,1p,2(2x,d7.1),1p,2(1x,d10.3)) >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* prn2lb_ */

/* ======================= The end of prn2lb ============================= */
/*<    >*/
/* Subroutine */ int prn3lb_(integer *n, doublereal *x, doublereal *f, char *
        task, integer *iprint, integer *info, integer *itfile, integer *iter, 
        integer *nfgv, integer *nintol, integer *nskip, integer *nact, 
        doublereal *sbgnrm, doublereal *time, integer *nint, char *word, 
        integer *iback, doublereal *stp, doublereal *xstep, integer *k, 
        doublereal *cachyt, doublereal *sbtime, doublereal *lnscht, ftnlen 
        task_len, ftnlen word_len)
{
    /* Builtin functions */
    integer s_cmp(char *, char *, ftnlen, ftnlen);

    (void)itfile;
    (void)task_len;
    (void)word_len;
    (void)nint;
    (void)word;
    (void)iback;
    (void)stp;
    (void)xstep;

/*<       character*60     task >*/
/*<       character*3      word >*/
/*<    >*/
/*<    >*/
/*     ************ */

/*     Subroutine prn3lb */

/*     This subroutine prints out information when either a built-in */
/*       convergence test is satisfied or when an error message is */
/*       generated. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer i >*/
/*<       if (task(1:5) .eq. 'ERROR') goto 999 >*/
    /* Parameter adjustments */
    --x;

    /* Function Body */
    if (s_cmp(task, "ERROR", (ftnlen)5, (ftnlen)5) == 0) {
        goto L999;
    }
/*<       if (iprint .ge. 0) then >*/
    if (*iprint >= 0) {
/*<          write (6,3003) >*/
/*
 3003 format (/,
     + '           * * *',/,/,
     + 'Tit   = total number of iterations',/,
     + 'Tnf   = total number of function evaluations',/,
     + 'Tnint = total number of segments explored during',
     +           ' Cauchy searches',/,
     + 'Skip  = number of BFGS updates skipped',/,
     + 'Nact  = number of active bounds at final generalized',
     +          ' Cauchy point',/,
     + 'Projg = norm of the final projected gradient',/,
     + 'F     = final function value',/,/,
     + '           * * *')
*/
        printf(
          "           * * *\n"
          "\n"
          "Tit   = total number of iterations\n"
          "Tnf   = total number of function evaluations\n"
          "Tnint = total number of segments explored during Cauchy searches\n"
          "Skip  = number of BFGS updates skipped\n"
          "Nact  = number of active bounds at final generalized Cauchy point\n"
          "Projg = norm of the final projected gradient\n"
          "F     = final function value\n"
          "\n"
          "           * * *\n");
/*<          write (6,3004) >*/
/*
 3004 format (/,3x,'N',3x,'Tit',2x,'Tnf',2x,'Tnint',2x,
     +       'Skip',2x,'Nact',5x,'Projg',8x,'F')
*/
        printf("   N   Tit  Tnf  Tnint  Skip  Nact     Projg        F\n");
/*<          write(6,3005) n,iter,nfgv,nintol,nskip,nact,sbgnrm,f >*/
/*
 3005 format (i5,2(1x,i4),(1x,i6),(2x,i4),(1x,i5),1p,2(2x,d10.3))
*/
        printf(" %4ld %4ld %4ld %6ld %4ld %5ld  %10.3g  %10.3g\n",
               *n, *iter, *nfgv, *nintol, *nskip, *nact, *sbgnrm, *f);
/*<          if (iprint .ge. 100) then >*/
        if (*iprint >= 100) {
/*<             write (6,1004) 'X =',(x(i),i = 1,n) >*/
            lbfgsb_printf_vec("X", x, *n);
/*<          endif   >*/
        }
/*<          if (iprint .ge. 1) write (6,*) ' F =',f >*/
        if (*iprint >= 1) {
            printf("F = %g\n", *f);
        }
/*<       endif  >*/
    }
/*<  999  continue >*/
L999:
/*<       if (iprint .ge. 0) then >*/
    if (*iprint >= 0) {
/*<          write (6,3009) task >*/
        printf("%60s\n", task);
/*<          if (info .ne. 0) then >*/
        if (*info != 0) {
/*<             if (info .eq. -1) write (6,9011) >*/
            if (*info == -1) {
/*
 9011 format (/,
     +' Matrix in 1st Cholesky factorization in formk is not Pos. Def.')
*/
                printf(" Matrix in 1st Cholesky factorization in formk is not Pos. Def.\n");
            }
/*<             if (info .eq. -2) write (6,9012) >*/
            if (*info == -2) {
/*
 9012 format (/,
     +' Matrix in 2st Cholesky factorization in formk is not Pos. Def.')
*/
                printf(" Matrix in 2st Cholesky factorization in formk is not Pos. Def.\n");
            }
/*<             if (info .eq. -3) write (6,9013) >*/
            if (*info == -3) {
/*
 9013 format (/,
     +' Matrix in the Cholesky factorization in formt is not Pos. Def.')
*/
                printf(" Matrix in the Cholesky factorization in formk is not Pos. Def.\n");
            }
/*<             if (info .eq. -4) write (6,9014) >*/
            if (*info == -4) {
/*
 9014 format (/,
     +' Derivative >= 0, backtracking line search impossible.',/,
     +'   Previous x, f and g restored.',/,
     +' Possible causes: 1 error in function or gradient evaluation;',/,
     +'                  2 rounding errors dominate computation.')
 */
                printf(" Derivative >= 0, backtracking line search impossible.\n"
                       "   Previous x, f and g restored.\n"
                       " Possible causes: 1 error in function or gradient evaluation;\n"
                       "                  2 rounding errors dominate computation.\n");
            }
/*<             if (info .eq. -5) write (6,9015) >*/
            if (*info == -5) {
/*
 9015 format (/,
     +' Warning:  more than 10 function and gradient',/,
     +'   evaluations in the last line search.  Termination',/,
     +'   may possibly be caused by a bad search direction.')
*/
                printf(" Warning:  more than 10 function and gradient\n"
                       "   evaluations in the last line search.  Termination\n"
                       "   may possibly be caused by a bad search direction.");
            }
/*<             if (info .eq. -6) write (6,*)' Input nbd(',k,') is invalid.' >*/
            if (*info == -6) {
                printf(" Input nbd(%ld) is invalid.\n", *k);
            }
/*<    >*/
            if (*info == -7) {
                printf(" l(%ld) > u(%ld).  No feasible solution.\n", *k, *k);
            }
/*<             if (info .eq. -8) write (6,9018) >*/
            if (*info == -8) {
/*
 9018 format (/,' The triangular system is singular.')
*/
                printf(" The triangular system is singular.\n");
            }
/*<             if (info .eq. -9) write (6,9019) >*/
            if (*info == -9) {
/*
 9019 format (/,
     +' Line search cannot locate an adequate point after 20 function',/
     +,'  and gradient evaluations.  Previous x, f and g restored.',/,
     +' Possible causes: 1 error in function or gradient evaluation;',/,
     +'                  2 rounding error dominate computation.')

*/
                printf(" Line search cannot locate an adequate point after 20 function\n"
                       "  and gradient evaluations.  Previous x, f and g restored.\n"
                       " Possible causes: 1 error in function or gradient evaluation;\n"
                       "                  2 rounding error dominate computation.\n");
            }
/*<          endif >*/
        }
/*<          if (iprint .ge. 1) write (6,3007) cachyt,sbtime,lnscht >*/
/*
 3007 format (/,' Cauchy                time',1p,e10.3,' seconds.',/ 
     +        ' Subspace minimization time',1p,e10.3,' seconds.',/
     +        ' Line search           time',1p,e10.3,' seconds.')
*/
        if (*iprint >= 1) {
            printf(" Cauchy                time %10.3g seconds.\n"
                   " Subspace minimization time %10.3g seconds.\n"
                   " Line search           time %10.3g seconds.\n",
                   *cachyt, *sbtime, *lnscht);
        }
/*<          write (6,3008) time >*/
/*
 3008 format (/,' Total User time',1p,e10.3,' seconds.',/)
 */
        printf(" Total User time %10.3g seconds.\n", *time);
/*<          if (iprint .ge. 1) then >*/
        if (*iprint >= 1) {
/*<             if (info .eq. -4 .or. info .eq. -9) then >*/
            if (*info == -4 || *info == -9) {
/*<    >*/
/*
 3002 format(2(1x,i4),2(1x,i5),2x,a3,1x,i4,1p,2(2x,d7.1),6x,'-',10x,'-')
 */
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                fprintf(0,
                        " %4ld %4ld %5ld %5ld  %3s %4ld  %7.1g %7.1g      -         -\n",
                        *iter, *nfgv, *nint, *nact, word, *iback, *stp, *xstep);
#endif
/*<             endif >*/
            }
/*<             write (itfile,3009) task >*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
            fprintf(0, "%60s\n", task);
#endif
/*<             if (info .ne. 0) then >*/
            if (*info != 0) {
/*<                if (info .eq. -1) write (itfile,9011) >*/
/*
 9011 format (/,
     +' Matrix in 1st Cholesky factorization in formk is not Pos. Def.')
 */
                if (*info == -1) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Matrix in 1st Cholesky factorization in formk is not Pos. Def.\n");
#endif
                }
/*<                if (info .eq. -2) write (itfile,9012) >*/
/*
 9012 format (/,
     +' Matrix in 2st Cholesky factorization in formk is not Pos. Def.')
 */
                if (*info == -2) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Matrix in 2st Cholesky factorization in formk is not Pos. Def.\n");
#endif
                }
/*<                if (info .eq. -3) write (itfile,9013) >*/
/*
 9013 format (/,
     +' Matrix in the Cholesky factorization in formt is not Pos. Def.')
 */
                if (*info == -3) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Matrix in the Cholesky factorization in formk is not Pos. Def.\n");
#endif
                }
/*<                if (info .eq. -4) write (itfile,9014) >*/
/*
 9014 format (/,
     +' Derivative >= 0, backtracking line search impossible.',/,
     +'   Previous x, f and g restored.',/,
     +' Possible causes: 1 error in function or gradient evaluation;',/,
     +'                  2 rounding errors dominate computation.')
 */
                if (*info == -4) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Derivative >= 0, backtracking line search impossible.\n"
                            "   Previous x, f and g restored.\n"
                            " Possible causes: 1 error in function or gradient evaluation;\n"
                            "                  2 rounding errors dominate computation.\n");
#endif
                }
/*<                if (info .eq. -5) write (itfile,9015) >*/
/*
 9015 format (/,
     +' Warning:  more than 10 function and gradient',/,
     +'   evaluations in the last line search.  Termination',/,
     +'   may possibly be caused by a bad search direction.')
 */
                if (*info == -5) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Warning:  more than 10 function and gradient\n"
                            "   evaluations in the last line search.  Termination\n"
                            "   may possibly be caused by a bad search direction.");
#endif
                }
/*<                if (info .eq. -8) write (itfile,9018) >*/
/*
 9018 format (/,' The triangular system is singular.')
 */
                if (*info == -8) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " The triangular system is singular.\n");
#endif
                }
/*<                if (info .eq. -9) write (itfile,9019) >*/
/*
 9019 format (/,
     +' Line search cannot locate an adequate point after 20 function',/
     +,'  and gradient evaluations.  Previous x, f and g restored.',/,
     +' Possible causes: 1 error in function or gradient evaluation;',/,
     +'                  2 rounding error dominate computation.')
 */
                if (*info == -9) {
#ifdef LBFGSB_ENABLE_ITERATE_FILE
                    fprintf(0, " Line search cannot locate an adequate point after 20 function\n"
                            "  and gradient evaluations.  Previous x, f and g restored.\n"
                            " Possible causes: 1 error in function or gradient evaluation;\n"
                            "                  2 rounding error dominate computation.\n");
#endif
                }
/*<             endif >*/
            }
/*<             write (itfile,3008) time >*/
#ifdef LBFGSB_ENABLE_ITERATE_FILE
            fprintf(0, " Total User time %10.3g seconds.\n", *time);
#endif
/*<          endif >*/
        }
/*<       endif >*/
    }
/*<  1004 format (/,a4, 1p, 6(1x,d11.4),/,(4x,1p,6(1x,d11.4))) >*/
/*<  3002 format(2(1x,i4),2(1x,i5),2x,a3,1x,i4,1p,2(2x,d7.1),6x,'-',10x,'-') >*/
/*<  3 >*/
/*<  3 >*/
/*<  3005 format (i5,2(1x,i4),(1x,i6),(2x,i4),(1x,i5),1p,2(2x,d10.3)) >*/
/*<  3006 format (i5,2(1x,i4),2(1x,i6),(1x,i4),(1x,i5),7x,'-',10x,'-') >*/
/* L3006: */
/*<  3 >*/
/*<  3008 format (/,' Total User time',1p,e10.3,' seconds.',/) >*/
/*<  3009 format (/,a60) >*/
/*<  9 >*/
/*<  9 >*/
/*<  9 >*/
/*<  9 >*/
/*<  9 >*/
/*<  9018 format (/,' The triangular system is singular.') >*/
/*<  9 >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* prn3lb_ */

/* ======================= The end of prn3lb ============================= */
/*<       subroutine projgr(n, l, u, nbd, x, g, sbgnrm) >*/
/* Subroutine */ int projgr_(integer *n, doublereal *l, doublereal *u, 
        integer *nbd, doublereal *x, doublereal *g, doublereal *sbgnrm)
{
    /* System generated locals */
    integer i__1;
    doublereal d__1, d__2;

    /* Local variables */
    integer i__;
    doublereal gi;

/*<       integer          n, nbd(n) >*/
/*<       double precision sbgnrm, x(n), l(n), u(n), g(n) >*/
/*     ************ */

/*     Subroutine projgr */

/*     This subroutine computes the infinity norm of the projected */
/*       gradient. */


/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer i >*/
/*<       double precision gi >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*<       sbgnrm = zero >*/
    /* Parameter adjustments */
    --g;
    --x;
    --nbd;
    --u;
    --l;

    /* Function Body */
    *sbgnrm = 0.;
/*<       do 15 i = 1, n >*/
    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<     gi = g(i) >*/
        gi = g[i__];
/*<         if (nbd(i) .ne. 0) then >*/
        if (nbd[i__] != 0) {
/*<            if (gi .lt. zero) then >*/
            if (gi < 0.) {
/*<               if (nbd(i) .ge. 2) gi = max((x(i)-u(i)),gi) >*/
                if (nbd[i__] >= 2) {
/* Computing MAX */
                    d__1 = x[i__] - u[i__];
                    gi = max(d__1,gi);
                }
/*<                else >*/
            } else {
/*<               if (nbd(i) .le. 2) gi = min((x(i)-l(i)),gi) >*/
                if (nbd[i__] <= 2) {
/* Computing MIN */
                    d__1 = x[i__] - l[i__];
                    gi = min(d__1,gi);
                }
/*<            endif >*/
            }
/*<         endif >*/
        }
/*<     sbgnrm = max(sbgnrm,abs(gi)) >*/
/* Computing MAX */
        d__1 = *sbgnrm, d__2 = abs(gi);
        *sbgnrm = max(d__1,d__2);
/*<   15  continue >*/
/* L15: */
    }
/*<       return >*/
    return 0;
/*<       end >*/
} /* projgr_ */

/* ======================= The end of projgr ============================= */
/*<    >*/
/* Subroutine */ int subsm_(integer *n, integer *m, integer *nsub, integer *
        ind, doublereal *l, doublereal *u, integer *nbd, doublereal *x, 
        doublereal *d__, doublereal *ws, doublereal *wy, doublereal *theta, 
        integer *col, integer *head, integer *iword, doublereal *wv, 
        doublereal *wn, integer *iprint, integer *info)
{
    /* System generated locals */
    integer ws_dim1, ws_offset, wy_dim1, wy_offset, wn_dim1, wn_offset, i__1, 
            i__2;

    /* Local variables */
    integer i__, j, k, m2;
    doublereal dk;
    integer js, jy, ibd=0, col2;
    doublereal temp1, temp2, alpha;
    extern /* Subroutine */ int dtrsl_(doublereal *, integer *, integer *, 
            doublereal *, integer *, integer *);
    integer pointr;

/*<    >*/
/*<    >*/
/*     ************ */

/*     Subroutine subsm */

/*     Given xcp, l, u, r, an index set that specifies */
/*      the active set at xcp, and an l-BFGS matrix B */
/*      (in terms of WY, WS, SY, WT, head, col, and theta), */
/*      this subroutine computes an approximate solution */
/*      of the subspace problem */

/*      (P)   min Q(x) = r'(x-xcp) + 1/2 (x-xcp)' B (x-xcp) */

/*             subject to l<=x<=u */
/*                      x_i=xcp_i for all i in A(xcp) */

/*      along the subspace unconstrained Newton direction */

/*         d = -(Z'BZ)^(-1) r. */

/*       The formula for the Newton direction, given the L-BFGS matrix */
/*       and the Sherman-Morrison formula, is */

/*         d = (1/theta)r + (1/theta*2) Z'WK^(-1)W'Z r. */

/*       where */
/*                 K = [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                     [L_a -R_z           theta*S'AA'S ] */

/*     Note that this procedure for computing d differs */
/*     from that described in [1]. One can show that the matrix K is */
/*     equal to the matrix M^[-1]N in that paper. */

/*     n is an integer variable. */
/*       On entry n is the dimension of the problem. */
/*       On exit n is unchanged. */

/*     m is an integer variable. */
/*       On entry m is the maximum number of variable metric corrections */
/*         used to define the limited memory matrix. */
/*       On exit m is unchanged. */

/*     nsub is an integer variable. */
/*       On entry nsub is the number of free variables. */
/*       On exit nsub is unchanged. */

/*     ind is an integer array of dimension nsub. */
/*       On entry ind specifies the coordinate indices of free variables. */
/*       On exit ind is unchanged. */

/*     l is a double precision array of dimension n. */
/*       On entry l is the lower bound of x. */
/*       On exit l is unchanged. */

/*     u is a double precision array of dimension n. */
/*       On entry u is the upper bound of x. */
/*       On exit u is unchanged. */

/*     nbd is a integer array of dimension n. */
/*       On entry nbd represents the type of bounds imposed on the */
/*         variables, and must be specified as follows: */
/*         nbd(i)=0 if x(i) is unbounded, */
/*                1 if x(i) has only a lower bound, */
/*                2 if x(i) has both lower and upper bounds, and */
/*                3 if x(i) has only an upper bound. */
/*       On exit nbd is unchanged. */

/*     x is a double precision array of dimension n. */
/*       On entry x specifies the Cauchy point xcp. */
/*       On exit x(i) is the minimizer of Q over the subspace of */
/*                                                        free variables. */

/*     d is a double precision array of dimension n. */
/*       On entry d is the reduced gradient of Q at xcp. */
/*       On exit d is the Newton direction of Q. */

/*     ws and wy are double precision arrays; */
/*     theta is a double precision variable; */
/*     col is an integer variable; */
/*     head is an integer variable. */
/*       On entry they store the information defining the */
/*                                          limited memory BFGS matrix: */
/*         ws(n,m) stores S, a set of s-vectors; */
/*         wy(n,m) stores Y, a set of y-vectors; */
/*         theta is the scaling factor specifying B_0 = theta I; */
/*         col is the number of variable metric corrections stored; */
/*         head is the location of the 1st s- (or y-) vector in S (or Y). */
/*       On exit they are unchanged. */

/*     iword is an integer variable. */
/*       On entry iword is unspecified. */
/*       On exit iword specifies the status of the subspace solution. */
/*         iword = 0 if the solution is in the box, */
/*                 1 if some bound is encountered. */

/*     wv is a double precision working array of dimension 2m. */

/*     wn is a double precision array of dimension 2m x 2m. */
/*       On entry the upper triangle of wn stores the LEL^T factorization */
/*         of the indefinite matrix */

/*              K = [-D -Y'ZZ'Y/theta     L_a'-R_z'  ] */
/*                  [L_a -R_z           theta*S'AA'S ] */
/*                                                    where E = [-I  0] */
/*                                                              [ 0  I] */
/*       On exit wn is unchanged. */

/*     iprint is an INTEGER variable that must be set by the user. */
/*       It controls the frequency and type of output generated: */
/*        iprint<0    no output is generated; */
/*        iprint=0    print only one line at the last iteration; */
/*        0<iprint<99 print also f and |proj g| every iprint iterations; */
/*        iprint=99   print details of every iteration except n-vectors; */
/*        iprint=100  print also the changes of active set and final x; */
/*        iprint>100  print details of every iteration including x and g; */
/*       When iprint > 0, the file iterate.dat will be created to */
/*                        summarize the iteration. */

/*     info is an integer variable. */
/*       On entry info is unspecified. */
/*       On exit info = 0       for normal return, */
/*                    = nonzero for abnormal return */
/*                                  when the matrix K is ill-conditioned. */

/*     Subprograms called: */

/*       Linpack dtrsl. */


/*     References: */

/*       [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited */
/*       memory algorithm for bound constrained optimization'', */
/*       SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208. */



/*                           *  *  * */

/*     NEOS, November 1994. (Latest revision June 1996.) */
/*     Optimization Technology Center. */
/*     Argonne National Laboratory and Northwestern University. */
/*     Written by */
/*                        Ciyou Zhu */
/*     in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal. */


/*     ************ */
/*<       integer          pointr,m2,col2,ibd,jy,js,i,j,k >*/
/*<       double precision alpha,dk,temp1,temp2 >*/
/*<       double precision one,zero >*/
/*<       parameter        (one=1.0d0,zero=0.0d0) >*/
/*<       if (nsub .le. 0) return >*/
    /* Parameter adjustments */
    --d__;
    --x;
    --nbd;
    --u;
    --l;
    wn_dim1 = 2 * *m;
    wn_offset = 1 + wn_dim1;
    wn -= wn_offset;
    --wv;
    wy_dim1 = *n;
    wy_offset = 1 + wy_dim1;
    wy -= wy_offset;
    ws_dim1 = *n;
    ws_offset = 1 + ws_dim1;
    ws -= ws_offset;
    --ind;

    /* Function Body */
    if (*nsub <= 0) {
        return 0;
    }
/*<       if (iprint .ge. 99) write (6,1001) >*/
/*
 1001 format (/,'----------------SUBSM entered-----------------',/)
 */
    if (*iprint >= 99) {
        printf("----------------SUBSM entered-----------------");
    }
/*     Compute wv = W'Zd. */
/*<       pointr = head  >*/
    pointr = *head;
/*<       do 20 i = 1, col >*/
    i__1 = *col;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<              temp1 = zero >*/
        temp1 = 0.;
/*<      temp2 = zero >*/
        temp2 = 0.;
/*<      do 10 j = 1, nsub >*/
        i__2 = *nsub;
        for (j = 1; j <= i__2; ++j) {
/*<         k = ind(j) >*/
            k = ind[j];
/*<         temp1 = temp1 + wy(k,pointr)*d(j) >*/
            temp1 += wy[k + pointr * wy_dim1] * d__[j];
/*<         temp2 = temp2 + ws(k,pointr)*d(j) >*/
            temp2 += ws[k + pointr * ws_dim1] * d__[j];
/*<   10     continue >*/
/* L10: */
        }
/*<      wv(i) = temp1 >*/
        wv[i__] = temp1;
/*<      wv(col + i) = theta*temp2 >*/
        wv[*col + i__] = *theta * temp2;
/*<      pointr = mod(pointr,m) + 1 >*/
        pointr = pointr % *m + 1;
/*<   20  continue >*/
/* L20: */
    }
/*     Compute wv:=K^(-1)wv. */
/*<       m2 = 2*m >*/
    m2 = *m << 1;
/*<       col2 = 2*col >*/
    col2 = *col << 1;
/*<       call dtrsl(wn,m2,col2,wv,11,info) >*/
    dtrsl_(&wn[wn_offset], &m2, &col2, &wv[1], &c__11, info);
/*<       if (info .ne. 0) return >*/
    if (*info != 0) {
        return 0;
    }
/*<       do 25 i = 1, col >*/
    i__1 = *col;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      wv(i) = -wv(i) >*/
        wv[i__] = -wv[i__];
/*<   25     continue >*/
/* L25: */
    }
/*<       call dtrsl(wn,m2,col2,wv,01,info) >*/
    dtrsl_(&wn[wn_offset], &m2, &col2, &wv[1], &c__1, info);
/*<       if (info .ne. 0) return >*/
    if (*info != 0) {
        return 0;
    }
/*     Compute d = (1/theta)d + (1/theta**2)Z'W wv. */
/*<       pointr = head >*/
    pointr = *head;
/*<       do 40 jy = 1, col >*/
    i__1 = *col;
    for (jy = 1; jy <= i__1; ++jy) {
/*<          js = col + jy >*/
        js = *col + jy;
/*<      do 30 i = 1, nsub >*/
        i__2 = *nsub;
        for (i__ = 1; i__ <= i__2; ++i__) {
/*<         k = ind(i) >*/
            k = ind[i__];
/*<       >*/
            d__[i__] = d__[i__] + wy[k + pointr * wy_dim1] * wv[jy] / *theta 
                    + ws[k + pointr * ws_dim1] * wv[js];
/*<   30     continue >*/
/* L30: */
        }
/*<      pointr = mod(pointr,m) + 1 >*/
        pointr = pointr % *m + 1;
/*<   40  continue >*/
/* L40: */
    }
/*<       do 50 i = 1, nsub >*/
    i__1 = *nsub;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      d(i) = d(i)/theta >*/
        d__[i__] /= *theta;
/*<   50  continue >*/
/* L50: */
    }
/*     Backtrack to the feasible region. */
/*<       alpha = one >*/
    alpha = 1.;
/*<       temp1 = alpha  >*/
    temp1 = alpha;
/*<       do 60 i = 1, nsub >*/
    i__1 = *nsub;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      k = ind(i) >*/
        k = ind[i__];
/*<          dk = d(i) >*/
        dk = d__[i__];
/*<      if (nbd(k) .ne. 0) then >*/
        if (nbd[k] != 0) {
/*<         if (dk .lt. zero .and. nbd(k) .le. 2) then >*/
            if (dk < 0. && nbd[k] <= 2) {
/*<            temp2 = l(k) - x(k) >*/
                temp2 = l[k] - x[k];
/*<            if (temp2 .ge. zero) then >*/
                if (temp2 >= 0.) {
/*<               temp1 = zero >*/
                    temp1 = 0.;
/*<            else if (dk*alpha .lt. temp2) then >*/
                } else if (dk * alpha < temp2) {
/*<               temp1 = temp2/dk >*/
                    temp1 = temp2 / dk;
/*<            endif >*/
                }
/*<         else if (dk .gt. zero .and. nbd(k) .ge. 2) then >*/
            } else if (dk > 0. && nbd[k] >= 2) {
/*<            temp2 = u(k) - x(k) >*/
                temp2 = u[k] - x[k];
/*<            if (temp2 .le. zero) then >*/
                if (temp2 <= 0.) {
/*<               temp1 = zero >*/
                    temp1 = 0.;
/*<            else if (dk*alpha .gt. temp2) then >*/
                } else if (dk * alpha > temp2) {
/*<               temp1 = temp2/dk >*/
                    temp1 = temp2 / dk;
/*<            endif >*/
                }
/*<             endif >*/
            }
/*<             if (temp1 .lt. alpha) then >*/
            if (temp1 < alpha) {
/*<            alpha = temp1 >*/
                alpha = temp1;
/*<            ibd = i >*/
                ibd = i__;
/*<             endif >*/
            }
/*<          endif >*/
        }
/*<   60  continue >*/
/* L60: */
    }
/*<       if (alpha .lt. one) then >*/
    if (alpha < 1.) {
/*<              dk = d(ibd) >*/
        dk = d__[ibd];
/*<              k = ind(ibd) >*/
        k = ind[ibd];
/*<              if (dk .gt. zero) then >*/
        if (dk > 0.) {
/*<             x(k) = u(k) >*/
            x[k] = u[k];
/*<             d(ibd) = zero >*/
            d__[ibd] = 0.;
/*<              else if (dk .lt. zero) then >*/
        } else if (dk < 0.) {
/*<             x(k) = l(k) >*/
            x[k] = l[k];
/*<         d(ibd) = zero >*/
            d__[ibd] = 0.;
/*<              endif >*/
        }
/*<       endif >*/
    }
/*<       do 70 i = 1, nsub >*/
    i__1 = *nsub;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<      k = ind(i) >*/
        k = ind[i__];
/*<      x(k) = x(k) + alpha*d(i) >*/
        x[k] += alpha * d__[i__];
/*<   70  continue >*/
/* L70: */
    }
/*<       if (iprint .ge. 99) then >*/
    if (*iprint >= 99) {
/*<      if (alpha .lt. one) then >*/
        if (alpha < 1.) {
/*<             write (6,1002) alpha >*/
/*
 1002 format ( 'ALPHA = ',f7.5,' backtrack to the BOX') 
*/
            printf("ALPHA = %7.5g backtrack to the BOX\n", alpha);
/*<          else >*/
        } else {
/*<             write (6,*) 'SM solution inside the box' >*/
            printf("SM solution inside the box\n");
/*<      end if  >*/
        }
/*<      if (iprint .gt.100) write (6,1003) (x(i),i=1,n) >*/
/*
 1003 format ('Subspace solution X =  ',/,(4x,1p,6(1x,d11.4)))
 */
        if (*iprint > 100) {
            i__1 = *n;
            lbfgsb_printf_vec("Subspace solution X", x, i__1);
        }
/*<       endif >*/
    }
/*<       if (alpha .lt. one) then >*/
    if (alpha < 1.) {
/*<          iword = 1 >*/
        *iword = 1;
/*<       else >*/
    } else {
/*<          iword = 0 >*/
        *iword = 0;
/*<       endif  >*/
    }
/*<       if (iprint .ge. 99) write (6,1004) >*/
/*
 1004 format (/,'----------------exit SUBSM --------------------',/)
 */
    if (*iprint >= 99) {
        printf("----------------exit SUBSM --------------------");
    }
/*<  1001 format (/,'----------------SUBSM entered-----------------',/) >*/
/*<  1002 format ( 'ALPHA = ',f7.5,' backtrack to the BOX')      >*/
/*<  1003 format ('Subspace solution X =  ',/,(4x,1p,6(1x,d11.4))) >*/
/*<  1004 format (/,'----------------exit SUBSM --------------------',/) >*/
/*<       return >*/
    return 0;
/*<       end >*/
} /* subsm_ */

/* ====================== The end of subsm =============================== */
/*<    >*/
/* Subroutine */ int dcsrch_(doublereal *f, doublereal *g, doublereal *stp, 
        doublereal *ftol, doublereal *gtol, doublereal *xtol, doublereal *
        stpmin, doublereal *stpmax, char *task, integer *isave, doublereal *
        dsave, ftnlen task_len)
{
    /* System generated locals */
    doublereal d__1;

    /* Builtin functions */
    integer s_cmp(char *, char *, ftnlen, ftnlen);
    /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen);

    /* Local variables */
    doublereal fm, gm, fx, fy, gx, gy, fxm, fym, gxm, gym, stx, sty;
    integer stage;
    doublereal finit, ginit, width, ftest, gtest, stmin, stmax, width1;
    logical brackt;
    extern /* Subroutine */ int dcstep_(doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *, doublereal *, doublereal *, logical *, doublereal *,
             doublereal *);

/*<       character*(*) task >*/
/*<       integer isave(2) >*/
/*<       double precision f,g,stp,ftol,gtol,xtol,stpmin,stpmax >*/
/*<       double precision dsave(13) >*/
/*     ********** */

/*     Subroutine dcsrch */

/*     This subroutine finds a step that satisfies a sufficient */
/*     decrease condition and a curvature condition. */

/*     Each call of the subroutine updates an interval with */
/*     endpoints stx and sty. The interval is initially chosen */
/*     so that it contains a minimizer of the modified function */

/*           psi(stp) = f(stp) - f(0) - ftol*stp*f'(0). */

/*     If psi(stp) <= 0 and f'(stp) >= 0 for some step, then the */
/*     interval is chosen so that it contains a minimizer of f. */

/*     The algorithm is designed to find a step that satisfies */
/*     the sufficient decrease condition */

/*           f(stp) <= f(0) + ftol*stp*f'(0), */

/*     and the curvature condition */

/*           abs(f'(stp)) <= gtol*abs(f'(0)). */

/*     If ftol is less than gtol and if, for example, the function */
/*     is bounded below, then there is always a step which satisfies */
/*     both conditions. */

/*     If no step can be found that satisfies both conditions, then */
/*     the algorithm stops with a warning. In this case stp only */
/*     satisfies the sufficient decrease condition. */

/*     A typical invocation of dcsrch has the following outline: */

/*     task = 'START' */
/*  10 continue */
/*        call dcsrch( ... ) */
/*        if (task .eq. 'FG') then */
/*           Evaluate the function and the gradient at stp */
/*           goto 10 */
/*           end if */

/*     NOTE: The user must no alter work arrays between calls. */

/*     The subroutine statement is */

/*        subroutine dcsrch(f,g,stp,ftol,gtol,xtol,stpmin,stpmax, */
/*                          task,isave,dsave) */
/*     where */

/*       f is a double precision variable. */
/*         On initial entry f is the value of the function at 0. */
/*            On subsequent entries f is the value of the */
/*            function at stp. */
/*         On exit f is the value of the function at stp. */

/*      g is a double precision variable. */
/*         On initial entry g is the derivative of the function at 0. */
/*            On subsequent entries g is the derivative of the */
/*            function at stp. */
/*         On exit g is the derivative of the function at stp. */

/*      stp is a double precision variable. */
/*         On entry stp is the current estimate of a satisfactory */
/*            step. On initial entry, a positive initial estimate */
/*            must be provided. */
/*         On exit stp is the current estimate of a satisfactory step */
/*            if task = 'FG'. If task = 'CONV' then stp satisfies */
/*            the sufficient decrease and curvature condition. */

/*       ftol is a double precision variable. */
/*         On entry ftol specifies a nonnegative tolerance for the */
/*            sufficient decrease condition. */
/*         On exit ftol is unchanged. */

/*       gtol is a double precision variable. */
/*         On entry gtol specifies a nonnegative tolerance for the */
/*            curvature condition. */
/*         On exit gtol is unchanged. */

/*      xtol is a double precision variable. */
/*         On entry xtol specifies a nonnegative relative tolerance */
/*            for an acceptable step. The subroutine exits with a */
/*            warning if the relative difference between sty and stx */
/*            is less than xtol. */
/*         On exit xtol is unchanged. */

/*      stpmin is a double precision variable. */
/*         On entry stpmin is a nonnegative lower bound for the step. */
/*         On exit stpmin is unchanged. */

/*      stpmax is a double precision variable. */
/*         On entry stpmax is a nonnegative upper bound for the step. */
/*         On exit stpmax is unchanged. */

/*       task is a character variable of length at least 60. */
/*         On initial entry task must be set to 'START'. */
/*         On exit task indicates the required action: */

/*            If task(1:2) = 'FG' then evaluate the function and */
/*            derivative at stp and call dcsrch again. */

/*            If task(1:4) = 'CONV' then the search is successful. */

/*            If task(1:4) = 'WARN' then the subroutine is not able */
/*            to satisfy the convergence conditions. The exit value of */
/*            stp contains the best point found during the search. */

/*            If task(1:5) = 'ERROR' then there is an error in the */
/*            input arguments. */

/*         On exit with convergence, a warning or an error, the */
/*            variable task contains additional information. */

/*       isave is an integer work array of dimension 2. */

/*       dsave is a double precision work array of dimension 13. */

/*     Subprograms called */

/*      MINPACK-2 ... dcstep */

/*     MINPACK-1 Project. June 1983. */
/*     Argonne National Laboratory. */
/*     Jorge J. More' and David J. Thuente. */

/*     MINPACK-2 Project. October 1993. */
/*     Argonne National Laboratory and University of Minnesota. */
/*     Brett M. Averick, Richard G. Carter, and Jorge J. More'. */

/*     ********** */
/*<       double precision zero,p5,p66 >*/
/*<       parameter(zero=0.0d0,p5=0.5d0,p66=0.66d0) >*/
/*<       double precision xtrapl,xtrapu >*/
/*<       parameter(xtrapl=1.1d0,xtrapu=4.0d0) >*/
/*<       logical brackt >*/
/*<       integer stage >*/
/*<    >*/
/*     Initialization block. */
/*<       if (task(1:5) .eq. 'START') then >*/
    /* Parameter adjustments */
    --dsave;
    --isave;

    /* Function Body */
    if (s_cmp(task, "START", (ftnlen)5, (ftnlen)5) == 0) {
/*        Check the input arguments for errors. */
/*<          if (stp .lt. stpmin) task = 'ERROR: STP .LT. STPMIN' >*/
        if (*stp < *stpmin) {
            s_copy(task, "ERROR: STP .LT. STPMIN", task_len, (ftnlen)22);
        }
/*<          if (stp .gt. stpmax) task = 'ERROR: STP .GT. STPMAX' >*/
        if (*stp > *stpmax) {
            s_copy(task, "ERROR: STP .GT. STPMAX", task_len, (ftnlen)22);
        }
/*<          if (g .ge. zero) task = 'ERROR: INITIAL G .GE. ZERO' >*/
        if (*g >= 0.) {
            s_copy(task, "ERROR: INITIAL G .GE. ZERO", task_len, (ftnlen)26);
        }
/*<          if (ftol .lt. zero) task = 'ERROR: FTOL .LT. ZERO' >*/
        if (*ftol < 0.) {
            s_copy(task, "ERROR: FTOL .LT. ZERO", task_len, (ftnlen)21);
        }
/*<          if (gtol .lt. zero) task = 'ERROR: GTOL .LT. ZERO' >*/
        if (*gtol < 0.) {
            s_copy(task, "ERROR: GTOL .LT. ZERO", task_len, (ftnlen)21);
        }
/*<          if (xtol .lt. zero) task = 'ERROR: XTOL .LT. ZERO' >*/
        if (*xtol < 0.) {
            s_copy(task, "ERROR: XTOL .LT. ZERO", task_len, (ftnlen)21);
        }
/*<          if (stpmin .lt. zero) task = 'ERROR: STPMIN .LT. ZERO' >*/
        if (*stpmin < 0.) {
            s_copy(task, "ERROR: STPMIN .LT. ZERO", task_len, (ftnlen)23);
        }
/*<          if (stpmax .lt. stpmin) task = 'ERROR: STPMAX .LT. STPMIN' >*/
        if (*stpmax < *stpmin) {
            s_copy(task, "ERROR: STPMAX .LT. STPMIN", task_len, (ftnlen)25);
        }
/*        Exit if there are errors on input. */
/*<          if (task(1:5) .eq. 'ERROR') return >*/
        if (s_cmp(task, "ERROR", (ftnlen)5, (ftnlen)5) == 0) {
            return 0;
        }
/*        Initialize local variables. */
/*<          brackt = .false. >*/
        brackt = FALSE_;
/*<          stage = 1 >*/
        stage = 1;
/*<          finit = f >*/
        finit = *f;
/*<          ginit = g >*/
        ginit = *g;
/*<          gtest = ftol*ginit >*/
        gtest = *ftol * ginit;
/*<          width = stpmax - stpmin >*/
        width = *stpmax - *stpmin;
/*<          width1 = width/p5 >*/
        width1 = width / .5;
/*        The variables stx, fx, gx contain the values of the step, */
/*        function, and derivative at the best step. */
/*        The variables sty, fy, gy contain the value of the step, */
/*        function, and derivative at sty. */
/*        The variables stp, f, g contain the values of the step, */
/*        function, and derivative at stp. */
/*<          stx = zero >*/
        stx = 0.;
/*<          fx = finit >*/
        fx = finit;
/*<          gx = ginit >*/
        gx = ginit;
/*<          sty = zero >*/
        sty = 0.;
/*<          fy = finit >*/
        fy = finit;
/*<          gy = ginit >*/
        gy = ginit;
/*<          stmin = zero >*/
        stmin = 0.;
/*<          stmax = stp + xtrapu*stp >*/
        stmax = *stp + *stp * 4.;
/*<          task = 'FG' >*/
        s_copy(task, "FG", task_len, (ftnlen)2);
/*<          goto 1000 >*/
        goto L1000;
/*<       else >*/
    } else {
/*        Restore local variables. */
/*<          if (isave(1) .eq. 1) then >*/
        if (isave[1] == 1) {
/*<             brackt = .true. >*/
            brackt = TRUE_;
/*<          else >*/
        } else {
/*<             brackt = .false. >*/
            brackt = FALSE_;
/*<          endif >*/
        }
/*<          stage = isave(2)  >*/
        stage = isave[2];
/*<          ginit = dsave(1)  >*/
        ginit = dsave[1];
/*<          gtest = dsave(2)  >*/
        gtest = dsave[2];
/*<          gx = dsave(3)  >*/
        gx = dsave[3];
/*<          gy = dsave(4)  >*/
        gy = dsave[4];
/*<          finit = dsave(5)  >*/
        finit = dsave[5];
/*<          fx = dsave(6)  >*/
        fx = dsave[6];
/*<          fy = dsave(7)  >*/
        fy = dsave[7];
/*<          stx = dsave(8)  >*/
        stx = dsave[8];
/*<          sty = dsave(9)  >*/
        sty = dsave[9];
/*<          stmin = dsave(10)  >*/
        stmin = dsave[10];
/*<          stmax = dsave(11)  >*/
        stmax = dsave[11];
/*<          width = dsave(12)  >*/
        width = dsave[12];
/*<          width1 = dsave(13)  >*/
        width1 = dsave[13];
/*<       endif >*/
    }
/*     If psi(stp) <= 0 and f'(stp) >= 0 for some step, then the */
/*     algorithm enters the second stage. */
/*<       ftest = finit + stp*gtest >*/
    ftest = finit + *stp * gtest;
/*<    >*/
    if (stage == 1 && *f <= ftest && *g >= 0.) {
        stage = 2;
    }
/*     Test for warnings. */
/*<    >*/
    if (brackt && (*stp <= stmin || *stp >= stmax)) {
        s_copy(task, "WARNING: ROUNDING ERRORS PREVENT PROGRESS", task_len, (
                ftnlen)41);
    }
/*<    >*/
    if (brackt && stmax - stmin <= *xtol * stmax) {
        s_copy(task, "WARNING: XTOL TEST SATISFIED", task_len, (ftnlen)28);
    }
/*<    >*/
    if (*stp == *stpmax && *f <= ftest && *g <= gtest) {
        s_copy(task, "WARNING: STP = STPMAX", task_len, (ftnlen)21);
    }
/*<    >*/
    if (*stp == *stpmin && (*f > ftest || *g >= gtest)) {
        s_copy(task, "WARNING: STP = STPMIN", task_len, (ftnlen)21);
    }
/*     Test for convergence. */
/*<    >*/
    if (*f <= ftest && abs(*g) <= *gtol * (-ginit)) {
        s_copy(task, "CONVERGENCE", task_len, (ftnlen)11);
    }
/*     Test for termination. */
/*<       if (task(1:4) .eq. 'WARN' .or. task(1:4) .eq. 'CONV') goto 1000 >*/
    if (s_cmp(task, "WARN", (ftnlen)4, (ftnlen)4) == 0 || s_cmp(task, "CONV", 
            (ftnlen)4, (ftnlen)4) == 0) {
        goto L1000;
    }
/*     A modified function is used to predict the step during the */
/*     first stage if a lower function value has been obtained but */
/*     the decrease is not sufficient. */
/*<       if (stage .eq. 1 .and. f .le. fx .and. f .gt. ftest) then >*/
    if (stage == 1 && *f <= fx && *f > ftest) {
/*        Define the modified function and derivative values. */
/*<          fm = f - stp*gtest >*/
        fm = *f - *stp * gtest;
/*<          fxm = fx - stx*gtest >*/
        fxm = fx - stx * gtest;
/*<          fym = fy - sty*gtest >*/
        fym = fy - sty * gtest;
/*<          gm = g - gtest >*/
        gm = *g - gtest;
/*<          gxm = gx - gtest >*/
        gxm = gx - gtest;
/*<          gym = gy - gtest >*/
        gym = gy - gtest;
/*        Call dcstep to update stx, sty, and to compute the new step. */
/*<    >*/
        dcstep_(&stx, &fxm, &gxm, &sty, &fym, &gym, stp, &fm, &gm, &brackt, &
                stmin, &stmax);
/*        Reset the function and derivative values for f. */
/*<          fx = fxm + stx*gtest >*/
        fx = fxm + stx * gtest;
/*<          fy = fym + sty*gtest >*/
        fy = fym + sty * gtest;
/*<          gx = gxm + gtest >*/
        gx = gxm + gtest;
/*<          gy = gym + gtest >*/
        gy = gym + gtest;
/*<       else >*/
    } else {
/*       Call dcstep to update stx, sty, and to compute the new step. */
/*<    >*/
        dcstep_(&stx, &fx, &gx, &sty, &fy, &gy, stp, f, g, &brackt, &stmin, &
                stmax);
/*<       endif >*/
    }
/*     Decide if a bisection step is needed. */
/*<       if (brackt) then >*/
    if (brackt) {
/*<          if (abs(sty-stx) .ge. p66*width1) stp = stx + p5*(sty - stx) >*/
        if ((d__1 = sty - stx, abs(d__1)) >= width1 * .66) {
            *stp = stx + (sty - stx) * .5;
        }
/*<          width1 = width >*/
        width1 = width;
/*<          width = abs(sty-stx) >*/
        width = (d__1 = sty - stx, abs(d__1));
/*<       endif >*/
    }
/*     Set the minimum and maximum steps allowed for stp. */
/*<       if (brackt) then >*/
    if (brackt) {
/*<          stmin = min(stx,sty) >*/
        stmin = min(stx,sty);
/*<          stmax = max(stx,sty) >*/
        stmax = max(stx,sty);
/*<       else >*/
    } else {
/*<          stmin = stp + xtrapl*(stp - stx) >*/
        stmin = *stp + (*stp - stx) * 1.1;
/*<          stmax = stp + xtrapu*(stp - stx) >*/
        stmax = *stp + (*stp - stx) * 4.;
/*<       endif >*/
    }
/*     Force the step to be within the bounds stpmax and stpmin. */
/*<       stp = max(stp,stpmin) >*/
    *stp = max(*stp,*stpmin);
/*<       stp = min(stp,stpmax) >*/
    *stp = min(*stp,*stpmax);
/*     If further progress is not possible, let stp be the best */
/*     point obtained during the search. */
/*<    >*/
    if ((brackt && (*stp <= stmin || *stp >= stmax)) ||
        (brackt && stmax - stmin <= *xtol * stmax)) {
        *stp = stx;
    }
/*     Obtain another function and derivative. */
/*<       task = 'FG' >*/
    s_copy(task, "FG", task_len, (ftnlen)2);
/*<  1000 continue >*/
L1000:
/*     Save local variables. */
/*<       if (brackt) then >*/
    if (brackt) {
/*<          isave(1) = 1 >*/
        isave[1] = 1;
/*<       else >*/
    } else {
/*<          isave(1) = 0 >*/
        isave[1] = 0;
/*<       endif >*/
    }
/*<       isave(2) = stage >*/
    isave[2] = stage;
/*<       dsave(1) =  ginit >*/
    dsave[1] = ginit;
/*<       dsave(2) =  gtest >*/
    dsave[2] = gtest;
/*<       dsave(3) =  gx >*/
    dsave[3] = gx;
/*<       dsave(4) =  gy >*/
    dsave[4] = gy;
/*<       dsave(5) =  finit >*/
    dsave[5] = finit;
/*<       dsave(6) =  fx >*/
    dsave[6] = fx;
/*<       dsave(7) =  fy >*/
    dsave[7] = fy;
/*<       dsave(8) =  stx >*/
    dsave[8] = stx;
/*<       dsave(9) =  sty >*/
    dsave[9] = sty;
/*<       dsave(10) = stmin >*/
    dsave[10] = stmin;
/*<       dsave(11) = stmax >*/
    dsave[11] = stmax;
/*<       dsave(12) = width >*/
    dsave[12] = width;
/*<       dsave(13) = width1 >*/
    dsave[13] = width1;
/*<       end >*/
    return 0;
} /* dcsrch_ */

/* ====================== The end of dcsrch ============================== */
/*<    >*/
/* Subroutine */ int dcstep_(doublereal *stx, doublereal *fx, doublereal *dx, 
        doublereal *sty, doublereal *fy, doublereal *dy, doublereal *stp, 
        doublereal *fp, doublereal *dp, logical *brackt, doublereal *stpmin, 
        doublereal *stpmax)
{
    /* System generated locals */
    doublereal d__1, d__2, d__3;

    /* Builtin functions */
    double sqrt(doublereal);

    /* Local variables */
    doublereal p, q, r__, s, sgnd, stpc, stpf, stpq, gamma, theta;

/*<       logical brackt >*/
/*<       double precision stx,fx,dx,sty,fy,dy,stp,fp,dp,stpmin,stpmax >*/
/*     ********** */

/*     Subroutine dcstep */

/*     This subroutine computes a safeguarded step for a search */
/*     procedure and updates an interval that contains a step that */
/*     satisfies a sufficient decrease and a curvature condition. */

/*     The parameter stx contains the step with the least function */
/*     value. If brackt is set to .true. then a minimizer has */
/*     been bracketed in an interval with endpoints stx and sty. */
/*     The parameter stp contains the current step. */
/*     The subroutine assumes that if brackt is set to .true. then */

/*           min(stx,sty) < stp < max(stx,sty), */

/*     and that the derivative at stx is negative in the direction */
/*     of the step. */

/*     The subroutine statement is */

/*       subroutine dcstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,brackt, */
/*                         stpmin,stpmax) */

/*     where */

/*       stx is a double precision variable. */
/*         On entry stx is the best step obtained so far and is an */
/*            endpoint of the interval that contains the minimizer. */
/*         On exit stx is the updated best step. */

/*       fx is a double precision variable. */
/*         On entry fx is the function at stx. */
/*         On exit fx is the function at stx. */

/*       dx is a double precision variable. */
/*         On entry dx is the derivative of the function at */
/*            stx. The derivative must be negative in the direction of */
/*            the step, that is, dx and stp - stx must have opposite */
/*            signs. */
/*         On exit dx is the derivative of the function at stx. */

/*       sty is a double precision variable. */
/*         On entry sty is the second endpoint of the interval that */
/*            contains the minimizer. */
/*         On exit sty is the updated endpoint of the interval that */
/*            contains the minimizer. */

/*       fy is a double precision variable. */
/*         On entry fy is the function at sty. */
/*         On exit fy is the function at sty. */

/*       dy is a double precision variable. */
/*         On entry dy is the derivative of the function at sty. */
/*         On exit dy is the derivative of the function at the exit sty. */

/*       stp is a double precision variable. */
/*         On entry stp is the current step. If brackt is set to .true. */
/*            then on input stp must be between stx and sty. */
/*         On exit stp is a new trial step. */

/*       fp is a double precision variable. */
/*         On entry fp is the function at stp */
/*         On exit fp is unchanged. */

/*       dp is a double precision variable. */
/*         On entry dp is the the derivative of the function at stp. */
/*         On exit dp is unchanged. */

/*       brackt is an logical variable. */
/*         On entry brackt specifies if a minimizer has been bracketed. */
/*            Initially brackt must be set to .false. */
/*         On exit brackt specifies if a minimizer has been bracketed. */
/*            When a minimizer is bracketed brackt is set to .true. */

/*       stpmin is a double precision variable. */
/*         On entry stpmin is a lower bound for the step. */
/*         On exit stpmin is unchanged. */

/*       stpmax is a double precision variable. */
/*         On entry stpmax is an upper bound for the step. */
/*         On exit stpmax is unchanged. */

/*     MINPACK-1 Project. June 1983 */
/*     Argonne National Laboratory. */
/*     Jorge J. More' and David J. Thuente. */

/*     MINPACK-2 Project. October 1993. */
/*     Argonne National Laboratory and University of Minnesota. */
/*     Brett M. Averick and Jorge J. More'. */

/*     ********** */
/*<       double precision zero,p66,two,three >*/
/*<       parameter(zero=0.0d0,p66=0.66d0,two=2.0d0,three=3.0d0) >*/
/*<       double precision gamma,p,q,r,s,sgnd,stpc,stpf,stpq,theta >*/
/*<       sgnd = dp*(dx/abs(dx)) >*/
    sgnd = *dp * (*dx / abs(*dx));
/*     First case: A higher function value. The minimum is bracketed. */
/*     If the cubic step is closer to stx than the quadratic step, the */
/*     cubic step is taken, otherwise the average of the cubic and */
/*     quadratic steps is taken. */
/*<       if (fp .gt. fx) then >*/
    if (*fp > *fx) {
/*<          theta = three*(fx - fp)/(stp - stx) + dx + dp >*/
        theta = (*fx - *fp) * 3. / (*stp - *stx) + *dx + *dp;
/*<          s = max(abs(theta),abs(dx),abs(dp)) >*/
/* Computing MAX */
        d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
                *dp);
        s = max(d__1,d__2);
/*<          gamma = s*sqrt((theta/s)**2 - (dx/s)*(dp/s)) >*/
/* Computing 2nd power */
        d__1 = theta / s;
        gamma = s * sqrt(d__1 * d__1 - *dx / s * (*dp / s));
/*<          if (stp .lt. stx) gamma = -gamma >*/
        if (*stp < *stx) {
            gamma = -gamma;
        }
/*<          p = (gamma - dx) + theta >*/
        p = gamma - *dx + theta;
/*<          q = ((gamma - dx) + gamma) + dp >*/
        q = gamma - *dx + gamma + *dp;
/*<          r = p/q >*/
        r__ = p / q;
/*<          stpc = stx + r*(stp - stx) >*/
        stpc = *stx + r__ * (*stp - *stx);
/*<    >*/
        stpq = *stx + *dx / ((*fx - *fp) / (*stp - *stx) + *dx) / 2. * (*stp 
                - *stx);
/*<          if (abs(stpc-stx) .lt. abs(stpq-stx)) then >*/
        if ((d__1 = stpc - *stx, abs(d__1)) < (d__2 = stpq - *stx, abs(d__2)))
                 {
/*<             stpf = stpc >*/
            stpf = stpc;
/*<          else >*/
        } else {
/*<             stpf = stpc + (stpq - stpc)/two >*/
            stpf = stpc + (stpq - stpc) / 2.;
/*<          endif >*/
        }
/*<          brackt = .true. >*/
        *brackt = TRUE_;
/*     Second case: A lower function value and derivatives of opposite */
/*     sign. The minimum is bracketed. If the cubic step is farther from */
/*     stp than the secant step, the cubic step is taken, otherwise the */
/*     secant step is taken. */
/*<       else if (sgnd .lt. zero) then >*/
    } else if (sgnd < 0.) {
/*<          theta = three*(fx - fp)/(stp - stx) + dx + dp >*/
        theta = (*fx - *fp) * 3. / (*stp - *stx) + *dx + *dp;
/*<          s = max(abs(theta),abs(dx),abs(dp)) >*/
/* Computing MAX */
        d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
                *dp);
        s = max(d__1,d__2);
/*<          gamma = s*sqrt((theta/s)**2 - (dx/s)*(dp/s)) >*/
/* Computing 2nd power */
        d__1 = theta / s;
        gamma = s * sqrt(d__1 * d__1 - *dx / s * (*dp / s));
/*<          if (stp .gt. stx) gamma = -gamma >*/
        if (*stp > *stx) {
            gamma = -gamma;
        }
/*<          p = (gamma - dp) + theta >*/
        p = gamma - *dp + theta;
/*<          q = ((gamma - dp) + gamma) + dx >*/
        q = gamma - *dp + gamma + *dx;
/*<          r = p/q >*/
        r__ = p / q;
/*<          stpc = stp + r*(stx - stp) >*/
        stpc = *stp + r__ * (*stx - *stp);
/*<          stpq = stp + (dp/(dp - dx))*(stx - stp) >*/
        stpq = *stp + *dp / (*dp - *dx) * (*stx - *stp);
/*<          if (abs(stpc-stp) .gt. abs(stpq-stp)) then >*/
        if ((d__1 = stpc - *stp, abs(d__1)) > (d__2 = stpq - *stp, abs(d__2)))
                 {
/*<             stpf = stpc >*/
            stpf = stpc;
/*<          else >*/
        } else {
/*<             stpf = stpq >*/
            stpf = stpq;
/*<          endif >*/
        }
/*<          brackt = .true. >*/
        *brackt = TRUE_;
/*     Third case: A lower function value, derivatives of the same sign, */
/*     and the magnitude of the derivative decreases. */
/*<       else if (abs(dp) .lt. abs(dx)) then >*/
    } else if (abs(*dp) < abs(*dx)) {
/*        The cubic step is computed only if the cubic tends to infinity */
/*        in the direction of the step or if the minimum of the cubic */
/*        is beyond stp. Otherwise the cubic step is defined to be the */
/*        secant step. */
/*<          theta = three*(fx - fp)/(stp - stx) + dx + dp >*/
        theta = (*fx - *fp) * 3. / (*stp - *stx) + *dx + *dp;
/*<          s = max(abs(theta),abs(dx),abs(dp)) >*/
/* Computing MAX */
        d__1 = abs(theta), d__2 = abs(*dx), d__1 = max(d__1,d__2), d__2 = abs(
                *dp);
        s = max(d__1,d__2);
/*        The case gamma = 0 only arises if the cubic does not tend */
/*        to infinity in the direction of the step. */
/*<          gamma = s*sqrt(max(zero,(theta/s)**2-(dx/s)*(dp/s))) >*/
/* Computing MAX */
/* Computing 2nd power */
        d__3 = theta / s;
        d__1 = 0., d__2 = d__3 * d__3 - *dx / s * (*dp / s);
        gamma = s * sqrt((max(d__1,d__2)));
/*<          if (stp .gt. stx) gamma = -gamma >*/
        if (*stp > *stx) {
            gamma = -gamma;
        }
/*<          p = (gamma - dp) + theta >*/
        p = gamma - *dp + theta;
/*<          q = (gamma + (dx - dp)) + gamma >*/
        q = gamma + (*dx - *dp) + gamma;
/*<          r = p/q >*/
        r__ = p / q;
/*<          if (r .lt. zero .and. gamma .ne. zero) then >*/
        if (r__ < 0. && gamma != 0.) {
/*<             stpc = stp + r*(stx - stp) >*/
            stpc = *stp + r__ * (*stx - *stp);
/*<          else if (stp .gt. stx) then >*/
        } else if (*stp > *stx) {
/*<             stpc = stpmax >*/
            stpc = *stpmax;
/*<          else >*/
        } else {
/*<             stpc = stpmin >*/
            stpc = *stpmin;
/*<          endif >*/
        }
/*<          stpq = stp + (dp/(dp - dx))*(stx - stp) >*/
        stpq = *stp + *dp / (*dp - *dx) * (*stx - *stp);
/*<          if (brackt) then >*/
        if (*brackt) {
/*           A minimizer has been bracketed. If the cubic step is */
/*           closer to stp than the secant step, the cubic step is */
/*           taken, otherwise the secant step is taken. */
/*<             if (abs(stpc-stp) .lt. abs(stpq-stp)) then >*/
            if ((d__1 = stpc - *stp, abs(d__1)) < (d__2 = stpq - *stp, abs(
                    d__2))) {
/*<                stpf = stpc >*/
                stpf = stpc;
/*<             else >*/
            } else {
/*<                stpf = stpq >*/
                stpf = stpq;
/*<             endif >*/
            }
/*<             if (stp .gt. stx) then >*/
            if (*stp > *stx) {
/*<                stpf = min(stp+p66*(sty-stp),stpf) >*/
/* Computing MIN */
                d__1 = *stp + (*sty - *stp) * .66;
                stpf = min(d__1,stpf);
/*<             else >*/
            } else {
/*<                stpf = max(stp+p66*(sty-stp),stpf) >*/
/* Computing MAX */
                d__1 = *stp + (*sty - *stp) * .66;
                stpf = max(d__1,stpf);
/*<             endif >*/
            }
/*<          else >*/
        } else {
/*           A minimizer has not been bracketed. If the cubic step is */
/*           farther from stp than the secant step, the cubic step is */
/*           taken, otherwise the secant step is taken. */
/*<             if (abs(stpc-stp) .gt. abs(stpq-stp)) then >*/
            if ((d__1 = stpc - *stp, abs(d__1)) > (d__2 = stpq - *stp, abs(
                    d__2))) {
/*<                stpf = stpc >*/
                stpf = stpc;
/*<             else >*/
            } else {
/*<                stpf = stpq >*/
                stpf = stpq;
/*<             endif >*/
            }
/*<             stpf = min(stpmax,stpf) >*/
            stpf = min(*stpmax,stpf);
/*<             stpf = max(stpmin,stpf) >*/
            stpf = max(*stpmin,stpf);
/*<          endif >*/
        }
/*     Fourth case: A lower function value, derivatives of the same sign, */
/*     and the magnitude of the derivative does not decrease. If the */
/*     minimum is not bracketed, the step is either stpmin or stpmax, */
/*     otherwise the cubic step is taken. */
/*<       else >*/
    } else {
/*<          if (brackt) then >*/
        if (*brackt) {
/*<             theta = three*(fp - fy)/(sty - stp) + dy + dp >*/
            theta = (*fp - *fy) * 3. / (*sty - *stp) + *dy + *dp;
/*<             s = max(abs(theta),abs(dy),abs(dp)) >*/
/* Computing MAX */
            d__1 = abs(theta), d__2 = abs(*dy), d__1 = max(d__1,d__2), d__2 = 
                    abs(*dp);
            s = max(d__1,d__2);
/*<             gamma = s*sqrt((theta/s)**2 - (dy/s)*(dp/s)) >*/
/* Computing 2nd power */
            d__1 = theta / s;
            gamma = s * sqrt(d__1 * d__1 - *dy / s * (*dp / s));
/*<             if (stp .gt. sty) gamma = -gamma >*/
            if (*stp > *sty) {
                gamma = -gamma;
            }
/*<             p = (gamma - dp) + theta >*/
            p = gamma - *dp + theta;
/*<             q = ((gamma - dp) + gamma) + dy >*/
            q = gamma - *dp + gamma + *dy;
/*<             r = p/q >*/
            r__ = p / q;
/*<             stpc = stp + r*(sty - stp) >*/
            stpc = *stp + r__ * (*sty - *stp);
/*<             stpf = stpc >*/
            stpf = stpc;
/*<          else if (stp .gt. stx) then >*/
        } else if (*stp > *stx) {
/*<             stpf = stpmax >*/
            stpf = *stpmax;
/*<          else >*/
        } else {
/*<             stpf = stpmin >*/
            stpf = *stpmin;
/*<          endif >*/
        }
/*<       endif >*/
    }
/*     Update the interval which contains a minimizer. */
/*<       if (fp .gt. fx) then >*/
    if (*fp > *fx) {
/*<          sty = stp >*/
        *sty = *stp;
/*<          fy = fp >*/
        *fy = *fp;
/*<          dy = dp >*/
        *dy = *dp;
/*<       else >*/
    } else {
/*<          if (sgnd .lt. zero) then >*/
        if (sgnd < 0.) {
/*<             sty = stx >*/
            *sty = *stx;
/*<             fy = fx >*/
            *fy = *fx;
/*<             dy = dx >*/
            *dy = *dx;
/*<          endif >*/
        }
/*<          stx = stp >*/
        *stx = *stp;
/*<          fx = fp >*/
        *fx = *fp;
/*<          dx = dp >*/
        *dx = *dp;
/*<       endif >*/
    }
/*     Compute the new step. */
/*<       stp = stpf >*/
    *stp = stpf;
/*<       end >*/
    return 0;
} /* dcstep_ */

/* ====================== The end of dcstep ============================== */
/*<       subroutine timer(ttime) >*/
/* Subroutine */ int timer_(doublereal *ttime)
{
#if 0
    real temp;
    extern doublereal etime_(real *);
    real tarray[2];

/*<       double precision ttime >*/
/*     ********* */

/*     Subroutine timer */

/*     This subroutine is used to determine user time. In a typical */
/*     application, the user time for a code segment requires calls */
/*     to subroutine timer to determine the initial and final time. */

/*     The subroutine statement is */

/*       subroutine timer(ttime) */

/*     where */

/*       ttime is an output variable which specifies the user time. */

/*     Argonne National Laboratory and University of Minnesota. */
/*     MINPACK-2 Project. */

/*     Modified October 1990 by Brett M. Averick. */

/*     ********** */
/*<       real temp >*/
/*<       real tarray(2) >*/
/*<       real etime >*/
/*     The first element of the array tarray specifies user time */
/*<       temp = etime(tarray)  >*/
    temp = etime_(tarray);
/*<       ttime = dble(tarray(1)) >*/
    *ttime = (doublereal) tarray[0];
/*<       return >*/
    return 0;
/*<       end >*/
#else
    *ttime = 0;
    return 0;
#endif
} /* timer_ */

/* ====================== The end of timer =============================== */
/*<       double precision function dpmeps() >*/
doublereal dpmeps_()
{
    /* Initialized data */

    static doublereal zero = 0.; /* constant */
    static doublereal one = 1.; /* constant */
    static doublereal two = 2.; /* constant */

    /* System generated locals */
    integer i__1;
    doublereal ret_val;

    /* Local variables */
    doublereal a, b;
    integer i__, it;
    doublereal beta;
    integer irnd;
    doublereal temp, temp1, betah;
    integer ibeta, negep;
    doublereal tempa;
    integer itemp;
    doublereal betain;

/*     ********** */

/*     Subroutine dpeps */

/*     This subroutine computes the machine precision parameter */
/*     dpmeps as the smallest floating point number such that */
/*     1 + dpmeps differs from 1. */

/*     This subroutine is based on the subroutine machar described in */

/*     W. J. Cody, */
/*     MACHAR: A subroutine to dynamically determine machine parameters, */
/*     ACM Transactions on Mathematical Software, 14, 1988, pages 303-311. */

/*     The subroutine statement is: */

/*       subroutine dpeps(dpmeps) */

/*     where */

/*       dpmeps is a double precision variable. */
/*         On entry dpmeps need not be specified. */
/*         On exit dpmeps is the machine precision. */

/*     MINPACK-2 Project. February 1991. */
/*     Argonne National Laboratory and University of Minnesota. */
/*     Brett M. Averick. */

/*     ******* */
/*<       integer i,ibeta,irnd,it,itemp,negep >*/
/*<    >*/
/*<       data zero,one,two /0.0d0,1.0d0,2.0d0/ >*/
/*     determine ibeta, beta ala malcolm. */
/*<       a = one >*/
    a = one;
/*<       b = one >*/
    b = one;
/*<    10 continue >*/
L10:
/*<          a = a + a >*/
    a += a;
/*<          temp = a + one >*/
    temp = a + one;
/*<          temp1 = temp - a >*/
    temp1 = temp - a;
/*<       if (temp1 - one .eq. zero) go to 10 >*/
    if (temp1 - one == zero) {
        goto L10;
    }
/*<    20 continue >*/
L20:
/*<          b = b + b >*/
    b += b;
/*<          temp = a + b >*/
    temp = a + b;
/*<          itemp = int(temp - a) >*/
    itemp = (integer) (temp - a);
/*<       if (itemp .eq. 0) go to 20 >*/
    if (itemp == 0) {
        goto L20;
    }
/*<       ibeta = itemp >*/
    ibeta = itemp;
/*<       beta = dble(ibeta) >*/
    beta = (doublereal) ibeta;
/*     determine it, irnd. */
/*<       it = 0 >*/
    it = 0;
/*<       b = one >*/
    b = one;
/*<    30 continue >*/
L30:
/*<          it = it + 1 >*/
    ++it;
/*<          b = b * beta >*/
    b *= beta;
/*<          temp = b + one >*/
    temp = b + one;
/*<          temp1 = temp - b >*/
    temp1 = temp - b;
/*<       if (temp1 - one .eq. zero) go to 30 >*/
    if (temp1 - one == zero) {
        goto L30;
    }
/*<       irnd = 0 >*/
    irnd = 0;
/*<       betah = beta/two >*/
    betah = beta / two;
/*<       temp = a + betah >*/
    temp = a + betah;
/*<       if (temp - a .ne. zero) irnd = 1 >*/
    if (temp - a != zero) {
        irnd = 1;
    }
/*<       tempa = a + beta >*/
    tempa = a + beta;
/*<       temp = tempa + betah >*/
    temp = tempa + betah;
/*<       if ((irnd .eq. 0) .and. (temp - tempa .ne. zero)) irnd = 2 >*/
    if (irnd == 0 && temp - tempa != zero) {
        irnd = 2;
    }
/*     determine dpmeps. */
/*<       negep = it + 3 >*/
    negep = it + 3;
/*<       betain = one/beta >*/
    betain = one / beta;
/*<       a = one >*/
    a = one;
/*<       do 40 i = 1, negep >*/
    i__1 = negep;
    for (i__ = 1; i__ <= i__1; ++i__) {
/*<          a = a*betain >*/
        a *= betain;
/*<    40 continue >*/
/* L40: */
    }
/*<    50 continue >*/
L50:
/*<         temp = one + a >*/
    temp = one + a;
/*<         if (temp - one .ne. zero) go to 60 >*/
    if (temp - one != zero) {
        goto L60;
    }
/*<         a = a*beta >*/
    a *= beta;
/*<         go to  50 >*/
    goto L50;
/*<    60 continue >*/
L60:
/*<       dpmeps = a >*/
    ret_val = a;
/*<       if ((ibeta .eq. 2) .or. (irnd .eq. 0)) go to 70 >*/
    if (ibeta == 2 || irnd == 0) {
        goto L70;
    }
/*<       a = (a*(one + a))/two >*/
    a = a * (one + a) / two;
/*<       temp = one + a >*/
    temp = one + a;
/*<       if (temp - one .ne. zero) dpmeps = a >*/
    if (temp - one != zero) {
        ret_val = a;
    }
/*<    70 return >*/
L70:
    return ret_val;
/*<       end >*/
} /* dpmeps_ */

/* ====================== The end of dpmeps ============================== */
/*<       subroutine dtrsl(t,ldt,n,b,job,info) >*/
/* Subroutine */ int dtrsl_(doublereal *t, integer *ldt, integer *n, 
        doublereal *b, integer *job, integer *info)
{
    /* System generated locals */
    integer t_dim1, t_offset, i__1, i__2;

    /* Local variables */
    integer j, jj, case__;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
            integer *);
    doublereal temp;
    extern /* Subroutine */ int daxpy_(integer *, doublereal *, doublereal *, 
            integer *, doublereal *, integer *);

/*<       integer ldt,n,job,info >*/
/*<       double precision t(ldt,1),b(1) >*/


/*     dtrsl solves systems of the form */

/*                   t * x = b */
/*     or */
/*                   trans(t) * x = b */

/*     where t is a triangular matrix of order n. here trans(t) */
/*     denotes the transpose of the matrix t. */

/*     on entry */

/*         t         double precision(ldt,n) */
/*                   t contains the matrix of the system. the zero */
/*                   elements of the matrix are not referenced, and */
/*                   the corresponding elements of the array can be */
/*                   used to store other information. */

/*         ldt       integer */
/*                   ldt is the leading dimension of the array t. */

/*         n         integer */
/*                   n is the order of the system. */

/*         b         double precision(n). */
/*                   b contains the right hand side of the system. */

/*         job       integer */
/*                   job specifies what kind of system is to be solved. */
/*                   if job is */

/*                        00   solve t*x=b, t lower triangular, */
/*                        01   solve t*x=b, t upper triangular, */
/*                        10   solve trans(t)*x=b, t lower triangular, */
/*                        11   solve trans(t)*x=b, t upper triangular. */

/*     on return */

/*         b         b contains the solution, if info .eq. 0. */
/*                   otherwise b is unaltered. */

/*         info      integer */
/*                   info contains zero if the system is nonsingular. */
/*                   otherwise info contains the index of */
/*                   the first zero diagonal element of t. */

/*     linpack. this version dated 08/14/78 . */
/*     g. w. stewart, university of maryland, argonne national lab. */

/*     subroutines and functions */

/*     blas daxpy,ddot */
/*     fortran mod */

/*     internal variables */

/*<       double precision ddot,temp >*/
/*<       integer case,j,jj >*/

/*     begin block permitting ...exits to 150 */

/*        check for zero diagonal elements. */

/*<          do 10 info = 1, n >*/
    /* Parameter adjustments */
    t_dim1 = *ldt;
    t_offset = 1 + t_dim1;
    t -= t_offset;
    --b;

    /* Function Body */
    i__1 = *n;
    for (*info = 1; *info <= i__1; ++(*info)) {
/*     ......exit */
/*<             if (t(info,info) .eq. 0.0d0) go to 150 >*/
        if (t[*info + *info * t_dim1] == 0.) {
            goto L150;
        }
/*<    10    continue >*/
/* L10: */
    }
/*<          info = 0 >*/
    *info = 0;

/*        determine the task and go to it. */

/*<          case = 1 >*/
    case__ = 1;
/*<          if (mod(job,10) .ne. 0) case = 2 >*/
    if (*job % 10 != 0) {
        case__ = 2;
    }
/*<          if (mod(job,100)/10 .ne. 0) case = case + 2 >*/
    if (*job % 100 / 10 != 0) {
        case__ += 2;
    }
/*<          go to (20,50,80,110), case >*/
    switch (case__) {
        case 1:  goto L20;
        case 2:  goto L50;
        case 3:  goto L80;
        case 4:  goto L110;
    }

/*        solve t*x=b for t lower triangular */

/*<    20    continue >*/
L20:
/*<             b(1) = b(1)/t(1,1) >*/
    b[1] /= t[t_dim1 + 1];
/*<             if (n .lt. 2) go to 40 >*/
    if (*n < 2) {
        goto L40;
    }
/*<             do 30 j = 2, n >*/
    i__1 = *n;
    for (j = 2; j <= i__1; ++j) {
/*<                temp = -b(j-1) >*/
        temp = -b[j - 1];
/*<                call daxpy(n-j+1,temp,t(j,j-1),1,b(j),1) >*/
        i__2 = *n - j + 1;
        daxpy_(&i__2, &temp, &t[j + (j - 1) * t_dim1], &c__1, &b[j], &c__1);
/*<                b(j) = b(j)/t(j,j) >*/
        b[j] /= t[j + j * t_dim1];
/*<    30       continue >*/
/* L30: */
    }
/*<    40       continue >*/
L40:
/*<          go to 140 >*/
    goto L140;

/*        solve t*x=b for t upper triangular. */

/*<    50    continue >*/
L50:
/*<             b(n) = b(n)/t(n,n) >*/
    b[*n] /= t[*n + *n * t_dim1];
/*<             if (n .lt. 2) go to 70 >*/
    if (*n < 2) {
        goto L70;
    }
/*<             do 60 jj = 2, n >*/
    i__1 = *n;
    for (jj = 2; jj <= i__1; ++jj) {
/*<                j = n - jj + 1 >*/
        j = *n - jj + 1;
/*<                temp = -b(j+1) >*/
        temp = -b[j + 1];
/*<                call daxpy(j,temp,t(1,j+1),1,b(1),1) >*/
        daxpy_(&j, &temp, &t[(j + 1) * t_dim1 + 1], &c__1, &b[1], &c__1);
/*<                b(j) = b(j)/t(j,j) >*/
        b[j] /= t[j + j * t_dim1];
/*<    60       continue >*/
/* L60: */
    }
/*<    70       continue >*/
L70:
/*<          go to 140 >*/
    goto L140;

/*        solve trans(t)*x=b for t lower triangular. */

/*<    80    continue >*/
L80:
/*<             b(n) = b(n)/t(n,n) >*/
    b[*n] /= t[*n + *n * t_dim1];
/*<             if (n .lt. 2) go to 100 >*/
    if (*n < 2) {
        goto L100;
    }
/*<             do 90 jj = 2, n >*/
    i__1 = *n;
    for (jj = 2; jj <= i__1; ++jj) {
/*<                j = n - jj + 1 >*/
        j = *n - jj + 1;
/*<                b(j) = b(j) - ddot(jj-1,t(j+1,j),1,b(j+1),1) >*/
        i__2 = jj - 1;
        b[j] -= ddot_(&i__2, &t[j + 1 + j * t_dim1], &c__1, &b[j + 1], &c__1);
/*<                b(j) = b(j)/t(j,j) >*/
        b[j] /= t[j + j * t_dim1];
/*<    90       continue >*/
/* L90: */
    }
/*<   100       continue >*/
L100:
/*<          go to 140 >*/
    goto L140;

/*        solve trans(t)*x=b for t upper triangular. */

/*<   110    continue >*/
L110:
/*<             b(1) = b(1)/t(1,1) >*/
    b[1] /= t[t_dim1 + 1];
/*<             if (n .lt. 2) go to 130 >*/
    if (*n < 2) {
        goto L130;
    }
/*<             do 120 j = 2, n >*/
    i__1 = *n;
    for (j = 2; j <= i__1; ++j) {
/*<                b(j) = b(j) - ddot(j-1,t(1,j),1,b(1),1) >*/
        i__2 = j - 1;
        b[j] -= ddot_(&i__2, &t[j * t_dim1 + 1], &c__1, &b[1], &c__1);
/*<                b(j) = b(j)/t(j,j) >*/
        b[j] /= t[j + j * t_dim1];
/*<   120       continue >*/
/* L120: */
    }
/*<   130       continue >*/
L130:
/*<   140    continue >*/
L140:
/*<   150 continue >*/
L150:
/*<       return >*/
    return 0;
/*<       end >*/
} /* dtrsl_ */

#ifdef __cplusplus
        }
#endif