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-basedPermutation.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, exactBigIntorder, 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 exactCyclotomic; 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
RootSystemfor 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/youngToDynkinfor su(n).- Exact
clebschGordan,wigner3j,wigner6j,wigner9j,racahWby the Racah formulas, returned asSqrtRational(sign · √rational);SurdSumfor exact sums of square roots (orthogonality checks). NumericwignerSmallD,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 coverssu2ToSo3andsl2cToLorentz.
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) andMultivectorwith 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).GammaMatricesfor 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 withsu2Rotationandso3Rotation.CayleyDickson(exact): quaternions, octonions, sedenions and beyond; associator, commutator, norm, inverse;octonionTableand the Fano-planefanoLines.
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 withRationalendpoints, e.g. \sqrt{2} = (x^{2} - 2, (1, 2)). Constructorsrational,fromInt,sqrt,root(r, k),fromSqrtRational,rootInInterval(p, lo, hi),realRoots(p),goldenRatio, and exact trigonometric valuescosPi(r)/sinPi(r)(cos and sin of r\pi for rational r),cos2PiOver(n),sin2PiOver(n)withtwoCosMinimalPolynomial(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. toStringshows the minimal polynomial and interval;toLatexgives rationals, quadratic surds (\frac{1 + \sqrt{5}}{2}, also asquadraticSurd), real radicals\sqrt[k]{r}, otherwise the root of the minimal polynomial.QPoly(in its own file, soriptide_cascan 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 areRationalField(Q),PrimeField(p)(GF(p) for an odd prime p < 2^{31}, elements asint) andAlgebraicField(AlgebraicNumber, closed under real square roots).RtPoint<T>[x, y] andRtLine<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-madeRtPlane.rational,RtPlane.algebraicandRtPlane.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).
- Measures:
- Triangles:
measures(a1, a2, a3)returnsRtMeasures(Q_{i} opposite A_{i}, s_{i} at A_{i}).archimedesis A(Q_{1}, Q_{2}, Q_{3}) = 16 \cdot \mathrm{area}^{2};archimedesOfPointscomputes 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.
- Triple quad:
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 aQPoly, with s(n\theta) = S_{n}(s(\theta)).RtPlane.spreadPolynomialValue(n, s)evaluates it in any field.RtClassicalconverts to and from classical measures:distance(\sqrt{Q}, exactAlgebraicNumber) andquadrance;angle(asin \sqrt{s}, double) andangleBigFloat(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);spreadOfAngleandspreadOfPiMultiple(\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.
solveTrianglerecovers 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 (sqrtModPrimeTonelli–Shanks,sqrtModPrimeCipolla), \bmod p^{k} (sqrtModPrimePower, Hensel) and mod any n (sqrtMod, all roots, as SymPysqrt_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), Pellx² − dy² = 1(pellFundamental) and= −1(negativePellFundamental). - Elliptic curves:
EllipticCurveover Q in general Weierstrass form[a1, a2, a3, a4, a6]with exactRationalarithmetic — b2…b8, c4, c6, \Delta, j, group law (add, double, negate,multiply),integralModel,reduce(p),ap(p),anList(n),torsionPoints/torsionStructure(Nagell–Lutz onY² = X³ − 27c4X − 54c6+ Mazur),torsionBound(gcd of #E(F_{p})),conductor(semistable curves only),isMinimalAt(sufficient test).EllipticCurveFpover 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})^×) andDirichletCharacter(exactRootOfUnityvalues, numericvalue, 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,derivativeAt1for odd rank,partialEulerProduct). Complex numbers areNtComplex.
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,
- 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
BigInt resultantZ(List<BigInt> a0, List<BigInt> b0)The resultant of two integer polynomials by the subresultant algorithm (Cohen, Algorithm 3.3.7).
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())}');
}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.
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()));
}[[2, 2, 1], [2, -2, 1]]
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).
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]}');
}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).
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)}');
}Φ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}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print([for (var n = 1; n <= 12; n++) cyclotomicDegree(n)]);
}[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}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (var n = 1; n <= 4; n++) {
print('S$n(s) = ${spreadPolynomial(n)}');
}
}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
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).
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}');
}so(3): 3, so(3,1): 6, so(4): 6 closed under brackets: true
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.
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)}');
}U(2π) ≈ −I: true
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.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
final r = so3Rotation(3.141592653589793 / 2, [0, 0, 1]);
print(r);
}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
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.
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)}');
}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).
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)}');
}matches lorentzBoost: true
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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(sl2cBoost(1.0, [1, 0, 0]));
}1.1276259652063807+0.0i 0.5210953054937474+0.0i 0.5210953054937474+0.0i 1.1276259652063807+0.0i
CMatrix lorentzBoost(double rapidity, List<double> axis)The Lorentz boost with rapidity \eta along n: \exp(\eta n \cdot K).
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}');
}γ = 1.25, γv = 0.75
CMatrix su3Exp(List<double> theta)\exp(i \sum \theta_{a} \lambda_{a} / 2) ∈ SU(3).
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)}');
}unitary: true
BigInt weylDimension(RootSystem rs, Weight lambda)Weyl dimension formula for V(\lambda).
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)}');
}[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.
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]))}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('su(3) (2,1) → rows ${dynkinToYoung(Weight([2, 1]))}');
}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)).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('rows [3, 1] in su(4) → ${youngToDynkin([3, 1], 4)}');
}rows [3, 1] in su(4) → (2,1,0)
Rational half(int twice)\frac{k}{2} as a Rational (half-integers: half(1) = \frac{1}{2}, half(3) = \frac{3}{2}).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('half(1) = ${half(1)}, half(3) = ${half(3)}, half(4) = ${half(4)}');
}half(1) = 1/2, half(3) = 3/2, half(4) = 2
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.
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)}');
}(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).
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)}');
}(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).
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)}');
}√(1/2)
SqrtRational clebschGordanTwice(int j1, int m1, int j2, int m2, int j, int m)clebschGordan with doubled arguments.
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)}');
}⟨½ ½; ½ −½ | 0 0⟩ = √(1/2)
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.
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)}');
}{1 1 1; 1 1 1} = 1/6SqrtRational wigner6jTwice(int j1, int j2, int j3, int j4, int j5, int j6)wigner6j with doubled arguments.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('{½ ½ 1; ½ ½ 0} = ${wigner6jTwice(1, 1, 2, 1, 1, 0)}');
}{½ ½ 1; ½ ½ 0} = 1/2SqrtRational 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}\}.
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)}');
}-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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${wigner9jTwice(1, 1, 2, 1, 1, 2, 2, 2, 0)}');
}-1/18
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\}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final o = Rational.one;
print('W(1111; 11) = ${racahW(o, o, o, o, o, o)}');
}W(1111; 11) = 1/6
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.
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');
}0.8775825618903728 vs cos(0.5) ≈ 0.8775825618903728
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).
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)');
}(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.
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(' '));
}0.5000 -0.7071 0.5000 0.7071 0.0000 -0.7071 0.5000 0.7071 0.5000
int mobius(BigInt n)The Möbius function \mu(n) for n >= 1.
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');
}M(1..20) = [1, 0, -1, -1, -2, -1, -2, -2, -2, -1, -2, -2, -3, -2, -1, -1, -2, -2, -3, -3]
BigInt divisorSigma(BigInt n, [int k = 1])The divisor function \sigma_{k}(n) = \sum_{d \mid n} d^{k} for n >= 1, k >= 0.
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)}');
}σ(28) = 56, σ_2(12) = 210 σ(2^89 − 1) = 2^89 (a Mersenne prime): true
int numDivisors(BigInt n)The number of divisors \tau(n) = \sigma_{0}(n).
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('τ(360) = ${numDivisors(BigInt.from(360))}, τ(2^10) = ${numDivisors(BigInt.from(1024))}');
}τ(360) = 24, τ(2^10) = 11
int totientInt(int n)totient on ints.
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))}');
}φ(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
int mobiusInt(int n)mobius on ints.
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)}');
}μ(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).
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}');
}σ(220) − 220 = 284, σ(284) − 284 = 220
int carmichaelLambdaInt(int n)carmichaelLambda on ints.
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)}');
}λ(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).
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
final t = divisorSigmaTable(13, 1);
print('σ(0..12) = $t');
}σ(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.
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)}');
}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].
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)}');
}[3; 7, 15, 1, 292] = 103993/33102
List<Rational> convergents(List<BigInt> terms)The convergents p_i / q_i of [a0; a1...].
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(', '));
}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.
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))}');
}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)].
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');
}√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).
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}');
}
}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).
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)');
}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.
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');
}factors below 1000: {71: 1, 839: 1}, cofactor 10086647BigInt? 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).
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)}');
}factor of 10000000036999999769: 1000000007
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.
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}');
}ECM factor: 1000000000039, divides n: true
(BigInt, int) perfectPower(BigInt n)(r, k) with n = r^k and k maximal, for n >= 2.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in ['1024', '729', '1000000', '12']) print('$n = ${perfectPower(BigInt.parse(n))}');
}1024 = (2, 10) 729 = (3, 6) 1000000 = (10, 6) 12 = (12, 1)
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}).
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(' · ')}');
}
}360 = 2^3 · 3^2 · 5 600851475143 = 71 · 839 · 1471 · 6857 18446744073709551617 = 274177 · 67280421310721
Map<int, int> factorintInt(int n)factorint on ints: prime -> exponent, ascending.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in [84, 1001, 65536, 999999]) {
print('$n: ${factorintInt(n)}');
}
}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}List<BigInt> divisors(BigInt n)The positive divisors of n != 0, ascending.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('divisors(360) = ${divisors(BigInt.from(360))}');
}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]
List<int> divisorsInt(int n)divisors on ints.
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]}');
}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]
double hurwitzZeta(double s, double x)The Hurwitz zeta function \zeta(s, x) for real s != 1, x > 0, by Euler–Maclaurin summation.
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)');
}ζ(2, 1) = 1.6449340668482262 (π²/6 = 1.6449340668482264) ζ(2, 1/2) = 4.934802200544678 (= π²/2)
double digamma(double x)The digamma function \psi(x) for real x > 0.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('ψ(1) = ${digamma(1)} (−γ), ψ(1/2) = ${digamma(0.5)}');
}ψ(1) = -0.5772156649015325 (−γ), ψ(1/2) = -1.9635100260214227
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.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('ζ(2) = ${riemannZeta(2)}, ζ(3) = ${riemannZeta(3)}, ζ(−1) = ${riemannZeta(-1)}');
}ζ(2) = 1.6449340668482262, ζ(3) = 1.2020569031595938, ζ(−1) = -0.08333333333333334
double gamma(double x)The gamma function \Gamma(x) for real x (Lanczos, g = 7, about 1e-15 relative; reflection for x < 1/2).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('Γ(5) = ${gamma(5)}, Γ(1/2) = ${gamma(0.5)} (√π), Γ(−1/2) = ${gamma(-0.5)}');
}Γ(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).
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}');
}ζ(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.
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)}');
}E1(1) = 0.219383934396, E1(10) = 0.00000415696892969
(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.
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}');
}gcd(240, 46) = 2 240·(-9) + 46·(47) = 2
BigInt modInverse(BigInt a, BigInt m)a^-1 mod m in [0, m). Throws ArgumentError when gcd(a, m) != 1.
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)}');
}17⁻¹ mod 3120 = 2753 check: 1 3⁻¹ mod 7 (int) = 5
int modInverseInt(int a, int m)modInverse on ints.
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)}');
}3⁻¹ mod 7 = 5, 17⁻¹ mod 3120 = 2753
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).
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)}');
}a·b mod m = 115831543078036282 BigInt check: 115831543078036282
int modPowInt(int b, int e, int m)b^e mod m for ints (e >= 0, m >= 1), exact for every 64-bit modulus.
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)}');
}2^100 mod 1 000 000 007 = 976371285 (a·b) mod m without overflow: 2999999999999999999
BigInt modPow(BigInt b, BigInt e, BigInt m)b^e mod m on BigInt; a negative e uses the inverse of b.
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))}');
}cipher 2790, decrypted 65, 3^−1 mod 7 = 5
(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.
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})');
}x = 23 (mod 105) big: 233341700024 (mod 1000036000099)
(int, int)? crtInt(List<int> residues, List<int> moduli)crt on ints.
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])}');
}(23, 105) inconsistent: null
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.
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}');
}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.
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))}');
}√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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√2 mod 7³: ${sqrtModPrimePower(BigInt.two, BigInt.from(7), 3)}');
}√2 mod 7³: [108, 235]
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)).
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))}');
}√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)^×.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('λ(2^20) = ${carmichaelLambda(BigInt.from(1 << 20))}, λ(1001) = ${carmichaelLambda(BigInt.from(1001))}');
}λ(2^20) = 262144, λ(1001) = 60
BigInt multiplicativeOrder(BigInt a, BigInt n)The multiplicative order of a modulo n (gcd(a, n) = 1).
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))}');
}ord_7(3) = 6, ord_101(2) = 100
int multiplicativeOrderInt(int a, int n)multiplicativeOrder on ints.
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)}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print([for (var n = 2; n <= 20; n++) if (hasPrimitiveRoot(BigInt.from(n))) n]);
}[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.
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))}');
}g(7) = 3, g(2·3^5) = 5, g(8) = null
int? primitiveRootInt(int n)primitiveRoot on ints.
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)})');
}
}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)^×.
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))}');
}3 mod 7: true, 2 mod 7: false
BigInt totient(BigInt n)Euler's totient \varphi(n).
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('φ(36) = ${totient(BigInt.from(36))}, φ(10^20) = ${totient(BigInt.parse('100000000000000000000'))}');
}φ(36) = 12, φ(10^20) = 40000000000000000000
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)).
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)}');
}log_2(435147) mod 1000003 = 123456
int? discreteLogInt(int n, int a, int b)discreteLog on ints.
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)}');
}3^x ≡ 13 (mod 17): x = 4 check: 3^4 mod 17 = 13
Rational bernoulli(int n)The Bernoulli number B_{n}, exact, with the convention B_1 = −1/2 (B_n = 0 for odd n > 1).
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)}');
}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
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).
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))}');
}E4 = [1, 240, 2160, 6720, 17520, 30240] E6 = [1, -504, -16632, -122976, -532728, -1575504] (E4³ − E6²)/1728 = Δ: true
QSeries eulerProduct(int precision)The Euler function ∏_{n>=1} (1 − q^n) by the pentagonal number theorem.
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]}');
}∏(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]
QSeries delta(int precision)The discriminant Δ = q ∏ (1 − q^n)^24 = η^24, the weight-12 cusp form of level 1.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('Δ = ${delta(8).coefficients}');
}Δ = [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.
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]}');
}τ(1..10) = [1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920] τ multiplicative: τ(2)τ(3) = τ(6): true
BigInt ramanujanTau(int n)Ramanujan's \tau(n).
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)}');
}τ(0..10) = [0, 1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920] τ(6) = -6048, τ(2)·τ(3) = -6048
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.
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}');
}[0, 1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1]
QSeries thetaZ(int precision)The Jacobi theta series θ(q) = Σ_{n ∈ Z} q^{n^2} = 1 + 2q + 2q^4 +..., the theta series of Z.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('θ = ${thetaZ(17).coefficients}');
}θ = [1, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2]
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.
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}');
}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
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.
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}');
}[1, 6, 0, 6, 6, 0, 0, 12]
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}.
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))}');
}θ_E8 = [1, 240, 2160, 6720, 17520], equals E4: true
int isqrtSmall(int n)floor(sqrt(n)) for small non-negative ints.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${isqrtSmall(99)}, ${isqrtSmall(100)}');
}9, 10
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).
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))}');
}T_2 Δ = −24 Δ: true
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).
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 Δ)');
}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).
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)}');
}2047 base 2: true, base 3: false
bool isPrime(int n)Deterministic primality for every int (n < 2^63): trial division, then Miller–Rabin with millerRabinBases64.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in [2, 91, 97, 2147483647, 1000000007, 561]) {
print('$n prime: ${isPrime(n)}');
}
}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).
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))}');
}base 2: true base 3: true base 5: true base 7: true base 11: false
bool isSquare(BigInt n)Whether n is a perfect square.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${isSquare(BigInt.parse('152415787532388367501905199875019052100'))}, ${isSquare(BigInt.parse('152415787532388367501905199875019052101'))}');
}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.
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))}');
}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).
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))}');
}2^127 − 1 is prime: true 2^128 − 1 is prime: false next prime after 2^64: 18446744073709551629
int nextPrime(int n)The least prime > n.
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])]}');
}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)]
int prevPrime(int n)The greatest prime < n; throws for n <= 2.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('prevPrime(100) = ${prevPrime(100)}, prevPrime(2^31) = ${prevPrime(1 << 31)}');
}prevPrime(100) = 97, prevPrime(2^31) = 2147483647
BigInt nextPrimeBig(BigInt n)The least probable prime > n (BigInt).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('next prime after 10^30: ${nextPrimeBig(BigInt.from(10).pow(30))}');
}next prime after 10^30: 1000000000000000000000000000057
BigInt prevPrimeBig(BigInt n)The greatest probable prime < n (BigInt); throws for n <= 2.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('largest prime below 2^128: ${prevPrimeBig(BigInt.two.pow(128))}');
}largest prime below 2^128: 340282366920938463463374607431768211297
int isqrtInt(int n)floor(sqrt(n)) for n >= 0, exact for every 64-bit int.
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()}');
}isqrtInt(n) = 67108864 sqrt(n).floor() = 67108865
BigInt isqrt(BigInt n)floor(sqrt(n)) for n >= 0 on BigInt (Newton).
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)}');
}⌊√10^60⌋ = 1000000000000000000000000000000 ⌊√(10^60 − 1)⌋ = 999999999999999999999999999999
List<int> primesUpTo(int n)All primes <= n, ascending (odd-only sieve of Eratosthenes).
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)}');
}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)).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('primes in [10^9, 10^9 + 100): ${primesInRange(1000000000, 1000000100)}');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('primes in [10^6, 2·10^6): ${countPrimesInRange(1000000, 2000000)}');
}primes in [10^6, 2·10^6): 70435
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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in [10, 1000, 1000000, 10000000000]) print('π($n) = ${primePi(n)}');
}π(10) = 4 π(1000) = 168 π(1000000) = 78498 π(10000000000) = 455052511
int jacobiSymbol(BigInt a, BigInt n)The Jacobi symbol (a/n) for odd n > 0.
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))}');
}(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).
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]}');
}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.
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)}');
}(5/8) = -1, (−3/−7) = -1, (3/0) = 0
int jacobiSymbolInt(int a, int n)jacobiSymbol on ints.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${[for (var a = 0; a < 9; a++) jacobiSymbolInt(a, 9)]}');
}[0, 1, 1, 0, 1, 1, 0, 1, 1]
int legendreSymbolInt(int a, int p)legendreSymbol on ints.
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)}');
}quadratic residues mod 13: [1, 3, 4, 9, 10, 12] Jacobi (2/15) = 1
int kroneckerSymbolInt(int a, int n)kroneckerSymbol on ints.
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)]}');
}[1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0]
Constants and variables
List<List<(int, int)>> get octonionTableThe octonion multiplication table in this Cayley–Dickson convention: octonionTable[j] = (s, k) with e_{i} e_{j} = s e_{k}.
List<(int, int, int)> get fanoLinesThe 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}.
final List<CMatrix> gellMannThe Gell-Mann matrices \lambda_{1} \cdots \lambda_{8} (\operatorname{Tr} \lambda_{a} \lambda_{b} = 2\delta_{ab}).
final List<CMatrix> so3Generatorsso(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 lorentzGeneratorsThe 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 minkowskiMetricThe Minkowski metric \operatorname{diag}(+1, -1, -1, -1) (mostly minus) as a matrix.
const List<List<int>> e8CartanMatrixThe 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
AlgebraicNumber
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.
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()}');
}√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.
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()}');
}22/7, degree 1, toRational 22/7
factory AlgebraicNumber.fromInt(int n)The integer n.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${AlgebraicNumber.fromInt(-5)}, minimal polynomial ${AlgebraicNumber.fromInt(-5).minimalPolynomial}');
}-5, minimal polynomial x + 5
factory AlgebraicNumber.sqrt(Rational r)The non-negative square root \sqrt{r} of r \ge 0.
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)}');
}√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).
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)}');
}∛2: x^3 - 2, cubed 2
factory AlgebraicNumber.fromSqrtRational(SqrtRational v)Sign · √square, from the exact Wigner-symbol values.
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}');
}-√(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.
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)}');
}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}).
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))}');
}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)).
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}');
}sin(π/12) ≈ 0.25881904510252076234, degree 4
factory AlgebraicNumber.cos2PiOver(int n)\cos(\frac{2\pi}{n}).
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}');
}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}).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('sin(2π/8) = ${AlgebraicNumber.sin2PiOver(8).toLatex()}');
}sin(2π/8) = \frac{\sqrt{2}}{2}Static
static List<AlgebraicNumber> realRoots(QPoly p)The distinct real roots of a nonzero polynomial, in increasing order.
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))}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in [5, 7, 9]) print('Ψ_$n = ${AlgebraicNumber.twoCosMinimalPolynomial(n)}');
}Ψ_5 = x^2 + x - 1 Ψ_7 = x^3 + x^2 - 2*x - 1 Ψ_9 = x^3 - 3*x + 1
static AlgebraicNumber get goldenRatioThe golden ratio \frac{1 + \sqrt{5}}{2}.
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}');
}φ ≈ 1.618033988749894 φ² − φ − 1 = 0 minimal polynomial: x^2 - x - 1
static final AlgebraicNumber zerostatic final AlgebraicNumber oneProperties
final QPoly minimalPolynomialThe minimal polynomial over Q: irreducible, primitive with integer coefficients, positive leading coefficient.
bool get isRational(Rational, Rational) get intervalThe current isolating interval (lo, hi) (equal ends for a rational).
int get hashCode({Rational rational, Rational coefficient, BigInt radicand})? get quadraticSurdp + 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.
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()}');
}√(9/4) → 3/2, √2 → null
AlgebraicNumber pow(int k)The integer power \mathrm{this}^{k} (k < 0 needs a nonzero value).
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}');
}φ^10 ≈ 122.9918693812, φ^-1 = φ − 1: true
AlgebraicNumber nthRoot(int k)The real k-th root (k \ge 1): the non-negative one for even k (which needs this ≥ 0).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final x = AlgebraicNumber.fromInt(-27).nthRoot(3);
print('∛(−27) = $x');
}∛(−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.
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)}');
}8^(2/3) = 4 2^(3/2) ≈ 2.828427124746190
AlgebraicNumber sqrt()The non-negative square root (this ≥ 0).
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))}');
}√(3 + √8) = 1 + √2: true
AlgebraicNumber abs()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('|1 − √2| ≈ ${(AlgebraicNumber.one - AlgebraicNumber.sqrt(Rational.fromInt(2))).abs().toDecimalString(10)}');
}|1 − √2| ≈ 0.4142135623
int compareTo(AlgebraicNumber o)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)))}');
}compare(√2 + √3, 22/7) = 1
BigInt floor()The largest integer ≤ this.
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()}');
}⌊√2·10^20⌋ = 141421356237309504880
BigInt ceil()The smallest integer ≥ this.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('⌈∛100⌉ = ${AlgebraicNumber.root(Rational.fromInt(100), 3).ceil()}');
}⌈∛100⌉ = 5
BigInt round()The nearest integer (ties, which only rationals have, away from zero as Rational.round).
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()]);
}[3, 4, 7, 11, 18, 29, 47, 76, 123]
BigInt scaledFloor(int places)floor(this · 10^{\mathrm{places}}): the decimal digits, exactly.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('⌊√2 · 10^40⌋ = ${AlgebraicNumber.sqrt(Rational.fromInt(2)).scaledFloor(40)}');
}⌊√2 · 10^40⌋ = 14142135623730950488016887242096980785696
String toDecimalString(int places)The decimal expansion truncated (towards -\infty) to places digits after the point.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√2 = ${AlgebraicNumber.sqrt(Rational.fromInt(2)).toDecimalString(50)}');
}√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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√2 to 200 bits: ${AlgebraicNumber.sqrt(Rational.fromInt(2)).toBigFloat(prec: 200)}');
}√2 to 200 bits: 1.4142135623730950488016887242096980785696718753769480731766796
double toDouble()The nearest double.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${AlgebraicNumber.cosPi(Rational.fromInt(1, 7)).toDouble()}');
}0.9009688679024191
String toString()root of <minimal polynomial> in (lo, hi) ≈ <double>, or the rational itself.
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.
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());
}
}\frac{1 + \sqrt{5}}{2}
\sqrt[3]{5}
\operatorname{root}\left(8 x^{3} + 4 x^{2} - 4 x - 1,\ (0, 2)\right)Operators
AlgebraicNumber operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final a = AlgebraicNumber.sqrt(Rational.fromInt(2));
print('√2 − √2 = ${a - a}');
}√2 − √2 = 0
AlgebraicNumber operator +(AlgebraicNumber o)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}');
}√2 + √3: minimal polynomial x^4 - 10*x^2 + 1
AlgebraicNumber operator -(AlgebraicNumber o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('−√5 ≈ ${(-AlgebraicNumber.sqrt(Rational.fromInt(5))).toDecimalString(8)}');
}−√5 ≈ -2.23606798
AlgebraicNumber operator *(AlgebraicNumber o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√2·√8 = ${AlgebraicNumber.sqrt(Rational.fromInt(2)) * AlgebraicNumber.sqrt(Rational.fromInt(8))}');
}√2·√8 = 4
AlgebraicNumber operator /(AlgebraicNumber o)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))}');
}√6/√3 == √2: true
bool operator <(AlgebraicNumber o)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}');
}√2 + √3 < √10: true
bool operator <=(AlgebraicNumber o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${AlgebraicNumber.sqrt(Rational.fromInt(4)) <= AlgebraicNumber.fromInt(2)}');
}true
bool operator >(AlgebraicNumber o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√(10^12 + 1) > 10^6: ${AlgebraicNumber.sqrt(Rational(BigInt.from(1000000000001))) > AlgebraicNumber.fromInt(1000000)}');
}√(10^12 + 1) > 10^6: true
bool operator >=(AlgebraicNumber o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${AlgebraicNumber.cosPi(Rational.fromInt(1, 4)) >= AlgebraicNumber.sinPi(Rational.fromInt(1, 4))}');
}true
bool operator ==(Object other)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).
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()}');
}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
factory QPoly(List<Rational> coefficients)From coefficients, constant term first.
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}');
}p = -(3/4)*x^2 + 1/2, degree 2
factory QPoly.fromInts(List<int> coefficients)From int coefficients, constant term first.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final p = QPoly.fromInts([-1, 0, 0, 1]);
print('x³ − 1 = $p, factors ${p.irreducibleFactors()}');
}x³ − 1 = x^3 - 1, factors [x - 1, x^2 + x + 1]
factory QPoly.fromBigInts(List<BigInt> coefficients)From BigInt coefficients, constant term first.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final p = QPoly.fromBigInts([BigInt.parse('123456789012345678901234567890'), BigInt.one]);
print(p);
}x + 123456789012345678901234567890
factory QPoly.constant(Rational c)The constant polynomial c.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.constant(Rational.fromInt(5, 3))}, degree ${QPoly.constant(Rational.fromInt(5, 3)).degree}');
}5/3, degree 0
factory QPoly.monomial(Rational c, int k)c \cdot x^{k}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.monomial(Rational.fromInt(-2), 5)}');
}-2*x^5
factory QPoly.fromRoots(Iterable<Rational> roots)The monic polynomial with the given roots, \prod (x - r_{i}).
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');
}p = x^3 + (1/2)*x^2 - (13/2)*x + 3
Static
static final QPoly zerostatic final QPoly onestatic QPoly gcd(QPoly a, QPoly b)The monic greatest common divisor (zero when both are zero).
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)}');
}gcd(x² − 1, x² + 2x + 1) = x + 1 resultant: 0
static (QPoly, QPoly, QPoly) extendedGcd(QPoly a, QPoly b)The extended gcd: (g, s, t) with s \cdot a + t \cdot b = g monic.
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}');
}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
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.
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}');
}Res(x² − 2, 2x) = -8, discriminant 8
Properties
List<Rational> get coefficientsThe coefficients, constant term first (unmodifiable, no trailing zeros).
bool get isZeroRational get leadingCoefficientThe leading coefficient (0 for the zero polynomial).
Rational get contentThe positive content: the rational c > 0 such that this / c has coprime integer coefficients.
Rational get discriminantThe discriminant (-1)^{n(n - 1)/2} \cdot \operatorname{Res}(p, p') / lc.
int get hashCodeMethods
QPoly scale(Rational s)Multiplication by a scalar.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([2, 4, 6]).scale(Rational.fromInt(1, 2))}');
}3*x^2 + 2*x + 1
QPoly pow(int k)The k-th power.
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
final p = QPoly.fromInts([1, 1]).pow(6);
print('(1 + x)⁶ = $p');
}(1 + x)⁶ = x^6 + 6*x^5 + 15*x^4 + 20*x^3 + 15*x^2 + 6*x + 1
(QPoly, QPoly) divRem(QPoly d)Euclidean division: (q, r) with this = q \cdot d + r and deg r < \deg d.
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');
}q = (1/2)*x^2 + (5/4)*x - 5/8, r = 45/8
bool isDivisibleBy(QPoly d)True when d divides this exactly.
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]))}');
}by x² + x + 1: true, by x² + 1: false
QPoly derivative()The formal derivative.
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()}');
}p = 2*x^3 - 3*x + 1, p′ = 6*x^2 - 3, p″ = 12*x
Rational evaluate(Rational x)The value at x (Horner).
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))}');
}p(99/70) = 1/4900
int signAt(Rational x)The sign of the value at x (-1, 0, 1), computed on integers without building the rational value.
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))}');
}sign at 99/70: 1, at 140/99: -1
QPoly compose(QPoly g)The composition \operatorname{this}(g(x)).
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)}');
}p(g(x)) = x^2 + 2*x + 1, g(p(x)) = x^2 + 1
QPoly shift(Rational c)p(x + c) (a Taylor shift).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('x³ shifted by 1: ${QPoly.fromInts([0, 0, 0, 1]).shift(Rational.one)}');
}x³ shifted by 1: x^3 + 3*x^2 + 3*x + 1
QPoly scaleVariable(Rational c)p(c \cdot x).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([1, 1, 1]).scaleVariable(Rational.fromInt(2))}');
}4*x^2 + 2*x + 1
QPoly reflect()p(-x).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('p(−x) for p = 1 + 2x + 3x²: ${QPoly.fromInts([1, 2, 3]).reflect()}');
}p(−x) for p = 1 + 2x + 3x²: 3*x^2 - 2*x + 1
QPoly reverse()The reversal x^{n} \cdot p(\frac{1}{x}) (its roots are the reciprocals).
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())}');
}x^2 - 5*x + 6 → 6*x^2 - 5*x + 1, roots [1/3, 1/2]
QPoly substitutePower(int k)p(x^{k}).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([1, 1]).substitutePower(3)}');
}x^3 + 1
QPoly monic()This divided by its leading coefficient.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([3, 6, 9]).monic()}');
}x^2 + (2/3)*x + 1/3
QPoly primitive()The primitive part with a positive leading coefficient: coprime integer coefficients.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly([Rational.fromInt(1, 2), Rational.fromInt(-1, 3), Rational.fromInt(5, 6)]).primitive()}');
}5*x^2 - 2*x + 3
List<BigInt> integerCoefficients()The integer coefficients of primitive (constant term first).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly([Rational.fromInt(1, 2), Rational.fromInt(-1, 3), Rational.fromInt(5, 6)]).integerCoefficients()}');
}[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).
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());
}[x - 1, x + 1, x - 2]
QPoly squareFreePart()The monic square-free part (the product of the distinct monic irreducible factors).
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()}');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final s in QPoly.fromInts([-1, -1, 0, 1]).sturmSequence()) print(s);
}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).
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)}');
}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).
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()}');
}x³ − 2: [(-4, 4)] x⁴ − 10x² + 1: [(-4, -2), (-2, 0), (0, 2), (2, 4)]
(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.
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');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QPoly.fromInts([-1, 0, 0, 0, 0, 0, 1]).irreducibleFactors());
}[x + 1, x - 1, x^2 + x + 1, x^2 - x + 1]
String toString([String variable = 'x'])Plain text in the variable variable, highest power first, e.g. x^4 - 10*x^2 + 1.
String toLatex([String variable = 'x'])LaTeX, e.g. x^{4} - 10 x^{2} + 1.
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'));
}3 x^{3} - \frac{1}{2} x + 1
3 t^{3} - \frac{1}{2} t + 1Operators
Rational operator [](int i)The coefficient of x^{i} (0 beyond the degree).
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]}');
}p[0] = 7, p[2] = -2, p[9] = 0
QPoly operator +(QPoly o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([1, 2, 3]) + QPoly.fromInts([0, -2, 0, 1])}');
}x^3 + 3*x^2 + 1
QPoly operator -(QPoly o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QPoly.fromInts([1, 2, 3]) - QPoly.fromInts([1, 2, 3])}');
}0
QPoly operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-QPoly.fromInts([1, -2])}');
}2*x - 1
QPoly operator *(QPoly o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('(x − 1)(x² + x + 1) = ${QPoly.fromInts([-1, 1]) * QPoly.fromInts([1, 1, 1])}');
}(x − 1)(x² + x + 1) = x^3 - 1
QPoly operator ~/(QPoly d)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('(x³ − 1) ~/ (x − 1) = ${QPoly.fromInts([-1, 0, 0, 1]) ~/ QPoly.fromInts([-1, 1])}');
}(x³ − 1) ~/ (x − 1) = x^2 + x + 1
QPoly operator %(QPoly d)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])}');
}(x³ + 2) % (x² + 1) = -x + 2
bool operator ==(Object other)CliffordAlgebra
final class CliffordAlgebra
The Clifford algebra with generators e_{0} \cdots e_{n - 1}, e_{k}^{2} = \mathrm{metric}[k] \in \{+1, -1, 0\}.
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}');
}e1² = 1, (e1e2)² = -1, e1e2 + e2e1 = 0
Constructors
CliffordAlgebra(List<int> metric)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}');
}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.
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}');
}e3² = 0, dimension 16
Properties
final List<int> metricint get generatorCountbool get isDegeneratelate final List<QIMatrix> complexGeneratorsComplex 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.
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)}');
}e12·e23 sign: 1, e1·e1: 1
Multivector scalar(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CliffordAlgebra.signature(2).scalar(Rational.fromInt(3, 4))}');
}3/4
Multivector e(int k)The generator e_{k}.
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)}');
}e0² = 1, e1² = -1
Multivector blade(int mask, [Rational? c])The basis blade with bitmask mask (coefficient 1).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final cl = CliffordAlgebra.signature(3);
print('${cl.blade(5)}, ${cl.blade(7, Rational.fromInt(2))}');
}1*e02, 2*e012
Multivector product(List<int> indices)The blade e_{i_{1}} e_{i_{2}} \cdots (indices in any order, reordered with sign).
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])}');
}e2 e0 e1 = 1*e012, e1 e0 = -1*e01
Multivector vector(List<Rational> v)\sum v_{k} e_{k}.
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)');
}v = 1*e0 + 2*e1 + 2*e2, v² = 9 (its quadrance)
String bladeName(int mask)Name of a blade, e.g. e013, 1 for the scalar.
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)]);
}[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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final cl = CliffordAlgebra.signature(2);
print(cl.leftRegular(cl.e(0)));
}0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 0
QIMatrix represent(Multivector x)The image of a multivector in complexGenerators' representation.
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))}');
}0 1 1 0 — 0 -i i 0
String toString()Multivector
final class Multivector
An element of a CliffordAlgebra with exact rational coefficients.
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))}');
}R e1 R⁻¹ = 1*e1
Properties
final CliffordAlgebra algebrabool get isZeroMultivector get cliffordConjugateClifford conjugate \bar{x} = reverse of the grade involution: (-1)^{k(k + 1)/2}.
bool get isScalarMultivector get versorInverseThe inverse of a versor (or any x with x x̃ scalar and nonzero): x̃ / (x x̃).
Multivector get inverseThe general inverse via the left-regular representation (exact; throws if singular).
int get hashCodeMethods
Rational coefficient(int blade)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}');
}coefficient of e01: 1, scalar part 2
Multivector scale(Rational r)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))}');
}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).
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))}');
}e1 ⌋ e12 = 1*e1
Multivector rightContraction(Multivector o)The right contraction x ⌊ y.
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))}');
}e12 ⌊ e2 = 1*e0
Rational scalarProduct(Multivector o)The scalar product \langle x y\rangle_{0}.
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)}');
}⟨uv⟩₀ = 1
Multivector dot(Multivector o)Hestenes' inner product: grade |r - s| parts of products of nonscalar blades.
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))}');
}e1 · e12 = 1*e1
Multivector commutatorProduct(Multivector o)The commutator product \frac{xy - yx}{2}.
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))}');
}e12 × e23 = 1*e02
Multivector grade(int k)\langle x\rangle_{k}.
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)}');
}grade 0: 1 grade 1: 1*e0 grade 2: 1*e01 grade 3: 1*e012
Multivector sandwich(Multivector x)The sandwich v \times v^{-1} (v a versor), e.g. a rotation by a rotor.
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)}');
}e1 v e1⁻¹ = 1*e0 - 2*e1 - 3*e2
String toString()Operators
Multivector operator +(Multivector o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final cl = CliffordAlgebra.signature(2);
print('${cl.one + cl.e(0)}');
}1 + 1*e0
Multivector operator -()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)}');
}2*e01
Multivector operator -(Multivector o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final cl = CliffordAlgebra.signature(2);
print('${-(cl.one + cl.e(1))}');
}-1 - 1*e1
Multivector operator *(Multivector o)The geometric product.
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)');
}uv = 1 + 1*e01 (scalar u·v plus bivector u∧v)
Multivector operator ^(Multivector o)The outer (wedge) product.
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)}');
}e1 ∧ e2 = 1*e01, e1 ∧ e1 = 0
bool operator ==(Object other)GammaBasis
enum GammaBasis
Representation basis of the gamma matrices.
Values
dirac\gamma^{0} = \operatorname{diag}(1, 1, -1, -1) (Dirac–Pauli; standard for the non-relativistic limit and hydrogen).
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).
majoranaAll \gamma^{\mu} purely imaginary in mostly-minus (real in mostly-plus), so the Dirac equation is real and Majorana spinors are real.
MetricSignature
enum MetricSignature
Metric signature convention.
Values
mostlyPlus\eta = \operatorname{diag}(-1, +1, +1, +1) (general relativity, Srednicki, Weinberg); \gamma^{\mu} = i × the mostly-minus matrices.
GammaMatrices
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}.
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)}');
}{γ⁰, γ⁰} = 2I: true
{γ¹, γ¹} = −2I: trueConstructors
factory GammaMatrices({GammaBasis basis = GammaBasis.dirac, MetricSignature signature = MetricSignature.mostlyMinus})import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final w = GammaMatrices(basis: GammaBasis.weyl);
print('γ⁵ in the Weyl basis:\n${w.gamma5}');
}γ⁵ in the Weyl basis: -1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 1
Properties
final GammaBasis basisfinal MetricSignature signatureQIMatrix 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 projectorLeftChiral projectors P_{L} = \frac{1 - \gamma^{5}}{2}, P_{R} = \frac{1 + \gamma^{5}}{2}.
QIMatrix get projectorRightList<QIMatrix> get cliffordBasisThe 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 chargeConjugationThe 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
QIMatrix sigma(int mu, int nu)\sigma^{\mu \nu} = (\frac{i}{2})[\gamma^{\mu}, \gamma^{\nu}].
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(GammaMatrices().sigma(1, 2));
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(GammaMatrices().lorentzGenerator(0, 1));
}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).
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]}');
}maps γ² over: true
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).
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))}');
}p̸² = 16 I: true
QIMatrix slashExact(List<Rational> p)Exact \not{p} for rational p^{\nu}.
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');
}p̸² = 16 · I, exactly
double dot(List<double> a, List<double> b)Minkowski product a \cdot b = \eta_{\mu \nu} a^{\mu} b^{\nu}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GammaMatrices().dot([5, 0, 0, 3], [5, 0, 0, 3])}');
}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.
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])}');
}Tr(γ⁰γ⁰γ¹γ¹) = -4, Tr(γ⁰γ¹) = 0
GaussianRational trace(List<int> indices)Exact trace of a product of \gamma^{\mu} by matrix multiplication.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GammaMatrices().trace([0, 0, 1, 1])} (by matrix multiplication; matches the formula)');
}-4 (by matrix multiplication; matches the formula)
List<Complex> adjoint(List<Complex> psi)The Dirac adjoint \bar{\psi} = \psi^{\dagger}\gamma^{0} (as a row).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GammaMatrices().adjoint([Complex(1, 0), Complex(0, 1), Complex(0, 0), Complex(2, 0)])}');
}[(1.0+0.0j), (0.0-1.0j), (0.0+0.0j), (-2.0+0.0j)]
Complex bilinear(List<Complex> psi, CMatrix m, List<Complex> chi)\bar{\psi} M \chi.
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)}');
}ψ̄ψ = (1.0+0.0j)
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.
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}');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GammaMatrices().chargeConjugate([Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0)])}');
}[(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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(GammaMatrices().spinorBoost(0.5, [0, 0, 1]));
}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).
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))}');
}true
SpinorBilinears
final class SpinorBilinears
The bilinear covariants of a Dirac spinor.
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}');
}σ 0.0, π 0.0, J [2.0, 0.0, 0.0, 2.0], A [2.0, 0.0, 0.0, 2.0]
Constructors
SpinorBilinears({required this.scalar, required this.pseudoscalar, required this.vector, required this.axial, required this.tensor})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}');
}1.0, [1.0, 0.0, 0.0, 0.0]
Properties
Cyclotomic
final class Cyclotomic
An exact element of the cyclotomic field Q(\zeta_{n}), \zeta_{n} = e^{2\pi i/n}.
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))}');
}ζ + ζ⁴ = -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).
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');
}sum of the fifth roots of unity: 0
factory Cyclotomic.rational(Rational r)The rational number r, viewed in Q(\zeta_{1}) = Q.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Cyclotomic.rational(Rational.fromInt(3, 7))}');
}3/7
factory Cyclotomic.integer(int k)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Cyclotomic.integer(4) * Cyclotomic.rootOfUnity(3)}');
}4*z3
factory Cyclotomic.rootOfUnity(int n, [int k = 1])\zeta_{n}^{k}.
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}');
}ω = 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.
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)}');
}√5 = -1 - 2*z5^2 - 2*z5^3, squared 5 √−3 squared -3
Properties
final int orderThe field is Q(\zeta_{\mathrm{order}}); the element may lie in a smaller subfield.
List<Rational> get coefficientsCoefficients on 1, \zeta, …, \zeta^{\varphi(n) - 1}.
Cyclotomic get inverseThe inverse, as the product of the other Galois conjugates divided by the (rational) norm.
bool get isZerobool get isRationalWhether the element is rational (only the constant coefficient survives reduction).
int get hashCodeMethods
Cyclotomic lift(int m)The same element as a polynomial in \zeta_{m} (n must divide m): \zeta_{n} = \zeta_{m}^{m/n}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final i = Cyclotomic.rootOfUnity(4);
print('i in ℚ(ζ₁₂): ${i.lift(12)}');
}i in ℚ(ζ₁₂): z12^3
Cyclotomic scale(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Cyclotomic.rootOfUnity(8).scale(Rational.fromInt(1, 2))}');
}1/2*z8
Cyclotomic galois(int k)The Galois automorphism \sigma_{k}: \zeta \mapsto \zeta^{k} (k coprime to the order).
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})');
}σ_2(√5) = 1 + 2*z5^2 + 2*z5^3 (= −√5: true)
Rational toRational()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()}');
}ζ + ζ² + … + ζ⁶ = -1
(double, double) toComplex()Numerical value (re, im).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('ζ₈ ≈ ${Cyclotomic.rootOfUnity(8).toComplex()}');
}ζ₈ ≈ (0.7071067811865476, 0.7071067811865475)
Operators
Cyclotomic operator +(Cyclotomic o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Cyclotomic.rootOfUnity(4) + Cyclotomic.integer(1)}');
}1 + z4
Cyclotomic operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Cyclotomic.rootOfUnity(4) - Cyclotomic.rootOfUnity(4)}');
}0
Cyclotomic operator -(Cyclotomic o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-Cyclotomic.rootOfUnity(6)}');
}-z6
Cyclotomic operator *(Cyclotomic o)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)}');
}i·i = -1, (1 + i)² = 2*z4
Cyclotomic operator /(Cyclotomic o)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)}');
}1/(1 + i) = 1/2 - 1/2*z4
bool operator ==(Object other)RtPoint
final class RtPoint<T>
A point [x, y] with coordinates in a field.
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}');
}p = [1/2, 3], x 1/2, y 3
Constructors
const RtPoint(this.x, this.y)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))}');
}a point of GF(7)²: [3, 5], quadrance to (0,0) 6
Properties
final T xfinal T yint get hashCodeMethods
String toString()Operators
bool operator ==(Object other)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.
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}');
}l = ⟨1 : -1 : 0⟩ (the line y = x), a 1, b -1, c 0
Constructors
const RtLine(this.a, this.b, this.c)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)}');
}⟨2 : 3 : -6⟩, (3, 0) on it: true
Properties
final T afinal T bfinal T cint get hashCodeMethods
String toString()Operators
bool operator ==(Object other)RtMeasures
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).
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}');
}Q: 25, 9, 16; s: 1, 9/25, 16/25
Constructors
const RtMeasures(this.q1, this.q2, this.q3, this.s1, this.s2, this.s3)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)}');
}the 3-4-5 measures satisfy every law: true
Properties
final T q1final T q2final T q3final T s1final T s2final T s3List<T> get quadrancesList<T> get spreadsint get hashCodeMethods
String toString()Operators
bool operator ==(Object other)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.
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)}');
}quadrances: 25, 9, 16 spreads: 1, 9/25, 16/25 Pythagoras Q2 + Q3 = Q1: true
Constructors
const RtPlane(this.field)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))}');
}GF(13): Q((0,0), (3,4)) = 12
Static
static const RtPlane<Rational> rational = RtPlane<Rational>(RationalField())Rational trigonometry over Q.
static const RtPlane<AlgebraicNumber> algebraic = RtPlane<AlgebraicNumber>(AlgebraicField())Over the real algebraic numbers (every real square root exists).
static RtPlane<int> finite(int p)Over GF(p), p an odd prime.
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)}');
}over F13: Q = 1, 4, 10; s = 1, 4, 10 laws hold: true
Properties
final ScalarField<T> fieldMethods
RtPoint<T> point(T x, T y)The point [x, y].
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))}');
}[1/3, -2]
RtPoint<T> pointInt(int x, int y)The point with integer coordinates mapped into F.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final f = RtPlane.finite(5);
print('(7, -1) in GF(5)²: ${f.pointInt(7, -1)}');
}(7, -1) in GF(5)²: [2, 4]
RtPoint<T> pointRational(Rational x, Rational y)The point with rational coordinates mapped into F.
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))}');
}(1/2, 1/3) in GF(7)²: [4, 5]
RtLine<T> line(T a, T b, T c)The line \langle a:b:c\rangle (throws when a = b = 0).
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)');
}⟨3 : 4 : -25⟩ (3x + 4y = 25)
T quadrance(RtPoint<T> p, RtPoint<T> q)The quadrance Q(A, B) = (x_{2} - x_{1})^{2} + (y_{2} - y_{1})^{2}.
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)}');
}quadrance = 25 midpoint = [5/2, 4]
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.
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)))}');
}null in GF(5): true; over ℚ ⟨1 : 2 : 0⟩: false
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.
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)}');
}spread(x-axis, diagonal) = 1/2 spread(x-axis, y-axis) = 1 cross(x-axis, diagonal) = 1/2
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.
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)}');
}c = 9/25, s = 16/25, sum 1
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.
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)');
}t = 16/9 (tan² of the angle, exactly)
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.
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');
}join: ⟨-4 : 3 : -2⟩
RtPoint<T>? meet(RtLine<T> l1, RtLine<T> l2)The intersection of two lines, or null when they are parallel.
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)))}');
}meet: [1, 1]; parallel lines: null
bool liesOn(RtPoint<T> p, RtLine<T> l)True when the point lies on the line.
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)}');
}(1/3, 2/3): true, (1, 1): false
bool sameLine(RtLine<T> l1, RtLine<T> l2)True when the triples are proportional.
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)))}');
}true
bool isParallel(RtLine<T> l1, RtLine<T> l2)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)))}');
}true, false
bool isPerpendicular(RtLine<T> l1, RtLine<T> l2)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)))}');
}true
bool isCollinear(RtPoint<T> p, RtPoint<T> q, RtPoint<T> r)True when the three points lie on one line.
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))}');
}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).
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)))}');
}true
RtLine<T> parallelThrough(RtPoint<T> p, RtLine<T> l)The line through p parallel to l.
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)))}');
}⟨1 : -2 : 6⟩
RtLine<T> perpendicularThrough(RtPoint<T> p, RtLine<T> l)The altitude: the line through p perpendicular to l.
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)))}');
}⟨2 : 1 : -3⟩
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).
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)))}');
}foot: [6, 3], on the line: true
RtPoint<T> midpoint(RtPoint<T> p, RtPoint<T> q)The midpoint.
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)))}');
}[3/2, 7/6]
RtPoint<T> reflect(RtPoint<T> p, RtLine<T> l)The reflection of p in the (non-null) line l.
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)))}');
}(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.
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))}');
}GF(13): [⟨5 : 12 : 10⟩, ⟨5 : 1 : 6⟩] ℚ: []
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).
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)}');
}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.
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²');
}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)²).
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²');
}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.
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))}');
}(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).
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 ℚ)');
}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).
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))}');
}true, true
T pythagorasHypotenuse(T q1, T q2)The quadrance of the hypotenuse, Q_{1} + Q_{2}.
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))}');
}13/36
T pythagorasLeg(T hypotenuse, T leg)The other leg, Q_{3} - Q_{1}.
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))}');
}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}.
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)}');
}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}}.
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))}');
}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}}.
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))}');
}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}).
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))}');
}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}}.
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))}');
}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})}.
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))}');
}[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).
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)');
}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})}.
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))}');
}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).
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)))}');
}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.
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}');
}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).
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))}');
}S_3(1/4) = 1, S_5(1/3) = 1/243
RtClassical
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.
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))}·π');
}distance √2 = \sqrt{2} ≈ 1.414213562373
angle of spread 3/4 = 1/3·πStatic
static AlgebraicNumber distance(Rational quadrance)The distance \sqrt{Q}, exactly.
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()}');
}√(25/4) = 5/2, √8 = 2\sqrt{2}static AlgebraicNumber distanceOf(AlgebraicNumber quadrance)The distance \sqrt{Q} of an algebraic quadrance, exactly.
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()}');
}√(3 + √8) = 1 + \sqrt{2}static AlgebraicNumber quadrance(AlgebraicNumber distance)The quadrance d^{2} of a distance, exactly.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('(1 + √2)² = ${RtClassical.quadrance(AlgebraicNumber.one + AlgebraicNumber.sqrt(Rational.fromInt(2))).toLatex()}');
}(1 + √2)² = 3 + 2\sqrt{2}static double angle(double spread)The angle asin \sqrt{s} in radians as a double (0 \le s \le 1).
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
print('asin √(1/2) = ${RtClassical.angle(0.5)} (π/4 = ${3.141592653589793 / 4})');
}asin √(1/2) = 0.7853981633974484 (π/4 = 0.7853981633974483)
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).
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)}');
}angle of spread 1/3 to 120 bits: 0.61547970867038734106746458912399368811
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.
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)}');
}
}spread 1/4 → 1/6 spread 1/2 → 1/4 spread 1 → 1/2 spread 1/3 → null
static double spreadOfAngle(double theta)The spread \sin^{2}\theta of an angle in radians (double).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('sin²(π/6) in doubles: ${RtClassical.spreadOfAngle(3.141592653589793 / 6)} (exactly 1/4)');
}sin²(π/6) in doubles: 0.24999999999999994 (exactly 1/4)
static AlgebraicNumber spreadOfPiMultiple(Rational r)The spread \sin^{2}(r\pi) of the angle r\pi, exactly.
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))}');
}sin²(π/5) = \frac{5 - \sqrt{5}}{8}
sin²(π/6) = 1/4ScalarField
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.
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)}');
}a = 1/3, b = 1/6: a + b = 1/2, a·b = 1/18, a/b = 2
Properties
T get zeroT get oneMethods
T fromInt(int n)The image of the integer n.
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)}');
}fromInt(-4) in ℚ: -4
T fromRational(Rational r)The image of a rational (throws when the denominator vanishes in F).
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))}');
}1/2 in ℚ: 1/2
T add(T a, T b)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)}');
}a + b = 1/2
T sub(T a, T b)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)}');
}a − b = 1/6
T mul(T a, T b)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)}');
}a · b = 1/18
T div(T a, T b)A / b (throws for b = 0).
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)}');
}a / b = 2
T neg(T a)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)}');
}−a = -1/3
bool isZero(T a)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)}');
}a − a is zero: true, a: false
bool equals(T a, T b)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)}');
}a·b/b equals a: true
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.
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)}');
}sqrt(a·a) = 1/3, sqrt(b) = null
RationalField
final class RationalField implements ScalarField<Rational>
The rationals Q, with exact square roots of perfect squares only.
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)}');
}a = 1/3, b = 1/6: a + b = 1/2, a·b = 1/18, a/b = 2
Constructors
const RationalField()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
const f = RationalField();
print('zero ${f.zero}, one ${f.one}, characteristic ${f.characteristic}');
}zero 0, one 1, characteristic 0
Properties
Rational get zeroRational get oneint get characteristicMethods
Rational fromInt(int n)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)}');
}fromInt(-4) in ℚ: -4
Rational fromRational(Rational r)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))}');
}1/2 in ℚ: 1/2
Rational add(Rational a, Rational b)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)}');
}a + b = 1/2
Rational sub(Rational a, Rational b)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)}');
}a − b = 1/6
Rational mul(Rational a, Rational b)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)}');
}a · b = 1/18
Rational div(Rational a, Rational b)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)}');
}a / b = 2
Rational neg(Rational a)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)}');
}−a = -1/3
bool isZero(Rational a)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)}');
}a − a is zero: true, a: false
bool equals(Rational a, Rational b)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)}');
}a·b/b equals a: true
Rational? sqrt(Rational a)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)}');
}sqrt(a·a) = 1/3, sqrt(b) = null
PrimeField
final class PrimeField implements ScalarField<int>
The finite field GF(p) for an odd prime p < 2^{31}, elements as ints in [0, p).
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)}');
}a = 3, b = 7: a + b = 10, a·b = 10, a/b = 2
Constructors
PrimeField(this.p)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final f = PrimeField(11);
print('zero ${f.zero}, one ${f.one}, characteristic ${f.characteristic}');
}zero 0, one 1, characteristic 11
Properties
final int pint get zeroint get oneint get characteristicbool get hasSquareRootOfMinusOneTrue when -1 is a square (p \equiv 1 \bmod 4), i.e. when null lines exist.
Methods
int fromInt(int n)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)}');
}fromInt(-4) in ℤ/11: 7
int fromRational(Rational r)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))}');
}1/2 in ℤ/11: 6
int add(int a, int b)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)}');
}a + b = 10
int sub(int a, int b)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)}');
}a − b = 7
int mul(int a, int b)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)}');
}a · b = 10
int div(int a, int b)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)}');
}a / b = 2
int neg(int a)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final f = PrimeField(11);
final a = 3, b = 7;
print('−a = ${f.neg(a)}');
}−a = 8
bool isZero(int a)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)}');
}a − a is zero: true, a: false
bool equals(int a, int b)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)}');
}a·b/b equals a: true
int inverse(int a)The multiplicative inverse (throws for 0).
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))}');
}7⁻¹ mod 101 = 29, check 1
int? sqrt(int a)The smaller of the two square roots, or null for a non-residue.
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)}');
}sqrt(a·a) = 3, sqrt(b) = null
AlgebraicField
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.
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();a = \sqrt{2}, b = 3: a + b = 3 + \sqrt{2}, a·b = 3\sqrt{2}, a/b = \frac{\sqrt{2}}{3}Constructors
const AlgebraicField()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();zero 0, one 1, characteristic 0
Properties
AlgebraicNumber get zeroAlgebraicNumber get oneint get characteristicMethods
AlgebraicNumber fromInt(int n)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();fromInt(-4) in ℝ_alg: -4
AlgebraicNumber fromRational(Rational r)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();1/2 in ℝ_alg: \frac{1}{2}AlgebraicNumber add(AlgebraicNumber a, AlgebraicNumber b)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();a + b = 3 + \sqrt{2}AlgebraicNumber sub(AlgebraicNumber a, AlgebraicNumber b)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();a − b = -3 + \sqrt{2}AlgebraicNumber mul(AlgebraicNumber a, AlgebraicNumber b)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();a · b = 3\sqrt{2}AlgebraicNumber div(AlgebraicNumber a, AlgebraicNumber b)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();a / b = \frac{\sqrt{2}}{3}AlgebraicNumber neg(AlgebraicNumber a)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();−a = -\sqrt{2}bool isZero(AlgebraicNumber a)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();a − a is zero: true, a: false
bool equals(AlgebraicNumber a, AlgebraicNumber b)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();a·b/b equals a: true
AlgebraicNumber? sqrt(AlgebraicNumber a)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();sqrt(a·a) = \sqrt{2}, sqrt(b) = \sqrt{3}GaussianRational
final class GaussianRational
re + i \cdot im with exact rational parts.
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}');
}z = 1+2i, w = 3-i z · w = 5+5i z / w = 1/10+7/10i |z|² = 5
Constructors
GaussianRational(this.re, [Rational? im])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}');
}z = 1/2-3i, z·z = -35/4-3i
factory GaussianRational.integer(int re, [int im = 0])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)}');
}(3 + 4i)(3 − 4i) = 25
Static
static final GaussianRational zerostatic final GaussianRational onestatic final GaussianRational iProperties
final Rational refinal Rational imbool get isZerobool get isRealbool get isImaginaryGaussianRational get conjugateint get hashCodeMethods
GaussianRational scale(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GaussianRational.integer(3, 4).scale(Rational.fromInt(1, 5))}');
}3/5+4/5i
Complex toComplex()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GaussianRational(Rational.fromInt(1, 3), Rational.fromInt(2, 3)).toComplex()}');
}(0.3333333333333333+0.6666666666666666j)
String toString()Operators
GaussianRational operator +(GaussianRational o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GaussianRational.integer(1, 2) + GaussianRational.integer(3, -5)}');
}4-3i
GaussianRational operator -(GaussianRational o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${GaussianRational.integer(1, 2) - GaussianRational.integer(3, -5)}');
}-2+7i
GaussianRational operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-GaussianRational.integer(1, -2)}');
}-1+2i
GaussianRational operator *(GaussianRational o)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)}');
}(2 + i)² = 3+4i
GaussianRational operator /(GaussianRational o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('1/(1 + i) = ${GaussianRational.one / GaussianRational.integer(1, 1)}');
}1/(1 + i) = 1/2-1/2i
bool operator ==(Object other)SqrtRational
final class SqrtRational
The number sign · √square with square ≥ 0 (sign is 0 exactly when square is 0).
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}');
}⟨½ ½; ½ −½ | 1 0⟩ = √(1/2) as a double: 0.7071067811865476 squared: 1/2
Constructors
factory SqrtRational(int sign, Rational square)Sign · √square.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final v = SqrtRational(-1, Rational.fromInt(2, 3));
print('v = $v ≈ ${v.toDouble()}');
}v = -√(2/3) ≈ -0.816496580927726
factory SqrtRational.rational(Rational r)The rational r itself.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SqrtRational.rational(Rational.fromInt(-3, 4))}');
}-3/4
factory SqrtRational.parseSigned(String s)Parses -3/4 meaning -\sqrt{\frac{3}{4}} (the fixture format).
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')}');
}-√(3/4), √(1/2)
Static
static final SqrtRational zerostatic final SqrtRational oneProperties
final int signfinal Rational squarebool get isZeroint get hashCodeMethods
SqrtRational scale(Rational r)Multiplies by a rational.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SqrtRational(1, Rational.fromInt(1, 2)).scale(Rational.fromInt(-2))}');
}-√(2)
double toDouble()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SqrtRational.parseSigned('1/3').toDouble()}');
}0.5773502691896257
SurdSum toSurd()Exact value as a surd sum (for exact sums of products).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SqrtRational.parseSigned('8/9').toSurd()}');
}2/3*√2
String toString()Operators
SqrtRational operator *(SqrtRational o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√(1/2)·√(1/2) = ${SqrtRational.parseSigned('1/2') * SqrtRational.parseSigned('1/2')}');
}√(1/2)·√(1/2) = 1/2
SqrtRational operator /(SqrtRational o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√(1/2) / √(1/8) = ${SqrtRational.parseSigned('1/2') / SqrtRational.parseSigned('1/8')}');
}√(1/2) / √(1/8) = 2
SqrtRational operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-SqrtRational.parseSigned('2/3')}');
}-√(2/3)
bool operator ==(Object other)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).
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()}');
}√8 + √2 = 3*√2 ≈ 4.242640687119286
Constructors
factory SurdSum.rational(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SurdSum.rational(Rational.fromInt(5, 2)) + SurdSum.sqrtOf(Rational.fromInt(3))}');
}5/2 + 1*√3
factory SurdSum.sqrtOf(Rational r)\sqrt{r} for a rational r \ge 0.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('√12 = ${SurdSum.sqrtOf(Rational.fromInt(12))}, √(1/18) = ${SurdSum.sqrtOf(Rational.fromInt(1, 18))}');
}√12 = 2*√3, √(1/18) = 1/6*√2
Static
static final SurdSum zeroProperties
bool get isZerobool get isRationalint get hashCodeMethods
SurdSum scale(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(SurdSum.sqrtOf(Rational.fromInt(2)) + SurdSum.rational(Rational.one)).scale(Rational.fromInt(3))}');
}3 + 3*√2
Rational toRational()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()}');
}(√2 + 1)(√2 − 1) = 1
double toDouble()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(SurdSum.sqrtOf(Rational.fromInt(3)) + SurdSum.sqrtOf(Rational.fromInt(5))).toDouble()}');
}3.968118785068667
String toString()Operators
SurdSum operator +(SurdSum o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SurdSum.sqrtOf(Rational.fromInt(18)) + SurdSum.sqrtOf(Rational.fromInt(50))}');
}8*√2
SurdSum operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${SurdSum.sqrtOf(Rational.fromInt(18)) - SurdSum.sqrtOf(Rational.fromInt(8))}');
}1*√2
SurdSum operator -(SurdSum o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-SurdSum.sqrtOf(Rational.fromInt(7))}');
}-1*√7
SurdSum operator *(SurdSum o)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)))}');
}(√2 + √3)² = 5 + 2*√6
bool operator ==(Object other)CharacterTable
final class CharacterTable
The character table of a finite group with exact cyclotomic values.
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()}');
}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.
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());
}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 groupOrderfinal List<ConjugacyClass> classesfinal List<ClassFunction> charactersIrreducible characters, the trivial one first, sorted by degree.
int get classCountList<int> get classSizesList<int> get elementOrdersList<int> get degreesClassFunction get permutationCharacterThe permutation character of the group's action (number of fixed points), useful for decomposition checks.
Methods
int powerMap(int i, int k)The class index of g^{k} for the representative of class i.
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)]}');
}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.
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)]]}');
}⟨χ_i, χ_j⟩: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
ClassFunction tensor(ClassFunction a, ClassFunction b)Pointwise product (the character of the tensor product).
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)');
}χ ⊗ χ decomposes as [1, 1, 1] (multiplicities in table order)
List<Rational> decompose(ClassFunction f)Multiplicities of each irreducible in a class function (exact rationals; integers for characters).
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)}');
}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).
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()}');
}
}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.
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)]}');
}
}Q8: [1, 1, 1, 1, -1] D4: [1, 1, 1, 1, 1] C3: [1, 0, 0]
String toString()FpGroup
final class FpGroup
A group \langle x_{1}, \ldots, x_{m} \mid r_{1}, \ldots, r_{k}\rangle.
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()}');
}order 6
Constructors
FpGroup(this.generatorCount, List<List<int>> relators, {List<String>? names})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()}');
}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}).
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()}');
}order 24
Properties
final int generatorCountfinal List<List<int>> relatorsfinal List<String> namesMethods
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.
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}');
}index 2, action of the generators: [[0, 1], [1, 0]]
int order({int maxCosets = 1000000})| G | , when finite (enumerating the cosets of the trivial subgroup). |
|---|
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()}');
}⟨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).
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}');
}A5 acting on the cosets of ⟨b⟩: degree 20, order 60
CosetTable
final class CosetTable
The result of coset enumeration: the action of each generator on the cosets 0 … index−1 (coset 0 = H).
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]}');
}cosets 4; a acts as [1, 2, 3, 0]
Properties
final List<List<int>> generatorActionsgeneratorActions[c] = the coset c \cdot x_{i}.
int get indexList<Permutation> get permutationsPermutationGroup get permutationGroupNamedGroups
abstract final class NamedGroups
Constructors for standard groups. Points are 0-based.
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}');
}|S4| = 24, |A5| = 60 A5 abelian: false conjugacy classes of S4: 5
Static
static PermutationGroup cyclic(int n)The cyclic group C_{n} acting regularly on n points.
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}');
}C12: order 12, abelian character degrees [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
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}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final d = NamedGroups.dihedral(6);
print('D6: order ${d.order}, classes ${d.conjugacyClasses().length}');
}D6: order 12, classes 6
static PermutationGroup symmetric(int n)The symmetric group S_{n}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final n in [3, 5, 10]) print('S$n: order ${NamedGroups.symmetric(n).order}');
}S3: order 6 S5: order 120 S10: order 3628800
static PermutationGroup alternating(int n)The alternating group A_{n}, generated by the 3-cycles (0 1 k).
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}');
}A5: order 60, character degrees [1, 3, 3, 4, 5]
static PermutationGroup kleinFour()The Klein four-group V_{4} = C_{2} \times C_{2} acting regularly on 4 points.
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)}');
}V4: order 4, every element squares to 1: true
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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final q = NamedGroups.quaternion();
print('Q8: order ${q.order}, degrees ${CharacterTable.of(q).degrees}');
}Q8: order 8, degrees [1, 1, 1, 1, 2]
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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('Dic3: order ${NamedGroups.dicyclic(3).order}');
}Dic3: order 12
static PermutationGroup generalisedQuaternion(int order)The generalised quaternion group of order order (a power of 2, ≥ 8).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('Q16: order ${NamedGroups.generalisedQuaternion(16).order}');
}Q16: order 16
static PermutationGroup elementaryAbelian(int p, int k)Elementary abelian group (C_{p})^{k} acting regularly on p^{k} points.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('(C3)³: order ${NamedGroups.elementaryAbelian(3, 3).order}');
}(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).
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}');
}PSL(2,7): order 168
static PermutationGroup mathieu11()The Mathieu group M_{11} (order 7920) on 11 points: ⟨(1,…,11), (3,7,11,8)(4,10,5,6)⟩.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('M11: order ${NamedGroups.mathieu11().order}');
}M11: order 7920
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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('M12: order ${NamedGroups.mathieu12().order}');
}M12: order 95040
Permutation
final class Permutation
A permutation of {0, …, degree−1}, immutable.
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}');
}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.
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}');
}p = (1,2,3)(4,5), cycles [[0, 1, 2], [3, 4]], sign -1
factory Permutation.identity(int n)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final e = Permutation.identity(4);
print('$e, identity ${e.isIdentity}');
}(), 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]].
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}');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final p = Permutation.parse('(1,2,3)(4,5)', 5);
print('$p, order ${p.order}');
}(1,2,3)(4,5), order 6
Properties
int get degreeList<int> get imagesPermutation get inversebool get isIdentityList<int> get cycleTypeCycle lengths including fixed points, sorted descending (the partition of n).
bool get isEvenint get hashCodeMethods
Permutation pow(int k)p^{k} for any integer k.
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)}');
}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.
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)}');
}x⁻¹px = (2,3,4)
Permutation commutator(Permutation q)The commutator p^{-1} q^{-1} p q.
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)}');
}[p, q] = (1,2,3)
Permutation extend(int n)Same permutation on n ≥ degree points.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Permutation.parse('(1,2)', 2).extend(5)}');
}(1,2)
Operators
int operator [](int x)The image of x.
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)');
}p[0] = 2, p[2] = 4, p[1] = 1 (0-based)
Permutation operator *(Permutation o)Left-to-right product: apply this, then o. Degrees are padded to the larger one.
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}');
}p*q = (1,3,2), q*p = (1,2,3)
bool operator ==(Object other)StabiliserLevel
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.
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}');
}base point 0: orbit size 4 base point 1: orbit size 3 base point 2: orbit size 2
Properties
final int basePointfinal List<Permutation> generatorsfinal Map<int, Permutation> transversalPermutationGroup
class PermutationGroup
A group generated by permutations of {0, …, degree−1}.
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}');
}rotations: order 4, abelian true with a flip: order 8, abelian false transitive on the 4 corners: true
Constructors
PermutationGroup(List<Permutation> generators, {int? degree})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}');
}order 24, base [0, 1]
Properties
final int degreefinal List<Permutation> generatorslate final List<StabiliserLevel> chainPermutation get identitybool get isTransitivebool get isAbelianIterable<Permutation> get elementsEvery element, as products u_{k} \cdots u_{1} of transversal elements. Use only for small groups.
Methods
bool contains(Permutation g)Sifts g through the chain; the residue is the identity exactly when g \in G.
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))}');
}(1,2,3) ∈ A4: true, (1,2) ∈ A4: false
bool containsGroup(PermutationGroup other)Whether every element of other is in this group.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('A4 ⊂ S4: ${NamedGroups.symmetric(4).containsGroup(NamedGroups.alternating(4))}');
}A4 ⊂ S4: true
List<int> orbit(int point)The orbit of a point.
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)}');
}orbit of 0: [0, 1], of 4: [4]
Permutation random(math.Random rng)A uniformly random element (one random transversal element per level).
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)]);
}[(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.
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}');
}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}).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
for (final c in NamedGroups.symmetric(4).conjugacyClasses()) print('${c.representative.cycleType}: ${c.size} elements');
}[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.
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}');
}C2 × C3: order 6, degree 5
String toString()ConjugacyClass
final class ConjugacyClass
A conjugacy class of a finite permutation group.
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(' '));
}() (1) (1,3)(2,4) (1) (1,4)(2,3) (2) (2,4) (2) (1,2,3,4) (2)
Properties
final Permutation representativefinal List<Permutation> membersint get sizeMethods
String toString()CayleyDickson
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}, ….
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)}');
}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).
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}');
}an octonion: 1/2e1 + 1e2 + 1/2e4 + 1e5 + 1/2e7, |o|² = 11/4
factory CayleyDickson.ints(List<int> coords)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}');
}x x⁻¹ = 1
factory CayleyDickson.unit(int level, int k)The basis unit e_{k} of the algebra of the given level.
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}');
}e1·e2 = 1e3, e2·e1 = -1e3
factory CayleyDickson.zero(int level)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CayleyDickson.zero(2)}');
}0
Static
static CayleyDickson quaternion(int w, int x, int y, int z)Quaternions (level 2), octonions (3) and sedenions (4).
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}');
}ij = 1e3, ji = -1e3
static CayleyDickson associator(CayleyDickson x, CayleyDickson y, CayleyDickson z)The associator (xy)z - x(yz).
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])}');
}quaternions: 0 octonions: 2e7
static CayleyDickson commutator(CayleyDickson x, CayleyDickson y)The commutator xy - yx.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CayleyDickson.commutator(CayleyDickson.unit(2, 1), CayleyDickson.unit(2, 2))}');
}2e3
Properties
final int levelfinal List<Rational> cint get dimensionCayleyDickson get conjugateCayleyDickson get inversex* / |x|^{2} (a two-sided inverse up to the octonions; sedenions have zero divisors).
bool get isZeroint get hashCodeMethods
CayleyDickson scale(Rational r)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CayleyDickson.quaternion(1, 2, 3, 4).scale(Rational.fromInt(1, 2))}');
}1/2 + 1e1 + 3/2e2 + 2e3
String toString()Operators
CayleyDickson operator +(CayleyDickson o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CayleyDickson.quaternion(1, 2, 3, 4) + CayleyDickson.quaternion(0, -2, 1, 0)}');
}1 + 4e2 + 4e3
CayleyDickson operator -(CayleyDickson o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${CayleyDickson.quaternion(1, 2, 3, 4) - CayleyDickson.quaternion(1, 2, 3, 4)}');
}0
CayleyDickson operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-CayleyDickson.quaternion(1, -2, 3, -4)}');
}-1 + 2e1 - 3e2 + 4e3
CayleyDickson operator *(CayleyDickson o)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}');
}(e3 + e10)(e6 − e15) = 0
bool operator ==(Object other)Quaternion
final class Quaternion
w + x i + y j + z k with i^{2} = j^{2} = k^{2} = ijk = -1.
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}');
}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
const Quaternion(this.w, [this.x = 0, this.y = 0, this.z = 0])import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
const q = Quaternion(1, 2, 3, 4);
print('q = $q, q·q⁻¹ = ${q * q.inverse}');
}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*.
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)}');
}q = 0.7071067811865476 + 0.0i + 0.0j + 0.7071067811865475k, rotates (1,0,0) to (2.220446049250313e-16, 1.0, 0.0)
Static
static const Quaternion one = Quaternion(1)static const Quaternion i = Quaternion(0, 1)static const Quaternion j = Quaternion(0, 0, 1)static const Quaternion k = Quaternion(0, 0, 0, 1)Properties
final double wfinal double xfinal double yfinal double zQuaternion get conjugatedouble get normQuaternion get inverseQuaternion get normalizedint get hashCodeMethods
Quaternion scale(double s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${const Quaternion(1, 2, 3, 4).scale(0.5)}');
}0.5 + 1.0i + 1.5j + 2.0k
(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.
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)');
}(1,0,0) ↦ (1.1102230246251565e-16, 1.0, -1.1102230246251565e-16) (exactly (0,1,0); doubles leave small residues)
Quaternion exp()e^{q}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('exp(iπ/2) = ${const Quaternion(0, 3.141592653589793 / 2).exp()}');
}exp(iπ/2) = 6.123233995736766e-17 + 1.0i + 0.0j + 0.0k
Quaternion log()Principal logarithm.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('log(i) = ${Quaternion.i.log()}');
}log(i) = 0.0 + 1.5707963267948966i + 0.0j + 0.0k
Quaternion slerp(Quaternion to, double t)Spherical linear interpolation between unit quaternions (shortest arc).
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)}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(Quaternion.axisAngle(0, 0, 1, 3.141592653589793 / 2).toRotationMatrix());
}[[2.220446049250313e-16, -1.0, 0.0], [1.0, 2.220446049250313e-16, 0.0], [0.0, 0.0, 1.0]]
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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(Quaternion.j.toSu2());
}0.0+-0.0i -1.0+-0.0i 1.0+-0.0i 0.0+0.0i
bool approxEquals(Quaternion o, [double tol = 1e-12])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))}');
}equal to the 0.7 rotation: false, approximately: true
String toString()Operators
Quaternion operator +(Quaternion o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Quaternion.i + Quaternion.j}');
}0.0 + 1.0i + 1.0j + 0.0k
Quaternion operator -(Quaternion o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${const Quaternion(1, 2, 3, 4) - Quaternion.one}');
}0.0 + 2.0i + 3.0j + 4.0k
Quaternion operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-Quaternion.k}');
}-0.0 + 0.0i + 0.0j - 1.0k
Quaternion operator *(Quaternion o)The Hamilton product.
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}');
}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
Quaternion operator /(Quaternion o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('k / j = ${Quaternion.k / Quaternion.j}');
}k / j = 0.0 + 1.0i + 0.0j + 0.0k
bool operator ==(Object other)MatrixLieAlgebra
final class MatrixLieAlgebra
A Lie algebra spanned by linearly independent matrices, with bracket the commutator.
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}');
}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
MatrixLieAlgebra(List<CMatrix> basis)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final g = MatrixLieAlgebra(so3Generators);
print('so(3): c_012 = ${g.structureConstants[0][1][2]}');
}so(3): c_012 = (1.0+0.0j)
Properties
final List<CMatrix> basisint get dimensionlate final List<List<List<Complex>>> structureConstantsc_{\mathrm{abc}} with [X_{a}, X_{b}] = \sum_{c} c_{\mathrm{abc}} X_{c}.
List<List<List<double>>> get physicsStructureConstantsFor 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 killingFormThe Killing form K_{ab} = \operatorname{Tr}(ad X_{a} ad X_{b}).
double get jacobiDefectMax |\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).
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)}');
}[(2.0+0.0j), (0.0+0.0j), (-1.0+0.0j)]
bool spans(CMatrix y, [double tol = 1e-10])Whether y lies in the span (residual below tol).
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))}');
}[L1, L2] in so(3): true, identity: false
CMatrix adjoint(int a)The adjoint matrix \operatorname{ad}(X_{a}): (ad X_{a})_cb = c_abc.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(MatrixLieAlgebra(so3Generators).adjoint(2));
}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
Irrep
final class Irrep
The irreducible representation V(\lambda) of the simple Lie algebra with root system rootSystem.
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}');
}dimension 8, Casimir 3, weights 7
Constructors
Irrep(this.rootSystem, this.highestWeight)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final v = Irrep(RootSystem.g2(), Weight([1, 0]));
print('G2 V(ω1): dimension ${v.dimension}');
}G2 V(ω1): dimension 7
factory Irrep.of(RootSystem rs, List<int> labels)V(\lambda) from Dynkin labels.
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}');
}quark triplet: dim 3, Casimir 4/3, dual (0,1)
Properties
final RootSystem rootSystemfinal Weight highestWeightBigInt get dimensionWeyl dimension formula \prod_{\alpha > 0} \frac{(\lambda + \rho, \alpha)}{(\rho, \alpha)}, exact.
Rational get casimirThe 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 casimirPhysicsC_{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 dynkinIndexThe 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).
Map<Weight, int> get dominantWeightsDominant weights with their multiplicities, from the highest weight downwards.
Methods
int multiplicity(Weight w)The multiplicity of a weight (0 if absent).
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]))}');
}mult(0) = 2, mult(highest) = 1
String toString()Weight
final class Weight
A weight in Dynkin labels (coefficients on the fundamental weights), immutable with value equality.
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])}');
}w = (2,-1), 3w = (6,-3), w + ρ = (3,0)
Constructors
factory Weight(List<int> labels)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight([1, 0, 1])}');
}(1,0,1)
factory Weight.zero(int rank)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight.zero(4)}');
}(0,0,0,0)
Properties
int get rankList<int> get labelsbool get isDominantbool get isZeroint get hashCodeMethods
Weight times(int k)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight([1, 1]).times(2)}');
}(2,2)
String toString()Operators
int operator [](int i)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight([3, 5])[1]}');
}5
Weight operator +(Weight o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight([1, 0]) + Weight([0, 1])}');
}(1,1)
Weight operator -(Weight o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${Weight([1, 1]) - Weight([2, -1])}');
}(-1,2)
Weight operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${-Weight([1, -2])}');
}(-1,2)
bool operator ==(Object other)LieFamily
enum LieFamily
The Cartan–Killing type of a simple Lie algebra.
Values
abcdefgRootSystem
final class RootSystem
A simple root system of type X_{n}.
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}');
}E8: 120 positive roots, |W| = 696729600, Coxeter number 30
Constructors
factory RootSystem.a(int n)A_{n} (su(n + 1)), n \ge 1.
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}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final b3 = RootSystem.b(3);
print('B3: Cartan ${b3.cartan}, |W| = ${b3.weylGroupOrder}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('C3: |W| = ${RootSystem.c(3).weylGroupOrder}, exponents ${RootSystem.c(3).exponents}');
}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}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('D4: degrees ${RootSystem.d(4).degrees}, |W| = ${RootSystem.d(4).weylGroupOrder}');
}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).
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}');
}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).
import 'package:riptide_algebra/riptide_algebra.ray';
import 'raya:convert';
void main() {
final f = RootSystem.f4();
print('F4: Cartan ${f.cartan}, |W| = ${f.weylGroupOrder}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final g = RootSystem.g2();
print('G2: positive roots ${g.positiveRoots}, |W| = ${g.weylGroupOrder}');
}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, …
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}');
}A4: rank 4, dimension 24 B2: rank 2, dimension 10 E7: rank 7, dimension 133
Properties
final LieFamily familyfinal int ranklate final List<Rational> halfLengths\frac{(\alpha_{i}, \alpha_{i})}{2} for each simple root (1 for long roots).
late final List<List<Rational>> weightForm(\omega_{i}, \omega_{j}): the quadratic form on weights in Dynkin labels.
late final List<List<int>> positiveRootsSimplePositive roots in simple-root coordinates, sorted by height (simple roots first).
String get nameint get dimensionWeight get highestRootThe highest root \theta (the highest weight of the adjoint representation).
List<int> get exponentsThe 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).
int get dualCoxeterNumberThe dual Coxeter number h \vee = 1 + (\theta, \rho) (long roots of length² 2).
List<int> get longestElementA 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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final a2 = RootSystem.a(2);
print('α1 = ${a2.simpleRoot(0)}, α2 = ${a2.simpleRoot(1)}');
}α1 = (2,-1), α2 = (-1,2)
Weight fundamental(int i)The fundamental weight \omega_{i}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('ω2 of A3: ${RootSystem.a(3).fundamental(1)}');
}ω2 of A3: (0,1,0)
Weight rootToWeight(List<int> c)A root in simple-root coordinates → Dynkin labels.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('α1 + α2 in A2: ${RootSystem.a(2).rootToWeight([1, 1])}');
}α1 + α2 in A2: (1,1)
List<Rational> weightToRootCoordinates(Weight w)Dynkin labels → simple-root coordinates (rational in general).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('ω1 of A2 = ${RootSystem.a(2).weightToRootCoordinates(Weight([1, 0]))} in simple roots');
}ω1 of A2 = [2/3, 1/3] in simple roots
Rational inner(Weight a, Weight b)(\lambda, \mu) for weights in Dynkin labels.
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]))}');
}(α1, α1) = 2, (α1, α2) = -1, (ω1, ω1) = 2/3
Weight reflect(Weight w, int i)The simple reflection s_{i}: \lambda \mapsto \lambda - \lambda_{i} \alpha_{i}.
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)}');
}s1(ω1) in A2: (-1,1)
Weight applyWord(List<int> word, Weight w)Applies w = s_(i1) s_(i2) ⋯ s_(ik) (the rightmost reflection acts first).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('s1 s2 (ρ) = ${RootSystem.a(2).applyWord([0, 1], Weight([1, 1]))}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${RootSystem.a(2).dominantConjugate(Weight([-1, -1]))}');
}((1,1), -1)
List<Weight> orbit(Weight w)The Weyl orbit of a weight.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('Weyl orbit of ω1 in B2: ${RootSystem.b(2).orbit(Weight([1, 0]))}');
}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.
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');
}6 elements of W(A2) as reduced words: [[], [0], [1], [1, 0], [0, 1], [0, 1, 0]]
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.
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)}');
}length of s1 s2 s1 = 3, of s1 s1 = 0, of w0 = 6
Weight dual(Weight lambda)The highest weight of the dual representation, -w_{0}\lambda.
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]))}');
}dual of su(3) (1,0): (0,1); of so(5) ω1: (1,0)
String toString()CMatrix
final class CMatrix
An immutable rows × cols complex matrix.
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)))}');
}σ1σ2 = iσ3: true
Constructors
factory CMatrix(List<List<Complex>> entries)From nested rows of Complex.
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}');
}Hermitian: true, det (0.0+0.0j)
factory CMatrix.real(List<List<num>> entries)From nested rows of real numbers.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(CMatrix.real([[1, 2], [3, 4.5]]));
}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)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(CMatrix.generate(2, 3, (i, j) => Complex(i.toDouble(), j.toDouble())));
}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])import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(CMatrix.zero(2, 3));
}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)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(CMatrix.identity(3));
}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)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(CMatrix.diagonal([Complex(1, 0), Complex(0, 1)]));
}1.0+0.0i 0.0+0.0i 0.0+0.0i 0.0+1.0i
Properties
final int rowsfinal int colsbool get isSquareCMatrix get transposeCMatrix get conjugateComplex get traceMethods
Complex at(int i, int j)Entry (i, j).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${pauli[1].at(0, 1)}');
}(-0.0-1.0j)
CMatrix scale(Complex s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[0].scale(Complex(0, 1)));
}0.0+0.0i 0.0+1.0i 0.0+1.0i 0.0+0.0i
CMatrix scaleReal(double s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[2].scaleReal(0.5));
}0.5+0.0i 0.0+0.0i 0.0+0.0i -0.5+-0.0i
CMatrix commutator(CMatrix o)[A, B] = AB − BA.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
// [σ1, σ2] = 2iσ3.
print(pauli[0].commutator(pauli[1]));
}0.0+2.0i 0.0+0.0i 0.0+0.0i 0.0-2.0i
CMatrix anticommutator(CMatrix o)\{A, B\} = AB + BA.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[0].anticommutator(pauli[0]));
}2.0+0.0i 0.0+0.0i 0.0+0.0i 2.0+0.0i
CMatrix kron(CMatrix o)Kronecker product A \otimes B.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final k = pauli[0].kron(pauli[2]);
print('${k.rows}×${k.cols}:\n$k');
}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
Complex inner(CMatrix o)Frobenius inner product \operatorname{Tr}(A^{\dagger} B).
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])}');
}Tr(σ1† σ1) = (2.0+0.0j), Tr(σ1† σ2) = (0.0+0.0j)
double distance(CMatrix o)Max-entry distance to o.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${pauli[0].distance(pauli[2])}');
}1.0
bool approxEquals(CMatrix o, [double tol = 1e-12])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))}');
}U U† ≈ I: true
CMatrix exp()The matrix exponential e^{A} (expm on complex128).
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());
}1.5013372914324722e-16-1.0i 0.0+0.0i 0.0+0.0i 1.5013372914324722e-16+1.0i
List<List<Complex>> toList()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[1].toList());
}[[(0.0+0.0j), (-0.0-1.0j)], [(0.0+1.0j), (0.0+0.0j)]]
String toString()Operators
Complex operator [](int k)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
// Row-major flat index.
print('${pauli[1][1]}, ${pauli[1][2]}');
}(-0.0-1.0j), (0.0+1.0j)
CMatrix operator +(CMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[0] + pauli[2]);
}1.0+0.0i 1.0+0.0i 1.0+0.0i -1.0+0.0i
CMatrix operator -(CMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[0] - pauli[0]);
}0.0+0.0i 0.0+0.0i 0.0+0.0i 0.0+0.0i
CMatrix operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(-pauli[2]);
}-1.0+-0.0i -0.0+-0.0i -0.0+-0.0i 1.0+0.0i
CMatrix operator *(CMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(pauli[0] * pauli[0]);
}1.0+0.0i 0.0+0.0i 0.0+0.0i 1.0+0.0i
QIMatrix
final class QIMatrix
An immutable rows × cols matrix over Q(i).
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)}');
}σ1σ2 == iσ3: true
Constructors
factory QIMatrix(List<List<GaussianRational>> entries)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}');
}m⁻¹ = 2 -i i 1
factory QIMatrix.ints(List<List<Object>> entries)From integer entries (re, im) pairs or plain ints.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.ints([[1, (0, 1)], [(0, -1), 1]]));
}1 i -i 1
factory QIMatrix.generate(int rows, int cols, GaussianRational Function(int i, int j) f)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.generate(2, 2, (i, j) => GaussianRational.integer(i, j)));
}0 i 1 1+i
factory QIMatrix.zero(int rows, [int? cols])import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.zero(2));
}0 0 0 0
factory QIMatrix.identity(int n)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.identity(2));
}1 0 0 1
Static
static QIMatrix block(QIMatrix a, QIMatrix b, QIMatrix c, QIMatrix d)Block matrix \begin{bmatrix}a & b \\ c & d\end{bmatrix}.
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));
}0 1 -1 0
Properties
final int rowsfinal int colsQIMatrix get transposeQIMatrix get daggerQIMatrix get conjugateGaussianRational get tracebool get isZerobool get isRealbool get isImaginaryGaussianRational? get scalarValueWhether this is s \cdot I for some scalar s (returned), else null.
int get hashCodeMethods
GaussianRational at(int i, int j)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${exactPauli[1].at(1, 0)}');
}i
QIMatrix scale(GaussianRational s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.identity(2).scale(GaussianRational.integer(1, 1)));
}1+i 0 0 1+i
QIMatrix scaleRational(Rational s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.identity(2).scaleRational(Rational.fromInt(1, 3)));
}1/3 0 0 1/3
QIMatrix scaleInt(int k)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(QIMatrix.identity(2).scaleInt(-2));
}-2 0 0 -2
QIMatrix commutator(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print(s[0].commutator(s[1]));
}2i 0 0 -2i
QIMatrix anticommutator(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print('{σ1, σ2} = 0: ${s[0].anticommutator(s[1]) == QIMatrix.zero(2)}');
}{σ1, σ2} = 0: trueQIMatrix kron(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print(s[2].kron(s[2]));
}1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1
CMatrix toCMatrix()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(exactPauli[1].toCMatrix());
}0.0+0.0i 0.0-1.0i 0.0+1.0i 0.0+0.0i
List<Complex> apply(List<Complex> v)Applies to a numeric vector.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${exactPauli[0].apply([Complex(1, 0), Complex(0, 0)])}');
}[(0.0+0.0j), (1.0+0.0j)]
String toString()Operators
QIMatrix operator +(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print(s[0] + s[2]);
}1 1 1 -1
QIMatrix operator -(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print(s[0] - s[0]);
}0 0 0 0
QIMatrix operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(-exactPauli[1]);
}0 i -i 0
QIMatrix operator *(QIMatrix o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final s = exactPauli;
print('σ2² = I: ${s[1] * s[1] == QIMatrix.identity(2)}');
}σ2² = I: true
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> preperiodThe terms before the period (for sqrt(n) just [floor(sqrt n)]).
Methods
String toString()EcPoint
final class EcPoint
A point on an EllipticCurve over Q: affine (x, y) or the point at infinity.
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)}');
}(2, 2) on y² = x³ − 2x: true
Constructors
const EcPoint(Rational this.x, Rational this.y)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EcPoint(Rational.fromInt(1, 4), Rational.fromInt(-3, 8))}');
}(1/4, -3/8)
const EcPoint.infinity()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('O = ${EcPoint.infinity()}, isInfinity ${EcPoint.infinity().isInfinity}');
}O = O, isInfinity true
factory EcPoint.ints(int x, int y)(x, y) from ints.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EcPoint.ints(3, 5)}');
}(3, 5)
Properties
final Rational? xfinal Rational? yfinal bool isInfinityint get hashCodeMethods
String toString()Operators
bool operator ==(Object other)EllipticCurve
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.
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)}');
}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
EllipticCurve(this.a1, this.a2, this.a3, this.a4, this.a6)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}');
}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).
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()}');
}11a1: Δ = -161051, conductor 11
factory EllipticCurve.short(Rational a, Rational b)The short Weierstrass curve y^2 = x^3 + a x + b.
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()}');
}y² = x³ − x: j = 1728, torsion [2, 2]
Properties
final Rational a1final Rational a2final Rational a3final Rational a4final Rational a6late final Rational b2late final Rational b4late final Rational b6late final Rational b8late final Rational c4late final Rational c6late final Rational discriminantThe discriminant Δ = −b2²b8 − 8b4³ − 27b6² + 9b2b4b6.
List<BigInt> get badPrimesThe primes of bad reduction of this (integral) model: the primes dividing \Delta.
bool get isSemistableWhether the (minimal integral) model is semistable: no bad prime divides c4.
Methods
bool contains(EcPoint p)Whether p is on the curve.
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))}');
}(−2, 3): true, (−1, 4): true, (1, 1): false
EcPoint negate(EcPoint p)-P = (x, -y - a_{1} x - a_{3}).
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))}');
}−(0, 0) = (0, -1)
EcPoint add(EcPoint p, EcPoint q)P + Q.
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)}');
}P + Q = (4, -9)
EcPoint double(EcPoint p)2P.
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');
}
}2^1 P = (1, 0) 2^2 P = (2, -3) 2^3 P = (21/25, -69/125) 2^4 P = (480106/4225, 332513754/274625)
EcPoint multiply(EcPoint p, int k)kP (negative k multiplies -P), by double-and-add.
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)}');
}[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.
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');
}integral model [0, 0, 0, 4, 8], u = 2
EllipticCurveFp reduce(BigInt p)The reduction modulo the prime p (the model must be p-integral). The result may be singular.
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');
}11a1 mod 7: 10 points
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.
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)]}');
}a_p for 11a1: [-2, -1, 1, -2, 4]
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.
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)}');
}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.
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))}');
}minimal at 11: true
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.
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()}');
}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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('11a1 torsion divides ${EllipticCurve.fromInts([0, -1, 1, -10, -20]).torsionBound()}');
}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}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print(EllipticCurve.fromInts([0, -1, 1, -10, -20]).torsionPoints());
}[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}.
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()}');
}11a1: [5], y² = x³ − x: [2, 2]
String toString()EcPointFp
final class EcPointFp
A point on an EllipticCurveFp: affine (x, y) with coordinates in [0, p), or the point at infinity.
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)}');
}(3, 6) on the curve: true, order 5
Constructors
const EcPointFp(BigInt this.x, BigInt this.y)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EcPointFp(BigInt.from(1), BigInt.from(2))}');
}(1, 2)
const EcPointFp.infinity()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EcPointFp.infinity()}');
}O
Properties
final BigInt? xfinal BigInt? yfinal bool isInfinityint get hashCodeMethods
String toString()Operators
bool operator ==(Object other)EllipticCurveFp
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}.
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)}');
}P = (0, 1), Q = (1, 1) P + Q = (96, 96) 2P = (73, 84) #E(F_97) = 98 order of P = 98
Constructors
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).
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}');
}#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.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final e = EllipticCurveFp.fromInts([0, 0, 0, -1, 0], 101);
print('#E = ${e.countPoints()}');
}#E = 104
Properties
final BigInt a1final BigInt a2final BigInt a3final BigInt a4final BigInt a6final BigInt plate final BigInt b2late final BigInt b4late final BigInt b6late final BigInt b8late final BigInt c4late final BigInt c6late final BigInt discriminantbool get isSingularMethods
bool contains(EcPointFp q)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)))}');
}true, false
EcPointFp negate(EcPointFp q)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)))}');
}(3, 91)
EcPointFp add(EcPointFp q, EcPointFp r)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))}');
}P + P = (80, 10), P + (−P) = O
EcPointFp double(EcPointFp q)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)))}');
}(80, 10)
EcPointFp multiply(EcPointFp q, BigInt k)kQ by double-and-add.
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))}');
}[5]P = O
List<EcPointFp> points()Every point (O first), for small p.
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');
}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.
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()}');
}#E(F_10007) = 10065
EcPointFp randomPoint(math.Random rnd)A uniformly random affine point (p odd).
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)}');
}(1955, 1239), on curve true
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.
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()}');
}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).
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()}');
}#E(F_1000003) = 1000727
BigInt trace()The trace of Frobenius p + 1 − #E(F_p).
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()}');
}trace of Frobenius -57
String toString()NtComplex
final class NtComplex
A complex number in double precision (the numeric value of a character or an L-value).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final z = NtComplex(3, 4);
print('$z, |z|² = ${(z * NtComplex(3, -4)).re}');
}3.0 + 4.0i, |z|² = 25.0
Constructors
const NtComplex(this.re, [this.im = 0])import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${NtComplex(1, -2) + NtComplex(0.5, 0.5)}');
}1.5 - 1.5i
factory NtComplex.unitTurn(double t)e^{2πi t}.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('e^{2πi/4} = ${NtComplex.unitTurn(0.25)}');
}e^{2πi/4} = 6.123233995736766e-17 + 1.0iProperties
final double refinal double imdouble get absMethods
NtComplex scale(double s)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${NtComplex(1, 2).scale(3)}');
}3.0 + 6.0i
String toString()Operators
NtComplex operator +(NtComplex o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${NtComplex(1, 2) + NtComplex(3, 4)}');
}4.0 + 6.0i
NtComplex operator -(NtComplex o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${NtComplex(1, 2) - NtComplex(3, 4)}');
}-2.0 - 2.0i
NtComplex operator *(NtComplex o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${NtComplex(1, 2) * NtComplex(3, 4)}');
}-5.0 + 10.0i
RootOfUnity
final class RootOfUnity
The exact root of unity e^{2πi k / order}, 0 <= k < order, in lowest terms.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final w = RootOfUnity(1, 3);
print('ω = $w, ω³ = ${w * w * w}, conj ${w.conjugate()}');
}ω = e^(2πi·1/3), ω³ = 1, conj e^(2πi·2/3)
Constructors
factory RootOfUnity(int k, int order)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${RootOfUnity(2, 6)} (reduced from 2/6)');
}e^(2πi·1/3) (reduced from 2/6)
Properties
final int kfinal int orderbool get isOneNtComplex get valueint get hashCodeMethods
RootOfUnity conjugate()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${RootOfUnity(1, 8).conjugate()}');
}e^(2πi·7/8)
String toString()Operators
RootOfUnity operator *(RootOfUnity o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${RootOfUnity(1, 4) * RootOfUnity(1, 6)}');
}e^(2πi·5/12)
bool operator ==(Object other)DirichletGroup
final class DirichletGroup
The group of Dirichlet characters modulo n: all \varphi(n) characters, built from the structure of (Z/n)^×.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final g = DirichletGroup(15);
print('(ℤ/15)^× factors ${g.factorOrders}, ${g.characters.length} characters');
}(ℤ/15)^× factors [2, 4], 8 characters
Constructors
DirichletGroup(this.modulus)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${DirichletGroup(16).factorOrders}');
}[2, 4]
Properties
final int modulusList<int> get factorOrdersThe orders of the cyclic factors of (Z/n)^× (their product is \varphi(n)).
List<DirichletCharacter> get charactersAll \varphi(n) characters; the principal character first.
Methods
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}.
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)]}');
}χ mod 7: [1, e^(2πi·1/3), e^(2πi·1/6), e^(2πi·2/3), e^(2πi·5/6), -1]
DirichletCharacter
final class DirichletCharacter
A Dirichlet character modulo modulus with exact values e^{2πi e(a) / order} (0 off the units).
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)}');
}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).
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}');
}(5/a): [1, -1, -1, 1, 0, 1, -1, -1, 1, 0], conductor 5
factory DirichletCharacter.principal(int n)The principal character modulo n.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final chi = DirichletCharacter.principal(6);
print('${[for (var a = 1; a <= 6; a++) chi(a)]}');
}[1, null, null, null, 1, null]
Properties
final int modulusbool get isPrincipalbool get isRealint get conductorThe conductor: the least d | modulus such that \chi(a) = 1 whenever a ≡ 1 (mod d) and gcd(a, modulus) = 1.
bool get isPrimitiveMethods
RootOfUnity? call(int a)\chi(a) exactly, or null when gcd(a, modulus) > 1.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final chi = DirichletGroup(5).character([1]);
print('χ(2) = ${chi(2)}, χ(5) = ${chi(5)}');
}χ(2) = e^(2πi·1/4), χ(5) = null
NtComplex value(int a)\chi(a) as a complex number (0 off the units).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final chi = DirichletGroup(5).character([1]);
print('χ(2) ≈ ${chi.value(2)}');
}χ(2) ≈ 6.123233995736766e-17 + 1.0i
DirichletCharacter primitive()The primitive character inducing \chi (of modulus conductor).
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}');
}conductor 3, primitive modulus 3, same as (−3/·): true
DirichletCharacter conjugate()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
final chi = DirichletGroup(5).character([1]);
print('χ(2) = ${chi(2)}, conj χ(2) = ${chi.conjugate()(2)}');
}χ(2) = e^(2πi·1/4), conj χ(2) = e^(2πi·3/4)
NtComplex gaussSum()The Gauss sum τ(χ) = Σ_a χ(a) e^{2πi a / modulus}.
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}');
}τ = 5.551115123125783e-16 + 2.6457513110645907i, |τ|² = 7.000000000000001
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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('L(2, χ₋₄) = ${DirichletCharacter.kronecker(-4).lValue(2).re} (Catalan’s constant)');
}L(2, χ₋₄) = 0.9159655941772189 (Catalan’s constant)
String toString()Operators
DirichletCharacter operator *(DirichletCharacter o)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)!}');
}(a·b)(3) = -1, a(3)·b(3) = -1
EllipticLSeries
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.
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()}');
}root number -1, L′(E, 1) = 0.30599977383405197
Constructors
EllipticLSeries(this.curve, {BigInt? conductor})conductor defaults to EllipticCurve.conductor (semistable curves only). The model must be integral and minimal at the bad primes.
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}');
}11a1: conductor 11, root number 1
Properties
final EllipticCurve curvefinal BigInt conductorlate final int rootNumberThe 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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20])).functionalEquationResidual()}');
}1.1102230246251565e-16
double valueAt1()L(E, 1) (0 when the root number is -1).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
// L(11a1, 1) ≈ 0.2538418608.
print('${EllipticLSeries(EllipticCurve.fromInts([0, -1, 1, -10, -20])).valueAt1()}');
}0.25384186085591065
double derivativeAt1()L'(E, 1) when the root number is -1 (odd analytic rank); throws otherwise.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${EllipticLSeries(EllipticCurve.fromInts([0, 0, 1, -1, 0])).derivativeAt1()}');
}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}.
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)}');
}s = 2, p ≤ 1000: 0.5460478673445237
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.
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)}');
}(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
factory QSeries(List<BigInt> coefficients, [BigInt? denominator])coefficients are the numerators of q^0.. q^(precision−1).
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}');
}[1/2, 3/2, -1/2], precision 3
factory QSeries.ints(List<int> c)From int coefficients.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QSeries.ints([1, -1, 0, 1]).pow(2).coefficients}');
}[1, -2, 1, 2]
factory QSeries.rationals(List<Rational> c)From exact rationals.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QSeries.rationals([Rational.one, Rational.fromInt(1, 2), Rational.fromInt(1, 3)]).rationalCoefficients}');
}[1, 1/2, 1/3]
factory QSeries.constant(BigInt c, int precision)The constant c + O(q^precision).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QSeries.constant(BigInt.from(5), 4).coefficients}');
}[5, 0, 0, 0]
Properties
final List<BigInt> coefficientsfinal BigInt denominatorint get precisionThe number of known coefficients: the series is known modulo q^precision.
bool get isIntegralList<Rational> get rationalCoefficientsThe coefficients as exact rationals.
int get hashCodeMethods
BigInt integerCoefficient(int n)The coefficient of q^n (the series must be integral).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('τ(12) = ${delta(13).integerCoefficient(12)}');
}τ(12) = -370944
QSeries truncate(int precision)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${thetaZ(20).truncate(6).coefficients}');
}[1, 2, 0, 0, 2, 0]
QSeries scale(Rational r)Multiplication by the rational r.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${eisenstein(4, 4).scale(Rational.fromInt(1, 240)).rationalCoefficients}');
}[1/240, 1, 9, 28]
QSeries scaleInt(int k)Multiplication by the integer k.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${thetaZ(5).scaleInt(3).coefficients}');
}[3, 6, 0, 0, 6]
QSeries pow(int k)this^k for k >= 0 (repeated squaring).
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}');
}[1, 8, 24, 32, 24, 48, 96, 64, 24, 104]
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).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QSeries.ints([1, 2, 3]).shift(2).coefficients}');
}[0, 0, 1, 2, 3]
QSeries substitutePower(int m, [int? precision])f(q^m).
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${QSeries.ints([1, 1, 1]).substitutePower(3).coefficients}');
}[1, 0, 0, 1, 0, 0, 1]
bool agreesWith(QSeries o)Equality of the first min(precision, o.precision) coefficients.
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)}');
}true
String toString()Operators
Rational operator [](int n)The coefficient of q^n as a Rational.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${eisenstein(4, 3)[1]}');
}240
QSeries operator +(QSeries o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(QSeries.ints([1, 1]) + QSeries.ints([0, 1, 1])).coefficients}');
}[1, 2]
QSeries operator -()import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(thetaZ(5) - thetaZ(5)).coefficients}');
}[0, 0, 0, 0, 0]
QSeries operator -(QSeries o)import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(-QSeries.ints([1, -2, 3])).coefficients}');
}[-1, 2, -3]
QSeries operator *(QSeries o)The Cauchy product, truncated at the smaller precision.
import 'package:riptide_algebra/riptide_algebra.ray';
void main() {
print('${(QSeries.ints([1, 1, 0, 0]) * QSeries.ints([1, -1, 1, -1])).coefficients}');
}[1, 0, 0, 0]
bool operator ==(Object other)