Skip to main content

Crate puremp

Crate puremp 

Source
Expand description

puremp — arbitrary-precision arithmetic written entirely in Rust, depending on no foreign code.

It provides a family of numeric types, built bottom-up:

  1. Integers — unsigned Nat and signed Int, the workhorse layer that carries the hard limb-level algorithms (multiplication, division, GCD, modular arithmetic, …). Enabled by the int feature.
  2. RationalsRational, exact p/q fractions kept in lowest terms; plus InfRational, the same extended with ±∞/NaN. rational feature.
  3. DyadicsDyadic, exact n·2^-k binary fractions. dyadic feature.
  4. FloatsFloat, binary floating-point with a caller-chosen precision and directed RoundingMode, aiming at MPFR-class correct rounding, plus FixedFloat, a fixed-precision wrapper with operators. float feature.
  5. DecimalsDecimal, exact base-10 floating point (Python Decimal-style), with directed rounding. decimal feature.

Built on top of these are several derived structures, each generic or specialised as noted:

  • ModInt — modular integers ℤ/mℤ with automatic reduction (int).
  • Complex — generic complex numbers / Gaussian integers (complex).
  • GaloisField / GfElement — finite field extensions GF(pᵏ) (galois).
  • Poly — generic univariate polynomials (poly).
  • Matrix — dense matrices with exact determinant/inverse/solve (matrix).

The generic Poly/Matrix containers work over any Ring, an abstraction whose zero/one are taken relative to a sample element so that context-carrying rings (ModInt, GfElement) can supply identities in their own modulus/field.

  • Interval — outward-rounded interval arithmetic (interval).
  • Ball — midpoint–radius (mid-rad) rigorous arithmetic, Arb-style (ball).
  • Padic — fixed-precision p-adic numbers in ℚ_p (padic).
  • Quadratic / Algebraic — exact quadratic irrationals ℚ(√d) and general real algebraic numbers (algebraic).
  • EllipticCurve / Point — elliptic curves y² = x³ + a·x + b over GF(p) or , with the chord-and-tangent group law (elliptic).

Int/Rational also carry a number-theory toolkit (factorization, sqrt_mod, Jacobi/Legendre, CRT, random_prime, combinatorics, continued-fraction approximation), certificate-based primality proving (primality, the primality feature), and an optional num-traits bridge slots the types into generic numeric code.

puremp is usable as a Rust library, a C library (the ffi feature; see include/puremp.h), and a standalone command-line calculator (the cli feature; the puremp binary).

This is a clean-room implementation: it is MIT-licensed and its algorithms are drawn from the open literature (Knuth; Brent & Zimmermann’s Modern Computer Arithmetic; the HAC), never from GMP/MPFR source. See the README’s “Design & provenance” section for the algorithm references.

§Example

use puremp::{Int, Rational};

// Arbitrary-precision integers.
let big = Int::from(2).pow(128);
assert_eq!(big.to_string(), "340282366920938463463374607431768211456");
assert_eq!(Int::from(1071).gcd(&Int::from(462)).to_string(), "21");
assert_eq!(Int::from(2).modpow(&Int::from(10), &Int::from(1000)).to_string(), "24");

// Exact rationals, always in lowest terms.
let third = Rational::new(Int::from(1), Int::from(3));
let sum = &(&third + &third) + &third;
assert_eq!(sum.to_string(), "1");

§no_std

The crate is #![no_std] at its core. Arbitrary-precision types are heap-backed, so they need the alloc crate; the alloc feature (implied by every type layer) pulls it in. The std feature (enabled by default) adds the pieces that genuinely need the operating system — the CLI, std::error integration, and system I/O. Build with --no-default-features for a bare no_std target.

Re-exports§

pub use error::Error;
pub use error::Result;
pub use ring::Field;
pub use ring::Ring;
pub use ring::FiniteField;
pub use int::Int;
pub use int::Sign;
pub use nat::Nat;
pub use nat::Reciprocal;
pub use nat::u_gcd;
pub use nat::u64_gcd;
pub use random::RandomSource;
pub use random::SeedRng;
pub use inf_rational::InfRational;
pub use rational::Rational;
pub use mod_int::ModInt;
pub use dyadic::Dyadic;
pub use padic::Padic;
pub use decimal::Decimal;
pub use decimal::Rounding;
pub use complex::Complex;
pub use galois::GaloisField;
pub use galois::GfElement;
pub use poly::Poly;
pub use matrix::FieldMatrix;
pub use matrix::Matrix;
pub use matrix::RingMatrix;
pub use lattice::lll_reduce;
pub use lattice::lll_reduce_delta;
pub use identify::Identification;
pub use identify::identify;
pub use identify::identify_with;
pub use identify::machin_like;
pub use primality::Primality;
pub use primality::PrimalityCertificate;
pub use primality::prove_prime;
pub use algebraic::Algebraic;
pub use quadratic::Quadratic;
pub use elliptic::EllipticCurve;
pub use elliptic::Point;
pub use fixed_float::FixedFloat;
pub use float::Float;
pub use float::RoundingMode;
pub use ball::Ball;
pub use ball_solve::bisect_root;
pub use interval::Interval;

Modules§

algebraic
General real algebraic numbers.
ball
Ball (midpoint–radius) arithmetic — rigorous real arithmetic in the style of Arb.
ball_solve
Rigorous root solving on top of Ball arithmetic.
complex
Generic complex numbers re + im·i.
decimal
Arbitrary-precision base-10 floating point.
dyadic
Exact dyadic rationals — numbers of the form n · 2^-k, the rationals whose denominator is a power of two.
elliptic
Elliptic curves in short Weierstrass form y² = x³ + a·x + b.
error
Error and result types shared across the crate.
fixed_float
Fixed-precision floating point — a mpfx-style convenience wrapper over Float.
float
Arbitrary-precision binary floating-point numbers.
galois
Finite (Galois) field extensions GF(pᵏ).
identify
Experimental-mathematics helpers: an inverse symbolic calculator and Machin-like formula discovery, both built on the PSLQ integer-relation algorithm (crate::lattice::pslq).
inf_rational
Extended rationals with infinities — an exact Rational augmented with +∞, -∞, and NaN, following the usual IEEE-style arithmetic (1/0 = +∞, -1/0 = -∞, 0/0 = NaN, ∞ − ∞ = NaN, ∞ · 0 = NaN, …).
int
Arbitrary-precision signed integers with small-value inlining.
interval
Interval arithmetic with outward rounding (verified computing).
lattice
Lattice basis reduction.
matrix
Generic dense matrices Matrix<T>.
mod_int
Modular integers — residue classes ℤ/mℤ for a fixed modulus.
nat
Arbitrary-precision natural numbers (unsigned integers).
padic
Fixed-precision p-adic numbers — elements of the field ℚ_p (and its ring of integers ℤ_p), carried to a bounded number of significant p-adic digits.
poly
Generic dense univariate polynomials Poly<T>.
primality
Certificate-based primality proving — deterministic proofs, not just probable-prime tests.
quadratic
Quadratic irrationals — exact arithmetic in a field ℚ(√d).
random
Random-number generation for Nat and Int.
rational
Arbitrary-precision rational numbers (exact p/q fractions).
ring
The Ring abstraction: identities relative to a sample element.

Constants§

VERSION
The crate version string (CARGO_PKG_VERSION), exposed for the C ABI and CLI.

Traits§

FactorOverField
Factorization of a univariate polynomial over a finite field GF(q), by Cantor–Zassenhaus.