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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@step-wise/polynomials

v0.1.0

Published

Utilities for representing and manipulating sparse multivariable polynomials.

Downloads

256

Readme

@step-wise/polynomials

Utilities for representing and manipulating sparse multivariable polynomials such as 2+4*b+3*a+5*a*b. Only nonzero terms are stored.

Installation

npm install @step-wise/polynomials

Representation

A polynomial contains an ordered list of variables and a sparse list of terms:

import type { Polynomial } from '@step-wise/polynomials'

const polynomial: Polynomial = {
	variables: ['a', 'b'],
	terms: [
		{ coefficient: 2, exponents: [0, 0] },
		{ coefficient: 4, exponents: [0, 1] },
		{ coefficient: 3, exponents: [1, 0] },
		{ coefficient: 5, exponents: [1, 1] },
	],
}

Each exponent corresponds to the variable at the same index. For example, { coefficient: 5, exponents: [1, 1] } represents 5*a*b.

Polynomials use a canonical shape:

  • Terms have nonzero, finite coefficients.
  • Exponents are non-negative safe integers.
  • Every term has one exponent per variable.
  • Terms are unique and sorted by their exponent arrays.
  • The zero polynomial has no terms. It may retain a variable list, while createConstantPolynomial(0) produces { terms: [], variables: [] }.
  • A nonzero constant has no variables and one term, such as { terms: [{ coefficient: 5, exponents: [] }], variables: [] }.

Use createPolynomial instead of assembling this shape manually. It validates the input, combines like terms, removes zero terms and sorts the result.

Quick start

import { createPolynomial, evaluatePolynomial, polynomialToString } from '@step-wise/polynomials'

const polynomial = createPolynomial([
	{ coefficient: 2, exponents: [0, 0] },
	{ coefficient: 4, exponents: [0, 1] },
	{ coefficient: 3, exponents: [1, 0] },
	{ coefficient: 5, exponents: [1, 1] },
], ['a', 'b'])

polynomialToString(polynomial) // 2+4*b+3*a+5*a*b
evaluatePolynomial(polynomial, { a: 2, b: 3 }) // 50

Creation and validation

  • createPolynomial(terms, variables) creates a canonical sparse polynomial.
  • createConstantPolynomial(value) creates a constant polynomial.
  • ensurePolynomial(value) validates that a value is a canonical polynomial and returns it.
  • ensurePolynomialVariables, ensurePolynomialExponents and ensurePolynomialTerm validate the corresponding constituent types.

Manipulation

  • negatePolynomial(polynomial) returns -f.
  • scalePolynomial(polynomial, factor) multiplies every coefficient by a factor.
  • addConstantToPolynomial(polynomial, value) changes the constant term.
  • oneMinusPolynomial(polynomial) returns 1-f.
  • addPolynomials(polynomials, variables?) adds one or more polynomials.
  • subtractPolynomials(a, b, variables?) returns a-b.
  • multiplyPolynomials(polynomials, variables?) multiplies one or more polynomials.
  • raisePolynomialToPower(polynomial, exponent) expands a non-negative integer power.
  • getPolynomialPowers(polynomial, maxExponent) returns powers from zero through the maximum, inclusive.

The optional variables argument on addition, subtraction and multiplication determines the variable order of the result. Without it, the functions derive an order from their operands.

Variables, substitution and moments

  • alignPolynomialVariables(polynomial, variables) changes the variable order and can add or remove inactive variables.
  • substitutePolynomial(polynomial, values) substitutes the provided variables and returns the remaining polynomial.
  • evaluatePolynomial(polynomial, values) requires a value for every variable and returns a number.
  • substitutePolynomialMoments(polynomial, getMoment, variables?) substitutes independent moments returned by getMoment(variable, exponent).

Moment substitution uses the independence identity E[X^a * Y^b] = E[X^a] * E[Y^b]. Moments requested more than once during one substitution are cached.

Comparison and display

  • comparePolynomialTerms(a, b) compares two canonical term lists with floating-point tolerance for coefficients.
  • comparePolynomials(a, b, options?) compares complete polynomials. Variable reordering is allowed by default; pass { allowVariableReordering: false } to require the same order.
  • polynomialToString(polynomial) creates a plain-text representation.

Dense univariate conversion

getUnivariatePolynomialCoefficients(polynomial) converts a polynomial with exactly one variable to a dense coefficient array ordered by exponent. Missing powers become zeroes:

import { createPolynomial, getUnivariatePolynomialCoefficients } from '@step-wise/polynomials'

const polynomial = createPolynomial([
	{ coefficient: 2, exponents: [0] },
	{ coefficient: 5, exponents: [3] },
], ['x'])

getUnivariatePolynomialCoefficients(polynomial) // [2, 0, 0, 5]

This helper is intended for interoperability with APIs that require dense univariate coefficients. It rejects constant and multivariable polynomials.