| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Math::Brent - Single Dimensional Function Minimisation |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Math::Brent qw(Minimise1D); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my ($x, $y) = Minimise1D($guess, $scale, \&func, $tol, $itmax); |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
or |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Math::Brent qw(BracketMinimum Brent); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my ($ax, $bx, $cx, $fa, $fb, $fc) = BracketMinimum($ax, $bx, \&func); |
|
16
|
|
|
|
|
|
|
my ($x, $y) = Brent($ax, $bx, $cx, \&func, $tol, $itmax); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This is an implementation of Brent's method for One-Dimensional |
|
21
|
|
|
|
|
|
|
minimisation of a function without using derivatives. This algorithm |
|
22
|
|
|
|
|
|
|
cleverly uses both the Golden Section Search and parabolic |
|
23
|
|
|
|
|
|
|
interpolation. |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 FUNCTIONS |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
The functions may be imported by name, or by using the export |
|
28
|
|
|
|
|
|
|
tag "all". |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head3 Minimise1D() |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Provides a simple interface to the L and L |
|
33
|
|
|
|
|
|
|
routines. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Given a function, an initial guess for the function's |
|
36
|
|
|
|
|
|
|
minimum, and its scaling, this routine converges |
|
37
|
|
|
|
|
|
|
to the function's minimum using Brent's method. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
($x, $y) = Minimise1D($guess, $scale, \&func); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
The minimum is reached within a certain tolerance (defaulting 1e-7), and |
|
42
|
|
|
|
|
|
|
attempts to do so within a maximum number of iterations (defaulting to 100). |
|
43
|
|
|
|
|
|
|
You may override them by providing alternate values: |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
($x, $y) = Minimise1D($guess, $scale, \&func, 1.5e-8, 120); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head3 Brent() |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Given a function and a triplet of abcissas B<$ax>, B<$bx>, B<$cx>, such that |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over 4 |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item 1. B<$bx> is between B<$ax> and B<$cx>, and |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item 2. B is less than both B and B), |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=back |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Brent() isolates the minimum to a fractional precision of about B<$tol> |
|
60
|
|
|
|
|
|
|
using Brent's method. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
A maximum number of iterations B<$itmax> may be specified for this search - it |
|
63
|
|
|
|
|
|
|
defaults to 100. Returned is a list consisting of the abcissa of the minum |
|
64
|
|
|
|
|
|
|
and the function value there. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head3 BracketMinimum() |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Given a function reference B<\&func> and |
|
69
|
|
|
|
|
|
|
distinct initial points B<$ax> and B<$bx> searches in the downhill |
|
70
|
|
|
|
|
|
|
direction (defined by the function as evaluated at the initial points) |
|
71
|
|
|
|
|
|
|
and returns a list of the three points B<$ax>, B<$bx>, B<$cx> which |
|
72
|
|
|
|
|
|
|
bracket the minimum of the function and the function values at those |
|
73
|
|
|
|
|
|
|
points. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 EXAMPLE |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
use Math::Brent qw(Minimise1D); |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub sinc { |
|
80
|
|
|
|
|
|
|
my $x = shift ; |
|
81
|
|
|
|
|
|
|
return $x ? sin($x)/$x: 1; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my($x, $y) = Minimise1D(1, 1, \&sinc, 1e-7); |
|
85
|
|
|
|
|
|
|
print "Minimum is at sinc($x) = $y\n"; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
produces the output |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Minimum is at sinc(4.4934094397196) = -.217233628211222 |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Anonymous subroutines may also be used as the function reference: |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $cubic_ref = sub {my($x) = @_; return 6.25 + $x*$x*(-24 + $x*8));}; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my($x, $y) = Minimise1D(3, 1, $cubic_ref); |
|
96
|
|
|
|
|
|
|
print "Minimum of the cubic at $x = $y\n"; |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 BUGS |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Please report any bugs or feature requests via Github's L |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 AUTHOR |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
John A.R. Williams B |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
John M. Gamble B (current maintainer) |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
"Numerical Recipies: The Art of Scientific Computing" |
|
112
|
|
|
|
|
|
|
W.H. Press, B.P. Flannery, S.A. Teukolsky, W.T. Vetterling. |
|
113
|
|
|
|
|
|
|
Cambridge University Press. ISBN 0 521 30811 9. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Richard P. Brent, L |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Professor (Emeritus) Richard Brent has a web page at |
|
118
|
|
|
|
|
|
|
L |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
package Math::Brent; |
|
123
|
|
|
|
|
|
|
|
|
124
|
2
|
|
|
2
|
|
40797
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
67
|
|
|
125
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
60
|
|
|
126
|
2
|
|
|
2
|
|
27
|
use 5.8.3; |
|
|
2
|
|
|
|
|
10
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
2
|
|
|
2
|
|
9
|
use Exporter; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
282
|
|
|
129
|
|
|
|
|
|
|
our (@ISA, @EXPORT_OK, %EXPORT_TAGS); |
|
130
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
131
|
|
|
|
|
|
|
%EXPORT_TAGS = ( |
|
132
|
|
|
|
|
|
|
all => [qw( |
|
133
|
|
|
|
|
|
|
FindMinima |
|
134
|
|
|
|
|
|
|
BracketMinimum |
|
135
|
|
|
|
|
|
|
Brent Minimise1D |
|
136
|
|
|
|
|
|
|
) ], |
|
137
|
|
|
|
|
|
|
); |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
@EXPORT_OK = ( @{ $EXPORT_TAGS{all} } ); |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
our $VERSION = 0.04; |
|
142
|
|
|
|
|
|
|
|
|
143
|
2
|
|
|
2
|
|
1316
|
use Math::VecStat qw(max min); |
|
|
2
|
|
|
|
|
2547
|
|
|
|
2
|
|
|
|
|
181
|
|
|
144
|
2
|
|
|
2
|
|
1145
|
use Math::Fortran qw(sign); |
|
|
2
|
|
|
|
|
969
|
|
|
|
2
|
|
|
|
|
134
|
|
|
145
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
1983
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub Minimise1D |
|
148
|
|
|
|
|
|
|
{ |
|
149
|
5
|
|
|
5
|
1
|
1534
|
my ($guess, $scale, $func, $tol, $itmax) = @_; |
|
150
|
5
|
|
|
|
|
17
|
my ($a, $b, $c) = BracketMinimum($guess - $scale, $guess + $scale, $func); |
|
151
|
|
|
|
|
|
|
|
|
152
|
5
|
|
|
|
|
13
|
return Brent($a, $b, $c, $func, $tol, $itmax); |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# |
|
156
|
|
|
|
|
|
|
# BracketMinimum |
|
157
|
|
|
|
|
|
|
# |
|
158
|
|
|
|
|
|
|
# BracketMinimum is MNBRAK minimum bracketing routine from section 10.1 |
|
159
|
|
|
|
|
|
|
# of Numerical Recipies |
|
160
|
|
|
|
|
|
|
# |
|
161
|
|
|
|
|
|
|
# Given a function func, and distinct initial points ax & bx this |
|
162
|
|
|
|
|
|
|
# routine searches in the downhill direction and returns new points ax, |
|
163
|
|
|
|
|
|
|
# bx, cx which bracket the minimum. The function values at the 3 points |
|
164
|
|
|
|
|
|
|
# are returned in fa, fb, fc respectively. |
|
165
|
|
|
|
|
|
|
# |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $GOLD = 0.5 + sqrt(1.25); # Default magnification ratio for intervals is phi. |
|
168
|
|
|
|
|
|
|
my $GLIMIT = 100.0; # Max magnification for parabolic fit step |
|
169
|
|
|
|
|
|
|
my $TINY = 1E-20; |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub BracketMinimum |
|
172
|
|
|
|
|
|
|
{ |
|
173
|
5
|
|
|
5
|
1
|
7
|
my ($ax, $bx, $func) = @_; |
|
174
|
5
|
|
|
|
|
14
|
my ($fa, $fb) = (&$func($ax), &$func($bx)); |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# |
|
177
|
|
|
|
|
|
|
# Swap the a and b values if we weren't going in |
|
178
|
|
|
|
|
|
|
# a downhill direction. |
|
179
|
|
|
|
|
|
|
# |
|
180
|
5
|
100
|
|
|
|
85
|
if ($fb > $fa) |
|
181
|
|
|
|
|
|
|
{ |
|
182
|
2
|
|
|
|
|
4
|
my $t = $ax; $ax = $bx; $bx = $t; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
3
|
|
|
183
|
2
|
|
|
|
|
3
|
$t = $fa; $fa = $fb; $fb = $t; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
4
|
|
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
|
|
186
|
5
|
|
|
|
|
12
|
my $cx = $bx + $GOLD * ($bx - $ax); |
|
187
|
5
|
|
|
|
|
9
|
my $fc = &$func($cx); |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Loop here until we bracket |
|
190
|
5
|
|
|
|
|
43
|
while ($fb >= $fc) |
|
191
|
|
|
|
|
|
|
{ |
|
192
|
|
|
|
|
|
|
# |
|
193
|
|
|
|
|
|
|
# Compute U by parabolic extrapolation from |
|
194
|
|
|
|
|
|
|
# a, b, c. TINY used to prevent div by zero |
|
195
|
|
|
|
|
|
|
# |
|
196
|
2
|
|
|
|
|
7
|
my $r = ($bx - $ax) * ($fb - $fc); |
|
197
|
2
|
|
|
|
|
4
|
my $q = ($bx - $cx) * ($fb - $fa); |
|
198
|
2
|
|
|
|
|
14
|
my $u = $bx - (($bx - $cx) * $q - ($bx - $ax) * $r)/ |
|
199
|
|
|
|
|
|
|
(2.0 * sign(max(abs($q - $r), $TINY), $q - $r)); |
|
200
|
|
|
|
|
|
|
|
|
201
|
2
|
|
|
|
|
56
|
my $ulim = $bx + $GLIMIT * ($cx - $bx); # We won't go further than this |
|
202
|
2
|
|
|
|
|
2
|
my $fu; |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# |
|
205
|
|
|
|
|
|
|
# Parabolic U between B & C - try it |
|
206
|
|
|
|
|
|
|
# |
|
207
|
2
|
50
|
|
|
|
15
|
if (($bx - $u) * ($u - $cx) > 0.0) |
|
|
|
50
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
{ |
|
209
|
0
|
|
|
|
|
0
|
$fu = &$func($u); |
|
210
|
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
0
|
if ($fu < $fc) |
|
|
|
0
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
{ |
|
213
|
|
|
|
|
|
|
# Minimum between B & C |
|
214
|
0
|
|
|
|
|
0
|
$ax = $bx; $fa = $fb; $bx = $u; $fb = $fu; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
215
|
0
|
|
|
|
|
0
|
next; |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
elsif ($fu > $fb) |
|
218
|
|
|
|
|
|
|
{ |
|
219
|
|
|
|
|
|
|
# Minimum between A & U |
|
220
|
0
|
|
|
|
|
0
|
$cx = $u; $fc = $fu; |
|
|
0
|
|
|
|
|
0
|
|
|
221
|
0
|
|
|
|
|
0
|
next; |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
0
|
$u = $cx + $GOLD * ($cx - $bx); |
|
225
|
0
|
|
|
|
|
0
|
$fu = &$func($u); |
|
226
|
|
|
|
|
|
|
} |
|
227
|
|
|
|
|
|
|
elsif (($cx - $u) * ($u - $ulim) > 0) |
|
228
|
|
|
|
|
|
|
{ |
|
229
|
|
|
|
|
|
|
# parabolic fit between C and limit |
|
230
|
2
|
|
|
|
|
8
|
$fu = &$func($u); |
|
231
|
|
|
|
|
|
|
|
|
232
|
2
|
50
|
|
|
|
18
|
if ($fu < $fc) |
|
233
|
|
|
|
|
|
|
{ |
|
234
|
0
|
|
|
|
|
0
|
$bx = $cx; $cx = $u; |
|
|
0
|
|
|
|
|
0
|
|
|
235
|
0
|
|
|
|
|
0
|
$u = $cx + $GOLD * ($cx - $bx); |
|
236
|
0
|
|
|
|
|
0
|
$fb = $fc; $fc = $fu; |
|
|
0
|
|
|
|
|
0
|
|
|
237
|
0
|
|
|
|
|
0
|
$fu = &$func($u); |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
elsif (($u - $ulim) * ($ulim - $cx) >= 0) |
|
241
|
|
|
|
|
|
|
{ |
|
242
|
|
|
|
|
|
|
# Limit parabolic U to maximum |
|
243
|
0
|
|
|
|
|
0
|
$u = $ulim; |
|
244
|
0
|
|
|
|
|
0
|
$fu = &$func($u); |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
else |
|
247
|
|
|
|
|
|
|
{ |
|
248
|
|
|
|
|
|
|
# eject parabolic U, use default magnification |
|
249
|
0
|
|
|
|
|
0
|
$u = $cx + $GOLD * ($cx - $bx); |
|
250
|
0
|
|
|
|
|
0
|
$fu = &$func($u); |
|
251
|
|
|
|
|
|
|
} |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# Eliminate oldest point & continue |
|
254
|
2
|
|
|
|
|
5
|
$ax = $bx; $bx = $cx; $cx = $u; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
2
|
|
|
255
|
2
|
|
|
|
|
3
|
$fa = $fb; $fb = $fc; $fc = $fu; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
6
|
|
|
256
|
|
|
|
|
|
|
} |
|
257
|
|
|
|
|
|
|
|
|
258
|
5
|
|
|
|
|
18
|
return ($ax, $bx, $cx, $fa, $fb, $fc); |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# |
|
262
|
|
|
|
|
|
|
# The complementary step is (3 - sqrt(5))/2, which resolves to 2 - phi. |
|
263
|
|
|
|
|
|
|
# |
|
264
|
|
|
|
|
|
|
my $CGOLD = 2 - $GOLD; |
|
265
|
|
|
|
|
|
|
my $ZEPS = 1e-10; |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub Brent |
|
268
|
|
|
|
|
|
|
{ |
|
269
|
5
|
|
|
5
|
1
|
7
|
my ($ax, $bx, $cx, $func, $tol, $ITMAX) = @_; |
|
270
|
5
|
|
|
|
|
41
|
my ($d, $u, $x, $w, $v); # ordinates |
|
271
|
0
|
|
|
|
|
0
|
my ($fu, $fx, $fw, $fv); # function evaluations |
|
272
|
|
|
|
|
|
|
|
|
273
|
5
|
50
|
|
|
|
14
|
$ITMAX = 100 unless (defined $ITMAX); |
|
274
|
5
|
100
|
|
|
|
16
|
$tol = 1e-8 unless (defined $tol); |
|
275
|
|
|
|
|
|
|
|
|
276
|
5
|
|
|
|
|
15
|
my $a = min($ax, $cx); |
|
277
|
5
|
|
|
|
|
79
|
my $b = max($ax, $cx); |
|
278
|
|
|
|
|
|
|
|
|
279
|
5
|
|
|
|
|
54
|
$x = $w = $v = $bx; |
|
280
|
5
|
|
|
|
|
12
|
$fx = $fw = $fv = &$func($x); |
|
281
|
5
|
|
|
|
|
26
|
my $e = 0.0; # will be distance moved on the step before last |
|
282
|
5
|
|
|
|
|
6
|
my $iter = 0; |
|
283
|
|
|
|
|
|
|
|
|
284
|
5
|
|
|
|
|
14
|
while ($iter < $ITMAX) |
|
285
|
|
|
|
|
|
|
{ |
|
286
|
49
|
|
|
|
|
86
|
my $xm = 0.5 * ($a + $b); |
|
287
|
49
|
|
|
|
|
69
|
my $tol1 = $tol * abs($x) + $ZEPS; |
|
288
|
49
|
|
|
|
|
51
|
my $tol2 = 2.0 * $tol1; |
|
289
|
|
|
|
|
|
|
|
|
290
|
49
|
100
|
|
|
|
125
|
last if (abs($x - $xm) <= ($tol2 - 0.5 * ($b - $a))); |
|
291
|
|
|
|
|
|
|
|
|
292
|
44
|
100
|
|
|
|
74
|
if (abs($e) > $tol1) |
|
293
|
|
|
|
|
|
|
{ |
|
294
|
39
|
|
|
|
|
49
|
my $r = ($x-$w) * ($fx-$fv); |
|
295
|
39
|
|
|
|
|
44
|
my $q = ($x-$v) * ($fx-$fw); |
|
296
|
39
|
|
|
|
|
51
|
my $p = ($x-$v) * $q-($x-$w)*$r; |
|
297
|
|
|
|
|
|
|
|
|
298
|
39
|
100
|
|
|
|
78
|
$p = -$p if (($q = 2 * ($q - $r)) > 0.0); |
|
299
|
|
|
|
|
|
|
|
|
300
|
39
|
|
|
|
|
33
|
$q = abs($q); |
|
301
|
39
|
|
|
|
|
34
|
my $etemp = $e; |
|
302
|
39
|
|
|
|
|
33
|
$e = $d; |
|
303
|
|
|
|
|
|
|
|
|
304
|
39
|
50
|
66
|
|
|
259
|
unless ( (abs($p) >= abs(0.5 * $q * $etemp)) || |
|
|
|
|
66
|
|
|
|
|
|
305
|
|
|
|
|
|
|
($p <= $q * ($a - $x)) || ($p >= $q * ($b - $x)) ) |
|
306
|
|
|
|
|
|
|
{ |
|
307
|
|
|
|
|
|
|
# |
|
308
|
|
|
|
|
|
|
# Parabolic step OK here - take it. |
|
309
|
|
|
|
|
|
|
# |
|
310
|
34
|
|
|
|
|
34
|
$d = $p/$q; |
|
311
|
34
|
|
|
|
|
32
|
$u = $x + $d; |
|
312
|
|
|
|
|
|
|
|
|
313
|
34
|
100
|
100
|
|
|
125
|
if ( (($u - $a) < $tol2) || (($b - $u) < $tol2) ) |
|
314
|
|
|
|
|
|
|
{ |
|
315
|
5
|
|
|
|
|
14
|
$d = sign($tol1, $xm - $x); |
|
316
|
|
|
|
|
|
|
} |
|
317
|
34
|
|
|
|
|
317
|
goto dcomp; # Skip the golden section step. |
|
318
|
|
|
|
|
|
|
} |
|
319
|
|
|
|
|
|
|
} |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# |
|
322
|
|
|
|
|
|
|
# Golden section step. |
|
323
|
|
|
|
|
|
|
# |
|
324
|
10
|
100
|
|
|
|
20
|
$e = (($x >= $xm) ? $a : $b) - $x; |
|
325
|
10
|
|
|
|
|
9
|
$d = $CGOLD * $e; |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
# |
|
328
|
|
|
|
|
|
|
# We arrive here with d from Golden section or parabolic step. |
|
329
|
|
|
|
|
|
|
# |
|
330
|
44
|
100
|
|
|
|
92
|
dcomp: |
|
331
|
|
|
|
|
|
|
$u = $x + ((abs($d) >= $tol1) ? $d : sign($tol1, $d)); |
|
332
|
44
|
|
|
|
|
111
|
$fu = &$func($u); # 1 &$function evaluation per iteration |
|
333
|
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
# |
|
335
|
|
|
|
|
|
|
# Decide what to do with &$function evaluation |
|
336
|
|
|
|
|
|
|
# |
|
337
|
44
|
100
|
|
|
|
238
|
if ($fu <= $fx) |
|
338
|
|
|
|
|
|
|
{ |
|
339
|
33
|
100
|
|
|
|
46
|
if ($u >= $x) |
|
340
|
|
|
|
|
|
|
{ |
|
341
|
14
|
|
|
|
|
15
|
$a = $x; |
|
342
|
|
|
|
|
|
|
} |
|
343
|
|
|
|
|
|
|
else |
|
344
|
|
|
|
|
|
|
{ |
|
345
|
19
|
|
|
|
|
22
|
$b = $x; |
|
346
|
|
|
|
|
|
|
} |
|
347
|
33
|
|
|
|
|
31
|
$v = $w; $fv = $fw; |
|
|
33
|
|
|
|
|
33
|
|
|
348
|
33
|
|
|
|
|
29
|
$w = $x; $fw = $fx; |
|
|
33
|
|
|
|
|
29
|
|
|
349
|
33
|
|
|
|
|
29
|
$x = $u; $fx = $fu; |
|
|
33
|
|
|
|
|
32
|
|
|
350
|
|
|
|
|
|
|
} |
|
351
|
|
|
|
|
|
|
else |
|
352
|
|
|
|
|
|
|
{ |
|
353
|
11
|
100
|
|
|
|
17
|
if ($u < $x) |
|
354
|
|
|
|
|
|
|
{ |
|
355
|
5
|
|
|
|
|
6
|
$a = $u; |
|
356
|
|
|
|
|
|
|
} |
|
357
|
|
|
|
|
|
|
else |
|
358
|
|
|
|
|
|
|
{ |
|
359
|
6
|
|
|
|
|
7
|
$b = $u; |
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
|
|
362
|
11
|
100
|
100
|
|
|
60
|
if ($fu <= $fw || $w == $x) |
|
|
|
50
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
363
|
|
|
|
|
|
|
{ |
|
364
|
7
|
|
|
|
|
10
|
$v = $w; $fv = $fw; |
|
|
7
|
|
|
|
|
5
|
|
|
365
|
7
|
|
|
|
|
7
|
$w = $u; $fw = $fu; |
|
|
7
|
|
|
|
|
8
|
|
|
366
|
|
|
|
|
|
|
} |
|
367
|
|
|
|
|
|
|
elsif ( $fu <= $fv || $v == $x || $v == $w ) |
|
368
|
|
|
|
|
|
|
{ |
|
369
|
4
|
|
|
|
|
4
|
$v = $u; $fv = $fu; |
|
|
4
|
|
|
|
|
5
|
|
|
370
|
|
|
|
|
|
|
} |
|
371
|
|
|
|
|
|
|
} |
|
372
|
|
|
|
|
|
|
|
|
373
|
44
|
|
|
|
|
99
|
$iter++; |
|
374
|
|
|
|
|
|
|
} |
|
375
|
|
|
|
|
|
|
|
|
376
|
5
|
50
|
|
|
|
19
|
carp "Brent Exceed Maximum Iterations.\n" if ($iter >= $ITMAX); |
|
377
|
5
|
|
|
|
|
21
|
return ($x, $fx); |
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
1; |