npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flo-poly

v6.0.8

Published

A practical, root-focused JavaScript polynomial utility library.

Downloads

26,455

Readme

Coverage Status Build Status

Overview

The focus is to find real roots of real coefficient polynomials from degree 1 up to about degree 20 as accurately and as fast as possible, e.g.

// some polynomial with double precision coefficients, i.e. x^6 - 21x^5 + 175x^4 - 735x^3 + 1624x^2 - 1764 + 720
const p = [1, -21, 175, -735, 1624, -1764, 720];  
allRoots(p); //=> [0.9999999999999997, 2.0000000000000013, 2.9999999999999316, 4.00000000000096, 5.000000000000012, 6.00000000000028]

However, the above function, allRoots, does not take error bounds into account and can thus be inaccurate if the roots have high condition numbers.

For extremely accurate (no matter how high the condition number) certified results use e.g.:

const p = [1, -21, 175, -735, 1624, -1764, 720];  // some polynomial with double precision coefficients
allRootsCertifiedSimplified(p);

or for a more flexible function that takes the input polynomial coefficients as double-double precision and the ability to specify error bounds on the coefficients in addition to a fallback function to specify exact coefficients (in the form of Shewchuk expansions) use allRootsCertified.

Though the focus is on root finding, the library include numerous useful operators on polynomials with double, double-double, Shewchuk expansion and bigint coefficients, e.g

add([1,2,3], [3,4]); //=> [1,5,7]

Why only up to about degree 20?

This isn't a hard limit. Roughly speaking, since the roots are found using Rolle's Theorem it becomes asymptotically slower (compared to Descartes Methods), i.e. roughly O(n²) vs O(n) the higher the polynomial degree, n.

Another reason is that evaluation of the polynomial at x when |x| >> 1 can result in overflow when the result is larger than about 1.8 x 10^308 (the max value a double precision floating point value can be).

Documentation

For more in-depth documentation please read the docs!.

Installation

npm install flo-poly

This package is ESM only and can be used in Node.js (or in a browser when bundled using e.g. Webpack).

Additionally, self-contained ECMAScript Module (ESM) files index.js and index.min.js in the ./browser folder is provided.

Usage

Node.js

import { allRoots } from 'flo-poly';

// some polynomial with double precision coefficients, i.e. x^6 - 21x^5 + 175x^4 - 735x^3 + 1624x^2 - 1764 + 720
const p = [1, -21, 175, -735, 1624, -1764, 720];  
const roots = allRoots(p);

if (roots.length === 6) {
    console.log('success! 😁');  // we should get to here!
} else {
    console.log('failure! 😥');  // ...and not here
}

Browsers - directly, without a bundler, using the pre-bundled minified .js file

Please note that no tree shaking will take place in this case.

<!doctype html>

<html lang="en">
<head>
    <script type="module">
        import { allRoots } from "./node_modules/flo-poly/browser/index.min.js";

        // some polynomial with double precision coefficients, i.e. x^6 - 21x^5 + 175x^4 - 735x^3 + 1624x^2 - 1764 + 720
        const p = [1, -21, 175, -735, 1624, -1764, 720];  
        const roots = allRoots(p);

        if (roots.length === 6) {
            console.log('success! 😁');  // we should get to here!
        } else {
            console.log('failure! 😥');  // ...and not here
        }
    </script>
</head>

<body>Check the console.</body>

</html>

Bundlers (Webpack, Rollup, ...)

Webpack will be taken as an example here.

Since your webpack config file might still use CommonJS you must rename webpack.config.js to webpack.config.cjs.

If you are using TypeScript:

Since this is an ESM only library you must use resolve-typescript-plugin (at least until webpack catches up with ESM?) in your webpack.config.cjs file.

npm install --save-dev resolve-typescript-plugin

and follow the instructions given at resolve-typescript-plugin.

Additionally, follow this guide.