Polynomials

Polynomials in several variables

Choose the variables, and every polynomial in them lives in one ring. Arithmetic is exact, and a polynomial prints the way you would write it.

The ring K[x₁, …, xₙ]

Fix a field KK of coefficients and variables x1,…,xnx_1, \dots, x_n. A monomial is a product of powers xa=x1a1⋯xnanx^a = x_1^{a_1}\cdots x_n^{a_n} with whole-number exponents, and a polynomial is a finite sum

f=∑aca xa,ca∈K. f = \sum_a c_a\, x^a, \qquad c_a \in K .

Two polynomials are equal exactly when they have the same coefficients. Sums and products follow the usual rules, with xa⋅xb=xa+bx^a \cdot x^b = x^{a+b}, and the result is a ring K[x1,…,xn]K[x_1, \dots, x_n]. The total degree of a monomial is a1+⋯+ana_1 + \dots + a_n, and the degree of ff is the largest total degree among its terms.

The library stores only the nonzero terms (a sparse representation), so x100y−1x^{100}y - 1 takes two entries, and multiplication costs the product of the numbers of terms.

Making polynomials

import { ring } from "@ratmath/structures";

const R = ring("x, y, z");            // Q[x, y, z], terms ordered by grevlex
const [x, y, z] = R.gens;             // the variables as polynomials
const f = R.poly("x^2 y - 3/2 x z + 1");
const g = x.pow(2).mul(y).sub(x.mul(z).mul("3/2")).add(1);
f.equals(g);                          // true
String(f);                            // "x²y − (3/2)xz + 1"
f.toText();                           // "x^2*y - 3/2*x*z + 1"   (R.poly reads this back)
f.toTeX();                            // "x^{2}y - \\frac{3}{2}xz + 1"

Text allows juxtaposition for products (3xy), ^ or ** for powers, parentheses, and exact numbers in any form Core reads: 3/4, 1.25, 0.#3 (one third). Dividing is allowed when it comes out exactly, as in (x^2 - 1)/(x - 1). Every method that takes a polynomial also takes text or a number.

Looking inside

f.degree();              // 3      total degree
f.degree("x");           // 2      degree in x
f.degrees();             // { x: 2, y: 1, z: 1 }
f.variables();           // ["x", "y", "z"]
f.coefficient({ x: 1, z: 1 });    // Rational −3/2
f.coefficients("x");     // [1, −(3/2)z, y]   as a polynomial in x, constant first
f.leadingTerm();         // x²y  (largest in the ring's monomial order)
f.terms();               // [{ coefficient, exponents, monomial }, …]
f.isHomogeneous();       // false

Calculus and substitution

Partial derivatives are taken term by term: ∂x(c xayb)=a c xa−1yb\partial_x (c\,x^a y^b) = a\,c\,x^{a-1}y^b. Substitution replaces a variable by a number or another polynomial of the ring; evaluating at a point gives an exact number.

f.diff("x");                      // 2xy − (3/2)z
f.gradient();                     // [∂f/∂x, ∂f/∂y, ∂f/∂z]
f.subs({ z: "x + y" });           // still in Q[x, y, z]
f.at({ x: 2, y: "1/3", z: 1 });   // Rational −2/3
f.compose({ x: "s + t", y: "s - t", z: 1 }, ring("s, t"));   // a change of variables
f.homogenize("h");                // every term brought up to degree 3 with a new variable h

Other coefficient fields

A ring takes any field object RatMath uses, such as those of @ratmath/algebraic. Over the integers modulo 5 the "freshman's dream" (x+y)5=x5+y5(x + y)^5 = x^5 + y^5 is true, because every middle binomial coefficient is divisible by 5.

import { ring, primeField, NumberField } from "@ratmath/structures";

const S = ring("x, y", { field: primeField(5) });
String(S.poly("(x + y)^5"));          // "x⁵ + y⁵"

const K = NumberField.quadratic(2);   // Q(√2)
const T = ring("x", { field: K });
const r = T.constant(K.gen);          // √2 as a constant polynomial
String(T.gens[0].add(r).pow(2));      // "x² + 2√2·x + 2"
Try itEdit the code and run it with @ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs