|
ECMAScript/JavaScript Profiling
Using Firebug |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
in the toolbar
XMLHttpRequest:
/**
* See if two floating point values are approximately equal
*
* @param a {number} One value
* @param b {number} The other value
* @return true if a and b are approximately equal
*/
function equals(a, b)
{
"use strict";
if (Math.abs(a-b) < 0.000001){return true;}
return false;
}
/**
* Return the midpoint between two floating point values
*
* @param a {number} One value
* @param b {number} The other value
* @return The midpoint
*/
function midpoint(a, b)
{
"use strict";
return (a+b)/2.0;
}
/**
* Minimize a "well-behaved" function of one variable using the Golden
* Section algorithm. Note: If the solution is not be within the bounds
* this function will iterate forever.
*
* @param f The function
* @param L_0 The lower bound of the search space
* @param U_0 The upper bound of the search space
* @return The value of x that minimizes f (i.e., the argmin over [L_0, U_0])
*/
function argminGoldenSection(f, L_0, U_0)
{
"use strict";
var L, t_L, t_R, U;
// Initialize
L = L_0;
U = U_0;
t_L = L + 0.382*(U - L);
t_R = L + 0.618*(U - L);
// Iterate
while (!equals(L,U)){
if (f(t_L) < f(t_R)){
// L = L;
U = t_R;
t_R = t_L;
t_L = L + 0.382*(U - L);
}else{
L = t_L;
// U = U;
t_L = t_R;
t_R = L + 0.618*(U - L);
}
}
return midpoint(L,U);
}