FloPoly Polynomial Utility
FloPoly is a JavaScript/TypeScript utility library focused on efficient and accurate real root-finding for real-coefficient polynomials. Currently at version 7.0.1, it supports Node.js (>=12.20.0) and browser environments, distributed as an ECMAScript Module (ESM). The library excels in root-finding for polynomials up to approximately degree 20, employing Rolle's Theorem. It offers advanced capabilities like 'certified' root-finding, which are robust against high condition numbers, and supports various coefficient precisions including `double`, `double-double`, `Shewchuk expansions`, and `bigint`. Beyond root-finding, FloPoly provides a suite of polynomial operators. Its ESM-only distribution simplifies its use in modern JavaScript projects, though it marks a breaking change for older CommonJS environments. While a precise release cadence is not specified, the version jumps suggest significant, though not frequent, updates.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` to import `flo-poly` in an ESM-only Node.js environment or a browser.fixEnsure your project is set up for ESM by adding `"type": "module"` to your `package.json` and using `import { ... } from 'flo-poly';`. If in a browser without a bundler, import from the explicit browser path: `import { ... } from './node_modules/flo-poly/browser/index.min.js';` -
TypeError: (0 , flo_poly__WEBPACK_IMPORTED_MODULE_0__.allRoots) is not a function
cause This error typically occurs in a bundled environment (e.g., Webpack) when a CommonJS default export is incorrectly imported as a named ESM export, or vice-versa. Although FloPoly is ESM-only, incorrect import syntax can trigger similar errors. It could also happen if a module is incorrectly interpreted as having a default export when it only has named exports.fixVerify that `allRoots` is correctly imported as a named export: `import { allRoots } from 'flo-poly';`. FloPoly primarily uses named exports. If the issue persists with a bundler, ensure your bundler configuration correctly handles ESM imports, particularly for TypeScript projects where `resolve-typescript-plugin` might be needed. -
Uncaught SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import` syntax in a browser `<script>` tag without `type="module"` or in a Node.js environment that is configured for CommonJS.fixFor browsers, ensure your script tag is `<script type="module">`. For Node.js, ensure your `package.json` includes `"type": "module"` to enable ESM globally, or run with `node --input-type=module` for single files.
Warnings
- breaking FloPoly is an ESM-only package since version 1.0.0 (or possibly earlier, certainly by 7.x.x). This means it cannot be `require()`-d in CommonJS environments without a transpilation step or a dynamic `import()` call, which may be slower.
- gotcha The `allRoots` function does not account for error bounds and can yield inaccurate results for polynomials with high condition numbers.
- gotcha The library's performance using Rolle's Theorem for root finding becomes asymptotically slower (`O(n²)`) for higher polynomial degrees, typically beyond degree 20, compared to `O(n)` methods like Descartes' Rule of Signs.
- gotcha Evaluating polynomials with large `|x|` values can lead to floating-point overflow, as standard double-precision numbers have a maximum value of approximately `1.8 x 10^308`.
Install
-
npm install flo-poly -
yarn add flo-poly -
pnpm add flo-poly
Imports
- allRoots
const { allRoots } = require('flo-poly');import { allRoots } from 'flo-poly'; - allRootsCertifiedSimplified
import allRootsCertifiedSimplified from 'flo-poly';
import { allRootsCertifiedSimplified } from 'flo-poly'; - add
import { add } from 'flo-poly'; - allRoots (browser)
import { allRoots } from 'flo-poly';import { allRoots } from './node_modules/flo-poly/browser/index.min.js';
Quickstart
import { allRoots, allRootsCertifiedSimplified, add } from 'flo-poly';
// Example 1: Finding roots of a double-precision polynomial
const p1 = [1, -21, 175, -735, 1624, -1764, 720]; // x^6 - 21x^5 + ... + 720
const roots1 = allRoots(p1);
console.log('Roots (approximate):', roots1);
// Example 2: Certified root finding for high accuracy
const p2 = [1, -21, 175, -735, 1624, -1764, 720];
const certifiedRoots = allRootsCertifiedSimplified(p2);
console.log('Roots (certified simplified):', certifiedRoots);
// Example 3: Polynomial addition
const polyA = [1, 2, 3]; // 1x^2 + 2x + 3
const polyB = [3, 4]; // 3x + 4
const sumPoly = add(polyA, polyB);
console.log('Polynomial sum (1x^2 + 2x + 3) + (3x + 4):', sumPoly); // Expected: [1, 5, 7]
if (roots1.length === 6 && sumPoly.length === 3) {
console.log('FloPoly functions executed successfully! 😁');
} else {
console.log('Something went wrong. 😥');
}