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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Namespace MathEx.LBFGSB

    '/*************************************************************************
    '   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.

    '   Contributors:
    '       * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
    '         pseudocode.

    '   This software is freely available, but we  expect  that  all  publications
    '   describing  work using this software, or all commercial products using it,
    '   quote at least one of the references given below:
    '       * R. H. Byrd, P. Lu and J. Nocedal.  A Limited  Memory  Algorithm  for
    '         Bound Constrained Optimization, (1995), SIAM Journal  on  Scientific
    '         and Statistical Computing , 16, 5, pp. 1190-1208.
    '       * C. Zhu, R.H. Byrd and J. Nocedal. L-BFGS-B: Algorithm 778: L-BFGS-B,
    '         FORTRAN routines for  large  scale  bound  constrained  optimization
    '         (1997), ACM Transactions on Mathematical Software,  Vol 23,  Num. 4,
    '         pp. 550 - 560.
    '   *************************************************************************/

    Public Class lbfgsb

        Delegate Sub funcgraddelegate(ByVal x As Double(), ByRef f As Double, ByRef g As Double())
        Delegate Sub newiterdelegate(ByRef x As Double(), ByVal f As Double, ByRef g As Double(), ByRef abort As Boolean)
        Public fc As funcgraddelegate
        Public fc2 As newiterdelegate

        Sub New()

        End Sub

        Sub DefineFuncGradDelegate(ByVal fg As funcgraddelegate)
            Me.fc = fg
        End Sub

        Sub funcgrad(ByVal x As Double(), ByRef f As Double, ByRef g As Double())
            fc.Invoke(x, f, g)
        End Sub

        Sub DefineNewIterDelegate(ByVal fg As newiterdelegate)
            Me.fc2 = fg
        End Sub

        Private Sub lbfgsbnewiteration(ByRef x As Double(), ByVal f As Double, ByRef g As Double(), ByRef abort As Boolean)
            fc2.Invoke(x, f, g, abort)
        End Sub

        '/*************************************************************************
        'The  subroutine  minimizes  the  function  F(x) of N arguments with simple
        'constraints using a quasi-Newton method (LBFGS scheme) which is  optimized
        'to use a minimum amount of memory.

        'The subroutine generates the approximation of an inverse Hessian matrix by
        'using information about the last M steps of the algorithm (instead  of N).
        'It lessens a required amount of memory from a value  of  order  N^2  to  a
        'value of order 2*N*M.

        'This subroutine uses the FuncGrad subroutine which calculates the value of
        'the function F and gradient G in point X. The programmer should define the
        'FuncGrad subroutine by himself.  It should be noted  that  the  subroutine
        'doesn't need to waste  time for memory allocation of array G, because  the
        'memory is allocated in calling the  subroutine.  Setting  a  dimension  of
        'array G each time when calling a subroutine will excessively slow down  an
        'algorithm.

        'The programmer could also redefine the LBFGSNewIteration subroutine  which
        'is called on each new step. The current point X, the function value F  and
        'the gradient G are passed  into  this  subroutine.  It  is  reasonable  to
        'redefine the subroutine for better debugging, for  example,  to  visualize
        'the solution process.

        'Input parameters:
        '    N       -   problem dimension. N>0
        '    M       -   number of  corrections  in  the  BFGS  scheme  of  Hessian
        '                approximation  update.  Recommended value:  3<=M<=7.   The
        '                smaller value causes worse convergence,  the  bigger  will
        '                not  cause  a  considerably  better  convergence, but will
        '                cause a fall in the performance. M<=N.
        '    X       -   initial solution approximation.
        '                Array whose index ranges from 1 to N.
        '    EpsG    -   positive number which defines a precision of  search.  The
        '                subroutine finishes its work if the condition ||G|| < EpsG
        '                is satisfied, where ||.|| means Euclidian norm, G - gradient
        '                projection onto a feasible set, X - current approximation.
        '    EpsF    -   positive number which defines a precision of  search.  The
        '                subroutine  finishes  its  work if on iteration number k+1
        '                the condition |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
        '                is satisfied.
        '    EpsX    -   positive number which defines a precision of  search.  The
        '                subroutine  finishes  its  work if on iteration number k+1
        '                the condition |X(k+1)-X(k)| <= EpsX is satisfied.
        '    MaxIts  -   maximum number of iterations.
        '                If MaxIts=0, the number of iterations is unlimited.
        '    NBD     -   constraint type. If NBD(i) is equal to:
        '                * 0, X(i) has no constraints,
        '                * 1, X(i) has only lower boundary,
        '                * 2, X(i) has both lower and upper boundaries,
        '                * 3, X(i) has only upper boundary,
        '                Array whose index ranges from 1 to N.
        '    L       -   lower boundaries of X(i) variables.
        '                Array whose index ranges from 1 to N.
        '    U       -   upper boundaries of X(i) variables.
        '                Array whose index ranges from 1 to N.

        'Output parameters:
        '    X       -   solution approximation.
        'Array whose index ranges from 1 to N.
        '    Info    -   a return code:
        '                    * -2 unknown internal error,
        '                    * -1 wrong parameters were specified,
        '                    * 0 interrupted by user,
        '                    * 1 relative function decreasing is less or equal to EpsF,
        '                    * 2 step is less or equal to EpsX,
        '                    * 4 gradient norm is less or equal to EpsG,
        '                    * 5 number of iterations exceeds MaxIts.

        'FuncGrad routine description. User-defined.
        'Input parameters:
        '    X   -   array whose index ranges from 1 to N.
        'Output parameters:
        '    F   -   function value at X.
        '    G   -   function gradient.
        '            Array whose index ranges from 1 to N.
        'The memory for array G has already been allocated in the calling subroutine,
        'and it isn't necessary to allocate it in the FuncGrad subroutine.

        '    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.
        '*************************************************************************/

        ' Methods
        Private Function additionallbfgsbstoppingcriterion(ByVal iter As Integer, ByRef x As Double(), ByVal f As Double, ByRef g As Double()) As Boolean
            Return False
        End Function

        Private Sub lbfgsbactive(ByVal n As Integer, ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef x As Double(), ByRef iwhere As Integer(), ByRef prjctd As Boolean, ByRef cnstnd As Boolean, ByRef boxed As Boolean)
            Dim num As Integer = 0
            Dim index As Integer = 0
            num = 0
            prjctd = False
            cnstnd = False
            boxed = True
            index = 1
            Do While (index <= n)
                If (nbd(index) > 0) Then
                    If ((nbd(index) <= 2) And (x(index) <= l(index))) Then
                        If (x(index) < l(index)) Then
                            prjctd = True
                            x(index) = l(index)
                        End If
                        num += 1
                    ElseIf ((nbd(index) >= 2) And (x(index) >= u(index))) Then
                        If (x(index) > u(index)) Then
                            prjctd = True
                            x(index) = u(index)
                        End If
                        num += 1
                    End If
                End If
                index += 1
            Loop
            index = 1
            Do While (index <= n)
                If (nbd(index) <> 2) Then
                    boxed = False
                End If
                If (nbd(index) = 0) Then
                    iwhere(index) = -1
                Else
                    cnstnd = True
                    If ((nbd(index) = 2) And ((u(index) - l(index)) <= 0)) Then
                        iwhere(index) = 3
                    Else
                        iwhere(index) = 0
                    End If
                End If
                index += 1
            Loop
        End Sub

        Private Sub lbfgsbbmv(ByVal m As Integer, ByRef sy As Double(,), ByRef wt As Double(,), ByVal col As Integer, ByRef v As Double(), ByRef p As Double(), ByRef info As Integer, ByRef workvec As Double())
            Dim index As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Double = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            If (col <> 0) Then
                p((col + 1)) = v((col + 1))
                index = 2
                Do While (index <= col)
                    num3 = (col + index)
                    num4 = 0
                    num2 = 1
                    Do While (num2 <= (index - 1))
                        num4 = (num4 + ((sy(index, num2) * v(num2)) / sy(num2, num2)))
                        num2 += 1
                    Loop
                    p(num3) = (v(num3) + num4)
                    index += 1
                Loop
                num6 = ((col + 1) - 1)
                num5 = 1
                Do While (num5 <= col)
                    workvec(num5) = p((num5 + num6))
                    num5 += 1
                Loop
                Me.lbfgsbdtrsl(wt, col, workvec, 11, info)
                num6 = (1 - (col + 1))
                num5 = (col + 1)
                Do While (num5 <= (col + col))
                    p(num5) = workvec((num5 + num6))
                    num5 += 1
                Loop
                If (info = 0) Then
                    index = 1
                    Do While (index <= col)
                        p(index) = (v(index) / Math.Sqrt(sy(index, index)))
                        index += 1
                    Loop
                    num6 = ((col + 1) - 1)
                    num5 = 1
                    Do While (num5 <= col)
                        workvec(num5) = p((num5 + num6))
                        num5 += 1
                    Loop
                    Me.lbfgsbdtrsl(wt, col, workvec, 1, info)
                    num6 = (1 - (col + 1))
                    num5 = (col + 1)
                    Do While (num5 <= (col + col))
                        p(num5) = workvec((num5 + num6))
                        num5 += 1
                    Loop
                    If (info = 0) Then
                        index = 1
                        Do While (index <= col)
                            p(index) = -(p(index) / Math.Sqrt(sy(index, index)))
                            index += 1
                        Loop
                        index = 1
                        Do While (index <= col)
                            num4 = 0
                            num2 = (index + 1)
                            Do While (num2 <= col)
                                num4 = (num4 + ((sy(num2, index) * p((col + num2))) / sy(index, index)))
                                num2 += 1
                            Loop
                            p(index) = (p(index) + num4)
                            index += 1
                        Loop
                    End If
                End If
            End If
        End Sub

        Private Sub lbfgsbcauchy(ByVal n As Integer, ByRef x As Double(), ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef g As Double(), ByRef iorder As Integer(), ByRef iwhere As Integer(), ByRef t As Double(), ByRef d As Double(), ByRef xcp As Double(), ByVal m As Integer, ByRef wy As Double(,), ByRef ws As Double(,), ByRef sy As Double(,), ByRef wt As Double(,), ByVal theta As Double, ByVal col As Integer, ByVal head As Integer, ByRef p As Double(), ByRef c As Double(), ByRef wbp As Double(), ByRef v As Double(), ByRef nint As Integer, ByRef sg As Double(), ByRef yg As Double(), ByVal sbgnrm As Double, ByRef info As Integer, ByRef workvec As Double())
            Dim flag As Boolean = False
            Dim flag2 As Boolean = False
            Dim flag3 As Boolean = False
            Dim index As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            Dim num7 As Integer = 0
            Dim num8 As Integer = 0
            Dim num9 As Integer = 0
            Dim num10 As Integer = 0
            Dim num11 As Double = 0
            Dim num12 As Double = 0
            Dim num13 As Double = 0
            Dim num14 As Double = 0
            Dim num15 As Double = 0
            Dim num16 As Double = 0
            Dim num17 As Double = 0
            Dim num18 As Double = 0
            Dim num19 As Double = 0
            Dim num20 As Double = 0
            Dim num21 As Double = 0
            Dim num22 As Double = 0
            Dim num23 As Double = 0
            Dim num24 As Double = 0
            Dim num25 As Double = 0
            Dim num26 As Double = 0
            Dim num27 As Double = 0
            Dim num28 As Double = 0
            Dim num29 As Double = 0
            Dim num30 As Integer = 0
            If (sbgnrm <= 0) Then
                num30 = 1
                Do While (num30 <= n)
                    xcp(num30) = x(num30)
                    num30 += 1
                Loop
                Return
            End If
            flag3 = True
            num4 = (n + 1)
            num5 = 0
            num9 = 0
            num19 = 0
            num3 = (2 * col)
            num11 = 0
            index = 1
            Do While (index <= num3)
                p(index) = 0
                index += 1
            Loop
            index = 1
            Do While (index <= n)
                num27 = -g(index)
                If ((iwhere(index) <> 3) And (iwhere(index) <> -1)) Then
                    num21 = 0
                    num20 = 0
                    If (nbd(index) <= 2) Then
                        num21 = (x(index) - l(index))
                    End If
                    If (nbd(index) >= 2) Then
                        num20 = (u(index) - x(index))
                    End If
                    flag = ((nbd(index) <= 2) And (num21 <= 0))
                    flag2 = ((nbd(index) >= 2) And (num20 <= 0))
                    iwhere(index) = 0
                    If flag Then
                        If (num27 <= 0) Then
                            iwhere(index) = 1
                        End If
                    ElseIf flag2 Then
                        If (num27 >= 0) Then
                            iwhere(index) = 2
                        End If
                    ElseIf (Math.Abs(num27) <= 0) Then
                        iwhere(index) = -3
                    End If
                End If
                num6 = head
                If ((iwhere(index) <> 0) And (iwhere(index) <> -1)) Then
                    d(index) = 0
                Else
                    d(index) = num27
                    num11 = (num11 - (num27 * num27))
                    num2 = 1
                    Do While (num2 <= col)
                        p(num2) = (p(num2) + (wy(index, num6) * num27))
                        p((col + num2)) = (p((col + num2)) + (ws(index, num6) * num27))
                        num6 = ((num6 Mod m) + 1)
                        num2 += 1
                    Loop
                    If (((nbd(index) <= 2) And (nbd(index) <> 0)) And (num27 < 0)) Then
                        num5 += 1
                        iorder(num5) = index
                        t(num5) = (num21 / -num27)
                        If ((num5 = 1) Or (t(num5) < num19)) Then
                            num19 = t(num5)
                            num9 = num5
                        End If
                    ElseIf ((nbd(index) >= 2) And (num27 > 0)) Then
                        num5 += 1
                        iorder(num5) = index
                        t(num5) = (num20 / num27)
                        If ((num5 = 1) Or (t(num5) < num19)) Then
                            num19 = t(num5)
                            num9 = num5
                        End If
                    Else
                        num4 -= 1
                        iorder(num4) = index
                        If (Math.Abs(num27) > 0) Then
                            flag3 = False
                        End If
                    End If
                End If
                index += 1
            Loop
            If (theta <> 1) Then
                num30 = (col + 1)
                Do While (num30 <= (col + col))
                    p(num30) = (theta * p(num30))
                    num30 += 1
                Loop
            End If
            num30 = 1
            Do While (num30 <= n)
                xcp(num30) = x(num30)
                num30 += 1
            Loop
            If ((num5 = 0) And (num4 = (n + 1))) Then
                Return
            End If
            num2 = 1
            Do While (num2 <= num3)
                c(num2) = 0
                num2 += 1
            Loop
            num12 = -(theta * num11)
            num28 = num12
            If (col > 0) Then
                Me.lbfgsbbmv(m, sy, wt, col, p, v, info, workvec)
                If (Not info = 0) Then
                    Return
                End If
                num29 = 0
                num30 = 1
                Do While (num30 <= num3)
                    num29 = (num29 + (v(num30) * p(num30)))
                    num30 += 1
                Loop
                num12 = (num12 - num29)
            End If
            num14 = -(num11 / num12)
            num15 = 0
            nint = 1
            If (num5 = 0) Then
                GoTo Label_07E7
            End If
            num8 = num5
            num10 = 1
            num25 = 0
Label_04DC:
            num26 = num25
            If (num10 = 1) Then
                num25 = num19
                num7 = iorder(num9)
            Else
                If ((num10 = 2) AndAlso (num9 <> num5)) Then
                    t(num9) = t(num5)
                    iorder(num9) = iorder(num5)
                End If
                Me.lbfgsbhpsolb(num8, t, iorder, num10 - 2)
                num25 = t(num8)
                num7 = iorder(num8)
            End If
            num13 = (num25 - num26)
            If (num14 >= num13) Then
                num15 = (num15 + num13)
                num8 -= 1
                num10 += 1
                num16 = d(num7)
                d(num7) = 0
                If (num16 > 0) Then
                    num17 = (u(num7) - x(num7))
                    xcp(num7) = u(num7)
                    iwhere(num7) = 2
                Else
                    num17 = (l(num7) - x(num7))
                    xcp(num7) = l(num7)
                    iwhere(num7) = 1
                End If
                If ((num8 = 0) And (num5 = n)) Then
                    num14 = num13
                    If (col > 0) Then
                        num30 = 1
                        Do While (num30 <= num3)
                            c(num30) = (c(num30) + (num14 * p(num30)))
                            num30 += 1
                        Loop
                    End If
                    Return
                End If
                nint += 1
                num18 = Math.Sqrt(num16)
                num11 = (((num11 + (num13 * num12)) + num18) - ((theta * num16) * num17))
                num12 = (num12 - (theta * num18))
                If (col > 0) Then
                    num30 = 1
                    Do While (num30 <= num3)
                        c(num30) = (c(num30) + (num13 * p(num30)))
                        num30 += 1
                    Loop
                    num6 = head
                    num2 = 1
                    Do While (num2 <= col)
                        wbp(num2) = wy(num7, num6)
                        wbp((col + num2)) = (theta * ws(num7, num6))
                        num6 = ((num6 Mod m) + 1)
                        num2 += 1
                    Loop
                    Me.lbfgsbbmv(m, sy, wt, col, wbp, v, info, workvec)
                    If (Not info = 0) Then
                        Return
                    End If
                    num22 = 0
                    num30 = 1
                    Do While (num30 <= num3)
                        num22 = (num22 + (c(num30) * v(num30)))
                        num30 += 1
                    Loop
                    num23 = 0
                    num30 = 1
                    Do While (num30 <= num3)
                        num23 = (num23 + (p(num30) * v(num30)))
                        num30 += 1
                    Loop
                    num24 = 0
                    num30 = 1
                    Do While (num30 <= num3)
                        num24 = (num24 + (wbp(num30) * v(num30)))
                        num30 += 1
                    Loop
                    num30 = 1
                    Do While (num30 <= num3)
                        p(num30) = (p(num30) - (num16 * wbp(num30)))
                        num30 += 1
                    Loop
                    num11 = (num11 + (num16 * num22))
                    num12 = ((num12 + ((2 * num16) * num23)) - (num18 * num24))
                End If
                num12 = Math.Max((0.0000000000000005 * num28), num12)
                If (num8 > 0) Then
                    num14 = -(num11 / num12)
                    GoTo Label_04DC
                End If
                If flag3 Then
                    num11 = 0
                    num12 = 0
                    num14 = 0
                Else
                    num14 = -(num11 / num12)
                End If
            End If
Label_07E7:
            If (num14 <= 0) Then
                num14 = 0
            End If
            num15 = (num15 + num14)
            num30 = 1
            Do While (num30 <= n)
                xcp(num30) = (xcp(num30) + (num15 * d(num30)))
                num30 += 1
            Loop
            If (col > 0) Then
                num30 = 1
                Do While (num30 <= num3)
                    c(num30) = (c(num30) + (num14 * p(num30)))
                    num30 += 1
                Loop
            End If
        End Sub

        Private Sub lbfgsbcmprlb(ByVal n As Integer, ByVal m As Integer, ByRef x As Double(), ByRef g As Double(), ByRef ws As Double(,), ByRef wy As Double(,), ByRef sy As Double(,), ByRef wt As Double(,), ByRef z As Double(), ByRef r As Double(), ByRef wa As Double(), ByRef index As Integer(), ByVal theta As Double, ByVal col As Integer, ByVal head As Integer, ByVal nfree As Integer, ByVal cnstnd As Boolean, ByRef info As Integer, ByRef workvec As Double(), ByRef workvec2 As Double())
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Double = 0
            Dim num6 As Double = 0
            Dim num7 As Integer = 0
            Dim num8 As Integer = 0
            If (Not cnstnd And (col > 0)) Then
                num = 1
                Do While (num <= n)
                    r(num) = -g(num)
                    num += 1
                Loop
            Else
                num = 1
                Do While (num <= nfree)
                    num3 = index(num)
                    r(num) = (-(theta * (z(num3) - x(num3))) - g(num3))
                    num += 1
                Loop
                num8 = (((2 * m) + 1) - 1)
                num7 = 1
                Do While (num7 <= (2 * m))
                    workvec2(num7) = wa((num7 + num8))
                    num7 += 1
                Loop
                Me.lbfgsbbmv(m, sy, wt, col, workvec2, wa, info, workvec)
                num8 = (1 - ((2 * m) + 1))
                num7 = ((2 * m) + 1)
                Do While (num7 <= (4 * m))
                    wa(num7) = workvec2((num7 + num8))
                    num7 += 1
                Loop
                If (Not info = 0) Then
                    info = -8
                Else
                    num4 = head
                    num2 = 1
                    Do While (num2 <= col)
                        num5 = wa(num2)
                        num6 = (theta * wa((col + num2)))
                        num = 1
                        Do While (num <= nfree)
                            num3 = index(num)
                            r(num) = ((r(num) + (wy(num3, num4) * num5)) + (ws(num3, num4) * num6))
                            num += 1
                        Loop
                        num4 = ((num4 Mod m) + 1)
                        num2 += 1
                    Loop
                End If
            End If
        End Sub

        Private Sub lbfgsbdcsrch(ByVal f As Double, ByVal g As Double, ByRef stp As Double, ByVal ftol As Double, ByVal gtol As Double, ByVal xtol As Double, ByVal stpmin As Double, ByVal stpmax As Double, ByRef task As Integer, ByRef isave As Integer(), ByRef dsave As Double(), ByRef addinfo As Integer)
            Dim brackt As Boolean = False
            Dim num As Integer = 0
            Dim num2 As Double = 0
            Dim num3 As Double = 0
            Dim fp As Double = 0
            Dim fx As Double = 0
            Dim num6 As Double = 0
            Dim fy As Double = 0
            Dim num8 As Double = 0
            Dim num9 As Double = 0
            Dim num10 As Double = 0
            Dim dp As Double = 0
            Dim dx As Double = 0
            Dim num13 As Double = 0
            Dim dy As Double = 0
            Dim num15 As Double = 0
            Dim stx As Double = 0
            Dim sty As Double = 0
            Dim num18 As Double = 0
            Dim num19 As Double = 0
            Dim num20 As Double = 0
            Dim num21 As Double = 0
            Dim num22 As Double = 0
            Dim num23 As Double = 0
            num22 = 1.1
            num23 = 4
            If (task = 0) Then
                If (stp < stpmin) Then
                    task = 2
                    addinfo = 0
                End If
                If (stp > stpmax) Then
                    task = 2
                    addinfo = 0
                End If
                If (g >= 0) Then
                    task = 2
                    addinfo = 0
                End If
                If (ftol < 0) Then
                    task = 2
                    addinfo = 0
                End If
                If (gtol < 0) Then
                    task = 2
                    addinfo = 0
                End If
                If (xtol < 0) Then
                    task = 2
                    addinfo = 0
                End If
                If (stpmin < 0) Then
                    task = 2
                    addinfo = 0
                End If
                If (stpmax < stpmin) Then
                    task = 2
                    addinfo = 0
                End If
                If (task = 2) Then
                    Return
                End If
                brackt = False
                num = 1
                num2 = f
                num9 = g
                num10 = (ftol * num9)
                num20 = (stpmax - stpmin)
                num21 = (num20 / 0.5)
                stx = 0
                fx = num2
                dx = num9
                sty = 0
                fy = num2
                dy = num9
                num18 = 0
                num19 = (stp + (num23 * stp))
                task = 1
            Else
                If (isave(1) = 1) Then
                    brackt = True
                Else
                    brackt = False
                End If
                num = isave(2)
                num9 = dsave(1)
                num10 = dsave(2)
                dx = dsave(3)
                dy = dsave(4)
                num2 = dsave(5)
                fx = dsave(6)
                fy = dsave(7)
                stx = dsave(8)
                sty = dsave(9)
                num18 = dsave(10)
                num19 = dsave(11)
                num20 = dsave(12)
                num21 = dsave(13)
                num3 = (num2 + (stp * num10))
                If (((num = 1) And (f <= num3)) And (g >= 0)) Then
                    num = 2
                End If
                If (brackt And ((stp <= num18) Or (stp >= num19))) Then
                    task = 3
                    addinfo = 1
                End If
                If (brackt And ((num19 - num18) <= (xtol * num19))) Then
                    task = 3
                    addinfo = 2
                End If
                If (((stp = stpmax) And (f <= num3)) And (g <= num10)) Then
                    task = 3
                    addinfo = 3
                End If
                If ((stp = stpmin) And ((f > num3) Or (g >= num10))) Then
                    task = 3
                    addinfo = 4
                End If
                If ((f <= num3) And (Math.Abs(g) <= (gtol * -num9))) Then
                    task = 4
                    addinfo = -1
                End If
                If Not ((task = 3) Or (task = 4)) Then
                    If (((num = 1) And (f <= fx)) And (f > num3)) Then
                        fp = (f - (stp * num10))
                        num6 = (fx - (stx * num10))
                        num8 = (fy - (sty * num10))
                        dp = (g - num10)
                        num13 = (dx - num10)
                        num15 = (dy - num10)
                        Me.lbfgsbdcstep(stx, num6, num13, sty, num8, num15, stp, fp, dp, brackt, num18, num19)
                        fx = (num6 + (stx * num10))
                        fy = (num8 + (sty * num10))
                        dx = (num13 + num10)
                        dy = (num15 + num10)
                    Else
                        Me.lbfgsbdcstep(stx, fx, dx, sty, fy, dy, stp, f, g, brackt, num18, num19)
                    End If
                    If brackt Then
                        If (Math.Abs(Convert.ToDouble((sty - stx))) >= (0.66 * num21)) Then
                            stp = (stx + (0.5 * (sty - stx)))
                        End If
                        num21 = num20
                        num20 = Math.Abs(Convert.ToDouble((sty - stx)))
                    End If
                    If brackt Then
                        num18 = Math.Min(stx, sty)
                        num19 = Math.Max(stx, sty)
                    Else
                        num18 = (stp + (num22 * (stp - stx)))
                        num19 = (stp + (num23 * (stp - stx)))
                    End If
                    stp = Math.Max(stp, stpmin)
                    stp = Math.Min(stp, stpmax)
                    If ((brackt And ((stp <= num18) Or (stp >= num19))) Or (brackt And ((num19 - num18) <= (xtol * num19)))) Then
                        stp = stx
                    End If
                    task = 1
                End If
            End If
            If brackt Then
                isave(1) = 1
            Else
                isave(1) = 0
            End If
            isave(2) = num
            dsave(1) = num9
            dsave(2) = num10
            dsave(3) = dx
            dsave(4) = dy
            dsave(5) = num2
            dsave(6) = fx
            dsave(7) = fy
            dsave(8) = stx
            dsave(9) = sty
            dsave(10) = num18
            dsave(11) = num19
            dsave(12) = num20
            dsave(13) = num21
        End Sub

        Private Sub lbfgsbdcstep(ByRef stx As Double, ByRef fx As Double, ByRef dx As Double, ByRef sty As Double, ByRef fy As Double, ByRef dy As Double, ByRef stp As Double, ByVal fp As Double, ByVal dp As Double, ByRef brackt As Boolean, ByVal stpmin As Double, ByVal stpmax As Double)
            Dim num As Double = 0
            Dim num2 As Double = 0
            Dim num3 As Double = 0
            Dim num4 As Double = 0
            Dim num5 As Double = 0
            Dim num6 As Double = 0
            Dim num7 As Double = 0
            Dim num8 As Double = 0
            Dim num9 As Double = 0
            Dim num10 As Double = 0
            num6 = (dp * (dx / Math.Abs(dx)))
            If (fp > fx) Then
                num10 = ((((3 * (fx - fp)) / (stp - stx)) + dx) + dp)
                num5 = Math.Max(Math.Abs(num10), Math.Max(Math.Abs(dx), Math.Abs(dp)))
                num = (num5 * Math.Sqrt((Math.Sqrt((num10 / num5)) - ((dx / num5) * (dp / num5)))))
                If (stp < stx) Then
                    num = -num
                End If
                num2 = ((num - dx) + num10)
                num3 = (((num - dx) + num) + dp)
                num4 = (num2 / num3)
                num7 = (stx + (num4 * (stp - stx)))
                num9 = (stx + (((dx / (((fx - fp) / (stp - stx)) + dx)) / 2) * (stp - stx)))
                If (Math.Abs(Convert.ToDouble((num7 - stx))) < Math.Abs(Convert.ToDouble((num9 - stx)))) Then
                    num8 = num7
                Else
                    num8 = (num7 + ((num9 - num7) / 2))
                End If
                brackt = True
            ElseIf (num6 < 0) Then
                num10 = ((((3 * (fx - fp)) / (stp - stx)) + dx) + dp)
                num5 = Math.Max(Math.Abs(num10), Math.Max(Math.Abs(dx), Math.Abs(dp)))
                num = (num5 * Math.Sqrt((Math.Sqrt((num10 / num5)) - ((dx / num5) * (dp / num5)))))
                If (stp > stx) Then
                    num = -num
                End If
                num2 = ((num - dp) + num10)
                num3 = (((num - dp) + num) + dx)
                num4 = (num2 / num3)
                num7 = (stp + (num4 * (stx - stp)))
                num9 = (stp + ((dp / (dp - dx)) * (stx - stp)))
                If (Math.Abs(Convert.ToDouble((num7 - stp))) > Math.Abs(Convert.ToDouble((num9 - stp)))) Then
                    num8 = num7
                Else
                    num8 = num9
                End If
                brackt = True
            ElseIf (Math.Abs(dp) < Math.Abs(dx)) Then
                num10 = ((((3 * (fx - fp)) / (stp - stx)) + dx) + dp)
                num5 = Math.Max(Math.Abs(num10), Math.Max(Math.Abs(dx), Math.Abs(dp)))
                num = (num5 * Math.Sqrt(Math.Max(Convert.ToDouble(0), Convert.ToDouble((Math.Sqrt((num10 / num5)) - ((dx / num5) * (dp / num5)))))))
                If (stp > stx) Then
                    num = -num
                End If
                num2 = ((num - dp) + num10)
                num3 = ((num + (dx - dp)) + num)
                num4 = (num2 / num3)
                If ((num4 < 0) And (num <> 0)) Then
                    num7 = (stp + (num4 * (stx - stp)))
                ElseIf (stp > stx) Then
                    num7 = stpmax
                Else
                    num7 = stpmin
                End If
                num9 = (stp + ((dp / (dp - dx)) * (stx - stp)))
                If brackt Then
                    If (Math.Abs(Convert.ToDouble((num7 - stp))) < Math.Abs(Convert.ToDouble((num9 - stp)))) Then
                        num8 = num7
                    Else
                        num8 = num9
                    End If
                    If (stp > stx) Then
                        num8 = Math.Min((stp + (0.66 * (sty - stp))), num8)
                    Else
                        num8 = Math.Max((stp + (0.66 * (sty - stp))), num8)
                    End If
                Else
                    If (Math.Abs(Convert.ToDouble((num7 - stp))) > Math.Abs(Convert.ToDouble((num9 - stp)))) Then
                        num8 = num7
                    Else
                        num8 = num9
                    End If
                    num8 = Math.Min(stpmax, num8)
                    num8 = Math.Max(stpmin, num8)
                End If
            ElseIf brackt Then
                num10 = ((((3 * (fp - fy)) / (sty - stp)) + dy) + dp)
                num5 = Math.Max(Math.Abs(num10), Math.Max(Math.Abs(dy), Math.Abs(dp)))
                num = (num5 * Math.Sqrt((Math.Sqrt((num10 / num5)) - ((dy / num5) * (dp / num5)))))
                If (stp > sty) Then
                    num = -num
                End If
                num2 = ((num - dp) + num10)
                num3 = (((num - dp) + num) + dy)
                num4 = (num2 / num3)
                num7 = (stp + (num4 * (sty - stp)))
                num8 = num7
            ElseIf (stp > stx) Then
                num8 = stpmax
            Else
                num8 = stpmin
            End If
            If (fp > fx) Then
                sty = stp
                fy = fp
                dy = dp
            Else
                If (num6 < 0) Then
                    sty = stx
                    fy = fx
                    dy = dx
                End If
                stx = stp
                fx = fp
                dx = dp
            End If
            stp = num8
        End Sub

        Private Function lbfgsbdpofa(ByRef a As Double(,), ByVal n As Integer) As Boolean
            Dim num As Double = 0
            Dim d As Double = 0
            Dim num3 As Double = 0
            Dim num4 As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            Dim num7 As Integer = 0
            num4 = 1
            Do While (num4 <= n)
                d = 0
                num5 = (num4 - 1)
                If (num5 >= 1) Then
                    num6 = 1
                    Do While (num6 <= num5)
                        num3 = 0
                        num7 = 1
                        Do While (num7 <= (num6 - 1))
                            num3 = (num3 + (a(num7, num6) * a(num7, num4)))
                            num7 += 1
                        Loop
                        num = (a(num6, num4) - num3)
                        num = (num / a(num6, num6))
                        a(num6, num4) = num
                        d = (d + (num * num))
                        num6 += 1
                    Loop
                End If
                d = (a(num4, num4) - d)
                If (d <= 0) Then
                    Return False
                End If
                a(num4, num4) = Math.Sqrt(d)
                num4 += 1
            Loop
            Return True
        End Function

        Private Sub lbfgsbdtrsl(ByRef t As Double(,), ByVal n As Integer, ByRef b As Double(), ByVal job As Integer, ByRef info As Integer)
            Dim num As Double = 0
            Dim num2 As Double = 0
            Dim num3 As Integer = 0
            Dim index As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            index = 1
            Do While (index <= n)
                If (t(index, index) = 0) Then
                    info = index
                    Return
                End If
                index += 1
            Loop
            info = 0
            num3 = 1
            If ((job Mod 10) <> 0) Then
                num3 = 2
            End If
            If (((job Mod 100) / 10) <> 0) Then
                num3 = (num3 + 2)
            End If
            If (num3 = 1) Then
                b(1) = (b(1) / t(1, 1))
                If (n >= 2) Then
                    index = 2
                    Do While (index <= n)
                        num = -b((index - 1))
                        num6 = index
                        Do While (num6 <= n)
                            b(num6) = (b(num6) + (num * t(num6, (index - 1))))
                            num6 += 1
                        Loop
                        b(index) = (b(index) / t(index, index))
                        index += 1
                    Loop
                End If
            ElseIf (num3 = 2) Then
                b(n) = (b(n) / t(n, n))
                If (n >= 2) Then
                    num5 = 2
                    Do While (num5 <= n)
                        index = ((n - num5) + 1)
                        num = -b((index + 1))
                        num6 = 1
                        Do While (num6 <= index)
                            b(num6) = (b(num6) + (num * t(num6, (index + 1))))
                            num6 += 1
                        Loop
                        b(index) = (b(index) / t(index, index))
                        num5 += 1
                    Loop
                End If
            ElseIf (num3 = 3) Then
                b(n) = (b(n) / t(n, n))
                If (n >= 2) Then
                    num5 = 2
                    Do While (num5 <= n)
                        index = ((n - num5) + 1)
                        num2 = 0
                        num6 = (index + 1)
                        Do While (num6 <= ((((index + 1) + num5) - 1) - 1))
                            num2 = (num2 + (t(num6, index) * b(num6)))
                            num6 += 1
                        Loop
                        b(index) = (b(index) - num2)
                        b(index) = (b(index) / t(index, index))
                        num5 += 1
                    Loop
                End If
            ElseIf (num3 = 4) Then
                b(1) = (b(1) / t(1, 1))
                If (n >= 2) Then
                    index = 2
                    Do While (index <= n)
                        num2 = 0
                        num6 = 1
                        Do While (num6 <= (index - 1))
                            num2 = (num2 + (t(num6, index) * b(num6)))
                            num6 += 1
                        Loop
                        b(index) = (b(index) - num2)
                        b(index) = (b(index) / t(index, index))
                        index += 1
                    Loop
                End If
            End If
        End Sub

        Private Sub lbfgsberrclb(ByVal n As Integer, ByVal m As Integer, ByVal factr As Double, ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef task As Integer, ByRef info As Integer, ByRef k As Integer)
            Dim index As Integer = 0
            If (n <= 0) Then
                task = 2
            End If
            If (m <= 0) Then
                task = 2
            End If
            If (m > n) Then
                task = 2
            End If
            If (factr < 0) Then
                task = 2
            End If
            index = 1
            Do While (index <= n)
                If ((nbd(index) < 0) Or (nbd(index) > 3)) Then
                    task = 2
                    info = -6
                    k = index
                End If
                If ((nbd(index) = 2) AndAlso (l(index) > u(index))) Then
                    task = 2
                    info = -7
                    k = index
                End If
                index += 1
            Loop
        End Sub

        Private Sub lbfgsbformk(ByVal n As Integer, ByVal nsub As Integer, ByRef ind As Integer(), ByVal nenter As Integer, ByVal ileave As Integer, ByRef indx2 As Integer(), ByVal iupdat As Integer, ByVal updatd As Boolean, ByRef wn As Double(,), ByRef wn1 As Double(,), ByVal m As Integer, ByRef ws As Double(,), ByRef wy As Double(,), ByRef sy As Double(,), ByVal theta As Double, ByVal col As Integer, ByVal head As Integer, ByRef info As Integer, ByRef workvec As Double(), ByRef workmat As Double(,))
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            Dim num7 As Integer = 0
            Dim num8 As Integer = 0
            Dim num9 As Integer = 0
            Dim num10 As Integer = 0
            Dim index As Integer = 0
            Dim num12 As Integer = 0
            Dim num13 As Integer = 0
            Dim num14 As Integer = 0
            Dim num15 As Integer = 0
            Dim num16 As Integer = 0
            Dim num17 As Integer = 0
            Dim num18 As Double = 0
            Dim num19 As Double = 0
            Dim num20 As Double = 0
            Dim num21 As Double = 0
            Dim num22 As Double = 0
            Dim num23 As Integer = 0
            Dim num24 As Integer = 0
            Dim num25 As Integer = 0
            If updatd Then
                If (iupdat > m) Then
                    num5 = 1
                    Do While (num5 <= (m - 1))
                        num6 = (m + num5)
                        num25 = ((num5 + 1) - num5)
                        num24 = num5
                        Do While (num24 <= (m - 1))
                            wn1(num24, num5) = wn1((num24 + num25), (num5 + 1))
                            num24 += 1
                        Loop
                        num25 = ((num6 + 1) - num6)
                        num24 = num6
                        Do While (num24 <= (((num6 + m) - num5) - 1))
                            wn1(num24, num6) = wn1((num24 + num25), (num6 + 1))
                            num24 += 1
                        Loop
                        num25 = ((m + 2) - (m + 1))
                        num24 = (m + 1)
                        Do While (num24 <= ((m + m) - 1))
                            wn1(num24, num5) = wn1((num24 + num25), (num5 + 1))
                            num24 += 1
                        Loop
                        num5 += 1
                    Loop
                End If
                num13 = 1
                num14 = nsub
                num15 = (nsub + 1)
                num16 = n
                num3 = col
                num4 = (m + col)
                num = ((head + col) - 1)
                If (num > m) Then
                    num = (num - m)
                End If
                num2 = head
                num5 = 1
                Do While (num5 <= col)
                    num6 = (m + num5)
                    num18 = 0
                    num19 = 0
                    num20 = 0
                    index = num13
                    Do While (index <= num14)
                        num9 = ind(index)
                        num18 = (num18 + (wy(num9, num) * wy(num9, num2)))
                        index += 1
                    Loop
                    index = num15
                    Do While (index <= num16)
                        num9 = ind(index)
                        num19 = (num19 + (ws(num9, num) * ws(num9, num2)))
                        num20 = (num20 + (ws(num9, num) * wy(num9, num2)))
                        index += 1
                    Loop
                    wn1(num3, num5) = num18
                    wn1(num4, num6) = num19
                    wn1(num4, num5) = num20
                    num2 = ((num2 Mod m) + 1)
                    num5 += 1
                Loop
                num5 = col
                num2 = ((head + col) - 1)
                If (num2 > m) Then
                    num2 = (num2 - m)
                End If
                num = head
                num10 = 1
                Do While (num10 <= col)
                    num4 = (m + num10)
                    num20 = 0
                    index = num13
                    Do While (index <= num14)
                        num9 = ind(index)
                        num20 = (num20 + (ws(num9, num) * wy(num9, num2)))
                        index += 1
                    Loop
                    num = ((num Mod m) + 1)
                    wn1(num4, num5) = num20
                    num10 += 1
                Loop
                num17 = (col - 1)
            Else
                num17 = col
            End If
            num = head
            num3 = 1
            Do While (num3 <= num17)
                num4 = (m + num3)
                num2 = head
                num5 = 1
                Do While (num5 <= num3)
                    num6 = (m + num5)
                    num18 = 0
                    num19 = 0
                    num20 = 0
                    num21 = 0
                    index = 1
                    Do While (index <= nenter)
                        num9 = indx2(index)
                        num18 = (num18 + (wy(num9, num) * wy(num9, num2)))
                        num19 = (num19 + (ws(num9, num) * ws(num9, num2)))
                        index += 1
                    Loop
                    index = ileave
                    Do While (index <= n)
                        num9 = indx2(index)
                        num20 = (num20 + (wy(num9, num) * wy(num9, num2)))
                        num21 = (num21 + (ws(num9, num) * ws(num9, num2)))
                        index += 1
                    Loop
                    wn1(num3, num5) = ((wn1(num3, num5) + num18) - num20)
                    wn1(num4, num6) = ((wn1(num4, num6) - num19) + num21)
                    num2 = ((num2 Mod m) + 1)
                    num5 += 1
                Loop
                num = ((num Mod m) + 1)
                num3 += 1
            Loop
            num = head
            num4 = (m + 1)
            Do While (num4 <= (m + num17))
                num2 = head
                num5 = 1
                Do While (num5 <= num17)
                    num18 = 0
                    num20 = 0
                    index = 1
                    Do While (index <= nenter)
                        num9 = indx2(index)
                        num18 = (num18 + (ws(num9, num) * wy(num9, num2)))
                        index += 1
                    Loop
                    index = ileave
                    Do While (index <= n)
                        num9 = indx2(index)
                        num20 = (num20 + (ws(num9, num) * wy(num9, num2)))
                        index += 1
                    Loop
                    If (num4 <= (num5 + m)) Then
                        wn1(num4, num5) = ((wn1(num4, num5) + num18) - num20)
                    Else
                        wn1(num4, num5) = ((wn1(num4, num5) - num18) + num20)
                    End If
                    num2 = ((num2 Mod m) + 1)
                    num5 += 1
                Loop
                num = ((num Mod m) + 1)
                num4 += 1
            Loop
            num3 = 1
            Do While (num3 <= col)
                num4 = (col + num3)
                num7 = (m + num3)
                num5 = 1
                Do While (num5 <= num3)
                    num6 = (col + num5)
                    num8 = (m + num5)
                    wn(num5, num3) = (wn1(num3, num5) / theta)
                    wn(num6, num4) = (wn1(num7, num8) * theta)
                    num5 += 1
                Loop
                num5 = 1
                Do While (num5 <= (num3 - 1))
                    wn(num5, num4) = -wn1(num7, num5)
                    num5 += 1
                Loop
                num5 = num3
                Do While (num5 <= col)
                    wn(num5, num4) = wn1(num7, num5)
                    num5 += 1
                Loop
                wn(num3, num3) = (wn(num3, num3) + sy(num3, num3))
                num3 += 1
            Loop
            info = 0
            If Not Me.lbfgsbdpofa(wn, col) Then
                info = -1
            Else
                num12 = (2 * col)
                num6 = (col + 1)
                Do While (num6 <= num12)
                    num24 = 1
                    Do While (num24 <= col)
                        workvec(num24) = wn(num24, num6)
                        num24 += 1
                    Loop
                    Me.lbfgsbdtrsl(wn, col, workvec, 11, info)
                    num24 = 1
                    Do While (num24 <= col)
                        wn(num24, num6) = workvec(num24)
                        num24 += 1
                    Loop
                    num6 += 1
                Loop
                num4 = (col + 1)
                Do While (num4 <= num12)
                    num6 = num4
                    Do While (num6 <= num12)
                        num22 = 0
                        num24 = 1
                        Do While (num24 <= col)
                            num22 = (num22 + (wn(num24, num4) * wn(num24, num6)))
                            num24 += 1
                        Loop
                        wn(num4, num6) = (wn(num4, num6) + num22)
                        num6 += 1
                    Loop
                    num4 += 1
                Loop
                num23 = 1
                Do While (num23 <= col)
                    num25 = ((col + 1) - 1)
                    num24 = 1
                    Do While (num24 <= col)
                        workmat(num23, num24) = wn((col + num23), (num24 + num25))
                        num24 += 1
                    Loop
                    num23 += 1
                Loop
                info = 0
                If Not Me.lbfgsbdpofa(workmat, col) Then
                    info = -2
                Else
                    num23 = 1
                    Do While (num23 <= col)
                        num25 = (1 - (col + 1))
                        num24 = (col + 1)
                        Do While (num24 <= (col + col))
                            wn((col + num23), num24) = workmat(num23, (num24 + num25))
                            num24 += 1
                        Loop
                        num23 += 1
                    Loop

                End If
            End If
        End Sub

        Private Sub lbfgsbformt(ByVal m As Integer, ByRef wt As Double(,), ByRef sy As Double(,), ByRef ss As Double(,), ByVal col As Integer, ByVal theta As Double, ByRef info As Integer)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Double = 0
            num2 = 1
            Do While (num2 <= col)
                wt(1, num2) = (theta * ss(1, num2))
                num2 += 1
            Loop
            num = 2
            Do While (num <= col)
                num2 = num
                Do While (num2 <= col)
                    num4 = (Math.Min(num, num2) - 1)
                    num5 = 0
                    num3 = 1
                    Do While (num3 <= num4)
                        num5 = (num5 + ((sy(num, num3) * sy(num2, num3)) / sy(num3, num3)))
                        num3 += 1
                    Loop
                    wt(num, num2) = (num5 + (theta * ss(num, num2)))
                    num2 += 1
                Loop
                num += 1
            Loop
            info = 0
            If Not Me.lbfgsbdpofa(wt, col) Then
                info = -3
            End If
        End Sub

        Private Sub lbfgsbfreev(ByVal n As Integer, ByRef nfree As Integer, ByRef index As Integer(), ByRef nenter As Integer, ByRef ileave As Integer, ByRef indx2 As Integer(), ByRef iwhere As Integer(), ByRef wrk As Boolean, ByVal updatd As Boolean, ByVal cnstnd As Boolean, ByVal iter As Integer)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            nenter = 0
            ileave = (n + 1)
            If ((iter > 0) And cnstnd) Then
                num2 = 1
                Do While (num2 <= nfree)
                    num3 = index(num2)
                    If (iwhere(num3) > 0) Then
                        ileave -= 1
                        indx2(ileave) = num3
                    End If
                    num2 += 1
                Loop
                num2 = (1 + nfree)
                Do While (num2 <= n)
                    num3 = index(num2)
                    If (iwhere(num3) <= 0) Then
                        nenter += 1
                        indx2(nenter) = num3
                    End If
                    num2 += 1
                Loop
            End If
            wrk = (((ileave < (n + 1)) Or (nenter > 0)) Or updatd)
            nfree = 0
            num = (n + 1)
            num2 = 1
            Do While (num2 <= n)
                If (iwhere(num2) <= 0) Then
                    nfree += 1
                    index(nfree) = num2
                Else
                    num -= 1
                    index(num) = num2
                End If
                num2 += 1
            Loop
        End Sub

        Private Sub lbfgsbhpsolb(ByVal n As Integer, ByRef t As Double(), ByRef iorder As Integer(), ByVal iheap As Integer)
            Dim index As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Double = 0
            Dim num7 As Double = 0
            If (iheap = 0) Then
                num3 = 2
                Do While (num3 <= n)
                    num6 = t(num3)
                    num4 = iorder(num3)
                    index = num3
                    Do While True
                        If (index <= 1) Then
                            Exit Do
                        End If
                        num2 = (index / 2)
                        If (num6 >= t(num2)) Then
                            Exit Do
                        End If
                        t(index) = t(num2)
                        iorder(index) = iorder(num2)
                        index = num2
                    Loop
                    t(index) = num6
                    iorder(index) = num4
                    num3 += 1
                Loop
            End If
            If (n <= 1) Then
                Return
            End If
            index = 1
            num7 = t(1)
            num5 = iorder(1)
            num6 = t(n)
            num4 = iorder(n)
Label_0089:
            num2 = (index + index)
            If (num2 <= (n - 1)) Then
                If (t((num2 + 1)) < t(num2)) Then
                    num2 += 1
                End If
                If (t(num2) < num6) Then
                    t(index) = t(num2)
                    iorder(index) = iorder(num2)
                    index = num2
                    GoTo Label_0089
                End If
            End If
            t(index) = num6
            iorder(index) = num4
            t(n) = num7
            iorder(n) = num5
        End Sub

        Private Sub lbfgsblnsrlb(ByVal n As Integer, ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef x As Double(), ByVal f As Double, ByRef fold As Double, ByRef gd As Double, ByRef gdold As Double, ByRef g As Double(), ByRef d As Double(), ByRef r As Double(), ByRef t As Double(), ByRef z As Double(), ByRef stp As Double, ByRef dnrm As Double, ByRef dtd As Double, ByRef xstep As Double, ByRef stpmx As Double, ByVal iter As Integer, ByRef ifun As Integer, ByRef iback As Integer, ByRef nfgv As Integer, ByRef info As Integer, ByRef task As Integer, ByVal boxed As Boolean, ByVal cnstnd As Boolean, ByRef csave As Integer, ByRef isave As Integer(), ByRef dsave As Double())
            Dim index As Integer = 0
            Dim num2 As Double = 0
            Dim num3 As Double = 0
            Dim num4 As Double = 0
            Dim ftol As Double = 0
            Dim gtol As Double = 0
            Dim xtol As Double = 0
            Dim num8 As Double = 0
            Dim addinfo As Integer = 0
            Dim num10 As Integer = 0
            addinfo = 0
            num8 = 10000000000
            ftol = 0.001
            gtol = 0.9
            xtol = 0.1
            If (Not task = 1) Then
                num4 = 0
                num10 = 1
                Do While (num10 <= n)
                    num4 = (num4 + (d(num10) * d(num10)))
                    num10 += 1
                Loop
                dtd = num4
                dnrm = Math.Sqrt(dtd)
                stpmx = num8
                If cnstnd Then
                    If (iter = 0) Then
                        stpmx = 1
                    Else
                        index = 1
                        Do While (index <= n)
                            num2 = d(index)
                            If (nbd(index) <> 0) Then
                                If ((num2 < 0) And (nbd(index) <= 2)) Then
                                    num3 = (l(index) - x(index))
                                    If (num3 >= 0) Then
                                        stpmx = 0
                                    ElseIf ((num2 * stpmx) < num3) Then
                                        stpmx = (num3 / num2)
                                    End If
                                ElseIf ((num2 > 0) And (nbd(index) >= 2)) Then
                                    num3 = (u(index) - x(index))
                                    If (num3 <= 0) Then
                                        stpmx = 0
                                    ElseIf ((num2 * stpmx) > num3) Then
                                        stpmx = (num3 / num2)
                                    End If
                                End If
                            End If
                            index += 1
                        Loop
                    End If
                End If
                If ((iter = 0) And Not boxed) Then
                    stp = Math.Min((1 / dnrm), stpmx)
                Else
                    stp = 1
                End If
                num10 = 1
                Do While (num10 <= n)
                    t(num10) = x(num10)
                    num10 += 1
                Loop
                num10 = 1
                Do While (num10 <= n)
                    r(num10) = g(num10)
                    num10 += 1
                Loop
                fold = f
                ifun = 0
                iback = 0
                csave = 0
            End If
            num4 = 0
            num10 = 1
            Do While (num10 <= n)
                num4 = (num4 + (g(num10) * d(num10)))
                num10 += 1
            Loop
            gd = num4
            If (ifun = 0) Then
                gdold = gd
                If (gd >= 0) Then
                    info = -4
                    Return
                End If
            End If
            Me.lbfgsbdcsrch(f, gd, stp, ftol, gtol, xtol, 0, stpmx, csave, isave, dsave, addinfo)
            xstep = (stp * dnrm)
            If ((Not csave = 4) And (Not csave = 3)) Then
                task = 1
                ifun += 1
                nfgv += 1
                iback = (ifun - 1)
                If (stp = 1) Then
                    num10 = 1
                    Do While (num10 <= n)
                        x(num10) = z(num10)
                        num10 += 1
                    Loop
                Else
                    index = 1
                    Do While (index <= n)
                        x(index) = ((stp * d(index)) + t(index))
                        index += 1
                    Loop
                End If
            Else
                task = 5
            End If
        End Sub

        Private Sub lbfgsbmatupd(ByVal n As Integer, ByVal m As Integer, ByRef ws As Double(,), ByRef wy As Double(,), ByRef sy As Double(,), ByRef ss As Double(,), ByRef d As Double(), ByRef r As Double(), ByRef itail As Integer, ByVal iupdat As Integer, ByRef col As Integer, ByRef head As Integer, ByRef theta As Double, ByVal rr As Double, ByVal dr As Double, ByVal stp As Double, ByVal dtd As Double)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim num3 As Double = 0
            Dim index As Integer = 0
            Dim num5 As Integer = 0
            If (iupdat <= m) Then
                col = iupdat
                itail = ((((head + iupdat) - 2) Mod m) + 1)
            Else
                itail = ((itail Mod m) + 1)
                head = ((head Mod m) + 1)
            End If
            index = 1
            Do While (index <= n)
                ws(index, itail) = d(index)
                index += 1
            Loop
            index = 1
            Do While (index <= n)
                wy(index, itail) = r(index)
                index += 1
            Loop
            theta = (rr / dr)
            If (iupdat > m) Then
                num = 1
                Do While (num <= (col - 1))
                    num5 = 1
                    index = 1
                    Do While (index <= num)
                        ss(index, num) = ss((index + num5), (num + 1))
                        index += 1
                    Loop
                    num5 = ((num + 1) - num)
                    index = num
                    Do While (index <= (col - 1))
                        sy(index, num) = sy((index + num5), (num + 1))
                        index += 1
                    Loop
                    num += 1
                Loop
            End If
            num2 = head
            num = 1
            Do While (num <= (col - 1))
                num3 = 0
                index = 1
                Do While (index <= n)
                    num3 = (num3 + (d(index) * wy(index, num2)))
                    index += 1
                Loop
                sy(col, num) = num3
                num3 = 0
                index = 1
                Do While (index <= n)
                    num3 = (num3 + (ws(index, num2) * d(index)))
                    index += 1
                Loop
                ss(num, col) = num3
                num2 = ((num2 Mod m) + 1)
                num += 1
            Loop
            If (stp = 1) Then
                ss(col, col) = dtd
            Else
                ss(col, col) = ((stp * stp) * dtd)
            End If
            sy(col, col) = dr
        End Sub

        Public Sub lbfgsbminimize(ByVal n As Integer, ByVal m As Integer, ByRef x As Double(), ByVal epsg As Double, ByVal epsf As Double, ByVal epsx As Double, ByVal maxits As Integer, ByRef nbd As Integer(), ByRef l As Double(), ByRef u As Double(), ByRef info As Integer)
            Dim f As Double = 0
            Dim g As Double()
            Dim abort As Boolean = False
            Dim numArray2 As Double()
            Dim numArray3 As Double()
            Dim ws(,) As Double
            Dim wy(,) As Double
            Dim sy(,) As Double
            Dim ss(,) As Double
            Dim wt(,) As Double
            Dim wn(,) As Double
            Dim numArray10(,) As Double
            Dim xcp As Double()
            Dim r As Double()
            Dim d As Double()
            Dim t As Double()
            Dim wa As Double()
            Dim sg As Double()
            Dim yg As Double()
            Dim index As Integer()
            Dim iwhere As Integer()
            Dim iorder As Integer()
            Dim csave As Integer = 0
            Dim task As Integer = 0
            Dim prjctd As Boolean = False
            Dim cnstnd As Boolean = False
            Dim boxed As Boolean = False
            Dim updatd As Boolean = False
            Dim wrk As Boolean = False
            Dim num4 As Integer = 0
            Dim k As Integer = 0
            Dim num6 As Integer = 0
            Dim iback As Integer = 0
            Dim num8 As Integer = 0
            Dim head As Integer = 0
            Dim col As Integer = 0
            Dim iter As Integer = 0
            Dim itail As Integer = 0
            Dim iupdat As Integer = 0
            Dim nint As Integer = 0
            Dim nfgv As Integer = 0
            Dim num16 As Integer = 0
            Dim ifun As Integer = 0
            Dim iword As Integer = 0
            Dim nfree As Integer = 0
            Dim ileave As Integer = 0
            Dim nenter As Integer = 0
            Dim theta As Double = 0
            Dim fold As Double = 0
            Dim dr As Double = 0
            Dim rr As Double = 0
            Dim dnrm As Double = 0
            Dim xstep As Double = 0
            Dim sbgnrm As Double = 0
            Dim num29 As Double = 0
            Dim dtd As Double = 0
            Dim gd As Double = 0
            Dim gdold As Double = 0
            Dim stp As Double = 0
            Dim stpmx As Double = 0
            Dim num35 As Double = 0
            Dim workvec As Double()
            Dim numArray22 As Double()
            Dim dsave As Double()
            Dim p As Double()
            Dim c As Double()
            Dim wbp As Double()
            Dim v As Double()
            Dim workmat(,) As Double
            Dim isave As Integer()
            Dim num36 As Integer = 0
            Dim num37 As Integer = 0
            workvec = New Double((m + 1) - 1) {}
            numArray22 = New Double(((2 * m) + 1) - 1) {}
            workmat = New Double((m + 1) - 1, (m + 1) - 1) {}
            isave = New Integer(3 - 1) {}
            dsave = New Double(14 - 1) {}
            p = New Double(((2 * m) + 1) - 1) {}
            c = New Double(((2 * m) + 1) - 1) {}
            wbp = New Double(((2 * m) + 1) - 1) {}
            v = New Double(((2 * m) + 1) - 1) {}
            g = New Double((n + 1) - 1) {}
            numArray2 = New Double((n + 1) - 1) {}
            numArray3 = New Double((n + 1) - 1) {}
            ws = New Double((n + 1) - 1, (m + 1) - 1) {}
            wy = New Double((n + 1) - 1, (m + 1) - 1) {}
            sy = New Double((m + 1) - 1, (m + 1) - 1) {}
            ss = New Double((m + 1) - 1, (m + 1) - 1) {}
            wt = New Double((m + 1) - 1, (m + 1) - 1) {}
            wn = New Double(((2 * m) + 1) - 1, ((2 * m) + 1) - 1) {}
            numArray10 = New Double(((2 * m) + 1) - 1, ((2 * m) + 1) - 1) {}
            xcp = New Double((n + 1) - 1) {}
            r = New Double((n + 1) - 1) {}
            d = New Double((n + 1) - 1) {}
            t = New Double((n + 1) - 1) {}
            wa = New Double(((8 * m) + 1) - 1) {}
            sg = New Double((m + 1) - 1) {}
            yg = New Double((m + 1) - 1) {}
            index = New Integer((n + 1) - 1) {}
            iwhere = New Integer((n + 1) - 1) {}
            iorder = New Integer((n + 1) - 1) {}
            col = 0
            head = 1
            theta = 1
            iupdat = 0
            updatd = False
            iter = 0
            nfgv = 0
            nint = 0
            num6 = 0
            num8 = 0
            nfree = n
            num16 = 0
            Me.lbfgsberrclb(n, m, epsf, l, u, nbd, task, num16, k)
            If ((((task = 2) Or (maxits < 0)) Or (epsg < 0)) Or (epsx < 0)) Then
                info = -1
                Return
            End If
            Me.lbfgsbactive(n, l, u, nbd, x, iwhere, prjctd, cnstnd, boxed)
            num36 = 1
            Do While (num36 <= n)
                numArray2(num36) = x(num36)
                num36 += 1
            Loop
            Me.funcgrad(x, f, g)
            nfgv = 1
            Me.lbfgsbprojgr(n, l, u, nbd, x, g, sbgnrm)
            If (sbgnrm <= epsg) Then
                info = 4
                Return
            End If
Label_03EE:
            iword = -1
            If (Not cnstnd And (col > 0)) Then
                num36 = 1
                Do While (num36 <= n)
                    xcp(num36) = x(num36)
                    num36 += 1
                Loop
                wrk = updatd
                nint = 0
            Else
                num36 = 1
                Do While (num36 <= (2 * m))
                    p(num36) = wa(num36)
                    num36 += 1
                Loop
                num37 = (((2 * m) + 1) - 1)
                num36 = 1
                Do While (num36 <= (2 * m))
                    c(num36) = wa((num36 + num37))
                    num36 += 1
                Loop
                num37 = (((4 * m) + 1) - 1)
                num36 = 1
                Do While (num36 <= (2 * m))
                    wbp(num36) = wa((num36 + num37))
                    num36 += 1
                Loop
                num37 = (((6 * m) + 1) - 1)
                num36 = 1
                Do While (num36 <= (2 * m))
                    v(num36) = wa((num36 + num37))
                    num36 += 1
                Loop
                Me.lbfgsbcauchy(n, x, l, u, nbd, g, iorder, iwhere, t, d, xcp, m, wy, ws, sy, wt, theta, col, head, p, c, wbp, v, nint, sg, yg, sbgnrm, num16, workvec)
                num36 = 1
                Do While (num36 <= (2 * m))
                    wa(num36) = p(num36)
                    num36 += 1
                Loop
                num37 = (1 - ((2 * m) + 1))
                num36 = ((2 * m) + 1)
                Do While (num36 <= (4 * m))
                    wa(num36) = c((num36 + num37))
                    num36 += 1
                Loop
                num37 = (1 - ((4 * m) + 1))
                num36 = ((4 * m) + 1)
                Do While (num36 <= (6 * m))
                    wa(num36) = wbp((num36 + num37))
                    num36 += 1
                Loop
                num37 = (1 - ((6 * m) + 1))
                num36 = ((6 * m) + 1)
                Do While (num36 <= (8 * m))
                    wa(num36) = v((num36 + num37))
                    num36 += 1
                Loop
                If (num16 <> 0) Then
                    num16 = 0
                    col = 0
                    head = 1
                    theta = 1
                    iupdat = 0
                    updatd = False
                    GoTo Label_03EE
                End If
                num6 = (num6 + nint)
                Me.lbfgsbfreev(n, nfree, index, nenter, ileave, iorder, iwhere, wrk, updatd, cnstnd, iter)
            End If
            If ((nfree <> 0) And (col <> 0)) Then
                If wrk Then
                    Me.lbfgsbformk(n, nfree, index, nenter, ileave, iorder, iupdat, updatd, wn, numArray10, m, ws, wy, sy, theta, col, head, num16, workvec, workmat)
                End If
                If (num16 <> 0) Then
                    num16 = 0
                    col = 0
                    head = 1
                    theta = 1
                    iupdat = 0
                    updatd = False
                    GoTo Label_03EE
                End If
                Me.lbfgsbcmprlb(n, m, x, g, ws, wy, sy, wt, xcp, r, wa, index, theta, col, head, nfree, cnstnd, num16, workvec, numArray22)
                If (num16 = 0) Then
                    Me.lbfgsbsubsm(n, m, nfree, index, l, u, nbd, xcp, r, ws, wy, theta, col, head, iword, wa, wn, num16)
                End If
                If (num16 <> 0) Then
                    num16 = 0
                    col = 0
                    head = 1
                    theta = 1
                    iupdat = 0
                    updatd = False
                    GoTo Label_03EE
                End If
            End If
            num4 = 1
            Do While (num4 <= n)
                d(num4) = (xcp(num4) - x(num4))
                num4 += 1
            Loop
            task = 0
            Do While True
                Me.lbfgsblnsrlb(n, l, u, nbd, x, f, fold, gd, gdold, g, d, r, t, xcp, stp, dnrm, dtd, xstep, stpmx, iter, ifun, iback, nfgv, num16, task, boxed, cnstnd, csave, isave, dsave)
                If (((num16 <> 0) Or (iback >= 20)) Or (task <> 1)) Then
                    Exit Do
                End If
                Me.funcgrad(x, f, g)
                If Double.IsNaN(x(1)) Then Exit Do
            Loop
            If (num16 <> 0) Then
                num36 = 1
                Do While (num36 <= n)
                    x(num36) = t(num36)
                    num36 += 1
                Loop
                num36 = 1
                Do While (num36 <= n)
                    g(num36) = r(num36)
                    num36 += 1
                Loop
                f = fold
                If (col = 0) Then
                    If (num16 = 0) Then
                        num16 = -9
                        nfgv -= 1
                        ifun -= 1
                        iback -= 1
                    End If
                    task = 2
                    iter += 1
                    info = -2
                    Return
                End If
                If (num16 = 0) Then
                    nfgv -= 1
                End If
                num16 = 0
                col = 0
                head = 1
                theta = 1
                iupdat = 0
                updatd = False
                GoTo Label_03EE
            End If
            iter += 1
            Me.lbfgsbnewiteration(x, f, g, abort)
            If abort Then Exit Sub
            Me.lbfgsbprojgr(n, l, u, nbd, x, g, sbgnrm)
            If (sbgnrm <= epsg) Then
                info = 4
            Else
                num36 = 1
                Do While (num36 <= n)
                    numArray3(num36) = numArray2(num36)
                    num36 += 1
                Loop
                num36 = 1
                Do While (num36 <= n)
                    numArray3(num36) = (numArray3(num36) - x(num36))
                    num36 += 1
                Loop
                num35 = 0
                num36 = 1
                Do While (num36 <= n)
                    num35 = (num35 + (numArray3(num36) * numArray3(num36)))
                    num36 += 1
                Loop
                If (Math.Sqrt(num35) <= epsx) Then
                    info = 2
                Else
                    num29 = Math.Max(Math.Abs(fold), Math.Max(Math.Abs(f), 1))
                    If ((fold - f) <= (epsf * num29)) Then
                        info = 1
                    ElseIf ((iter > maxits) And (maxits > 0)) Then
                        info = 5
                    ElseIf Me.additionallbfgsbstoppingcriterion(iter, x, f, g) Then
                        info = 0
                    Else
                        num36 = 1
                        Do While (num36 <= n)
                            numArray2(num36) = x(num36)
                            num36 += 1
                        Loop
                        num4 = 1
                        Do While (num4 <= n)
                            r(num4) = (g(num4) - r(num4))
                            num4 += 1
                        Loop
                        rr = 0
                        num36 = 1
                        Do While (num36 <= n)
                            rr = (rr + (r(num36) * r(num36)))
                            num36 += 1
                        Loop
                        If (stp = 1) Then
                            dr = (gd - gdold)
                            num29 = -gdold
                        Else
                            dr = ((gd - gdold) * stp)
                            num36 = 1
                            Do While (num36 <= n)
                                d(num36) = (stp * d(num36))
                                num36 += 1
                            Loop
                            num29 = -(gdold * stp)
                        End If
                        If (dr <= (0.0000000000000005 * num29)) Then
                            num8 += 1
                            updatd = False
                        Else
                            updatd = True
                            iupdat += 1
                            Me.lbfgsbmatupd(n, m, ws, wy, sy, ss, d, r, itail, iupdat, col, head, theta, rr, dr, stp, dtd)
                            Me.lbfgsbformt(m, wt, sy, ss, col, theta, num16)
                            If (num16 <> 0) Then
                                num16 = 0
                                col = 0
                                head = 1
                                theta = 1
                                iupdat = 0
                                updatd = False
                            End If
                        End If
                        GoTo Label_03EE
                    End If
                End If
            End If
        End Sub

        Private Sub lbfgsbprojgr(ByVal n As Integer, ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef x As Double(), ByRef g As Double(), ByRef sbgnrm As Double)
            Dim index As Integer = 0
            Dim num2 As Double = 0
            sbgnrm = 0
            index = 1
            Do While (index <= n)
                num2 = g(index)
                If (nbd(index) <> 0) Then
                    If (num2 < 0) Then
                        If (nbd(index) >= 2) Then
                            num2 = Math.Max((x(index) - u(index)), num2)
                        End If
                    ElseIf (nbd(index) <= 2) Then
                        num2 = Math.Min((x(index) - l(index)), num2)
                    End If
                End If
                sbgnrm = Math.Max(sbgnrm, Math.Abs(num2))
                index += 1
            Loop
        End Sub

        Private Sub lbfgsbsubsm(ByVal n As Integer, ByVal m As Integer, ByVal nsub As Integer, ByRef ind As Integer(), ByRef l As Double(), ByRef u As Double(), ByRef nbd As Integer(), ByRef x As Double(), ByRef d As Double(), ByRef ws As Double(,), ByRef wy As Double(,), ByVal theta As Double, ByVal col As Integer, ByVal head As Integer, ByRef iword As Integer, ByRef wv As Double(), ByRef wn As Double(,), ByRef info As Integer)
            Dim num As Integer = 0
            Dim num2 As Integer = 0
            Dim index As Integer = 0
            Dim num4 As Integer = 0
            Dim num5 As Integer = 0
            Dim num6 As Integer = 0
            Dim num7 As Integer = 0
            Dim num8 As Integer = 0
            Dim num9 As Double = 0
            Dim num10 As Double = 0
            Dim num11 As Double = 0
            Dim num12 As Double = 0
            If (nsub > 0) Then
                num = head
                num6 = 1
                Do While (num6 <= col)
                    num11 = 0
                    num12 = 0
                    num7 = 1
                    Do While (num7 <= nsub)
                        num8 = ind(num7)
                        num11 = (num11 + (wy(num8, num) * d(num7)))
                        num12 = (num12 + (ws(num8, num) * d(num7)))
                        num7 += 1
                    Loop
                    wv(num6) = num11
                    wv((col + num6)) = (theta * num12)
                    num = ((num Mod m) + 1)
                    num6 += 1
                Loop
                num2 = (2 * col)
                Me.lbfgsbdtrsl(wn, num2, wv, 11, info)
                If (info = 0) Then
                    num6 = 1
                    Do While (num6 <= col)
                        wv(num6) = -wv(num6)
                        num6 += 1
                    Loop
                    Me.lbfgsbdtrsl(wn, num2, wv, 1, info)
                    If (info = 0) Then
                        num = head
                        num4 = 1
                        Do While (num4 <= col)
                            num5 = (col + num4)
                            num6 = 1
                            Do While (num6 <= nsub)
                                num8 = ind(num6)
                                d(num6) = ((d(num6) + ((wy(num8, num) * wv(num4)) / theta)) + (ws(num8, num) * wv(num5)))
                                num6 += 1
                            Loop
                            num = ((num Mod m) + 1)
                            num4 += 1
                        Loop
                        num6 = 1
                        Do While (num6 <= nsub)
                            d(num6) = (d(num6) / theta)
                            num6 += 1
                        Loop
                        num9 = 1
                        num11 = num9
                        num6 = 1
                        Do While (num6 <= nsub)
                            num8 = ind(num6)
                            num10 = d(num6)
                            If (nbd(num8) <> 0) Then
                                If ((num10 < 0) And (nbd(num8) <= 2)) Then
                                    num12 = (l(num8) - x(num8))
                                    If (num12 >= 0) Then
                                        num11 = 0
                                    ElseIf ((num10 * num9) < num12) Then
                                        num11 = (num12 / num10)
                                    End If
                                ElseIf ((num10 > 0) And (nbd(num8) >= 2)) Then
                                    num12 = (u(num8) - x(num8))
                                    If (num12 <= 0) Then
                                        num11 = 0
                                    ElseIf ((num10 * num9) > num12) Then
                                        num11 = (num12 / num10)
                                    End If
                                End If
                                If (num11 < num9) Then
                                    num9 = num11
                                    index = num6
                                End If
                            End If
                            num6 += 1
                        Loop
                        If (num9 < 1) Then
                            num10 = d(index)
                            num8 = ind(index)
                            If (num10 > 0) Then
                                x(num8) = u(num8)
                                d(index) = 0
                            ElseIf (num10 < 0) Then
                                x(num8) = l(num8)
                                d(index) = 0
                            End If
                        End If
                        num6 = 1
                        Do While (num6 <= nsub)
                            num8 = ind(num6)
                            x(num8) = (x(num8) + (num9 * d(num6)))
                            num6 += 1
                        Loop
                        If (num9 < 1) Then
                            iword = 1
                        Else
                            iword = 0
                        End If
                    End If
                End If
            End If
        End Sub

    End Class

End Namespace