RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Exact and higher mathematics

Algebra and number theory

Permutation groups and character tables, Lie groups and algebras, Clifford algebras and spinors, quaternions and octonions, exact real algebraic numbers, primes, elliptic curves and modular forms.

import 'package:riptide_algebra/riptide_algebra.ray';
import 'package:riptide_algebra/number_theory.ray';
49 classes845 functions and methods613 of 1,044 documented643 runnable examples

Overview

Algebra. Exact arithmetic on Rational from riptide_precision. Everything is exported from package:riptide_algebra/riptide_algebra.ray.

Groups and characters

  • Permutation: images, cycles (1-based Permutation.parse('(1,2,3)(4,5)', n)), left-to-right product ((p*q)(x) = q(p(x)), as SymPy and GAP), inverse, powers, order, sign, cycle type, conjugation, commutators.
  • PermutationGroup: deterministic Schreier–Sims (Holt's algorithm) — base, strong generators and the stabiliser chain, exact BigInt order, membership by sifting, pointwise stabilisers, orbits, transitivity, uniform random elements, element enumeration, direct products, conjugacy classes (small groups).
  • NamedGroups: cyclic, dihedral, symmetric, alternating, Klein four, quaternion Q8, dicyclic and generalised quaternion, elementary abelian, PSL(2, p) on the projective line, Mathieu M11 and M12.
  • FpGroup: finitely presented groups (FpGroup.parse('a,b', ['a^2', 'b^3', 'a*b*a*b*a*b*a*b']), capitals are inverses) and Todd–Coxeter coset enumeration (HLT with coincidences): group order, subgroup index, the permutation action on cosets.
  • CharacterTable: Burnside–Dixon over F_{p} (p \equiv 1 mod the exponent, p > 2\sqrt{|G|}) with each value lifted to an exact Cyclotomic; power maps, inner products, decomposition of class functions, tensor products, Frobenius–Schur indicators, both orthogonality relations.
  • Cyclotomic: exact elements of Q(\zeta_{n}) reduced modulo \Phi_{n} (cyclotomicPolynomial), field operations, Galois automorphisms, conjugation, inverse via the norm, Cyclotomic.sqrt(d) by Gauss sums.

Verified by tests: group axioms on random permutations, orders (S_{20} = 20!, A_{7}, PSL(2,7) = 168, PSL(2,11) = 660, M_{11} = 7920, M_{12} = 95040), M12 sharply 5-transitive, Todd–Coxeter orders of Q8, D6, the (2,3,4) and (2,3,5) triangle groups, the Coxeter presentation of S5, class counts of S_{n} equal the partition numbers, exact tables of S3, S4, S5, A_{4} (\omega), A5 (golden ratio), Q8 vs D4 (Frobenius–Schur -1 vs +1), C5, PSL(2,7) ((−1 ± √−7)/2), M11 — every table passes both orthogonality relations exactly and \sum \chi(1)^{2} = |G|.

Lie theory

  • RootSystem for A_{n}, B_{n}, C_{n}, D_{n}, E6–E8, F4, G2 (RootSystem.parse('E8')): Cartan matrix (Humphreys, a_{ij} = \langle\alpha_{i}, \alpha_{j}\wedge \vee \rangle), root lengths, bilinear forms on roots and weights, positive roots (simple-root coordinates and Dynkin labels), highest root, Weyl group (simple reflections, words, orbits, dominant conjugates, exponents, degrees, exact order, element enumeration with reduced words, lengths, the longest element, duals -w_{0}\lambda), Coxeter and dual Coxeter numbers.
  • Irrep (highest weight in Dynkin labels): Weyl dimension, dominant weights and Freudenthal multiplicities, full weight diagram, quadratic Casimir (raw and physics normalisation), Dynkin index, dual. tensorProduct (Racah–Speiser / Klimyk). dynkinToYoung / youngToDynkin for su(n).
  • Exact clebschGordan, wigner3j, wigner6j, wigner9j, racahW by the Racah formulas, returned as SqrtRational (sign · √rational); SurdSum for exact sums of square roots (orthogonality checks). Numeric wignerSmallD, wignerD, wignerDMatrix (z-y-z, Condon–Shortley).
  • Matrix Lie algebras on CMatrix (double complex; expm from riptide_numerics_cpu): pauli, gellMann, so3Generators, soGenerators(p, q), lorentzGenerators; MatrixLieAlgebra (closure, structure constants, physics f_{\mathrm{abc}}, adjoint, Killing form, Jacobi defect). Groups: su2Rotation, so3Rotation, su3Exp, lorentzBoost, sl2cBoost; the covers su2ToSo3 and sl2cToLorentz.

Verified: 150 3j, 192 CG, 120 6j and 40 9j values equal SymPy's exactly; exact CG orthonormality, 3j symmetries, 6j orthogonality, Biedenharn–Elliott spot checks and the 9j \to 6j reduction (all in SurdSum, no rounding); D-matrix unitarity and composition. Root systems: \dim g, |\Phi^{+}|, |W| (E8: 696729600), h and h∨ for 13 types; enumerated Weyl groups match \prod d_{i} and the Poincaré polynomial; Freudenthal multiplicities sum to the Weyl dimension (E8 adjoint: zero weight ×8); tensor products (su(3) 8 \otimes 8, G2 7 \otimes 7, E6 27 \otimes 27̄); adjoint Casimir = h∨. Matrix algebras: \operatorname{su}(3) f_{\mathrm{abc}} table, total antisymmetry, Jacobi identity, Killing form -3\delta for \frac{i\lambda}{2} and its ad-invariance (so(3,1), so(5), su(3)), Lorentz relations, exponentials land in SU(3) / SO⁺(3,1), covering maps are two-to-one homomorphisms.

Showcases: SU(3) weight diagrams (3, 3̄, 8, 10, 27 with multiplicities; 3 \otimes 3̄ = 8 \oplus 1, 3 \otimes 3 \otimes 3 = 10 \oplus 8 \oplus 8 \oplus 1; Casimirs) and the spinor 720^{\circ} property (-1 at 2\pi, +1 at 4\pi, D^{j}(2\pi) = (-1)^{2j}).

Clifford algebras, spinors and gamma matrices

  • CliffordAlgebra \operatorname{Cl}(p, q, r) for any metric (\pm1, 0; up to 12 generators) and Multivector with exact Rational coefficients: geometric, outer, left/right contractions, scalar and Hestenes inner products, commutator product, reversion, grade involution, Clifford conjugate, grade projections, versor and general inverses, duals, sandwich (rotors). Matrix representations: the faithful left-regular one (also degenerate algebras) and the irreducible complex spinor representation of size 2\wedge\lfloor\frac{n}{2}\rfloor (Brauer–Weyl), exact over Q(i) (GaussianRational, QIMatrix).
  • GammaMatrices for Cl(1,3) in the Dirac, Weyl (chiral) and Majorana bases, mostly-minus and mostly-plus: \gamma^{\mu}, \gamma_{\mu}, \gamma^{5}, \sigma^{\mu \nu}, Lorentz generators, chiral projectors, the 16-element Clifford basis, charge conjugation C, basis changes (intertwiner, Schur averaging, exact), Feynman slash, the trace recursion, Dirac adjoint, the five bilinear covariants, charge conjugate spinors, spinor boosts and rotations.

Verified exactly: \{\gamma^{\mu}, \gamma^{\nu}\} = 2\eta^{\mu \nu} in all six basis/signature combinations; \gamma^{5} Hermitian, squares to 1, anticommutes; Majorana gammas imaginary (mostly-minus) and real (mostly-plus); C \gamma^{\mu} C^{-1} = -\gamma^{\mu T}; basis changes; [S^{\mu \nu}, \gamma^{\rho}] relations; matrix traces = trace recursion on random index strings; \operatorname{Tr} \gamma^{5}\gamma^{0}\gamma^{1}\gamma^{2}\gamma^{3} = -4i; Fierz completeness. Numerically: S^{-1}\gamma^{\mu}S = \Lambda^{\mu}_{\nu} \gamma^{\nu} for boosts and rotations; 2\pi rotation = -1 on Dirac spinors; the Fierz identities J^{2} = \sigma^{2} + \omega^{2} = -K^{2}, J \cdot K = 0; \operatorname{Cl}(p, q) associativity, involution laws, representation homomorphisms; \operatorname{Spin}(p, q) generators have the \operatorname{so}(p, q) structure constants.

Quaternions and octonions

  • Quaternion (double, raya:value): Hamilton product, conjugate, norm, inverse, exp/log, slerp, rotation by conjugation, rotation matrix, and the map to SU(2) (toSu2), consistent with su2Rotation and so3Rotation.
  • CayleyDickson (exact): quaternions, octonions, sedenions and beyond; associator, commutator, norm, inverse; octonionTable and the Fano-plane fanoLines.

Verified: Hamilton relations; quaternion associativity; octonion alternativity, flexibility, all four Moufang identities and |xy| = |x||y| on random exact elements; non-associativity; the seven Fano lines each give a quaternion subalgebra and every pair of units lies on exactly one; sedenion zero divisors found by search, norm not multiplicative, power-associativity kept.

Showcase: the Clifford relations in three bases, the QED trace theorems, and e^{+}e^{-} \to \mu^{+}\mu^{-} — the spin-averaged |M|^{2} by traces and by explicit Dirac spinors over all 16 spin combinations, both equal to 8[(p \cdot k)(p' \cdot k') + (p \cdot k')(p' \cdot k) + m_{\mu}^{2}(p \cdot p')] to 1e-9.

Algebraic numbers

Exact real algebraic numbers, so that geometry and trigonometry never fall back on decimals.

  • AlgebraicNumber: the minimal polynomial over Q (irreducible, primitive, integer coefficients) plus an open isolating interval with Rational endpoints, e.g. \sqrt{2} = (x^{2} - 2, (1, 2)). Constructors rational, fromInt, sqrt, root(r, k), fromSqrtRational, rootInInterval(p, lo, hi), realRoots(p), goldenRatio, and exact trigonometric values cosPi(r) / sinPi(r) (cos and sin of r\pi for rational r), cos2PiOver(n), sin2PiOver(n) with twoCosMinimalPolynomial(n) (\Psi_{n} from \Phi_{n}(x) = x^{\varphi(n)/2} \Psi_{n}(x + \frac{1}{x})).
  • Arithmetic: + − × ÷ by resultants (\operatorname{Res}_{y}(p(y), q(x - y)) for sums, \operatorname{Res}_{y}(p(y), y^{n}q(\frac{x}{y})) for products, built by interpolation of univariate subresultant resultants), then factorisation over Q and the root picked out by an interval enclosure; rational operands take exact shortcuts (shift, scale, reversal). pow(k) (any integer), nthRoot(k), powRational, sqrt, reciprocal, abs.
  • Exact comparison and sign (refine until the intervals separate; equality by the minimal polynomial), floor, ceil, round, scaledFloor(places) (exact decimal digits), toDecimalString, toBigFloat(prec, rnd) (correctly rounded in every mode), toDouble (correctly rounded). Refinement is bisection plus Newton steps verified by a sign change, so 1000 digits take milliseconds.
  • toString shows the minimal polynomial and interval; toLatex gives rationals, quadratic surds (\frac{1 + \sqrt{5}}{2}, also as quadraticSurd), real radicals \sqrt[k]{r}, otherwise the root of the minimal polynomial.
  • QPoly (in its own file, so riptide_cas can replace it): dense univariate polynomials over Q — division, gcd and extended gcd, Yun square-free decomposition, the subresultant resultant (resultantZ, Cohen 3.3.7), discriminant, Sturm sequences, root counting and isolation, and factorisation over Q (factorSquareFreeZ: Zassenhaus with Cantor–Zassenhaus modulo a small prime, linear Hensel lifting past the Landau–Mignotte bound, subset recombination).

Verified against SymPy 1.14 / mpmath 1.3: 73 minimal polynomials (\sqrt{2} \cdot \sqrt{3} = \sqrt{6}, \sqrt{2} + \sqrt{3} \mapsto x^{4} - 10x^{2} + 1, 2\cos(\frac{2\pi}{7}) \mapsto x^{3} + x^{2} - 2x - 1, \sqrt[3]{2} + \sqrt{3}, \sqrt{2} + \sqrt{3} + \sqrt{5}, \sqrt{1 + \sqrt{2}}, (\sqrt{2} + \sqrt{3})^{2/3}, the real root of x^{5} - x - 1 plus \sqrt{2}, cos and sin of \frac{2\pi}{n} for n \le 24), \Psi_{n} for n \le 40, 1000 decimal digits of nine numbers, nsimplify forms of ten quadratic surds, real_roots of seven polynomials to 30 digits, near-equal comparisons (\sqrt{2} vs \frac{665857}{470832}, sums of two square roots that agree to 6 digits), factor_list of 20 polynomials (including x^{8} - 40x^{6} + 352x^{4} - 960x^{2} + 576, irreducible but split modulo every prime) and resultants and discriminants of 20 random pairs (against the Sylvester determinant: sympy.resultant gets the sign wrong for some negative leading coefficients). Also: the golden-ratio identities, field axioms, \sin^{2} + \cos^{2} = 1 exactly, cos/sin(kπ/n) against raya:math for n \le 30, BigFloat rounding in all modes against BigFloat.sqrt, x^{n} - 1 = \prod \Phi_{d} for n \le 36, the resultant against an independent Euclidean one.

Limits: real numbers only (no complex algebraic numbers yet); recombination in the factoriser is exponential in the number of modular factors (fine up to degree ~30, not for Swinnerton-Dyer polynomials of degree 64+; no LLL); every operation on two irrationals builds a resultant of degree m \cdot n, so towers of many nested operations get slow — reduce with exact shortcuts where possible.

Rational trigonometry

Why. We want geometry that never depends on representations that don't terminate: no \pi, no decimal approximations of \sqrt{2}, no floating-point angles. Points, lines, quadrances and spreads are exact field elements, so two computations of the same configuration compare equal, and the result is the same on every machine and in any order. The self-organising modular-systems research will consume this as exact connector geometry. For that reason the API below (RtPoint, RtLine, RtPlane.quadrance, RtPlane.spread, RtMeasures) is meant to be a stable contract for downstream packages. It is generic over the field, plain immutable values, and has no hidden state.

  • ScalarField<T>: the field interface. Implementations are RationalField (Q), PrimeField(p) (GF(p) for an odd prime p < 2^{31}, elements as int) and AlgebraicField (AlgebraicNumber, closed under real square roots).
  • RtPoint<T> [x, y] and RtLine<T> \langle a:b:c\rangle (ax + by + c = 0) are immutable values with ==. They are not @pragma('raya:value'): a value class can't be generic and holds only primitive fields.
  • RtPlane<T>(field), with the ready-made RtPlane.rational, RtPlane.algebraic and RtPlane.finite(p):
    • Measures: quadrance, spread, cross (= 1 - s), twist (= \frac{s}{c});
    • Incidence: join, meet, liesOn, sameLine, isParallel, isPerpendicular, isCollinear, isConcurrent;
    • Constructions: parallelThrough, perpendicularThrough (altitude), foot, midpoint, reflect;
    • Null lines: isNull, nullLinesThrough (over GF(p), these exist when -1 is a square).
  • Triangles: measures(a1, a2, a3) returns RtMeasures (Q_{i} opposite A_{i}, s_{i} at A_{i}). archimedes is A(Q_{1}, Q_{2}, Q_{3}) = 16 \cdot \mathrm{area}^{2}; archimedesOfPoints computes the same from coordinates.
  • The five main laws, each as a check and as a solver. Solvers that need a square root return only the solutions that exist in the field.
    • Triple quad: tripleQuadHolds / solveTripleQuad.
    • Pythagoras: pythagorasHolds / pythagorasHypotenuse / pythagorasLeg.
    • Spread law: spreadLawHolds / spreadLawSpread / spreadLawQuadrance.
    • Cross law: crossLawHolds / crossLawSpread / crossLawQuadrance.
    • Triple spread: tripleSpreadHolds / solveTripleSpread.
    • satisfiesLaws(m) checks all of them at once.
  • solveTriangle(q1, q2, q3, s1, s2, s3:) takes any three measures, at least one of them a quadrance, and returns every non-degenerate solution. The ambiguous cases give two.
  • spreadPolynomial(n) returns S_{n} as a QPoly, with s(n\theta) = S_{n}(s(\theta)). RtPlane.spreadPolynomialValue(n, s) evaluates it in any field.
  • RtClassical converts to and from classical measures:
    • distance (\sqrt{Q}, exact AlgebraicNumber) and quadrance;
    • angle (asin \sqrt{s}, double) and angleBigFloat (exact \sqrt{s}, then a correctly rounded asin);
    • angleAsPiMultiple, exact for the rational spreads 0, \frac{1}{4}, \frac{1}{2}, \frac{3}{4}, 1 (by Niven's theorem these are the only ones);
    • spreadOfAngle and spreadOfPiMultiple (\sin^{2}(r\pi), exact).

What the tests check:

  • The right isosceles triangle has quadrances 1, 1, 2 and spreads \frac{1}{2}, \frac{1}{2}, 1.
  • On 200 random rational triangles, every law holds exactly. A = 4Q_{1}Q_{2}s_{3} is computed from coordinates. Pythagoras holds exactly when s_{3} = 1, and the triple quad formula holds exactly for collinear points. The orthocentre test: the feet of the altitudes lie on the sides and the altitudes are concurrent.
  • solveTriangle recovers random triangles from six patterns of three measures, over Q, and over the algebraic numbers whenever the solution needs a square root. Given Q_{1} = Q_{2} = 1 and s_{3} = \frac{3}{4} it returns both solutions.
  • Regular triangle, square, hexagon and octagon, built from the exact cos and sin over the algebraic numbers:
    • The interior spreads are \frac{3}{4}, 1, \frac{3}{4} and \frac{1}{2};
    • The side quadrances are 2 - 2\cos(\frac{2\pi}{n});
    • The spread from side 0 to side k equals S_{k}(\sin^{2}(\frac{2\pi}{n})) for every k.
  • S_{n} \circ S_{m} = S_{nm}.
  • Over GF(7), GF(11), GF(13), GF(101) and GF(1009), random triangles satisfy the laws and solve back from their quadrances.
  • Null lines exist exactly when p \equiv 1 (mod 4). They are self-perpendicular and have quadrance 0 along them.
  • Round trips against raya:math (atan2 angles, distances) agree to 1e-9. Asin \sqrt{\frac{3}{4}} = \frac{\pi}{3} holds to 200 bits.

Number theory

Also importable alone as package:riptide_algebra/number_theory.ray. BigInt throughout, with int conveniences (...Int) for 64-bit values.

  • Modular arithmetic: egcd, modInverse, crt (arbitrary, non-coprime moduli; null when inconsistent), modPow / modPowInt / mulModInt (exact for every 63-bit modulus), square roots mod p (sqrtModPrime Tonelli–Shanks, sqrtModPrimeCipolla), \bmod p^{k} (sqrtModPrimePower, Hensel) and mod any n (sqrtMod, all roots, as SymPy sqrt_mod(all_roots=True)), discreteLog (Pohlig–Hellman + baby-step giant-step), multiplicativeOrder, carmichaelLambda, primitiveRoot (smallest) / isPrimitiveRoot, totient.
  • Symbols: legendreSymbol, jacobiSymbol, kroneckerSymbol.
  • Primality: isPrime(int) — deterministic Miller–Rabin with bases 2…37 (proven below 3.3 \cdot 10^{24}, so for every int; products via BigInt above 2^{31.5}); isProbablePrime(BigInt) — Baillie–PSW (strong base 2 + strong Lucas, Selfridge method A) above 2^{63}, exact below; isStrongLucasProbablePrime; nextPrime / prevPrime (int and BigInt).
  • Factorisation: trialDivision, pollardRhoBrent, ecmFactor (Montgomery curves, Suyama, x-only ladder, stage 1), perfectPower, factorint / factorintInt (sorted prime → exponent), divisors.
  • Sieves: primesUpTo (odd-only Eratosthenes), primesInRange (segmented), countPrimesInRange, primePi (Lucy Hedgehog, O(n^{3/4}); \pi(10^{10}) in well under a second), isqrt.
  • Arithmetic functions: mobius, divisorSigma (\sigma_{k}), numDivisors (\tau), divisorSigmaTable (sieve).
  • Continued fractions: of Rationals (continuedFraction, fromContinuedFraction, convergents), of quadratic irrationals (p + √d)/q (quadraticContinuedFraction, sqrtContinuedFraction → preperiod + period), Pell x² − dy² = 1 (pellFundamental) and = −1 (negativePellFundamental).
  • Elliptic curves: EllipticCurve over Q in general Weierstrass form [a1, a2, a3, a4, a6] with exact Rational arithmetic — b2…b8, c4, c6, \Delta, j, group law (add, double, negate, multiply), integralModel, reduce(p), ap(p), anList(n), torsionPoints / torsionStructure (Nagell–Lutz on Y² = X³ − 27c4X − 54c6 + Mazur), torsionBound (gcd of #E(F_{p})), conductor (semistable curves only), isMinimalAt (sufficient test). EllipticCurveFp over F_{p}: group law, countPoints (character sum, O(p)), orderOf (BSGS in the Hasse interval), groupOrder (lcm of point orders, Mestre-style), randomPoint, jInvariant.
  • Modular forms on q-expansions: QSeries (BigInt numerators over one common denominator, truncated); bernoulli (exact), eisenstein(k), delta, ramanujanTau / ramanujanTauList, eulerProduct, etaProduct({m: r_m}), thetaZ / thetaZn, latticeTheta(gram) (Fincke–Pohst), thetaE8, e8CartanMatrix, hecke(f, n, k) / heckePrime.
  • L-functions: DirichletGroup(n).characters (all \varphi(n) characters from the cyclic decomposition of (\frac{Z}{n})^×) and DirichletCharacter (exact RootOfUnity values, numeric value, order, parity, conductor, primitive, products, conjugate, gaussSum, DirichletCharacter.kronecker(d)), lValue(s) for real s (Hurwitz \zeta via Euler–Maclaurin; digamma at s = 1), hurwitzZeta, riemannZeta, zetaEvenCoefficient (\frac{\zeta(2k)}{\pi^{2k}} exact), digamma, gamma, exponentialIntegralE1; EllipticLSeries (root number found numerically from the functional equation, valueAt1, derivativeAt1 for odd rank, partialEulerProduct). Complex numbers are NtComplex.

Elliptic curves: a_{p} of 11a_{1} and 37a_{1} for p < 100 hard-coded from the LMFDB, a_{p} of 11a_{1}, 14a_{1}, 15a_{1}, 37a_{1}, 389a_{1} for p < 230 against brute-force counts (bad primes give \pm1 / 0), the newform \eta(\tau)^{2}\eta(11\tau)^{2} equals the a_{n} of 11a_{1} (n < 300), Hasse bound for six random curves at every p < 3000 with BSGS = counting, associativity and scalar-multiplication laws on random points mod p (p up to 998244353) and on multiples of the 37a_{1} generator (0, 0) over Q, torsion \frac{Z}{5} (11a_{1}), trivial (37a_{1}), \frac{Z}{6}, \frac{Z}{2} \times \frac{Z}{4}, \frac{Z}{2} \times \frac{Z}{2}, \frac{Z}{2} \times \frac{Z}{8}, \frac{Z}{7}, conductors 11, 37, 14, 15,

  1. Modular forms: E_{4}^{3} - E_{6}^{2} = 1728\Delta, E_{4}^{2} = E_{8}, E_{4}E_{6} = E_{10}, 441 \times 10^{43} + 250 \times 10^{62} = 691 \times 10^{12}, \Theta_{E8} = E_{4} (two constructions), Jacobi's four-square and the eight-square formula, T_{n} \Delta = \tau(n)\Delta (n \le 13), T_{p} E_{4} = \sigma3(p)E_{4}, \tau multiplicative, \tau(p^{2}) = \tau(p)^{2} - p^{11}, \tau \equiv \sigma11 \bmod 691, Deligne's bound. L-functions: L(1, \chi_{-4}) = \frac{\pi}{4}, L(2, \chi_{-4}) = Catalan, the class number formula (d = -3, -23, -84, 5), a complex character mod 5, character orthogonality, the count of primitive characters, |\tau(\chi)|^{2} = conductor, \zeta(2k) = rational·\pi^{2k}, L(11a_{1}, 1) = 0.25384186085591068 (w = +1), L'(37a_{1}, 1) = 0.30599977383405230 (w = -1), L(389a_{1}, 1) = 0 (rank 2), a wrong conductor is rejected by the functional equation.

Limits: conductors only for semistable curves with a model provably minimal at each bad prime (no Tate algorithm, no minimal-model reduction); L(E, s) only at s = 1 (and L'(E, 1) only for w = -1); Nagell–Lutz locates integer roots in double precision (|X| of torsion points below ~10^{14}); Dirichlet L-values for real s in double precision; Hecke operators assume level 1 (or p \nmid N with trivial character); naive point counting below p = 2^{40}; int functions assume the Raya VM (64-bit ints), not the web.

Next: Tate's algorithm and minimal models (all conductors, Kodaira symbols, Tamagawa numbers), L(E, s) for any s (incomplete gamma) and higher derivatives, Schoof / SEA point counting, canonical heights and the BSD ratio, modular symbols (newforms of level N), the quadratic sieve, complex s for Dirichlet L-functions.

Also exported here

Functions and constants

Functions

resultantZfunction
BigInt resultantZ(List<BigInt> a0, List<BigInt> b0)

The resultant of two integer polynomials by the subresultant algorithm (Cohen, Algorithm 3.3.7).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Res(x² − 2, x² − 3) over ℤ: nonzero, so no common root.
  final a = [-2, 0, 1].map(BigInt.from).toList(), b = [-3, 0, 1].map(BigInt.from).toList();
  print('Res = ${resultantZ(a, b)}');
  print('Res(x² − 1, x − 1) = ${resultantZ([-1, 0, 1].map(BigInt.from).toList(), [-1, 1].map(BigInt.from).toList())}');
}
Output
Res = 1
Res(x² − 1, x − 1) = 0
List<List<BigInt>> factorSquareFreeZ(List<BigInt> f0)

The irreducible factors over Z of a square-free primitive integer polynomial of degree ≥ 1 (Zassenhaus). Each factor is primitive with a positive leading coefficient. The recombination step tries subsets, so it is exponential in the number of modular factors; fine for the modest degrees of algebraic-number arithmetic.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // x⁴ + 4 = (x² − 2x + 2)(x² + 2x + 2) (Sophie Germain).
  print(factorSquareFreeZ([4, 0, 0, 0, 1].map(BigInt.from).toList()));
}
Output
[[2, 2, 1], [2, -2, 1]]
intertwinerfunction
QIMatrix intertwiner(List<QIMatrix> a, List<QIMatrix> b)

The matrix S with S A_{k} = B_{k} S for two irreducible sets of anticommuting generators satisfying the same Clifford relations (Schur's lemma: S = \sum_{\Gamma} \Gamma_{B} M \Gamma_{A}^{-1} over the products of generators, for any M making it nonzero).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // The matrix taking the Dirac basis of γ matrices to the Weyl basis: S γ_D S⁻¹ = γ_W.
  final d = GammaMatrices(), w = GammaMatrices(basis: GammaBasis.weyl);
  final s = intertwiner(d.upper, w.upper);
  print('S γ⁰ S⁻¹ == γ⁰_Weyl: ${s * d.upper[0] * s.inverse == w.upper[0]}');
}
Output
S γ⁰ S⁻¹ == γ⁰_Weyl: true
List<int> cyclotomicPolynomial(int n)

The integer coefficients of the n-th cyclotomic polynomial \Phi_{n}, constant term first (\Phi_{n} is monic).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [1, 2, 3, 4, 6, 12]) {
    print('Φ$n coefficients: ${cyclotomicPolynomial(n)}');
  }
  print('degree of Φ105 = ${cyclotomicDegree(105)}');
}
Output
Φ1 coefficients: [-1, 1]
Φ2 coefficients: [1, 1]
Φ3 coefficients: [1, 1, 1]
Φ4 coefficients: [1, 0, 1]
Φ6 coefficients: [1, -1, 1]
Φ12 coefficients: [1, 0, -1, 0, 1]
degree of Φ105 = 48
int cyclotomicDegree(int n)

Exposed for tests: \varphi(n) via the degree of \Phi_{n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print([for (var n = 1; n <= 12; n++) cyclotomicDegree(n)]);
}
Output
[1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4]
QPoly spreadPolynomial(int n)

The spread polynomial S_{n}(s) over Q (integer coefficients): S_{0} = 0, S_{1} = s, S_{2} = 4s - 4s^{2}, S_{3} = s(3 - 4s)^{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (var n = 1; n <= 4; n++) {
    print('S$n(s) = ${spreadPolynomial(n)}');
  }
}
Output
S1(s) = x
S2(s) = -4*x^2 + 4*x
S3(s) = 16*x^3 - 24*x^2 + 9*x
S4(s) = -64*x^4 + 128*x^3 - 80*x^2 + 16*x
soGeneratorsfunction
List<CMatrix> soGenerators(int p, [int q = 0])

The basis M_{ab} (a < b) of \operatorname{so}(p, q) = \{X: X^{T}\eta + \eta X = 0\}, \eta = \operatorname{diag}(+1 \times p, -1 \times q).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // so(3) has 3 generators, so(3,1) (the Lorentz algebra) has 6.
  print('so(3): ${soGenerators(3).length}, so(3,1): ${soGenerators(3, 1).length}, so(4): ${soGenerators(4).length}');
  print('closed under brackets: ${MatrixLieAlgebra(soGenerators(3, 1)).jacobiDefect < 1e-12}');
}
Output
so(3): 3, so(3,1): 6, so(4): 6
closed under brackets: true
su2Rotationfunction
CMatrix su2Rotation(double theta, List<double> axis)

U = \exp(-i \theta n \cdot \frac{\sigma}{2}) = \cos(\frac{\theta}{2}) I - i \sin(\frac{\theta}{2}) n \cdot \sigma ∈ SU(2): the spinor rotation by \theta about n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // A full turn of a spinor is −1; two full turns are +1.
  final u = su2Rotation(2 * 3.141592653589793, [0, 0, 1]);
  print('U(2π) ≈ −I: ${u.approxEquals(CMatrix.identity(2).scaleReal(-1), 1e-12)}');
}
Output
U(2π) ≈ −I: true
so3Rotationfunction
CMatrix so3Rotation(double theta, List<double> axis)

The rotation by \theta about n in SO(3) (Rodrigues): R = I + \sin \theta K + (1 - \cos \theta) K^{2}, K = n \cdot L.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final r = so3Rotation(3.141592653589793 / 2, [0, 0, 1]);
  print(r);
}
Output
1.1102230246251565e-16+0.0i -1.0+0.0i 0.0+0.0i
1.0+0.0i 1.1102230246251565e-16+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i 1.0+0.0i
su2ToSo3function
CMatrix su2ToSo3(CMatrix u)

The double cover SU(2) → SO(3): R_{ij} = \tfrac{1}{2} \operatorname{Tr}(\sigma_{i} U \sigma_{j} U^{\dagger}). U and -U give the same rotation.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // U and −U give the same rotation: the double cover.
  final u = su2Rotation(1.0, [1, 0, 0]);
  print('same rotation: ${su2ToSo3(u).approxEquals(su2ToSo3(u.scaleReal(-1)), 1e-12)}');
  print('matches so3Rotation: ${su2ToSo3(u).approxEquals(so3Rotation(1.0, [1, 0, 0]), 1e-12)}');
}
Output
same rotation: true
matches so3Rotation: true
CMatrix sl2cToLorentz(CMatrix a)

The covering map SL(2,C) → SO⁺(3,1): \Lambda_{\mu \nu} = \tfrac{1}{2} \operatorname{Tr}(\sigma_{\mu} A \sigma_{\nu} A^{\dagger}) with \sigma_{\mu} = (I, \sigma), from A X A^{\dagger} for X = x^{\mu} \sigma_{\mu} (\det X = t^{2} - |x|^{2} is preserved).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final l = sl2cToLorentz(sl2cBoost(0.5, [0, 0, 1]));
  print('matches lorentzBoost: ${l.approxEquals(lorentzBoost(0.5, [0, 0, 1]), 1e-12)}');
}
Output
matches lorentzBoost: true
sl2cBoostfunction
CMatrix sl2cBoost(double rapidity, List<double> axis)

The SL(2,C) element of a boost with rapidity \eta along n: \exp(\eta n \cdot \frac{\sigma}{2}) (acting on right-handed Weyl spinors).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(sl2cBoost(1.0, [1, 0, 0]));
}
Output
1.1276259652063807+0.0i 0.5210953054937474+0.0i
0.5210953054937474+0.0i 1.1276259652063807+0.0i
lorentzBoostfunction
CMatrix lorentzBoost(double rapidity, List<double> axis)

The Lorentz boost with rapidity \eta along n: \exp(\eta n \cdot K).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Rapidity η: γ = cosh η, so η = ln 2 gives γ = 5/4 and v = 3/5.
  final l = lorentzBoost(0.6931471805599453, [1, 0, 0]);
  print('γ = ${l.at(0, 0).re}, γv = ${l.at(0, 1).re}');
}
Output
γ = 1.25, γv = 0.75
su3Expfunction
CMatrix su3Exp(List<double> theta)

\exp(i \sum \theta_{a} \lambda_{a} / 2) ∈ SU(3).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final u = su3Exp([0, 0, 0, 0, 0, 0, 0, 1]);
  print('unitary: ${(u * u.dagger).approxEquals(CMatrix.identity(3), 1e-12)}');
}
Output
unitary: true
BigInt weylDimension(RootSystem rs, Weight lambda)

Weyl dimension formula for V(\lambda).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // su(3) irreps by Dynkin labels: (1,0) = 3, (1,1) = 8, (3,0) = 10.
  final a2 = RootSystem.a(2);
  for (final l in [[1, 0], [1, 1], [3, 0], [2, 2]]) print('$l: ${weylDimension(a2, Weight(l))}');
  print('E8 adjoint: ${weylDimension(RootSystem.e(8), RootSystem.e(8).highestRoot)}');
}
Output
[1, 0]: 3
[1, 1]: 8
[3, 0]: 10
[2, 2]: 27
E8 adjoint: 248
Map<Weight, int> tensorProduct(RootSystem rs, Weight lambda, Weight mu)

V(\lambda) \otimes V(\mu) = \oplus N_{\nu} V(\nu), by the Racah–Speiser (Klimyk) algorithm: \sum over weights \nu of V(\mu) of m_{\mu}(\nu) \cdot \operatorname{sign}(w) V(w(\lambda + \nu + \rho) - \rho). Returns highest weight → multiplicity.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 3 ⊗ 3̄ = 8 ⊕ 1 and 3 ⊗ 3 = 6 ⊕ 3̄ in su(3).
  final a2 = RootSystem.a(2);
  print('3 ⊗ 3̄: ${tensorProduct(a2, Weight([1, 0]), Weight([0, 1]))}');
  print('3 ⊗ 3:  ${tensorProduct(a2, Weight([1, 0]), Weight([1, 0]))}');
}
Output
3 ⊗ 3̄: {(1,1): 1, (0,0): 1}
3 ⊗ 3:  {(2,0): 1, (0,1): 1}
List<int> dynkinToYoung(Weight lambda)

Dynkin labels of \operatorname{su}(n) (A_{n - 1}) → the Young diagram's row lengths (r_{i} = \sum_{j \ge i} \lambda_{j}), trailing zeros dropped.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('su(3) (2,1) → rows ${dynkinToYoung(Weight([2, 1]))}');
}
Output
su(3) (2,1) → rows [3, 1]
Weight youngToDynkin(List<int> rows, int n)

A Young diagram (row lengths, weakly decreasing) for su(n) → Dynkin labels of A_{n - 1}. Full columns of height n are dropped (they are the determinant, trivial for SU(n)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('rows [3, 1] in su(4) → ${youngToDynkin([3, 1], 4)}');
}
Output
rows [3, 1] in su(4) → (2,1,0)
halffunction
Rational half(int twice)

\frac{k}{2} as a Rational (half-integers: half(1) = \frac{1}{2}, half(3) = \frac{3}{2}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('half(1) = ${half(1)}, half(3) = ${half(3)}, half(4) = ${half(4)}');
}
Output
half(1) = 1/2, half(3) = 3/2, half(4) = 2
wigner3jfunction
SqrtRational wigner3j(Rational j1, Rational j2, Rational j3, Rational m1, Rational m2, Rational m3)

The Wigner 3j symbol (j_{1} j_{2} j_{3}; m_{1} m_{2} m_{3}), exact.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final w = wigner3j(half(2), half(2), half(2), Rational.zero, Rational.zero, Rational.zero);
  print('(1 1 1; 0 0 0) = $w');
  print('(1 1 2; 1 −1 0) = ${wigner3j(Rational.one, Rational.one, Rational.fromInt(2), Rational.one, -Rational.one, Rational.zero)}');
}
Output
(1 1 1; 0 0 0) = 0
(1 1 2; 1 −1 0) = √(1/30)
SqrtRational wigner3jTwice(int j1, int j2, int j3, int m1, int m2, int m3)

wigner3j with every argument doubled (so half-integers are odd ints).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // (1 1 0; 0 0 0) with integer spins, given as twice their values.
  print('(1 1 0; 0 0 0) = ${wigner3jTwice(2, 2, 0, 0, 0, 0)}');
  print('(1 1 2; 1 -1 0) = ${wigner3jTwice(2, 2, 4, 2, -2, 0)}');
}
Output
(1 1 0; 0 0 0) = -√(1/3)
(1 1 2; 1 -1 0) = √(1/30)
SqrtRational clebschGordan(Rational j1, Rational m1, Rational j2, Rational m2, Rational j, Rational m)

The Clebsch–Gordan coefficient \langle j_{1} m_{1}; j_{2} m_{2} \mid j m\rangle (Condon–Shortley phase), exact. Note the bra-ket argument order (SymPy's clebsch_gordan takes j1, j2, j, m1, m2, m).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Two spin-1/2: ⟨½ ½; ½ −½ | 1 0⟩ = √(1/2).
  print('${clebschGordan(half(1), half(1), half(1), half(-1), Rational.one, Rational.zero)}');
}
Output
√(1/2)
SqrtRational clebschGordanTwice(int j1, int m1, int j2, int m2, int j, int m)

clebschGordan with doubled arguments.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Doubled arguments: spin 1/2 is 1.
  print('⟨½ ½; ½ −½ | 0 0⟩ = ${clebschGordanTwice(1, 1, 1, -1, 0, 0)}');
}
Output
⟨½ ½; ½ −½ | 0 0⟩ = √(1/2)
wigner6jfunction
SqrtRational wigner6j(Rational j1, Rational j2, Rational j3, Rational j4, Rational j5, Rational j6)

The Wigner 6j symbol \{j_{1} j_{2} j_{3}; j_{4} j_{5} j_{6}\}, exact.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('{1 1 1; 1 1 1} = ${wigner6j(Rational.one, Rational.one, Rational.one, Rational.one, Rational.one, Rational.one)}');
}
Output
{1 1 1; 1 1 1} = 1/6
SqrtRational wigner6jTwice(int j1, int j2, int j3, int j4, int j5, int j6)

wigner6j with doubled arguments.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('{½ ½ 1; ½ ½ 0} = ${wigner6jTwice(1, 1, 2, 1, 1, 0)}');
}
Output
{½ ½ 1; ½ ½ 0} = 1/2
wigner9jfunction
SqrtRational wigner9j(Rational j1, Rational j2, Rational j3, Rational j4, Rational j5, Rational j6, Rational j7, Rational j8, Rational j9)

The Wigner 9j symbol \{j_{1} j_{2} j_{3}; j_{4} j_{5} j_{6}; j_{7} j_{8} j_{9}\}, exact, as \sum_{x} (-1)^{2x} (2x + 1) \{j_{1} j_{4} j_{7}; j_{8} j_{9} x\}\{j_{2} j_{5} j_{8}; j_{4} x j_{6}\}\{j_{3} j_{6} j_{9}; x j_{1} j_{2}\}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final o = Rational.one, h = half(1);
  print('${wigner9j(h, h, o, h, h, o, o, o, Rational.zero)}');
}
Output
-1/18
SqrtRational wigner9jTwice(int j1, int j2, int j3, int j4, int j5, int j6, int j7, int j8, int j9)

wigner9j with doubled arguments.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${wigner9jTwice(1, 1, 2, 1, 1, 2, 2, 2, 0)}');
}
Output
-1/18
racahWfunction
SqrtRational racahW(Rational a, Rational b, Rational c, Rational d, Rational e, Rational f)

The Racah W coefficient W(abcd; ef) = (-1)^{a + b + c + d} \{a b e; d c f\}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final o = Rational.one;
  print('W(1111; 11) = ${racahW(o, o, o, o, o, o)}');
}
Output
W(1111; 11) = 1/6
wignerSmallDfunction
double wignerSmallD(Rational j, Rational mp, Rational m, double beta)

Wigner small-d matrix element d^{j}_{m'm}(\beta) (Condon–Shortley; active rotations, as Sakurai), double precision.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // d^{1/2}_{1/2,1/2}(β) = cos(β/2).
  print('${wignerSmallD(half(1), half(1), half(1), 1.0)} vs cos(0.5) ≈ 0.8775825618903728');
}
Output
0.8775825618903728 vs cos(0.5) ≈ 0.8775825618903728
wignerDfunction
Complex wignerD(Rational j, Rational mp, Rational m, double alpha, double beta, double gamma)

Wigner D-matrix element D^{j}_{m'm}(\alpha, \beta, \gamma) = e^{-i m' \alpha} d^{j}_{m'm}(\beta) e^{-i m \gamma} (z-y-z Euler angles).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${wignerD(Rational.one, Rational.zero, Rational.zero, 0.3, 1.2, 0.7)}  (= cos β for j = 1, m = m′ = 0)');
}
Output
(0.36235775447667357-0.0j)  (= cos β for j = 1, m = m′ = 0)
List<List<Complex>> wignerDMatrix(Rational j, double alpha, double beta, double gamma)

The full (2j + 1) \times (2j + 1) D-matrix, rows m' = j \cdots - j, columns m = j \cdots - j.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final d = wignerDMatrix(Rational.one, 0, 3.141592653589793 / 2, 0);
  for (final r in d) print(r.map((z) => z.re.toStringAsFixed(4)).join('  '));
}
Output
0.5000  -0.7071  0.5000
0.7071  0.0000  -0.7071
0.5000  0.7071  0.5000
mobiusfunction
int mobius(BigInt n)

The Möbius function \mu(n) for n >= 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Mertens function: the running sum of μ(n).
  var m = 0;
  final out = <int>[];
  for (var n = 1; n <= 20; n++) {
    m += mobius(BigInt.from(n));
    out.add(m);
  }
  print('M(1..20) = $out');
}
Output
M(1..20) = [1, 0, -1, -1, -2, -1, -2, -2, -2, -1, -2, -2, -3, -2, -1, -1, -2, -2, -3, -3]
divisorSigmafunction
BigInt divisorSigma(BigInt n, [int k = 1])

The divisor function \sigma_{k}(n) = \sum_{d \mid n} d^{k} for n >= 1, k >= 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // σ_k(n) = Σ_{d | n} d^k. 28 is perfect: σ(28) = 2·28.
  print('σ(28) = ${divisorSigma(BigInt.from(28))}, σ_2(12) = ${divisorSigma(BigInt.from(12), 2)}');
  print('σ(2^89 − 1) = 2^89 (a Mersenne prime): ${divisorSigma(BigInt.two.pow(89) - BigInt.one) == BigInt.two.pow(89)}');
}
Output
σ(28) = 56, σ_2(12) = 210
σ(2^89 − 1) = 2^89 (a Mersenne prime): true
numDivisorsfunction
int numDivisors(BigInt n)

The number of divisors \tau(n) = \sigma_{0}(n).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('τ(360) = ${numDivisors(BigInt.from(360))}, τ(2^10) = ${numDivisors(BigInt.from(1024))}');
}
Output
τ(360) = 24, τ(2^10) = 11
totientIntfunction
int totientInt(int n)

totient on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('φ(n) for n = 1..12: ${[for (var n = 1; n <= 12; n++) totientInt(n)]}');
  print('Möbius μ(n):        ${[for (var n = 1; n <= 12; n++) mobiusInt(n)]}');
  // Σ φ(d) over the divisors d of n is n.
  print('Σ φ(d) for d | 36:  ${divisorsInt(36).fold<int>(0, (s, d) => s + totientInt(d))}');
}
Output
φ(n) for n = 1..12: [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4]
Möbius μ(n):        [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0]
Σ φ(d) for d | 36:  36
mobiusIntfunction
int mobiusInt(int n)

mobius on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print([for (var n = 1; n <= 12; n++) 'μ($n)=${mobiusInt(n)}'].join('  '));
  // Σ_{n ≤ N} μ(n) (the Mertens function) stays small.
  print('Mertens M(1000) = ${[for (var n = 1; n <= 1000; n++) mobiusInt(n)].reduce((a, b) => a + b)}');
}
Output
μ(1)=1  μ(2)=-1  μ(3)=-1  μ(4)=0  μ(5)=-1  μ(6)=1  μ(7)=-1  μ(8)=0  μ(9)=0  μ(10)=1  μ(11)=-1  μ(12)=0
Mertens M(1000) = 2
int divisorSigmaInt(int n, [int k = 1])

divisorSigma on ints (the result must fit an int).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Amicable pair 220 and 284: σ(n) − n gives the other.
  print('σ(220) − 220 = ${divisorSigmaInt(220) - 220}, σ(284) − 284 = ${divisorSigmaInt(284) - 284}');
}
Output
σ(220) − 220 = 284, σ(284) − 284 = 220
int carmichaelLambdaInt(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // λ(n) divides φ(n); 561 is a Carmichael number since λ(561) = 80 divides 560.
  for (final n in [8, 15, 561]) print('λ($n) = ${carmichaelLambdaInt(n)}, φ($n) = ${totientInt(n)}');
}
Output
λ(8) = 2, φ(8) = 4
λ(15) = 4, φ(15) = 8
λ(561) = 80, φ(561) = 320
List<BigInt> divisorSigmaTable(int length, int k)

\sigma_{k}(n) for every n in [0, length) (index 0 is 0), by a divisor sieve: O(length log length).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final t = divisorSigmaTable(13, 1);
  print('σ(0..12) = $t');
}
Output
σ(0..12) = [0, 1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28]
List<BigInt> continuedFraction(Rational x)

The regular continued fraction [a0; a1... ak] of x (finite; the last term is > 1 unless x is an integer), matching SymPy continued_fraction.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final x = Rational.parse('415/93');
  final cf = continuedFraction(x);
  print('415/93 = $cf');
  print('back: ${fromContinuedFraction(cf)}');
  print('convergents: ${convergents(cf)}');
}
Output
415/93 = [4, 2, 6, 7]
back: 415/93
convergents: [4, 9/2, 58/13, 415/93]
Rational fromContinuedFraction(List<BigInt> terms)

The value of the finite continued fraction [a0; a1... ak].

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final terms = [3, 7, 15, 1, 292].map(BigInt.from).toList();
  print('[3; 7, 15, 1, 292] = ${fromContinuedFraction(terms)}');
}
Output
[3; 7, 15, 1, 292] = 103993/33102
convergentsfunction
List<Rational> convergents(List<BigInt> terms)

The convergents p_i / q_i of [a0; a1...].

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // The convergents of π's continued fraction: 3, 22/7, 333/106, 355/113, …
  print(convergents([3, 7, 15, 1, 292, 1].map(BigInt.from).toList()).join(', '));
}
Output
3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215
PeriodicContinuedFraction quadraticContinuedFraction(BigInt p, BigInt q, BigInt d)

The continued fraction of (p + sqrt(d)) / q with d > 0 not a square and q != 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // (1 + √5)/2 = [1; (1)].
  final cf = quadraticContinuedFraction(BigInt.one, BigInt.two, BigInt.from(5));
  print('golden ratio: $cf, first terms ${cf.take(8)}');
  print('(3 + √7)/2: ${quadraticContinuedFraction(BigInt.from(3), BigInt.two, BigInt.from(7))}');
}
Output
golden ratio: [; (1)], first terms [1, 1, 1, 1, 1, 1, 1, 1]
(3 + √7)/2: [2; (1, 4, 1, 1)]
PeriodicContinuedFraction sqrtContinuedFraction(BigInt n)

The continued fraction of sqrt(n) for a non-square n > 0: [a0; (a1... a_l)].

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cf = sqrtContinuedFraction(BigInt.from(7));
  print('√7 = $cf');
  final cf2 = sqrtContinuedFraction(BigInt.from(2));
  print('√2 = $cf2');
}
Output
√7 = [2; (1, 1, 1, 4)]
√2 = [1; (2)]
(BigInt, BigInt) pellFundamental(BigInt d)

The fundamental solution (x, y) of Pell's equation x^2 - d y^2 = 1 (d > 0 not a square).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final d in [2, 7, 61]) {
    final (x, y) = pellFundamental(BigInt.from(d));
    print('x² − ${d}y² = 1: x = $x, y = $y, check ${x * x - BigInt.from(d) * y * y}');
  }
}
Output
x² − 2y² = 1: x = 3, y = 2, check 1
x² − 7y² = 1: x = 8, y = 3, check 1
x² − 61y² = 1: x = 1766319049, y = 226153980, check 1
(BigInt, BigInt)? negativePellFundamental(BigInt d)

The fundamental solution of x^2 - d y^2 = -1, or null when it has none (the period of sqrt d is even).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('x² − 2y² = −1: ${negativePellFundamental(BigInt.two)}');
  print('x² − 13y² = −1: ${negativePellFundamental(BigInt.from(13))}');
  print('x² − 3y² = −1: ${negativePellFundamental(BigInt.from(3))} (none: the period of √3 is even)');
}
Output
x² − 2y² = −1: (1, 1)
x² − 13y² = −1: (18, 5)
x² − 3y² = −1: null (none: the period of √3 is even)
(Map<BigInt, int>, BigInt) trialDivision(BigInt n, {int bound = 10000})

Trial division of n > 0 by primes <= bound: the factors found and the unfactored cofactor.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final (found, rest) = trialDivision(BigInt.parse('600851475143'), bound: 1000);
  print('factors below 1000: $found, cofactor $rest');
}
Output
factors below 1000: {71: 1, 839: 1}, cofactor 10086647
BigInt? pollardRhoBrent(BigInt n, {int maxIterations = 1 << 20, int seed = 1})

Pollard's rho with Brent's cycle detection and batched gcds: a non-trivial factor of the composite n, or null after maxIterations steps per polynomial (tries x^2 + c for several c).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // A product of two 10-digit primes.
  final n = BigInt.parse('1000000007') * BigInt.parse('9999999967');
  print('factor of $n: ${pollardRhoBrent(n)}');
}
Output
factor of 10000000036999999769: 1000000007
ecmFactorfunction
BigInt? ecmFactor(BigInt n, {int b1 = 2000, int curves = 40, int seed = 7})

Lenstra's elliptic-curve method, stage 1 only, on Montgomery curves with Suyama's parametrisation and the x-only Montgomery ladder: a non-trivial factor of n (odd, composite, not a prime power), or null.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final n = BigInt.parse('1000000000039') * BigInt.parse('1000000000061');
  final f = ecmFactor(n, b1: 5000, curves: 60);
  print('ECM factor: $f, divides n: ${f != null && n % f == BigInt.zero}');
}
Output
ECM factor: 1000000000039, divides n: true
perfectPowerfunction
(BigInt, int) perfectPower(BigInt n)

(r, k) with n = r^k and k maximal, for n >= 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in ['1024', '729', '1000000', '12']) print('$n = ${perfectPower(BigInt.parse(n))}');
}
Output
1024 = (2, 10)
729 = (3, 6)
1000000 = (10, 6)
12 = (12, 1)
factorintfunction
Map<BigInt, int> factorint(BigInt n)

The prime factorisation of n != 0 as a map prime -> exponent, ascending (SymPy factorint).

n = 1 gives {}; a negative n gets the key -1. Trial division, then Pollard rho (Brent), then ECM stage 1 for what is left; primality of the pieces by isProbablePrime (BPSW above 2^{63}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final s in ['360', '600851475143', '18446744073709551617']) {
    final f = factorint(BigInt.parse(s));
    print('$s = ${f.entries.map((e) => e.value == 1 ? '${e.key}' : '${e.key}^${e.value}').join(' · ')}');
  }
}
Output
360 = 2^3 · 3^2 · 5
600851475143 = 71 · 839 · 1471 · 6857
18446744073709551617 = 274177 · 67280421310721
factorintIntfunction
Map<int, int> factorintInt(int n)

factorint on ints: prime -> exponent, ascending.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [84, 1001, 65536, 999999]) {
    print('$n: ${factorintInt(n)}');
  }
}
Output
84: {2: 2, 3: 1, 7: 1}
1001: {7: 1, 11: 1, 13: 1}
65536: {2: 16}
999999: {3: 3, 7: 1, 11: 1, 13: 1, 37: 1}
divisorsfunction
List<BigInt> divisors(BigInt n)

The positive divisors of n != 0, ascending.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('divisors(360) = ${divisors(BigInt.from(360))}');
}
Output
divisors(360) = [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60, 72, 90, 120, 180, 360]
divisorsIntfunction
List<int> divisorsInt(int n)

divisors on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('divisors of 60: ${divisorsInt(60)}');
  print('number of divisors: ${numDivisors(BigInt.from(60))}');
  print('sum of divisors σ(60): ${divisorSigmaInt(60)}');
  // Perfect numbers: σ(n) = 2n.
  print('perfect numbers below 10 000: ${[for (var n = 2; n < 10000; n++) if (divisorSigmaInt(n) == 2 * n) n]}');
}
Output
divisors of 60: [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
number of divisors: 12
sum of divisors σ(60): 168
perfect numbers below 10 000: [6, 28, 496, 8128]
hurwitzZetafunction
double hurwitzZeta(double s, double x)

The Hurwitz zeta function \zeta(s, x) for real s != 1, x > 0, by Euler–Maclaurin summation.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('ζ(2, 1) = ${hurwitzZeta(2, 1)} (π²/6 = ${3.141592653589793 * 3.141592653589793 / 6})');
  print('ζ(2, 1/2) = ${hurwitzZeta(2, 0.5)} (= π²/2)');
}
Output
ζ(2, 1) = 1.6449340668482262 (π²/6 = 1.6449340668482264)
ζ(2, 1/2) = 4.934802200544678 (= π²/2)
digammafunction
double digamma(double x)

The digamma function \psi(x) for real x > 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('ψ(1) = ${digamma(1)} (−γ), ψ(1/2) = ${digamma(0.5)}');
}
Output
ψ(1) = -0.5772156649015325 (−γ), ψ(1/2) = -1.9635100260214227
riemannZetafunction
double riemannZeta(double s)

The Riemann zeta function \zeta(s) for real s != 1: hurwitzZeta at x = 1 for s >= 1/2, the functional equation ζ(s) = 2^s π^{s−1} sin(πs/2) Γ(1−s) ζ(1−s) below.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('ζ(2) = ${riemannZeta(2)}, ζ(3) = ${riemannZeta(3)}, ζ(−1) = ${riemannZeta(-1)}');
}
Output
ζ(2) = 1.6449340668482262, ζ(3) = 1.2020569031595938, ζ(−1) = -0.08333333333333334
gammafunction
double gamma(double x)

The gamma function \Gamma(x) for real x (Lanczos, g = 7, about 1e-15 relative; reflection for x < 1/2).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('Γ(5) = ${gamma(5)}, Γ(1/2) = ${gamma(0.5)} (√π), Γ(−1/2) = ${gamma(-0.5)}');
}
Output
Γ(5) = 23.999999999999996, Γ(1/2) = 1.7724538509055159 (√π), Γ(−1/2) = -3.5449077018110295
Rational zetaEvenCoefficient(int k)

\zeta(2k) = c \cdot \pi^{2k} with the exact rational c = (−1)^{k+1} B_{2k} 2^{2k−1} / (2k)! (k >= 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // ζ(2k) = c·π^{2k} with c exact.
  for (var k = 1; k <= 4; k++) print('ζ(${2 * k}) = ${zetaEvenCoefficient(k)} · π^${2 * k}');
}
Output
ζ(2) = 1/6 · π^2
ζ(4) = 1/90 · π^4
ζ(6) = 1/945 · π^6
ζ(8) = 1/9450 · π^8
double exponentialIntegralE1(double x)

The exponential integral E_1(x) = ∫_x^∞ e^{−t} / t dt for x > 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('E1(1) = ${exponentialIntegralE1(1).toStringAsPrecision(12)}, E1(10) = ${exponentialIntegralE1(10).toStringAsPrecision(12)}');
}
Output
E1(1) = 0.219383934396, E1(10) = 0.00000415696892969
egcdfunction
(BigInt, BigInt, BigInt) egcd(BigInt a, BigInt b)

The extended Euclidean algorithm: (g, x, y) with a*x + b*y = g = gcd(a, b), g >= 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final (g, x, y) = egcd(BigInt.from(240), BigInt.from(46));
  print('gcd(240, 46) = $g');
  print('240·($x) + 46·($y) = ${BigInt.from(240) * x + BigInt.from(46) * y}');
}
Output
gcd(240, 46) = 2
240·(-9) + 46·(47) = 2
modInversefunction
BigInt modInverse(BigInt a, BigInt m)

a^-1 mod m in [0, m). Throws ArgumentError when gcd(a, m) != 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final inv = modInverse(BigInt.from(17), BigInt.from(3120));
  print('17⁻¹ mod 3120 = $inv');
  print('check: ${(BigInt.from(17) * inv) % BigInt.from(3120)}');
  print('3⁻¹ mod 7 (int) = ${modInverseInt(3, 7)}');
}
Output
17⁻¹ mod 3120 = 2753
check: 1
3⁻¹ mod 7 (int) = 5
int modInverseInt(int a, int m)

modInverse on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('3⁻¹ mod 7 = ${modInverseInt(3, 7)}, 17⁻¹ mod 3120 = ${modInverseInt(17, 3120)}');
}
Output
3⁻¹ mod 7 = 5, 17⁻¹ mod 3120 = 2753
mulModIntfunction
int mulModInt(int a, int b, int m)

a * b mod m for ints, exact for every 0 < m < 2^63 (falls back to BigInt when the product could overflow).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // a·b overflows 64 bits; mulModInt is still exact.
  const a = 9000000000000000000, b = 8000000000000000000, m = 9223372036854775783;
  print('a·b mod m = ${mulModInt(a, b, m)}');
  print('BigInt check: ${(BigInt.from(a) * BigInt.from(b)) % BigInt.from(m)}');
}
Output
a·b mod m = 115831543078036282
BigInt check: 115831543078036282
modPowIntfunction
int modPowInt(int b, int e, int m)

b^e mod m for ints (e >= 0, m >= 1), exact for every 64-bit modulus.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('2^100 mod 1 000 000 007 = ${modPowInt(2, 100, 1000000007)}');
  print('(a·b) mod m without overflow: ${mulModInt(4000000000000000000, 3, 9000000000000000001)}');
}
Output
2^100 mod 1 000 000 007 = 976371285
(a·b) mod m without overflow: 2999999999999999999
modPowfunction
BigInt modPow(BigInt b, BigInt e, BigInt m)

b^e mod m on BigInt; a negative e uses the inverse of b.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // RSA in miniature: encrypt with e, decrypt with d = e⁻¹ mod φ.
  final p = BigInt.from(61), q = BigInt.from(53), n = p * q, e = BigInt.from(17);
  final d = modInverse(e, (p - BigInt.one) * (q - BigInt.one));
  final c = modPow(BigInt.from(65), e, n);
  print('cipher $c, decrypted ${modPow(c, d, n)}, 3^−1 mod 7 = ${modPow(BigInt.from(3), BigInt.from(-1), BigInt.from(7))}');
}
Output
cipher 2790, decrypted 65, 3^−1 mod 7 = 5
crtfunction
(BigInt, BigInt)? crt(List<BigInt> residues, List<BigInt> moduli)

The Chinese remainder theorem for arbitrary (not necessarily coprime) moduli.

Returns (x, l) with 0 <= x < l = lcm(moduli) and x ≡ residues[i] (mod moduli[i]), or null when the congruences are inconsistent.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7): Sunzi's problem.
  final r = crtInt([2, 3, 2], [3, 5, 7]);
  print('x = ${r!.$1} (mod ${r.$2})');
  final big = crt([BigInt.from(1), BigInt.from(2)], [BigInt.from(1000003), BigInt.from(1000033)]);
  print('big: ${big!.$1} (mod ${big.$2})');
}
Output
x = 23 (mod 105)
big: 233341700024 (mod 1000036000099)
crtIntfunction
(int, int)? crtInt(List<int> residues, List<int> moduli)

crt on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
  print('${crtInt([2, 3, 2], [3, 5, 7])}');
  print('inconsistent: ${crtInt([1, 2], [4, 6])}');
}
Output
(23, 105)
inconsistent: null
sqrtModPrimefunction
BigInt? sqrtModPrime(BigInt a, BigInt p)

A square root of a modulo the odd prime p by Tonelli–Shanks, or null when a is a non-residue. p is not checked for primality.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = BigInt.from(10009);
  final r = sqrtModPrime(BigInt.from(2), p)!;
  print('a square root of 2 mod 10009: $r');
  print('check: $r² mod p = ${(r * r) % p}');
  print('3 is a square mod 7: ${sqrtModPrime(BigInt.from(3), BigInt.from(7)) != null}');
}
Output
a square root of 2 mod 10009: 4419
check: 4419² mod p = 2
3 is a square mod 7: false
BigInt? sqrtModPrimeCipolla(BigInt a, BigInt p)

A square root of a modulo the odd prime p by Cipolla's algorithm, or null for a non-residue.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final r = sqrtModPrimeCipolla(BigInt.from(10), BigInt.from(13))!;
  print('√10 mod 13 = $r, check ${r * r % BigInt.from(13)}');
  print('√5 mod 13: ${sqrtModPrimeCipolla(BigInt.from(5), BigInt.from(13))}');
}
Output
√10 mod 13 = 6, check 10
√5 mod 13: null
List<BigInt> sqrtModPrimePower(BigInt a, BigInt p, int k)

All square roots of a modulo p^k (p prime, k >= 1), sorted ascending.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√2 mod 7³: ${sqrtModPrimePower(BigInt.two, BigInt.from(7), 3)}');
}
Output
√2 mod 7³: [108, 235]
sqrtModfunction
List<BigInt> sqrtMod(BigInt a, BigInt n)

All square roots of a modulo n >= 1, sorted ascending (SymPy sqrt_mod(a, n, all_roots=True)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 1 has four square roots mod 15.
  print('√1 mod 15: ${sqrtMod(BigInt.one, BigInt.from(15))}, √4 mod 12: ${sqrtMod(BigInt.from(4), BigInt.from(12))}');
}
Output
√1 mod 15: [1, 4, 11, 14], √4 mod 12: [2, 4, 8, 10]
BigInt carmichaelLambda(BigInt n)

Carmichael's function \lambda(n): the exponent of (Z/n)^×.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('λ(2^20) = ${carmichaelLambda(BigInt.from(1 << 20))}, λ(1001) = ${carmichaelLambda(BigInt.from(1001))}');
}
Output
λ(2^20) = 262144, λ(1001) = 60
BigInt multiplicativeOrder(BigInt a, BigInt n)

The multiplicative order of a modulo n (gcd(a, n) = 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('ord_7(3) = ${multiplicativeOrder(BigInt.from(3), BigInt.from(7))}, ord_101(2) = ${multiplicativeOrder(BigInt.two, BigInt.from(101))}');
}
Output
ord_7(3) = 6, ord_101(2) = 100
int multiplicativeOrderInt(int a, int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The period of the decimal expansion of 1/7 is ord_7(10).
  print('period of 1/7: ${multiplicativeOrderInt(10, 7)}, of 1/97: ${multiplicativeOrderInt(10, 97)}');
}
Output
period of 1/7: 6, of 1/97: 96
bool hasPrimitiveRoot(BigInt n)

Whether (Z/n)^× is cyclic: n = 1, 2, 4, p^{k} or 2p^{k} with p an odd prime.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print([for (var n = 2; n <= 20; n++) if (hasPrimitiveRoot(BigInt.from(n))) n]);
}
Output
[2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 17, 18, 19]
BigInt? primitiveRoot(BigInt n)

The smallest primitive root modulo n, or null when (Z/n)^× is not cyclic.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('g(7) = ${primitiveRoot(BigInt.from(7))}, g(2·3^5) = ${primitiveRoot(BigInt.from(486))}, g(8) = ${primitiveRoot(BigInt.from(8))}');
}
Output
g(7) = 3, g(2·3^5) = 5, g(8) = null
int? primitiveRootInt(int n)

primitiveRoot on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final p in [7, 11, 23, 41]) {
    final g = primitiveRootInt(p)!;
    print('smallest primitive root of $p: $g (order ${multiplicativeOrderInt(g, p)})');
  }
}
Output
smallest primitive root of 7: 3 (order 6)
smallest primitive root of 11: 2 (order 10)
smallest primitive root of 23: 5 (order 22)
smallest primitive root of 41: 6 (order 40)
bool isPrimitiveRoot(BigInt g, BigInt n)

Whether g generates (Z/n)^×.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('3 mod 7: ${isPrimitiveRoot(BigInt.from(3), BigInt.from(7))}, 2 mod 7: ${isPrimitiveRoot(BigInt.two, BigInt.from(7))}');
}
Output
3 mod 7: true, 2 mod 7: false
totientfunction
BigInt totient(BigInt n)

Euler's totient \varphi(n).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('φ(36) = ${totient(BigInt.from(36))}, φ(10^20) = ${totient(BigInt.parse('100000000000000000000'))}');
}
Output
φ(36) = 12, φ(10^20) = 40000000000000000000
discreteLogfunction
BigInt? discreteLog(BigInt n, BigInt a, BigInt b)

The discrete logarithm: the least x >= 0 with b^x ≡ a (mod n), or null when a is not a power of b.

Pohlig–Hellman over the factorisation of the order of b, with baby-step giant-step on each prime-power component. Requires gcd(b, n) = 1 (SymPy discrete_log(n, a, b)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final n = BigInt.from(1000003), b = BigInt.two;
  final a = modPow(b, BigInt.from(123456), n);
  print('log_2($a) mod $n = ${discreteLog(n, a, b)}');
}
Output
log_2(435147) mod 1000003 = 123456
int? discreteLogInt(int n, int a, int b)

discreteLog on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Solve 3^x ≡ 13 (mod 17).
  final x = discreteLogInt(17, 13, 3)!;
  print('3^x ≡ 13 (mod 17): x = $x');
  print('check: 3^$x mod 17 = ${modPowInt(3, x, 17)}');
}
Output
3^x ≡ 13 (mod 17): x = 4
check: 3^4 mod 17 = 13
bernoullifunction
Rational bernoulli(int n)

The Bernoulli number B_{n}, exact, with the convention B_1 = −1/2 (B_n = 0 for odd n > 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('B0..B12: ${[for (var n = 0; n <= 12; n++) bernoulli(n)]}');
  // ζ(2k) = (−1)^(k+1) B(2k) (2π)^(2k) / (2 (2k)!): the rational part for k = 1 is 1/6.
  print('ζ(2) / π² = ${zetaEvenCoefficient(1)}, ζ(4) / π⁴ = ${zetaEvenCoefficient(2)}');
}
Output
B0..B12: [1, -1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66, 0, -691/2730]
ζ(2) / π² = 1/6, ζ(4) / π⁴ = 1/90
eisensteinfunction
QSeries eisenstein(int k, int precision)

The normalised Eisenstein series E_k = 1 − (2k / B_k) Σ_{n>=1} σ_{k−1}(n) q^n of even weight k >= 2 (E_2 is quasi-modular).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final e4 = eisenstein(4, 6), e6 = eisenstein(6, 6);
  print('E4 = ${e4.coefficients}');
  print('E6 = ${e6.coefficients}');
  print('(E4³ − E6²)/1728 = Δ: ${(e4.pow(3) - e6.pow(2)).scale(Rational.fromInt(1, 1728)).agreesWith(delta(6))}');
}
Output
E4 = [1, 240, 2160, 6720, 17520, 30240]
E6 = [1, -504, -16632, -122976, -532728, -1575504]
(E4³ − E6²)/1728 = Δ: true
eulerProductfunction
QSeries eulerProduct(int precision)

The Euler function ∏_{n>=1} (1 − q^n) by the pentagonal number theorem.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Euler's product ∏(1 − qⁿ) as a power series: the pentagonal number theorem says only
  // exponents k(3k − 1)/2 survive, with coefficients ±1.
  final e = eulerProduct(30);
  print('∏(1 − qⁿ) = $e');
  final nonzero = [for (var n = 0; n < 30; n++) if (e.coefficients[n] != BigInt.zero) n];
  print('nonzero exponents: $nonzero');
  print('pentagonal numbers: ${[for (final k in [0, 1, -1, 2, -2, 3, -3, 4, -4]) k * (3 * k - 1) ~/ 2]}');
}
Output
∏(1 − qⁿ) = 1 + (-1)q + (-1)q^2 + (1)q^5 + (1)q^7 + (-1)q^12 + (-1)q^15 + (1)q^22 + (1)q^26 + O(q^30)
nonzero exponents: [0, 1, 2, 5, 7, 12, 15, 22, 26]
pentagonal numbers: [0, 1, 2, 5, 7, 12, 15, 22, 26]
deltafunction
QSeries delta(int precision)

The discriminant Δ = q ∏ (1 − q^n)^24 = η^24, the weight-12 cusp form of level 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('Δ = ${delta(8).coefficients}');
}
Output
Δ = [0, 1, -24, 252, -1472, 4830, -6048, -16744]
List<BigInt> ramanujanTauList(int count)

Ramanujan's \tau(n) for 1 <= n < count + 1 (index 0 is 0): the coefficients of delta.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final t = ramanujanTauList(10);
  print('τ(1..10) = ${t.sublist(1)}');
  print('τ multiplicative: τ(2)τ(3) = τ(6): ${t[2] * t[3] == t[6]}');
}
Output
τ(1..10) = [1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920]
τ multiplicative: τ(2)τ(3) = τ(6): true
ramanujanTaufunction
BigInt ramanujanTau(int n)

Ramanujan's \tau(n).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('τ(0..10) = ${ramanujanTauList(10)}');
  // τ is multiplicative: τ(6) = τ(2) τ(3).
  print('τ(6) = ${ramanujanTau(6)}, τ(2)·τ(3) = ${ramanujanTau(2) * ramanujanTau(3)}');
}
Output
τ(0..10) = [0, 1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920]
τ(6) = -6048, τ(2)·τ(3) = -6048
etaProductfunction
QSeries etaProduct(Map<int, int> exponents, int precision)

The eta product ∏_m η(mτ)^{r_m} (keys m, values r_m, which may be negative) as a q-series.

The leading power Σ m r_m / 24 must be a non-negative integer; the result is known to O(q^precision). For example {1: 2, 11: 2} is the weight-2 newform of 11a_{1} and {1: 24} is \Delta.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // η(τ)²η(11τ)², the weight-2 newform of level 11.
  print('${etaProduct({1: 2, 11: 2}, 12).coefficients}');
}
Output
[0, 1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1]
thetaZfunction
QSeries thetaZ(int precision)

The Jacobi theta series θ(q) = Σ_{n ∈ Z} q^{n^2} = 1 + 2q + 2q^4 +..., the theta series of Z.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('θ = ${thetaZ(17).coefficients}');
}
Output
θ = [1, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2]
thetaZnfunction
QSeries thetaZn(int n, int precision)

The theta series of Z^{n}: θ^n, whose q^m coefficient is r_n(m), the number of representations of m as a sum of n squares.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The coefficient of qⁿ in θ(q)⁴ counts the ways to write n as a sum of 4 squares.
  final t4 = thetaZn(4, 11);
  print('r4(n), n = 0..10: ${[for (var n = 0; n <= 10; n++) t4[n]]}');
  print('Jacobi: r4(n) = 8 × (sum of divisors not divisible by 4); r4(10) = 8 × (1 + 2 + 5 + 10) = ${8 * 18}');
}
Output
r4(n), n = 0..10: [1, 8, 24, 32, 24, 48, 96, 64, 24, 104, 144]
Jacobi: r4(n) = 8 × (sum of divisors not divisible by 4); r4(10) = 8 × (1 + 2 + 5 + 10) = 144
latticeThetafunction
QSeries latticeTheta(List<List<int>> gram, int precision, {bool even = true})

The theta series Σ_{x ∈ Z^n} q^{Q(x)} of the positive definite integral quadratic form Q(x) = x^T G x / 2 for an even Gram matrix G (even lattice; e.g. a Cartan matrix), or Q(x) = x^T G x when even is false.

Fincke–Pohst enumeration over the Cholesky decomposition (in doubles, with a safety margin; every counted vector is verified exactly in integers). Suited to small precision and dimension.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The hexagonal lattice A2 (Gram [[2, −1], [−1, 2]]): 6 vectors of norm 2.
  print('${latticeTheta([[2, -1], [-1, 2]], 8).coefficients}');
}
Output
[1, 6, 0, 6, 6, 0, 0, 12]
thetaE8function
QSeries thetaE8(int precision)

The theta series of the E8 lattice, Σ_{x ∈ E8} q^{x·x/2}, counted exactly via E_{8} = D_{8} \cup (D_{8} + \tfrac{1}{2} \cdot 1): integer vectors with even coordinate sum, plus half-integer vectors with even sum. Equals E_{4}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 240 roots; the theta series equals E4.
  final t = thetaE8(5);
  print('θ_E8 = ${t.coefficients}, equals E4: ${t.agreesWith(eisenstein(4, 5))}');
}
Output
θ_E8 = [1, 240, 2160, 6720, 17520], equals E4: true
isqrtSmallfunction
int isqrtSmall(int n)

floor(sqrt(n)) for small non-negative ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${isqrtSmall(99)}, ${isqrtSmall(100)}');
}
Output
9, 10
heckefunction
QSeries hecke(QSeries f, int n, int k)

The Hecke operator T_n on a q-expansion of a level-1 (or level-N, gcd(n, N) = 1, trivial character) form of weight k: b(m) = Σ_{d | gcd(m, n)} d^{k−1} a(mn / d^2).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Δ is a Hecke eigenform: T_2 Δ = τ(2) Δ.
  final d = delta(20);
  print('T_2 Δ = −24 Δ: ${hecke(d, 2, 12).agreesWith(d.scaleInt(-24))}');
}
Output
T_2 Δ = −24 Δ: true
heckePrimefunction
QSeries heckePrime(QSeries f, int p, int k)

The Hecke operator T_p for a prime p: b(m) = a(pm) + p^{k−1} a(m/p).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final d = delta(20);
  print('T_3 Δ = ${heckePrime(d, 3, 12).coefficients.take(5).toList()} (= 252 Δ)');
}
Output
T_3 Δ = [0, 252, -6048, 63504, -370944] (= 252 Δ)
bool isStrongProbablePrimeInt(int n, int a)

Whether n is a strong probable prime to base a (n odd, n > 2).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 2047 = 23·89 fools base 2 but not base 3.
  print('2047 base 2: ${isStrongProbablePrimeInt(2047, 2)}, base 3: ${isStrongProbablePrimeInt(2047, 3)}');
}
Output
2047 base 2: true, base 3: false
isPrimefunction
bool isPrime(int n)

Deterministic primality for every int (n < 2^63): trial division, then Miller–Rabin with millerRabinBases64.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [2, 91, 97, 2147483647, 1000000007, 561]) {
    print('$n prime: ${isPrime(n)}');
  }
}
Output
2 prime: true
91 prime: false
97 prime: true
2147483647 prime: true
1000000007 prime: true
561 prime: false
bool isStrongProbablePrime(BigInt n, BigInt a)

Whether n is a strong probable prime to base a (n odd, n > 2).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final n = BigInt.from(3215031751); // a strong pseudoprime to 2, 3, 5, 7
  for (final a in [2, 3, 5, 7, 11]) print('base $a: ${isStrongProbablePrime(n, BigInt.from(a))}');
}
Output
base 2: true
base 3: true
base 5: true
base 7: true
base 11: false
isSquarefunction
bool isSquare(BigInt n)

Whether n is a perfect square.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${isSquare(BigInt.parse('152415787532388367501905199875019052100'))}, ${isSquare(BigInt.parse('152415787532388367501905199875019052101'))}');
}
Output
true, false
bool isStrongLucasProbablePrime(BigInt n)

The strong Lucas probable-prime test with Selfridge's parameters (method A: the first D in 5, -7, 9, -11, … with (D/n) = −1, P = 1, Q = (1 − D)/4). n odd, n > 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('2^127 − 1: ${isStrongLucasProbablePrime(BigInt.two.pow(127) - BigInt.one)}, 3215031751: ${isStrongLucasProbablePrime(BigInt.from(3215031751))}');
}
Output
2^127 − 1: true, 3215031751: false
bool isProbablePrime(BigInt n)

Primality of a BigInt: exact below 2^{63} (deterministic Miller–Rabin), Baillie–PSW above (strong base-2 Miller–Rabin plus the strong Lucas test; no BPSW pseudoprime is known).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final m127 = BigInt.two.pow(127) - BigInt.one; // a Mersenne prime
  final m128 = BigInt.two.pow(128) - BigInt.one;
  print('2^127 − 1 is prime: ${isProbablePrime(m127)}');
  print('2^128 − 1 is prime: ${isProbablePrime(m128)}');
  print('next prime after 2^64: ${nextPrimeBig(BigInt.two.pow(64))}');
}
Output
2^127 − 1 is prime: true
2^128 − 1 is prime: false
next prime after 2^64: 18446744073709551629
nextPrimefunction
int nextPrime(int n)

The least prime > n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('next prime after 1000: ${nextPrime(1000)}');
  print('previous prime before 1000: ${prevPrime(1000)}');
  // Twin primes below 100.
  final ps = primesUpTo(100);
  print('twin primes: ${[for (var i = 1; i < ps.length; i++) if (ps[i] - ps[i - 1] == 2) (ps[i - 1], ps[i])]}');
}
Output
next prime after 1000: 1009
previous prime before 1000: 997
twin primes: [(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)]
prevPrimefunction
int prevPrime(int n)

The greatest prime < n; throws for n <= 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('prevPrime(100) = ${prevPrime(100)}, prevPrime(2^31) = ${prevPrime(1 << 31)}');
}
Output
prevPrime(100) = 97, prevPrime(2^31) = 2147483647
nextPrimeBigfunction
BigInt nextPrimeBig(BigInt n)

The least probable prime > n (BigInt).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('next prime after 10^30: ${nextPrimeBig(BigInt.from(10).pow(30))}');
}
Output
next prime after 10^30: 1000000000000000000000000000057
prevPrimeBigfunction
BigInt prevPrimeBig(BigInt n)

The greatest probable prime < n (BigInt); throws for n <= 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('largest prime below 2^128: ${prevPrimeBig(BigInt.two.pow(128))}');
}
Output
largest prime below 2^128: 340282366920938463463374607431768211297
isqrtIntfunction
int isqrtInt(int n)

floor(sqrt(n)) for n >= 0, exact for every 64-bit int.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:math';

void main() {
  // n = 67108865² − 1: the true floor of √n is 67108864.
  const n = 67108865 * 67108865 - 1;
  print('isqrtInt(n)        = ${isqrtInt(n)}');
  // n is above 2^52, so n.toDouble() rounds up to 67108865², and the double square root overshoots.
  print('sqrt(n).floor()    = ${sqrt(n.toDouble()).floor()}');
}
Output
isqrtInt(n)        = 67108864
sqrt(n).floor()    = 67108865
isqrtfunction
BigInt isqrt(BigInt n)

floor(sqrt(n)) for n >= 0 on BigInt (Newton).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final n = BigInt.parse('1' + '0' * 60);
  print('⌊√10^60⌋ = ${isqrt(n)}');
  print('⌊√(10^60 − 1)⌋ = ${isqrt(n - BigInt.one)}');
}
Output
⌊√10^60⌋ = 1000000000000000000000000000000
⌊√(10^60 − 1)⌋ = 999999999999999999999999999999
primesUpTofunction
List<int> primesUpTo(int n)

All primes <= n, ascending (odd-only sieve of Eratosthenes).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('primes up to 50: ${primesUpTo(50)}');
  print('how many below 10 000: ${primesUpTo(10000).length}');
  print('primePi(10^6) = ${primePi(1000000)}');
}
Output
primes up to 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
how many below 10 000: 1229
primePi(10^6) = 78498
List<int> primesInRange(int lo, int hi, {int segment = 1 << 16})

All primes in [lo, hi), by a segmented sieve (memory O(sqrt(hi) + segment)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('primes in [10^9, 10^9 + 100): ${primesInRange(1000000000, 1000000100)}');
}
Output
primes in [10^9, 10^9 + 100): [1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093, 1000000097]
int countPrimesInRange(int lo, int hi)

The number of primes in [lo, hi) (segmented sieve).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('primes in [10^6, 2·10^6): ${countPrimesInRange(1000000, 2000000)}');
}
Output
primes in [10^6, 2·10^6): 70435
primePifunction
int primePi(int n)

The prime-counting function \pi(n), by the Lucy Hedgehog / Legendre-style recursion in O(n^(3/4)) time and O(sqrt n) memory. Practical up to about 10^12.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [10, 1000, 1000000, 10000000000]) print('π($n) = ${primePi(n)}');
}
Output
π(10) = 4
π(1000) = 168
π(1000000) = 78498
π(10000000000) = 455052511
jacobiSymbolfunction
int jacobiSymbol(BigInt a, BigInt n)

The Jacobi symbol (a/n) for odd n > 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // (a/n) for odd n; (2/15) = 1 although 2 is not a square mod 15.
  print('(2/15) = ${jacobiSymbol(BigInt.two, BigInt.from(15))}, (7/15) = ${jacobiSymbol(BigInt.from(7), BigInt.from(15))}');
}
Output
(2/15) = 1, (7/15) = -1
int legendreSymbol(BigInt a, BigInt p)

The Legendre symbol (a/p) for an odd prime p (primality is not checked; equals the Jacobi symbol).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = BigInt.from(23);
  print('squares mod 23: ${[for (var a = 1; a < 23; a++) if (legendreSymbol(BigInt.from(a), p) == 1) a]}');
}
Output
squares mod 23: [1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18]
int kroneckerSymbol(BigInt a, BigInt n)

The Kronecker symbol (a/n) for any integers a, n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Defined for every n, including even and negative.
  print('(5/8) = ${kroneckerSymbol(BigInt.from(5), BigInt.from(8))}, (−3/−7) = ${kroneckerSymbol(BigInt.from(-3), BigInt.from(-7))}, (3/0) = ${kroneckerSymbol(BigInt.from(3), BigInt.zero)}');
}
Output
(5/8) = -1, (−3/−7) = -1, (3/0) = 0
int jacobiSymbolInt(int a, int n)

jacobiSymbol on ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${[for (var a = 0; a < 9; a++) jacobiSymbolInt(a, 9)]}');
}
Output
[0, 1, 1, 0, 1, 1, 0, 1, 1]
int legendreSymbolInt(int a, int p)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const p = 13;
  print('quadratic residues mod 13: ${[for (var a = 1; a < p; a++) if (legendreSymbolInt(a, p) == 1) a]}');
  print('Jacobi (2/15) = ${jacobiSymbolInt(2, 15)}');
}
Output
quadratic residues mod 13: [1, 3, 4, 9, 10, 12]
Jacobi (2/15) = 1
int kroneckerSymbolInt(int a, int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The character of ℚ(√−4): (−4/n).
  print('${[for (var n = 1; n <= 12; n++) kroneckerSymbolInt(-4, n)]}');
}
Output
[1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0]

Constants and variables

List<QIMatrix> get exactPauli

The exact Pauli matrices over Q(i).

List<List<(int, int)>> get octonionTable

The octonion multiplication table in this Cayley–Dickson convention: octonionTable[j] = (s, k) with e_{i} e_{j} = s e_{k}.

fanoLinesgetter
List<(int, int, int)> get fanoLines

The seven lines of the Fano plane: oriented triples (i, j, k) of imaginary units with e_{i} e_{j} = e_{k} (and cyclically), each spanning a quaternion subalgebra.

final List<CMatrix> pauliWithIdentity

\sigma_{0} = I, \sigma_{1}, \sigma_{2}, \sigma_{3}.

pauligetter
List<CMatrix> get pauli

The Pauli matrices \sigma_{1}, \sigma_{2}, \sigma_{3}.

gellMannvariable
final List<CMatrix> gellMann

The Gell-Mann matrices \lambda_{1} \cdots \lambda_{8} (\operatorname{Tr} \lambda_{a} \lambda_{b} = 2\delta_{ab}).

final List<CMatrix> so3Generators

so(3): (L_{i})_{jk} = -\varepsilon_{\mathrm{ijk}}, so [L_{i}, L_{j}] = \varepsilon_{\mathrm{ijk}} L_{k} and e^{\theta n \cdot L} is the rotation by \theta about n.

({List<CMatrix> rotations, List<CMatrix> boosts}) get lorentzGenerators

The Lorentz algebra so(3,1) on (t, x, y, z): rotations J_{i} (as so(3) on the spatial block) and boosts K_{i} with (K_{i})_{0i} = (K_{i})_{i_{0}} = 1. [J_{i}, J_{j}] = \varepsilon J_{k}, [J_{i}, K_{j}] = \varepsilon K_{k}, [K_{i}, K_{j}] = -\varepsilon J_{k}.

CMatrix get minkowskiMetric

The Minkowski metric \operatorname{diag}(+1, -1, -1, -1) (mostly minus) as a matrix.

const List<List<int>> e8CartanMatrix

The Cartan matrix of E8 (Bourbaki numbering), the Gram matrix of the E8 root lattice.

const List<int> millerRabinBases64 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

The Miller–Rabin bases that are deterministic below 3.3 \cdot 10^{24} (Sorenson & Webster 2015), so for every int.

Typedefs

typedef ClassFunction = List<Cyclotomic>

A class function or character: one exact value per conjugacy class.

Classes

final class AlgebraicNumber implements Comparable<AlgebraicNumber>

A real algebraic number, held exactly as its minimal polynomial over Q (primitive, integer coefficients, positive leading coefficient) and an open isolating interval (lo, hi) with rational endpoints containing exactly one root of it (for a rational number the interval is the degenerate (r, r)).

Values are immutable; only the cached interval gets narrower as comparisons and conversions refine it.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final r2 = AlgebraicNumber.sqrt(Rational.fromInt(2));
  final r3 = AlgebraicNumber.sqrt(Rational.fromInt(3));
  final s = r2 + r3;
  print('√2 + √3 ≈ ${s.toDecimalString(20)}');
  print('minimal polynomial: ${s.minimalPolynomial}');
  print('(√2)² is rational: ${(r2 * r2).isRational}, value ${(r2 * r2).toRational()}');
}
Output
√2 + √3 ≈ 3.14626436994197234232
minimal polynomial: x^4 - 10*x^2 + 1
(√2)² is rational: true, value 2

Constructors

factory AlgebraicNumber.rational(Rational r)

The rational number r.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = AlgebraicNumber.rational(Rational.fromInt(22, 7));
  print('$a, degree ${a.degree}, toRational ${a.toRational()}');
}
Output
22/7, degree 1, toRational 22/7
factory AlgebraicNumber.fromInt(int n)

The integer n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${AlgebraicNumber.fromInt(-5)}, minimal polynomial ${AlgebraicNumber.fromInt(-5).minimalPolynomial}');
}
Output
-5, minimal polynomial x + 5
factory AlgebraicNumber.sqrt(Rational r)

The non-negative square root \sqrt{r} of r \ge 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final r = AlgebraicNumber.sqrt(Rational.fromInt(2));
  print('√2: minimal polynomial ${r.minimalPolynomial}, ≈ ${r.toDecimalString(30)}');
  print('(√2)² == 2: ${r * r == AlgebraicNumber.fromInt(2)}');
}
Output
√2: minimal polynomial x^2 - 2, ≈ 1.414213562373095048801688724209
(√2)² == 2: true
factory AlgebraicNumber.root(Rational r, int k)

The real k-th root of r (the non-negative one for even k, which needs r \ge 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final c = AlgebraicNumber.root(Rational.fromInt(2), 3);
  print('∛2: ${c.minimalPolynomial}, cubed ${c.pow(3)}');
}
Output
∛2: x^3 - 2, cubed 2
factory AlgebraicNumber.fromSqrtRational(SqrtRational v)

Sign · √square, from the exact Wigner-symbol values.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final v = SqrtRational.parseSigned('-3/8');
  final a = AlgebraicNumber.fromSqrtRational(v);
  print('$v → ${a.toDecimalString(12)}, squared ${a * a}');
}
Output
-√(3/8) → -0.612372435696, squared 3/8
factory AlgebraicNumber.rootInInterval(QPoly p, Rational lo, Rational hi)

The unique root of p in the interval (lo, hi]; throws unless there is exactly one.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The real root of x³ − x − 1 (the plastic number) in (1, 2].
  final p = AlgebraicNumber.rootInInterval(QPoly.fromInts([-1, -1, 0, 1]), Rational.one, Rational.fromInt(2));
  print('plastic number ≈ ${p.toDecimalString(25)}');
}
Output
plastic number ≈ 1.3247179572447460259609088
factory AlgebraicNumber.cosPi(Rational r)

\cos(r\pi) for rational r, exactly (\Psi_{N} for the reduced \frac{r}{2} = \frac{k}{N}, with the root picked by its rank, since 2\cos(\frac{2\pi j}{N}) decreases in j on 0 < j < \frac{N}{2}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // cos(π/5) = (1 + √5)/4, exactly.
  final c = AlgebraicNumber.cosPi(Rational.fromInt(1, 5));
  print('cos(π/5): ${c.minimalPolynomial}, ≈ ${c.toDecimalString(20)}');
  print('cos(π/3) = ${AlgebraicNumber.cosPi(Rational.fromInt(1, 3))}');
}
Output
cos(π/5): 4*x^2 - 2*x - 1, ≈ 0.80901699437494742410
cos(π/3) = 1/2
factory AlgebraicNumber.sinPi(Rational r)

\sin(r\pi) for rational r, exactly (= \cos((\frac{1}{2} - r)\pi)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final s = AlgebraicNumber.sinPi(Rational.fromInt(1, 12));
  print('sin(π/12) ≈ ${s.toDecimalString(20)}, degree ${s.degree}');
}
Output
sin(π/12) ≈ 0.25881904510252076234, degree 4
factory AlgebraicNumber.cos2PiOver(int n)

\cos(\frac{2\pi}{n}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // cos(2π/7) has degree 3; cos(2π/17) has degree 8 (Gauss's 17-gon).
  for (final n in [5, 7, 17]) print('cos(2π/$n): degree ${AlgebraicNumber.cos2PiOver(n).degree}');
}
Output
cos(2π/5): degree 2
cos(2π/7): degree 3
cos(2π/17): degree 8
factory AlgebraicNumber.sin2PiOver(int n)

\sin(\frac{2\pi}{n}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('sin(2π/8) = ${AlgebraicNumber.sin2PiOver(8).toLatex()}');
}
Output
sin(2π/8) = \frac{\sqrt{2}}{2}

Static

realRootsstaticmethod
static List<AlgebraicNumber> realRoots(QPoly p)

The distinct real roots of a nonzero polynomial, in increasing order.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rs = AlgebraicNumber.realRoots(QPoly.fromInts([1, 0, -10, 0, 1]));
  print('x⁴ − 10x² + 1: ${rs.map((r) => r.toDecimalString(10)).join(', ')}');
  print('largest is √2 + √3: ${rs.last == AlgebraicNumber.sqrt(Rational.fromInt(2)) + AlgebraicNumber.sqrt(Rational.fromInt(3))}');
}
Output
x⁴ − 10x² + 1: -3.1462643700, -0.3178372452, 0.3178372451, 3.1462643699
largest is √2 + √3: true
static QPoly twoCosMinimalPolynomial(int n)

The minimal polynomial of 2\cos(\frac{2\pi}{n}) (\Psi_{n}, from \Phi_{n}(x) = x^{\varphi(n)/2} \Psi_{n}(x + \frac{1}{x})), primitive with integer coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [5, 7, 9]) print('Ψ_$n = ${AlgebraicNumber.twoCosMinimalPolynomial(n)}');
}
Output
Ψ_5 = x^2 + x - 1
Ψ_7 = x^3 + x^2 - 2*x - 1
Ψ_9 = x^3 - 3*x + 1
goldenRatiostaticgetter
static AlgebraicNumber get goldenRatio

The golden ratio \frac{1 + \sqrt{5}}{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final phi = AlgebraicNumber.goldenRatio;
  print('φ ≈ ${phi.toDecimalString(15)}');
  print('φ² − φ − 1 = ${phi * phi - phi - AlgebraicNumber.one}');
  print('minimal polynomial: ${phi.minimalPolynomial}');
}
Output
φ ≈ 1.618033988749894
φ² − φ − 1 = 0
minimal polynomial: x^2 - x - 1
zerostaticfield
static final AlgebraicNumber zero
onestaticfield
static final AlgebraicNumber one

Properties

final QPoly minimalPolynomial

The minimal polynomial over Q: irreducible, primitive with integer coefficients, positive leading coefficient.

degreegetter
int get degree

The degree of the number over Q (1 for rationals).

bool get isRational
intervalgetter
(Rational, Rational) get interval

The current isolating interval (lo, hi) (equal ends for a rational).

AlgebraicNumber get reciprocal

1 / this (throws for zero).

signgetter
int get sign

-1, 0 or 1.

hashCodegetter
int get hashCode
({Rational rational, Rational coefficient, BigInt radicand})? get quadraticSurd

p + q\sqrt{d} with d > 1 square-free, when this has degree 2.

Methods

Rational? toRational()

The value as a Rational, or null when irrational.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final r = AlgebraicNumber.sqrt(Rational.fromInt(9, 4));
  print('√(9/4) → ${r.toRational()}, √2 → ${AlgebraicNumber.sqrt(Rational.fromInt(2)).toRational()}');
}
Output
√(9/4) → 3/2, √2 → null
powmethod
AlgebraicNumber pow(int k)

The integer power \mathrm{this}^{k} (k < 0 needs a nonzero value).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final phi = (AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(5))) * AlgebraicNumber.rational(Rational.fromInt(1, 2));
  print('φ^10 ≈ ${phi.pow(10).toDecimalString(10)}, φ^-1 = φ − 1: ${phi.pow(-1) == phi - AlgebraicNumber.one}');
}
Output
φ^10 ≈ 122.9918693812, φ^-1 = φ − 1: true
nthRootmethod
AlgebraicNumber nthRoot(int k)

The real k-th root (k \ge 1): the non-negative one for even k (which needs this ≥ 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final x = AlgebraicNumber.fromInt(-27).nthRoot(3);
  print('∛(−27) = $x');
}
Output
∛(−27) = -3
AlgebraicNumber powRational(Rational r)

The rational power \mathrm{this}^{r} = (\mathrm{this}^{1/q})^{p} for r = \frac{p}{q}; even q needs this ≥ 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('8^(2/3) = ${AlgebraicNumber.fromInt(8).powRational(Rational.fromInt(2, 3))}');
  print('2^(3/2) ≈ ${AlgebraicNumber.fromInt(2).powRational(Rational.fromInt(3, 2)).toDecimalString(15)}');
}
Output
8^(2/3) = 4
2^(3/2) ≈ 2.828427124746190
sqrtmethod
AlgebraicNumber sqrt()

The non-negative square root (this ≥ 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = AlgebraicNumber.fromInt(3) + AlgebraicNumber.sqrt(Rational.fromInt(8));
  print('√(3 + √8) = 1 + √2: ${a.sqrt() == AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(2))}');
}
Output
√(3 + √8) = 1 + √2: true
absmethod
AlgebraicNumber abs()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('|1 − √2| ≈ ${(AlgebraicNumber.one - AlgebraicNumber.sqrt(Rational.fromInt(2))).abs().toDecimalString(10)}');
}
Output
|1 − √2| ≈ 0.4142135623
compareTomethod
int compareTo(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // √2 + √3 vs π-like rational 22/7: decided exactly.
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)) + AlgebraicNumber.sqrt(Rational.fromInt(3));
  print('compare(√2 + √3, 22/7) = ${a.compareTo(AlgebraicNumber.rational(Rational.fromInt(22, 7)))}');
}
Output
compare(√2 + √3, 22/7) = 1
floormethod
BigInt floor()

The largest integer ≤ this.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('⌊√2·10^20⌋ = ${(AlgebraicNumber.sqrt(Rational.fromInt(2)) * AlgebraicNumber.rational(Rational(BigInt.from(10).pow(20)))).floor()}');
}
Output
⌊√2·10^20⌋ = 141421356237309504880
ceilmethod
BigInt ceil()

The smallest integer ≥ this.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('⌈∛100⌉ = ${AlgebraicNumber.root(Rational.fromInt(100), 3).ceil()}');
}
Output
⌈∛100⌉ = 5
roundmethod
BigInt round()

The nearest integer (ties, which only rationals have, away from zero as Rational.round).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // φ^n rounds to the Lucas number L_n.
  final phi = (AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(5))) * AlgebraicNumber.rational(Rational.fromInt(1, 2));
  print([for (var n = 2; n <= 10; n++) phi.pow(n).round()]);
}
Output
[3, 4, 7, 11, 18, 29, 47, 76, 123]
BigInt scaledFloor(int places)

floor(this · 10^{\mathrm{places}}): the decimal digits, exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('⌊√2 · 10^40⌋ = ${AlgebraicNumber.sqrt(Rational.fromInt(2)).scaledFloor(40)}');
}
Output
⌊√2 · 10^40⌋ = 14142135623730950488016887242096980785696
String toDecimalString(int places)

The decimal expansion truncated (towards -\infty) to places digits after the point.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√2 = ${AlgebraicNumber.sqrt(Rational.fromInt(2)).toDecimalString(50)}');
}
Output
√2 = 1.41421356237309504880168872420969807856967187537694
BigFloat toBigFloat({int prec = 53, RoundingMode rnd = RoundingMode.nearestEven})

The value correctly rounded to a BigFloat of prec bits in the rounding mode rnd.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√2 to 200 bits: ${AlgebraicNumber.sqrt(Rational.fromInt(2)).toBigFloat(prec: 200)}');
}
Output
√2 to 200 bits: 1.4142135623730950488016887242096980785696718753769480731766796
toDoublemethod
double toDouble()

The nearest double.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${AlgebraicNumber.cosPi(Rational.fromInt(1, 7)).toDouble()}');
}
Output
0.9009688679024191
toStringmethod
String toString()

root of <minimal polynomial> in (lo, hi) ≈ <double>, or the rational itself.

toLatexmethod
String toLatex()

LaTeX: rationals, quadratic surds (\frac{1 + \sqrt{5}}{2}), real radicals \sqrt[k]{r}, otherwise the root of the minimal polynomial with its isolating interval.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final phi = (AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(5))) * AlgebraicNumber.rational(Rational.fromInt(1, 2));
  for (final a in [phi, AlgebraicNumber.root(Rational.fromInt(5), 3), AlgebraicNumber.cos2PiOver(7)]) {
    print(a.toLatex());
  }
}
Output
\frac{1 + \sqrt{5}}{2}
\sqrt[3]{5}
\operatorname{root}\left(8 x^{3} + 4 x^{2} - 4 x - 1,\ (0, 2)\right)

Operators

operator -operator
AlgebraicNumber operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2));
  print('√2 − √2 = ${a - a}');
}
Output
√2 − √2 = 0
operator +operator
AlgebraicNumber operator +(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = AlgebraicNumber.sqrt(Rational.fromInt(2)) + AlgebraicNumber.sqrt(Rational.fromInt(3));
  print('√2 + √3: minimal polynomial ${s.minimalPolynomial}');
}
Output
√2 + √3: minimal polynomial x^4 - 10*x^2 + 1
operator -operator
AlgebraicNumber operator -(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('−√5 ≈ ${(-AlgebraicNumber.sqrt(Rational.fromInt(5))).toDecimalString(8)}');
}
Output
−√5 ≈ -2.23606798
operator *operator
AlgebraicNumber operator *(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√2·√8 = ${AlgebraicNumber.sqrt(Rational.fromInt(2)) * AlgebraicNumber.sqrt(Rational.fromInt(8))}');
}
Output
√2·√8 = 4
operator /operator
AlgebraicNumber operator /(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√6/√3 == √2: ${AlgebraicNumber.sqrt(Rational.fromInt(6)) / AlgebraicNumber.sqrt(Rational.fromInt(3)) == AlgebraicNumber.sqrt(Rational.fromInt(2))}');
}
Output
√6/√3 == √2: true
operator <operator
bool operator <(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // √2 + √3 ≈ 3.146 and √10 ≈ 3.162: compared exactly.
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)) + AlgebraicNumber.sqrt(Rational.fromInt(3)), b = AlgebraicNumber.sqrt(Rational.fromInt(10));
  print('√2 + √3 < √10: ${a < b}');
}
Output
√2 + √3 < √10: true
operator <=operator
bool operator <=(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${AlgebraicNumber.sqrt(Rational.fromInt(4)) <= AlgebraicNumber.fromInt(2)}');
}
Output
true
operator >operator
bool operator >(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√(10^12 + 1) > 10^6: ${AlgebraicNumber.sqrt(Rational(BigInt.from(1000000000001))) > AlgebraicNumber.fromInt(1000000)}');
}
Output
√(10^12 + 1) > 10^6: true
operator >=operator
bool operator >=(AlgebraicNumber o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${AlgebraicNumber.cosPi(Rational.fromInt(1, 4)) >= AlgebraicNumber.sinPi(Rational.fromInt(1, 4))}');
}
Output
true
operator ==operator
bool operator ==(Object other)
class

QPoly

final class QPoly

A dense polynomial c_{0} + c_{1}x + \cdots + c_{n}x^{n} with exact Rational coefficients (no trailing zeros; the zero polynomial has no coefficients and degree -1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-6, 11, -6, 1]); // x³ − 6x² + 11x − 6
  print('p = $p');
  print('factor: ${p.factor()}');
  print('real roots: ${p.countRealRoots()}');
  print('p(4) = ${p.evaluate(Rational.fromInt(4))}');
  print('derivative: ${p.derivative()}');
}
Output
p = x^3 - 6*x^2 + 11*x - 6
factor: (1, [(x - 1, 1), (x - 2, 1), (x - 3, 1)])
real roots: 3
p(4) = 6
derivative: 3*x^2 - 12*x + 11

Constructors

QPolyfactory
factory QPoly(List<Rational> coefficients)

From coefficients, constant term first.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = QPoly([Rational.fromInt(1, 2), Rational.zero, Rational.fromInt(-3, 4)]);
  print('p = $p, degree ${p.degree}');
}
Output
p = -(3/4)*x^2 + 1/2, degree 2
factory QPoly.fromInts(List<int> coefficients)

From int coefficients, constant term first.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-1, 0, 0, 1]);
  print('x³ − 1 = $p, factors ${p.irreducibleFactors()}');
}
Output
x³ − 1 = x^3 - 1, factors [x - 1, x^2 + x + 1]
factory QPoly.fromBigInts(List<BigInt> coefficients)

From BigInt coefficients, constant term first.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromBigInts([BigInt.parse('123456789012345678901234567890'), BigInt.one]);
  print(p);
}
Output
x + 123456789012345678901234567890
factory QPoly.constant(Rational c)

The constant polynomial c.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.constant(Rational.fromInt(5, 3))}, degree ${QPoly.constant(Rational.fromInt(5, 3)).degree}');
}
Output
5/3, degree 0
factory QPoly.monomial(Rational c, int k)

c \cdot x^{k}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.monomial(Rational.fromInt(-2), 5)}');
}
Output
-2*x^5
factory QPoly.fromRoots(Iterable<Rational> roots)

The monic polynomial with the given roots, \prod (x - r_{i}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = QPoly.fromRoots([Rational.fromInt(1, 2), Rational.fromInt(-3), Rational.fromInt(2)]);
  print('p = $p');
}
Output
p = x^3 + (1/2)*x^2 - (13/2)*x + 3

Static

zerostaticfield
static final QPoly zero
onestaticfield
static final QPoly one
xstaticfield
static final QPoly x

The polynomial x.

gcdstaticmethod
static QPoly gcd(QPoly a, QPoly b)

The monic greatest common divisor (zero when both are zero).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = QPoly.fromInts([-1, 0, 1]); // x² − 1
  final b = QPoly.fromInts([1, 2, 1]); // x² + 2x + 1
  print('gcd(x² − 1, x² + 2x + 1) = ${QPoly.gcd(a, b)}');
  print('resultant: ${QPoly.resultant(a, b)}');
}
Output
gcd(x² − 1, x² + 2x + 1) = x + 1
resultant: 0
extendedGcdstaticmethod
static (QPoly, QPoly, QPoly) extendedGcd(QPoly a, QPoly b)

The extended gcd: (g, s, t) with s \cdot a + t \cdot b = g monic.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = QPoly.fromInts([-1, 0, 1]), b = QPoly.fromInts([1, 1]);
  final (g, s, t) = QPoly.extendedGcd(a, QPoly.fromInts([2, 1]));
  print('gcd(x² − 1, x + 2) = $g, s = $s, t = $t');
  print('s·a + t·b = ${s * a + t * QPoly.fromInts([2, 1])}');
  print('gcd(x² − 1, x + 1) = ${QPoly.extendedGcd(a, b).$1}');
}
Output
gcd(x² − 1, x + 2) = 1, s = 1/3, t = -(1/3)*x + 2/3
s·a + t·b = 1
gcd(x² − 1, x + 1) = x + 1
resultantstaticmethod
static Rational resultant(QPoly a, QPoly b)

The resultant \operatorname{Res}(a, b), by Collins' subresultant algorithm on the integer primitive parts (Cohen, "A Course in Computational Algebraic Number Theory", Algorithm 3.3.7), rescaled to the rational inputs.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-2, 0, 1]);
  print('Res(x² − 2, 2x) = ${QPoly.resultant(p, p.derivative())}, discriminant ${p.discriminant}');
}
Output
Res(x² − 2, 2x) = -8, discriminant 8

Properties

List<Rational> get coefficients

The coefficients, constant term first (unmodifiable, no trailing zeros).

degreegetter
int get degree

The degree; -1 for the zero polynomial.

isZerogetter
bool get isZero
bool get isConstant

True for constants (including zero).

Rational get leadingCoefficient

The leading coefficient (0 for the zero polynomial).

contentgetter
Rational get content

The positive content: the rational c > 0 such that this / c has coprime integer coefficients.

bool get isSquareFree

True when this has no repeated factor.

Rational get discriminant

The discriminant (-1)^{n(n - 1)/2} \cdot \operatorname{Res}(p, p') / lc.

bool get isIrreducible

True when this is irreducible over Q (degree ≥ 1).

hashCodegetter
int get hashCode

Methods

scalemethod
QPoly scale(Rational s)

Multiplication by a scalar.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([2, 4, 6]).scale(Rational.fromInt(1, 2))}');
}
Output
3*x^2 + 2*x + 1
powmethod
QPoly pow(int k)

The k-th power.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = QPoly.fromInts([1, 1]).pow(6);
  print('(1 + x)⁶ = $p');
}
Output
(1 + x)⁶ = x^6 + 6*x^5 + 15*x^4 + 20*x^3 + 15*x^2 + 6*x + 1
divRemmethod
(QPoly, QPoly) divRem(QPoly d)

Euclidean division: (q, r) with this = q \cdot d + r and deg r < \deg d.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final (q, r) = QPoly.fromInts([5, 0, 3, 1]).divRem(QPoly.fromInts([1, 2]));
  print('q = $q, r = $r');
}
Output
q = (1/2)*x^2 + (5/4)*x - 5/8, r = 45/8
bool isDivisibleBy(QPoly d)

True when d divides this exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-1, 0, 0, 0, 0, 0, 1]); // x⁶ − 1
  print('by x² + x + 1: ${p.isDivisibleBy(QPoly.fromInts([1, 1, 1]))}, by x² + 1: ${p.isDivisibleBy(QPoly.fromInts([1, 0, 1]))}');
}
Output
by x² + x + 1: true, by x² + 1: false
QPoly derivative()

The formal derivative.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([1, -3, 0, 2]);
  print('p = $p, p′ = ${p.derivative()}, p″ = ${p.derivative().derivative()}');
}
Output
p = 2*x^3 - 3*x + 1, p′ = 6*x^2 - 3, p″ = 12*x
evaluatemethod
Rational evaluate(Rational x)

The value at x (Horner).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-2, 0, 1]);
  print('p(99/70) = ${p.evaluate(Rational.fromInt(99, 70))}');
}
Output
p(99/70) = 1/4900
signAtmethod
int signAt(Rational x)

The sign of the value at x (-1, 0, 1), computed on integers without building the rational value.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The sign without building the (huge) rational value.
  final p = QPoly.fromInts([-2, 0, 1]);
  print('sign at 99/70: ${p.signAt(Rational.fromInt(99, 70))}, at 140/99: ${p.signAt(Rational.fromInt(140, 99))}');
}
Output
sign at 99/70: 1, at 140/99: -1
composemethod
QPoly compose(QPoly g)

The composition \operatorname{this}(g(x)).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([0, 0, 1]), g = QPoly.fromInts([1, 1]);
  print('p(g(x)) = ${p.compose(g)}, g(p(x)) = ${g.compose(p)}');
}
Output
p(g(x)) = x^2 + 2*x + 1, g(p(x)) = x^2 + 1
shiftmethod
QPoly shift(Rational c)

p(x + c) (a Taylor shift).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('x³ shifted by 1: ${QPoly.fromInts([0, 0, 0, 1]).shift(Rational.one)}');
}
Output
x³ shifted by 1: x^3 + 3*x^2 + 3*x + 1
QPoly scaleVariable(Rational c)

p(c \cdot x).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([1, 1, 1]).scaleVariable(Rational.fromInt(2))}');
}
Output
4*x^2 + 2*x + 1
reflectmethod
QPoly reflect()

p(-x).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('p(−x) for p = 1 + 2x + 3x²: ${QPoly.fromInts([1, 2, 3]).reflect()}');
}
Output
p(−x) for p = 1 + 2x + 3x²: 3*x^2 - 2*x + 1
reversemethod
QPoly reverse()

The reversal x^{n} \cdot p(\frac{1}{x}) (its roots are the reciprocals).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Roots 2 and 3 become 1/2 and 1/3.
  final p = QPoly.fromRoots([Rational.fromInt(2), Rational.fromInt(3)]);
  print('$p → ${p.reverse()}, roots ${AlgebraicNumber.realRoots(p.reverse())}');
}
Output
x^2 - 5*x + 6 → 6*x^2 - 5*x + 1, roots [1/3, 1/2]
QPoly substitutePower(int k)

p(x^{k}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([1, 1]).substitutePower(3)}');
}
Output
x^3 + 1
monicmethod
QPoly monic()

This divided by its leading coefficient.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([3, 6, 9]).monic()}');
}
Output
x^2 + (2/3)*x + 1/3
primitivemethod
QPoly primitive()

The primitive part with a positive leading coefficient: coprime integer coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly([Rational.fromInt(1, 2), Rational.fromInt(-1, 3), Rational.fromInt(5, 6)]).primitive()}');
}
Output
5*x^2 - 2*x + 3
List<BigInt> integerCoefficients()

The integer coefficients of primitive (constant term first).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly([Rational.fromInt(1, 2), Rational.fromInt(-1, 3), Rational.fromInt(5, 6)]).integerCoefficients()}');
}
Output
[3, -2, 5]
List<QPoly> squareFreeDecomposition()

Yun's square-free decomposition: monic a_{1}, a_{2}, …, a_{k}, pairwise coprime and square-free, with this = lc \cdot a_{1} \cdot a_{2}^{2} \cdot \cdots \cdot a_{k}^{k} (some a_{i} may be 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // (x − 1)(x + 1)²(x − 2)³ = a₁ · a₂² · a₃³.
  final p = QPoly.fromInts([-1, 1]) * QPoly.fromInts([1, 1]).pow(2) * QPoly.fromInts([-2, 1]).pow(3);
  print(p.squareFreeDecomposition());
}
Output
[x - 1, x + 1, x - 2]
QPoly squareFreePart()

The monic square-free part (the product of the distinct monic irreducible factors).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([1, 1]).pow(3) * QPoly.fromInts([0, 1]).pow(2);
  print('$p → ${p.squareFreePart()}');
}
Output
x^5 + 3*x^4 + 3*x^3 + x^2 → x^2 + x
List<QPoly> sturmSequence()

The Sturm sequence p, p', -rem(p, p'), … (each term scaled by a positive rational to a primitive integer polynomial, which keeps the sign pattern).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final s in QPoly.fromInts([-1, -1, 0, 1]).sturmSequence()) print(s);
}
Output
x^3 - x - 1
3*x^2 - 1
2*x + 3
-1
int countRealRoots([Rational? lo, Rational? hi])

The number of distinct real roots in the half-open interval (lo, hi]; the whole line when both are null. One end may be null (-\infty or +\infty).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([1, 0, -4, 0, 1]); // x⁴ − 4x² + 1
  print('real roots: ${p.countRealRoots()}, in (0, 1]: ${p.countRealRoots(Rational.zero, Rational.one)}');
}
Output
real roots: 4, in (0, 1]: 1
List<(Rational, Rational)> isolateRealRoots()

Disjoint intervals (lo, hi], one around each distinct real root, in increasing order. An irrational root satisfies lo < root < hi with p(lo)·p(hi) < 0 for the square-free part p, except that a root which lands on a bisection point r is returned as the degenerate interval (r, r).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([-2, 0, 0, 1]);
  print('x³ − 2: ${p.isolateRealRoots()}');
  print('x⁴ − 10x² + 1: ${QPoly.fromInts([1, 0, -10, 0, 1]).isolateRealRoots()}');
}
Output
x³ − 2: [(-4, 4)]
x⁴ − 10x² + 1: [(-4, -2), (-2, 0), (0, 2), (2, 4)]
factormethod
(Rational, List<(QPoly, int)>) factor()

The factorisation over Q: (unit, [(f_{1}, e_{1}), \ldots]) with this = \mathrm{unit} \cdot \prod f_{i}^{e_{i}}, each f_{i} irreducible over Q, primitive with integer coefficients and a positive leading coefficient, sorted by degree then coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = QPoly.fromInts([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); // x¹² − 1
  final (unit, fs) = p.factor();
  print('unit $unit');
  for (final (f, e) in fs) print('  ($f)^$e');
}
Output
unit 1
  (x + 1)^1
  (x - 1)^1
  (x^2 + 1)^1
  (x^2 + x + 1)^1
  (x^2 - x + 1)^1
  (x^4 - x^2 + 1)^1
List<QPoly> irreducibleFactors()

The distinct irreducible factors (primitive, positive leading coefficient).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QPoly.fromInts([-1, 0, 0, 0, 0, 0, 1]).irreducibleFactors());
}
Output
[x + 1, x - 1, x^2 + x + 1, x^2 - x + 1]
toStringmethod
String toString([String variable = 'x'])

Plain text in the variable variable, highest power first, e.g. x^4 - 10*x^2 + 1.

toLatexmethod
String toLatex([String variable = 'x'])

LaTeX, e.g. x^{4} - 10 x^{2} + 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = QPoly([Rational.one, Rational.fromInt(-1, 2), Rational.zero, Rational.fromInt(3)]);
  print(p.toLatex());
  print(p.toLatex('t'));
}
Output
3 x^{3} - \frac{1}{2} x + 1
3 t^{3} - \frac{1}{2} t + 1

Operators

operator []operator
Rational operator [](int i)

The coefficient of x^{i} (0 beyond the degree).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = QPoly.fromInts([7, 0, -2]);
  print('p[0] = ${p[0]}, p[2] = ${p[2]}, p[9] = ${p[9]}');
}
Output
p[0] = 7, p[2] = -2, p[9] = 0
operator +operator
QPoly operator +(QPoly o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([1, 2, 3]) + QPoly.fromInts([0, -2, 0, 1])}');
}
Output
x^3 + 3*x^2 + 1
operator -operator
QPoly operator -(QPoly o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QPoly.fromInts([1, 2, 3]) - QPoly.fromInts([1, 2, 3])}');
}
Output
0
operator -operator
QPoly operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-QPoly.fromInts([1, -2])}');
}
Output
2*x - 1
operator *operator
QPoly operator *(QPoly o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(x − 1)(x² + x + 1) = ${QPoly.fromInts([-1, 1]) * QPoly.fromInts([1, 1, 1])}');
}
Output
(x − 1)(x² + x + 1) = x^3 - 1
operator ~/operator
QPoly operator ~/(QPoly d)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(x³ − 1) ~/ (x − 1) = ${QPoly.fromInts([-1, 0, 0, 1]) ~/ QPoly.fromInts([-1, 1])}');
}
Output
(x³ − 1) ~/ (x − 1) = x^2 + x + 1
operator %operator
QPoly operator %(QPoly d)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(x³ + 2) % (x² + 1) = ${QPoly.fromInts([2, 0, 0, 1]) % QPoly.fromInts([1, 0, 1])}');
}
Output
(x³ + 2) % (x² + 1) = -x + 2
operator ==operator
bool operator ==(Object other)
final class CliffordAlgebra

The Clifford algebra with generators e_{0} \cdots e_{n - 1}, e_{k}^{2} = \mathrm{metric}[k] \in \{+1, -1, 0\}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Cl(3,0): vectors square to their quadrance; e1e2 is a bivector squaring to −1.
  final cl = CliffordAlgebra.signature(3);
  final e1 = cl.e(0), e2 = cl.e(1);
  print('e1² = ${e1 * e1}, (e1e2)² = ${(e1 * e2) * (e1 * e2)}, e1e2 + e2e1 = ${e1 * e2 + e2 * e1}');
}
Output
e1² = 1, (e1e2)² = -1, e1e2 + e2e1 = 0

Constructors

CliffordAlgebraconstructor
CliffordAlgebra(List<int> metric)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Spacetime algebra: e0² = +1, e1² = e2² = e3² = −1.
  final sta = CliffordAlgebra([1, -1, -1, -1]);
  print('dimension ${sta.dimension}, pseudoscalar² = ${sta.pseudoscalar * sta.pseudoscalar}');
}
Output
dimension 16, pseudoscalar² = -1
factory CliffordAlgebra.signature(int p, [int q = 0, int r = 0])

\operatorname{Cl}(p, q, r): p generators squaring to +1, then q to -1, then r to 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Projective geometric algebra Cl(3,0,1): one null generator.
  final pga = CliffordAlgebra.signature(3, 0, 1);
  print('e3² = ${pga.e(3) * pga.e(3)}, dimension ${pga.dimension}');
}
Output
e3² = 0, dimension 16

Properties

metricfield
final List<int> metric
int get generatorCount
dimensiongetter
int get dimension

2^{n}.

bool get isDegenerate
onegetter
Multivector get one

The scalar 1.

Multivector get pseudoscalar

The unit pseudoscalar e_{0} e_{1} \cdots e_{n - 1}.

late final List<QIMatrix> complexGenerators

Complex matrices for the generators in an irreducible representation of the complexification, of size 2\wedge\lfloor\frac{n}{2}\rfloor (Brauer–Weyl: tensor products of Pauli matrices; generators with e^{2} = -1 get a factor i). For odd n this is one of the two inequivalent irreducible representations (e_{0}\cdots e_{n - 1} acts as a scalar).

Methods

int bladeProductSign(int a, int b)

e_a e_b = sign · e_(a xor b) for basis blades a, b.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e12·e23 sign: ${cl.bladeProductSign(3, 6)}, e1·e1: ${cl.bladeProductSign(1, 1)}');
}
Output
e12·e23 sign: 1, e1·e1: 1
scalarmethod
Multivector scalar(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CliffordAlgebra.signature(2).scalar(Rational.fromInt(3, 4))}');
}
Output
3/4
emethod
Multivector e(int k)

The generator e_{k}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(1, 1);
  print('e0² = ${cl.e(0) * cl.e(0)}, e1² = ${cl.e(1) * cl.e(1)}');
}
Output
e0² = 1, e1² = -1
blademethod
Multivector blade(int mask, [Rational? c])

The basis blade with bitmask mask (coefficient 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('${cl.blade(5)}, ${cl.blade(7, Rational.fromInt(2))}');
}
Output
1*e02, 2*e012
productmethod
Multivector product(List<int> indices)

The blade e_{i_{1}} e_{i_{2}} \cdots (indices in any order, reordered with sign).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e2 e0 e1 = ${cl.product([2, 0, 1])}, e1 e0 = ${cl.product([1, 0])}');
}
Output
e2 e0 e1 = 1*e012, e1 e0 = -1*e01
vectormethod
Multivector vector(List<Rational> v)

\sum v_{k} e_{k}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  final v = cl.vector([Rational.fromInt(1), Rational.fromInt(2), Rational.fromInt(2)]);
  print('v = $v, v² = ${v * v} (its quadrance)');
}
Output
v = 1*e0 + 2*e1 + 2*e2, v² = 9 (its quadrance)
bladeNamemethod
String bladeName(int mask)

Name of a blade, e.g. e013, 1 for the scalar.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(4);
  print([for (final m in [0, 1, 3, 11, 15]) cl.bladeName(m)]);
}
Output
[1, e0, e01, e013, e0123]
QIMatrix leftRegular(Multivector m)

The left-regular representation: the 2^{n} \times 2^{n} matrix of x \mapsto m x on the blade basis. Faithful for every signature (degenerate ones included).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(2);
  print(cl.leftRegular(cl.e(0)));
}
Output
0	1	0	0
1	0	0	0
0	0	0	1
0	0	1	0
representmethod
QIMatrix represent(Multivector x)

The image of a multivector in complexGenerators' representation.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Cl(3,0) in 2 × 2 complex matrices (the Pauli matrices).
  final cl = CliffordAlgebra.signature(3);
  print('${cl.represent(cl.e(0))}\n—\n${cl.represent(cl.e(1))}');
}
Output
0	1
1	0
—
0	-i
i	0
toStringmethod
String toString()
final class Multivector

An element of a CliffordAlgebra with exact rational coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // A rotor in the e1e2 plane: R = (1 + e2e1)/… rotates e1 to e2 by sandwiching, exactly.
  final cl = CliffordAlgebra.signature(3);
  final r = cl.one + cl.e(1) * cl.e(0);
  print('R e1 R⁻¹ = ${r.sandwich(cl.e(0))}');
}
Output
R e1 R⁻¹ = 1*e1

Properties

algebrafield
final CliffordAlgebra algebra
termsfield
final Map<int, Rational> terms

Blade bitmask → nonzero coefficient.

Rational get scalarPart

The scalar part \langle x\rangle_{0}.

isZerogetter
bool get isZero
reversegetter
Multivector get reverse

Reversion \tilde{x}: (-1)^{k(k - 1)/2} on grade k.

Multivector get gradeInvolution

Grade involution \hat{x}: (-1)^{k} on grade k.

Multivector get cliffordConjugate

Clifford conjugate \bar{x} = reverse of the grade involution: (-1)^{k(k + 1)/2}.

evengetter
Multivector get even

The even part.

gradesgetter
Set<int> get grades

Grades present.

isScalargetter
bool get isScalar
Multivector get normSquared

X x̃ (scalar for versors).

Multivector get versorInverse

The inverse of a versor (or any x with x x̃ scalar and nonzero): x̃ / (x x̃).

inversegetter
Multivector get inverse

The general inverse via the left-regular representation (exact; throws if singular).

dualgetter
Multivector get dual

The dual x I^{-1} (non-degenerate algebras).

hashCodegetter
int get hashCode

Methods

Rational coefficient(int blade)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  final m = cl.scalar(Rational.fromInt(2)) + cl.e(0) * cl.e(1);
  print('coefficient of e01: ${m.coefficient(3)}, scalar part ${m.scalarPart}');
}
Output
coefficient of e01: 1, scalar part 2
scalemethod
Multivector scale(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(2);
  print('${(cl.e(0) + cl.e(1)).scale(Rational.fromInt(1, 2))}');
}
Output
1/2*e0 + 1/2*e1
Multivector leftContraction(Multivector o)

The left contraction x ⌋ y (\operatorname{grade}(y) - \operatorname{grade}(x) parts of blade products with a \subseteq b).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e1 ⌋ e12 = ${cl.e(0).leftContraction(cl.e(0) * cl.e(1))}');
}
Output
e1 ⌋ e12 = 1*e1
Multivector rightContraction(Multivector o)

The right contraction x ⌊ y.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e12 ⌊ e2 = ${(cl.e(0) * cl.e(1)).rightContraction(cl.e(1))}');
}
Output
e12 ⌊ e2 = 1*e0
Rational scalarProduct(Multivector o)

The scalar product \langle x y\rangle_{0}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  final u = cl.vector([Rational.fromInt(1), Rational.fromInt(2), Rational.zero]), v = cl.vector([Rational.fromInt(3), Rational.fromInt(-1), Rational.fromInt(5)]);
  print('⟨uv⟩₀ = ${u.scalarProduct(v)}');
}
Output
⟨uv⟩₀ = 1
dotmethod
Multivector dot(Multivector o)

Hestenes' inner product: grade |r - s| parts of products of nonscalar blades.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e1 · e12 = ${cl.e(0).dot(cl.e(0) * cl.e(1))}');
}
Output
e1 · e12 = 1*e1
Multivector commutatorProduct(Multivector o)

The commutator product \frac{xy - yx}{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e12 × e23 = ${(cl.e(0) * cl.e(1)).commutatorProduct(cl.e(1) * cl.e(2))}');
}
Output
e12 × e23 = 1*e02
grademethod
Multivector grade(int k)

\langle x\rangle_{k}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  final m = cl.one + cl.e(0) + cl.e(0) * cl.e(1) + cl.pseudoscalar;
  for (var k = 0; k <= 3; k++) print('grade $k: ${m.grade(k)}');
}
Output
grade 0: 1
grade 1: 1*e0
grade 2: 1*e01
grade 3: 1*e012
sandwichmethod
Multivector sandwich(Multivector x)

The sandwich v \times v^{-1} (v a versor), e.g. a rotation by a rotor.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Reflection of a vector in the plane perpendicular to e1: −e1 v e1⁻¹ (here the sandwich gives the reflection up to sign).
  final cl = CliffordAlgebra.signature(3);
  final v = cl.vector([Rational.fromInt(1), Rational.fromInt(2), Rational.fromInt(3)]);
  print('e1 v e1⁻¹ = ${cl.e(0).sandwich(v)}');
}
Output
e1 v e1⁻¹ = 1*e0 - 2*e1 - 3*e2
toStringmethod
String toString()

Operators

operator +operator
Multivector operator +(Multivector o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(2);
  print('${cl.one + cl.e(0)}');
}
Output
1 + 1*e0
operator -operator
Multivector operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(2);
  print('${cl.e(0) * cl.e(1) - cl.e(1) * cl.e(0)}');
}
Output
2*e01
operator -operator
Multivector operator -(Multivector o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(2);
  print('${-(cl.one + cl.e(1))}');
}
Output
-1 - 1*e1
operator *operator
Multivector operator *(Multivector o)

The geometric product.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final cl = CliffordAlgebra.signature(3);
  final u = cl.vector([Rational.fromInt(1), Rational.fromInt(0), Rational.fromInt(0)]), v = cl.vector([Rational.fromInt(1), Rational.fromInt(1), Rational.fromInt(0)]);
  print('uv = ${u * v} (scalar u·v plus bivector u∧v)');
}
Output
uv = 1 + 1*e01 (scalar u·v plus bivector u∧v)
operator ^operator
Multivector operator ^(Multivector o)

The outer (wedge) product.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cl = CliffordAlgebra.signature(3);
  print('e1 ∧ e2 = ${cl.e(0) ^ cl.e(1)}, e1 ∧ e1 = ${cl.e(0) ^ cl.e(0)}');
}
Output
e1 ∧ e2 = 1*e01, e1 ∧ e1 = 0
operator ==operator
bool operator ==(Object other)
enum GammaBasis

Representation basis of the gamma matrices.

Values

diracvalue
dirac

\gamma^{0} = \operatorname{diag}(1, 1, -1, -1) (Dirac–Pauli; standard for the non-relativistic limit and hydrogen).

weylvalue
weyl

\gamma^{0} off-diagonal, \gamma^{5} = \operatorname{diag}(-1, -1, 1, 1): left- and right-handed Weyl spinors are the upper and lower halves (Peskin & Schroeder).

majorana

All \gamma^{\mu} purely imaginary in mostly-minus (real in mostly-plus), so the Dirac equation is real and Majorana spinors are real.

enum MetricSignature

Metric signature convention.

Values

mostlyMinus

\eta = \operatorname{diag}(+1, -1, -1, -1) (particle physics).

mostlyPlus

\eta = \operatorname{diag}(-1, +1, +1, +1) (general relativity, Srednicki, Weinberg); \gamma^{\mu} = i × the mostly-minus matrices.

final class GammaMatrices

A set of gamma matrices \gamma^{\mu} (upper index), \mu = 0 \cdots 3, with \{\gamma^{\mu}, \gamma^{\nu}\} = 2\eta^{\mu \nu}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Dirac matrices over ℚ(i), exactly: {γ^μ, γ^ν} = 2η^μν.
  final g = GammaMatrices();
  print('{γ⁰, γ⁰} = 2I: ${g.upper[0].anticommutator(g.upper[0]) == QIMatrix.identity(4).scaleInt(2)}');
  print('{γ¹, γ¹} = −2I: ${g.upper[1].anticommutator(g.upper[1]) == QIMatrix.identity(4).scaleInt(-2)}');
}
Output
{γ⁰, γ⁰} = 2I: true
{γ¹, γ¹} = −2I: true

Constructors

factory GammaMatrices({GammaBasis basis = GammaBasis.dirac, MetricSignature signature = MetricSignature.mostlyMinus})
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final w = GammaMatrices(basis: GammaBasis.weyl);
  print('γ⁵ in the Weyl basis:\n${w.gamma5}');
}
Output
γ⁵ in the Weyl basis:
-1	0	0	0
0	-1	0	0
0	0	1	0
0	0	0	1

Properties

basisfield
final GammaBasis basis
final MetricSignature signature
upperfield
final List<QIMatrix> upper

\gamma^{\mu}.

metricgetter
List<int> get metric

\eta^{\mu \nu} = \eta_{\mu \nu} (diagonal \pm1).

lowergetter
List<QIMatrix> get lower

\gamma_{\mu} = \eta_{\mu \nu} \gamma^{\nu}.

gamma5getter
QIMatrix get gamma5

\gamma^{5} = i \gamma^{0}\gamma^{1}\gamma^{2}\gamma^{3} (the same matrix in both signatures): Hermitian, squares to 1, anticommutes with every \gamma^{\mu}.

QIMatrix get projectorLeft

Chiral projectors P_{L} = \frac{1 - \gamma^{5}}{2}, P_{R} = \frac{1 + \gamma^{5}}{2}.

QIMatrix get projectorRight
List<QIMatrix> get cliffordBasis

The 16 basis elements \Gamma_{A} = products \gamma^{\mu1}\cdots\gamma^{\mu k} for \mu1 < \cdots < \mu k (A a bitmask), a basis of all 4 \times 4 matrices.

QIMatrix get chargeConjugation

The charge-conjugation matrix C with C \gamma^{\mu} C^{-1} = -(\gamma^{\mu})^{T}, unitary, normalised with its first nonzero entry 1 (Dirac basis: \propto i\gamma^{2}\gamma^{0}).

Methods

sigmamethod
QIMatrix sigma(int mu, int nu)

\sigma^{\mu \nu} = (\frac{i}{2})[\gamma^{\mu}, \gamma^{\nu}].

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(GammaMatrices().sigma(1, 2));
}
Output
1	0	0	0
0	-1	0	0
0	0	1	0
0	0	0	-1
QIMatrix lorentzGenerator(int mu, int nu)

The Lorentz generators of the Dirac representation, S^{\mu \nu} = \sigma^{\mu \nu} / 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(GammaMatrices().lorentzGenerator(0, 1));
}
Output
0	0	0	1/2i
0	0	1/2i	0
0	1/2i	0	0
1/2i	0	0	0
QIMatrix basisChange(GammaMatrices other)

The matrix S with \gamma'^{\mu} = S \gamma^{\mu} S^{-1} taking spinors of this basis to other's: ψ' = S \psi (same signature).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final d = GammaMatrices(), m = GammaMatrices(basis: GammaBasis.majorana);
  final s = d.basisChange(m);
  print('maps γ² over: ${s * d.upper[2] * s.inverse == m.upper[2]}');
}
Output
maps γ² over: true
slashmethod
CMatrix slash(List<double> p)

Feynman slash \not{p} = \gamma^{\mu} p_{\mu} = \gamma^{\mu} \eta_{\mu \nu} p^{\nu} for a contravariant p^{\nu} (double precision).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // p̸² = p² for p = (E, p): here (5, 0, 0, 3), mass² 16.
  final g = GammaMatrices();
  final ps = g.slash([5, 0, 0, 3]);
  print('p̸² = 16 I: ${(ps * ps).approxEquals(CMatrix.identity(4).scaleReal(16))}');
}
Output
p̸² = 16 I: true
QIMatrix slashExact(List<Rational> p)

Exact \not{p} for rational p^{\nu}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = GammaMatrices();
  final ps = g.slashExact([Rational.fromInt(5), Rational.zero, Rational.zero, Rational.fromInt(3)]);
  print('p̸² = ${(ps * ps).scalarValue} · I, exactly');
}
Output
p̸² = 16 · I, exactly
dotmethod
double dot(List<double> a, List<double> b)

Minkowski product a \cdot b = \eta_{\mu \nu} a^{\mu} b^{\nu}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GammaMatrices().dot([5, 0, 0, 3], [5, 0, 0, 3])}');
}
Output
16.0
int traceFormula(List<int> indices)

\operatorname{Tr}(\gamma^{\mu1} \cdots \gamma^{\mu n}) by the recursion \operatorname{Tr}(\gamma^{a} \gamma^{\mu2}\cdots) = \sum_{k} (-1)^{k} \eta^{a \mu k} \operatorname{Tr}(… without a, \mu k \cdots), \operatorname{Tr} 1 = 4.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Tr(γ^μγ^νγ^ργ^σ) = 4(η^μν η^ρσ − η^μρ η^νσ + η^μσ η^νρ).
  final g = GammaMatrices();
  print('Tr(γ⁰γ⁰γ¹γ¹) = ${g.traceFormula([0, 0, 1, 1])}, Tr(γ⁰γ¹) = ${g.traceFormula([0, 1])}');
}
Output
Tr(γ⁰γ⁰γ¹γ¹) = -4, Tr(γ⁰γ¹) = 0
tracemethod
GaussianRational trace(List<int> indices)

Exact trace of a product of \gamma^{\mu} by matrix multiplication.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GammaMatrices().trace([0, 0, 1, 1])} (by matrix multiplication; matches the formula)');
}
Output
-4 (by matrix multiplication; matches the formula)
adjointmethod
List<Complex> adjoint(List<Complex> psi)

The Dirac adjoint \bar{\psi} = \psi^{\dagger}\gamma^{0} (as a row).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GammaMatrices().adjoint([Complex(1, 0), Complex(0, 1), Complex(0, 0), Complex(2, 0)])}');
}
Output
[(1.0+0.0j), (0.0-1.0j), (0.0+0.0j), (-2.0+0.0j)]
bilinearmethod
Complex bilinear(List<Complex> psi, CMatrix m, List<Complex> chi)

\bar{\psi} M \chi.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = GammaMatrices();
  final psi = [Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0)];
  print('ψ̄ψ = ${g.bilinear(psi, CMatrix.identity(4), psi)}');
}
Output
ψ̄ψ = (1.0+0.0j)
bilinearsmethod
SpinorBilinears bilinears(List<Complex> psi)

The five bilinear covariants of a Dirac spinor (mostly-minus): scalar \bar{\psi}\psi, pseudoscalar \bar{\psi}i\gamma^{5}\psi, vector \bar{\psi}\gamma^{\mu}\psi, axial \bar{\psi}\gamma^{\mu}\gamma^{5}\psi, tensor \bar{\psi}\sigma^{\mu \nu}\psi.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final b = GammaMatrices().bilinears([Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0)]);
  print('scalar ${b.scalar}, pseudoscalar ${b.pseudoscalar}, vector ${b.vector}');
}
Output
scalar 1.0, pseudoscalar 0.0, vector [1.0, 0.0, 0.0, 0.0]
List<Complex> chargeConjugate(List<Complex> psi)

The charge conjugate \psi^{c} = C \bar{\psi}^{T} = C \gamma^{0T} \psi* (mostly-minus).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GammaMatrices().chargeConjugate([Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0)])}');
}
Output
[(0.0+0.0j), (0.0+0.0j), (0.0+0.0j), (-1.0+0.0j)]
CMatrix spinorBoost(double rapidity, List<double> axis)

The Dirac-spinor representation of a Lorentz boost with rapidity \eta along n: S = \exp(\tfrac{1}{2} \eta n_{i} \gamma^{0}\gamma^{i}), so S^{-1} \gamma^{\mu} S = \Lambda^{\mu}_{\nu} \gamma^{\nu} with \Lambda = lorentzBoost (mostly-minus).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(GammaMatrices().spinorBoost(0.5, [0, 0, 1]));
}
Output
1.0314130998795732+0.0i 0.0+0.0i 0.2526123168081683+0.0i 0.0+0.0i
0.0+0.0i 1.0314130998795732+0.0i 0.0+0.0i -0.2526123168081683+0.0i
0.2526123168081683+0.0i 0.0+0.0i 1.031413099879573+0.0i 0.0+0.0i
0.0+0.0i -0.2526123168081683+0.0i 0.0+0.0i 1.031413099879573+0.0i
CMatrix spinorRotation(double theta, List<double> axis)

The Dirac-spinor representation of a rotation by \theta about n: S = \exp(\tfrac{1}{2} \theta (n_{z} \gamma^{1}\gamma^{2} + n_{x} \gamma^{2}\gamma^{3} + n_{y} \gamma^{3}\gamma^{1})). \theta = 2\pi gives -1 (the 720^{\circ} property).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // A 2π rotation of a Dirac spinor is −1.
  print('${GammaMatrices().spinorRotation(2 * 3.141592653589793, [0, 0, 1]).approxEquals(CMatrix.identity(4).scaleReal(-1))}');
}
Output
true
final class SpinorBilinears

The bilinear covariants of a Dirac spinor.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final b = GammaMatrices().bilinears([Complex(1, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0)]);
  print('σ ${b.scalar}, π ${b.pseudoscalar}, J ${b.vector}, A ${b.axial}');
}
Output
σ 0.0, π 0.0, J [2.0, 0.0, 0.0, 2.0], A [2.0, 0.0, 0.0, 2.0]

Constructors

SpinorBilinearsconstructor
SpinorBilinears({required this.scalar, required this.pseudoscalar, required this.vector, required this.axial, required this.tensor})
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final b = SpinorBilinears(scalar: 1, pseudoscalar: 0, vector: [1, 0, 0, 0], axial: [0, 0, 0, 1], tensor: List.generate(4, (_) => List.filled(4, 0.0)));
  print('${b.scalar}, ${b.vector}');
}
Output
1.0, [1.0, 0.0, 0.0, 0.0]

Properties

scalarfield
final double scalar

\sigma = \bar{\psi}\psi.

final double pseudoscalar

\omega = \bar{\psi} i\gamma^{5} \psi.

vectorfield
final List<double> vector

J^{\mu} = \bar{\psi}\gamma^{\mu}\psi.

axialfield
final List<double> axial

K^{\mu} = \bar{\psi}\gamma^{\mu}\gamma^{5}\psi.

tensorfield
final List<List<double>> tensor

S^{\mu \nu} = \bar{\psi}\sigma^{\mu \nu}\psi.

final class Cyclotomic

An exact element of the cyclotomic field Q(\zeta_{n}), \zeta_{n} = e^{2\pi i/n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Exact arithmetic in ℚ(ζ_n): ζ₅ + ζ₅⁴ = 2cos(2π/5) = (√5 − 1)/2.
  final z = Cyclotomic.rootOfUnity(5);
  final c = z + z.galois(4);
  print('ζ + ζ⁴ = $c, real ${c.isReal}, ≈ ${c.toComplex().$1}');
  print('equals (√5 − 1)/2: ${c == (Cyclotomic.sqrt(5) - Cyclotomic.integer(1)).scale(Rational.fromInt(1, 2))}');
}
Output
ζ + ζ⁴ = -1 - z5^2 - z5^3, real true, ≈ 0.6180339887498948
equals (√5 − 1)/2: true

Constructors

factory Cyclotomic.fromTerms(int n, Map<int, Rational> terms)

\sum_{k} \operatorname{terms}[k] \cdot \zeta_{n}^{k} (exponents taken mod n).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 1 + ζ + ζ² + ζ³ + ζ⁴ = 0 in ℚ(ζ₅).
  final s = Cyclotomic.fromTerms(5, {0: Rational.one, 1: Rational.one, 2: Rational.one, 3: Rational.one, 4: Rational.one});
  print('sum of the fifth roots of unity: $s');
}
Output
sum of the fifth roots of unity: 0
factory Cyclotomic.rational(Rational r)

The rational number r, viewed in Q(\zeta_{1}) = Q.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Cyclotomic.rational(Rational.fromInt(3, 7))}');
}
Output
3/7
factory Cyclotomic.integer(int k)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Cyclotomic.integer(4) * Cyclotomic.rootOfUnity(3)}');
}
Output
4*z3
factory Cyclotomic.rootOfUnity(int n, [int k = 1])

\zeta_{n}^{k}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final w = Cyclotomic.rootOfUnity(3);
  print('ω = $w, ω³ = ${w * w * w}, 1 + ω + ω² = ${Cyclotomic.integer(1) + w + w * w}');
}
Output
ω = z3, ω³ = 1, 1 + ω + ω² = 0
factory Cyclotomic.sqrt(int d)

\sqrt{d} for an integer d (Gauss sums: \sqrt{p} = \sum (\frac{a}{p}) \zeta_{p}^{a} for p \equiv 1 \bmod 4, i\sqrt{p} for p \equiv 3 \bmod 4), so quadratic irrationalities like \frac{1 + \sqrt{5}}{2} are exact cyclotomic numbers.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // √5 from a Gauss sum, √−3 = 2ω + 1.
  final r = Cyclotomic.sqrt(5);
  print('√5 = $r, squared ${r * r}');
  print('√−3 squared ${Cyclotomic.sqrt(-3) * Cyclotomic.sqrt(-3)}');
}
Output
√5 = -1 - 2*z5^2 - 2*z5^3, squared 5
√−3 squared -3

Properties

orderfield
final int order

The field is Q(\zeta_{\mathrm{order}}); the element may lie in a smaller subfield.

List<Rational> get coefficients

Coefficients on 1, \zeta, …, \zeta^{\varphi(n) - 1}.

conjugategetter
Cyclotomic get conjugate

Complex conjugate (\sigma_{-1}).

inversegetter
Cyclotomic get inverse

The inverse, as the product of the other Galois conjugates divided by the (rational) norm.

isZerogetter
bool get isZero
bool get isRational

Whether the element is rational (only the constant coefficient survives reduction).

isRealgetter
bool get isReal

Whether the element is real (equal to its complex conjugate).

hashCodegetter
int get hashCode

Methods

liftmethod
Cyclotomic lift(int m)

The same element as a polynomial in \zeta_{m} (n must divide m): \zeta_{n} = \zeta_{m}^{m/n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = Cyclotomic.rootOfUnity(4);
  print('i in ℚ(ζ₁₂): ${i.lift(12)}');
}
Output
i in ℚ(ζ₁₂): z12^3
scalemethod
Cyclotomic scale(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Cyclotomic.rootOfUnity(8).scale(Rational.fromInt(1, 2))}');
}
Output
1/2*z8
galoismethod
Cyclotomic galois(int k)

The Galois automorphism \sigma_{k}: \zeta \mapsto \zeta^{k} (k coprime to the order).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final r = Cyclotomic.sqrt(5);
  print('σ_2(√5) = ${r.galois(2)} (= −√5: ${r.galois(2) == -r})');
}
Output
σ_2(√5) = 1 + 2*z5^2 + 2*z5^3 (= −√5: true)
Rational toRational()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final z = Cyclotomic.rootOfUnity(7);
  var s = Cyclotomic.integer(0);
  for (var k = 1; k < 7; k++) s = s + z.galois(k);
  print('ζ + ζ² + … + ζ⁶ = ${s.toRational()}');
}
Output
ζ + ζ² + … + ζ⁶ = -1
toComplexmethod
(double, double) toComplex()

Numerical value (re, im).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('ζ₈ ≈ ${Cyclotomic.rootOfUnity(8).toComplex()}');
}
Output
ζ₈ ≈ (0.7071067811865476, 0.7071067811865475)
toStringmethod
String toString()

2, -1/2 + 1/2*z3^1, … with zN = \zeta_{N}.

Operators

operator +operator
Cyclotomic operator +(Cyclotomic o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Cyclotomic.rootOfUnity(4) + Cyclotomic.integer(1)}');
}
Output
1 + z4
operator -operator
Cyclotomic operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Cyclotomic.rootOfUnity(4) - Cyclotomic.rootOfUnity(4)}');
}
Output
0
operator -operator
Cyclotomic operator -(Cyclotomic o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-Cyclotomic.rootOfUnity(6)}');
}
Output
-z6
operator *operator
Cyclotomic operator *(Cyclotomic o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = Cyclotomic.rootOfUnity(4);
  print('i·i = ${i * i}, (1 + i)² = ${(Cyclotomic.integer(1) + i) * (Cyclotomic.integer(1) + i)}');
}
Output
i·i = -1, (1 + i)² = 2*z4
operator /operator
Cyclotomic operator /(Cyclotomic o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = Cyclotomic.rootOfUnity(4);
  print('1/(1 + i) = ${Cyclotomic.integer(1) / (Cyclotomic.integer(1) + i)}');
}
Output
1/(1 + i) = 1/2 - 1/2*z4
operator ==operator
bool operator ==(Object other)
class

RtPoint

final class RtPoint<T>

A point [x, y] with coordinates in a field.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final p = RtPoint<Rational>(r(1, 2), r(3));
  print('p = $p, x ${p.x}, y ${p.y}');
}
Output
p = [1/2, 3], x 1/2, y 3

Constructors

RtPointconstructor
const RtPoint(this.x, this.y)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = RtPoint<int>(3, 5);
  print('a point of GF(7)²: $a, quadrance to (0,0) ${RtPlane.finite(7).quadrance(a, RtPoint<int>(0, 0))}');
}
Output
a point of GF(7)²: [3, 5], quadrance to (0,0) 6

Properties

xfield
final T x
yfield
final T y
hashCodegetter
int get hashCode

Methods

toStringmethod
String toString()

Operators

operator ==operator
bool operator ==(Object other)
class

RtLine

final class RtLine<T>

The line \langle a:b:c\rangle, i.e. ax + by + c = 0, with (a, b) \ne (0, 0). Proportional triples are the same line; compare with RtPlane.sameLine.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = rt.line(r(1), r(-1), r(0));
  print('l = $l (the line y = x), a ${l.a}, b ${l.b}, c ${l.c}');
}
Output
l = ⟨1 : -1 : 0⟩ (the line y = x), a 1, b -1, c 0

Constructors

RtLineconstructor
const RtLine(this.a, this.b, this.c)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = RtLine<Rational>(r(2), r(3), r(-6));
  print('$l, (3, 0) on it: ${rt.liesOn(rt.pointInt(3, 0), l)}');
}
Output
⟨2 : 3 : -6⟩, (3, 0) on it: true

Properties

afield
final T a
bfield
final T b
cfield
final T c
hashCodegetter
int get hashCode

Methods

toStringmethod
String toString()

Operators

operator ==operator
bool operator ==(Object other)
final class RtMeasures<T>

The six measures of a triangle: quadrances Q_{1}, Q_{2}, Q_{3} (Q_{i} opposite vertex i) and spreads s_{1}, s_{2}, s_{3} (s_{i} at vertex i).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final m = rt.measures(rt.pointInt(0, 0), rt.pointInt(4, 0), rt.pointInt(0, 3));
  print('Q: ${m.q1}, ${m.q2}, ${m.q3}; s: ${m.s1}, ${m.s2}, ${m.s3}');
}
Output
Q: 25, 9, 16; s: 1, 9/25, 16/25

Constructors

RtMeasuresconstructor
const RtMeasures(this.q1, this.q2, this.q3, this.s1, this.s2, this.s3)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final m = RtMeasures<Rational>(r(9), r(16), r(25), r(9, 25), r(16, 25), r(1));
  print('the 3-4-5 measures satisfy every law: ${rt.satisfiesLaws(m)}');
}
Output
the 3-4-5 measures satisfy every law: true

Properties

q1field
final T q1
q2field
final T q2
q3field
final T q3
s1field
final T s1
s2field
final T s2
s3field
final T s3
List<T> get quadrances
spreadsgetter
List<T> get spreads
hashCodegetter
int get hashCode

Methods

toStringmethod
String toString()

Operators

operator ==operator
bool operator ==(Object other)
class

RtPlane

final class RtPlane<T>

The affine plane F^{2} over a field, with Wildberger's measures, constructions and the five main laws. Every method is exact: no transcendental functions (the solvers that need a square root use ScalarField.sqrt and return only the solutions that exist in F).

The laws need characteristic ≠ 2; PrimeField only allows odd primes.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const plane = RtPlane.rational;
  final a = plane.pointInt(0, 0), b = plane.pointInt(4, 0), c = plane.pointInt(0, 3);
  final m = plane.measures(a, b, c);
  print('quadrances: ${m.q1}, ${m.q2}, ${m.q3}');
  print('spreads:    ${m.s1}, ${m.s2}, ${m.s3}');
  // The right angle is at vertex 1, so Q2 + Q3 = Q1.
  print('Pythagoras Q2 + Q3 = Q1: ${plane.pythagorasHolds(m.q2, m.q3, m.q1)}');
}
Output
quadrances: 25, 9, 16
spreads:    1, 9/25, 16/25
Pythagoras Q2 + Q3 = Q1: true

Constructors

RtPlaneconstructor
const RtPlane(this.field)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = RtPlane<int>(PrimeField(13));
  print('GF(13): Q((0,0), (3,4)) = ${f.quadrance(f.pointInt(0, 0), f.pointInt(3, 4))}');
}
Output
GF(13): Q((0,0), (3,4)) = 12

Static

rationalconstant
static const RtPlane<Rational> rational = RtPlane<Rational>(RationalField())

Rational trigonometry over Q.

algebraicconstant
static const RtPlane<AlgebraicNumber> algebraic = RtPlane<AlgebraicNumber>(AlgebraicField())

Over the real algebraic numbers (every real square root exists).

finitestaticmethod
static RtPlane<int> finite(int p)

Over GF(p), p an odd prime.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The same laws over the field of 13 elements: geometry with no decimals at all.
  final plane = RtPlane.finite(13);
  final m = plane.measures(plane.pointInt(0, 0), plane.pointInt(3, 1), plane.pointInt(4, 1));
  print('over F13: Q = ${m.q1}, ${m.q2}, ${m.q3}; s = ${m.s1}, ${m.s2}, ${m.s3}');
  print('laws hold: ${plane.satisfiesLaws(m)}');
}
Output
over F13: Q = 1, 4, 10; s = 1, 4, 10
laws hold: true

Properties

fieldfield
final ScalarField<T> field

Methods

pointmethod
RtPoint<T> point(T x, T y)

The point [x, y].

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.point(r(1, 3), r(-2))}');
}
Output
[1/3, -2]
pointIntmethod
RtPoint<T> pointInt(int x, int y)

The point with integer coordinates mapped into F.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = RtPlane.finite(5);
  print('(7, -1) in GF(5)²: ${f.pointInt(7, -1)}');
}
Output
(7, -1) in GF(5)²: [2, 4]
RtPoint<T> pointRational(Rational x, Rational y)

The point with rational coordinates mapped into F.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final f = RtPlane.finite(7);
  print('(1/2, 1/3) in GF(7)²: ${f.pointRational(r(1, 2), r(1, 3))}');
}
Output
(1/2, 1/3) in GF(7)²: [4, 5]
linemethod
RtLine<T> line(T a, T b, T c)

The line \langle a:b:c\rangle (throws when a = b = 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.line(r(3), r(4), r(-25))}  (3x + 4y = 25)');
}
Output
⟨3 : 4 : -25⟩  (3x + 4y = 25)
quadrancemethod
T quadrance(RtPoint<T> p, RtPoint<T> q)

The quadrance Q(A, B) = (x_{2} - x_{1})^{2} + (y_{2} - y_{1})^{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const plane = RtPlane.rational;
  final a = plane.pointInt(1, 2), b = plane.pointInt(4, 6);
  print('quadrance = ${plane.quadrance(a, b)}');
  print('midpoint  = ${plane.midpoint(a, b)}');
}
Output
quadrance = 25
midpoint  = [5/2, 4]
isNullmethod
bool isNull(RtLine<T> l)

True for a null line (a^{2} + b^{2} = 0; perpendicular to itself). Only exists when -1 is a square in F.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // Over GF(5), −1 = 4 = 2², so the line ⟨2 : −1 : 0⟩ is null: perpendicular to itself.
  final f = RtPlane.finite(5);
  print('null in GF(5): ${f.isNull(f.line(2, 4, 0))}; over ℚ ⟨1 : 2 : 0⟩: ${rt.isNull(rt.line(r(1), r(2), r(0)))}');
}
Output
null in GF(5): true; over ℚ ⟨1 : 2 : 0⟩: false
spreadmethod
T spread(RtLine<T> l1, RtLine<T> l2)

The spread s(l_{1}, l_{2}) = (a_{1}b_{2} - a_{2}b_{1})^{2} / ((a_{1}^{2} + b_{1}^{2})(a_{2}^{2} + b_{2}^{2})); throws for null lines.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const plane = RtPlane.rational;
  final o = plane.pointInt(0, 0);
  final l1 = plane.join(o, plane.pointInt(1, 0));
  final l2 = plane.join(o, plane.pointInt(1, 1));
  final l3 = plane.join(o, plane.pointInt(0, 1));
  print('spread(x-axis, diagonal) = ${plane.spread(l1, l2)}');
  print('spread(x-axis, y-axis)   = ${plane.spread(l1, l3)}');
  print('cross(x-axis, diagonal)  = ${plane.cross(l1, l2)}');
}
Output
spread(x-axis, diagonal) = 1/2
spread(x-axis, y-axis)   = 1
cross(x-axis, diagonal)  = 1/2
crossmethod
T cross(RtLine<T> l1, RtLine<T> l2)

The cross c(l_{1}, l_{2}) = (a_{1}a_{2} + b_{1}b_{2})^{2} / ((a_{1}^{2} + b_{1}^{2})(a_{2}^{2} + b_{2}^{2})) = 1 - s.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = rt.line(r(0), r(1), r(0)), m = rt.line(r(4), r(-3), r(0));
  print('c = ${rt.cross(l, m)}, s = ${rt.spread(l, m)}, sum ${rt.cross(l, m) + rt.spread(l, m)}');
}
Output
c = 9/25, s = 16/25, sum 1
twistmethod
T twist(RtLine<T> l1, RtLine<T> l2)

The twist t(l_{1}, l_{2}) = s / c = (a_{1}b_{2} - a_{2}b_{1})^{2} / (a_{1}a_{2} + b_{1}b_{2})^{2}; throws for perpendicular lines.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = rt.line(r(0), r(1), r(0)), m = rt.line(r(4), r(-3), r(0));
  print('t = ${rt.twist(l, m)} (tan² of the angle, exactly)');
}
Output
t = 16/9 (tan² of the angle, exactly)
joinmethod
RtLine<T> join(RtPoint<T> p, RtPoint<T> q)

The line through two distinct points: \langle y_{1} - y_{2}: x_{2} - x_{1}: x_{1}y_{2} - x_{2}y_{1}\rangle.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = rt.join(rt.pointInt(1, 2), rt.pointInt(4, 6));
  print('join: $l');
}
Output
join: ⟨-4 : 3 : -2⟩
meetmethod
RtPoint<T>? meet(RtLine<T> l1, RtLine<T> l2)

The intersection of two lines, or null when they are parallel.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final p = rt.meet(rt.line(r(1), r(1), r(-2)), rt.line(r(1), r(-2), r(1)));
  print('meet: $p; parallel lines: ${rt.meet(rt.line(r(1), r(1), r(0)), rt.line(r(2), r(2), r(5)))}');
}
Output
meet: [1, 1]; parallel lines: null
liesOnmethod
bool liesOn(RtPoint<T> p, RtLine<T> l)

True when the point lies on the line.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = rt.line(r(1), r(1), r(-1));
  print('(1/3, 2/3): ${rt.liesOn(rt.point(r(1, 3), r(2, 3)), l)}, (1, 1): ${rt.liesOn(rt.pointInt(1, 1), l)}');
}
Output
(1/3, 2/3): true, (1, 1): false
sameLinemethod
bool sameLine(RtLine<T> l1, RtLine<T> l2)

True when the triples are proportional.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.sameLine(rt.line(r(1), r(2), r(3)), rt.line(r(-2), r(-4), r(-6)))}');
}
Output
true
bool isParallel(RtLine<T> l1, RtLine<T> l2)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.isParallel(rt.line(r(1), r(2), r(0)), rt.line(r(2), r(4), r(9)))}, ${rt.isParallel(rt.line(r(1), r(2), r(0)), rt.line(r(2), r(1), r(0)))}');
}
Output
true, false
bool isPerpendicular(RtLine<T> l1, RtLine<T> l2)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.isPerpendicular(rt.line(r(1), r(2), r(0)), rt.line(r(2), r(-1), r(5)))}');
}
Output
true
bool isCollinear(RtPoint<T> p, RtPoint<T> q, RtPoint<T> r)

True when the three points lie on one line.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.isCollinear(rt.pointInt(0, 0), rt.point(r(1, 3), r(1, 7)), rt.pointInt(7, 3))}');
}
Output
true
bool isConcurrent(RtLine<T> l1, RtLine<T> l2, RtLine<T> l3)

True when the three lines pass through one point (or are all parallel).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // The three medians of a triangle are concurrent.
  final a = rt.pointInt(0, 0), b = rt.pointInt(6, 0), c = rt.pointInt(2, 4);
  print('${rt.isConcurrent(rt.join(a, rt.midpoint(b, c)), rt.join(b, rt.midpoint(a, c)), rt.join(c, rt.midpoint(a, b)))}');
}
Output
true
RtLine<T> parallelThrough(RtPoint<T> p, RtLine<T> l)

The line through p parallel to l.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.parallelThrough(rt.pointInt(0, 3), rt.line(r(1), r(-2), r(0)))}');
}
Output
⟨1 : -2 : 6⟩
RtLine<T> perpendicularThrough(RtPoint<T> p, RtLine<T> l)

The altitude: the line through p perpendicular to l.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.perpendicularThrough(rt.pointInt(1, 1), rt.line(r(1), r(-2), r(0)))}');
}
Output
⟨2 : 1 : -3⟩
footmethod
RtPoint<T> foot(RtPoint<T> p, RtLine<T> l)

The foot of the altitude from p to l (throws for a null line, whose altitude is the line itself).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final f = rt.foot(rt.pointInt(5, 5), rt.line(r(1), r(-2), r(0)));
  print('foot: $f, on the line: ${rt.liesOn(f, rt.line(r(1), r(-2), r(0)))}');
}
Output
foot: [6, 3], on the line: true
midpointmethod
RtPoint<T> midpoint(RtPoint<T> p, RtPoint<T> q)

The midpoint.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.midpoint(rt.pointInt(1, 2), rt.point(r(2), r(1, 3)))}');
}
Output
[3/2, 7/6]
reflectmethod
RtPoint<T> reflect(RtPoint<T> p, RtLine<T> l)

The reflection of p in the (non-null) line l.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('(3, 1) reflected in y = x: ${rt.reflect(rt.pointInt(3, 1), rt.line(r(1), r(-1), r(0)))}');
}
Output
(3, 1) reflected in y = x: [1, 3]
List<RtLine<T>> nullLinesThrough(RtPoint<T> p)

The null lines through p: \langle i: - 1: y - ix\rangle and \langle i: 1: - y - ix\rangle for i^{2} = -1 — none when -1 is not a square.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // In GF(13), −1 = 5² so null lines exist; over ℚ there are none.
  print('GF(13): ${RtPlane.finite(13).nullLinesThrough(RtPoint<int>(1, 2))}');
  print('ℚ: ${rt.nullLinesThrough(rt.pointInt(1, 2))}');
}
Output
GF(13): [⟨5 : 12 : 10⟩, ⟨5 : 1 : 6⟩]
ℚ: []
measuresmethod
RtMeasures<T> measures(RtPoint<T> a1, RtPoint<T> a2, RtPoint<T> a3)

The quadrances and spreads of the triangle A_{1}A_{2}A_{3} (throws for coincident points or null sides).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  const plane = RtPlane.rational;
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a1 = plane.point(r(0), r(0)), a2 = plane.point(r(7, 2), r(1)), a3 = plane.point(r(1), r(5, 3));
  final m = plane.measures(a1, a2, a3);
  print('Q1 = ${m.q1}, Q2 = ${m.q2}, Q3 = ${m.q3}');
  print('s1 = ${m.s1}, s2 = ${m.s2}, s3 = ${m.s3}');
  print('spread law s1/Q1 = s2/Q2 = s3/Q3: ${plane.spreadLawHolds(m)}');
}
Output
Q1 = 241/36, Q2 = 34/9, Q3 = 53/4
s1 = 841/1802, s2 = 3364/12773, s3 = 7569/8194
spread law s1/Q1 = s2/Q2 = s3/Q3: true
T archimedes(T q1, T q2, T q3)

Archimedes' function A(Q_{1}, Q_{2}, Q_{3}) = (Q_{1} + Q_{2} + Q_{3})^{2} - 2(Q_{1}^{2} + Q_{2}^{2} + Q_{3}^{2}) = 4Q_{1}Q_{2} - (Q_{1} + Q_{2} - Q_{3})^{2}, which is 16 \cdot \mathrm{area}^{2} over the reals; zero exactly for collinear points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('A(9, 16, 25) = ${rt.archimedes(r(9), r(16), r(25))} = 16·6²');
}
Output
A(9, 16, 25) = 576 = 16·6²
T archimedesOfPoints(RtPoint<T> a1, RtPoint<T> a2, RtPoint<T> a3)

The same as archimedes from the vertices (16 \cdot \mathrm{area}^{2} from the coordinates: 4·(cross product)²).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.archimedesOfPoints(rt.pointInt(0, 0), rt.pointInt(4, 0), rt.pointInt(1, 3))} = 16·6²');
}
Output
576 = 16·6²
bool tripleQuadHolds(T q1, T q2, T q3)

Triple quad formula: (Q_{1} + Q_{2} + Q_{3})^{2} = 2(Q_{1}^{2} + Q_{2}^{2} + Q_{3}^{2}), which holds iff the three points are collinear.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('(1, 4, 9): ${rt.tripleQuadHolds(r(1), r(4), r(9))}, (1, 1, 1): ${rt.tripleQuadHolds(r(1), r(1), r(1))}');
}
Output
(1, 4, 9): true, (1, 1, 1): false
List<T> solveTripleQuad(T q1, T q2)

The Q_{3} making three points with quadrances Q_{1}, Q_{2} collinear: Q_{1} + Q_{2} \pm 2\sqrt{Q_{1}Q_{2}} (those that exist in F).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('Q3 for collinear points with Q1 = 1, Q2 = 4: ${rt.solveTripleQuad(r(1), r(4))}');
  print('Q1 = 1, Q2 = 2: ${rt.solveTripleQuad(r(1), r(2))} (needs √2, not in ℚ)');
}
Output
Q3 for collinear points with Q1 = 1, Q2 = 4: [1, 9]
Q1 = 1, Q2 = 2: [] (needs √2, not in ℚ)
bool pythagorasHolds(T q1, T q2, T q3)

Pythagoras' theorem: Q_{1} + Q_{2} = Q_{3} (which holds iff s_{3} = 1, i.e. the sides opposite Q_{1}, Q_{2} are perpendicular).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.pythagorasHolds(r(9), r(16), r(25))}, ${rt.pythagorasHolds(r(1, 4), r(1, 9), r(13, 36))}');
}
Output
true, true
T pythagorasHypotenuse(T q1, T q2)

The quadrance of the hypotenuse, Q_{1} + Q_{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.pythagorasHypotenuse(r(1, 4), r(1, 9))}');
}
Output
13/36
T pythagorasLeg(T hypotenuse, T leg)

The other leg, Q_{3} - Q_{1}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.pythagorasLeg(r(25), r(9))}');
}
Output
16
bool spreadLawHolds(RtMeasures<T> m)

Spread law: \frac{s_{1}}{Q_{1}} = \frac{s_{2}}{Q_{2}} = \frac{s_{3}}{Q_{3}}, checked as s_{1}Q_{2} = s_{2}Q_{1}, s_{2}Q_{3} = s_{3}Q_{2}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final m = rt.measures(rt.pointInt(0, 0), rt.pointInt(5, 0), rt.pointInt(2, 4));
  print('${rt.spreadLawHolds(m)}');
}
Output
true
T spreadLawSpread(T s1, T q1, T q2)

The spread opposite Q_{2} given s_{1} opposite Q_{1}: s_{2} = \frac{s_{1}Q_{2}}{Q_{1}}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('s2 = ${rt.spreadLawSpread(r(9, 25), r(9), r(16))}');
}
Output
s2 = 16/25
T spreadLawQuadrance(T q1, T s1, T s2)

The quadrance opposite s_{2} given the pair (Q_{1}, s_{1}): Q_{2} = \frac{Q_{1}s_{2}}{s_{1}}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('Q2 = ${rt.spreadLawQuadrance(r(9), r(9, 25), r(16, 25))}');
}
Output
Q2 = 16
bool crossLawHolds(T q1, T q2, T q3, T s3)

Cross law: (Q_{1} + Q_{2} - Q_{3})^{2} = 4Q_{1}Q_{2}(1 - s_{3}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.crossLawHolds(r(9), r(16), r(25), r(1))}');
}
Output
true
T crossLawSpread(T q1, T q2, T q3)

The spread s_{3} opposite Q_{3} from three quadrances: 1 - \frac{(Q_{1} + Q_{2} - Q_{3})^{2}}{4Q_{1}Q_{2}} = \frac{A(Q_{1}, Q_{2}, Q_{3})}{4Q_{1}Q_{2}}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('spread opposite 13 in the triangle 4, 9, 13: ${rt.crossLawSpread(r(4), r(9), r(13))}');
}
Output
spread opposite 13 in the triangle 4, 9, 13: 1
List<T> crossLawQuadrance(T q1, T q2, T s3)

The quadrances Q_{3} opposite the spread s_{3} between the sides Q_{1}, Q_{2}: Q_{1} + Q_{2} \pm 2\sqrt{Q_{1}Q_{2}(1 - s_{3})}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.crossLawQuadrance(r(4), r(9), r(1))}, ${rt.crossLawQuadrance(r(1), r(1), r(3, 4))}');
}
Output
[13], [1, 3]
bool tripleSpreadHolds(T s1, T s2, T s3)

Triple spread formula: (s_{1} + s_{2} + s_{3})^{2} = 2(s_{1}^{2} + s_{2}^{2} + s_{3}^{2}) + 4s_{1}s_{2}s_{3} (holds for the spreads of any triangle, and of any three concurrent or parallel lines).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.tripleSpreadHolds(r(3, 4), r(3, 4), r(3, 4))} (equilateral)');
}
Output
true (equilateral)
List<T> solveTripleSpread(T s1, T s2)

The third spreads: s_{1} + s_{2} - 2s_{1}s_{2} \pm 2\sqrt{s_{1}s_{2}(1 - s_{1})(1 - s_{2})}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('third spreads for s1 = s2 = 3/4: ${rt.solveTripleSpread(r(3, 4), r(3, 4))}');
}
Output
third spreads for s1 = s2 = 3/4: [0, 3/4]
bool satisfiesLaws(RtMeasures<T> m)

True when the measures satisfy the spread law, the triple spread formula, the cross law at every vertex (which contains Pythagoras' theorem as s = 1 and the triple quad formula as s = 0), and are non-degenerate (non-zero quadrances).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${rt.satisfiesLaws(rt.measures(rt.pointInt(0, 0), rt.pointInt(7, 1), rt.pointInt(2, 5)))}');
}
Output
true
List<RtMeasures<T>> solveTriangle({T? q1, T? q2, T? q3, T? s1, T? s2, T? s3})

Solves a triangle from three of its six measures (at least one quadrance; spreads alone fix only the shape), returning every non-degenerate solution in F (the ambiguous cases give two). Unknowns are null.

QQQ uses the cross law; QQs with the included spread the cross law for the third quadrance; a known pair (Q_{i}, s_{i}) the spread law; two known spreads the triple spread formula.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // Two sides and the spread between them.
  for (final m in rt.solveTriangle(q1: r(4), q2: r(9), s3: r(1))) print('Q3 ${m.q3}, s1 ${m.s1}, s2 ${m.s2}');
}
Output
Q3 13, s1 4/13, s2 9/13
T spreadPolynomialValue(int n, T s)

S_{n}(s), the spread of n \cdot \theta when s is the spread of \theta, by S_{n+1} = 2(1 - 2s)S_{n} - S_{n-1} + 2s (S_{0} = 0, S_{1} = s).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final rt = RtPlane.rational;
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('S_3(1/4) = ${rt.spreadPolynomialValue(3, r(1, 4))}, S_5(1/3) = ${rt.spreadPolynomialValue(5, r(1, 3))}');
}
Output
S_3(1/4) = 1, S_5(1/3) = 1/243
abstract final class RtClassical

Conversions between rational and classical measures: distance = \sqrt{Q}, angle = \operatorname{asin} \sqrt{s} (for 0 \le s \le 1, the angle in [0, \frac{\pi}{2}] between two lines). Exact as AlgebraicNumber where the classical value is algebraic.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // From quadrance and spread to distance and angle, only at the end.
  final d = RtClassical.distance(Rational.fromInt(2));
  print('distance √2 = ${d.toLatex()} ≈ ${d.toDecimalString(12)}');
  print('angle of spread 3/4 = ${RtClassical.angleAsPiMultiple(Rational.fromInt(3, 4))}·π');
}
Output
distance √2 = \sqrt{2} ≈ 1.414213562373
angle of spread 3/4 = 1/3·π

Static

distancestaticmethod
static AlgebraicNumber distance(Rational quadrance)

The distance \sqrt{Q}, exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√(25/4) = ${RtClassical.distance(Rational.fromInt(25, 4))}, √8 = ${RtClassical.distance(Rational.fromInt(8)).toLatex()}');
}
Output
√(25/4) = 5/2, √8 = 2\sqrt{2}
distanceOfstaticmethod
static AlgebraicNumber distanceOf(AlgebraicNumber quadrance)

The distance \sqrt{Q} of an algebraic quadrance, exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = AlgebraicNumber.fromInt(3) + AlgebraicNumber.sqrt(Rational.fromInt(8));
  print('√(3 + √8) = ${RtClassical.distanceOf(q).toLatex()}');
}
Output
√(3 + √8) = 1 + \sqrt{2}
quadrancestaticmethod
static AlgebraicNumber quadrance(AlgebraicNumber distance)

The quadrance d^{2} of a distance, exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(1 + √2)² = ${RtClassical.quadrance(AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(2))).toLatex()}');
}
Output
(1 + √2)² = 3 + 2\sqrt{2}
anglestaticmethod
static double angle(double spread)

The angle asin \sqrt{s} in radians as a double (0 \le s \le 1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('asin √(1/2) = ${RtClassical.angle(0.5)} (π/4 = ${3.141592653589793 / 4})');
}
Output
asin √(1/2) = 0.7853981633974484 (π/4 = 0.7853981633974483)
angleBigFloatstaticmethod
static BigFloat angleBigFloat(Rational spread, {int prec = 53})

The angle asin \sqrt{s} in radians as a BigFloat of prec bits (\sqrt{s} is exact, then one correctly rounded asin).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('angle of spread 1/3 to 120 bits: ${RtClassical.angleBigFloat(Rational.fromInt(1, 3), prec: 120)}');
}
Output
angle of spread 1/3 to 120 bits: 0.61547970867038734106746458912399368811
angleAsPiMultiplestaticmethod
static Rational? angleAsPiMultiple(Rational spread)

The angle as an exact rational multiple r of \pi (\theta = r\pi \in [0, \frac{1}{2}]) when one exists: by Niven's theorem a rational spread has a rational-multiple-of-π angle only for s \in \{0, \frac{1}{4}, \frac{1}{2}, \frac{3}{4}, 1\}; otherwise null.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final s in [Rational.fromInt(1, 4), Rational.fromInt(1, 2), Rational.one, Rational.fromInt(1, 3)]) {
    print('spread $s → ${RtClassical.angleAsPiMultiple(s)}');
  }
}
Output
spread 1/4 → 1/6
spread 1/2 → 1/4
spread 1 → 1/2
spread 1/3 → null
spreadOfAnglestaticmethod
static double spreadOfAngle(double theta)

The spread \sin^{2}\theta of an angle in radians (double).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('sin²(π/6) in doubles: ${RtClassical.spreadOfAngle(3.141592653589793 / 6)} (exactly 1/4)');
}
Output
sin²(π/6) in doubles: 0.24999999999999994 (exactly 1/4)
spreadOfPiMultiplestaticmethod
static AlgebraicNumber spreadOfPiMultiple(Rational r)

The spread \sin^{2}(r\pi) of the angle r\pi, exactly.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('sin²(π/5) = ${RtClassical.spreadOfPiMultiple(Rational.fromInt(1, 5)).toLatex()}');
  print('sin²(π/6) = ${RtClassical.spreadOfPiMultiple(Rational.fromInt(1, 6))}');
}
Output
sin²(π/5) = \frac{5 - \sqrt{5}}{8}
sin²(π/6) = 1/4
abstract interface class ScalarField<T>

The operations of a field F whose elements are values of type T. Rational trigonometry (RtPlane) is written against this interface, so the same code runs over Q, GF(p) and the real algebraic numbers.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // A field interface: geometry written once runs over ℚ, ℤ/p and the real algebraic numbers.
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a = $a, b = $b: a + b = ${f.add(a, b)}, a·b = ${f.mul(a, b)}, a/b = ${f.div(a, b)}');
}
Output
a = 1/3, b = 1/6: a + b = 1/2, a·b = 1/18, a/b = 2

Properties

zerogetter
T get zero
onegetter
T get one
int get characteristic

0 for Q and the algebraic numbers, p for GF(p).

Methods

fromIntmethod
T fromInt(int n)

The image of the integer n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('fromInt(-4) in ℚ: ${f.fromInt(-4)}');
}
Output
fromInt(-4) in ℚ: -4
T fromRational(Rational r)

The image of a rational (throws when the denominator vanishes in F).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('1/2 in ℚ: ${f.fromRational(Rational.fromInt(1, 2))}');
}
Output
1/2 in ℚ: 1/2
addmethod
T add(T a, T b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a + b = ${f.add(a, b)}');
}
Output
a + b = 1/2
submethod
T sub(T a, T b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a − b = ${f.sub(a, b)}');
}
Output
a − b = 1/6
mulmethod
T mul(T a, T b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a · b = ${f.mul(a, b)}');
}
Output
a · b = 1/18
divmethod
T div(T a, T b)

A / b (throws for b = 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a / b = ${f.div(a, b)}');
}
Output
a / b = 2
negmethod
T neg(T a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('−a = ${f.neg(a)}');
}
Output
−a = -1/3
isZeromethod
bool isZero(T a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a − a is zero: ${f.isZero(f.sub(a, a))}, a: ${f.isZero(a)}');
}
Output
a − a is zero: true, a: false
equalsmethod
bool equals(T a, T b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a·b/b equals a: ${f.equals(f.div(f.mul(a, b), b), a)}');
}
Output
a·b/b equals a: true
sqrtmethod
T? sqrt(T a)

A square root of a in F, or null when a is not a square in F. Over ordered fields the non-negative one.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const ScalarField<Rational> f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('sqrt(a·a) = ${f.sqrt(f.mul(a, a))}, sqrt(b) = ${f.sqrt(b)}');
}
Output
sqrt(a·a) = 1/3, sqrt(b) = null
final class RationalField implements ScalarField<Rational>

The rationals Q, with exact square roots of perfect squares only.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The field ℚ.
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a = $a, b = $b: a + b = ${f.add(a, b)}, a·b = ${f.mul(a, b)}, a/b = ${f.div(a, b)}');
}
Output
a = 1/3, b = 1/6: a + b = 1/2, a·b = 1/18, a/b = 2

Constructors

RationalFieldconstructor
const RationalField()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  print('zero ${f.zero}, one ${f.one}, characteristic ${f.characteristic}');
}
Output
zero 0, one 1, characteristic 0

Properties

zerogetter
Rational get zero
onegetter
Rational get one
int get characteristic

Methods

fromIntmethod
Rational fromInt(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('fromInt(-4) in ℚ: ${f.fromInt(-4)}');
}
Output
fromInt(-4) in ℚ: -4
Rational fromRational(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('1/2 in ℚ: ${f.fromRational(Rational.fromInt(1, 2))}');
}
Output
1/2 in ℚ: 1/2
addmethod
Rational add(Rational a, Rational b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a + b = ${f.add(a, b)}');
}
Output
a + b = 1/2
submethod
Rational sub(Rational a, Rational b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a − b = ${f.sub(a, b)}');
}
Output
a − b = 1/6
mulmethod
Rational mul(Rational a, Rational b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a · b = ${f.mul(a, b)}');
}
Output
a · b = 1/18
divmethod
Rational div(Rational a, Rational b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a / b = ${f.div(a, b)}');
}
Output
a / b = 2
negmethod
Rational neg(Rational a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('−a = ${f.neg(a)}');
}
Output
−a = -1/3
isZeromethod
bool isZero(Rational a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a − a is zero: ${f.isZero(f.sub(a, a))}, a: ${f.isZero(a)}');
}
Output
a − a is zero: true, a: false
equalsmethod
bool equals(Rational a, Rational b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('a·b/b equals a: ${f.equals(f.div(f.mul(a, b), b), a)}');
}
Output
a·b/b equals a: true
sqrtmethod
Rational? sqrt(Rational a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = RationalField();
  final a = Rational.fromInt(1, 3), b = Rational.fromInt(1, 6);
  print('sqrt(a·a) = ${f.sqrt(f.mul(a, a))}, sqrt(b) = ${f.sqrt(b)}');
}
Output
sqrt(a·a) = 1/3, sqrt(b) = null
final class PrimeField implements ScalarField<int>

The finite field GF(p) for an odd prime p < 2^{31}, elements as ints in [0, p).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The field ℤ/11.
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a = $a, b = $b: a + b = ${f.add(a, b)}, a·b = ${f.mul(a, b)}, a/b = ${f.div(a, b)}');
}
Output
a = 3, b = 7: a + b = 10, a·b = 10, a/b = 2

Constructors

PrimeFieldconstructor
PrimeField(this.p)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  print('zero ${f.zero}, one ${f.one}, characteristic ${f.characteristic}');
}
Output
zero 0, one 1, characteristic 11

Properties

pfield
final int p
zerogetter
int get zero
onegetter
int get one
int get characteristic
bool get hasSquareRootOfMinusOne

True when -1 is a square (p \equiv 1 \bmod 4), i.e. when null lines exist.

Methods

fromIntmethod
int fromInt(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('fromInt(-4) in ℤ/11: ${f.fromInt(-4)}');
}
Output
fromInt(-4) in ℤ/11: 7
int fromRational(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('1/2 in ℤ/11: ${f.fromRational(Rational.fromInt(1, 2))}');
}
Output
1/2 in ℤ/11: 6
addmethod
int add(int a, int b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a + b = ${f.add(a, b)}');
}
Output
a + b = 10
submethod
int sub(int a, int b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a − b = ${f.sub(a, b)}');
}
Output
a − b = 7
mulmethod
int mul(int a, int b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a · b = ${f.mul(a, b)}');
}
Output
a · b = 10
divmethod
int div(int a, int b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a / b = ${f.div(a, b)}');
}
Output
a / b = 2
negmethod
int neg(int a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('−a = ${f.neg(a)}');
}
Output
−a = 8
isZeromethod
bool isZero(int a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a − a is zero: ${f.isZero(f.sub(a, a))}, a: ${f.isZero(a)}');
}
Output
a − a is zero: true, a: false
equalsmethod
bool equals(int a, int b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('a·b/b equals a: ${f.equals(f.div(f.mul(a, b), b), a)}');
}
Output
a·b/b equals a: true
inversemethod
int inverse(int a)

The multiplicative inverse (throws for 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(101);
  print('7⁻¹ mod 101 = ${f.inverse(7)}, check ${f.mul(7, f.inverse(7))}');
}
Output
7⁻¹ mod 101 = 29, check 1
sqrtmethod
int? sqrt(int a)

The smaller of the two square roots, or null for a non-residue.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = PrimeField(11);
  final a = 3, b = 7;
  print('sqrt(a·a) = ${f.sqrt(f.mul(a, a))}, sqrt(b) = ${f.sqrt(b)}');
}
Output
sqrt(a·a) = 3, sqrt(b) = null
final class AlgebraicField implements ScalarField<AlgebraicNumber>

The real algebraic numbers: closed under square roots of non-negative elements, so every rational-trigonometry solver that needs √ succeeds whenever the answer is real.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The field of real algebraic numbers (shown in LaTeX form).
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a = ${_s(a)}, b = ${_s(b)}: a + b = ${_s(f.add(a, b))}, a·b = ${_s(f.mul(a, b))}, a/b = ${_s(f.div(a, b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a = \sqrt{2}, b = 3: a + b = 3 + \sqrt{2}, a·b = 3\sqrt{2}, a/b = \frac{\sqrt{2}}{3}

Constructors

AlgebraicFieldconstructor
const AlgebraicField()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  print('zero ${f.zero}, one ${f.one}, characteristic ${f.characteristic}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
zero 0, one 1, characteristic 0

Properties

zerogetter
AlgebraicNumber get zero
onegetter
AlgebraicNumber get one
int get characteristic

Methods

fromIntmethod
AlgebraicNumber fromInt(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('fromInt(-4) in ℝ_alg: ${_s(f.fromInt(-4))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
fromInt(-4) in ℝ_alg: -4
AlgebraicNumber fromRational(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('1/2 in ℝ_alg: ${_s(f.fromRational(Rational.fromInt(1, 2)))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
1/2 in ℝ_alg: \frac{1}{2}
addmethod
AlgebraicNumber add(AlgebraicNumber a, AlgebraicNumber b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a + b = ${_s(f.add(a, b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a + b = 3 + \sqrt{2}
submethod
AlgebraicNumber sub(AlgebraicNumber a, AlgebraicNumber b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a − b = ${_s(f.sub(a, b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a − b = -3 + \sqrt{2}
mulmethod
AlgebraicNumber mul(AlgebraicNumber a, AlgebraicNumber b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a · b = ${_s(f.mul(a, b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a · b = 3\sqrt{2}
divmethod
AlgebraicNumber div(AlgebraicNumber a, AlgebraicNumber b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a / b = ${_s(f.div(a, b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a / b = \frac{\sqrt{2}}{3}
negmethod
AlgebraicNumber neg(AlgebraicNumber a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('−a = ${_s(f.neg(a))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
−a = -\sqrt{2}
isZeromethod
bool isZero(AlgebraicNumber a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a − a is zero: ${f.isZero(f.sub(a, a))}, a: ${f.isZero(a)}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a − a is zero: true, a: false
equalsmethod
bool equals(AlgebraicNumber a, AlgebraicNumber b)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('a·b/b equals a: ${f.equals(f.div(f.mul(a, b), b), a)}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
a·b/b equals a: true
sqrtmethod
AlgebraicNumber? sqrt(AlgebraicNumber a)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const f = AlgebraicField();
  final a = AlgebraicNumber.sqrt(Rational.fromInt(2)), b = AlgebraicNumber.fromInt(3);
  print('sqrt(a·a) = ${_s(f.sqrt(f.mul(a, a)))}, sqrt(b) = ${_s(f.sqrt(b))}');
}

String _s(AlgebraicNumber? x) => x == null ? 'null' : x.toLatex();
Output
sqrt(a·a) = \sqrt{2}, sqrt(b) = \sqrt{3}
final class GaussianRational

re + i \cdot im with exact rational parts.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final z = GaussianRational(Rational.fromInt(1), Rational.fromInt(2));
  final w = GaussianRational(Rational.fromInt(3), Rational.fromInt(-1));
  print('z = $z, w = $w');
  print('z · w = ${z * w}');
  print('z / w = ${z / w}');
  print('|z|² = ${z.normSquared}');
}
Output
z = 1+2i, w = 3-i
z · w = 5+5i
z / w = 1/10+7/10i
|z|² = 5

Constructors

GaussianRationalconstructor
GaussianRational(this.re, [Rational? im])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final z = GaussianRational(Rational.fromInt(1, 2), Rational.fromInt(-3));
  print('z = $z, z·z = ${z * z}');
}
Output
z = 1/2-3i, z·z = -35/4-3i
factory GaussianRational.integer(int re, [int im = 0])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final z = GaussianRational.integer(3, 4);
  print('(3 + 4i)(3 − 4i) = ${z * GaussianRational.integer(3, -4)}');
}
Output
(3 + 4i)(3 − 4i) = 25

Static

zerostaticfield
static final GaussianRational zero
onestaticfield
static final GaussianRational one
istaticfield
static final GaussianRational i

Properties

refield
final Rational re
imfield
final Rational im
isZerogetter
bool get isZero
isRealgetter
bool get isReal
bool get isImaginary
conjugategetter
GaussianRational get conjugate
Rational get normSquared
z².
hashCodegetter
int get hashCode

Methods

scalemethod
GaussianRational scale(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GaussianRational.integer(3, 4).scale(Rational.fromInt(1, 5))}');
}
Output
3/5+4/5i
toComplexmethod
Complex toComplex()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GaussianRational(Rational.fromInt(1, 3), Rational.fromInt(2, 3)).toComplex()}');
}
Output
(0.3333333333333333+0.6666666666666666j)
toStringmethod
String toString()

Operators

operator +operator
GaussianRational operator +(GaussianRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GaussianRational.integer(1, 2) + GaussianRational.integer(3, -5)}');
}
Output
4-3i
operator -operator
GaussianRational operator -(GaussianRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${GaussianRational.integer(1, 2) - GaussianRational.integer(3, -5)}');
}
Output
-2+7i
operator -operator
GaussianRational operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-GaussianRational.integer(1, -2)}');
}
Output
-1+2i
operator *operator
GaussianRational operator *(GaussianRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // (2 + i)² = 3 + 4i: Gaussian integers generate Pythagorean triples.
  print('(2 + i)² = ${GaussianRational.integer(2, 1) * GaussianRational.integer(2, 1)}');
}
Output
(2 + i)² = 3+4i
operator /operator
GaussianRational operator /(GaussianRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('1/(1 + i) = ${GaussianRational.one / GaussianRational.integer(1, 1)}');
}
Output
1/(1 + i) = 1/2-1/2i
operator ==operator
bool operator ==(Object other)
final class SqrtRational

The number sign · √square with square ≥ 0 (sign is 0 exactly when square is 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Clebsch-Gordan coefficients are square roots of rationals, kept exact.
  final c = clebschGordanTwice(1, 1, 1, -1, 2, 0); // ⟨½ ½; ½ −½ | 1 0⟩
  print('⟨½ ½; ½ −½ | 1 0⟩ = $c');
  print('as a double: ${c.toDouble()}');
  print('squared: ${c.signedSquare}');
}
Output
⟨½ ½; ½ −½ | 1 0⟩ = √(1/2)
as a double: 0.7071067811865476
squared: 1/2

Constructors

factory SqrtRational(int sign, Rational square)

Sign · √square.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final v = SqrtRational(-1, Rational.fromInt(2, 3));
  print('v = $v ≈ ${v.toDouble()}');
}
Output
v = -√(2/3) ≈ -0.816496580927726
factory SqrtRational.rational(Rational r)

The rational r itself.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SqrtRational.rational(Rational.fromInt(-3, 4))}');
}
Output
-3/4
factory SqrtRational.parseSigned(String s)

Parses -3/4 meaning -\sqrt{\frac{3}{4}} (the fixture format).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The tables' format: −3/4 means −√(3/4).
  print('${SqrtRational.parseSigned('-3/4')}, ${SqrtRational.parseSigned('1/2')}');
}
Output
-√(3/4), √(1/2)

Static

zerostaticfield
static final SqrtRational zero
onestaticfield
static final SqrtRational one

Properties

signfield
final int sign
squarefield
final Rational square
isZerogetter
bool get isZero
Rational get signedSquare

The signed square, sign · square (the fixture format).

hashCodegetter
int get hashCode

Methods

scalemethod
SqrtRational scale(Rational r)

Multiplies by a rational.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SqrtRational(1, Rational.fromInt(1, 2)).scale(Rational.fromInt(-2))}');
}
Output
-√(2)
toDoublemethod
double toDouble()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SqrtRational.parseSigned('1/3').toDouble()}');
}
Output
0.5773502691896257
toSurdmethod
SurdSum toSurd()

Exact value as a surd sum (for exact sums of products).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SqrtRational.parseSigned('8/9').toSurd()}');
}
Output
2/3*√2
toStringmethod
String toString()

Operators

operator *operator
SqrtRational operator *(SqrtRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√(1/2)·√(1/2) = ${SqrtRational.parseSigned('1/2') * SqrtRational.parseSigned('1/2')}');
}
Output
√(1/2)·√(1/2) = 1/2
operator /operator
SqrtRational operator /(SqrtRational o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√(1/2) / √(1/8) = ${SqrtRational.parseSigned('1/2') / SqrtRational.parseSigned('1/8')}');
}
Output
√(1/2) / √(1/8) = 2
operator -operator
SqrtRational operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-SqrtRational.parseSigned('2/3')}');
}
Output
-√(2/3)
operator ==operator
bool operator ==(Object other)
class

SurdSum

final class SurdSum

An exact finite sum \sum c_{k} \sqrt{k} over squarefree positive integers k with rational coefficients c_{k}.

The radicals \sqrt{k} for distinct squarefree k are linearly independent over Q, so equality is coefficient-wise. Squarefree parts are found by trial division up to 10^{5} and a perfect-square test of the cofactor; integers whose cofactor hides the square of a prime above 10^{5} are not reduced (never the case for factorial-built Wigner values).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Sums of square roots, kept exact: √8 + √2 = 3√2.
  final s = SurdSum.sqrtOf(Rational.fromInt(8)) + SurdSum.sqrtOf(Rational.fromInt(2));
  print('√8 + √2 = $s ≈ ${s.toDouble()}');
}
Output
√8 + √2 = 3*√2 ≈ 4.242640687119286

Constructors

factory SurdSum.rational(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SurdSum.rational(Rational.fromInt(5, 2)) + SurdSum.sqrtOf(Rational.fromInt(3))}');
}
Output
5/2 + 1*√3
factory SurdSum.sqrtOf(Rational r)

\sqrt{r} for a rational r \ge 0.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('√12 = ${SurdSum.sqrtOf(Rational.fromInt(12))}, √(1/18) = ${SurdSum.sqrtOf(Rational.fromInt(1, 18))}');
}
Output
√12 = 2*√3, √(1/18) = 1/6*√2

Static

zerostaticfield
static final SurdSum zero

Properties

termsfield
final Map<BigInt, Rational> terms

Squarefree radicand → coefficient.

isZerogetter
bool get isZero
bool get isRational
hashCodegetter
int get hashCode

Methods

scalemethod
SurdSum scale(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(SurdSum.sqrtOf(Rational.fromInt(2)) + SurdSum.rational(Rational.one)).scale(Rational.fromInt(3))}');
}
Output
3 + 3*√2
Rational toRational()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = (SurdSum.sqrtOf(Rational.fromInt(2)) + SurdSum.rational(Rational.one)) * (SurdSum.sqrtOf(Rational.fromInt(2)) - SurdSum.rational(Rational.one));
  print('(√2 + 1)(√2 − 1) = ${s.toRational()}');
}
Output
(√2 + 1)(√2 − 1) = 1
toDoublemethod
double toDouble()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(SurdSum.sqrtOf(Rational.fromInt(3)) + SurdSum.sqrtOf(Rational.fromInt(5))).toDouble()}');
}
Output
3.968118785068667
toStringmethod
String toString()

Operators

operator +operator
SurdSum operator +(SurdSum o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SurdSum.sqrtOf(Rational.fromInt(18)) + SurdSum.sqrtOf(Rational.fromInt(50))}');
}
Output
8*√2
operator -operator
SurdSum operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${SurdSum.sqrtOf(Rational.fromInt(18)) - SurdSum.sqrtOf(Rational.fromInt(8))}');
}
Output
1*√2
operator -operator
SurdSum operator -(SurdSum o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-SurdSum.sqrtOf(Rational.fromInt(7))}');
}
Output
-1*√7
operator *operator
SurdSum operator *(SurdSum o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(√2 + √3)² = ${(SurdSum.sqrtOf(Rational.fromInt(2)) + SurdSum.sqrtOf(Rational.fromInt(3))) * (SurdSum.sqrtOf(Rational.fromInt(2)) + SurdSum.sqrtOf(Rational.fromInt(3)))}');
}
Output
(√2 + √3)² = 5 + 2*√6
operator ==operator
bool operator ==(Object other)
final class CharacterTable

The character table of a finite group with exact cyclotomic values.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final table = CharacterTable.of(NamedGroups.symmetric(3));
  print('degrees: ${table.degrees}');
  print(table.toString().trimRight());
  print('orthogonality: ${table.checkOrthogonality()}');
}
Output
degrees: [1, 1, 2]
order 1	2	3
size  1	3	2
χ1    1	1	1
χ2    1	-1	1
χ3    2	0	-1
orthogonality: true

Constructors

factory CharacterTable.of(PermutationGroup g)

Computes the table of a (small) permutation group.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // The character table of A4, with its cube roots of unity, exactly.
  final t = CharacterTable.of(NamedGroups.alternating(4));
  print('class sizes ${t.classSizes}, element orders ${t.elementOrders}, degrees ${t.degrees}');
  print(t.toString().trimRight());
}
Output
class sizes [1, 3, 4, 4], element orders [1, 2, 3, 3], degrees [1, 1, 1, 3]
order 1	2	3	3
size  1	3	4	4
χ1    1	1	1	1
χ2    1	1	z3	-1 - z3
χ3    1	1	-1 - z3	z3
χ4    3	-1	0	0

Properties

final int groupOrder
classesfield
final List<ConjugacyClass> classes
final List<ClassFunction> characters

Irreducible characters, the trivial one first, sorted by degree.

int get classCount
List<int> get classSizes
List<int> get elementOrders
degreesgetter
List<int> get degrees
ClassFunction get permutationCharacter

The permutation character of the group's action (number of fixed points), useful for decomposition checks.

Methods

powerMapmethod
int powerMap(int i, int k)

The class index of g^{k} for the representative of class i.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final t = CharacterTable.of(NamedGroups.symmetric(4));
  print('element orders: ${t.elementOrders}');
  print('class of g²: ${[for (var i = 0; i < t.classCount; i++) t.powerMap(i, 2)]}');
}
Output
element orders: [1, 2, 2, 3, 4]
class of g²: [0, 0, 0, 3, 1]
Cyclotomic innerProduct(ClassFunction chi, ClassFunction psi)

\langle\chi, \psi\rangle = (\frac{1}{|G|}) \sum_{\mathrm{classes}} |C| \chi(g) \operatorname{conj}(\psi(g)), exact.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final t = CharacterTable.of(NamedGroups.symmetric(3));
  final chi = t.characters;
  print('⟨χ_i, χ_j⟩: ${[for (final a in chi) [for (final b in chi) t.innerProduct(a, b)]]}');
}
Output
⟨χ_i, χ_j⟩: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
tensormethod
ClassFunction tensor(ClassFunction a, ClassFunction b)

Pointwise product (the character of the tensor product).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The 2-dimensional character of S3 squared: 1 + sign + 2-dim.
  final t = CharacterTable.of(NamedGroups.symmetric(3));
  final two = t.characters.firstWhere((c) => c[0].toRational() == Rational.fromInt(2));
  print('χ ⊗ χ decomposes as ${t.decompose(t.tensor(two, two))} (multiplicities in table order)');
}
Output
χ ⊗ χ decomposes as [1, 1, 1] (multiplicities in table order)
decomposemethod
List<Rational> decompose(ClassFunction f)

Multiplicities of each irreducible in a class function (exact rationals; integers for characters).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The permutation character of S4 on 4 points = trivial + standard.
  final t = CharacterTable.of(NamedGroups.symmetric(4));
  print('degrees ${t.degrees}');
  print('permutation character: ${t.decompose(t.permutationCharacter)}');
}
Output
degrees [1, 1, 2, 3, 3]
permutation character: [1, 0, 0, 1, 0]
bool checkOrthogonality()

Whether \langle\chi_{i}, \chi_{j}\rangle = \delta_{ij} (first orthogonality) and \sum_{\chi} \chi(g_{a}) \operatorname{conj}(\chi(g_{b})) = \delta_{ab} |C_{G}(g_{a})| (second).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final (n, g) in [('Q8', NamedGroups.quaternion()), ('D5', NamedGroups.dihedral(5)), ('A5', NamedGroups.alternating(5))]) {
    print('$n: ${CharacterTable.of(g).checkOrthogonality()}');
  }
}
Output
Q8: true
D5: true
A5: true
int frobeniusSchur(ClassFunction chi)

The Frobenius–Schur indicator (\frac{1}{|G|}) \sum \chi(g^{2}): 1 real, -1 quaternionic, 0 complex type.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Q8's 2-dimensional character is quaternionic (−1); D4's is real (1).
  for (final (n, g) in [('Q8', NamedGroups.quaternion()), ('D4', NamedGroups.dihedral(4)), ('C3', NamedGroups.cyclic(3))]) {
    final t = CharacterTable.of(g);
    print('$n: ${[for (final c in t.characters) t.frobeniusSchur(c)]}');
  }
}
Output
Q8: [1, 1, 1, 1, -1]
D4: [1, 1, 1, 1, 1]
C3: [1, 0, 0]
toStringmethod
String toString()
class

FpGroup

final class FpGroup

A group \langle x_{1}, \ldots, x_{m} \mid r_{1}, \ldots, r_{k}\rangle.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // ⟨a, b | a³, b², (ab)²⟩ is S3: coset enumeration finds order 6.
  final g = FpGroup.parse('a,b', ['a^3', 'b^2', 'a*b*a*b']);
  print('order ${g.order()}');
}
Output
order 6

Constructors

FpGroupconstructor
FpGroup(this.generatorCount, List<List<int>> relators, {List<String>? names})
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Generators are 1, 2, …; negative numbers are inverses. ⟨x | x⁵⟩.
  final g = FpGroup(1, [[1, 1, 1, 1, 1]]);
  print('order ${g.order()}');
}
Output
order 5
factory FpGroup.parse(String generators, List<String> relators)

Parses relators written with single-letter generator names and powers, e.g. FpGroup.parse('a,b', ['a^4', 'a^2*b^-2', 'b^-1*a*b*a']). Uppercase letters denote inverses (A = a^{-1}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The binary tetrahedral group ⟨r, s, t | r² = s³ = t³ = rst⟩; an upper-case letter is the inverse generator.
  final g = FpGroup.parse('r,s,t', ['r^2*S^3', 'r^2*T^3', 'r^2*T*S*R']);
  print('order ${g.order()}');
}
Output
order 24

Properties

final int generatorCount
final List<List<int>> relators
namesfield
final List<String> names

Methods

CosetTable enumerateCosets({List<List<int>> subgroup = const <List<int>>[], int maxCosets = 1000000})

Enumerates the cosets of the subgroup generated by subgroup (words); throws StateError past maxCosets.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Cosets of ⟨a⟩ in S3 = ⟨a, b | a³, b², (ab)²⟩: index 2.
  final g = FpGroup.parse('a,b', ['a^3', 'b^2', 'a*b*a*b']);
  final t = g.enumerateCosets(subgroup: [[1]]);
  print('index ${t.index}, action of the generators: ${t.generatorActions}');
}
Output
index 2, action of the generators: [[0, 1], [1, 0]]
ordermethod
int order({int maxCosets = 1000000})
G, when finite (enumerating the cosets of the trivial subgroup).
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('⟨a, b | a⁴, b², abab⟩ (D4): ${FpGroup.parse('a,b', ['a^4', 'b^2', 'a*b*a*b']).order()}');
  print('⟨a, b | a², b³, (ab)⁵⟩ (A5): ${FpGroup.parse('a,b', ['a^2', 'b^3', 'a*b*a*b*a*b*a*b*a*b']).order()}');
}
Output
⟨a, b | a⁴, b², abab⟩ (D4): 8
⟨a, b | a², b³, (ab)⁵⟩ (A5): 60
PermutationGroup toPermutationGroup({List<List<int>> subgroup = const <List<int>>[], int maxCosets = 1000000})

The permutation group of G acting on the cosets of subgroup (faithful for the trivial subgroup).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = FpGroup.parse('a,b', ['a^2', 'b^3', 'a*b*a*b*a*b*a*b*a*b']);
  final p = g.toPermutationGroup(subgroup: [[2]]);
  print('A5 acting on the cosets of ⟨b⟩: degree ${p.degree}, order ${p.order}');
}
Output
A5 acting on the cosets of ⟨b⟩: degree 20, order 60
final class CosetTable

The result of coset enumeration: the action of each generator on the cosets 0 … index−1 (coset 0 = H).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final t = FpGroup.parse('a', ['a^4']).enumerateCosets();
  print('cosets ${t.index}; a acts as ${t.generatorActions[0]}');
}
Output
cosets 4; a acts as [1, 2, 3, 0]

Properties

final List<List<int>> generatorActions

generatorActions[c] = the coset c \cdot x_{i}.

indexgetter
int get index
List<Permutation> get permutations
PermutationGroup get permutationGroup
abstract final class NamedGroups

Constructors for standard groups. Points are 0-based.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s4 = NamedGroups.symmetric(4);
  final a5 = NamedGroups.alternating(5);
  print('|S4| = ${s4.order}, |A5| = ${a5.order}');
  print('A5 abelian: ${a5.isAbelian}');
  print('conjugacy classes of S4: ${s4.conjugacyClasses().length}');
}
Output
|S4| = 24, |A5| = 60
A5 abelian: false
conjugacy classes of S4: 5

Static

cyclicstaticmethod
static PermutationGroup cyclic(int n)

The cyclic group C_{n} acting regularly on n points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final c = NamedGroups.cyclic(12);
  print('C12: order ${c.order}, abelian character degrees ${CharacterTable.of(c).degrees}');
}
Output
C12: order 12, abelian character degrees [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
dihedralstaticmethod
static PermutationGroup dihedral(int n)

The dihedral group D_{n} of order 2n (symmetries of the regular n-gon), n \ge 3; D_{1} \cong C_{2} and D_{2} \cong V_{4}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final d = NamedGroups.dihedral(6);
  print('D6: order ${d.order}, classes ${d.conjugacyClasses().length}');
}
Output
D6: order 12, classes 6
symmetricstaticmethod
static PermutationGroup symmetric(int n)

The symmetric group S_{n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [3, 5, 10]) print('S$n: order ${NamedGroups.symmetric(n).order}');
}
Output
S3: order 6
S5: order 120
S10: order 3628800
alternatingstaticmethod
static PermutationGroup alternating(int n)

The alternating group A_{n}, generated by the 3-cycles (0 1 k).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a5 = NamedGroups.alternating(5);
  print('A5: order ${a5.order}, character degrees ${CharacterTable.of(a5).degrees}');
}
Output
A5: order 60, character degrees [1, 3, 3, 4, 5]
kleinFourstaticmethod
static PermutationGroup kleinFour()

The Klein four-group V_{4} = C_{2} \times C_{2} acting regularly on 4 points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final v = NamedGroups.kleinFour();
  print('V4: order ${v.order}, every element squares to 1: ${v.conjugacyClasses().every((c) => (c.representative * c.representative).isIdentity)}');
}
Output
V4: order 4, every element squares to 1: true
quaternionstaticmethod
static PermutationGroup quaternion()

The quaternion group Q_{8} = \{\pm1, \pm i, \pm j, \pm k\} acting regularly on 8 points (Todd–Coxeter on \langle i, j \mid i^{4}, i^{2}j^{-2}, j^{-1}iji\rangle).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = NamedGroups.quaternion();
  print('Q8: order ${q.order}, degrees ${CharacterTable.of(q).degrees}');
}
Output
Q8: order 8, degrees [1, 1, 1, 1, 2]
dicyclicstaticmethod
static PermutationGroup dicyclic(int n)

The dicyclic group Dic_{n} of order 4n: \langle a, x \mid a^{2n}, x^{2}a^{-n}, x^{-1}axa\rangle; generalised quaternion for n a power of 2. Regular action from coset enumeration.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('Dic3: order ${NamedGroups.dicyclic(3).order}');
}
Output
Dic3: order 12
static PermutationGroup generalisedQuaternion(int order)

The generalised quaternion group of order order (a power of 2, ≥ 8).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('Q16: order ${NamedGroups.generalisedQuaternion(16).order}');
}
Output
Q16: order 16
elementaryAbelianstaticmethod
static PermutationGroup elementaryAbelian(int p, int k)

Elementary abelian group (C_{p})^{k} acting regularly on p^{k} points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('(C3)³: order ${NamedGroups.elementaryAbelian(3, 3).order}');
}
Output
(C3)³: order 27
static PermutationGroup projectiveSpecialLinear2(int p)

PSL(2, p) for a prime p \ge 5, acting on the projective line \{0, \ldots, p - 1, \infty\} (\infty = point p): z \mapsto z + 1 and z \mapsto -\frac{1}{z}. |PSL(2,p)| = \frac{p(p^{2} - 1)}{2}; PSL(2,5) ≅ A_5, PSL(2,7) ≅ GL(3,2).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // PSL(2, 7), the simple group of order 168.
  print('PSL(2,7): order ${NamedGroups.projectiveSpecialLinear2(7).order}');
}
Output
PSL(2,7): order 168
mathieu11staticmethod
static PermutationGroup mathieu11()

The Mathieu group M_{11} (order 7920) on 11 points: ⟨(1,…,11), (3,7,11,8)(4,10,5,6)⟩.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('M11: order ${NamedGroups.mathieu11().order}');
}
Output
M11: order 7920
mathieu12staticmethod
static PermutationGroup mathieu12()

The Mathieu group M_{12} (order 95040) on 12 points: M_11's generators and (1,12)(2,11)(3,6)(4,8)(5,9)(7,10).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('M12: order ${NamedGroups.mathieu12().order}');
}
Output
M12: order 95040
final class Permutation

A permutation of {0, …, degree−1}, immutable.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = Permutation.parse('(1,2,3)(4,5)', 5);
  final q = Permutation.parse('(1,4)', 5);
  print('p = $p, order ${p.order}, sign ${p.sign}, cycle type ${p.cycleType}');
  print('p * q = ${p * q}');
  print('p⁻¹ = ${p.inverse}');
  print('p^6 is the identity: ${p.pow(6).isIdentity}');
}
Output
p = (1,2,3)(4,5), order 6, sign -1, cycle type [3, 2]
p * q = (1,2,3,4,5)
p⁻¹ = (1,3,2)(4,5)
p^6 is the identity: true

Constructors

factory Permutation(List<int> images)

From the image list: images[i] is the image of i. Throws if it is not a bijection.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // images[i] is where i goes.
  final p = Permutation([1, 2, 0, 4, 3]);
  print('p = $p, cycles ${p.cycles}, sign ${p.sign}');
}
Output
p = (1,2,3)(4,5), cycles [[0, 1, 2], [3, 4]], sign -1
factory Permutation.identity(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = Permutation.identity(4);
  print('$e, identity ${e.isIdentity}');
}
Output
(), identity true
factory Permutation.cycles(int n, List<List<int>> cycles)

From disjoint or overlapping cycles (composed left to right), e.g. [[0, 1, 2], [3, 4]].

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final p = Permutation.cycles(6, [[0, 1, 2], [3, 4]]);
  print('p = $p, cycle type ${p.cycleType}, order ${p.order}');
}
Output
p = (1,2,3)(4,5), cycle type [3, 2, 1], order 6
factory Permutation.parse(String s, int n)

Parses 1-based cycle notation such as (1,2,3)(4,5) on n points (GAP / textbook style).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = Permutation.parse('(1,2,3)(4,5)', 5);
  print('$p, order ${p.order}');
}
Output
(1,2,3)(4,5), order 6

Properties

degreegetter
int get degree
imagesgetter
List<int> get images
inversegetter
Permutation get inverse
bool get isIdentity
cyclesgetter
List<List<int>> get cycles

Nontrivial cycles, each starting at its smallest point.

cycleTypegetter
List<int> get cycleType

Cycle lengths including fixed points, sorted descending (the partition of n).

ordergetter
int get order

Order: the lcm of the cycle lengths.

signgetter
int get sign

+1 for even, -1 for odd permutations.

isEvengetter
bool get isEven
supportgetter
List<int> get support

Points moved.

int get firstMoved

The smallest moved point, or -1 for the identity.

hashCodegetter
int get hashCode

Methods

powmethod
Permutation pow(int k)

p^{k} for any integer k.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = Permutation.parse('(1,2,3,4,5,6)', 6);
  for (final k in [2, 3, 6, -1]) print('p^$k = ${p.pow(k)}');
}
Output
p^2 = (1,3,5)(2,4,6)
p^3 = (1,4)(2,5)(3,6)
p^6 = ()
p^-1 = (1,6,5,4,3,2)
Permutation conjugateBy(Permutation x)

Conjugate x^{-1} p x.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Conjugation relabels the points but keeps the cycle type.
  final p = Permutation.parse('(1,2,3)', 4), x = Permutation.parse('(1,4)', 4);
  print('x⁻¹px = ${p.conjugateBy(x)}');
}
Output
x⁻¹px = (2,3,4)
Permutation commutator(Permutation q)

The commutator p^{-1} q^{-1} p q.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = Permutation.parse('(1,2)', 3), q = Permutation.parse('(2,3)', 3);
  print('[p, q] = ${p.commutator(q)}');
}
Output
[p, q] = (1,2,3)
extendmethod
Permutation extend(int n)

Same permutation on n ≥ degree points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Permutation.parse('(1,2)', 2).extend(5)}');
}
Output
(1,2)
toStringmethod
String toString()

1-based cycle notation, `` for the identity.

Operators

operator []operator
int operator [](int x)

The image of x.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final p = Permutation.parse('(1,3,5)', 6);
  print('p[0] = ${p[0]}, p[2] = ${p[2]}, p[1] = ${p[1]} (0-based)');
}
Output
p[0] = 2, p[2] = 4, p[1] = 1 (0-based)
operator *operator
Permutation operator *(Permutation o)

Left-to-right product: apply this, then o. Degrees are padded to the larger one.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Left to right: apply p, then q.
  final p = Permutation.parse('(1,2)', 3), q = Permutation.parse('(2,3)', 3);
  print('p*q = ${p * q}, q*p = ${q * p}');
}
Output
p*q = (1,3,2), q*p = (1,2,3)
operator ==operator
bool operator ==(Object other)
final class StabiliserLevel

One level of a stabiliser chain: the base point, the strong generators fixing the earlier base points, and a transversal u[b] (an element mapping the base point to b) for the basic orbit.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // One level of the Schreier–Sims chain: a base point and its orbit (transversal).
  final g = NamedGroups.symmetric(4);
  for (final l in g.chain) print('base point ${l.basePoint}: orbit size ${l.transversal.length}');
}
Output
base point 0: orbit size 4
base point 1: orbit size 3
base point 2: orbit size 2

Properties

final int basePoint
final List<Permutation> generators
final Map<int, Permutation> transversal
orbitgetter
Iterable<int> get orbit

The basic orbit of the base point under this level's stabiliser.

class PermutationGroup

A group generated by permutations of {0, …, degree−1}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The rotations of a square, as permutations of its corners 1..4.
  final r = Permutation.parse('(1,2,3,4)', 4);
  final f = Permutation.parse('(1,3)', 4);
  final rot = PermutationGroup([r], degree: 4);
  final dihedral = PermutationGroup([r, f], degree: 4);
  print('rotations: order ${rot.order}, abelian ${rot.isAbelian}');
  print('with a flip: order ${dihedral.order}, abelian ${dihedral.isAbelian}');
  print('transitive on the 4 corners: ${dihedral.isTransitive}');
}
Output
rotations: order 4, abelian true
with a flip: order 8, abelian false
transitive on the 4 corners: true

Constructors

PermutationGroupconstructor
PermutationGroup(List<Permutation> generators, {int? degree})
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The rotations of a cube acting on its 8 vertices.
  final g = PermutationGroup([Permutation.parse('(1,2,3,4)(5,6,7,8)', 8), Permutation.parse('(1,2,6,5)(4,3,7,8)', 8)]);
  print('order ${g.order}, base ${g.base}');
}
Output
order 24, base [0, 1]

Properties

degreefield
final int degree
final List<Permutation> generators
chainfield
late final List<StabiliserLevel> chain
identitygetter
Permutation get identity
basegetter
List<int> get base

The base (\beta_{1}, \ldots, \beta_{k}).

ordergetter
BigInt get order
G= Π\Delta_{i}, exact.
orbitsgetter
List<List<int>> get orbits

All orbits on {0, …, degree−1}.

bool get isTransitive
isAbeliangetter
bool get isAbelian
elementsgetter
Iterable<Permutation> get elements

Every element, as products u_{k} \cdots u_{1} of transversal elements. Use only for small groups.

Methods

containsmethod
bool contains(Permutation g)

Sifts g through the chain; the residue is the identity exactly when g \in G.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a4 = NamedGroups.alternating(4);
  print('(1,2,3) ∈ A4: ${a4.contains(Permutation.parse('(1,2,3)', 4))}, (1,2) ∈ A4: ${a4.contains(Permutation.parse('(1,2)', 4))}');
}
Output
(1,2,3) ∈ A4: true, (1,2) ∈ A4: false
bool containsGroup(PermutationGroup other)

Whether every element of other is in this group.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('A4 ⊂ S4: ${NamedGroups.symmetric(4).containsGroup(NamedGroups.alternating(4))}');
}
Output
A4 ⊂ S4: true
orbitmethod
List<int> orbit(int point)

The orbit of a point.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = PermutationGroup([Permutation.parse('(1,2)(3,4)', 6)]);
  print('orbit of 0: ${g.orbit(0)}, of 4: ${g.orbit(4)}');
}
Output
orbit of 0: [0, 1], of 4: [4]
randommethod
Permutation random(math.Random rng)

A uniformly random element (one random transversal element per level).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:math';

void main() {
  // Seeded, so the output is the same every run.
  final rng = Random(42);
  final g = NamedGroups.symmetric(5);
  print([for (var i = 0; i < 3; i++) g.random(rng)]);
}
Output
[(3,5), (2,3,5), (2,3,4)]
PermutationGroup stabiliser(List<int> points)

The pointwise stabiliser of points, by Schreier–Sims on a base starting with them.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = NamedGroups.symmetric(5).stabiliser([0, 1]);
  print('stabiliser of 0 and 1 in S5: order ${s.order}');
}
Output
stabiliser of 0 and 1 in S5: order 6
List<ConjugacyClass> conjugacyClasses()

Conjugacy classes, by orbits of conjugation on the enumerated elements. Use only for small groups (≤ ~10^{5}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final c in NamedGroups.symmetric(4).conjugacyClasses()) print('${c.representative.cycleType}: ${c.size} elements');
}
Output
[1, 1, 1, 1]: 1 elements
[2, 2]: 3 elements
[2, 1, 1]: 6 elements
[3, 1]: 8 elements
[4]: 6 elements
PermutationGroup directProduct(PermutationGroup h)

Direct product G \times H acting on \operatorname{degree}(G) + \operatorname{degree}(H) points.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = NamedGroups.cyclic(2).directProduct(NamedGroups.cyclic(3));
  print('C2 × C3: order ${g.order}, degree ${g.degree}');
}
Output
C2 × C3: order 6, degree 5
toStringmethod
String toString()
final class ConjugacyClass

A conjugacy class of a finite permutation group.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final cs = NamedGroups.dihedral(4).conjugacyClasses();
  print([for (final c in cs) '${c.representative} (${c.size})'].join('  '));
}
Output
() (1)  (1,3)(2,4) (1)  (1,4)(2,3) (2)  (2,4) (2)  (1,2,3,4) (2)

Properties

final Permutation representative
membersfield
final List<Permutation> members
sizegetter
int get size

Methods

toStringmethod
String toString()
final class CayleyDickson

An element of the Cayley–Dickson algebra of dimension 2^{\mathrm{level}} with exact rational coordinates on e_{0} = 1, e_{1}, ….

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = CayleyDickson.quaternion(0, 1, 0, 0);
  final j = CayleyDickson.quaternion(0, 0, 1, 0);
  print('i·j = ${i * j}, j·i = ${j * i}');
  final e1 = CayleyDickson.unit(3, 1), e2 = CayleyDickson.unit(3, 2), e4 = CayleyDickson.unit(3, 4);
  print('octonion associator (e1, e2, e4) = ${CayleyDickson.associator(e1, e2, e4)}');
}
Output
i·j = 1e3, j·i = -1e3
octonion associator (e1, e2, e4) = 2e7

Constructors

factory CayleyDickson(List<Rational> coords)

From coordinates (length a power of two).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final o = CayleyDickson([for (var i = 0; i < 8; i++) Rational.fromInt(i % 3, 2)]);
  print('an octonion: $o, |o|² = ${o.normSquared}');
}
Output
an octonion: 1/2e1 + 1e2 + 1/2e4 + 1e5 + 1/2e7, |o|² = 11/4
factory CayleyDickson.ints(List<int> coords)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final x = CayleyDickson.ints([1, 2, 0, 0, 0, 0, 0, 3]);
  print('x x⁻¹ = ${x * x.inverse}');
}
Output
x x⁻¹ = 1
factory CayleyDickson.unit(int level, int k)

The basis unit e_{k} of the algebra of the given level.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e1 = CayleyDickson.unit(3, 1), e2 = CayleyDickson.unit(3, 2);
  print('e1·e2 = ${e1 * e2}, e2·e1 = ${e2 * e1}');
}
Output
e1·e2 = 1e3, e2·e1 = -1e3
factory CayleyDickson.zero(int level)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CayleyDickson.zero(2)}');
}
Output
0

Static

quaternionstaticmethod
static CayleyDickson quaternion(int w, int x, int y, int z)

Quaternions (level 2), octonions (3) and sedenions (4).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = CayleyDickson.quaternion(0, 1, 0, 0), j = CayleyDickson.quaternion(0, 0, 1, 0);
  print('ij = ${i * j}, ji = ${j * i}');
}
Output
ij = 1e3, ji = -1e3
associatorstaticmethod
static CayleyDickson associator(CayleyDickson x, CayleyDickson y, CayleyDickson z)

The associator (xy)z - x(yz).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Zero for quaternions; nonzero for octonions (they are not associative).
  final q = [for (var k = 1; k <= 3; k++) CayleyDickson.unit(2, k)];
  final o = [CayleyDickson.unit(3, 1), CayleyDickson.unit(3, 2), CayleyDickson.unit(3, 4)];
  print('quaternions: ${CayleyDickson.associator(q[0], q[1], q[2])}');
  print('octonions:   ${CayleyDickson.associator(o[0], o[1], o[2])}');
}
Output
quaternions: 0
octonions:   2e7
commutatorstaticmethod
static CayleyDickson commutator(CayleyDickson x, CayleyDickson y)

The commutator xy - yx.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CayleyDickson.commutator(CayleyDickson.unit(2, 1), CayleyDickson.unit(2, 2))}');
}
Output
2e3

Properties

levelfield
final int level
cfield
final List<Rational> c
dimensiongetter
int get dimension
conjugategetter
CayleyDickson get conjugate
Rational get normSquared

X x* = \sum c_{k}^{2} (always a nonnegative rational).

inversegetter
CayleyDickson get inverse

x* / |x|^{2} (a two-sided inverse up to the octonions; sedenions have zero divisors).

isZerogetter
bool get isZero
hashCodegetter
int get hashCode

Methods

scalemethod
CayleyDickson scale(Rational r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CayleyDickson.quaternion(1, 2, 3, 4).scale(Rational.fromInt(1, 2))}');
}
Output
1/2 + 1e1 + 3/2e2 + 2e3
toStringmethod
String toString()

Operators

operator +operator
CayleyDickson operator +(CayleyDickson o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CayleyDickson.quaternion(1, 2, 3, 4) + CayleyDickson.quaternion(0, -2, 1, 0)}');
}
Output
1 + 4e2 + 4e3
operator -operator
CayleyDickson operator -(CayleyDickson o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${CayleyDickson.quaternion(1, 2, 3, 4) - CayleyDickson.quaternion(1, 2, 3, 4)}');
}
Output
0
operator -operator
CayleyDickson operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-CayleyDickson.quaternion(1, -2, 3, -4)}');
}
Output
-1 + 2e1 - 3e2 + 4e3
operator *operator
CayleyDickson operator *(CayleyDickson o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Sedenions have zero divisors: (e3 + e10)(e6 − e15) = 0.
  final a = CayleyDickson.unit(4, 3) + CayleyDickson.unit(4, 10), b = CayleyDickson.unit(4, 6) - CayleyDickson.unit(4, 15);
  print('(e3 + e10)(e6 − e15) = ${a * b}');
}
Output
(e3 + e10)(e6 − e15) = 0
operator ==operator
bool operator ==(Object other)
final class Quaternion

w + x i + y j + z k with i^{2} = j^{2} = k^{2} = ijk = -1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = Quaternion(1, 2, 3, 4);
  print('q = $q, |q| = ${q.norm.toStringAsFixed(6)}');
  print('q · q⁻¹ ≈ 1: ${(q * q.inverse).approxEquals(Quaternion.one)}');
  print('i · j = ${Quaternion.i * Quaternion.j}');
}
Output
q = 1.0 + 2.0i + 3.0j + 4.0k, |q| = 5.477226
q · q⁻¹ ≈ 1: true
i · j = 0.0 + 0.0i + 0.0j + 1.0k

Constructors

Quaternionconstructor
const Quaternion(this.w, [this.x = 0, this.y = 0, this.z = 0])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  const q = Quaternion(1, 2, 3, 4);
  print('q = $q, q·q⁻¹ = ${q * q.inverse}');
}
Output
q = 1.0 + 2.0i + 3.0j + 4.0k, q·q⁻¹ = 1.0 + 0.0i + 0.0j - 2.7755575615628914e-17k
factory Quaternion.axisAngle(double ax, double ay, double az, double theta)

The unit quaternion \cos(\frac{\theta}{2}) + \sin(\frac{\theta}{2}) \hat{n}, which rotates vectors by \theta about n under q v q*.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // A quarter turn about z.
  final q = Quaternion.axisAngle(0, 0, 1, 3.141592653589793 / 2);
  print('q = $q, rotates (1,0,0) to ${q.rotate(1, 0, 0)}');
}
Output
q = 0.7071067811865476 + 0.0i + 0.0j + 0.7071067811865475k, rotates (1,0,0) to (2.220446049250313e-16, 1.0, 0.0)

Static

oneconstant
static const Quaternion one = Quaternion(1)
iconstant
static const Quaternion i = Quaternion(0, 1)
jconstant
static const Quaternion j = Quaternion(0, 0, 1)
kconstant
static const Quaternion k = Quaternion(0, 0, 0, 1)

Properties

wfield
final double w
xfield
final double x
yfield
final double y
zfield
final double z
conjugategetter
Quaternion get conjugate
double get normSquared
q² = q q*.
normgetter
double get norm
inversegetter
Quaternion get inverse
Quaternion get normalized
hashCodegetter
int get hashCode

Methods

scalemethod
Quaternion scale(double s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${const Quaternion(1, 2, 3, 4).scale(0.5)}');
}
Output
0.5 + 1.0i + 1.5j + 2.0k
rotatemethod
(double, double, double) rotate(double vx, double vy, double vz)

q v q^{-1} for the pure quaternion v = (vx, vy, vz): the rotation of v when q is a unit quaternion.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = Quaternion.axisAngle(1, 1, 1, 2 * 3.141592653589793 / 3);
  print('(1,0,0) ↦ ${q.rotate(1, 0, 0)}  (exactly (0,1,0); doubles leave small residues)');
}
Output
(1,0,0) ↦ (1.1102230246251565e-16, 1.0, -1.1102230246251565e-16)  (exactly (0,1,0); doubles leave small residues)
expmethod
Quaternion exp()

e^{q}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('exp(iπ/2) = ${const Quaternion(0, 3.141592653589793 / 2).exp()}');
}
Output
exp(iπ/2) = 6.123233995736766e-17 + 1.0i + 0.0j + 0.0k
logmethod
Quaternion log()

Principal logarithm.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('log(i) = ${Quaternion.i.log()}');
}
Output
log(i) = 0.0 + 1.5707963267948966i + 0.0j + 0.0k
slerpmethod
Quaternion slerp(Quaternion to, double t)

Spherical linear interpolation between unit quaternions (shortest arc).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = Quaternion.one, b = Quaternion.axisAngle(0, 0, 1, 3.141592653589793 / 2);
  for (final t in [0.0, 0.5, 1.0]) print('t = $t: ${a.slerp(b, t)}');
}
Output
t = 0.0: 1.0 + 0.0i + 0.0j + 0.0k
t = 0.5: 0.9238795325112868 + 0.0i + 0.0j + 0.3826834323650898k
t = 1.0: 0.7071067811865476 + 0.0i + 0.0j + 0.7071067811865475k
List<List<double>> toRotationMatrix()

The 3 \times 3 rotation matrix of a unit quaternion.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(Quaternion.axisAngle(0, 0, 1, 3.141592653589793 / 2).toRotationMatrix());
}
Output
[[2.220446049250313e-16, -1.0, 0.0], [1.0, 2.220446049250313e-16, 0.0], [0.0, 0.0, 1.0]]
toSu2method
CMatrix toSu2()

The SU(2) matrix: i \mapsto -i\sigma_{1}, j \mapsto -i\sigma_{2}, k \mapsto -i\sigma_{3}, so w + \xi + yj + zk \mapsto \begin{bmatrix}w - iz & -y - ix \\ y - ix & w + iz\end{bmatrix}. A homomorphism; the axis-angle quaternion maps to su2Rotation with the same axis and angle.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(Quaternion.j.toSu2());
}
Output
0.0+-0.0i -1.0+-0.0i
1.0+-0.0i 0.0+0.0i
bool approxEquals(Quaternion o, [double tol = 1e-12])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = Quaternion.axisAngle(0, 1, 0, 0.3) * Quaternion.axisAngle(0, 1, 0, 0.4);
  print('equal to the 0.7 rotation: ${q == Quaternion.axisAngle(0, 1, 0, 0.7)}, approximately: ${q.approxEquals(Quaternion.axisAngle(0, 1, 0, 0.7))}');
}
Output
equal to the 0.7 rotation: false, approximately: true
toStringmethod
String toString()

Operators

operator +operator
Quaternion operator +(Quaternion o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Quaternion.i + Quaternion.j}');
}
Output
0.0 + 1.0i + 1.0j + 0.0k
operator -operator
Quaternion operator -(Quaternion o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${const Quaternion(1, 2, 3, 4) - Quaternion.one}');
}
Output
0.0 + 2.0i + 3.0j + 4.0k
operator -operator
Quaternion operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-Quaternion.k}');
}
Output
-0.0 + 0.0i + 0.0j - 1.0k
operator *operator
Quaternion operator *(Quaternion o)

The Hamilton product.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('i·j = ${Quaternion.i * Quaternion.j}, j·i = ${Quaternion.j * Quaternion.i}, i·j·k = ${Quaternion.i * Quaternion.j * Quaternion.k}');
}
Output
i·j = 0.0 + 0.0i + 0.0j + 1.0k, j·i = 0.0 + 0.0i + 0.0j - 1.0k, i·j·k = -1.0 + 0.0i + 0.0j + 0.0k
operator /operator
Quaternion operator /(Quaternion o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('k / j = ${Quaternion.k / Quaternion.j}');
}
Output
k / j = 0.0 + 1.0i + 0.0j + 0.0k
operator ==operator
bool operator ==(Object other)
final class MatrixLieAlgebra

A Lie algebra spanned by linearly independent matrices, with bracket the commutator.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // su(2) from the Pauli matrices: structure constants and the Killing form.
  final g = MatrixLieAlgebra([for (final s in pauli) s.scale(Complex(0, -0.5))]);
  print('dimension ${g.dimension}, Jacobi defect ${g.jacobiDefect}');
  print('Killing form:\n${g.killingForm}');
}
Output
dimension 3, Jacobi defect 0.0
Killing form:
-2.0+0.0i 0.0+0.0i 0.0+0.0i
0.0+0.0i -2.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i -2.0+0.0i

Constructors

MatrixLieAlgebraconstructor
MatrixLieAlgebra(List<CMatrix> basis)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = MatrixLieAlgebra(so3Generators);
  print('so(3): c_012 = ${g.structureConstants[0][1][2]}');
}
Output
so(3): c_012 = (1.0+0.0j)

Properties

basisfield
final List<CMatrix> basis
dimensiongetter
int get dimension
isClosedgetter
bool get isClosed

Closed under the bracket.

late final List<List<List<Complex>>> structureConstants

c_{\mathrm{abc}} with [X_{a}, X_{b}] = \sum_{c} c_{\mathrm{abc}} X_{c}.

List<List<List<double>>> get physicsStructureConstants

For a Hermitian basis T_{a} with [T_{a}, T_{b}] = i f_{\mathrm{abc}} T_{c}, the real f_{\mathrm{abc}} (= -i c_{\mathrm{abc}}).

CMatrix get killingForm

The Killing form K_{ab} = \operatorname{Tr}(ad X_{a} ad X_{b}).

double get jacobiDefect

Max |\sum_{d} (c_{\mathrm{abd}} c_{\mathrm{dce}} + c_{\mathrm{bcd}} c_{\mathrm{dae}} + c_{\mathrm{cad}} c_{\mathrm{dbe}})| over all a, b, c, e (zero for a Lie algebra).

Methods

List<Complex> coordinates(CMatrix y)

Coordinates of a matrix in the basis (least squares; exact when it lies in the span).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = MatrixLieAlgebra(so3Generators);
  final y = so3Generators[0].scaleReal(2) + so3Generators[2].scaleReal(-1);
  print('${g.coordinates(y)}');
}
Output
[(2.0+0.0j), (0.0+0.0j), (-1.0+0.0j)]
spansmethod
bool spans(CMatrix y, [double tol = 1e-10])

Whether y lies in the span (residual below tol).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = MatrixLieAlgebra(so3Generators);
  print('[L1, L2] in so(3): ${g.spans(so3Generators[0].commutator(so3Generators[1]))}, identity: ${g.spans(CMatrix.identity(3))}');
}
Output
[L1, L2] in so(3): true, identity: false
adjointmethod
CMatrix adjoint(int a)

The adjoint matrix \operatorname{ad}(X_{a}): (ad X_{a})_cb = c_abc.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(MatrixLieAlgebra(so3Generators).adjoint(2));
}
Output
0.0+0.0i -1.0+0.0i 0.0+0.0i
1.0+0.0i 0.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i 0.0+0.0i
class

Irrep

final class Irrep

The irreducible representation V(\lambda) of the simple Lie algebra with root system rootSystem.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The adjoint of su(3): dimension 8, Casimir 3 in the physics normalisation.
  final adj = Irrep.of(RootSystem.a(2), [1, 1]);
  print('dimension ${adj.dimension}, Casimir ${adj.casimirPhysics}, weights ${adj.weights.length}');
}
Output
dimension 8, Casimir 3, weights 7

Constructors

Irrepconstructor
Irrep(this.rootSystem, this.highestWeight)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final v = Irrep(RootSystem.g2(), Weight([1, 0]));
  print('G2 V(ω1): dimension ${v.dimension}');
}
Output
G2 V(ω1): dimension 7
Irrep.offactory
factory Irrep.of(RootSystem rs, List<int> labels)

V(\lambda) from Dynkin labels.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final q = Irrep.of(RootSystem.a(2), [1, 0]);
  print('quark triplet: dim ${q.dimension}, Casimir ${q.casimirPhysics}, dual ${q.dual.highestWeight}');
}
Output
quark triplet: dim 3, Casimir 4/3, dual (0,1)

Properties

final RootSystem rootSystem
final Weight highestWeight
dimensiongetter
BigInt get dimension

Weyl dimension formula \prod_{\alpha > 0} \frac{(\lambda + \rho, \alpha)}{(\rho, \alpha)}, exact.

casimirgetter
Rational get casimir

The quadratic Casimir (\lambda, \lambda + 2\rho) with long roots of length² 2. For su(2) this is 2j(j + 1)\cdots divided by 2: the physics normalisation (\operatorname{Tr} T_{a} T_{b} = \frac{\delta_{ab}}{2} in the fundamental of su(n)) is casimirPhysics.

Rational get casimirPhysics

C_{2} in the physics normalisation: j(j + 1) for su(2), \frac{4}{3} for the quark triplet of su(3), N for the adjoint of su(N).

Rational get dynkinIndex

The Dynkin index of the representation, normalised so the fundamental of su(n) has index \frac{1}{2}: T(R) = \dim(R) C_{2}(R) / \dim(g).

dualgetter
Irrep get dual

The dual (contragredient) representation, highest weight -w_{0}\lambda.

Map<Weight, int> get dominantWeights

Dominant weights with their multiplicities, from the highest weight downwards.

weightsgetter
Map<Weight, int> get weights

Every weight with its multiplicity (the weight diagram).

Methods

int multiplicity(Weight w)

The multiplicity of a weight (0 if absent).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // The zero weight appears twice in the adjoint of su(3).
  final adj = Irrep.of(RootSystem.a(2), [1, 1]);
  print('mult(0) = ${adj.multiplicity(Weight.zero(2))}, mult(highest) = ${adj.multiplicity(Weight([1, 1]))}');
}
Output
mult(0) = 2, mult(highest) = 1
toStringmethod
String toString()
class

Weight

final class Weight

A weight in Dynkin labels (coefficients on the fundamental weights), immutable with value equality.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final w = Weight([2, -1]);
  print('w = $w, 3w = ${w.times(3)}, w + ρ = ${w + Weight([1, 1])}');
}
Output
w = (2,-1), 3w = (6,-3), w + ρ = (3,0)

Constructors

Weightfactory
factory Weight(List<int> labels)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight([1, 0, 1])}');
}
Output
(1,0,1)
factory Weight.zero(int rank)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight.zero(4)}');
}
Output
(0,0,0,0)

Properties

rankgetter
int get rank
labelsgetter
List<int> get labels
bool get isDominant
isZerogetter
bool get isZero
hashCodegetter
int get hashCode

Methods

timesmethod
Weight times(int k)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight([1, 1]).times(2)}');
}
Output
(2,2)
toStringmethod
String toString()

Operators

operator []operator
int operator [](int i)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight([3, 5])[1]}');
}
Output
5
operator +operator
Weight operator +(Weight o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight([1, 0]) + Weight([0, 1])}');
}
Output
(1,1)
operator -operator
Weight operator -(Weight o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${Weight([1, 1]) - Weight([2, -1])}');
}
Output
(-1,2)
operator -operator
Weight operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${-Weight([1, -2])}');
}
Output
(-1,2)
operator ==operator
bool operator ==(Object other)
enum LieFamily

The Cartan–Killing type of a simple Lie algebra.

Values

avalue
a
bvalue
b
cvalue
c
dvalue
d
evalue
e
fvalue
f
gvalue
g
final class RootSystem

A simple root system of type X_{n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e8 = RootSystem.e(8);
  print('E8: ${e8.positiveRoots.length} positive roots, |W| = ${e8.weylGroupOrder}, Coxeter number ${e8.coxeterNumber}');
}
Output
E8: 120 positive roots, |W| = 696729600, Coxeter number 30

Constructors

factory RootSystem.a(int n)

A_{n} (su(n + 1)), n \ge 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final a3 = RootSystem.a(3);
  print('A3: Cartan ${a3.cartan}, positive roots ${a3.positiveRoots.length}, |W| = ${a3.weylGroupOrder}');
}
Output
A3: Cartan [[2, -1, 0], [-1, 2, -1], [0, -1, 2]], positive roots 6, |W| = 24
factory RootSystem.b(int n)

B_{n} (so(2n + 1)), n \ge 2: \alpha_{n} short.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final b3 = RootSystem.b(3);
  print('B3: Cartan ${b3.cartan}, |W| = ${b3.weylGroupOrder}');
}
Output
B3: Cartan [[2, -1, 0], [-1, 2, -2], [0, -1, 2]], |W| = 48
factory RootSystem.c(int n)

C_{n} (sp(2n)), n \ge 2 (C_{2} \cong B_{2}): \alpha_{n} long.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('C3: |W| = ${RootSystem.c(3).weylGroupOrder}, exponents ${RootSystem.c(3).exponents}');
}
Output
C3: |W| = 48, exponents [1, 3, 5]
factory RootSystem.d(int n)

D_{n} (so(2n)), n \ge 3 (D_{3} \cong A_{3}): \alpha_{n - 2} joined to \alpha_{n - 1} and \alpha_{n}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('D4: degrees ${RootSystem.d(4).degrees}, |W| = ${RootSystem.d(4).weylGroupOrder}');
}
Output
D4: degrees [2, 4, 4, 6], |W| = 192
factory RootSystem.e(int n)

E_{6}, E_{7}, E_{8} (Bourbaki: 1–3–4–5–6–7–8 with 2 attached to 4).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final n in [6, 7, 8]) print('E$n: |W| = ${RootSystem.e(n).weylGroupOrder}, h = ${RootSystem.e(n).coxeterNumber}');
}
Output
E6: |W| = 51840, h = 12
E7: |W| = 2903040, h = 18
E8: |W| = 696729600, h = 30
factory RootSystem.f4()

F_{4}: \alpha1–\alpha2 \Rightarrow \alpha3–\alpha4 (\alpha1, \alpha2 long).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  final f = RootSystem.f4();
  print('F4: Cartan ${f.cartan}, |W| = ${f.weylGroupOrder}');
}
Output
F4: Cartan [[2, -1, 0, 0], [-1, 2, -2, 0], [0, -1, 2, -1], [0, 0, -1, 2]], |W| = 1152
factory RootSystem.g2()

G_{2}: \alpha1 short, \alpha2 long.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = RootSystem.g2();
  print('G2: positive roots ${g.positiveRoots}, |W| = ${g.weylGroupOrder}');
}
Output
G2: positive roots [(2,-1), (-3,2), (-1,1), (1,0), (3,-1), (0,1)], |W| = 12
factory RootSystem.parse(String type)

Parses A2, B3, E8, F4, G2, …

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  for (final t in ['A4', 'B2', 'E7']) print('$t: rank ${RootSystem.parse(t).rank}, dimension ${RootSystem.parse(t).dimension}');
}
Output
A4: rank 4, dimension 24
B2: rank 2, dimension 10
E7: rank 7, dimension 133

Properties

familyfield
final LieFamily family
rankfield
final int rank
cartanfield
final List<List<int>> cartan

a_{ij} = \langle\alpha_{i}, \alpha_{j}\wedge \vee \rangle.

late final List<Rational> halfLengths

\frac{(\alpha_{i}, \alpha_{i})}{2} for each simple root (1 for long roots).

late final List<List<Rational>> rootForm

(\alpha_{i}, \alpha_{j}).

late final List<List<Rational>> weightForm

(\omega_{i}, \omega_{j}): the quadratic form on weights in Dynkin labels.

late final List<List<int>> positiveRootsSimple

Positive roots in simple-root coordinates, sorted by height (simple roots first).

late final List<Weight> positiveRoots

Positive roots as weights (Dynkin labels).

namegetter
String get name
dimensiongetter
int get dimension
rhogetter
Weight get rho

\rho = half the sum of the positive roots = \sum \omega_{i}.

Weight get highestRoot

The highest root \theta (the highest weight of the adjoint representation).

exponentsgetter
List<int> get exponents

The exponents m_{i}, from the heights of the positive roots (Kostant): the number of exponents equal to k is (#roots of height k) − (#roots of height k + 1).

degreesgetter
List<int> get degrees

Degrees of the basic invariants, m_{i} + 1.

BigInt get weylGroupOrder
W= \prod d_{i}.
int get coxeterNumber

The Coxeter number h = \operatorname{height}(\theta) + 1.

int get dualCoxeterNumber

The dual Coxeter number h \vee = 1 + (\theta, \rho) (long roots of length² 2).

List<int> get longestElement

A reduced word for the longest element w_{0} (it sends \rho to -\rho; length = number of positive roots).

Methods

Weight simpleRoot(int i)

The simple root \alpha_{i} as a weight.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a2 = RootSystem.a(2);
  print('α1 = ${a2.simpleRoot(0)}, α2 = ${a2.simpleRoot(1)}');
}
Output
α1 = (2,-1), α2 = (-1,2)
Weight fundamental(int i)

The fundamental weight \omega_{i}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('ω2 of A3: ${RootSystem.a(3).fundamental(1)}');
}
Output
ω2 of A3: (0,1,0)
Weight rootToWeight(List<int> c)

A root in simple-root coordinates → Dynkin labels.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('α1 + α2 in A2: ${RootSystem.a(2).rootToWeight([1, 1])}');
}
Output
α1 + α2 in A2: (1,1)
List<Rational> weightToRootCoordinates(Weight w)

Dynkin labels → simple-root coordinates (rational in general).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('ω1 of A2 = ${RootSystem.a(2).weightToRootCoordinates(Weight([1, 0]))} in simple roots');
}
Output
ω1 of A2 = [2/3, 1/3] in simple roots
innermethod
Rational inner(Weight a, Weight b)

(\lambda, \mu) for weights in Dynkin labels.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a2 = RootSystem.a(2);
  print('(α1, α1) = ${a2.inner(a2.simpleRoot(0), a2.simpleRoot(0))}, (α1, α2) = ${a2.inner(a2.simpleRoot(0), a2.simpleRoot(1))}, (ω1, ω1) = ${a2.inner(Weight([1, 0]), Weight([1, 0]))}');
}
Output
(α1, α1) = 2, (α1, α2) = -1, (ω1, ω1) = 2/3
reflectmethod
Weight reflect(Weight w, int i)

The simple reflection s_{i}: \lambda \mapsto \lambda - \lambda_{i} \alpha_{i}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  print('s1(ω1) in A2: ${RootSystem.a(2).reflect(Weight([1, 0]), 0)}');
}
Output
s1(ω1) in A2: (-1,1)
applyWordmethod
Weight applyWord(List<int> word, Weight w)

Applies w = s_(i1) s_(i2) ⋯ s_(ik) (the rightmost reflection acts first).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('s1 s2 (ρ) = ${RootSystem.a(2).applyWord([0, 1], Weight([1, 1]))}');
}
Output
s1 s2 (ρ) = (-2,1)
(Weight, int) dominantConjugate(Weight w)

The dominant weight in the Weyl orbit of w and the parity (-1)^{\ell} of the element used.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${RootSystem.a(2).dominantConjugate(Weight([-1, -1]))}');
}
Output
((1,1), -1)
orbitmethod
List<Weight> orbit(Weight w)

The Weyl orbit of a weight.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('Weyl orbit of ω1 in B2: ${RootSystem.b(2).orbit(Weight([1, 0]))}');
}
Output
Weyl orbit of ω1 in B2: [(1,0), (-1,2), (1,-2), (-1,0)]
List<List<int>> weylGroupElements({int limit = 5000000})

Every Weyl group element as a reduced word, by breadth-first search on the (free, regular) orbit of \rho; words come out in order of length. Use only for small groups.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final els = RootSystem.a(2).weylGroupElements();
  print('${els.length} elements of W(A2) as reduced words: $els');
}
Output
6 elements of W(A2) as reduced words: [[], [0], [1], [1, 0], [0, 1], [0, 1, 0]]
lengthmethod
int length(List<int> word)

The length of an element given by any word (number of positive roots it sends negative), via its action on \rho.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a3 = RootSystem.a(3);
  print('length of s1 s2 s1 = ${a3.length([0, 1, 0])}, of s1 s1 = ${a3.length([0, 0])}, of w0 = ${a3.length(a3.longestElement)}');
}
Output
length of s1 s2 s1 = 3, of s1 s1 = 0, of w0 = 6
dualmethod
Weight dual(Weight lambda)

The highest weight of the dual representation, -w_{0}\lambda.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('dual of su(3) (1,0): ${RootSystem.a(2).dual(Weight([1, 0]))}; of so(5) ω1: ${RootSystem.b(2).dual(Weight([1, 0]))}');
}
Output
dual of su(3) (1,0): (0,1); of so(5) ω1: (1,0)
toStringmethod
String toString()
class

CMatrix

final class CMatrix

An immutable rows × cols complex matrix.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = pauli;
  print('σ1σ2 = iσ3: ${(s[0] * s[1]).approxEquals(s[2].scale(Complex(0, 1)))}');
}
Output
σ1σ2 = iσ3: true

Constructors

CMatrixfactory
factory CMatrix(List<List<Complex>> entries)

From nested rows of Complex.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final m = CMatrix([[Complex(1, 0), Complex(0, -1)], [Complex(0, 1), Complex(1, 0)]]);
  print('Hermitian: ${m.approxEquals(m.dagger)}, det ${m.determinant}');
}
Output
Hermitian: true, det (0.0+0.0j)
factory CMatrix.real(List<List<num>> entries)

From nested rows of real numbers.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(CMatrix.real([[1, 2], [3, 4.5]]));
}
Output
1.0+0.0i 2.0+0.0i
3.0+0.0i 4.5+0.0i
factory CMatrix.generate(int rows, int cols, Complex Function(int i, int j) f)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(CMatrix.generate(2, 3, (i, j) => Complex(i.toDouble(), j.toDouble())));
}
Output
0.0+0.0i 0.0+1.0i 0.0+2.0i
1.0+0.0i 1.0+1.0i 1.0+2.0i
factory CMatrix.zero(int rows, [int? cols])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(CMatrix.zero(2, 3));
}
Output
0.0+0.0i 0.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i 0.0+0.0i
factory CMatrix.identity(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(CMatrix.identity(3));
}
Output
1.0+0.0i 0.0+0.0i 0.0+0.0i
0.0+0.0i 1.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i 1.0+0.0i
factory CMatrix.diagonal(List<Complex> d)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(CMatrix.diagonal([Complex(1, 0), Complex(0, 1)]));
}
Output
1.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+1.0i

Properties

rowsfield
final int rows
colsfield
final int cols
isSquaregetter
bool get isSquare
daggergetter
CMatrix get dagger

Conjugate transpose.

transposegetter
CMatrix get transpose
conjugategetter
CMatrix get conjugate
tracegetter
Complex get trace
normgetter
double get norm

Frobenius norm.

Complex get determinant

Determinant (Gaussian elimination with partial pivoting).

inversegetter
CMatrix get inverse

Inverse by Gauss–Jordan elimination.

Methods

atmethod
Complex at(int i, int j)

Entry (i, j).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${pauli[1].at(0, 1)}');
}
Output
(-0.0-1.0j)
scalemethod
CMatrix scale(Complex s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[0].scale(Complex(0, 1)));
}
Output
0.0+0.0i 0.0+1.0i
0.0+1.0i 0.0+0.0i
scaleRealmethod
CMatrix scaleReal(double s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[2].scaleReal(0.5));
}
Output
0.5+0.0i 0.0+0.0i
0.0+0.0i -0.5+-0.0i
CMatrix commutator(CMatrix o)

[A, B] = AB − BA.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // [σ1, σ2] = 2iσ3.
  print(pauli[0].commutator(pauli[1]));
}
Output
0.0+2.0i 0.0+0.0i
0.0+0.0i 0.0-2.0i
CMatrix anticommutator(CMatrix o)

\{A, B\} = AB + BA.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[0].anticommutator(pauli[0]));
}
Output
2.0+0.0i 0.0+0.0i
0.0+0.0i 2.0+0.0i
kronmethod
CMatrix kron(CMatrix o)

Kronecker product A \otimes B.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final k = pauli[0].kron(pauli[2]);
  print('${k.rows}×${k.cols}:\n$k');
}
Output
4×4:
0.0+0.0i 0.0+0.0i 1.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+-0.0i 0.0+0.0i -1.0+-0.0i
1.0+0.0i 0.0+0.0i 0.0+0.0i 0.0+0.0i
0.0+0.0i -1.0+-0.0i 0.0+0.0i 0.0+-0.0i
innermethod
Complex inner(CMatrix o)

Frobenius inner product \operatorname{Tr}(A^{\dagger} B).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('Tr(σ1† σ1) = ${pauli[0].inner(pauli[0])}, Tr(σ1† σ2) = ${pauli[0].inner(pauli[1])}');
}
Output
Tr(σ1† σ1) = (2.0+0.0j), Tr(σ1† σ2) = (0.0+0.0j)
distancemethod
double distance(CMatrix o)

Max-entry distance to o.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${pauli[0].distance(pauli[2])}');
}
Output
1.0
bool approxEquals(CMatrix o, [double tol = 1e-12])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final u = su2Rotation(0.1, [0, 0, 1]);
  print('U U† ≈ I: ${(u * u.dagger).approxEquals(CMatrix.identity(2))}');
}
Output
U U† ≈ I: true
expmethod
CMatrix exp()

The matrix exponential e^{A} (expm on complex128).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // exp(−iπσ3/2) = −iσ3.
  print(pauli[2].scale(Complex(0, -3.141592653589793 / 2)).exp());
}
Output
1.5013372914324722e-16-1.0i 0.0+0.0i
0.0+0.0i 1.5013372914324722e-16+1.0i
toListmethod
List<List<Complex>> toList()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[1].toList());
}
Output
[[(0.0+0.0j), (-0.0-1.0j)], [(0.0+1.0j), (0.0+0.0j)]]
toStringmethod
String toString()

Operators

operator []operator
Complex operator [](int k)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Row-major flat index.
  print('${pauli[1][1]}, ${pauli[1][2]}');
}
Output
(-0.0-1.0j), (0.0+1.0j)
operator +operator
CMatrix operator +(CMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[0] + pauli[2]);
}
Output
1.0+0.0i 1.0+0.0i
1.0+0.0i -1.0+0.0i
operator -operator
CMatrix operator -(CMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[0] - pauli[0]);
}
Output
0.0+0.0i 0.0+0.0i
0.0+0.0i 0.0+0.0i
operator -operator
CMatrix operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(-pauli[2]);
}
Output
-1.0+-0.0i -0.0+-0.0i
-0.0+-0.0i 1.0+0.0i
operator *operator
CMatrix operator *(CMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(pauli[0] * pauli[0]);
}
Output
1.0+0.0i 0.0+0.0i
0.0+0.0i 1.0+0.0i
final class QIMatrix

An immutable rows × cols matrix over Q(i).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Matrices over ℚ(i): exact Pauli algebra.
  final s = exactPauli;
  print('σ1σ2 == iσ3: ${s[0] * s[1] == s[2].scale(GaussianRational.i)}');
}
Output
σ1σ2 == iσ3: true

Constructors

QIMatrixfactory
factory QIMatrix(List<List<GaussianRational>> entries)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final m = QIMatrix([[GaussianRational.one, GaussianRational.i], [GaussianRational.i.scale(Rational.fromInt(-1)), GaussianRational.integer(2)]]);
  print('m⁻¹ =\n${m.inverse}');
}
Output
m⁻¹ =
2	-i
i	1
factory QIMatrix.ints(List<List<Object>> entries)

From integer entries (re, im) pairs or plain ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.ints([[1, (0, 1)], [(0, -1), 1]]));
}
Output
1	i
-i	1
factory QIMatrix.generate(int rows, int cols, GaussianRational Function(int i, int j) f)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.generate(2, 2, (i, j) => GaussianRational.integer(i, j)));
}
Output
0	i
1	1+i
factory QIMatrix.zero(int rows, [int? cols])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.zero(2));
}
Output
0	0
0	0
factory QIMatrix.identity(int n)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.identity(2));
}
Output
1	0
0	1

Static

blockstaticmethod
static QIMatrix block(QIMatrix a, QIMatrix b, QIMatrix c, QIMatrix d)

Block matrix \begin{bmatrix}a & b \\ c & d\end{bmatrix}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final i = QIMatrix.identity(1), z = QIMatrix.zero(1);
  print(QIMatrix.block(z, i, i.scaleInt(-1), z));
}
Output
0	1
-1	0

Properties

rowsfield
final int rows
colsfield
final int cols
transposegetter
QIMatrix get transpose
daggergetter
QIMatrix get dagger
conjugategetter
QIMatrix get conjugate
tracegetter
GaussianRational get trace
isZerogetter
bool get isZero
isRealgetter
bool get isReal
bool get isImaginary
GaussianRational? get scalarValue

Whether this is s \cdot I for some scalar s (returned), else null.

inversegetter
QIMatrix get inverse

Exact inverse (Gauss–Jordan over Q(i)).

hashCodegetter
int get hashCode

Methods

atmethod
GaussianRational at(int i, int j)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${exactPauli[1].at(1, 0)}');
}
Output
i
scalemethod
QIMatrix scale(GaussianRational s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.identity(2).scale(GaussianRational.integer(1, 1)));
}
Output
1+i	0
0	1+i
QIMatrix scaleRational(Rational s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.identity(2).scaleRational(Rational.fromInt(1, 3)));
}
Output
1/3	0
0	1/3
scaleIntmethod
QIMatrix scaleInt(int k)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(QIMatrix.identity(2).scaleInt(-2));
}
Output
-2	0
0	-2
QIMatrix commutator(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print(s[0].commutator(s[1]));
}
Output
2i	0
0	-2i
QIMatrix anticommutator(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print('{σ1, σ2} = 0: ${s[0].anticommutator(s[1]) == QIMatrix.zero(2)}');
}
Output
{σ1, σ2} = 0: true
kronmethod
QIMatrix kron(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print(s[2].kron(s[2]));
}
Output
1	0	0	0
0	-1	0	0
0	0	-1	0
0	0	0	1
toCMatrixmethod
CMatrix toCMatrix()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(exactPauli[1].toCMatrix());
}
Output
0.0+0.0i 0.0-1.0i
0.0+1.0i 0.0+0.0i
applymethod
List<Complex> apply(List<Complex> v)

Applies to a numeric vector.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${exactPauli[0].apply([Complex(1, 0), Complex(0, 0)])}');
}
Output
[(0.0+0.0j), (1.0+0.0j)]
toStringmethod
String toString()

Operators

operator +operator
QIMatrix operator +(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print(s[0] + s[2]);
}
Output
1	1
1	-1
operator -operator
QIMatrix operator -(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print(s[0] - s[0]);
}
Output
0	0
0	0
operator -operator
QIMatrix operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(-exactPauli[1]);
}
Output
0	i
-i	0
operator *operator
QIMatrix operator *(QIMatrix o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final s = exactPauli;
  print('σ2² = I: ${s[1] * s[1] == QIMatrix.identity(2)}');
}
Output
σ2² = I: true
operator ==operator
bool operator ==(Object other)
final class PeriodicContinuedFraction

The eventually periodic continued fraction of a quadratic irrational.

Constructors

const PeriodicContinuedFraction(this.preperiod, this.period)

Properties

final List<BigInt> preperiod

The terms before the period (for sqrt(n) just [floor(sqrt n)]).

periodfield
final List<BigInt> period

One full period.

Methods

takemethod
List<BigInt> take(int count)

The first count terms.

toStringmethod
String toString()
class

EcPoint

final class EcPoint

A point on an EllipticCurve over Q: affine (x, y) or the point at infinity.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, 0, 0, -2, 0]);
  final p = EcPoint.ints(2, 2);
  print('$p on y² = x³ − 2x: ${e.contains(p)}');
}
Output
(2, 2) on y² = x³ − 2x: true

Constructors

EcPointconstructor
const EcPoint(Rational this.x, Rational this.y)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EcPoint(Rational.fromInt(1, 4), Rational.fromInt(-3, 8))}');
}
Output
(1/4, -3/8)
EcPoint.infinityconstructor
const EcPoint.infinity()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('O = ${EcPoint.infinity()}, isInfinity ${EcPoint.infinity().isInfinity}');
}
Output
O = O, isInfinity true
factory EcPoint.ints(int x, int y)

(x, y) from ints.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EcPoint.ints(3, 5)}');
}
Output
(3, 5)

Properties

xfield
final Rational? x
yfield
final Rational? y
final bool isInfinity
hashCodegetter
int get hashCode

Methods

toStringmethod
String toString()

Operators

operator ==operator
bool operator ==(Object other)
final class EllipticCurve

An elliptic curve y^2 + a1 xy + a3 y = x^3 + a2 x^2 + a4 x + a6 over Q, with exact arithmetic.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // y² = x³ − 2 over the rationals: (3, 5) is on it, and doubling gives a new rational point.
  final e = EllipticCurve.fromInts([0, 0, 0, 0, -2]);
  final p = EcPoint.ints(3, 5);
  print('E: $e');
  print('P = $p, on the curve: ${e.contains(p)}');
  print('2P = ${e.double(p)}');
}
Output
E: y^2 + (0)xy + (0)y = x^3 + (0)x^2 + (0)x + (-2)
P = (3, 5), on the curve: true
2P = (129/100, -383/1000)

Constructors

EllipticCurveconstructor
EllipticCurve(this.a1, this.a2, this.a3, this.a4, this.a6)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve(Rational.zero, Rational.zero, Rational.one, -Rational.one, Rational.zero);
  print('y² + y = x³ − x (37a): Δ = ${e.discriminant}, j = ${e.jInvariant}');
}
Output
y² + y = x³ − x (37a): Δ = 37, j = 110592/37
factory EllipticCurve.fromInts(List<int> a)

From integer coefficients [a1, a2, a3, a4, a6] (the Cremona/LMFDB ordering).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, -1, 1, -10, -20]); // 11a1
  print('11a1: Δ = ${e.discriminant}, conductor ${e.conductor()}');
}
Output
11a1: Δ = -161051, conductor 11
factory EllipticCurve.short(Rational a, Rational b)

The short Weierstrass curve y^2 = x^3 + a x + b.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.short(Rational.fromInt(-1), Rational.zero);
  print('y² = x³ − x: j = ${e.jInvariant}, torsion ${e.torsionStructure()}');
}
Output
y² = x³ − x: j = 1728, torsion [2, 2]

Properties

a1field
final Rational a1
a2field
final Rational a2
a3field
final Rational a3
a4field
final Rational a4
a6field
final Rational a6
b2field
late final Rational b2
b4field
late final Rational b4
b6field
late final Rational b6
b8field
late final Rational b8
c4field
late final Rational c4
c6field
late final Rational c6
late final Rational discriminant

The discriminant Δ = −b2²b8 − 8b4³ − 27b6² + 9b2b4b6.

Rational get jInvariant

The j-invariant \frac{c_{4}^{3}}{\Delta}.

List<Rational> get coefficients

The coefficients [a1, a2, a3, a4, a6].

bool get isIntegral

Whether all a_i are integers.

badPrimesgetter
List<BigInt> get badPrimes

The primes of bad reduction of this (integral) model: the primes dividing \Delta.

bool get isSemistable

Whether the (minimal integral) model is semistable: no bad prime divides c4.

Methods

containsmethod
bool contains(EcPoint p)

Whether p is on the curve.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.short(Rational.zero, Rational.fromInt(17));
  print('(−2, 3): ${e.contains(EcPoint.ints(-2, 3))}, (−1, 4): ${e.contains(EcPoint.ints(-1, 4))}, (1, 1): ${e.contains(EcPoint.ints(1, 1))}');
}
Output
(−2, 3): true, (−1, 4): true, (1, 1): false
negatemethod
EcPoint negate(EcPoint p)

-P = (x, -y - a_{1} x - a_{3}).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, 0, 1, -1, 0]);
  print('−(0, 0) = ${e.negate(EcPoint.ints(0, 0))}');
}
Output
−(0, 0) = (0, -1)
addmethod
EcPoint add(EcPoint p, EcPoint q)

P + Q.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';

void main() {
  // Chord-and-tangent: rational points add to rational points.
  final e = EllipticCurve.short(Rational.zero, Rational.fromInt(17));
  final p = EcPoint.ints(-2, 3), q = EcPoint.ints(-1, 4);
  print('P + Q = ${e.add(p, q)}');
}
Output
P + Q = (4, -9)
doublemethod
EcPoint double(EcPoint p)

2P.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, 0, 1, -1, 0]);
  var p = EcPoint.ints(0, 0);
  for (var i = 1; i <= 4; i++) {
    p = e.double(p);
    print('2^$i P = $p');
  }
}
Output
2^1 P = (1, 0)
2^2 P = (2, -3)
2^3 P = (21/25, -69/125)
2^4 P = (480106/4225, 332513754/274625)
multiplymethod
EcPoint multiply(EcPoint p, int k)

kP (negative k multiplies -P), by double-and-add.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Heights grow fast: the denominators of [k]P on 37a.
  final e = EllipticCurve.fromInts([0, 0, 1, -1, 0]);
  for (final k in [1, 2, 3, 5, 8]) print('[$k]P = ${e.multiply(EcPoint.ints(0, 0), k)}');
}
Output
[1]P = (0, 0)
[2]P = (1, 0)
[3]P = (-1, -1)
[5]P = (1/4, -5/8)
[8]P = (21/25, -69/125)
(EllipticCurve, BigInt) integralModel()

An integral model: (E', u) with a_i' = u^i a_i integral, via (x, y) -> (u^2 x, u^3 y). u = 1 when the curve is already integral. Not minimal in general.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final (m, u) = EllipticCurve.short(Rational.fromInt(1, 4), Rational.fromInt(1, 8)).integralModel();
  print('integral model ${m.coefficients}, u = $u');
}
Output
integral model [0, 0, 0, 4, 8], u = 2
reducemethod
EllipticCurveFp reduce(BigInt p)

The reduction modulo the prime p (the model must be p-integral). The result may be singular.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, -1, 1, -10, -20]).reduce(BigInt.from(7));
  print('11a1 mod 7: ${e.countPoints()} points');
}
Output
11a1 mod 7: 10 points
apmethod
int ap(int p)

p + 1 − #Ẽ(F_p) for the reduction at the prime p, with the singular point counted when the reduction is bad. For a model minimal at p this is the trace of Frobenius at good primes and, at bad primes, 1 (split multiplicative), -1 (non-split multiplicative) or 0 (additive) — the Euler factor coefficient.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, -1, 1, -10, -20]);
  print('a_p for 11a1: ${[for (final p in [2, 3, 5, 7, 13]) e.ap(p)]}');
}
Output
a_p for 11a1: [-2, -1, 1, -2, 4]
anListmethod
List<int> anList(int n)

The coefficients a_1..a_n of the L-series (index 0 unused, set to 0), from ap and the Hecke recursion a_{p^k} = a_p a_{p^{k−1}} − p a_{p^{k−2}} at good p (a_{p^k} = a_p^k at bad p). Model minimal at bad primes assumed.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The L-series of 11a1 equals η(τ)²η(11τ)².
  final e = EllipticCurve.fromInts([0, -1, 1, -10, -20]);
  print('a_n: ${e.anList(12).sublist(1)}');
  print('η² η₁₁²: ${etaProduct({1: 2, 11: 2}, 13).coefficients.sublist(1)}');
}
Output
a_n: [1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1, -2]
η² η₁₁²: [1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1, -2]
bool isMinimalAt(BigInt p)

Whether the (integral) model is provably minimal at p: v_p(Δ) < 12 or v_p(c4) < 4. A sufficient test.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurve.fromInts([0, -1, 1, -10, -20]);
  print('minimal at 11: ${e.isMinimalAt(BigInt.from(11))}');
}
Output
minimal at 11: true
conductormethod
BigInt conductor()

The conductor, for semistable curves only: the product of the primes of (multiplicative) bad reduction.

Requires an integral model provably minimal at every bad prime (isMinimalAt); throws UnsupportedError on additive reduction (Tate's algorithm is not implemented) or a model that may be non-minimal.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('37a: ${EllipticCurve.fromInts([0, 0, 1, -1, 0]).conductor()}, 11a1: ${EllipticCurve.fromInts([0, -1, 1, -10, -20]).conductor()}');
}
Output
37a: 37, 11a1: 11
int torsionBound({int primes = 10})

A multiple of the torsion order: gcd #E(F_p) over the first primes odd primes of good reduction.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('11a1 torsion divides ${EllipticCurve.fromInts([0, -1, 1, -10, -20]).torsionBound()}');
}
Output
11a1 torsion divides 5
List<EcPoint> torsionPoints()

The rational torsion points (including O), by Nagell–Lutz on the integral model Y^2 = X^3 − 27c4 X − 54c6 and Mazur's bound (orders <= 12), mapped back to this model.

Integer roots of the cubics are located in double precision and verified exactly, so the curve's coefficients should keep |X| of torsion points below about 10^{14}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print(EllipticCurve.fromInts([0, -1, 1, -10, -20]).torsionPoints());
}
Output
[O, (5, 5), (16, 60), (5, -6), (16, -61)]
List<int> torsionStructure()

The torsion subgroup's invariants: [n] for \frac{Z}{n} ([] when trivial), or [2, 2m] for \frac{Z}{2} \times \frac{Z}{2m}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('11a1: ${EllipticCurve.fromInts([0, -1, 1, -10, -20]).torsionStructure()}, y² = x³ − x: ${EllipticCurve.short(-Rational.one, Rational.zero).torsionStructure()}');
}
Output
11a1: [5], y² = x³ − x: [2, 2]
toStringmethod
String toString()
final class EcPointFp

A point on an EllipticCurveFp: affine (x, y) with coordinates in [0, p), or the point at infinity.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  final p = EcPointFp(BigInt.from(3), BigInt.from(6));
  print('$p on the curve: ${e.contains(p)}, order ${e.orderOf(p)}');
}
Output
(3, 6) on the curve: true, order 5

Constructors

EcPointFpconstructor
const EcPointFp(BigInt this.x, BigInt this.y)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EcPointFp(BigInt.from(1), BigInt.from(2))}');
}
Output
(1, 2)
const EcPointFp.infinity()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EcPointFp.infinity()}');
}
Output
O

Properties

xfield
final BigInt? x
yfield
final BigInt? y
final bool isInfinity
hashCodegetter
int get hashCode

Methods

toStringmethod
String toString()

Operators

operator ==operator
bool operator ==(Object other)
final class EllipticCurveFp

An elliptic curve y^2 + a1 xy + a3 y = x^3 + a2 x^2 + a4 x + a6 over the prime field F_{p}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // y² = x³ − x + 1 over the field of 97 elements.
  final curve = EllipticCurveFp.fromInts([0, 0, 0, -1, 1], 97);
  final pts = curve.points();
  final p = pts[1], q = pts[3];
  print('P = $p, Q = $q');
  print('P + Q = ${curve.add(p, q)}');
  print('2P    = ${curve.double(p)}');
  print('#E(F_97) = ${curve.countPoints()}');
  print('order of P = ${curve.orderOf(p)}');
}
Output
P = (0, 1), Q = (1, 1)
P + Q = (96, 96)
2P    = (73, 84)
#E(F_97) = 98
order of P = 98

Constructors

EllipticCurveFpconstructor
EllipticCurveFp(BigInt a1, BigInt a2, BigInt a3, BigInt a4, BigInt a6, this.p, {bool allowSingular = false})

Coefficients are reduced mod p; p is assumed prime. Throws on a singular curve unless allowSingular (a singular reduction can still be point-counted).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp(BigInt.zero, BigInt.zero, BigInt.zero, BigInt.two, BigInt.from(3), BigInt.from(97));
  print('#E(F_97) = ${e.countPoints()}, j = ${e.jInvariant}');
}
Output
#E(F_97) = 100, j = 36
factory EllipticCurveFp.fromInts(List<int> a, int p, {bool allowSingular = false})

From ints [a1, a2, a3, a4, a6] and the prime p.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, -1, 0], 101);
  print('#E = ${e.countPoints()}');
}
Output
#E = 104

Properties

a1field
final BigInt a1
a2field
final BigInt a2
a3field
final BigInt a3
a4field
final BigInt a4
a6field
final BigInt a6
pfield
final BigInt p
b2field
late final BigInt b2
b4field
late final BigInt b4
b6field
late final BigInt b6
b8field
late final BigInt b8
c4field
late final BigInt c4
c6field
late final BigInt c6
late final BigInt discriminant
bool get isSingular
BigInt get jInvariant

The j-invariant c4^3 / Δ in F_{p}.

Methods

containsmethod
bool contains(EcPointFp q)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  print('${e.contains(EcPointFp(BigInt.from(3), BigInt.from(6)))}, ${e.contains(EcPointFp(BigInt.from(3), BigInt.from(7)))}');
}
Output
true, false
negatemethod
EcPointFp negate(EcPointFp q)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  print('${e.negate(EcPointFp(BigInt.from(3), BigInt.from(6)))}');
}
Output
(3, 91)
addmethod
EcPointFp add(EcPointFp q, EcPointFp r)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  final p = EcPointFp(BigInt.from(3), BigInt.from(6));
  print('P + P = ${e.add(p, p)}, P + (−P) = ${e.add(p, e.negate(p))}');
}
Output
P + P = (80, 10), P + (−P) = O
doublemethod
EcPointFp double(EcPointFp q)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  print('${e.double(EcPointFp(BigInt.from(3), BigInt.from(6)))}');
}
Output
(80, 10)
multiplymethod
EcPointFp multiply(EcPointFp q, BigInt k)

kQ by double-and-add.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 2, 3], 97);
  final p = EcPointFp(BigInt.from(3), BigInt.from(6));
  print('[5]P = ${e.multiply(p, BigInt.from(5))}');
}
Output
[5]P = O
pointsmethod
List<EcPointFp> points()

Every point (O first), for small p.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 5);
  final pts = e.points();
  print('${pts.length} points: $pts');
}
Output
9 points: [O, (0, 1), (0, 4), (2, 1), (2, 4), (3, 1), (3, 4), (4, 2), (4, 3)]
BigInt countPoints()

#E(F_p) (affine solutions plus O; a singular point counts once), by the character sum p + 1 + Σ_x ((4x^3 + b2 x^2 + 2 b4 x + b6) / p) — O(p). Brute force for p = 2.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Hasse: |#E − (p + 1)| ≤ 2√p.
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 10007);
  print('#E(F_10007) = ${e.countPoints()}');
}
Output
#E(F_10007) = 10065
EcPointFp randomPoint(math.Random rnd)

A uniformly random affine point (p odd).

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:math';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 10007);
  final p = e.randomPoint(Random(3));
  print('$p, on curve ${e.contains(p)}');
}
Output
(1955, 1239), on curve true
orderOfmethod
BigInt orderOf(EcPointFp q)

The order of the point q, by baby-step giant-step over the Hasse interval [p+1−2√p, p+1+2√p] and then removing prime factors.

Example
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:math';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 10007);
  final p = e.randomPoint(Random(3));
  print('order ${e.orderOf(p)} divides #E = ${e.groupOrder()}');
}
Output
order 3355 divides #E = 10065
BigInt groupOrder({int seed = 1})

#E(F_p): naive below p = 1000, otherwise the unique multiple in the Hasse interval of the lcm of the orders of random points (Mestre-style baby-step giant-step without the twist).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 1000003);
  print('#E(F_1000003) = ${e.groupOrder()}');
}
Output
#E(F_1000003) = 1000727
tracemethod
BigInt trace()

The trace of Frobenius p + 1 − #E(F_p).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final e = EllipticCurveFp.fromInts([0, 0, 0, 1, 1], 10007);
  print('trace of Frobenius ${e.trace()}');
}
Output
trace of Frobenius -57
toStringmethod
String toString()
final class NtComplex

A complex number in double precision (the numeric value of a character or an L-value).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final z = NtComplex(3, 4);
  print('$z, |z|² = ${(z * NtComplex(3, -4)).re}');
}
Output
3.0 + 4.0i, |z|² = 25.0

Constructors

NtComplexconstructor
const NtComplex(this.re, [this.im = 0])
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${NtComplex(1, -2) + NtComplex(0.5, 0.5)}');
}
Output
1.5 - 1.5i
factory NtComplex.unitTurn(double t)

e^{2πi t}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('e^{2πi/4} = ${NtComplex.unitTurn(0.25)}');
}
Output
e^{2πi/4} = 6.123233995736766e-17 + 1.0i

Properties

refield
final double re
imfield
final double im
absgetter
double get abs

Methods

scalemethod
NtComplex scale(double s)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${NtComplex(1, 2).scale(3)}');
}
Output
3.0 + 6.0i
toStringmethod
String toString()

Operators

operator +operator
NtComplex operator +(NtComplex o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${NtComplex(1, 2) + NtComplex(3, 4)}');
}
Output
4.0 + 6.0i
operator -operator
NtComplex operator -(NtComplex o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${NtComplex(1, 2) - NtComplex(3, 4)}');
}
Output
-2.0 - 2.0i
operator *operator
NtComplex operator *(NtComplex o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${NtComplex(1, 2) * NtComplex(3, 4)}');
}
Output
-5.0 + 10.0i
final class RootOfUnity

The exact root of unity e^{2πi k / order}, 0 <= k < order, in lowest terms.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final w = RootOfUnity(1, 3);
  print('ω = $w, ω³ = ${w * w * w}, conj ${w.conjugate()}');
}
Output
ω = e^(2πi·1/3), ω³ = 1, conj e^(2πi·2/3)

Constructors

factory RootOfUnity(int k, int order)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${RootOfUnity(2, 6)} (reduced from 2/6)');
}
Output
e^(2πi·1/3) (reduced from 2/6)

Properties

kfield
final int k
orderfield
final int order
isOnegetter
bool get isOne
valuegetter
NtComplex get value
hashCodegetter
int get hashCode

Methods

conjugatemethod
RootOfUnity conjugate()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${RootOfUnity(1, 8).conjugate()}');
}
Output
e^(2πi·7/8)
toStringmethod
String toString()

Operators

operator *operator
RootOfUnity operator *(RootOfUnity o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${RootOfUnity(1, 4) * RootOfUnity(1, 6)}');
}
Output
e^(2πi·5/12)
operator ==operator
bool operator ==(Object other)
final class DirichletGroup

The group of Dirichlet characters modulo n: all \varphi(n) characters, built from the structure of (Z/n)^×.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final g = DirichletGroup(15);
  print('(ℤ/15)^× factors ${g.factorOrders}, ${g.characters.length} characters');
}
Output
(ℤ/15)^× factors [2, 4], 8 characters

Constructors

DirichletGroupconstructor
DirichletGroup(this.modulus)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${DirichletGroup(16).factorOrders}');
}
Output
[2, 4]

Properties

modulusfield
final int modulus
List<int> get factorOrders

The orders of the cyclic factors of (Z/n)^× (their product is \varphi(n)).

List<DirichletCharacter> get characters

All \varphi(n) characters; the principal character first.

Methods

charactermethod
DirichletCharacter character(List<int> indices)

The character with exponent indices[j] on the j-th cyclic factor: χ(g_j) = e^{2πi indices[j] / ord_j}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletGroup(7).character([1]);
  print('χ mod 7: ${[for (var a = 1; a < 7; a++) chi(a)]}');
}
Output
χ mod 7: [1, e^(2πi·1/3), e^(2πi·1/6), e^(2πi·2/3), e^(2πi·5/6), -1]
final class DirichletCharacter

A Dirichlet character modulo modulus with exact values e^{2πi e(a) / order} (0 off the units).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // L(1, χ₋₄) = π/4 (Leibniz).
  final chi = DirichletCharacter.kronecker(-4);
  print('L(1, χ) = ${chi.lValue(1).re.toStringAsPrecision(12)}, π/4 = ${(3.141592653589793 / 4).toStringAsPrecision(12)}');
}
Output
L(1, χ) = 0.785398163397, π/4 = 0.785398163397

Constructors

factory DirichletCharacter.kronecker(int d)

The real character a ↦ (d / a) (Kronecker symbol) for a discriminant-like d ≡ 0, 1 (mod 4), d != 0, of modulus |d|. kronecker(-4) is \chi_{-4}, the character of Q(i).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletCharacter.kronecker(5);
  print('(5/a): ${[for (var a = 1; a <= 10; a++) chi.value(a).re.round()]}, conductor ${chi.conductor}');
}
Output
(5/a): [1, -1, -1, 1, 0, 1, -1, -1, 1, 0], conductor 5
factory DirichletCharacter.principal(int n)

The principal character modulo n.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletCharacter.principal(6);
  print('${[for (var a = 1; a <= 6; a++) chi(a)]}');
}
Output
[1, null, null, null, 1, null]

Properties

modulusfield
final int modulus
orderfield
final int order

The order of \chi in the character group.

bool get isPrincipal
isEvengetter
bool get isEven

\chi(-1) = 1.

isRealgetter
bool get isReal
conductorgetter
int get conductor

The conductor: the least d | modulus such that \chi(a) = 1 whenever a ≡ 1 (mod d) and gcd(a, modulus) = 1.

bool get isPrimitive

Methods

callmethod
RootOfUnity? call(int a)

\chi(a) exactly, or null when gcd(a, modulus) > 1.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletGroup(5).character([1]);
  print('χ(2) = ${chi(2)}, χ(5) = ${chi(5)}');
}
Output
χ(2) = e^(2πi·1/4), χ(5) = null
valuemethod
NtComplex value(int a)

\chi(a) as a complex number (0 off the units).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletGroup(5).character([1]);
  print('χ(2) ≈ ${chi.value(2)}');
}
Output
χ(2) ≈ 6.123233995736766e-17 + 1.0i
primitivemethod
DirichletCharacter primitive()

The primitive character inducing \chi (of modulus conductor).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // The character mod 12 induced from (−3/·) mod 3.
  final chi = DirichletCharacter.kronecker(-3);
  final c12 = DirichletGroup(12).characters.firstWhere((c) => c.conductor == 3);
  print('conductor ${c12.conductor}, primitive modulus ${c12.primitive().modulus}, same as (−3/·): ${c12.primitive().value(2).re == chi.value(2).re}');
}
Output
conductor 3, primitive modulus 3, same as (−3/·): true
conjugatemethod
DirichletCharacter conjugate()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final chi = DirichletGroup(5).character([1]);
  print('χ(2) = ${chi(2)}, conj χ(2) = ${chi.conjugate()(2)}');
}
Output
χ(2) = e^(2πi·1/4), conj χ(2) = e^(2πi·3/4)
gaussSummethod
NtComplex gaussSum()

The Gauss sum τ(χ) = Σ_a χ(a) e^{2πi a / modulus}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // |τ(χ)|² = q for a primitive character.
  final t = DirichletCharacter.kronecker(-7).gaussSum();
  print('τ = $t, |τ|² = ${t.re * t.re + t.im * t.im}');
}
Output
τ = 5.551115123125783e-16 + 2.6457513110645907i, |τ|² = 7.000000000000001
lValuemethod
NtComplex lValue(double s)

L(s, χ) = Σ χ(n) n^{−s} for real s (analytically continued, s != 1 for the principal character), via L(s, χ) = q^{−s} Σ_{a=1}^{q} χ(a) ζ(s, a/q) and, at s = 1, L(1, χ) = −(1/q) Σ χ(a) ψ(a/q). Double precision (about 1e-14 relative for moderate s and modulus).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('L(2, χ₋₄) = ${DirichletCharacter.kronecker(-4).lValue(2).re} (Catalan’s constant)');
}
Output
L(2, χ₋₄) = 0.9159655941772189 (Catalan’s constant)
toStringmethod
String toString()

Operators

operator *operator
DirichletCharacter operator *(DirichletCharacter o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Characters with the same modulus multiply pointwise.
  final g = DirichletGroup(7);
  final a = g.character([1]), b = g.character([2]);
  final c = a * b;
  print('(a·b)(3) = ${c(3)}, a(3)·b(3) = ${a(3)! * b(3)!}');
}
Output
(a·b)(3) = -1, a(3)·b(3) = -1
final class EllipticLSeries

The L-series of an elliptic curve over Q at s = 1, from its a_n and a conductor.

Uses the rapidly converging series from the functional equation Λ(s) = w Λ(2 − s): L(E, 1) = Σ a_n/n (e^{−2πn t/√N} + w e^{−2πn/(t√N)}) (any t > 0) and, when w = −1, L'(E, 1) = 2 Σ a_n/n E_1(2πn/√N). The root number w is found numerically from the t-independence.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // 37a has rank 1: L(E, 1) = 0 and L′(E, 1) ≈ 0.3059997738.
  final l = EllipticLSeries(EllipticCurve.fromInts([0, 0, 1, -1, 0]));
  print('root number ${l.rootNumber}, L′(E, 1) = ${l.derivativeAt1()}');
}
Output
root number -1, L′(E, 1) = 0.30599977383405197

Constructors

EllipticLSeriesconstructor
EllipticLSeries(this.curve, {BigInt? conductor})

conductor defaults to EllipticCurve.conductor (semistable curves only). The model must be integral and minimal at the bad primes.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final l = EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20]));
  print('11a1: conductor ${l.conductor}, root number ${l.rootNumber}');
}
Output
11a1: conductor 11, root number 1

Properties

curvefield
final EllipticCurve curve
final BigInt conductor
anfield
late final List<int> an

a_0.. a_terms (index 0 unused).

late final int rootNumber

The root number w = ±1 (the sign of the functional equation), determined numerically.

Methods

double functionalEquationResidual()

The residual of the functional equation |F(t1) − F(t2)| for the chosen root number (≈ 1e-16 when the conductor and coefficients are right).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20])).functionalEquationResidual()}');
}
Output
1.1102230246251565e-16
valueAt1method
double valueAt1()

L(E, 1) (0 when the root number is -1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // L(11a1, 1) ≈ 0.2538418608.
  print('${EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20])).valueAt1()}');
}
Output
0.25384186085591065
double derivativeAt1()

L'(E, 1) when the root number is -1 (odd analytic rank); throws otherwise.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${EllipticLSeries(EllipticCurve.fromInts([0, 0, 1, -1, 0])).derivativeAt1()}');
}
Output
0.30599977383405197
double partialEulerProduct(double s, int bound)

The partial Euler product ∏_{p <= bound} L_p(s)^{-1}-style truncation of L(E, s) for real s (converges for s > 3/2): good p contribute (1 − a_p p^{−s} + p^{1−2s})^{−1}, bad p (1 − a_p p^{−s})^{−1}.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final l = EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20]));
  print('s = 2, p ≤ 1000: ${l.partialEulerProduct(2, 1000)}');
}
Output
s = 2, p ≤ 1000: 0.5460478673445237
class

QSeries

final class QSeries

A power series Σ_{n < precision} (coefficients[n] / denominator) q^n, truncated at O(q^precision).

Coefficients are BigInt over one common positive denominator, kept in lowest terms, so integral series (\Delta, theta series, eta products) stay pure BigInt and rational ones (E_{12} = 1 + \frac{65520}{691} q +...) stay exact.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final a = QSeries.ints([1, 1, 0, 0, 0, 0, 0, 0]); // 1 + q
  final b = QSeries.ints([1, -1, 0, 0, 0, 0, 0, 0]); // 1 − q
  print('(1 + q)(1 − q) = ${a * b}');
  print('(1 + q)^5 = ${a.pow(5)}');
}
Output
(1 + q)(1 − q) = 1 + (-1)q^2 + O(q^8)
(1 + q)^5 = 1 + (5)q + (10)q^2 + (10)q^3 + (5)q^4 + (1)q^5 + O(q^8)

Constructors

QSeriesfactory
factory QSeries(List<BigInt> coefficients, [BigInt? denominator])

coefficients are the numerators of q^0.. q^(precision−1).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  final f = QSeries([BigInt.one, BigInt.from(3), BigInt.from(-1)], BigInt.two);
  print('${f.rationalCoefficients}, precision ${f.precision}');
}
Output
[1/2, 3/2, -1/2], precision 3
factory QSeries.ints(List<int> c)

From int coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QSeries.ints([1, -1, 0, 1]).pow(2).coefficients}');
}
Output
[1, -2, 1, 2]
factory QSeries.rationals(List<Rational> c)

From exact rationals.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QSeries.rationals([Rational.one, Rational.fromInt(1, 2), Rational.fromInt(1, 3)]).rationalCoefficients}');
}
Output
[1, 1/2, 1/3]
factory QSeries.constant(BigInt c, int precision)

The constant c + O(q^precision).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QSeries.constant(BigInt.from(5), 4).coefficients}');
}
Output
[5, 0, 0, 0]

Properties

final List<BigInt> coefficients
final BigInt denominator
precisiongetter
int get precision

The number of known coefficients: the series is known modulo q^precision.

bool get isIntegral
List<Rational> get rationalCoefficients

The coefficients as exact rationals.

hashCodegetter
int get hashCode

Methods

BigInt integerCoefficient(int n)

The coefficient of q^n (the series must be integral).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('τ(12) = ${delta(13).integerCoefficient(12)}');
}
Output
τ(12) = -370944
truncatemethod
QSeries truncate(int precision)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${thetaZ(20).truncate(6).coefficients}');
}
Output
[1, 2, 0, 0, 2, 0]
scalemethod
QSeries scale(Rational r)

Multiplication by the rational r.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${eisenstein(4, 4).scale(Rational.fromInt(1, 240)).rationalCoefficients}');
}
Output
[1/240, 1, 9, 28]
scaleIntmethod
QSeries scaleInt(int k)

Multiplication by the integer k.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${thetaZ(5).scaleInt(3).coefficients}');
}
Output
[3, 6, 0, 0, 6]
powmethod
QSeries pow(int k)

this^k for k >= 0 (repeated squaring).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // θ⁴: r_4(n), the number of ways to write n as a sum of four squares.
  print('${thetaZ(10).pow(4).coefficients}');
}
Output
[1, 8, 24, 32, 24, 48, 96, 64, 24, 104]
shiftmethod
QSeries shift(int s)

q^s · this, with s >= 0 (precision grows by s), or dropping the first -s coefficients for s < 0 (they must be 0).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QSeries.ints([1, 2, 3]).shift(2).coefficients}');
}
Output
[0, 0, 1, 2, 3]
QSeries substitutePower(int m, [int? precision])

f(q^m).

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${QSeries.ints([1, 1, 1]).substitutePower(3).coefficients}');
}
Output
[1, 0, 0, 1, 0, 0, 1]
bool agreesWith(QSeries o)

Equality of the first min(precision, o.precision) coefficients.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  // Jacobi: θ⁴ = 8 Σ (Σ_{4∤d|n} d) qⁿ, checked to 30 terms.
  final lhs = thetaZ(30).pow(4);
  final rhs = QSeries([for (var n = 0; n < 30; n++) n == 0 ? BigInt.one : BigInt.from(8 * [for (var d = 1; d <= n; d++) if (n % d == 0 && d % 4 != 0) d].fold(0, (a, b) => a + b))]);
  print('${lhs.agreesWith(rhs)}');
}
Output
true
toStringmethod
String toString()

Operators

operator []operator
Rational operator [](int n)

The coefficient of q^n as a Rational.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${eisenstein(4, 3)[1]}');
}
Output
240
operator +operator
QSeries operator +(QSeries o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(QSeries.ints([1, 1]) + QSeries.ints([0, 1, 1])).coefficients}');
}
Output
[1, 2]
operator -operator
QSeries operator -()
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(thetaZ(5) - thetaZ(5)).coefficients}');
}
Output
[0, 0, 0, 0, 0]
operator -operator
QSeries operator -(QSeries o)
Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(-QSeries.ints([1, -2, 3])).coefficients}');
}
Output
[-1, 2, -3]
operator *operator
QSeries operator *(QSeries o)

The Cauchy product, truncated at the smaller precision.

Example
import 'package:riptide_algebra/riptide_algebra.ray';

void main() {
  print('${(QSeries.ints([1, 1, 0, 0]) * QSeries.ints([1, -1, 1, -1])).coefficients}');
}
Output
[1, 0, 0, 0]
operator ==operator
bool operator ==(Object other)