arithmetic API

Purpose

Luna-Flow/linear-algebra/arithmetic is the scalar-operation layer of the repository. It re-exports the scalar types and traits that linear-algebra code uses from Luna-Flow/luna-generic and Luna-Flow/arithmetic, and adds five small operation traits: Abs, ApproxEq, CheckedDiv, CheckedSqrt and CheckedCompare. An operation trait says that an operation is available; it does not claim algebraic laws.

Source: src/arithmetic/operation_traits.mbt, src/arithmetic/alias.mbt. The reasoning is in the arithmetic design.

Importing

Import the package, and the upstream Luna-Flow/arithmetic for the context and error types, in moon.pkg:

///|
import {
  "Luna-Flow/linear-algebra/arithmetic" @la_arithmetic,
  "Luna-Flow/arithmetic" @lf_arith,
}

The examples on this page write every name with its package prefix, such as @la_arithmetic., instead of a using declaration: all pages of this manual compile into one test package, where the declarations of different pages would clash.

Re-exported names

The package re-exports upstream names with pub using, so @la_arithmetic.Sqrt and @lf_arith.Sqrt denote the same trait, and a scalar type that implements the upstream trait satisfies the re-exported one. Their full behaviour is documented upstream: luna-generic and arithmetic.

Zero, One, Inverse, Conjugate

These re-export the scalar operation traits of luna-generic: the additive and multiplicative identities, the multiplicative inverse and conjugation.

pub using @luna-generic {trait Zero}
pub using @luna-generic {trait One}
pub using @luna-generic {trait Inverse}
pub using @luna-generic {trait Conjugate}
NameMethodMeaning
Zerozero()additive identity 00
Oneone()multiplicative identity 11
Inverseinv(x)x−1x^{-1}; the Float and Double instances abort on 00
Conjugateconjugate(x)x‾\overline{x}; the builtin real types have no instance

They state no laws by themselves; the structure traits of luna-generic (Ring, Field, …) give them their meaning.

Sqrt, Cbrt, Power, Exponential, Logarithmic, Constants

These re-export the unchecked analytic operations of Luna-Flow/arithmetic.

pub using @Luna-Flow/arithmetic {trait Sqrt}
pub using @Luna-Flow/arithmetic {trait Cbrt}
pub using @Luna-Flow/arithmetic {trait Power}
pub using @Luna-Flow/arithmetic {trait Exponential}
pub using @Luna-Flow/arithmetic {trait Logarithmic}
pub using @Luna-Flow/arithmetic {trait Constants}
NameMethodsMeaning
Sqrtsqrt(x)x\sqrt{x}; IEEE NaN for x<0x < 0
Cbrtcbrt(x)x3\sqrt[3]{x}
Powerpow(x, y)xyx^y
Exponentialexp(x), exp2(x)exe^x, 2x2^x
Logarithmicln(x), log2(x), log10(x)logarithms
Constantspi(), tau(), e()π\pi, 2π2\pi, ee

Sqrt is the one this repository uses: mutable needs it for cholesky_decomposition, eigen, frobenius_norm and std_dev.

SqrtChecked, DivChecked, CompareChecked

These re-export the checked operations of Luna-Flow/arithmetic, which return Result instead of an IEEE special value.

pub using @Luna-Flow/arithmetic {trait SqrtChecked}
pub using @Luna-Flow/arithmetic {trait DivChecked}
pub using @Luna-Flow/arithmetic {trait CompareChecked}
NameMethod
SqrtCheckedsqrt_checked(x, ctx) -> Result[Self, ArithmeticError]
DivCheckeddiv_checked(x, y, ctx) -> Result[Self, ArithmeticError]
CompareCheckedcompare_checked(x, y) -> Result[Int, ArithmeticError]

The local traits CheckedSqrt, CheckedDiv and CheckedCompare below delegate to them for Float and Double.

ArithmeticContext, ArithmeticError, ArithmeticErrorKind, FpClass, RoundingMode

These re-export the types that the checked operations take and return.

pub using @Luna-Flow/arithmetic {type ArithmeticContext}
pub using @Luna-Flow/arithmetic {type ArithmeticError}
pub using @Luna-Flow/arithmetic {type ArithmeticErrorKind}
pub using @Luna-Flow/arithmetic {type FpClass}
pub using @Luna-Flow/arithmetic {type RoundingMode}
NameMeaning
ArithmeticContextprecision, rounding and optional exponent limits (e_min, e_max, clamp); build it with ArithmeticContext::new(precision)
ArithmeticErrorstructured scalar error with fields kind and message
ArithmeticErrorKindDivisionByZero, DomainError, UnorderedComparison, ParseError, FormatError, UnsupportedOperation, and CertificationFailure(detail) for proof-backed backends (never produced for Float or Double); keep a fallback branch when matching
FpClassFinite, Infinity, NaN
RoundingModeToNearestEven, TowardZero, TowardPositive, TowardNegative, AwayFromZero
///|
fn[T : @la_arithmetic.Zero + @la_arithmetic.Sqrt + Add + Mul] arith_api_hypot(
  x : T,
  y : T,
) -> T {
  @la_arithmetic.Sqrt::sqrt(@la_arithmetic.Zero::zero() + x * x + y * y)
}

///|
test "re-exported traits are the upstream ones" {
  inspect(arith_api_hypot(3.0, 4.0), content="5")
  let ctx = @lf_arith.ArithmeticContext::new(53)
  inspect(
    @la_arithmetic.SqrtChecked::sqrt_checked(-1.0, ctx) is Err(_),
    content="true",
  )
}

Absolute value

Abs

Abs marks scalar types with an absolute value.

pub(open) trait Abs {
  fn abs(Self) -> Self
}
pub impl Abs for Int
pub impl Abs for Float
pub impl Abs for Double

Abs::abs

Abs::abs returns ∣x∣|x|.

fn Abs::abs(Self) -> Self

The implementations delegate to Num::abs of luna-generic. For Int, ∣−231∣|-2^{31}| wraps to −231-2^{31}, as in two’s-complement arithmetic.

///|
fn[T : @la_arithmetic.Abs] arith_api_magnitude(x : T) -> T {
  @la_arithmetic.Abs::abs(x)
}

///|
test "Abs on integers and doubles" {
  inspect(arith_api_magnitude(-3), content="3")
  inspect(arith_api_magnitude(-2.5), content="2.5")
}

Approximate equality

ApproxEq

ApproxEq marks scalar types with an approximate comparison.

pub(open) trait ApproxEq {
  fn approx_eq(Self, Self) -> Bool
}
pub impl ApproxEq for Int
pub impl ApproxEq for Float
pub impl ApproxEq for Double

ApproxEq::approx_eq

ApproxEq::approx_eq reports whether two values are within a fixed absolute tolerance of each other.

fn ApproxEq::approx_eq(Self, Self) -> Bool
TypeRule
Inta=ba = b
Float∣a−b∣≤10−6\lvert a - b\rvert \le 10^{-6}
Double∣a−b∣≤10−12\lvert a - b\rvert \le 10^{-12}

The tolerance is absolute, so it is too strict for large magnitudes and too loose for tiny ones, and the relation is not transitive. It returns false whenever an operand is NaN, and for an infinity compared with itself (∞−∞\infty - \infty is NaN). See the design page for the consequences.

///|
test "ApproxEq uses an absolute tolerance" {
  inspect(
    @la_arithmetic.ApproxEq::approx_eq(1.0, 1.0 + 1.0e-13),
    content="true",
  )
  inspect(
    @la_arithmetic.ApproxEq::approx_eq(1.0e20, 1.0e20 + 1.0e5),
    content="false",
  )
  inspect(@la_arithmetic.ApproxEq::approx_eq(3, 3), content="true")
}

Checked operations

The checked traits return Result[_, ArithmeticError] instead of a NaN or an infinity. The implementations for Float and Double delegate to the upstream DivChecked, SqrtChecked and CompareChecked traits; they accept an ArithmeticContext for interface uniformity but ignore it, because hardware binary floating point has a fixed precision.

CheckedDiv

CheckedDiv marks scalar types with a division that reports invalid operands.

pub(open) trait CheckedDiv {
  fn checked_div(Self, Self, @Luna-Flow/arithmetic.ArithmeticContext) -> Result[Self, @Luna-Flow/arithmetic.ArithmeticError]
}
pub impl CheckedDiv for Float
pub impl CheckedDiv for Double

CheckedDiv::checked_div

CheckedDiv::checked_div(x, y, ctx) returns Ok(x / y) or an error.

fn CheckedDiv::checked_div(Self, Self, ArithmeticContext) -> Result[Self, ArithmeticError]
OperandsResult
0/00 / 0Err, kind DomainError
±∞/±∞\pm\infty / \pm\inftyErr, kind DomainError
x/0x / 0, x≠0x \ne 0 (also for x=∞x = \infty or NaN)Err, kind DivisionByZero
otherwiseOk(x / y), rounded to nearest; NaN operands give Ok(NaN)

CheckedSqrt

CheckedSqrt marks scalar types with a square root that reports domain errors.

pub(open) trait CheckedSqrt {
  fn checked_sqrt(Self, @Luna-Flow/arithmetic.ArithmeticContext) -> Result[Self, @Luna-Flow/arithmetic.ArithmeticError]
}
pub impl CheckedSqrt for Float
pub impl CheckedSqrt for Double

CheckedSqrt::checked_sqrt

CheckedSqrt::checked_sqrt(x, ctx) returns Ok(√x) for x≥0x \ge 0 and an error with kind DomainError for x<0x < 0.

fn CheckedSqrt::checked_sqrt(Self, ArithmeticContext) -> Result[Self, ArithmeticError]

A NaN argument is passed through as Ok(NaN).

CheckedCompare

CheckedCompare marks scalar types with a three-way comparison that reports unordered operands.

pub(open) trait CheckedCompare {
  fn checked_compare(Self, Self) -> Result[Int, @Luna-Flow/arithmetic.ArithmeticError]
}
pub impl CheckedCompare for Float
pub impl CheckedCompare for Double

CheckedCompare::checked_compare

CheckedCompare::checked_compare(x, y) returns Ok(-1), Ok(0) or Ok(1) for x<yx < y, x=yx = y and x>yx > y, and an error with kind UnorderedComparison when either operand is NaN.

fn CheckedCompare::checked_compare(Self, Self) -> Result[Int, ArithmeticError]

The three checked traits together:

///|
test "checked scalar operations" {
  let ctx = @lf_arith.ArithmeticContext::new(53)
  inspect(
    @la_arithmetic.CheckedDiv::checked_div(6.0, 2.0, ctx).unwrap(),
    content="3",
  )
  match @la_arithmetic.CheckedDiv::checked_div(1.0, 0.0, ctx) {
    Err(e) => inspect(e.is_division_by_zero(), content="true")
    Ok(_) => fail("1 / 0 must fail")
  }
  match @la_arithmetic.CheckedSqrt::checked_sqrt(-4.0, ctx) {
    Err(e) => inspect(e.is_domain_error(), content="true")
    Ok(_) => fail("sqrt(-4) must fail")
  }
  inspect(
    @la_arithmetic.CheckedCompare::checked_compare(2.0, 3.0).unwrap(),
    content="-1",
  )
  let nan = 0.0 / 0.0
  inspect(
    @la_arithmetic.CheckedCompare::checked_compare(nan, 1.0) is Err(_),
    content="true",
  )
}

Where the traits are used

The concrete matrix packages take their scalar bounds from luna-generic (Zero, AddMonoid, Semiring, Field, Num) and from Sqrt. The @mutable numerical routines use Sqrt through this re-export and their own Tolerance trait (see the mutable API). The local traits Abs, ApproxEq and the checked traits are building blocks for downstream algorithms; no matrix method of this repository requires them.