Issue link: http://dc.ee.ubm-us.com/i/71837

PROGRAMMER'S TOOLBOX e At first glance, fmin.f appears to be the worst kind of spaghetti code Fortran IV. However, take another look. It's really a straightforward translation of Brent's original code, which was written in Algol. (No wonder we don't find many copies of it!) I I swap two double-precision floats void swap(double &x1, double &x2){ double temp = x1; x1 = x2; x2 = temp; } II divide an interval according to the golden section double gold(double xO, double x2){ static const double g = 2-golden; return xO + g *(x2 - xO); } II shrink an interval by dividing one side of it void gold_shrink(double &xO, double &x1, double &x2, double &yO, double &y1, double &y2){ double x = gold(xO, x1); double y = f(x); if(y > y1 ){ xO = x; yO = y; } else{ x2 = x1; y2 = y1; x1 = x· , y1 = y; } swap(xO, x2); swap( yO, y2); } II Find the minimum of a function using golden section search double gold_search(double &xO, double &x2, double Eps){ double eps = Eps * (abs(x2-x0)); double x1 = gold(xO, x2); swap(xO, x2>; double yO = f(xQ); double y1 = f(x1); double y2 = f(x2>; while(abs(x2-x0) > eps) gold_shrink(xO, x1 , x2, yO, y1, y2); return (x0+x2)12.0; } single a rea of nume ri cal algorithms, Ala n 's your man . I've never see n so ma ny impleme ntatio ns (mos tly Fo rtra n and th e new la nguage, F- mo re on this next mo nth ) . Sadly, his Web site doesn ' t list an impleme nta- tio n of Bre nt's method . However, if you fo llow the trail thro ugh "No n- lin . programming," you ' ll arrive at plato. la.asu .edu/guide. html, a site ma in ta in ed by Hans D. Mittlemann and P. Spellucci of Ari zona State Unive rsity. Take an o th e r step through th eir "decision tree fo r o ptim izat io n softwa re," thro ugh pa ths fo r unco nstra in ed linear and nonlinear o ptimizati on, and lo and be h old , th e re's bin/nellibfiles. fJl ?f ilename=/ go/fmin.f, which is the ir impleme nta tio n of Bre nt's me thod . (Yo u can also find fmin . f by following a trail thro ugh the van Snyde r We b site.) At first glance, fmi n. f appears to be th e worst kind of spaghe tti code Fortran IV. Howeve r, take anoth e r look. It's really a straightforward tra nslation of Bre nt's origin al code, which was wt·itte n in Algol. (No wonder we do n 't find many copies of it!) The many GOTOs in th e Fo rtra n ve rsio n me re ly re present the limita tions of Fo rtra n IV, n o t Mittlemann and Spe lluci. Fo rtun ate ly, we a re not limited to Fo rtra n IV. I took a look at fmin . f myself, to see what it might have looked like had it auth o rs had access to th e structured constru cts of Fo rtran 77 o r 90. Not surprising- ly, since it was de rived from th e structured language Algol, the code unravels ni cely. Fo r th ose of you who like Fortra n, a copy of the mod- ifi ed code is shown in Listing 1. If I've do ne no thing e lse in thi s seri es, a t least I've give n th e world a version of Bre n t's algorithm th at o ne can actually read. Comparing th e Fortran code from fmin. f with the C code from Recipes, I was struck by two things. First, they are almost identical; even the same vari- able names are used in most places, 38 SEPTEMBER 2000 Embedded Systems Programming netlib. org/cgi-

- September001
- September002
- September003
- September004
- September005
- September006
- September007
- September008
- September009
- September010
- September011
- September012
- September013
- September014
- September015
- September016
- September017
- September018
- September019
- September020
- September021
- September022
- September023
- September024
- September025
- September026
- September027
- September028
- September029
- September030
- September031
- September032
- September033
- September034
- September035
- September036
- September037
- September038
- September039
- September040
- September041
- September042
- September043
- September044
- September045
- September046
- September047
- September048
- September049
- September050
- September051
- September052
- September053
- September054
- September055
- September056
- September057
- September058
- September059
- September060
- September061
- September062
- September063
- September064
- September065
- September066
- September067
- September068
- September069
- September070
- September071
- September072
- September073
- September074
- September075
- September076
- September077
- September078
- September079
- September080
- September081
- September082
- September083
- September084
- September085
- September086
- September087
- September088
- September089
- September090
- September091
- September092
- September093
- September094
- September095
- September096
- September097
- September098
- September099
- September100
- September101
- September102
- September103
- September104
- September105
- September106
- September107
- September108
- September109
- September110
- September111
- September112
- September113
- September114
- September115
- September116
- September117
- September118
- September119
- September120
- September121
- September122
- September123
- September124
- September125
- September126
- September127
- September128
- September129
- September130
- September131
- September132
- September133
- September134
- September135
- September136
- September137
- September138
- September139
- September140
- September141
- September142
- September143
- September144
- September145
- September146
- September147
- September148
- September149
- September150
- September151
- September152
- September153
- September154
- September155
- September156
- September157
- September158
- September159
- September160
- September161
- September162
- September163
- September164
- September165
- September166
- September167
- September168
- September169
- September171
- September172
- September173
- September174
- September175
- September176
- September177
- September178
- September179
- September180
- September181
- September182
- September183
- September184
- September185
- September186
- September187
- September188
- September189
- September190
- September191
- September192
- September193
- September194
- September195
- September196
- September197
- September198
- September199
- September200
- September201
- September202
- September203
- September204
- September205
- September206
- September207
- September208
- September209
- September210
- September211
- September212
- September213
- September214
- September215
- September216
- September217
- September218
- September219
- September220
- September221
- September222
- September223
- September224
- September225
- September226
- September227
- September228
- September229
- September230
- September231