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-genericre-exported by both facades. - Named variables through variable contexts, bridged to
type_theorynames. - Checked variants of every partial operation, returning
Noneinstead 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 ; 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.
| Part | Tutorial | API | Design |
|---|---|---|---|
core: exponent vectors, variables and contexts, shapes, capability traits, operation records | tutorial | API | design |
immut: facade of the immutable layer | tutorial | API | design |
immut/dense: dense univariate DensePolynomial | tutorial | API | design |
immut/term: sorted-term TermPolynomial | tutorial | API | design |
immut/sparse: ordered-map SparsePolynomial | tutorial | API | design |
immut/context: named-variable ContextPolynomial and substitution | tutorial | API | design |
mutable: facade of the mutable layer | tutorial | API | design |
mutable/dense: mutable DensePolynomial | tutorial | API | design |
mutable/term: mutable TermPolynomial | tutorial | API | design |
mutable/sparse: mutable SparsePolynomial | tutorial | API | design |
mutable/context: mutable ContextPolynomial | tutorial | API | design |
internal: module-private natural powers | tutorial | API | design |
consistency: test-only agreement checks between layers | tutorial | API | design |
| Architecture guide: layers, dependencies, invariants | architecture | ||
| Contributing guide | contributing |
Representations
DensePolynomial: univariate, ascending coefficient vector; Karatsuba, composition, derivativeTermPolynomial: multivariate, terms in descending monomial order; leading term firstSparsePolynomial: multivariate, ordered map from monomials to coefficients; logarithmic lookupContextPolynomial,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 bundlesUnivariatePolynomial,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")
}
Where to read next
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.
- New to the library: read the immut tutorial, then the tutorial of the representation you need: dense for one variable, term or sparse for several, context for named variables and substitution.
- Using it in a library: keep the immut API and the per-representation API pages at hand. Read the mutable tutorial when profiling shows that intermediate values matter, and the core tutorial to write code that works for every representation.
- Contributing: read the architecture guide, the core design, the design pages of the packages you touch, the consistency page and the contributing guide.
Validation
Recommended release checks, from the repository root (./ready_to_pr.sh runs them):
moon fmt
moon check --target all
moon test
moon info