complexNewtonRaphson = {
var tmp = [0, 0, 0, 0];
return function(out, f, z0, tolerance, maxIterations, verbose) {
maxIterations = maxIterations === undefined ? 50 : maxIterations;
tolerance = tolerance === undefined ? 1e-5 : tolerance;
out[0] = z0[0];
out[1] = z0[1];
var re, im;
re = z0[0];
im = z0[1];
var iteration = maxIterations;
var delta = 1;
var previousDelta = 0;
var error = Infinity;
while (error > tolerance && --iteration > 0) {
f(tmp, re, im);
cdiv(tmp, tmp[0], tmp[1], tmp[2], tmp[3]);
re -= tmp[0];
im -= tmp[1];
delta = cmod(tmp[0], tmp[1]);
error = Math.abs(delta / previousDelta);
previousDelta = delta;
}
if (iteration === 0 && error > tolerance) {
if (verbose)
console.warn(
'Newton-Raphson failed to converge after ' +
maxIterations +
' iterations'
);
out[0] = z0[0];
out[1] = z0[1];
} else {
out[0] = re;
out[1] = im;
}
return out;
};
}