luna-poly

This manual documents version 0.4.1 of Luna-Flow/luna-poly, the version in moon.mod.

Overview

luna-poly provides canonical polynomial types for MoonBit: dense univariate polynomials, multivariate polynomials as sorted term arrays or ordered maps, and polynomials over named variables with evaluation, partial evaluation and substitution. Every type comes in an immutable flavour, where polynomials are values, and a mutable flavour, where explicitly named methods update a container in place.

  • Canonical forms. Every representation removes zero terms and keeps one normal form, so == is equality of polynomials.
  • A shared monomial model. Exponent vectors without a fixed number of variables, ordered by a graded monomial order.
  • Algorithms with stated costs. Schoolbook and Karatsuba multiplication, Horner evaluation, composition, formal derivatives, binary powers, and simultaneous substitution as a ring homomorphism.
  • Generic code. Small capability traits, operation records, and the algebra traits of luna-generic re-exported by both facades.
  • Named variables through variable contexts, bridged to type_theory names.
  • Checked variants of every partial operation, returning None instead of aborting.

Version 0.2.0 replaced the former single root package by core, the implementation packages and the two facades. Import Luna-Flow/luna-poly/immut or Luna-Flow/luna-poly/mutable explicitly; code written for the old root package does not compile unchanged.

Version 0.3.0 bounds DensePolynomial::derivative by FromNat from luna-generic 0.4.0 instead of the deprecated NatHomomorphism. Fixed-width integer coefficients such as Int now have a derivative, computed modulo 2k2^k; a custom coefficient type needs a FromNat instance.

Install

moon add Luna-Flow/luna-poly@0.4.1

Then import a facade in your moon.pkg:

import {
  "Luna-Flow/luna-poly/immut",
}

The package needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20). It depends on Luna-Flow/luna-generic 0.4.0, Luna-Flow/arithmetic 0.5.2 and Luna-Flow/type_theory 0.4.1, which moon installs automatically.

Pages

Each package of the module has a tutorial, an API page and a design page. The two facades are the packages most programs import; internal and consistency are for contributors.

PartTutorialAPIDesign
core: exponent vectors, variables and contexts, shapes, capability traits, operation recordstutorialAPIdesign
immut: facade of the immutable layertutorialAPIdesign
immut/dense: dense univariate DensePolynomialtutorialAPIdesign
immut/term: sorted-term TermPolynomialtutorialAPIdesign
immut/sparse: ordered-map SparsePolynomialtutorialAPIdesign
immut/context: named-variable ContextPolynomial and substitutiontutorialAPIdesign
mutable: facade of the mutable layertutorialAPIdesign
mutable/dense: mutable DensePolynomialtutorialAPIdesign
mutable/term: mutable TermPolynomialtutorialAPIdesign
mutable/sparse: mutable SparsePolynomialtutorialAPIdesign
mutable/context: mutable ContextPolynomialtutorialAPIdesign
internal: module-private natural powerstutorialAPIdesign
consistency: test-only agreement checks between layerstutorialAPIdesign
Architecture guide: layers, dependencies, invariantsarchitecture
Contributing guidecontributing

Representations

  • DensePolynomial: univariate, ascending coefficient vector; Karatsuba, composition, derivative
  • TermPolynomial: multivariate, terms in descending monomial order; leading term first
  • SparsePolynomial: multivariate, ordered map from monomials to coefficients; logarithmic lookup
  • ContextPolynomial, ContextSubstitutionValue: polynomials over named variables, with named evaluation, partial evaluation and substitution

Each exists in immut and in mutable; the mutable types add set_coefficient, add_term_inplace, the *_inplace methods, copy, clear, to_immut and from_immut.

Shared vocabulary

  • Monomials: ExponentVector, ordered by a graded monomial order
  • Names: Variable, VariableContext
  • Shapes: PolynomialShape
  • Capability traits: HasLength, HasDegree, IsZero, HasTermCount, HasArity, HasTotalDegree, HasContext, HasShape, Clearable, Copyable, and the bundles UnivariatePolynomial, MultivariatePolynomial, ContextualPolynomial, MutablePolynomial
  • Operation records: UnivariateOps, MultivariateOps, ContextOps
  • Algebra traits re-exported from luna-generic: Zero, One, AddMonoid, MulMonoid, AddGroup, MulGroup, Semiring, Ring, Field, Num

A first example

test "first example" {
  let p = @immut.DensePolynomial::from_coefficients([1, 2, 3])
  inspect(p.pow(2).eval(2), content="289")

  let ctx = @immut.VariableContext::from_names(["x", "y"])
  let x = ctx.require_variable("x")
  let y = ctx.require_variable("y")
  let q = @immut.ContextPolynomial::from_named_terms_as_sparse(ctx, [
    ([(x, 2U)], 1),
    ([(x, 1U), (y, 1U)], 3),
    ([], 4),
  ])
  inspect(q.eval_named([(x, 2), (y, 5)]), content="38")
  inspect(q.eval_partial([(x, 2)]), content="8 + 6 * y")
}

The immut tutorial helps you pick a representation, and the tutorial of that representation shows the everyday tasks. The API pages state every precondition, failure case and cost, and the design pages derive the mathematics: the monomial order in core, Karatsuba and composition in immut/dense, substitution as a ring homomorphism in immut/context.

Validation

Recommended release checks, from the repository root (./ready_to_pr.sh runs them):

moon fmt
moon check --target all
moon test
moon info