File size: 27,085 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Namespace MathEx.LBFGS

    Public Class lbfgs

        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 lbfgsnewiteration(ByRef x As Double(), ByVal f As Double, ByRef g As Double(), ByRef abort As Boolean)
            fc2.Invoke(x, f, g, abort)
        End Sub

        '/*************************************************************************
        '        LIMITED MEMORY BFGS METHOD FOR LARGE SCALE OPTIMIZATION
        '                          JORGE NOCEDAL

        'The subroutine minimizes function F(x) of N arguments by  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, 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 fulfilled.
        '    MaxIts- maximum number of iterations. If MaxIts=0, the number of
        '            iterations is unlimited.

        'Output parameters:
        '    X   -   solution approximation. Array whose index ranges from 1 to N.
        '    Info-   a return code:
        '                    * -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 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.
        '*************************************************************************/

        Private Function lbfgsdotproduct(ByVal n As Integer, ByRef dx As Double(), ByVal sx As Integer, ByRef dy As Double(), ByVal sy As Integer) As Double
            Dim num2 As Double = 0
            Dim num3 As Integer = 0
            Dim index As Integer = 0
            Dim num5 As Integer = 0
            num3 = ((sx + n) - 1)
            num5 = (sy - sx)
            num2 = 0
            index = sx
            Do While (index <= num3)
                num2 = (num2 + (dx(index) * dy((index + num5))))
                index += 1
            Loop
            Return num2
        End Function

        Private Sub lbfgslincomb(ByVal n As Integer, ByVal da As Double, ByRef dx As Double(), ByVal sx As Integer, ByRef dy As Double(), ByVal sy As Integer)
            Dim num As Integer = 0
            Dim index As Integer = 0
            Dim num3 As Integer = 0
            num = ((sy + n) - 1)
            num3 = (sx - sy)
            index = sy
            Do While (index <= num)
                dy(index) = (dy(index) + (da * dx((index + num3))))
                index += 1
            Loop
        End Sub

        Private Sub lbfgsmcsrch(ByVal n As Integer, ByRef x As Double(), ByRef f As Double, ByRef g As Double(), ByRef s As Double(), ByVal sstart As Integer, ByRef stp As Double, ByVal ftol As Double, ByVal xtol As Double, ByVal maxfev As Integer, ByRef info As Integer, ByRef nfev As Integer, ByRef wa As Double(), ByVal gtol As Double, ByVal stpmin As Double, ByVal stpmax As Double)
            Dim num As Integer = 0
            Dim index As Integer = 0
            Dim brackt As Boolean = False
            Dim flag2 As Boolean = False
            Dim num3 As Double = 0
            Dim dp As Double = 0
            Dim num5 As Double = 0
            Dim num6 As Double = 0
            Dim dx As Double = 0
            Dim num8 As Double = 0
            Dim dy As Double = 0
            Dim num10 As Double = 0
            Dim num11 As Double = 0
            Dim num12 As Double = 0
            Dim fp As Double = 0
            Dim fx As Double = 0
            Dim num15 As Double = 0
            Dim fy As Double = 0
            Dim num17 As Double = 0
            Dim num18 As Double = 0
            Dim num19 As Double = 0
            Dim stx As Double = 0
            Dim sty As Double = 0
            Dim stmin As Double = 0
            Dim stmax 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
            sstart -= 1
            num18 = 0.5
            num19 = 0.66
            num26 = 4
            num27 = 0
            Me.funcgrad(x, f, g)
            num = 1
            info = 0
            If ((((((((n <= 0) Or (stp <= 0)) Or (ftol < 0)) Or (gtol < num27)) Or (xtol < num27)) Or (stpmin < num27)) Or (stpmax < stpmin)) Or (maxfev <= 0)) Then
                Return
            End If
            num5 = 0
            index = 1
            Do While (index <= n)
                num5 = (num5 + (g(index) * s((index + sstart))))
                index += 1
            Loop
            If (num5 >= 0) Then
                Return
            End If
            brackt = False
            flag2 = True
            nfev = 0
            num11 = f
            num6 = (ftol * num5)
            num24 = (stpmax - stpmin)
            num25 = (num24 / num18)
            index = 1
            Do While (index <= n)
                wa(index) = x(index)
                index += 1
            Loop
            stx = 0
            fx = num11
            dx = num5
            sty = 0
            fy = num11
            dy = num5
            Do While True
                If brackt Then
                    If (stx < sty) Then
                        stmin = stx
                        stmax = sty
                    Else
                        stmin = sty
                        stmax = stx
                    End If
                Else
                    stmin = stx
                    stmax = (stp + (num26 * (stp - stx)))
                End If
                If (stp > stpmax) Then
                    stp = stpmax
                End If
                If (stp < stpmin) Then
                    stp = stpmin
                End If
                If ((((brackt And ((stp <= stmin) Or (stp >= stmax))) Or (nfev >= (maxfev - 1))) Or (num = 0)) Or (brackt And ((stmax - stmin) <= (xtol * stmax)))) Then
                    stp = stx
                End If
                index = 1
                Do While (index <= n)
                    x(index) = (wa(index) + (stp * s((index + sstart))))
                    index += 1
                Loop
                Me.funcgrad(x, f, g)
                info = 0
                nfev += 1
                num3 = 0
                index = 1
                Do While (index <= n)
                    num3 = (num3 + (g(index) * s((index + sstart))))
                    index += 1
                Loop
                num12 = (num11 + (stp * num6))
                If ((brackt And ((stp <= stmin) Or (stp >= stmax))) Or (num = 0)) Then
                    info = 6
                End If
                If (((stp = stpmax) And (f <= num12)) And (num3 <= num6)) Then
                    info = 5
                End If
                If ((stp = stpmin) And ((f > num12) Or (num3 >= num6))) Then
                    info = 4
                End If
                If (nfev >= maxfev) Then
                    info = 3
                End If
                If (brackt And ((stmax - stmin) <= (xtol * stmax))) Then
                    info = 2
                End If
                If ((f <= num12) And (Math.Abs(num3) <= -(gtol * num5))) Then
                    info = 1
                End If
                If (Not info = 0) Then
                    Return
                End If
                num28 = ftol
                If (gtol < ftol) Then
                    num28 = gtol
                End If
                If ((flag2 And (f <= num12)) And (num3 >= (num28 * num5))) Then
                    flag2 = False
                End If
                If ((flag2 And (f <= fx)) And (f > num12)) Then
                    fp = (f - (stp * num6))
                    num15 = (fx - (stx * num6))
                    num17 = (fy - (sty * num6))
                    dp = (num3 - num6)
                    num8 = (dx - num6)
                    num10 = (dy - num6)
                    Me.lbfgsmcstep(stx, num15, num8, sty, num17, num10, stp, fp, dp, brackt, stmin, stmax, num)
                    fx = (num15 + (stx * num6))
                    fy = (num17 + (sty * num6))
                    dx = (num8 + num6)
                    dy = (num10 + num6)
                Else
                    Me.lbfgsmcstep(stx, fx, dx, sty, fy, dy, stp, f, num3, brackt, stmin, stmax, num)
                End If
                If brackt Then
                    If (Math.Abs(Convert.ToDouble((sty - stx))) >= (num19 * num25)) Then
                        stp = (stx + (num18 * (sty - stx)))
                    End If
                    num25 = num24
                    num24 = Math.Abs(Convert.ToDouble((sty - stx)))
                End If
            Loop
        End Sub

        Private Sub lbfgsmcstep(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 stmin As Double, ByVal stmax As Double, ByRef info As Integer)
            Dim flag As Boolean = False
            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
            info = 0
            If Not (((brackt And ((stp <= Math.Min(stx, sty)) Or (stp >= Math.Max(stx, sty)))) Or ((dx * (stp - stx)) >= 0)) Or (stmax < stmin)) Then
                num6 = (dp * (dx / Math.Abs(dx)))
                If (fp > fx) Then
                    info = 1
                    flag = True
                    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
                    info = 2
                    flag = False
                    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
                    info = 3
                    flag = True
                    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 = stmax
                    Else
                        num7 = stmin
                    End If
                    num9 = (stp + ((dp / (dp - dx)) * (stx - stp)))
                    If brackt Then
                        If (Math.Abs(Convert.ToDouble((stp - num7))) < Math.Abs(Convert.ToDouble((stp - num9)))) Then
                            num8 = num7
                        Else
                            num8 = num9
                        End If
                    ElseIf (Math.Abs(Convert.ToDouble((stp - num7))) > Math.Abs(Convert.ToDouble((stp - num9)))) Then
                        num8 = num7
                    Else
                        num8 = num9
                    End If
                Else
                    info = 4
                    flag = False
                    If 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 = stmax
                    Else
                        num8 = stmin
                    End If
                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
                num8 = Math.Min(stmax, num8)
                num8 = Math.Max(stmin, num8)
                stp = num8
                If (brackt And flag) Then
                    If (sty > stx) Then
                        stp = Math.Min((stx + (0.66 * (sty - stx))), stp)
                    Else
                        stp = Math.Max((stx + (0.66 * (sty - stx))), stp)
                    End If
                End If
            End If
        End Sub

        Public Sub lbfgsminimize(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 info As Integer)
            Dim dx As Double() = New Double(0 - 1) {}
            Dim abort As Boolean = False
            Dim f As Double = 0
            Dim num2 As Double = 0
            Dim num3 As Double = 0
            Dim num4 As Double = 0
            Dim numArray2 As Double()
            Dim numArray3 As Double()
            Dim g As Double()
            Dim wa As Double()
            Dim num6 As Double = 0
            Dim num7 As Double = 0
            Dim ftol As Double = 0
            Dim stp As Double = 0
            Dim num10 As Double = 0
            Dim num11 As Double = 0
            Dim num12 As Double = 0
            Dim num13 As Double = 0
            Dim da As Double = 0
            Dim num16 As Integer = 0
            Dim num17 As Integer = 0
            Dim num18 As Integer = 0
            Dim num19 As Integer = 0
            Dim num20 As Integer = 0
            Dim maxfev As Integer = 0
            Dim num22 As Integer = 0
            Dim num23 As Integer = 0
            Dim num24 As Integer = 0
            Dim index As Integer = 0
            Dim nfev As Integer = 0
            Dim num27 As Integer = 0
            Dim num28 As Integer = 0
            Dim num29 As Integer = 0
            Dim xtol As Double = 0
            Dim gtol As Double = 0
            Dim stpmin As Double = 0
            Dim stpmax As Double = 0
            Dim num34 As Integer = 0
            dx = New Double((((n * ((2 * m) + 1)) + (2 * m)) + 1) - 1) {}
            g = New Double((n + 1) - 1) {}
            numArray2 = New Double((n + 1) - 1) {}
            numArray3 = New Double((n + 1) - 1) {}
            wa = New Double((n + 1) - 1) {}
            Me.funcgrad(x, f, g)
            num2 = f
            num16 = 0
            info = 0
            If (((((((n <= 0) Or (m <= 0)) Or (m > n)) Or (epsg < 0)) Or (epsf < 0)) Or (epsx < 0)) Or (maxits < 0)) Then
                info = -1
                Return
            End If
            num17 = 1
            num18 = 0
            index = 1
            Do While (index <= n)
                wa(index) = 1
                index += 1
            Loop
            xtol = 0.00000000000005
            gtol = 0.9
            stpmin = Math.Pow(10, -20)
            stpmax = Math.Pow(10, 20)
            num19 = (n + (2 * m))
            num20 = (num19 + (n * m))
            index = 1
            Do While (index <= n)
                dx((num19 + index)) = -(g(index) * wa(index))
                index += 1
            Loop
            num6 = Math.Sqrt(Me.lbfgsdotproduct(n, g, 1, g, 1))
            num7 = (1 / num6)
            ftol = 0.0001
            maxfev = 20
Label_0289:
            num34 = 1
            Do While (num34 <= n)
                numArray2(num34) = x(num34)
                num34 += 1
            Loop
            num16 += 1
            info = 0
            num22 = (num16 - 1)
            If (num16 <> 1) Then
                If (num16 > m) Then
                    num22 = m
                End If
                num10 = Me.lbfgsdotproduct(n, dx, ((num20 + num23) + 1), dx, ((num19 + num23) + 1))
                num11 = Me.lbfgsdotproduct(n, dx, ((num20 + num23) + 1), dx, ((num20 + num23) + 1))
                index = 1
                Do While (index <= n)
                    wa(index) = (num10 / num11)
                    index += 1
                Loop
                num24 = num18
                If (num18 = 0) Then
                    num24 = m
                End If
                dx((n + num24)) = (1 / num10)
                index = 1
                Do While (index <= n)
                    dx(index) = -g(index)
                    index += 1
                Loop
                num24 = num18
                index = 1
                Do While (index <= num22)
                    num24 -= 1
                    If (num24 = -1) Then
                        num24 = (m - 1)
                    End If
                    num12 = Me.lbfgsdotproduct(n, dx, ((num19 + (num24 * n)) + 1), dx, 1)
                    num27 = (((n + m) + num24) + 1)
                    num28 = (num20 + (num24 * n))
                    dx(num27) = (dx(((n + num24) + 1)) * num12)
                    Me.lbfgslincomb(n, -dx(num27), dx, (num28 + 1), dx, 1)
                    index += 1
                Loop
                index = 1
                Do While (index <= n)
                    dx(index) = (wa(index) * dx(index))
                    index += 1
                Loop
                index = 1
                Do While (index <= num22)
                    num13 = Me.lbfgsdotproduct(n, dx, ((num20 + (num24 * n)) + 1), dx, 1)
                    da = (dx(((n + num24) + 1)) * num13)
                    num27 = (((n + m) + num24) + 1)
                    da = (dx(num27) - da)
                    num29 = (num19 + (num24 * n))
                    Me.lbfgslincomb(n, da, dx, (num29 + 1), dx, 1)
                    num24 += 1
                    If (num24 = m) Then
                        num24 = 0
                    End If
                    index += 1
                Loop
                index = 1
                Do While (index <= n)
                    dx(((num19 + (num18 * n)) + index)) = dx(index)
                    index += 1
                Loop
            End If
            nfev = 0
            stp = 1
            If (num16 = 1) Then
                stp = num7
            End If
            index = 1
            Do While (index <= n)
                dx(index) = g(index)
                index += 1
            Loop
            Me.lbfgsmcsrch(n, x, f, g, dx, ((num19 + (num18 * n)) + 1), stp, ftol, xtol, maxfev, info, nfev, wa, gtol, stpmin, stpmax)
            If ((Not info = 1) AndAlso (info = 0)) Then
                info = -1
            Else
                num17 = (num17 + nfev)
                num23 = (num18 * n)
                index = 1
                Do While (index <= n)
                    dx(((num19 + num23) + index)) = (stp * dx(((num19 + num23) + index)))
                    dx(((num20 + num23) + index)) = (g(index) - dx(index))
                    index += 1
                Loop
                num18 += 1
                If (num18 = m) Then
                    num18 = 0
                End If
                If ((num16 > maxits) And (maxits > 0)) Then
                    info = 5
                Else
                    Me.lbfgsnewiteration(x, f, g, abort)
                    If abort Then Exit Sub
                    If (Math.Sqrt(Me.lbfgsdotproduct(n, g, 1, g, 1)) <= epsg) Then
                        info = 4
                    Else
                        num3 = Math.Max(Math.Abs(num2), Math.Max(Math.Abs(f), 1))
                        If ((num2 - f) <= (epsf * num3)) Then
                            info = 1
                        Else
                            num34 = 1
                            Do While (num34 <= n)
                                numArray3(num34) = numArray2(num34)
                                num34 += 1
                            Loop
                            num34 = 1
                            Do While (num34 <= n)
                                numArray3(num34) = (numArray3(num34) - x(num34))
                                num34 += 1
                            Loop
                            num4 = Math.Max(Math.Max(Math.Sqrt(Me.lbfgsdotproduct(n, x, 1, x, 1)), Math.Sqrt(Me.lbfgsdotproduct(n, numArray2, 1, numArray2, 1))), 1)
                            If (Math.Sqrt(Me.lbfgsdotproduct(n, numArray3, 1, numArray3, 1)) <= epsx) Then
                                info = 2
                            Else
                                num2 = f
                                num34 = 1
                                Do While (num34 <= n)
                                    numArray2(num34) = x(num34)
                                    num34 += 1
                                Loop
                                GoTo Label_0289
                            End If
                        End If
                    End If
                End If
            End If
        End Sub


    End Class

End Namespace