Skip to content

Commit

Permalink
Randomized initial zero search
Browse files Browse the repository at this point in the history
  • Loading branch information
tinybike committed Feb 12, 2016
1 parent 8c5a345 commit 9965f53
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 50 deletions.
50 changes: 30 additions & 20 deletions dist/fzero.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,38 @@ module.exports = function (f, bounds, options) {
console.log("Search for an interval around", a.toString(), "containing a sign change:");
console.log("count\ta\t\tf(a)\t\t\t\tb\t\tf(b)");
}
var bracketed, blist;
var aa = (a.eq(new Decimal(0))) ? new Decimal(1) : a;
var blist = [
aa.times(new Decimal("0.9")),
aa.times(new Decimal("1.1")),
aa.minus(new Decimal(1)),
aa.plus(new Decimal(1)),
aa.times(new Decimal("0.5")),
aa.times(new Decimal("1.5")),
aa.neg(),
aa.times(new Decimal(2)),
aa.times(new Decimal(10)).neg(),
aa.times(new Decimal(10))
];
for (var j = 0, len = blist.length; j < len; ++j) {
b = blist[j];
fb = toDecimal(f(b.toString()));
if (verbose) {
console.log(nfev + "\t\t" + a + "\t\t" + fa + "\t\t" + b + "\t\t" + fb);
var tries = 0;
do {
blist = [
aa.times(new Decimal("0.9")),
aa.times(new Decimal("1.1")),
aa.minus(new Decimal(1)),
aa.plus(new Decimal(1)),
aa.times(new Decimal("0.5")),
aa.times(new Decimal("1.5")),
aa.neg(),
aa.times(new Decimal(2)),
aa.times(new Decimal(10)).neg(),
aa.times(new Decimal(10))
];
for (var j = 0, len = blist.length; j < len; ++j) {
b = blist[j];
fb = toDecimal(f(b.toString()));
if (verbose) {
console.log(nfev + "\t\t" + aa + "\t\t" + fa + "\t\t" + b + "\t\t" + fb);
}
nfev += 1;
if (fa.s * fb.s <= 0) {
a = aa;
bracketed = true;
break;
}
}
nfev += 1;
if (fa.s * fb.s <= 0) break;
}
aa = aa.times(new Decimal(Math.random().toString())).plus(new Decimal(tries*(Math.random() - 0.5).toString()));
fa = toDecimal(f(aa.toString()));
} while (!bracketed && ++tries < maxiter);
}

var u, fu;
Expand Down
4 changes: 2 additions & 2 deletions dist/fzero.min.js

Large diffs are not rendered by default.

50 changes: 30 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,38 @@ module.exports = function (f, bounds, options) {
console.log("Search for an interval around", a.toString(), "containing a sign change:");
console.log("count\ta\t\tf(a)\t\t\t\tb\t\tf(b)");
}
var bracketed, blist;
var aa = (a.eq(new Decimal(0))) ? new Decimal(1) : a;
var blist = [
aa.times(new Decimal("0.9")),
aa.times(new Decimal("1.1")),
aa.minus(new Decimal(1)),
aa.plus(new Decimal(1)),
aa.times(new Decimal("0.5")),
aa.times(new Decimal("1.5")),
aa.neg(),
aa.times(new Decimal(2)),
aa.times(new Decimal(10)).neg(),
aa.times(new Decimal(10))
];
for (var j = 0, len = blist.length; j < len; ++j) {
b = blist[j];
fb = toDecimal(f(b.toString()));
if (verbose) {
console.log(nfev + "\t\t" + a + "\t\t" + fa + "\t\t" + b + "\t\t" + fb);
var tries = 0;
do {
blist = [
aa.times(new Decimal("0.9")),
aa.times(new Decimal("1.1")),
aa.minus(new Decimal(1)),
aa.plus(new Decimal(1)),
aa.times(new Decimal("0.5")),
aa.times(new Decimal("1.5")),
aa.neg(),
aa.times(new Decimal(2)),
aa.times(new Decimal(10)).neg(),
aa.times(new Decimal(10))
];
for (var j = 0, len = blist.length; j < len; ++j) {
b = blist[j];
fb = toDecimal(f(b.toString()));
if (verbose) {
console.log(nfev + "\t\t" + aa + "\t\t" + fa + "\t\t" + b + "\t\t" + fb);
}
nfev += 1;
if (fa.s * fb.s <= 0) {
a = aa;
bracketed = true;
break;
}
}
nfev += 1;
if (fa.s * fb.s <= 0) break;
}
aa = aa.times(new Decimal(Math.random().toString())).plus(new Decimal(tries*(Math.random() - 0.5).toString()));
fa = toDecimal(f(aa.toString()));
} while (!bracketed && ++tries < maxiter);
}

var u, fu;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fzero",
"version": "0.2.0",
"version": "0.2.1",
"description": "Find a zero of a univariate function.",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 3 additions & 7 deletions test/fzero.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ function lslmsr(n, q, i, a, xi) {
qj[j] = q[j];
sum_q = sum_q.plus(q[j]);
}
// console.log(q[0].toString(), q[1].toString());
qj.splice(i, 1);
// console.log(qj[0].toString());
var q_plus_n = n.plus(sum_q);
var b = a.times(q_plus_n);
var exp_qi = q[i].plus(n).dividedBy(b).exp();
Expand All @@ -60,7 +58,6 @@ function lslmsr(n, q, i, a, xi) {


test("fzero", function (t) {
// t.plan(15);
var trial = function (tr) {
var code = (tr.code === undefined) ? 1 : tr.code;
var options = (tr.options === undefined) ? "" : ", " + JSON.stringify(tr.options);
Expand Down Expand Up @@ -215,7 +212,7 @@ test("fzero", function (t) {
},
label: "lslmsr(n, ['71.43667960674091200687', '72.6286796067409120068'], 0, '0.00790000000000000001', '0.6')",
bounds: 0.5,
options: {verbose: false},
options: {verbose: true},
expected: "1.61713310"
});
trial({
Expand All @@ -233,9 +230,8 @@ test("fzero", function (t) {
options: {tolx: 1e-12, maxiter: 1000},
expected: "1.61713310"
});

t.throws(function () { fzero(log, 2, 3); }, /Invalid initial bracketing/, "fzero(log, 2, 3) throws Error('Invalid initial bracketing')");
t.throws(function () { fzero(log, -1, 0); }, /Zero point is not bracketed/, "fzero(log, -1, 0) throws Error('Zero point not bracketed')");
t.throws(function () { fzero(log, [2, 3]); }, /Invalid initial bracketing/, "fzero(log, 2, 3) throws Error('Invalid initial bracketing')");
t.throws(function () { fzero(log, [-1, 0]); }, /Zero point is not bracketed/, "fzero(log, -1, 0) throws Error('Zero point not bracketed')");
t.throws(function () { fzero(exp); }, /Initial guess required/, "fzero(exp)");
t.end();
});

0 comments on commit 9965f53

Please sign in to comment.