
Research
Security News
Malicious npm Packages Target BSC and Ethereum to Drain Crypto Wallets
Socket uncovered four malicious npm packages that exfiltrate up to 85% of a victim’s Ethereum or BSC wallet using obfuscated JavaScript.
brent-zero-generator
Advanced tools
This module implements Brent's algorithm to find a root of a function f()
.
The module provides 3 functions: zero()
, zero_generator()
, and
zero_generator2()
. The functions zero_generator()
and zero_generator2()
facilitate solving asynchronous functions, and provide a lot of flexibility
(see example below). For typical use, the simpler zero()
function should
be sufficient.
Before you consider using another similar code, have a look at the README.md of my BrentZeroTester.JS repository, where several alternative codes are compared. Both horrible performances, and wrong results happen in some of these codes.
The package chandrupatla-zero contains my JavaScript implementation of T.R. Chandrupatla's algorithm. The repository compare-zeros, in my opinion, shows it is a bit better than Brent's algorithm.
The JavaScript code in this module follows Brent's original code of the
Algol 60 procedure zero()
very closely. In the first comment in that code,
Brent describes procedure zero()
as:
Procedure zero retuns a zero x of the function f in the given interval [a, b], to within a tolerance (4 macheps |x| + 2 t), where macheps is the relative machine precision and t is a positive tolerance. The procedure assumes that f(a) and f(b) have different signs
Parameter | Description |
---|---|
f | the function f() for which the root is sought. Only for the utility function zero() . |
a | the finite lowerbound of the interval in which to search the root |
b | the finite upperbound of the interval in which to search the root |
macheps | the relative tolerance. This parameter may be increased if a higher relative error is acceptable, but it should not be decreased, for then rounding errors might prevent convergence. (optional, default: Number.EPSILON ) |
t | the absolute tolerance. (optional, default: 0.0 ) |
zero_generator2(a, b, macheps, t)
, andzero_generator(a, b, macheps, t)
, andzero(f, a, b, macheps, t)
.The generator functions zero_generator()
and zero_generator2()
only yield,
the 'x' values, to request function evaluations. When the zero_generator2()
function is 'done', it returns an array with the following contents:
The functions zero_generator()
and zero()
only return the x-value,
thought to be closest to the root.
f(x) == 0
is found, or the interval [a, b] has
been sufficiently shrunk to be sure the x
is within the tolerance.
This says nothing about the value of f(x)
. For example the function
x => x < 3 ? -1 : 2
, will find a "root" close to 3, but f(x)
will be -1.The following examples shows simple usage. Given a function f(x) = x² - 5.
zero()
exampleconst brents = require('brent-zero-generator');
const f = x => x * x - 5;
console.log( brents.zero(f, 1, 4).toFixed(6) );
// expect 2.236068
zero_generator2()
exampleconst gen = brents.zero_generator2(-3, -2);
let result = gen.next();
while ( ! result.done ) {
const x = result.value;
const y = f(x);
result = gen.next(y)
}
console.log( result.value.toFixed(6) );
// expect [ -2.236068, 8.8e-16, -2.236068, -3.5e-15 ]
zero_generator()
exampleOften implementations have additional parameters for maximum number of
iterations (max_iter
), or an allowed tolerance for the root's function value
(yTol
). Brent's original code does not have these options, and I am fairly
sure he would not agree with them. However, using the generator function,
they are easily implemented, as the following example function shows:
function myZero(f, a, b, max_iter, yTol) {
const gen = zero_generator(a, b);
let result = gen.next();
for (let i=0; i<max_iter && ! result.done; ++i) {
const x = result.value;
const y = f(x);
if (Math.abs(y) < yTol) break;
result = gen.next(y)
}
return result.value;
}
zero_generator2()
as an export.The ultimate documentation on this algorithm can be found on Emeritus Professor
Richard P. Brent's page dedicated to his book Algorithms for Minimization
Without Derivatives.
On that page, not only the errata for the different editions of the book are
available, but also a scanned copy of the Prentice-Hall edition is also
available for download.
C, C++, Fortran66/Fortran77 and Fortran90 codes can be found on John Burkardt's homepage.
A Wolfram-MathWorld page explains the way in which the inverse quadratic interpolation, which is important for the numerical stability, is implemented.
FAQs
Brent's root finding algorithm, as a generator function.
The npm package brent-zero-generator receives a total of 757 weekly downloads. As such, brent-zero-generator popularity was classified as not popular.
We found that brent-zero-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket uncovered four malicious npm packages that exfiltrate up to 85% of a victim’s Ethereum or BSC wallet using obfuscated JavaScript.
Security News
TC39 advances 9 JavaScript proposals, including Array.fromAsync, Error.isError, and Explicit Resource Management, which are now headed into the ECMAScript spec.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.