File size: 10,390 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
'    Miscelaneous Math Functions for DWSIM
'    Copyright 2008 Daniel Wagner O. de Medeiros
'
'    This file is part of DWSIM.
'
'    DWSIM is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    DWSIM is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with DWSIM.  If not, see <http://www.gnu.org/licenses/>.

Namespace MathEx.BrentOpt

    Public Class BrentMinimize

        Public fc As funcdelegate
        Delegate Function funcdelegate(ByVal x As Double) As Double

        Sub New()

        End Sub

        Sub DefineFuncDelegate(ByVal fg As funcdelegate)
            Me.fc = fg
        End Sub

        Function f(ByVal x As Double) As Double
            Return fc.Invoke(x)
        End Function

        '************************************************************************
        '    Minimization by the Brent method
        '
        '    Input parameters:
        '        a - left boundary of an interval to search minimum in.
        '        b - right boundary of an interval to search minimum in.
        '        Epsilon � absolute error of the value of the function minimum.
        '
        '    Output parameters:
        '        XMin - point of minimum.
        '
        '    The result:
        '        function value at the point of minimum.
        '    ************************************************************************

        Public Function brentoptimize(ByVal a As Double, ByVal b As Double, ByVal epsilon As Double, ByRef xmin As Double) As Double
            Dim result As Double = 0
            Dim ia As Double = 0
            Dim ib As Double = 0
            Dim bx As Double = 0
            Dim d As Double = 0
            Dim e As Double = 0
            Dim etemp As Double = 0
            Dim fu As Double = 0
            Dim fv As Double = 0
            Dim fw As Double = 0
            Dim fx As Double = 0
            Dim iter As Integer = 0
            Dim p As Double = 0
            Dim q As Double = 0
            Dim r As Double = 0
            Dim u As Double = 0
            Dim v As Double = 0
            Dim w As Double = 0
            Dim x As Double = 0
            Dim xm As Double = 0
            Dim cgold As Double = 0

            cgold = 0.381966
            bx = 0.5 * (a + b)
            If a < b Then
                ia = a
            Else
                ia = b
            End If
            If a > b Then
                ib = a
            Else
                ib = b
            End If
            v = bx
            w = v
            x = v
            e = 0.0R
            fx = f(x)
            fv = fx
            fw = fx
            For iter = 1 To 100
                xm = 0.5 * (ia + ib)
                If Math.Abs(x - xm) <= epsilon * 2 - 0.5 * (ib - ia) Then
                    Exit For
                End If
                If Math.Abs(e) > epsilon Then
                    r = (x - w) * (fx - fv)
                    q = (x - v) * (fx - fw)
                    p = (x - v) * q - (x - w) * r
                    q = 2 * (q - r)
                    If q > 0 Then
                        p = -p
                    End If
                    q = Math.Abs(q)
                    etemp = e
                    e = d
                    If Not (Math.Abs(p) >= Math.Abs(0.5 * q * etemp) Or p <= q * (ia - x) Or p >= q * (ib - x)) Then
                        d = p / q
                        u = x + d
                        If u - ia < epsilon * 2 Or ib - u < epsilon * 2 Then
                            d = mysign(epsilon, xm - x)
                        End If
                    Else
                        If x >= xm Then
                            e = ia - x
                        Else
                            e = ib - x
                        End If
                        d = cgold * e
                    End If
                Else
                    If x >= xm Then
                        e = ia - x
                    Else
                        e = ib - x
                    End If
                    d = cgold * e
                End If
                If Math.Abs(d) >= epsilon Then
                    u = x + d
                Else
                    u = x + mysign(epsilon, d)
                End If
                fu = f(u)
                If Double.IsNaN(fu) Then Exit For
                If fu <= fx Then
                    If u >= x Then
                        ia = x
                    Else
                        ib = x
                    End If
                    v = w
                    fv = fw
                    w = x
                    fw = fx
                    x = u
                    fx = fu
                Else
                    If u < x Then
                        ia = u
                    Else
                        ib = u
                    End If
                    If fu <= fw Or w = x Then
                        v = w
                        fv = fw
                        w = u
                        fw = fu
                    Else
                        If fu <= fv Or v = x Or v = 2 Then
                            v = u
                            fv = fu
                        End If
                    End If
                End If
            Next
            xmin = x
            result = fx
            Return result
        End Function

        Public Function brentoptimize2(ByVal a As Double, ByVal b As Double, ByVal epsilon As Double, ByVal func As Func(Of Double, Double)) As Double
            Dim result As Double = 0
            Dim ia As Double = 0
            Dim ib As Double = 0
            Dim bx As Double = 0
            Dim d As Double = 0
            Dim e As Double = 0
            Dim etemp As Double = 0
            Dim fu As Double = 0
            Dim fv As Double = 0
            Dim fw As Double = 0
            Dim fx As Double = 0
            Dim iter As Integer = 0
            Dim p As Double = 0
            Dim q As Double = 0
            Dim r As Double = 0
            Dim u As Double = 0
            Dim v As Double = 0
            Dim w As Double = 0
            Dim x As Double = 0
            Dim xm As Double = 0
            Dim cgold As Double = 0

            cgold = 0.381966
            bx = 0.5 * (a + b)
            If a < b Then
                ia = a
            Else
                ia = b
            End If
            If a > b Then
                ib = a
            Else
                ib = b
            End If
            v = bx
            w = v
            x = v
            e = 0.0R
            fx = func(x)
            fv = fx
            fw = fx
            For iter = 1 To 100
                xm = 0.5 * (ia + ib)
                If Math.Abs(x - xm) <= epsilon * 2 - 0.5 * (ib - ia) Then
                    Exit For
                End If
                If Math.Abs(e) > epsilon Then
                    r = (x - w) * (fx - fv)
                    q = (x - v) * (fx - fw)
                    p = (x - v) * q - (x - w) * r
                    q = 2 * (q - r)
                    If q > 0 Then
                        p = -p
                    End If
                    q = Math.Abs(q)
                    etemp = e
                    e = d
                    If Not (Math.Abs(p) >= Math.Abs(0.5 * q * etemp) Or p <= q * (ia - x) Or p >= q * (ib - x)) Then
                        d = p / q
                        u = x + d
                        If u - ia < epsilon * 2 Or ib - u < epsilon * 2 Then
                            d = mysign(epsilon, xm - x)
                        End If
                    Else
                        If x >= xm Then
                            e = ia - x
                        Else
                            e = ib - x
                        End If
                        d = cgold * e
                    End If
                Else
                    If x >= xm Then
                        e = ia - x
                    Else
                        e = ib - x
                    End If
                    d = cgold * e
                End If
                If Math.Abs(d) >= epsilon Then
                    u = x + d
                Else
                    u = x + mysign(epsilon, d)
                End If
                fu = func(u)
                If Double.IsNaN(fu) Then Exit For
                If fu <= fx Then
                    If u >= x Then
                        ia = x
                    Else
                        ib = x
                    End If
                    v = w
                    fv = fw
                    w = x
                    fw = fx
                    x = u
                    fx = fu
                Else
                    If u < x Then
                        ia = u
                    Else
                        ib = u
                    End If
                    If fu <= fw Or w = x Then
                        v = w
                        fv = fw
                        w = u
                        fw = fu
                    Else
                        If fu <= fv Or v = x Or v = 2 Then
                            v = u
                            fv = fu
                        End If
                    End If
                End If
            Next
            Return x
        End Function


        Private Shared Function mysign(ByVal a As Double, ByVal b As Double) As Double
            Dim result As Double = 0

            If b > 0 Then
                result = Math.Abs(a)
            Else
                result = -Math.Abs(a)
            End If
            Return result
        End Function

    End Class

End Namespace