Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update edge case behavior; a function with f(l)===0 or f(h)===0 will …
…yield an interval beginning with l or ending with h.
- Loading branch information
1 parent
60120e8
commit 7f7e1c9
Showing
3 changed files
with
1,717 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
/* Copyright 2016 Paul Brewer, Economic & Financial Technology COnsulting LLC <drpaulbrewer@eaftc.com> */ | ||
/* License: MIT https://opensource.org/licenses/MIT */ | ||
|
||
module.exports = function(l,h,tol,f){ | ||
'use strict'; | ||
// returns a two element array giving an inclusive range of zero values of f | ||
// requires strictly everywhere non-descending function f | ||
// use with caution: this code assumes but does and can not check that f is everywhere non-descending | ||
var right=h,left=l,intflag=(tol>=1); | ||
var fl = f(l), fh=f(h); | ||
if ((fl===0) && (fh===0)) return [l,h]; | ||
if (fh<fl) throw new Error("findZeroRange: requires non-descending function"); | ||
if (fl>=0) return []; | ||
if (fh<=0) return []; | ||
var zleft,zright,mid; | ||
zright=left; | ||
while ((right-zright)>tol){ | ||
mid = (right+zright)/2; | ||
if (intflag) mid = Math.floor(mid); | ||
if (f(mid)>0) | ||
right = mid; | ||
else | ||
zright = mid; | ||
module.exports = function (l, h, tol, f) { | ||
'use strict'; | ||
// returns a two element array giving an inclusive range of zero values of f | ||
// requires strictly everywhere non-descending function f | ||
// use with caution: this code assumes but does and can not check that f is everywhere non-descending | ||
var right = h, | ||
left = l, | ||
intflag = (tol >= 1); | ||
var fl = f(l), | ||
fh = f(h); | ||
if ((fl === 0) && (fh === 0)) return [l, h]; | ||
if (fh < fl) throw new Error("findZeroRange: requires non-descending function"); | ||
if (fl > 0) return []; | ||
if (fh < 0) return []; | ||
var zleft, zright, mid; | ||
zright = left; | ||
if (fh===0){ | ||
zright = right; | ||
} else { | ||
while ((right - zright) > tol) { | ||
mid = (right + zright) / 2; | ||
if (intflag) mid = Math.floor(mid); | ||
if (f(mid) > 0) | ||
right = mid; | ||
else | ||
zright = mid; | ||
} | ||
if (f(zright)<0) | ||
return [zright]; | ||
zleft=zright; | ||
while ((zleft-left)>tol){ | ||
mid = (left+zleft)/2; | ||
if (intflag) mid = Math.floor(mid); | ||
if (f(mid)<0) | ||
left = mid; | ||
else | ||
zleft = mid; | ||
if (f(zright) < 0) | ||
return [zright]; | ||
} | ||
if (fl===0){ | ||
zleft = l; | ||
} else { | ||
zleft = zright; | ||
while ((zleft - left) > tol) { | ||
mid = (left + zleft) / 2; | ||
if (intflag) mid = Math.floor(mid); | ||
if (f(mid) < 0) | ||
left = mid; | ||
else | ||
zleft = mid; | ||
} | ||
if (zleft===zright) return [zright]; | ||
return [zleft, zright]; | ||
} | ||
if (zleft === zright) return [zright]; | ||
return [zleft, zright]; | ||
}; | ||
|
||
|
Oops, something went wrong.