blib/lib/Math/Brent.pm

filter: Statementstmt 85.7% 168 / 196 Branchbran 74.2% 49 / 66 Conditioncond 76.0% 38 / 50 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 79.6% 281 / 353 CC 52 SCAR 30.0
CC
52
Cov
82%
Brentzero
24.4
Brent
23.2
BracketMinimum
17.8
CRAP 20.0
↑ Summary
1 package Math::Brent;
2  
3 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 our (@ISA, @EXPORT_OK, %EXPORT_TAGS);
9 4 @ISA = qw(Exporter);
10 4 %EXPORT_TAGS = (
11 all => [qw(
12 BracketMinimum
13 Brent Minimise1D
14 Brentzero
15 ) ],
16 );
17  
18 4 @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
19  
20 4 our $VERSION = 1.00;
21  
22 4 ▶use Math::VecStat qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Math::Utils qw(:fortran);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 #use Smart::Comments ('###', '####'); # 3 for variables, 4 for 'here we are'.
26  
27 =head1 NAME
28  
29 Math::Brent - Brent's single dimensional function minimisation, and Brent's zero finder.
30  
31 =head1 SYNOPSIS
32  
33 use Math::Brent qw(Minimise1D);
34  
35 my $tolerance = 1e-7;
36 my $itmax = 80;
37  
38 sub sinc {
39 my $x = shift ;
40 return $x ? sin($x)/$x: 1;
41 }
42  
43 my($x, $y) = Minimise1D(1, 1, \&sinc, $tolerance, $itmax);
44  
45 print "Minimum is at sinc($x) = $y\n";
46  
47 or
48  
49 use Math::Brent qw(BracketMinimum Brent);
50  
51 my $tolerance = 1e-7;
52 my $itmax = 80;
53  
54 #
55 # If you want to use the separate functions
56 # instead of a single call to Minimise1D().
57 #
58 my($ax, $bx, $cx, $fa, $fb, $fc) = BracketMinimum($ax, $bx, \&sinc);
59 my($x, $y) = Brent($ax, $bx, $cx, \&sinc, $tolerance, $itmax);
60  
61 print "Minimum is at sinc($x) = $y\n";
62  
63 In either case the output will be C<Minimum is at sinc(4.4934094397196) = -.217233628211222>
64  
65 This module has implementations of Brent's method for one-dimensional
66 minimisation of a function without using derivatives. This algorithm
67 cleverly uses both the Golden Section Search and parabolic
68 interpolation.
69  
70 Anonymous subroutines may also be used as the function reference:
71  
72 my $cubic_ref = sub {my($x) = @_; return 6.25 + $x*$x*(-24 + $x*8));};
73  
74 my($x, $y) = Minimise1D(3, 1, $cubic_ref);
75 print "Minimum of the cubic at $x = $y\n";
76  
77 In addition to finding the minimum, there is also an implementation of the
78 Van Wijngaarden-Dekker-Brent Method, used to find a function's root without
79 using derivatives.
80  
81 use Math::Brent qw(Brentzero);
82  
83 my $tolerance = 1e-7;
84 my $itmax = 80;
85  
86 sub wobble
87 {
88 my($t) = @_;
89 return $t - cos($t);
90 }
91  
92 #
93 # Find the zero somewhere between .5 and 1.
94 #
95 $r = Brentzero(0.5, 1.0, \&wobble, $tolerance, $itmax);
96  
97 =head1 EXPORT
98  
99 Each function can be exported by name, or all may be exported by using the tag 'all'.
100  
101 =head2 FUNCTIONS
102  
103 The functions may be imported by name, or by using the export
104 tag "all".
105  
106 =cut
107  
108 =head3 Minimise1D()
109  
110 Provides a simple interface to the L</BracketMinimum()> and L</Brent()>
111 routines.
112  
113 Given a function, an initial guess for the function's
114 minimum, and its scaling, this routine converges
115 to the function's minimum using Brent's method.
116  
117 ($x, $y) = Minimise1D($guess, $scale, \&func);
118  
119 The minimum is reached within a certain tolerance (defaulting 1e-7), and
120 attempts to do so within a maximum number of iterations (defaulting to 100).
121 You may override them by providing alternate values:
122  
123 ($x, $y) = Minimise1D($guess, $scale, \&func, 1.5e-8, 120);
124  
125 =cut
126  
127 sub Minimise1D
128 5 ▶{
Subroutine Minimise1D: called CC=1 SCAR=0.0
Pod documented
129 5 my ($guess, $scale, $func, $tol, $itmax) = @_;
130 5 my ($a, $b, $c) = BracketMinimum($guess - $scale, $guess + $scale, $func);
131  
132 5 return Brent($a, $b, $c, $func, $tol, $itmax);
133 }
134  
135 #
136 # BracketMinimum
137 #
138 # BracketMinimum is MNBRAK minimum bracketing routine from section 10.1
139 # of Numerical Recipies
140 #
141  
142 4 my $GOLD = 0.5 + sqrt(1.25); # Default magnification ratio for intervals is phi.
143 4 my $GLIMIT = 100.0; # Max magnification for parabolic fit step
144 4 my $TINY = 1E-20;
145  
146 =head3 BracketMinimum()
147  
148 ($ax, $bx, $cx, $fa, $fb, $fc) = BracketMinimum($ax, $bx);
149  
150 Given a function reference B<\&func> and distinct initial points B<$ax>
151 and B<$bx>, this routine searches in the downhill direction and returns
152 a list of the three points B<$ax>, B<$bx>, B<$cx> which bracket the
153 minimum of the function, along with the function values at those three
154 points, $fa, $fb, $fc.
155  
156 The points B<$ax>, B<$bx>, B<$cx> may then be used in the function Brent().
157  
158 =cut
159  
160 sub BracketMinimum
161 5 ▶{
Subroutine BracketMinimum: called CC=9 SCAR=28.8
Pod documented
162 5 my ($ax, $bx, $func) = @_;
163 5 my ($fa, $fb) = (&$func($ax), &$func($bx));
164  
165 #
166 # Swap the a and b values if we weren't going in
167 # a downhill direction.
168 #
169 5 ▶ if ($fb > $fa)
Branch: 1 branch
BranchTrueFalseTotal
if ($fb > $fa) 2 3 5
170 {
171 2 my $t = $ax; $ax = $bx; $bx = $t;
172 2 $t = $fa; $fa = $fb; $fb = $t;
173 }
174  
175 5 my $cx = $bx + $GOLD * ($bx - $ax);
176 5 my $fc = &$func($cx);
177  
178 #
179 # Loop here until we bracket
180 #
181 5 ▶ while ($fb >= $fc)
Branch: 1 branch
BranchTrueFalseTotal
if ($fb >= $fc) 2 5 7
182 {
183 #
184 # Compute U by parabolic extrapolation from
185 # a, b, c. TINY used to prevent div by zero
186 #
187 2 my $r = ($bx - $ax) * ($fb - $fc);
188 2 my $q = ($bx - $cx) * ($fb - $fa);
189 2 my $u = $bx - (($bx - $cx) * $q - ($bx - $ax) * $r)/
190 (2.0 * copysign(max(abs($q - $r), $TINY), $q - $r));
191  
192 2 my $ulim = $bx + $GLIMIT * ($cx - $bx); # We won't go further than this
193 2 my $fu;
194  
195 #
196 # Parabolic U between B and C - try it.
197 #
198 2 ▶ if (($bx - $u) * ($u - $cx) > 0.0)
Branch: 3 branches
BranchTrueFalseTotal
if (($bx - $u) * ($u - $cx) > 0.0) { } 0 2 2
elsif (($cx - $u) * ($u - $ulim) > 0) { } 2 0 2
elsif (($u - $ulim) * ($ulim - $cx) >= 0) { } 0 0 0
199 {
200 0 $fu = &$func($u);
201  
202 0 ▶ if ($fu < $fc)
Branch: 2 branches
BranchTrueFalseTotal
if ($fu < $fc) { } 0 0 0
elsif ($fu > $fb) { } 0 0 0
203 {
204 # Minimum between B and C
205 0 $ax = $bx; $fa = $fb; $bx = $u; $fb = $fu;
206 0 next;
207 }
208 elsif ($fu > $fb)
209 {
210 # Minimum between A and U
211 0 $cx = $u; $fc = $fu;
212 0 next;
213 }
214  
215 0 $u = $cx + $GOLD * ($cx - $bx);
216 0 $fu = &$func($u);
217 }
218 elsif (($cx - $u) * ($u - $ulim) > 0)
219 {
220 # parabolic fit between C and limit
221 2 $fu = &$func($u);
222  
223 2 ▶ if ($fu < $fc)
Branch: 1 branch
BranchTrueFalseTotal
if ($fu < $fc) 0 2 2
224 {
225 0 $bx = $cx; $cx = $u;
226 0 $u = $cx + $GOLD * ($cx - $bx);
227 0 $fb = $fc; $fc = $fu;
228 0 $fu = &$func($u);
229 }
230 }
231 elsif (($u - $ulim) * ($ulim - $cx) >= 0)
232 {
233 # Limit parabolic U to maximum
234 0 $u = $ulim;
235 0 $fu = &$func($u);
236 }
237 else
238 {
239 # eject parabolic U, use default magnification
240 0 $u = $cx + $GOLD * ($cx - $bx);
241 0 $fu = &$func($u);
242 }
243  
244 # Eliminate oldest point and continue
245 2 $ax = $bx; $bx = $cx; $cx = $u;
246 2 $fa = $fb; $fb = $fc; $fc = $fu;
247 }
248  
249 5 return ($ax, $bx, $cx, $fa, $fb, $fc);
250 }
251  
252 #
253 # The complementary step is (3 - sqrt(5))/2, which resolves to 2 - phi.
254 #
255 4 my $CGOLD = 2 - $GOLD;
256 4 my $ZEPS = 1e-10;
257  
258 =head3 Brent()
259  
260 Given a function and a triplet of abcissas B<$ax>, B<$bx>, B<$cx>, such that
261  
262 =over 4
263  
264 =item 1. B<$bx> is between B<$ax> and B<$cx>, and
265  
266 =item 2. B<func($bx)> is less than both B<func($ax)> and B<func($cx)>),
267  
268 =back
269  
270 Brent() isolates the minimum to a fractional precision of about B<$tol>
271 using Brent's method.
272  
273 A maximum number of iterations B<$itmax> may be specified for this search - it
274 defaults to 100. Returned is a list consisting of the abcissa of the minum
275 and the function value there.
276  
277 =cut
278  
279 sub Brent
280 5 ▶{
Subroutine Brent: called CC=23 SCAR=31.5
Pod documented
281 5 my ($ax, $bx, $cx, $func, $tol, $ITMAX) = @_;
282 5 my ($d, $u, $x, $w, $v); # ordinates
283 5 my ($fu, $fx, $fw, $fv); # function evaluations
284  
285 5 ▶ $ITMAX //= 100;
Condition50%1/2
$ITMAX //= 100
l!l
05
MC/DC0%0/1
$ITMAX //= 100
$ITMAX
Truth table50%1/2
A
A= $ITMAX
Aresult
11
00
286 5 ▶ $tol //= 1e-8;
Condition100%2/2
$tol //= "1e-08"
l!l
14
MC/DC100%1/1
$tol //= "1e-08"
$tol
Truth table100%2/2
A
A= $tol
Aresult
11
00
287  
288 5 my $a = min($ax, $cx);
289 5 my $b = max($ax, $cx);
290  
291 5 $x = $w = $v = $bx;
292 5 $fx = $fw = $fv = &$func($x);
293 5 my $e = 0.0; # will be distance moved on the step before last
294 5 my $iter = 0;
295  
296 5 ▶ while ($iter < $ITMAX)
Branch: 1 branch
BranchTrueFalseTotal
if ($iter < $ITMAX) 49 0 49
297 {
298 49 my $xm = 0.5 * ($a + $b);
299 49 my $tol1 = $tol * abs($x) + $ZEPS;
300 49 my $tol2 = 2.0 * $tol1;
301  
302 49 ▶ last if (abs($x - $xm) <= ($tol2 - 0.5 * ($b - $a)));
Branch: 1 branch
BranchTrueFalseTotal
if abs $x - $xm <= $tol2 - "0.5" * ($b - $a) 5 44 49
303  
304 44 ▶ if (abs($e) > $tol1)
Branch: 1 branch
BranchTrueFalseTotal
if (abs $e > $tol1) 39 5 44
305 {
306 39 my $r = ($x-$w) * ($fx-$fv);
307 39 my $q = ($x-$v) * ($fx-$fw);
308 39 my $p = ($x-$v) * $q-($x-$w)*$r;
309  
310 39 ▶ $p = -$p if (($q = 2 * ($q - $r)) > 0.0);
Branch: 1 branch
BranchTrueFalseTotal
if ($q = 2 * ($q - $r)) > 0 17 22 39
311  
312 39 $q = abs($q);
313 39 my $etemp = $e;
314 39 $e = $d;
315  
316 39 ▶ unless ( (abs($p) >= abs(0.5 * $q * $etemp)) or
Branch: 1 branch
BranchTrueFalseTotal
unless (abs $p >= abs "0.5" * $q * $etemp or $p <= $q * ($a - $x) or $p >= $q * ($b - $x)) 34 5 39
Condition66%4/6
abs $p >= abs "0.5" * $q * $etemp or $p <= $q * ($a - $x) or $p >= $q * ($b - $x)
l!l&&r!l&&!r
5034
abs $p >= abs "0.5" * $q * $etemp or $p <= $q * ($a - $x)
l!l&&r!l&&!r
5034
MC/DC33%1/3
abs $p >= abs "0.5" * $q * $etemp or $p <= $q * ($a - $x) or $p >= $q * ($b - $x)
abs $p >= abs "0.5" * $q * $etemp $p <= $q * ($a - $x) $p >= $q * ($b - $x)
Truth table50%2/4
A or B or C
A= abs $p >= abs "0.5" * $q * $etempB= $p <= $q * ($a - $x)C= $p >= $q * ($b - $x)
ABCresult
1XX1
01X1
0011
0000
317 ($p <= $q * ($a - $x)) or ($p >= $q * ($b - $x)) )
318 {
319 #
320 # Parabolic step OK here - take it.
321 #
322 34 $d = $p/$q;
323 34 $u = $x + $d;
324  
325 34 ▶ if ( (($u - $a) < $tol2) or (($b - $u) < $tol2) )
Branch: 1 branch
BranchTrueFalseTotal
if ($u - $a < $tol2 or $b - $u < $tol2) 5 29 34
Condition100%3/3
$u - $a < $tol2 or $b - $u < $tol2
l!l&&r!l&&!r
4129
MC/DC100%2/2
$u - $a < $tol2 or $b - $u < $tol2
$u - $a < $tol2 $b - $u < $tol2
Truth table100%3/3
A or B
A= $u - $a < $tol2B= $b - $u < $tol2
ABresult
1X1
011
000
326 {
327 5 $d = copysign($tol1, $xm - $x);
328 }
329 34 goto dcomp; # Skip the golden section step.
330 }
331 }
332  
333 #
334 # Golden section step.
335 #
336 10 ▶ $e = (($x >= $xm) ? $a : $b) - $x;
Branch: 1 branch
BranchTrueFalseTotal
$x >= $xm ? : 8 2 10
337 10 $d = $CGOLD * $e;
338  
339 #
340 # We arrive here with d from Golden section or parabolic step.
341 #
342 44 ▶ dcomp:
Branch: 1 branch
BranchTrueFalseTotal
abs $d >= $tol1 ? : 39 5 44
343 $u = $x + ((abs($d) >= $tol1) ? $d : copysign($tol1, $d));
344 44 $fu = &$func($u); # 1 &$function evaluation per iteration
345  
346 #
347 # Decide what to do with &$function evaluation
348 #
349 44 ▶ if ($fu <= $fx)
Branch: 1 branch
BranchTrueFalseTotal
if ($fu <= $fx) { } 31 13 44
350 {
351 31 ▶ if ($u >= $x)
Branch: 1 branch
BranchTrueFalseTotal
if ($u >= $x) { } 14 17 31
352 {
353 14 $a = $x;
354 }
355 else
356 {
357 17 $b = $x;
358 }
359 31 $v = $w; $fv = $fw;
360 31 $w = $x; $fw = $fx;
361 31 $x = $u; $fx = $fu;
362 }
363 else
364 {
365 13 ▶ if ($u < $x)
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $x) { } 7 6 13
366 {
367 7 $a = $u;
368 }
369 else
370 {
371 6 $b = $u;
372 }
373  
374 13 ▶ if ($fu <= $fw or $w == $x)
Branch: 2 branches
BranchTrueFalseTotal
if ($fu <= $fw or $w == $x) { } 7 6 13
elsif ($fu <= $fv or $v == $x or $v == $w) { } 6 0 6
Condition100%3/3
$fu <= $fw or $w == $x
l!l&&r!l&&!r
526
MC/DC100%2/2
$fu <= $fw or $w == $x
$fu <= $fw $w == $x
Truth table100%3/3
A or B
A= $fu <= $fwB= $w == $x
ABresult
1X1
011
000
375 {
376 7 $v = $w; $fv = $fw;
377 7 $w = $u; $fw = $fu;
378 }
379 ▶ elsif ( $fu <= $fv or $v == $x or $v == $w )
Condition66%4/6
$fu <= $fv or $v == $x or $v == $w
l!l&&r!l&&!r
420
$fu <= $fv or $v == $x
l!l&&r!l&&!r
402
MC/DC0%0/3
$fu <= $fv or $v == $x or $v == $w
$fu <= $fv $v == $x $v == $w
Truth table50%2/4
A or B or C
A= $fu <= $fvB= $v == $xC= $v == $w
ABCresult
1XX1
01X1
0011
0000
380 {
381 6 $v = $u; $fv = $fu;
382 }
383 }
384  
385 44 $iter++;
386 }
387  
388 5 ▶ carp "Brent Exceed Maximum Iterations.\n" if ($iter >= $ITMAX);
Branch: 1 branch
BranchTrueFalseTotal
if $iter >= $ITMAX 0 5 5
389 5 return ($x, $fx);
390 }
391  
392 sub Brentzero
393 9 ▶{
Subroutine Brentzero: called CC=22 SCAR=32.0
Pod undocumented
394 9 my($a, $b, $func, $tol, $ITMAX) = @_;
395 9 my $fa = &$func($a);
396 9 my $fb = &$func($b);
397  
398 9 ▶ if (($fa > 0.0 and $fb > 0.0) or ($fa < 0.0 and $fb < 0.0))
Branch: 1 branch
BranchTrueFalseTotal
if ($fa > 0 and $fb > 0 or $fa < 0 and $fb < 0) 0 9 9
Condition55%5/9
$fa > 0 and $fb > 0 or $fa < 0 and $fb < 0
l!l&&r!l&&!r
009
$fa > 0 and $fb > 0
!ll&&!rl&&r
630
$fa < 0 and $fb < 0
!ll&&!rl&&r
360
MC/DC0%0/4
$fa > 0 and $fb > 0 or $fa < 0 and $fb < 0
$fa > 0 $fb > 0 $fa < 0 $fb < 0
Truth table28%2/7
A and B or C and D
A= $fa > 0B= $fb > 0C= $fa < 0D= $fb < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
399 {
400 0 carp "Brentzero(): root was not bracketed by [$a, $b].";
401 0 return undef;
402 }
403  
404 9 ▶ $ITMAX //= 100;
Condition50%1/2
$ITMAX //= 100
l!l
09
MC/DC0%0/1
$ITMAX //= 100
$ITMAX
Truth table50%1/2
A
A= $ITMAX
Aresult
11
00
405 9 ▶ $tol //= 1e-8;
Condition100%2/2
$tol //= "1e-08"
l!l
18
MC/DC100%1/1
$tol //= "1e-08"
$tol
Truth table100%2/2
A
A= $tol
Aresult
11
00
406  
407 9 my($c, $fc) = ($b, $fb);
408 9 my($d, $e);
409 9 my $iter = 0;
410  
411 9 ▶ while ($iter < $ITMAX)
Branch: 1 branch
BranchTrueFalseTotal
if ($iter < $ITMAX) 75 0 75
412 {
413 #
414 # Adjust bounding interval $d.
415 #
416 ### iteration: $iter
417 ### a: $a
418 ### b: $b
419 ### fa: $fa
420 ### fb: $fb
421 ### fc: $fc
422 #
423 75 ▶ if (($fb > 0.0 and $fc > 0.0) or ($fb < 0.0 and $fc < 0.0))
Branch: 1 branch
BranchTrueFalseTotal
if ($fb > 0 and $fc > 0 or $fb < 0 and $fc < 0) 50 25 75
Condition100%9/9
$fb > 0 and $fc > 0 or $fb < 0 and $fc < 0
l!l&&r!l&&!r
282225
$fb > 0 and $fc > 0
!ll&&!rl&&r
38928
$fb < 0 and $fc < 0
!ll&&!rl&&r
91622
MC/DC75%3/4
$fb > 0 and $fc > 0 or $fb < 0 and $fc < 0
$fb > 0 $fc > 0 $fb < 0 $fc < 0
Truth table57%4/7
A and B or C and D
A= $fb > 0B= $fc > 0C= $fb < 0D= $fc < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
424 {
425 50 $fc = $fa;
426 50 $c = $a;
427 50 $d = $b - $a;
428 50 $e = $d;
429 }
430  
431 75 ▶ if (abs($fc) < abs($fb))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $fc < abs $fb) 16 59 75
432 {
433 16 $a = $b;
434 16 $b = $c;
435 16 $c = $a;
436 16 $fa = $fb;
437 16 $fb = $fc;
438 16 $fc = $fa;
439 }
440  
441 #
442 # Convergence check.
443 #
444 ### a: $a
445 ### b: $b
446 ### c: $c
447 ### d: $d
448 ### fa: $fa
449 ### fb: $fb
450 ### fc: $fc
451 #
452 75 my $xm = ($c - $b) * 0.5;
453 75 my $tol1 = 2.0 * $ZEPS * abs($b) + ($tol * 0.5);
454  
455 #
456 ### tol1: $tol1
457 ### xm: $xm
458 #
459 75 ▶ return $b if (abs($xm) <= $tol1 or $fb == 0.0);
Branch: 1 branch
BranchTrueFalseTotal
if abs $xm <= $tol1 or $fb == 0 9 66 75
Condition66%2/3
abs $xm <= $tol1 or $fb == 0
l!l&&r!l&&!r
9066
MC/DC50%1/2
abs $xm <= $tol1 or $fb == 0
abs $xm <= $tol1 $fb == 0
Truth table66%2/3
A or B
A= abs $xm <= $tol1B= $fb == 0
ABresult
1X1
011
000
460  
461 66 ▶ if (abs($e) >= $tol1 and abs($fa) > abs($fb))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $e >= $tol1 and abs $fa > abs $fb) { } 57 9 66
Condition66%2/3
abs $e >= $tol1 and abs $fa > abs $fb
!ll&&!rl&&r
0957
MC/DC50%1/2
abs $e >= $tol1 and abs $fa > abs $fb
abs $e >= $tol1 abs $fa > abs $fb
Truth table66%2/3
A and B
A= abs $e >= $tol1B= abs $fa > abs $fb
ABresult
0X0
100
111
462 {
463 #
464 # Attempt inverse quadratic interpolation.
465 #
466 #### Branch (abs(e) >= tol1 and abs(fa) > abs(fb))
467 #
468 57 my($p, $q);
469 57 my $s = $fb/$fa;
470  
471 57 ▶ if ($a == $c)
Branch: 1 branch
BranchTrueFalseTotal
if ($a == $c) { } 41 16 57
472 {
473 #### Branch (a == c)
474 41 $p = 2.0 * $xm * $s;
475 41 $q = 1.0 - $s;
476 }
477 else
478 {
479 #### Branch (a != c)
480 16 my $r = $fb/$fc;
481 16 $q = $fa/$fc;
482 16 $p = $s * (2.0 * $xm * $q * ($q - $r) -
483 ($b - $a) * ($r - 1.0));
484 16 $q = ($q - 1.0) * ($r - 1.0) * ($s - 1.0);
485 }
486  
487 #
488 # Check if in bounds.
489 #
490 ### q: $q
491 ### p: $p
492 ### s: $s
493 ### e: $e
494 #
495 57 ▶ $q = - $q if ($p > 0.0);
Branch: 1 branch
BranchTrueFalseTotal
if $p > 0 31 26 57
496 57 $p = abs($p);
497 57 my $min1 = 3.0 * $xm * $q - abs($tol1 * $q);
498 57 my $min2 = abs($e * $q);
499  
500 57 ▶ if (2.0 * $p < min($min1, $min2))
Branch: 1 branch
BranchTrueFalseTotal
if (2.0 * $p < min($min1, $min2)) { } 57 0 57
501 {
502 #
503 # Interpolation worked, use it.
504 #
505 #### Branch (2.0 * p < min(min1, min2))
506 #
507 57 $e = $d;
508 57 $d = $p/$q;
509 }
510 else
511 {
512 #
513 # Interpolation failed, use bisection.
514 #
515 #### Branch (2.0 * p >= min(min1, min2))
516 #
517 0 $d = $xm;
518 0 $e = $d;
519 }
520 }
521 else
522 {
523 #
524 # Bounds decreasing too slowly for
525 # quadratic interpolation, use bisection.
526 #
527 9 $d = $xm;
528 9 $e = $d;
529 }
530  
531 #
532 # Move last best guess to $a.
533 #
534 66 $a = $b;
535 66 $fa = $fb;
536  
537 #
538 # Calculate the next guess.
539 #
540 66 ▶ $b += (abs($d) > $tol1)? $d: copysign($tol1, $xm);
Branch: 1 branch
BranchTrueFalseTotal
abs $d > $tol1 ? : 57 9 66
541 66 $fb = &$func($b);
542 66 $iter++;
543 }
544  
545 0 ▶ carp "Brentzero Exceed Maximum Iterations.\n" if ($iter >= $ITMAX);
Branch: 1 branch
BranchTrueFalseTotal
if $iter >= $ITMAX 0 0 0
546 0 return $a;
547 }
548  
549 4 1;
550 __END__
↑ Summary