blib/lib/Math/Brent.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 18 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 224 CC 23 SCAR 51.0
CC
23
Cov
0%
Brent
272.0
BracketMinimum
72.0
Minimise1D
2.0
CRAP 164.7
1 # $Id: Brent.pm,v 1.1 1995/12/26 10:06:36 willijar Exp $
2 =head1 NAME
3  
4 Math::Brent - Single Dimensional Function Minimisation
5  
6 =head1 SYNOPSIS
7  
8 use Math::Brent qw(FindMinima BracketMinimum Brent Minimise1D);
9 my ($x,$y)=Minimise1D($guess,$scale,\&func,$tol,$itmax);
10 my ($ax,$bx,$cx,$fa,$fb,$fc)=BracketMinimum($ax,$bx,$cx,\&func);
11 my ($x,$y)=Brent($ax,$bx,$cx,\&func,$tol,$itmax);
12  
13 =head1 DESCRIPTION
14  
15 This is an implementation of Brents method for One-Dimensional
16 minimisation of a function without using derivatives. This algorithm
17 cleverly uses both the Golden Section Search and parabolic
18 interpolation.
19  
20 The main function B<Brent>, given a function reference B<\&func> and a
21 bracketing triplet of abcissas B<$ax>, B<$bx>, B<$cx> (such that
22 B<$bx> is between B<$ax> and B<$cx> and B<func($bx)> is less than both
23 B<func($ax)> and B<func($cx)>), isolates the minimum to a fractional
24 precision of about B<$tol> using Brents method. A maximum number of
25 iterations B<$itmax> may be specified for this search - it defaults to
26 100. Returned is an array consisting of the abcissa of the minum and
27 the function value there.
28  
29 The function B<BracketMinimum>, given a function B<\&func> and
30 distinct initial points B<$ax> and B<$bx> searches in the downhill
31 direction (defined by the function as evaluated at the initial points)
32 and returns an array of the three points B<$ax>, B<$bx>, B<$cx> which
33 bracket the minimum of the function and the function values at those
34 points.
35  
36 The function B<Minimise1D> provides a simple interface to the above
37 two routines. Given a function B<\&func>, an initial guess for its
38 minimum, and its scaling (B<$guess>,B<$scale>) this routine isolates
39 the minimum to a fractional precision of about B<$tol> using Brents
40 method. A maximum number of iterations B<$itmax> may be specified for
41 this search - it defaults to 100. It returns an array consisting of
42 the abcissa of the minum and the function value there.
43  
44 =head1 EXAMPLE
45  
46 use Math::Brent qw(Minimise1D);
47 sub func {
48 my $x=shift ;
49 return $x ? sin($x)/$x: 1;
50 }
51 my ($x,$y)=Minimise1D(1,1,\&func,1e-7);
52 print "Minimum is func($x)=$y\n";
53  
54 produces the output
55  
56 Minimum is func(5.236068)=-.165388470697432
57
58 =head1 HISTORY
59  
60 $Log: Brent.pm,v $
61 Revision 1.1 1995/12/26 10:06:36 willijar
62 Initial revision
63  
64  
65 =head1 BUGS
66  
67 Let me know of any problems.
68  
69 =head1 AUTHOR
70  
71 John A.R. Williams <J.A.R.Williams@aston.ac.uk>
72  
73 =head1 SEE ALSO
74  
75 "Numerical Recipies: The Art of Scientific Computing"
76 W.H. Press, B.P. Flannery, S.A. Teukolsky, W.T. Vetterling.
77 Cambridge University Press. ISBN 0 521 30811 9.
78  
79 =cut
80  
81 require Exporter;
82 package Math::Brent;
83 @ISA=qw(Exporter);
84 @EXPORT_OK=qw(FindMinima BracketMinimum Brent Minimise1D);
85 use Math::VecStat qw(max min);
86 use Math::Fortran qw(sign);
87 use strict;
88 use Carp;
89  
90 sub Minimise1D {
91 my ($guess,$scale,$func,$tol,$itmax)=@_;
92 my ($a,$b,$c)=BracketMinimum($guess-$scale,$guess+$scale,$func);
93 return Brent($a,$b,$c,$func,$tol,$itmax);
94 }
95  
96 #----------------------------------------------------------------------
97 # BracketMinimum
98 # BracketMinimum is MNBRAK minimum bracketing routine from section 10.1
99 # of numerical recipies
100 # Given a function func, and distinct initial points ax & bx this
101 # routine searches in the downhill direction and returns new points ax,
102 # bx, cx which bracket the minimum. The function values at the 3 points
103 # are returned in fa, fb, fc respectively.
104 my $GOLD=1.618034; # default magnification ratio for intervals
105 my $GLIMIT=100.0; # Max magnification for parabolic fit step
106 my $TINY=1E-20;
107 sub BracketMinimum {
108 my ($ax,$bx,$func)=@_;
109 my ($fa,$fb)=(&$func($ax),&$func($bx));
110 if ($fb>$fa) { my $t=$ax; $ax=$bx; $bx=$t; $t=$fa; $fa=$fb; $fb=$t; }
111 my $cx=$bx+$GOLD*($bx-$ax);
112 my $fc=&$func($cx);
113 while($fb >= $fc) { # Loop here until we bracket
114 my $r=($bx-$ax)*($fb-$fc); # Compute U by parabolic extrapolation from
115 my $q=($bx-$cx)*($fb-$fa); # a,b,c. TINY used to prevent div by zero
116 my $u=$bx-(($bx-$cx)*$q-($bx-$ax)*$r)/
117 (2.0*sign(max(abs($q-$r),$TINY),$q-$r));
118 my $ulim=$bx+$GLIMIT*($cx-$bx); # We won't go further than this
119 my $fu;
120 if (($bx-$u)*($u-$cx)>0.0) { # parabolic U between B & C - try it
121 $fu=&$func($u);
122 if ($fu < $fc) { # Minimum between B & C
123 $ax=$bx; $fa=$fb; $bx=$u; $fb=$fu; next;
124 }
125 elsif ($fu > $fb) { # Minimum between A & U
126 $cx=$u; $fc=$fu; next;
127 }
128 $u=$cx+$GOLD*($cx-$bx);
129 $fu=&$func($u);
130 }
131 elsif (($cx-$u)*($u-$ulim)>0) { #p arabolic fit between C and limit
132 $fu=&$func($u);
133 if ($fu < $fc) {
134 $bx=$cx; $cx=$u;
135 $u=$cx+$GOLD*($cx-$bx);
136 $fb=$fc; $fc=$fu;
137 $fu=&$func($u);
138 }
139 }
140 elsif (($u-$ulim)*($ulim-$cx)>=0) { # Limit parabolic U to maximum
141 $u=$ulim; $fu=&$func($u);
142 }
143 else { # eject parabolic U, use default magnification
144 $u=$cx+$GOLD*($cx-$bx);
145 $fu=&$func($u);
146 }
147 # Eliminate oldest point & continue
148 $ax=$bx; $bx=$cx; $cx=$u;
149 $fa=$fb; $fb=$fc; $fc=$fu;
150 }
151 return ($ax,$bx,$cx,$fa,$fb,$fc);
152 }
153  
154 my $CGOLD=0.3819660;
155 my $ZEPS=1e-10;
156 sub Brent {
157 my ($ax,$bx,$cx,$func,$tol,$ITMAX)=@_;
158 if (!defined $ITMAX) { $ITMAX=100; }
159 if (!defined $tol) { $tol=1e-8; }
160 my $a=min($ax,$cx);
161 my $b=max($ax,$cx);
162 my ($d,$u,$x,$w,$v,$fu,$fx,$fw,$fv); # ordinates & function evaluations
163 $x=$w=$v=$bx;
164 $fx=$fw=$fv=&$func($x);
165 my $e=0.0; # will be distance moved on the step before last
166 my ($xm,$tol1,$tol2,$iter);
167 for($iter=0; $iter<$ITMAX; $iter++) {
168 $xm=0.5*($a+$b);
169 $tol1=$tol*abs($x)+$ZEPS;
170 $tol2=2.0*$tol1;
171 if (abs($x-$xm) <= ($tol2-0.5*($b-$a))) { last; }
172 if (abs($e)>$tol1) {
173 my $r=($x-$w)*($fx-$fv);
174 my $q=($x-$v)*($fx-$fw);
175 my $p=($x-$v)*$q-($x-$w)*$r;
176 if (($q=2*($q-$r)) > 0.0) { $p=-$p; }
177 $q=abs($q);
178 my $etemp=$e;
179 $e=$d;
180 if ( (abs($p)>=abs(0.5*$q*$etemp)) ||
181 ($p<=$q*($a-$x)) || ($p>=$q*($b-$x)) ) {
182 goto gsec;
183 }
184 # parabolic step OK here - take it
185 $d=$p/$q; $u=$x+$d;
186 if ( (($u-$a)<$tol2) || (($b-$u)<$tol2) ) {
187 $d=sign($tol1,$xm-$x);
188 }
189 goto dcomp;
190 }
191 gsec: # We arrive here for a Golden section step
192 $e = (($x>=$xm) ? $a : $b)-$x;
193 $d=$CGOLD*$e; # Golden section step
194 dcomp: # We arrive here with d from Golden section or parabolic step
195 $u=$x+( (abs($d)>=$tol1) ? $d : sign($tol1,$d));
196 $fu=&$func($u); # 1 &$function evaluation per iteration
197 if ($fu<=$fx) { # Decide what to do with &$function evaluation
198 if ($u>=$x) { $a=$x; } else { $b=$x; }
199 $v=$w; $fv=$fw;
200 $w=$x; $fw=$fx;
201 $x=$u; $fx=$fu;
202 }
203 else {
204 if ($u<$x) { $a=$u; } else { $b=$u; }
205 if ($fu<=$fw || $w==$x) {
206 $v=$w; $fv=$fw;
207 $w=$u; $fw=$fu;
208 }
209 elsif ( $fu<=$fv || $v==$x || $v==$w ) { $v=$u; $fv=$fu; }
210 }
211 }
212 if ($iter>=$ITMAX) { carp "Brent Exceed Maximum Iterations.\n"; }
213 return ($x,$fx);
214 }
215  
216  
217  
218 1;