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 of coefficients and variables . A monomial is a product of powers with whole-number exponents, and a polynomial is a finite sum
Two polynomials are equal exactly when they have the same coefficients. Sums and products follow the usual rules, with , and the result is a ring . The total degree of a monomial is , and the degree of is the largest total degree among its terms.
The library stores only the nonzero terms (a sparse representation), so 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: . 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" 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"
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.