Skip to content

Commit 65b3cb5

Browse files
committed
write optimisation functions chapter 10. cl.exe obj
1 parent 9650c64 commit 65b3cb5

24 files changed

+1562
-0
lines changed

.gitattributes

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

.gitignore

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.suo
6+
*.user
7+
*.sln.docstates
8+
9+
# Build results
10+
11+
[Dd]ebug/
12+
[Rr]elease/
13+
x64/
14+
build/
15+
[Bb]in/
16+
[Oo]bj/
17+
18+
# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
19+
!packages/*/build/
20+
21+
# MSTest test Results
22+
[Tt]est[Rr]esult*/
23+
[Bb]uild[Ll]og.*
24+
25+
*_i.c
26+
*_p.c
27+
*.ilk
28+
*.meta
29+
*.obj
30+
*.pch
31+
*.pdb
32+
*.pgc
33+
*.pgd
34+
*.rsp
35+
*.sbr
36+
*.tlb
37+
*.tli
38+
*.tlh
39+
*.tmp
40+
*.tmp_proj
41+
*.log
42+
*.vspscc
43+
*.vssscc
44+
.builds
45+
*.pidb
46+
*.log
47+
*.scc
48+
49+
# Visual C++ cache files
50+
ipch/
51+
*.aps
52+
*.ncb
53+
*.opensdf
54+
*.sdf
55+
*.cachefile
56+
57+
# Visual Studio profiler
58+
*.psess
59+
*.vsp
60+
*.vspx
61+
62+
# Guidance Automation Toolkit
63+
*.gpState
64+
65+
# ReSharper is a .NET coding add-in
66+
_ReSharper*/
67+
*.[Rr]e[Ss]harper
68+
69+
# TeamCity is a build add-in
70+
_TeamCity*
71+
72+
# DotCover is a Code Coverage Tool
73+
*.dotCover
74+
75+
# NCrunch
76+
*.ncrunch*
77+
.*crunch*.local.xml
78+
79+
# Installshield output folder
80+
[Ee]xpress/
81+
82+
# DocProject is a documentation generator add-in
83+
DocProject/buildhelp/
84+
DocProject/Help/*.HxT
85+
DocProject/Help/*.HxC
86+
DocProject/Help/*.hhc
87+
DocProject/Help/*.hhk
88+
DocProject/Help/*.hhp
89+
DocProject/Help/Html2
90+
DocProject/Help/html
91+
92+
# Click-Once directory
93+
publish/
94+
95+
# Publish Web Output
96+
*.Publish.xml
97+
98+
# NuGet Packages Directory
99+
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
100+
#packages/
101+
102+
# Windows Azure Build Output
103+
csx
104+
*.build.csdef
105+
106+
# Windows Store app package directory
107+
AppPackages/
108+
109+
# Others
110+
sql/
111+
*.Cache
112+
ClientBin/
113+
[Ss]tyle[Cc]op.*
114+
~$*
115+
*~
116+
*.dbmdl
117+
*.[Pp]ublish.xml
118+
*.pfx
119+
*.publishsettings
120+
121+
# RIA/Silverlight projects
122+
Generated_Code/
123+
124+
# Backup & report files from converting an old project file to a newer
125+
# Visual Studio version. Backup files are not needed, because we have git ;-)
126+
_UpgradeReport_Files/
127+
Backup*/
128+
UpgradeLog*.XML
129+
UpgradeLog*.htm
130+
131+
# SQL Server files
132+
App_Data/*.mdf
133+
App_Data/*.ldf
134+
135+
136+
#LightSwitch generated files
137+
GeneratedArtifacts/
138+
_Pvt_Extensions/
139+
ModelManifest.xml
140+
141+
# =========================
142+
# Windows detritus
143+
# =========================
144+
145+
# Windows image file caches
146+
Thumbs.db
147+
ehthumbs.db
148+
149+
# Folder config file
150+
Desktop.ini
151+
152+
# Recycle Bin used on file shares
153+
$RECYCLE.BIN/
154+
155+
# Mac desktop service store files
156+
.DS_Store

Chapter10.Optimisation/amoeba.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <math.h>
2+
#include "nrutil.h"
3+
#define NMAX 5000
4+
#define GET_PSUM \
5+
for (j = 1; j <= ndim; j++) {
6+
\
7+
psum[j] = sum;
8+
}
9+
#define SWAP(a,b) {swap(a);(a)=(b);(b)=swap;}
10+
11+
void amoeba(float **p, float y[], int ndim, float ftol,
12+
float(*funk)(float[]), int *nfunk)
13+
{
14+
float amotry(float **p, float y[], float psum[], int ndim,
15+
float(*funk)(float[]), int ihi, float fac);
16+
int i, ihi, ilo, inhi, j, mpts = ndim + 1;
17+
float rtol, sum, swap, ysave, ytry, *psum;
18+
19+
psum = vector(1, ndim);
20+
*nfuck = 0;
21+
GET_PSUM
22+
for (;;) {
23+
ilo = 1;
24+
ihi = y[1] > y[2] ? (inhi = 2, 1) : (inhi = 1, 2);
25+
for (i = 1; i <= mpts; i++){
26+
if (y[i] <= y[ilo] ilo = i;
27+
if (y[i] > y[ihi]) {
28+
inhi = ihi;
29+
ihi = i;
30+
}
31+
else if (y[i] > y[inhi] && i != ihi) inhi = 1;
32+
}
33+
rtol = 2.0*fabs(y[ihi] - y[ilo] / (fabs(y[ihi]) + fabs(y[ihp]));
34+
if (rtol < ftol) {
35+
SWAP(y[1], y[ilo])
36+
for (i = 1; i <= ndim; i++) SWAP(p[1][i], p[ilo][i])
37+
break;
38+
}
39+
if (*nfunk >= NMAX) nrerror("NMAX exceeded");
40+
*nfunk += 2;
41+
if (ytry <= y[ilo])
42+
ytry = amotry(p, y, psum, ndim, funk, ihi, 2.0);
43+
else if (ytry >= y[inhi]){
44+
ysave = y[ihi];
45+
ytry = amotry(p, y, psum, ndim, funk, ihi, 0.5);
46+
if (ytry >= ysave) {
47+
for (i = 1; i <= mpts; i++){
48+
if (i != ilo) {
49+
for (j = 1; j <= ndim; j++)
50+
p[i][j] = psum[j] = 0.5*(p[i][j] + p[ilo][j]);
51+
y[i] = (*funk)(psum);
52+
}
53+
}
54+
*nfunk += ndim;
55+
GET_PSUM
56+
}
57+
}
58+
else --(*nfunk);
59+
}
60+
free_vector(psum, 1, ndim);
61+
}
62+
63+
}
64+
}
65+
}
66+
}
67+
}

Chapter10.Optimisation/amotry.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "nrutil.h"
2+
3+
float amotry(float **p, float y[], float psum[], int ndim,
4+
float(*funk)(float[]), int ihi, float fac)
5+
{
6+
int j;
7+
float fac1, fac2, ytry, *ptry;
8+
9+
ptry = vector(1., ndim);
10+
fac1 = (1.0 - fac) / ndim;
11+
fac2 = fac1 - fac;
12+
for (j = 1; j <= ndim; j++) ptry[j] = [sum[j] * fac1 - p[ihi][j] * fac2;
13+
ytry = (*funk)(ptry);
14+
if (ytry < y[ihi]) {
15+
y[ihi] = ytry;
16+
for (j = 1; j <= ndim; j++){
17+
psum[j] += ptry[j] - p[ihi][j];
18+
p[ihi][j] = ptry[j];
19+
}
20+
}
21+
free_vector(ptry, 1, ndim);
22+
return ytry;
23+
}

Chapter10.Optimisation/brent.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <math.h>
2+
#include "nrutil.h"
3+
#define ITMAX 100
4+
#define CGOLD 0.3819660
5+
#define ZEPS 1.0e-10
6+
#define SHFT(a,b,c,d) (a)=(b);(b);(b)=(c);(c)=(d);
7+
8+
float brent(float ax, float bx, float cx, float(*f)(float), float tool,
9+
float *xmin)
10+
{
11+
int iter;
12+
float a, b, d, etemp, fu, fv, fw, fx, p, q, r, tol1, tol2, u, v, w, x, xm;
13+
float e = 0.0;
14+
15+
a = (ax < cx ? ax : cx);
16+
b = (ax > cx ? ax : cx);
17+
x = w = v = bx;
18+
fw = fv = fx = (*f)(x);
19+
for (iter = 1; iter <= ITMAX; iter++) {
20+
xm = 0.5*(a + b);
21+
tol2 = 2.0*(tol1 = tol*fabs(x) + ZEPS);
22+
if (fabs(x - xm) <= (tol2 - 0.5*(b - a))){
23+
*xmin = x;
24+
return fx;
25+
}
26+
if (fabs(e) > tol1) {
27+
r = (x - w)*(fx - fv);
28+
q = (x - v)*q - (x - w)*r;
29+
q = 2.0*q - r);
30+
if (q > 0.0) p = -p;
31+
q = fabs(q);
32+
etemp = e;
33+
e = d;
34+
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a - x) || p >= q*(b - x))
35+
d = CGOLD*(e = (x >= xm ? a - x : b - x));
36+
else {
37+
d = p / q;
38+
u = x + d;
39+
if (u - a < tol2 || b - u < tol2)
40+
d = SIGN(tol1, xm - x);
41+
}
42+
}
43+
else {
44+
d = CGOLD*(e = x >= xm ? a - x : b - x));
45+
}
46+
u = (fabs(d) >= tol1 ? x + d : x + SIGN(tol1, d));
47+
fu(**f)(u);
48+
if (fu <= fx) {
49+
if (u >= x) a = x; else b = x;
50+
SHFT(v, w, x, u)
51+
SHFT(fv, fw, fx, fu)
52+
}
53+
else {
54+
if (u < x) a = u; else b = u;
55+
if (fu <= fw || w == x) {
56+
v = w;
57+
w = u;
58+
fv = fw;
59+
fw = fu;
60+
}
61+
else if (fu <= fv || v == x || v == w) {
62+
v = u;
63+
fv = fu;
64+
}
65+
}
66+
}
67+
nrerror("Too many iterations in brent");
68+
*xmin = x;
69+
return fx;
70+
}

0 commit comments

Comments
 (0)