-
Notifications
You must be signed in to change notification settings - Fork 1
/
Func.js
430 lines (352 loc) · 13.8 KB
/
Func.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//-----Clairvoyant.js user defined function class --------------------------------------
function Func(func, parameters) {
'use strict';
this.func = func;
this.params = [];
if (typeof parameters !== 'undefined') {
if (parameters instanceof Array) {
this.params = parameters;
} else {
this.params[0] = parameters;
}
} else {
this.params[0] = 0; // dummy for no - parameter functions;
}
//-----methods for user defined function class--------------------------
this.evaluate = function (inputs) {
return this.func(inputs, this.params);
};
// load the current parameters of the Func into an Array
this.getParameters = function (pbr) {
var getP;
for (getP = 0; getP < this.params.length; getP++) {
pbr[getP] = this.params[getP];
}
return 0;
};
// set the parameters of a Func to some new values
this.setParameters = function (newParam) {
var newP;
for (newP = 0; newP < newParam.length; newP++) {
this.params[newP] = newParam[newP];
}
return 0;
};
// function to find this function's extremum on < min > .. < max > by finding the derivative
// zeroes. Tolerance defaults to 1 / 10^6 unless user specifies < tol > . Returns an array,
// first element = coordinate of extrema, second element is 0 for minima and 1 for maxima.
this.getExtremum = function (min, max, tol) {
var concavity, copyFunc, ddx, extrema, ddxFunc, results, tolerance;
tolerance = typeof tol !== 'undefined' ? tol : 0.000001;
results = [];
//ddxFunc can see copyFunc, allowing us to pass the 'this' down a level
copyFunc = this;
ddxFunc = function (x, par) {
return copyFunc.derivative(x, 0, 0.000001,0);
}
ddx = new Func(ddxFunc);
extrema = ddx.brentSoln(min, max, tolerance);
concavity = ddx.derivative(extrema);
results[0] = Math.round(extrema / tolerance) * tolerance;
if (concavity > 0) {
results[1] = 0;
}
if (concavity < 0) {
results[1] = 1;
}
return results;
};
// return a random pull from a 1D function, between < min > and < max > .
// Function must be non - negative and pole - free across the requested
// range for this to make sense.
this.randPull = function (min, max) {
var decision, done, extreme, thresh, x;
done = 0;
// find the highest point of the function in range: grid search to find global maximum,
// then getExtremum to zero in on it.
extreme = [];
extreme = this.getExtremum(min, max);
x = 0;
thresh = 0;
decision = 0;
while (done === 0) {
// choose a point in range
x = min + (max - min) * Math.random();
// find normalized height of the function at x
thresh = this.evaluate(x) / extreme[0];
// decide if we should keep the pull
decision = Math.random();
if (decision < thresh) {
return x;
}
}
};
// implementation of Brent's Algo for finding the zero of a 1D function
// between < hi > and < lo > . Letters label the steps in the wikipedia
// factoring of the algorithm; step (a) is the function call itself.
this.brentSoln = function (lo, hi, tol) {
var a, answer, b, buffer, c, d, f_a, f_b, f_c, f_s, initHi, initLo, loops, mflag, s, tolerance;
tolerance = typeof tol !== 'undefined' ? tol : 0.000001;
// (b)
initHi = this.evaluate(hi);
// (c)
initLo = this.evaluate(lo);
// (d)
try {
if ((initHi * initLo >= 0)) {
throw ('Range provided does not bracket a unique zero, attempting to recover...');
}
} catch (err) {
return this.biSoln(lo, hi);
}
// (e)
a = lo;
b = hi;
if (Math.abs(initLo) < Math.abs(initHi)) {
a = hi;
b = lo;
}
// (f)
c = a;
// (g)
mflag = 1;
// (h)
f_a = this.evaluate(a);
f_b = this.evaluate(b);
f_c = this.evaluate(c);
s = b - f_b * (b - a) / (f_b - f_a);
f_s = this.evaluate(s);
d = 0;
buffer = 0;
loops = 0;
while (f_b !== 0 && f_s !== 0 && Math.abs(b - a) > tolerance) {
loops++;
f_a = this.evaluate(a);
f_b = this.evaluate(b);
f_c = this.evaluate(c);
// (h_i)
if (f_a !== f_c && f_b !== f_c) {
// (h_i_1)
s = a * f_b * f_c / (f_a - f_b) / (f_a - f_c) + b * f_a * f_c / (f_b - f_a) / (f_b - f_c) + c * f_a * f_b / (f_c - f_a) / (f_c - f_b);
} else { //(h_ii)
// (h_ii_1)
s = b - f_b * (b - a) / (f_b - f_a);
}
// (h_iii)
// (h_iv)
if (((s > b && s > (3 * a + b) / 4) || (s < b && s < (3 * a + b) / 4)) || (mflag === 1 && Math.abs(s - b) >= Math.abs(b - c) / 2) || (mflag === 0 && Math.abs(s - b) >= Math.abs(c - d) / 2) || (mflag === 1 && Math.abs(b - c) < tolerance) || (mflag === 0 && Math.abs(c - d) < tolerance)) {
// (h_iv_1)
s = (a + b) / 2;
// (h_iv_2)
mflag = 1;
} else { //(h_v)
// (h_v_1)
mflag = 0;
}
// (h_vi)
// (h_vii)
f_s = this.evaluate(s);
// (h_viii)
d = c;
// (h_ix)
c = b;
// (h_x)
if (f_a * f_s < 0) {
b = s;
} else {
a = s;
}
// (h_xi)
if (Math.abs(this.evaluate(a)) < Math.abs(this.evaluate(b))) {
buffer = a;
a = b;
b = buffer;
}
}
// (i)
if (this.evaluate(b) === 0) {
answer = Math.round(b / tolerance) * tolerance;
} else {
answer = Math.round(s / tolerance) * tolerance;
}
return answer;
};
// Simple grid search + bisection method for finding a function zero in the range < min > .. < max > to tolerance < tol > .
// This is SLOW, and should only be called to help brentSoln recover when the user fails to bracket a unique zero.
this.biSoln = function (min, max, tol) {
var a, b, c, f_a, f_b, f_c, gridMin, gridS, gridSteps, here, high, low, lowestPoint, stepSize, tolerance;
// grid search to find some zero, very slow
gridSteps = 1000;
stepSize = (max - min) / gridSteps;
here = min;
gridMin = Math.abs(this.evaluate(min));
lowestPoint = here;
for (gridS = 0; gridS < gridSteps; gridS++) {
if (Math.abs(this.evaluate(here)) < gridMin) {
lowestPoint = here;
gridMin = Math.abs(this.evaluate(here));
}
here += stepSize;
}
low = lowestPoint - stepSize;
high = lowestPoint + stepSize;
tolerance = typeof tol !== 'undefined' ? tol : 0.000001;
a = low;
b = high;
f_a = Math.abs(this.evaluate(a));
f_b = Math.abs(this.evaluate(b));
c = (a + b) / 2;
f_c = Math.abs(this.evaluate(c));
while (Math.abs(a - b) > tolerance) {
if (f_a > f_b && f_a > f_c) {
a = c;
f_a = Math.abs(this.evaluate(a));
} else if (f_b > f_a && f_b > f_c) {
b = c;
f_b = Math.abs(this.evaluate(b));
}
c = (a + b) / 2;
f_c = Math.abs(this.evaluate(c));
}
return Math.round(c / tolerance) * tolerance;
};
// Richardson's extrapolation for derivative computation in 1D, evaluated at < x > in
// dimension < dim > (default 0) to tolerance < tol > (default 1 / 10^6). < roundoff > flag
// chooses whether or not to round the result to tolerance (default yes = 1); needs to
// be 0 for maxima finding so tolerances don't compound.
this.derivative = function (x, dim, tol, roundoff) {
var D, dimension, doRound, dtol, dtol2, tolerance, vary, Xhi, Xhi2, Xlo, Xlo2;
dimension = typeof dim !== 'undefined' ? dim : 0;
tolerance = typeof tol !== 'undefined' ? tol : 0.000001;
doRound = typeof roundoff !== 'undefined' ? roundoff : 1;
if (!(x instanceof Array)) {
dtol = (this.evaluate(x + tolerance) - this.evaluate(x - tolerance)) / (2 * tolerance);
dtol2 = (this.evaluate(x + tolerance / 2) - this.evaluate(x - tolerance / 2)) / tolerance;
} else {
Xhi = [];
Xlo = [];
Xhi2 = [];
Xlo2 = [];
for (vary = 0; vary < x.length; vary++) {
Xhi[vary] = x[vary];
Xlo[vary] = x[vary];
Xhi2[vary] = x[vary];
Xlo2[vary] = x[vary];
}
Xhi[dimension] += tolerance;
Xlo[dimension] -= tolerance;
Xhi2[dimension] += tolerance / 2;
Xlo2[dimension] -= tolerance / 2;
dtol = (this.evaluate(Xhi) - this.evaluate(Xlo)) / (2 * tolerance);
dtol2 = (this.evaluate(Xhi2) - this.evaluate(Xlo2)) / tolerance;
}
D = (4 * dtol2 - dtol) / 3;
if (doRound === 0) {
return D;
}
if (doRound === 1) {
return Math.round(D / tolerance) * tolerance;
}
};
//gradient of the function evaluated at <x>
this.gradient = function (x) {
var dim, dimension, grad;
dimension = x.length;
grad = [];
for (dim = 0; dim < dimension; dim++) {
grad[dim] = this.derivative(x, dim); // * direction[dim]
}
return grad;
};
//draw this function in the window from <xmin> to <xmax> and <ymin> to <ymax>
this.draw = function (canvas, xmin, xmax, ymin, ymax, title, xtitle, ytitle, plotstyle) {
var canvX, canvY, color, i, inWindow, lineWidth, nSamples, plot, xStep, x, y, yMin, yMax;
//auto-find a y window if ymin === ymax:
yMin = ymin;
yMax = ymax;
x = xmin;
xStep = (xmax - xmin) / 100;
if (ymin === ymax) {
for (i = 0; i < 100; i++) {
y = this.evaluate(x);
if (y > yMax) {
yMax = y;
}
if (y < yMin) {
yMin = y;
}
x += xStep;
}
yMax += (yMax - yMin) * 0.1;
yMin -= (yMax - yMin) * 0.1;
}
//make a new Plot, fetch style info
if (typeof plotstyle !== 'undefined') {
plot = new Plot(canvas, xmin, xmax, yMin, yMax, title, xtitle, ytitle, plotstyle);
color = plotstyle.color;
lineWidth = plotstyle.lineWidth;
} else {
plot = new Plot(canvas, xmin, xmax, yMin, yMax, title, xtitle, ytitle);
color = 'black';
lineWidth = 2;
}
//allow axis suppression for overlaying multiple drawings.
if ((typeof plotstyle !== 'undefined' && !plotstyle.suppress) || typeof plotstyle === 'undefined') {
plot.draw();
}
//draw function as nSamples line segements joining f(x) at each step of x in range.
//nSamples = #pixels in width of canvas ensures smooth-looking line (ie each line segment is at most 1px long).
nSamples = plot.canvas.width;
xStep = (xmax - xmin) / nSamples;
x = xmin;
inWindow = 0;
plot.context.beginPath();
plot.context.strokeStyle = color;
plot.context.lineWidth = lineWidth;
for (i = 0; i < nSamples + 1; i++) {
y = this.evaluate(x);
canvX = plot.marginScaleY * plot.marginSize + (x - xmin) / (xmax - xmin) * (plot.canvas.width - (1 + plot.marginScaleY) * plot.marginSize);
canvY = plot.canvas.height - (plot.marginSize + (y - yMin) / (yMax - yMin) * (plot.canvas.height - 2 * plot.marginSize));
if (y > yMin && y < yMax) {
if (inWindow === 0) {
plot.context.moveTo(canvX, canvY);
plot.context.beginPath();
inWindow = 1;
} else {
plot.context.lineTo(canvX, canvY);
}
} else {
inWindow = 0;
}
plot.context.stroke();
x += xStep;
}
return 0;
};
this.minimize = function (start, tol) {
var grad, here, location, previous, step, tolerance;
step = 1;
tolerance = typeof tol !== 'undefined' ? tol : 0.000001;
//Func values at the current and previous points:
here = this.evaluate(start);
previous = here + 2 * tolerance;
//Vector pointing at the current point:
location = new Vector(start);
//Vector gradient of the function at the current point:
grad = new Vector(this.gradient(start));
while ((Math.abs(here - previous) > tolerance)) {
previous = this.evaluate(location.elements);
grad = new Vector(this.gradient(location.elements));
if (grad.getLength() === 0) {
return location.elements;
}
grad = grad.scale(-1 * step / grad.getLength());
location = location.add(grad);
here = this.evaluate(location.elements);
if (here >= previous) {
step = step / 10;
}
}
return location.elements;
};
}