New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

brents-method

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brents-method

A root solver for functions using Brent's Method.

latest
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

Brent's Method

Description

Brent's Method is a novel, highly efficient method for finding the roots of a function within given bounds - that is, where the function returns 0 (or very nearly 0), also known as an x-intercept.

Wikipedia's entry reads:

In numerical analysis, Brent's method is a root-finding algorithm combining the bisection method, the secant method and inverse quadratic interpolation. It has the reliability of bisection but it can be as quick as some of the less-reliable methods. The algorithm tries to use the potentially fast-converging secant method or inverse quadratic interpolation if possible, but it falls back to the more robust bisection method if necessary.

Example

const brentMethod = require('brents-method')

brentsMethod(
  x => (x + 3) * Math.pow(x - 1, 2),
  -4,
  4 / 3,
) // -3

Parameters

ParamTypeDefaultDescription
ffunctionThe function for which roots are desired.
lowerLimitNumberThe lower value used to bracket root finding into the function.
upperLimitNumberThe upper value used to bracket root finding into the function.
optionsObjectAn optional arguments object.
[options.errorTolerance]Number1e-7Used to determine how close the bounds bracketing root finding must converge before quitting.
[options.maxIterations]Number50The maximum number of iterations before root finding will fail.

Specifics

In my implementation, the inverse quadratic interpolation is applied directly. In other cases, the Lagrange polynomial is reduced by defining the variables p, q, r, s, and t, and the x value is not overwritten with the bisection method, but modified. For simplicity's sake, the inverse quadratic interpolation is applied directly in this implementation and the new guess is overwritten if needed.

This is based in part on a number of other implementations and references.

Useful references: https://www.youtube.com/watch?v=-bLSRiokgFk https://en.wikipedia.org/wiki/Brent%27s_method#Brent's_method https://en.wikipedia.org/wiki?title=Talk:Brent%27s_method#Removed_code

Gotchas

Several implementations I have seen contain a common error, which I carefully corrected. The bug isn't fatal, but can cause the algorithm to run extra iterations. Two of these are listed below, both as useful references and as a warning to avoid this error.

One in Julia And one in C++

Testing

npm test

If you have suggestions for particularly trying functions, or better tests, please hit me up 😁

TODO:

  • More thorough description than a wiki link
  • Usage stats

Feedback ✉️

Website 🌐

js@jacobsmith.tech

https://github.com/limeandcoconut 🐙😸

@limeandcoconut 🐦

Cheers!

License

ISC, see license for details.

Keywords

brent

FAQs

Package last updated on 19 Jul 2020

Did you know?

Socket

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.

Install

Related posts