Overview
Wildberger's algebraic calculus, exact over Q and GF(p): polynumbers, subderivatives and tangents with no limits, signed areas, dCB curves, Cavalieri from five properties, quadratic curvature, boxes and maxels.
Also exported here
Functions and constants
Functions
Mset eulerProduct(int n)The truncated Euler product \prod^{\wedge}_{p \le n} [1, p, p^{2}, \ldots] over the primes p \le n, caret-multiplied and cut at n: unique factorisation says it is [1, 2, 3, \ldots, n].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The Euler product over the primes p ≤ 12 of [1, p, p², …], caret-multiplied and cut at 12.
final e = eulerProduct(12);
print('eulerProduct(12) = $e');
print('equals [1, 2, …, 12]: ${e == Mset([for (var k = 1; k <= 12; k++) k])}');
// One factor by hand: the powers of 2 and of 3 up to 12, caret-multiplied.
final twos = Mset([1, 2, 4, 8]), threes = Mset([1, 3, 9]);
print('[1,2,4,8] ^ [1,3,9] = ${twos ^ threes}');
print('cut at 12: ${(twos ^ threes).upTo(12)}');
}eulerProduct(12) = [1,2,3,4,5,6,7,8,9,10,11,12] equals [1, 2, …, 12]: true [1,2,4,8] ^ [1,3,9] = [1,2,3,4,6,8,9,12,18,24,36,72] cut at 12: [1,2,3,4,6,8,9,12]
CavalieriResult<T> cavalieri<T>(ScalarField<T> field, int n)Runs the derivation over field for x^{N}, using the A_{k} already derived for k < N (each by the same argument).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The area under xᴺ from 0 to 1 derived from five properties of area, for N = 1 … 4.
for (var n = 1; n <= 4; n++) {
final r = cavalieri(rationals, n);
print('N = $n: ok ${r.ok}, coefficient ${r.coefficient}');
}
}N = 1: ok true, coefficient 1/2 N = 2: ok true, coefficient 1/3 N = 3: ok true, coefficient 1/4 N = 4: ok true, coefficient 1/5
T colourQuadrance<T>(ScalarField<T> f, T x, T y, [Colour colour = Colour.blue])The quadrance of (x, y) in colour.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Chromogeometry: blue² = red² + green² for every vector.
final x = Rational.fromInt(5), y = Rational.fromInt(2);
final b = colourQuadrance(rationals, x, y);
final r = colourQuadrance(rationals, x, y, Colour.red);
final g = colourQuadrance(rationals, x, y, Colour.green);
print('blue $b, red $r, green $g');
print('blue² == red² + green²: ${b * b == r * r + g * g}');
}blue 29, red 21, green 20 blue² == red² + green²: true
T quadrance<T>(ScalarField<T> f, Pt<T> a, Pt<T> b, [Colour colour = Colour.blue])The quadrance between two points.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final a = (Rational.zero, Rational.zero);
final b = (Rational.fromInt(3), Rational.fromInt(4));
print('blue quadrance (x² + y²): ${quadrance(rationals, a, b)}');
print('red quadrance (x² − y²): ${quadrance(rationals, a, b, Colour.red)}');
print('green quadrance (2xy): ${quadrance(rationals, a, b, Colour.green)}');
}blue quadrance (x² + y²): 25 red quadrance (x² − y²): -7 green quadrance (2xy): 24
CurvatureAt<T> curvatureAt<T>(Bipolynumber<T> curve, T x, T y, [Colour colour = Colour.blue])The quadratic curvature of the curve curve = 0 at the point (x, y) on it.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Quadratic curvature of x² + y² = 25 at (3, 4) from the tangent conic, with no limits.
final circle = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 2): 1, (0, 0): -25});
final c = curvatureAt(circle, Rational.fromInt(3), Rational.fromInt(4));
print('tangent conic at (3, 4): ${circle.tangentAt(Rational.fromInt(3), Rational.fromInt(4), 2).format()}');
print('l = ${c.l}, m = ${c.m}, n = ${c.n}, p = ${c.p}, q = ${c.q}');
print('K = ${c.curvature}, radius quadrance 1/K = ${c.radiusQuadrance}, centre ${c.centre}');
// The parabola y = x² at (1, 1).
final par = Bipolynumber.ints(rationals, {(0, 1): 1, (2, 0): -1});
final k = curvatureAt(par, Rational.one, Rational.one);
print('y = x² at (1, 1): K = ${k.curvature}, centre ${k.centre}, focus ${k.focus}');
}tangent conic at (3, 4): 6x + 8y + x² + y² l = 6, m = 8, n = 1, p = 0, q = 1 K = 1/25, radius quadrance 1/K = 25, centre (0, 0) y = x² at (1, 1): K = 4/125, centre (-4, 7/2), focus (-3/2, 9/4)
T graphCurvature<T>(Polynumber<T> f, T x)The curvature of the graph y = f(x) at x: K = \frac{4(D_{2}f)^{2}}{(1 + (Df)^{2})^{3}} (with D_{2} the second subderivative).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Curvature of y = α² as an exact rational: 4(D2 f)² / (1 + (D1 f)²)³.
final parabola = Polynumber.ints(rationals, [0, 0, 1]);
for (final x in [0, 1, 2]) {
print('curvature of y = α² at $x: ${graphCurvature(parabola, Rational.fromInt(x))}');
}
}curvature of y = α² at 0: 4 curvature of y = α² at 1: 4/125 curvature of y = α² at 2: 4/4913
T? circumQuadrance<T>(ScalarField<T> f, Pt<T> a, Pt<T> b, Pt<T> c)The circumquadrance R = \frac{Q_{1}Q_{2}Q_{3}}{A} of three points, or null when they are collinear (A = 0).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
(Rational, Rational) pt(int x, int y) => (Rational.fromInt(x), Rational.fromInt(y));
final a = pt(0, 0), b = pt(4, 0), c = pt(0, 3);
final r = circumQuadrance(rationals, a, b, c)!;
print('circumquadrance (radius²): $r');
print('collinear: ${circumQuadrance(rationals, a, pt(1, 1), pt(2, 2))}');
'polygons': [[[0, 0], [4, 0], [0, 3]]],
'circles': [[2, 1.5, 2.5]],
'points': [[0, 0, 'A'], [4, 0, 'B'], [0, 3, 'C']]})}');
}circumquadrance (radius²): 25/4 collinear: null
T? threePointCurvature<T>(ScalarField<T> f, Pt<T> a, Pt<T> b, Pt<T> c)The three-point curvature K = \frac{A}{Q_{1}Q_{2}Q_{3}}, or null when two points coincide.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Wildberger's three-point curvature K = A/(Q₁Q₂Q₃): exact for three rational points, no sine or cosine.
// Three points on the cubic y = x³ − x.
final f = Polynumber.ints(rationals, [0, -1, 0, 1]);
Pt<Rational> on(Rational x) => (x, f.evaluate(x));
final a1 = on(Rational.fromInt(-1)), a2 = on(Rational.fromInt(1, 2)), a3 = on(Rational.fromInt(2));
final q1 = quadrance(rationals, a2, a3), q2 = quadrance(rationals, a1, a3), q3 = quadrance(rationals, a1, a2);
final area = quadrea(rationals, q1, q2, q3);
final k = threePointCurvature(rationals, a1, a2, a3)!;
print('A1 = $a1, A2 = $a2, A3 = $a3');
print('Q1 = $q1, Q2 = $q2, Q3 = $q3');
print('A (quadrea) = $area');
print('K = A/(Q1·Q2·Q3) = $k');
print('1/K = circumquadrance: ${Rational.one / k == circumQuadrance(rationals, a1, a2, a3)}');
print('three collinear points on the curve (x = −1, 0, 1): K = '
'${threePointCurvature(rationals, on(Rational.fromInt(-1)), on(Rational.zero), on(Rational.one))}');
print('two equal points: ${threePointCurvature(rationals, a1, a1, a3)}');
// The picture: the curve, the triangle with its quadrances, and the circle through the points (radius² = 1/K).
double d(Rational r) => r.toDouble();
final pts = [a1, a2, a3].map((p) => [d(p.$1), d(p.$2)]).toList();
final ax = pts[0][0], ay = pts[0][1], bx = pts[1][0], by = pts[1][1], cx = pts[2][0], cy = pts[2][1];
final den = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
final ux = ((ax * ax + ay * ay) * (by - cy) + (bx * bx + by * by) * (cy - ay) + (cx * cx + cy * cy) * (ay - by)) / den;
final uy = ((ax * ax + ay * ay) * (cx - bx) + (bx * bx + by * by) * (ax - cx) + (cx * cx + cy * cy) * (bx - ax)) / den;
final curve = [for (var i = 0; i < 40; i++) -1.5 + 3.8 * i / 39].map((x) => [x, x * x * x - x]).toList();
'segments': [for (var i = 0; i + 1 < curve.length; i++) [...curve[i], ...curve[i + 1]]],
'polygons': [pts], 'circles': [[ux, uy, _sqrtD(1 / d(k))]],
'points': [[ax, ay, 'A1'], [bx, by, 'A2'], [cx, cy, 'A3']]})}');
}
// Display only: the radius for drawing the circle on screen.
double _sqrtD(double q) {
var r = q;
for (var i = 0; i < 60; i++) r = (r + q / r) / 2;
return r;
}A1 = (-1, 0), A2 = (1/2, -3/8), A3 = (2, 6) Q1 = 2745/64, Q2 = 45, Q3 = 153/64 A (quadrea) = 6561/16 K = A/(Q1·Q2·Q3) = 2304/25925 1/K = circumquadrance: true three collinear points on the curve (x = −1, 0, 1): K = 0 two equal points: null
T quadrea<T>(ScalarField<T> f, T q1, T q2, T q3)Archimedes' quadrea A = (Q_{1} + Q_{2} + Q_{3})^{2} - 2(Q_{1}^{2} + Q_{2}^{2} + Q_{3}^{2}) = 16 \cdot \mathrm{area}^{2}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Archimedes: A = (Q1 + Q2 + Q3)² − 2(Q1² + Q2² + Q3²) = 16·area². The 3-4-5 triangle has area 6.
final a = quadrea(rationals, Rational.fromInt(9), Rational.fromInt(16), Rational.fromInt(25));
print('quadrea of the 3-4-5 triangle: $a');
print('16 · 6² = ${16 * 36}');
print('collinear points give 0: ${quadrea(rationals, Rational.one, Rational.fromInt(4), Rational.fromInt(9))}');
}quadrea of the 3-4-5 triangle: 576 16 · 6² = 576 collinear points give 0: 0
T graphThreePointCurvature<T>(Polynumber<T> f, T x1, T x2, T x3)The three-point curvature of the graph y = f(x) at x_{1}, x_{2}, x_{3} with the (x_{i} - x_{j})^{2} cancelled: K(x_{1}, x_{2}, x_{3}) = \frac{4 \cdot f[x_{1}, x_{2}, x_{3}]^{2}}{\prod (1 + f[x_{i}, x_{j}]^{2})} with the divided differences written as polynomials (complete symmetric sums), so the points may coincide. For f = x^{2} it is \frac{4}{(1 + (t_{2} + t_{3})^{2})(1 + (t_{1} + t_{3})^{2})(1 + (t_{1} + t_{2})^{2})}; at x_{1} = x_{2} = x_{3} it is K.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The three-point curvature of a graph y = f(x) with the (xᵢ − xⱼ)² factors cancelled, so the points may coincide.
final f = Polynumber.ints(rationals, [0, -1, 0, 1]); // y = x³ − x
final x1 = Rational.fromInt(-1), x2 = Rational.fromInt(1, 2), x3 = Rational.fromInt(2);
final k = graphThreePointCurvature(f, x1, x2, x3);
print('f = $f');
print('f[x1,x2,x3] = ${dividedDifference(f, [x1, x2, x3])}');
print('f[x2,x3] = ${dividedDifference(f, [x2, x3])}, f[x1,x3] = ${dividedDifference(f, [x1, x3])}, f[x1,x2] = ${dividedDifference(f, [x1, x2])}');
print('K(−1, 1/2, 2) = $k');
Pt<Rational> on(Rational x) => (x, f.evaluate(x));
print('same as threePointCurvature: ${k == threePointCurvature(rationals, on(x1), on(x2), on(x3))}');
final one = Rational.one;
print('all three at x = 1: K = ${graphThreePointCurvature(f, one, one, one)} = graphCurvature ${graphCurvature(f, one)}');
// Slide the three points x − 1/2, x, x + 1/2 along the curve and watch K; the coincident limit is graphCurvature.
final h = Rational.fromInt(1, 2);
final slide = <List<double>>[], limit = <List<double>>[];
for (var i = -16; i <= 16; i++) {
final x = Rational.fromInt(i, 8);
slide.add([x.toDouble(), graphThreePointCurvature(f, x - h, x, x + h).toDouble()]);
limit.add([x.toDouble(), graphCurvature(f, x).toDouble()]);
}
{'label': 'y = x³ − x', 'points': [for (var i = 0; i <= 40; i++) [-1.5 + 3.8 * i / 40, _f(-1.5 + 3.8 * i / 40)]]},
{'label': 'triangle', 'points': [for (final x in [-1.0, 0.5, 2.0, -1.0]) [x, _f(x)]]},
{'label': 'points', 'style': 'points', 'points': [for (final x in [-1.0, 0.5, 2.0]) [x, _f(x)]]},
]})}');
{'label': 'K(x − 1/2, x, x + 1/2)', 'points': slide},
{'label': 'K(x, x, x) = graphCurvature', 'points': limit},
]})}');
}
double _f(double x) => x * x * x - x;f = −α + α³ f[x1,x2,x3] = 3/2 f[x2,x3] = 17/4, f[x1,x3] = 2, f[x1,x2] = -1/4 K(−1, 1/2, 2) = 2304/25925 same as threePointCurvature: true all three at x = 1: K = 36/125 = graphCurvature 36/125
T dividedDifference<T>(Polynumber<T> f, List<T> xs)The divided difference f[x_{0}, \ldots, x_{k}] as \sum a_{n} \cdot h_{n - k}(x_{0}, \ldots, x_{k}) (complete homogeneous symmetric sums): a polynomial in the points, defined when they coincide (where it is the k-th subderivative).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final f = Polynumber.ints(rationals, [0, 0, 0, 1]); // α³
final r = [for (final x in [1, 2, 4]) Rational.fromInt(x)];
print('f[1, 2, 4] = ${dividedDifference(f, r)}');
// At coincident points it is a subderivative: f[2, 2, 2] = D2 f at 2.
final two = Rational.fromInt(2);
print('f[2, 2, 2] = ${dividedDifference(f, [two, two, two])}, D2 f(2) = ${f.subderivativeAt(2, two)}');
}f[1, 2, 4] = 7 f[2, 2, 2] = 6, D2 f(2) = 6
T completeSymmetric<T>(ScalarField<T> f, List<T> xs, int d)h_{d}(x_{0}, \ldots, x_{k}), the sum of all monomials of degree d.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final xs = [Rational.fromInt(1), Rational.fromInt(2)];
// h_2(x, y) = x² + xy + y² = 1 + 2 + 4.
print('h_2(1, 2) = ${completeSymmetric(rationals, xs, 2)}');
print('h_3(1, 2) = ${completeSymmetric(rationals, xs, 3)}');
}h_2(1, 2) = 7 h_3(1, 2) = 15
Pt<T>? graphCentre<T>(Polynumber<T> f, T x)The evolute point of the graph y = f(x) at x (the centre of curvature), or null where the bend is zero.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final parabola = Polynumber.ints(rationals, [0, 0, 1]);
final pts = <List<Object>>[];
for (final x in [0, 1, -1]) {
final c = graphCentre(parabola, Rational.fromInt(x))!;
print('centre of curvature at x = $x: (${c.$1}, ${c.$2})');
pts.add([c.$1.toDouble(), c.$2.toDouble(), 'C$x']);
}
'points': pts,
'polygons': [],
'segments': [for (var i = -15; i < 15; i++) [i / 10, i * i / 100, (i + 1) / 10, (i + 1) * (i + 1) / 100]]})}');
}centre of curvature at x = 0: (0, 1/2) centre of curvature at x = 1: (-4, 7/2) centre of curvature at x = -1: (4, 7/2)
Dual<T> evaluateDual<T>(Polynumber<T> p, Dual<T> x)P(a + b\varepsilon) by Horner with dual arithmetic: the value and the derivative in one pass.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
final r = evaluateDual(p, Dual.at(rationals, Rational.fromInt(2)));
print('p(2 + ε) = $r');
print('value p(2) = ${r.a}, derivative p\'(2) = ${r.b}');
}p(2 + ε) = 5 + 10ε value p(2) = 5, derivative p'(2) = 10
List<List<T>> evaluateDualMatrix<T>(Polynumber<T> p, T a)P evaluated on the matrix a \cdot I + \varepsilon (Horner with 2 \times 2 matrices): the "single matrix multiplication" that returns \begin{bmatrix} P(a) & 0 \\ DP(a) & P(a) \end{bmatrix}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
final m = evaluateDualMatrix(p, Rational.fromInt(2));
print('P(2·I + ε) = $m');
print('[[P(2), 0], [P\'(2), P(2)]]');
}P(2·I + ε) = [[5, 0], [10, 5]] [[P(2), 0], [P'(2), P(2)]]
List<List<T>> translationMatrix<T>(ScalarField<T> field, T c, int n)The matrix of the translation L_{c} on polynumbers of degree \le n: column j holds the coefficients of (c + \alpha)^{j}. For c = 1 it is the Pascal array; L_{c} L_{d} = L_{c + d}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The matrix taking coefficients of p(α) to those of p(c + α), for degree ≤ 4 and c = 1: Pascal's triangle.
final t = translationMatrix(rationals, Rational.one, 4);
for (final row in t) {
print(row.join(' '));
}
}1 1 1 1 1 0 1 2 3 4 0 0 1 3 6 0 0 0 1 4 0 0 0 0 1
BigInt binomial(int n, int k)The binomial coefficient C(n, k) (0 outside 0 \le k \le n), exactly.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
for (var n = 0; n <= 6; n++) {
print('row $n: ${[for (var k = 0; k <= n; k++) binomial(n, k)].join(' ')}');
}
print('C(60, 30) = ${binomial(60, 30)}');
}row 0: 1 row 1: 1 1 row 2: 1 2 1 row 3: 1 3 3 1 row 4: 1 4 6 4 1 row 5: 1 5 10 10 5 1 row 6: 1 6 15 20 15 6 1 C(60, 30) = 118264581564861424
String superscript(int n)n as superscript digits.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
print('α${superscript(2)} + α${superscript(10)} + α${superscript(123)}');
}α² + α¹⁰ + α¹²³
Rational cornerTurn(int n)A regular n-gon's corner, in turns: \frac{n - 2}{2n}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// A regular n-gon's corner in turns: (n − 2)/(2n).
for (final n in [3, 4, 5, 6]) {
print('$n-gon corner: ${cornerTurn(n)} of a turn');
}
}3-gon corner: 1/6 of a turn 4-gon corner: 1/4 of a turn 5-gon corner: 3/10 of a turn 6-gon corner: 1/3 of a turn
Rational vertexCurvature(List<int> sides)The curvature of a vertex surrounded by regular polygons with sides (in turns).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Three squares meet at a cube's corner: the leftover is the curvature, in turns.
print('cube corner [4,4,4]: ${vertexCurvature([4, 4, 4])}');
print('icosahedron corner [3×5]: ${vertexCurvature([3, 3, 3, 3, 3])}');
print('flat [6,6,6]: ${vertexCurvature([6, 6, 6])}');
}cube corner [4,4,4]: 1/4 icosahedron corner [3×5]: 1/6 flat [6,6,6]: 0
FaceMesh voxelSurface(Set<(int, int, int)> cubes)The surface of a set of unit cubes (voxels): every cube face not shared by two cubes, as a square-faced mesh. voxelRing is a cube with a square hole drilled through: genus 1, total curvature 0.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final cube = voxelSurface({(0, 0, 0)});
print('one cube: ${cube.faces.length} faces, ${cube.edgeCount} edges, Euler ${cube.euler}, total curvature ${cube.total}');
final ring = voxelSurface(voxelRing);
print('ring: ${ring.faces.length} faces, Euler ${ring.euler}, total curvature ${ring.total}');
}one cube: 6 faces, 12 edges, Euler 2, total curvature 2 ring: 32 faces, Euler 0, total curvature 0
T edgeArea<T>(ScalarField<T> f, Pt<T> a, Pt<T> b)The signed area of the oriented edge from a to b: \tfrac{1}{2}(a_{1}b_{2} - a_{2}b_{1}).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
(Rational, Rational) pt(int x, int y) => (Rational.fromInt(x), Rational.fromInt(y));
print('edge (1,0) -> (0,1): ${edgeArea(rationals, pt(1, 0), pt(0, 1))}');
print('edge (0,1) -> (1,0): ${edgeArea(rationals, pt(0, 1), pt(1, 0))}');
}edge (1,0) -> (0,1): 1/2 edge (0,1) -> (1,0): -1/2
T splineArea<T>(ScalarField<T> f, List<Pt<T>> points)The signed area of the spline [A_{0}, A_{1}, \ldots, A_{n}]: the sum over its edges (@09:48). Closed when A_{0} = A_{n}, and then translation invariant.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
(Rational, Rational) pt(int x, int y) => (Rational.fromInt(x), Rational.fromInt(y));
final open = [pt(1, 0), pt(1, 1), pt(0, 1)];
print('open spline: ${splineArea(rationals, open)}');
print('closed spline: ${splineArea(rationals, [...open, pt(0, 0), pt(1, 0)])}');
}open spline: 1 closed spline: 1
T polygonArea<T>(ScalarField<T> f, List<Pt<T>> points)The signed area of the closed polygon A_{0} \cdots A_{n-1} (the spline back to A_{0}).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
(Rational, Rational) pt(int x, int y) => (Rational.fromInt(x), Rational.fromInt(y));
final square = [pt(0, 0), pt(2, 0), pt(2, 2), pt(0, 2)];
print('counter-clockwise square: ${polygonArea(rationals, square)}');
print('clockwise square: ${polygonArea(rationals, square.reversed.toList())}');
final l = [pt(0, 0), pt(3, 0), pt(3, 1), pt(1, 1), pt(1, 3), pt(0, 3)];
print('L shape: ${polygonArea(rationals, l)}');
'polygons': [[for (final p in l) [p.$1.toDouble(), p.$2.toDouble()]]]})}');
}counter-clockwise square: 4 clockwise square: -4 L shape: 5
V3 v3(Rational x, Rational y, Rational z)A rational 3-vector.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A rational 3-vector from three Rationals.
final a = v3(Rational.fromInt(1, 2), Rational.fromInt(-2, 3), Rational.one);
print('a = $a');
print('a·a = ${a.dot(a)}');
print('a × (0,0,1) = ${a.cross(v3i(0, 0, 1))}');
}a = [1/2, -2/3, 1] a·a = 61/36 a × (0,0,1) = [-2/3, -1/2, 0]
V3 v3i(int x, int y, int z)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The same from integers: handy for points like the octant corners.
final x = v3i(1, 0, 0), y = v3i(0, 1, 0);
print('x = $x, y = $y');
print('x × y = ${x.cross(y)}');
print('elliptic quadrance of x and (1,1,0): ${ellipticQuadrance(x, v3i(1, 1, 0))}');
}x = [1, 0, 0], y = [0, 1, 0] x × y = [0, 0, 1] elliptic quadrance of x and (1,1,0): 1/2
V3 unitPoint(Rational u, Rational v)The rational unit vector by inverse stereographic projection of (u, v): \frac{(2u, 2v, u^{2} + v^{2} - 1)}{u^{2} + v^{2} + 1}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Rational points on the unit sphere from inverse stereographic projection.
final p = unitPoint(Rational.fromInt(1, 2), Rational.fromInt(1, 3));
print('point: $p');
print('x² + y² + z² = ${p.dot(p)}');
}point: [36/49, 24/49, -23/49] x² + y² + z² = 1
Rational ellipticQuadrance(V3 a, V3 b)The elliptic quadrance of the points a, b: 1 - (a \cdot b)^{2} / (Q(a) Q(b)), the spread between their lines through the origin.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final x = v3i(1, 0, 0), y = v3i(0, 1, 0), d = v3i(1, 1, 0);
print('between x and y axes: ${ellipticQuadrance(x, y)}');
print('between x and (1,1,0): ${ellipticQuadrance(x, d)}');
}between x and y axes: 1 between x and (1,1,0): 1/2
Rational vertexS(V3 a, V3 b, V3 c)The spread at vertex a of the triangle a, b, c: the spread of the planes a \times b and a \times c.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The spread at a vertex of a spherical triangle: the spread between the two great-circle planes.
final a = v3i(0, 0, 1), b = v3i(1, 0, 0), c = v3i(0, 1, 0);
print('octant triangle, spread at the pole: ${vertexS(a, b, c)}');
// A thinner triangle: b and (1,1,0) are an eighth of a turn apart as seen from the pole.
print('pole, (1,0,0), (1,1,0): ${vertexS(a, b, v3i(1, 1, 0))}');
print('pole, (3,0,0), (3,4,0): ${vertexS(a, v3i(3, 0, 0), v3i(3, 4, 0))}');
}octant triangle, spread at the pole: 1 pole, (1,0,0), (1,1,0): 1/2 pole, (3,0,0), (3,4,0): 16/25
RtQuaternion<Rational> halfway(V3 x, V3 y)The unnormalised half-way rotation carrying the unit vector x to the unit vector y along their great circle: (1 + x \cdot y, x \times y). Parallel transport along the arc.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The rotation (1 + x·y, x × y) carries the unit vector x to y along their great circle.
final x = v3i(1, 0, 0), y = v3i(0, 1, 0);
final q = halfway(x, y);
print('q = $q');
print('q rotates x to ${q.rotate(x)}');
// Rational unit vectors: (3/5, 4/5, 0) to (0, 0, 1).
final u = v3(Rational.fromInt(3, 5), Rational.fromInt(4, 5), Rational.zero);
print('u → pole: ${halfway(u, v3i(0, 0, 1)).rotate(u)}');
}q = (1, 0, 0, 1) q rotates x to [0, 1, 0] u → pole: [0, 0, 1]
RtQuaternion<Rational> partway(V3 x, V3 y, Rational s)The rotation about the same axis as halfway but only part of the way: (1 + x \cdot y, s \cdot (x \times y)) for 0 \le s \le 1. Exact for rational s; at s = 1 it is the whole arc. For animating a transport with rational frames.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Part of the arc from x to y: s = 0 stays put, s = 1 is the whole way; every frame is exact.
final x = v3i(1, 0, 0), y = v3i(0, 1, 0);
for (final s in [Rational.zero, Rational.fromInt(1, 2), Rational.one]) {
final p = partway(x, y, s).rotate(x);
print('s = $s: x goes to $p');
}
}s = 0: x goes to [1, 0, 0] s = 1/2: x goes to [3/5, 4/5, 0] s = 1: x goes to [0, 1, 0]
RtQuaternion<Rational> holonomy(List<V3> loop)The holonomy of a closed polygon of rational unit vectors p_{0} \to p_{1} \to \cdots \to p_{0}: the product of the arcs' half-way rotations, last arc leftmost.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Transport a tangent arrow around the octant triangle: it comes back turned by a quarter turn.
final loop = [v3i(1, 0, 0), v3i(0, 1, 0), v3i(0, 0, 1)];
final h = holonomy(loop);
print('holonomy quaternion: $h');
print('half-excess spread = ${halfExcessS(h)}, excess spread = ${excessS(h)}');
print('arrow (0,1,0) comes back as ${transportAround(loop, v3i(0, 1, 0))}');
}holonomy quaternion: (2, 2, 0, 0) half-excess spread = 1/2, excess spread = 1 arrow (0,1,0) comes back as [0, 0, 1]
Rational halfExcessS(RtQuaternion<Rational> h)\sin^{2}\left(\frac{E}{2}\right) = \frac{|\operatorname{vec} h|^{2}}{|h|^{2}} for a holonomy quaternion h.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The half-excess spread, read off a holonomy quaternion.
final octant = holonomy([v3i(1, 0, 0), v3i(0, 1, 0), v3i(0, 0, 1)]);
print('octant (E = quarter turn): half-excess spread = ${halfExcessS(octant)}');
// A loop that encloses nothing (there and back) has no excess.
print('there and back: ${halfExcessS(holonomy([v3i(1, 0, 0), v3i(0, 1, 0)]))}');
}octant (E = quarter turn): half-excess spread = 1/2 there and back: 0
Rational excessS(RtQuaternion<Rational> h)\frac{4w^{2}|v|^{2}}{|h|^{4}}: the spread of the transport's turn.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The spread of the turn a tangent arrow makes going round the loop.
final h = holonomy([v3i(1, 0, 0), v3i(0, 1, 0), v3i(0, 0, 1)]);
print('octant triangle: excess spread = ${excessS(h)} (E is a quarter turn)');
final u = v3(Rational.fromInt(3, 5), Rational.fromInt(4, 5), Rational.zero);
final h2 = holonomy([v3i(1, 0, 0), u, v3i(0, 0, 1)]);
print('thinner triangle: half-excess spread = ${halfExcessS(h2)}, excess spread = ${excessS(h2)}');
}octant triangle: excess spread = 1 (E is a quarter turn) thinner triangle: half-excess spread = 1/5, excess spread = 16/25
V3 transportAround(List<V3> loop, V3 t)Transports the tangent vector t at loop[0] around the loop; returns the arrow that comes back.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Carry a tangent arrow at the first vertex around the loop; it returns turned by the angle excess.
final loop = [v3i(0, 0, 1), v3i(1, 0, 0), v3i(0, 1, 0)];
final t = v3i(1, 0, 0); // tangent to the sphere at the pole
final back = transportAround(loop, t);
print('arrow out: $t');
print('arrow back: $back');
print('spread of the turn: ${sineSquared(t, back)}');
}arrow out: [1, 0, 0] arrow back: [0, 1, 0] spread of the turn: 1
Rational sineSquared(V3 a, V3 b)The spread between two vectors: 1 - \operatorname{cross} = Q(u \times v) / (Q(u) Q(v)).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The spread between two vectors, exactly: 1 − (a·b)²/(a·a · b·b).
print('(1,0,0), (0,1,0): ${sineSquared(v3i(1, 0, 0), v3i(0, 1, 0))}');
print('(1,0,0), (1,1,0): ${sineSquared(v3i(1, 0, 0), v3i(1, 1, 0))}');
print('(1,0,0), (1,2,2): ${sineSquared(v3i(1, 0, 0), v3i(1, 2, 2))}');
print('parallel: ${sineSquared(v3i(1, 2, 3), v3i(-2, -4, -6))}');
}(1,0,0), (0,1,0): 1 (1,0,0), (1,1,0): 1/2 (1,0,0), (1,2,2): 8/9 parallel: 0
Bipolynumber<Rational> normalParaboloid(Rational a, Rational b, Rational c)z = \tfrac{1}{2}(ax^{2} + 2bxy + cy^{2}).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final bowl = SurfacePoint(normalParaboloid(Rational.one, Rational.zero, Rational.one), Rational.zero, Rational.zero);
final saddle = SurfacePoint(normalParaboloid(Rational.one, Rational.zero, -Rational.one), Rational.zero, Rational.zero);
print('z = ½(x² + y²): Gaussian ${bowl.gaussian}, ${bowl.kind}');
print('z = ½(x² − y²): Gaussian ${saddle.gaussian}, ${saddle.kind}');
}z = ½(x² + y²): Gaussian 1, QuadricKind.elliptic z = ½(x² − y²): Gaussian -1, QuadricKind.hyperbolic
Bipolynumber<Rational> parabolicCurve(Bipolynumber<Rational> f)The parabolic curve of a graph: the polynomial f_{xx} f_{yy} - f_{xy}^{2} whose zero set separates bowls from saddles.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// z = x³ − 3xy² (the monkey saddle): f_xx f_yy − f_xy² vanishes only at the origin.
final monkey = Bipolynumber.ints(rationals, {(3, 0): 1, (1, 2): -3});
print('monkey saddle: ${parabolicCurve(monkey).format()}');
// z = x³ + y²: bowls for x > 0, saddles for x < 0, the parabolic curve is x = 0.
final f = Bipolynumber.ints(rationals, {(3, 0): 1, (0, 2): 1});
print('x³ + y²: ${parabolicCurve(f).format()}');
}monkey saddle: −36x² − 36y² x³ + y²: 12x
Constants and variables
const RationalField rationals = RationalField()The field of rational numbers, the default for everything in this package.
const uniformSolidsSet<(int, int, int)> get voxelRingThe 3 \times 3 \times 1 block of cubes with the middle one removed: a cube with a hole (his problem 19).
Typedefs
typedef V3 = Vec<Rational>Classes
Bipolynumber
final class Bipolynumber<T>
\sum a_{ij} \alpha^{i}\beta^{j} over field, stored sparsely by (i, j).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A polynomial in two variables, by its coefficients {(i, j): c} for c·αⁱβʲ.
final circle = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 2): 1, (0, 0): -25});
print('circle: ${circle.format()}');
print('degree ${circle.degree}');
print('at (3, 4): ${circle.evaluate(Rational.fromInt(3), Rational.fromInt(4))}');
print('∂/∂α: ${circle.dAlpha.format()}');
}circle: −25 + x² + y² degree 2 at (3, 4): 0 ∂/∂α: 2x
Constructors
factory Bipolynumber(ScalarField<T> field, Map<(int, int), T> coefficients)From a map (i, j) \to a_{ij} (zeros dropped).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Coefficients keyed by (i, j) for αⁱβʲ; zeros are dropped.
final p = Bipolynumber(rationals, {(0, 0): Rational.fromInt(1, 2), (1, 1): Rational.fromInt(-3), (2, 0): Rational.zero});
print('p = ${p.format()}');
print('stored terms: ${p.coefficients.length}');
}p = 1/2 − 3xy stored terms: 2
factory Bipolynumber.ints(ScalarField<T> field, Map<(int, int), int> coefficients)From integer coefficients.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The folium of Descartes x³ + y³ − 3xy from integer coefficients.
final folium = Bipolynumber.ints(rationals, {(3, 0): 1, (0, 3): 1, (1, 1): -3});
print('folium: ${folium.format()}');
final h = Rational.fromInt(3, 2);
print('(3/2, 3/2) on it: ${folium.evaluate(h, h)}');
}folium: −3xy + x³ + y³ (3/2, 3/2) on it: 0
factory Bipolynumber.zero(ScalarField<T> field)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final z = Bipolynumber.zero(rationals);
print('zero: ${z.format()}, degree ${z.degree}, isZero ${z.isZero}');
final a = Bipolynumber.alpha(rationals);
print('α − α == zero: ${a - a == z}');
}zero: 0, degree -1, isZero true α − α == zero: true
factory Bipolynumber.alpha(ScalarField<T> field)\alpha (the x direction).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// α and β are the building blocks: write curves as ordinary expressions.
final a = Bipolynumber.alpha(rationals), b = Bipolynumber.beta(rationals);
final one = Bipolynumber.constant(rationals, Rational.one);
final circle = a * a + b * b - one;
print('α² + β² − 1 = ${circle.format()}');
}α² + β² − 1 = −1 + x² + y²
factory Bipolynumber.beta(ScalarField<T> field)\beta (the y direction).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Bipolynumber.alpha(rationals), b = Bipolynumber.beta(rationals);
// The cuspidal cubic β² − α³.
final cusp = b.pow(2) - a.pow(3);
print('cusp: ${cusp.format()}');
print('(4, 8) on it: ${cusp.evaluate(Rational.fromInt(4), Rational.fromInt(8))}');
}cusp: y² − x³ (4, 8) on it: 0
factory Bipolynumber.constant(ScalarField<T> field, T c)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final c = Bipolynumber.constant(rationals, Rational.fromInt(7, 3));
print('constant: ${c.format()}, degree ${c.degree}');
print('value anywhere: ${c.evaluate(Rational.fromInt(100), Rational.fromInt(-5))}');
}constant: 7/3, degree 0 value anywhere: 7/3
factory Bipolynumber.graph(Polynumber<T> p)The graph curve \beta - p(\alpha) of a polynumber p.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The graph y = p(x) as the curve β − p(α) = 0.
final p = Polynumber.ints(rationals, [1, 0, -2, 1]);
final g = Bipolynumber.graph(p);
print('p = $p');
print('graph curve: ${g.format()}');
print('(2, p(2)) on it: ${g.evaluate(Rational.fromInt(2), p.evaluate(Rational.fromInt(2)))}');
}p = 1 − 2α² + α³ graph curve: −1 + y + 2x² − x³ (2, p(2)) on it: 0
Properties
final ScalarField<T> fieldbool get isZeroBipolynumber<T> get dAlphaThe partial subderivative \partial in \alpha (D on the first index).
int get hashCodeMethods
T coefficient(int i, int j)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(2, 1): 5, (0, 3): -1});
print('p = ${p.format()}');
print('coefficient of α²β: ${p.coefficient(2, 1)}');
print('coefficient of αβ (absent): ${p.coefficient(1, 1)}');
}p = 5x²y − y³ coefficient of α²β: 5 coefficient of αβ (absent): 0
Bipolynumber<T> scale(T c)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(2, 0): 2, (0, 2): 4, (0, 0): -6});
print('p = ${p.format()}');
print('p scaled by 1/2: ${p.scale(Rational.fromInt(1, 2)).format()}');
}p = −6 + 2x² + 4y² p scaled by 1/2: −3 + x² + 2y²
Bipolynumber<T> pow(int n)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// (α + β)⁴: the binomial coefficients appear exactly.
final s = Bipolynumber.alpha(rationals) + Bipolynumber.beta(rationals);
print('(α + β)⁴ = ${s.pow(4).format()}');
print('(α + β)⁰ = ${s.pow(0).format()}');
}(α + β)⁴ = x⁴ + 4x³y + 6x²y² + 4xy³ + y⁴ (α + β)⁰ = 1
T evaluate(T x, T y)The value at (x, y).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The circle x² + y² = 5/100 passes through (1/10, 2/10): exactly 0 in rationals.
final circle = Bipolynumber(rationals, {(2, 0): Rational.one, (0, 2): Rational.one, (0, 0): Rational.fromInt(-5, 100)});
print('at (1/10, 2/10): ${circle.evaluate(Rational.fromInt(1, 10), Rational.fromInt(2, 10))}');
print('same in doubles: ${0.1 * 0.1 + 0.2 * 0.2 - 0.05}');
}at (1/10, 2/10): 0 same in doubles: 6.938893903907228e-18
Bipolynumber<T> translate(T r, T s)The translation P(r + \alpha, s + \beta): the point (r, s) moved to the origin. Exact (binomial expansion).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Move the point (3, 4) of the circle x² + y² = 25 to the origin.
final circle = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 2): 1, (0, 0): -25});
final t = circle.translate(Rational.fromInt(3), Rational.fromInt(4));
print('P(3 + α, 4 + β) = ${t.format()}');
print('no constant term, so (3, 4) is on the curve');
}P(3 + α, 4 + β) = 6x + 8y + x² + y² no constant term, so (3, 4) is on the curve
Bipolynumber<T> truncate(int k)Keeps the terms of total degree \le k.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(0, 0): 1, (1, 0): 2, (1, 1): 3, (3, 0): 4, (2, 2): 5});
print('p = ${p.format()}');
for (final k in [0, 1, 2, 3]) print('truncate($k) = ${p.truncate(k).format()}');
}p = 1 + 2x + 3xy + 4x³ + 5x²y² truncate(0) = 1 truncate(1) = 1 + 2x truncate(2) = 1 + 2x + 3xy truncate(3) = 1 + 2x + 3xy + 4x³
Bipolynumber<T> homogeneous(int k)The homogeneous part of total degree k.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(0, 0): 1, (1, 0): 2, (0, 1): -1, (2, 0): 3, (1, 1): 4, (3, 0): 5});
print('p = ${p.format()}');
for (var k = 0; k <= 3; k++) print('degree $k part: ${p.homogeneous(k).format()}');
}p = 1 + 2x − y + 3x² + 4xy + 5x³ degree 0 part: 1 degree 1 part: 2x − y degree 2 part: 3x² + 4xy degree 3 part: 5x³
Bipolynumber<T> tangentAt(T r, T s, int k)The k-th tangent of the curve P = 0 at (r, s), in coordinates centred on the point: P(r + \alpha, s + \beta) truncated at total degree k. k = 1 the tangent line, k = 2 the tangent conic.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The folium x³ + y³ − 3xy at (3/2, 3/2), in coordinates centred on that point.
final folium = Bipolynumber.ints(rationals, {(3, 0): 1, (0, 3): 1, (1, 1): -3});
final h = Rational.fromInt(3, 2);
print('tangent line: ${folium.tangentAt(h, h, 1).format()}');
print('tangent conic: ${folium.tangentAt(h, h, 2).format()}');
// At the node (0, 0) the linear part vanishes: the lowest terms −3xy give two tangent lines.
print('at the node: ${folium.tangentAt(Rational.zero, Rational.zero, 2).format()}');
}tangent line: (9/4)x + (9/4)y tangent conic: (9/4)x + (9/4)y + (9/2)x² − 3xy + (9/2)y² at the node: −3xy
Bipolynumber<T> tangentInPlace(T r, T s, int k)The same tangent written back in the original coordinates (\alpha - r, \beta - s substituted).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The tangent line to x² + y² = 25 at (3, 4), back in x, y: 6x + 8y − 50, i.e. 3x + 4y = 25.
final circle = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 2): 1, (0, 0): -25});
final line = circle.tangentInPlace(Rational.fromInt(3), Rational.fromInt(4), 1);
print('tangent line: ${line.format()}');
print('(3, 4) on it: ${line.evaluate(Rational.fromInt(3), Rational.fromInt(4))}');
print('(-1, 7) on it: ${line.evaluate(Rational.fromInt(-1), Rational.fromInt(7))}');
}tangent line: −50 + 6x + 8y (3, 4) on it: 0 (-1, 7) on it: 0
String format([String x = 'x', String y = 'y'])import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(2, 0): 1, (1, 1): -1, (0, 2): 3, (0, 0): -2});
print('default: ${p.format()}');
print('α, β: ${p.format('α', 'β')}');
print('u, v: ${p.format('u', 'v')}');
}default: −2 + x² − xy + 3y² α, β: −2 + α² − αβ + 3β² u, v: −2 + u² − uv + 3v²
Operators
Bipolynumber<T> operator +(Bipolynumber<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 0): 1});
final q = Bipolynumber.ints(rationals, {(0, 2): 1, (0, 0): -1});
print('(${p.format()}) + (${q.format()}) = ${(p + q).format()}');
}(1 + x²) + (−1 + y²) = x² + y²
Bipolynumber<T> operator -(Bipolynumber<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Bipolynumber.ints(rationals, {(2, 0): 1, (1, 1): 2, (0, 2): 1});
final q = Bipolynumber.ints(rationals, {(2, 0): 1, (0, 2): 1});
print('(${p.format()}) − (${q.format()}) = ${(p - q).format()}');
print('p − p = ${(p - p).format()}');
}(x² + 2xy + y²) − (x² + y²) = 2xy p − p = 0
Bipolynumber<T> operator *(Bipolynumber<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// (α − β)(α + β) = α² − β²: the product of two lines is a degenerate conic.
final a = Bipolynumber.alpha(rationals), b = Bipolynumber.beta(rationals);
print('(α − β)(α + β) = ${((a - b) * (a + b)).format()}');
print('(α + 1)(β − 2) = ${((a + Bipolynumber.ints(rationals, {(0, 0): 1})) * (b - Bipolynumber.ints(rationals, {(0, 0): 2}))).format()}');
}(α − β)(α + β) = x² − y² (α + 1)(β − 2) = −2 − 2x + y + xy
bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Bipolynumber.alpha(rationals), b = Bipolynumber.beta(rationals);
// Equality compares coefficients exactly, so algebraic identities check out.
print('(α + β)² == α² + 2αβ + β²: ${(a + b).pow(2) == a * a + (a * b).scale(Rational.fromInt(2)) + b * b}');
print('α == β: ${a == b}');
}(α + β)² == α² + 2αβ + β²: true α == β: false
Box
final class Box implements Comparable<Box>
A box (black, anti = false) or anti-box (red) holding a multiset of boxes.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Arithmetic from nothing but boxes: 0 = [ ], a natural number is a box of zeros.
final two = Box.nat(2), three = Box.nat(3);
print('2 + 3 = ${two + three}');
print('2 × 3 = ${two * three}');
print('[2,3] × [1,1,0] = ${Box.natList([2, 3]) * Box.natList([1, 1, 0])}');
print('size of [2,3]: ${Box.natList([2, 3]).size}');
}2 + 3 = 5 2 × 3 = 6 [2,3] × [1,1,0] = [2,3,3,3,4,4] size of [2,3]: 2
Constructors
factory Box(Iterable<Box> elements, {bool anti = false})A box of elements (pairs of a box and its anti annihilate).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A box of boxes; a black box and an anti (red) box of the same contents annihilate.
final zero = Box.zero(), anti = Box.zero(anti: true);
print('[0, 0] = ${Box([zero, zero])}');
print('[0, anti 0] = ${Box([zero, anti])} (they cancel)');
print('[[0], 0] = ${Box([Box([zero]), zero])}');
}[0, 0] = 2 [0, anti 0] = 0 (they cancel) [[0], 0] = [0,1]
factory Box.zero({bool anti = false})The empty box 0 (or anti-zero).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final z = Box.zero(), a = Box.zero(anti: true);
print('zero box: $z, anti zero box: $a');
print('zero + anything: ${z + Box.nat(3)}');
print('equal: ${z == a}');
}zero box: 0, anti zero box: ⟦⟧ zero + anything: 3 equal: false
factory Box.nat(int n, {bool anti = false})The natural number n: a box of n zeros.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// n is a box holding n empty boxes.
for (final n in [0, 1, 3]) print('nat($n) = ${Box.nat(n)}');
print('anti 2 = ${Box.nat(2, anti: true)}');
print('2 + anti 2 = ${Box.nat(2) + Box.nat(2, anti: true)}');
}nat(0) = 0 nat(1) = 1 nat(3) = 3 anti 2 = ⟦0,0⟧ 2 + anti 2 = ⟦0,0,0,0⟧
factory Box.natList(List<int> ns, {bool anti = false})The mset of naturals [n_{1}, n_{2}, \ldots] (a polynumber written as its exponents: [2,3] is \alpha^{2} + \alpha^{3}).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A box of natural numbers: the multiset [1, 1, 2] plays the polynomial 2x + x².
final p = Box.natList([1, 1, 2]);
print('[1,1,2] = $p, as numbers ${p.asNatList}');
final q = Box.natList([0, 1]);
print('[1,1,2] × [0,1] = ${(p * q).asNatList}');
}[1,1,2] = [1,1,2], as numbers [1, 1, 2] [1,1,2] × [0,1] = [1, 1, 2, 2, 2, 3]
factory Box.integer(int k)The integer k: a box of |k| zeros, anti-zeros for negative k.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Negative integers hold anti-zeros; a zero and an anti-zero annihilate.
final a = Box.integer(5), b = Box.integer(-3);
final sum = Box([...a.elements, ...b.elements]);
print('5 and -3 pooled: ${sum.asInteger}');
print('-3 as a box: ${Box.integer(-3)}');
}5 and -3 pooled: 2 -3 as a box: [⟦⟧,⟦⟧,⟦⟧]
Properties
final bool antiint get sizeint get asIntegerAs an integer: the count of black zeros minus red zeros (for boxes of zeros).
List<int> get asNatListAs an mset of naturals, sorted descending in insertion-free canonical order (for boxes of black naturals).
int get hashCodeMethods
int compareTo(Box o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A total order on boxes, used to keep box contents sorted.
final boxes = [Box.nat(3), Box.natList([1]), Box.zero(), Box.nat(1), Box.natList([0, 2])];
boxes.sort((a, b) => a.compareTo(b));
print('sorted: ${boxes.join(' ')}');
print('nat(2) vs nat(2): ${Box.nat(2).compareTo(Box.nat(2))}');
}sorted: 0 1 [1] [0,2] 3 nat(2) vs nat(2): 0
String toString()Brackets: [ ] black, ⟦ ⟧ red; a box of black zeros prints as its count.
Operators
Box operator +(Box o)A + B: pool the contents; the type is the product of the types.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// + pours the contents of two boxes together.
print('2 + 3 = ${Box.nat(2) + Box.nat(3)}');
print('[1,2] + [2] = ${(Box.natList([1, 2]) + Box.natList([2])).asNatList}');
print('3 + (−3) = ${Box.integer(3) + Box.integer(-3)}');
}2 + 3 = 5 [1,2] + [2] = [1, 2, 2] 3 + (−3) = 0
Box operator *(Box o)A \times B = [\,a + b\ \text{for}\ a\ \text{in}\ A,\ b\ \text{in}\ B\,].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// × adds every pair of elements: on multisets of naturals it multiplies polynomials.
final p = Box.natList([0, 1]); // 1 + x
print('(1 + x)² as a box: ${(p * p).asNatList}');
print('(1 + x)³ as a box: ${(p * p * p).asNatList}');
}(1 + x)² as a box: [0, 1, 1, 2] (1 + x)³ as a box: [0, 1, 1, 1, 2, 2, 2, 3]
Box operator ^(Box o)A \wedge B (caret) = [\,a \times b\ \text{for}\ a\ \text{in}\ A,\ b\ \text{in}\ B\,].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final a = Box.natList([1, 2]), b = Box.natList([3]);
print('[1,2] ^ [3] = ${a ^ b}');
print('as naturals: ${(a ^ b).asNatList}');
}[1,2] ^ [3] = [3,6] as naturals: [3, 6]
bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
print('2 × 3 == 6: ${Box.nat(2) * Box.nat(3) == Box.nat(6)}');
print('[1,2] == [2,1]: ${Box.natList([1, 2]) == Box.natList([2, 1])}');
print('[1] == 1: ${Box.natList([1]) == Box.nat(1)}');
}2 × 3 == 6: true [1,2] == [2,1]: true [1] == 1: false
Mset
final class Mset
The mset of naturals as a multiset of ints (insertion order irrelevant): sum is union, product forms pairwise sums, caret pairwise products. The small, fast twin of Box for msets of black naturals.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Mset([2, 3]), b = Mset([1, 1, 0]);
print('A = $a, B = $b');
print('A + B (pool) = ${a + b}');
print('A × B (pairwise sums) = ${a * b}');
print('A ^ B (pairwise products) = ${a ^ b}');
}A = [2,3], B = [0,1,1] A + B (pool) = [0,1,1,2,3] A × B (pairwise sums) = [2,3,3,3,4,4] A ^ B (pairwise products) = [0,0,2,2,3,3]
Constructors
Mset(Iterable<int> elements)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A multiset of integers, kept sorted; repeats count.
final m = Mset([3, 1, 2, 1]);
print('Mset([3,1,2,1]) = $m');
print('same as [1,1,2,3]: ${m == Mset([1, 1, 2, 3])}');
}Mset([3,1,2,1]) = [1,1,2,3] same as [1,1,2,3]: true
Properties
final List<int> elementsint get hashCodeMethods
Mset upTo(int n)Keeps the elements \le n (a finite truncation of an "open box").
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final powersOf2 = Mset([for (var k = 0; k < 8; k++) 1 << k]);
print('powers of 2: $powersOf2');
print('upTo(20): ${powersOf2.upTo(20)}');
}powers of 2: [1,2,4,8,16,32,64,128] upTo(20): [1,2,4,8,16]
String toString()Operators
Mset operator +(Mset o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// + is multiset union with multiplicity.
final a = Mset([1, 2]), b = Mset([2, 3]);
print('$a + $b = ${a + b}');
}[1,2] + [2,3] = [1,2,2,3]
Mset operator *(Mset o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// An mset of naturals is a polynumber written as its exponents: [0, 1] is 1 + α.
final onePlusAlpha = Mset([0, 1]);
var p = Mset([0]);
for (var n = 1; n <= 4; n++) {
p = p * onePlusAlpha;
final counts = <int, int>{};
for (final e in p.elements) counts[e] = (counts[e] ?? 0) + 1;
print('(1 + α)^$n: exponents $p -> coefficients ${[for (var k = 0; k <= n; k++) counts[k]]}');
}
}(1 + α)^1: exponents [0,1] -> coefficients [1, 1] (1 + α)^2: exponents [0,1,1,2] -> coefficients [1, 2, 1] (1 + α)^3: exponents [0,1,1,1,2,2,2,3] -> coefficients [1, 3, 3, 1] (1 + α)^4: exponents [0,1,1,1,1,2,2,2,2,2,2,3,3,3,3,4] -> coefficients [1, 4, 6, 4, 1]
Mset operator ^(Mset o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Caret: every pairwise product. Divisors multiply: divisors(4) ^ divisors(9) = divisors(36).
final d4 = Mset([1, 2, 4]), d9 = Mset([1, 3, 9]);
print('[1,2,4] ^ [1,3,9] = ${d4 ^ d9}');
}[1,2,4] ^ [1,3,9] = [1,2,3,4,6,9,12,18,36]
bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
print('[1,2]*[0,1] == [1,2,2,3]: ${Mset([1, 2]) * Mset([0, 1]) == Mset([1, 2, 2, 3])}');
print('[1,2] == [1,2,2]: ${Mset([1, 2]) == Mset([1, 2, 2])}');
}[1,2]*[0,1] == [1,2,2,3]: true [1,2] == [1,2,2]: false
CavalieriResult
final class CavalieriResult<T>
The outcome of the five-property derivation for one N.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The result of deriving the area under x^N from five properties.
final r = cavalieri(rationals, 3);
print('n = ${r.n}, coefficient A_3 = ${r.coefficient}');
print('equations used: ${r.equations.length}');
// In characteristic 3, 1/(3 + 1) = 1/4 is fine; for N = 2 it needs 1/3 = 1/0.
final bad = cavalieri(PrimeField(3), 2);
print('GF(3), N = 2: ${bad.failure}');
}n = 3, coefficient A_3 = 1/4 equations used: 4 GF(3), N = 2: (N + 1)·A_N = 1 reads 0·A_N = 1 in characteristic 3: no integral has all five properties
Properties
final int nfinal T? coefficientA_{N} = \int_{0}^{1} x^{N}, or null when the properties are inconsistent in this field.
final List<(T, T)> equationsThe coefficient-comparison equations, k = 1 \cdots N + 1, as (left factor C(N + 1, k), right factor C(N, k - 1) \cdot A_{k - 1}).
bool get okColour
enum Colour
Which quadratic form measures quadrance: blue x^{2} + y^{2} (Euclid), red x^{2} - y^{2}, green 2xy (chromogeometry).
Values
blueredgreenCurvatureAt
final class CurvatureAt<T>
The quadratic curvature of an algebraic curve at one of its points.
Properties
final ScalarField<T> fieldfinal Pt<T> pointfinal Colour colourT get bendThe tangent conic's quadratic part read along the tangent direction: nm^{2} - 2plm + ql^{2}.
T get gradientQuadranceThe gradient's quadrance l^{2} + m^{2} (in colour, the gradient's dual form).
bool get isSingularTrue at a singular point (l = m = 0): no tangent line, no curvature.
T? get curvatureK = \frac{4(nm^{2} - 2plm + ql^{2})^{2}}{(l^{2} + m^{2})^{3}}, or null at a singular point (or a null tangent in red/green).
T? get radiusQuadranceR = \frac{1}{K}, the quadrance of the radius of curvature (null when K = 0 or undefined).
Pt<T>? get centreThe centre of curvature X - (l, m) \cdot \frac{l^{2} + m^{2}}{2 \cdot \mathrm{bend}}, or null on a line (bend 0) or at a singular point. In red/green the gradient is turned by the form's matrix first.
Pt<T>? get focusThe focus of the normal parabola: half way from the point to the centre (the centre is "twice the focus").
T? get radiusQuadranceFromFocusR = 4(f^{2} + g^{2}) from the focus offset (f, g): the same number as radiusQuadrance.
DcbCurve
final class DcbCurve<T>
A dCB curve with control points P_{0} \cdots P_{n} over field.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// A quadratic curve from three control points (de Casteljau / Bézier), exact at rational t.
final c = DcbCurve.ints(rationals, [(0, 0), (1, 2), (2, 0)]);
print('degree ${c.degree}');
print('at t = 1/2: ${c.at(Rational.fromInt(1, 2))}');
print('slice area: ${c.sliceArea}, control polygon area: ${c.controlPolygonArea}');
final pts = [for (var i = 0; i <= 20; i++) c.at(Rational.fromInt(i, 20))];
'segments': [for (var i = 0; i < 20; i++) [pts[i].$1.toDouble(), pts[i].$2.toDouble(), pts[i + 1].$1.toDouble(), pts[i + 1].$2.toDouble()]],
'points': [[0, 0, 'P0'], [1, 2, 'P1'], [2, 0, 'P2']]})}');
}degree 2 at t = 1/2: (1, 1) slice area: -4/3, control polygon area: -2
Constructors
DcbCurve(this.field, List<Pt<T>> controls)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A de Casteljau–Bézier curve from its control points (as (T, T) records).
final q = (int x, int y) => (Rational.fromInt(x), Rational.fromInt(y));
final c = DcbCurve(rationals, [q(0, 0), q(1, 2), q(3, 2), q(4, 0)]);
print(c);
print('degree ${c.controls.length - 1}, at t = 1/2: ${c.at(Rational.fromInt(1, 2))}');
}dCB[(0, 0), (1, 2), (3, 2), (4, 0)] degree 3, at t = 1/2: (2, 3/2)
factory DcbCurve.ints(ScalarField<T> field, List<(int, int)> points)Integer control points.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The parabola with control points (0,0), (1,2), (2,0).
final c = DcbCurve.ints(rationals, [(0, 0), (1, 2), (2, 0)]);
print(c);
print('Archimedes: slice area ${c.sliceArea} = 4/3 × triangle ${c.archimedesTriangleArea}');
}dCB[(0, 0), (1, 2), (2, 0)] Archimedes: slice area -4/3 = 4/3 × triangle -1
factory DcbCurve.fromPolyCurve(PolyCurve<T> g, [int? degree])The control points of the polynumber curve \gamma (degree n): b_{i} = \sum_{j \le i} \frac{C(i, j)}{C(n, j)} \cdot a_{j}, the inverse Pascal times diag(1/binomials).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The control points of the curve (t, t²) on [0, 1].
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t, t * t);
print('curve $g');
print('as dCB: ${DcbCurve.fromPolyCurve(g)}');
print('raised to degree 3: ${DcbCurve.fromPolyCurve(g, 3)}');
}curve (t, t²) as dCB: dCB[(0, 0), (1/2, 0), (1, 1)] raised to degree 3: dCB[(0, 0), (1/3, 0), (2/3, 1/3), (1, 1)]
Properties
final ScalarField<T> fieldfinal List<Pt<T>> controlsint get degreePolyCurve<T> get polyCurveThe polynumber curve \sum P_{i} \cdot C(n, i) \cdot t^{i}(1 - t)^{n-i} (the Bernstein form expanded).
T get controlPolygonAreaThe signed area of the closed control polygon P_{0} \cdots P_{n}.
T get sliceAreaThe signed area of the slice: the curve from P_{0} to P_{n}, then back along the chord.
Pt<T> get archimedeanPointThe Archimedean point: \gamma(\tfrac{1}{2}) for a parabola, \frac{P_{0} + 3P_{1} + 3P_{2} + P_{3}}{8} for a cubic, in general the binomially weighted centre, which is \gamma(\tfrac{1}{2}).
T get archimedesTriangleAreaThe signed area of the inscribed triangle P_{0}, C, P_{n}.
bool get isArchimedeanTrue when the cubic is Archimedean: \Delta_{23} = 0 for its polynumber curve (the point at infinity is singular; equivalently four equally spaced parallel lines through P_{0} \cdots P_{3}). Parabolas always are.
Methods
List<List<Pt<T>>> ladder(T t)The de Casteljau ladder at t: level 0 is the control points, level k has n + 1 - k points, the last level is the curve's point \gamma(t). Each rung is (1 - t) \cdot A + t \cdot B: {+}\ {-}\ \times only.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// de Casteljau's ladder at t = 1/2: each rung averages the one above.
final c = DcbCurve.ints(rationals, [(0, 0), (2, 4), (6, 4), (8, 0)]);
final rungs = c.ladder(Rational.fromInt(1, 2));
for (final r in rungs) print(r.map((p) => '(${p.$1}, ${p.$2})').join(' '));
for (final r in rungs) for (var i = 0; i + 1 < r.length; i++)
[r[i].$1.toDouble(), r[i].$2.toDouble(), r[i + 1].$1.toDouble(), r[i + 1].$2.toDouble()]
], 'points': [for (final r in rungs) for (final p in r) [p.$1.toDouble(), p.$2.toDouble()]]})}');
}(0, 0) (2, 4) (6, 4) (8, 0) (1, 2) (4, 4) (7, 2) (5/2, 3) (11/2, 3) (4, 3)
Pt<T> at(T t)\gamma(t) by de Casteljau.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final c = DcbCurve.ints(rationals, [(0, 0), (1, 2), (2, 0)]);
for (final t in [Rational.zero, Rational.fromInt(1, 4), Rational.fromInt(1, 2), Rational.one]) {
print('t = $t: ${c.at(t)}');
}
}t = 0: (0, 0) t = 1/4: (1/2, 3/4) t = 1/2: (1, 1) t = 1: (2, 0)
(DcbCurve<T>, DcbCurve<T>) split(T t)The two halves at t: [P_{0}, Q_{0}, R_{0}, \ldots, \gamma(t)] and [\gamma(t), \ldots, R_{1}, Q_{2}, P_{n}], each again a dCB curve on [0, 1].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final c = DcbCurve.ints(rationals, [(0, 0), (1, 2), (2, 0)]);
final (left, right) = c.split(Rational.fromInt(1, 2));
print('left controls: ${left.controls}');
print('right controls: ${right.controls}');
}left controls: [(0, 0), (1/2, 1), (1, 1)] right controls: [(1, 1), (3/2, 1), (2, 0)]
String toString()Dual
final class Dual<T>
a + b\varepsilon with \varepsilon^{2} = 0, over field.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// a + bε with ε² = 0: arithmetic that carries a derivative along.
final x = Dual(rationals, Rational.fromInt(3), Rational.one);
final y = x * x * x; // x³ at 3: value 27, derivative 27
print('x = $x');
print('x³ = $y');
}x = 3 + 1ε x³ = 27 + 27ε
Constructors
const Dual(this.field, this.a, this.b)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// a + bε: value a, derivative part b.
final x = Dual(rationals, Rational.fromInt(2), Rational.one);
final c = Dual(rationals, Rational.fromInt(5), Rational.zero); // a constant
print('x = $x, c = $c');
print('c·x + x·x = ${c * x + x * x} (value 14, slope 5 + 2·2 = 9)');
}x = 2 + 1ε, c = 5 + 0ε c·x + x·x = 14 + 9ε (value 14, slope 5 + 2·2 = 9)
factory Dual.at(ScalarField<T> field, T a)a + \varepsilon (the point a with unit infinitesimal direction).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Dual.at(a) is a + ε: the variable, seeded with derivative 1.
final x = Dual.at(rationals, Rational.fromInt(1, 3));
final f = x * x * x - x; // f(x) = x³ − x, f'(x) = 3x² − 1
print('f(1/3) + f\'(1/3)ε = $f');
}f(1/3) + f'(1/3)ε = -8/27 + -2/3ε
Properties
final ScalarField<T> fieldfinal T afinal T bbool get isInvertibleb\varepsilon alone has no inverse; a + b\varepsilon has one exactly when a \ne 0.
Dual<T> get inverseList<List<T>> get matrixThe 2 \times 2 matrix a \cdot I + b \cdot \varepsilon = \begin{bmatrix}a & 0 \\ b & a\end{bmatrix}.
int get hashCodeMethods
String toString()Operators
Dual<T> operator +(Dual<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final u = Dual(rationals, Rational.fromInt(3), Rational.fromInt(1));
final v = Dual(rationals, Rational.fromInt(1, 2), Rational.fromInt(-4));
print('($u) + ($v) = ${u + v}');
}(3 + 1ε) + (1/2 + -4ε) = 7/2 + -3ε
Dual<T> operator -(Dual<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final u = Dual(rationals, Rational.fromInt(3), Rational.fromInt(1));
final v = Dual(rationals, Rational.fromInt(1, 2), Rational.fromInt(-4));
print('($u) − ($v) = ${u - v}');
print('u − u = ${u - u}');
}(3 + 1ε) − (1/2 + -4ε) = 5/2 + 5ε u − u = 0 + 0ε
Dual<T> operator *(Dual<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// (a + bε)(c + dε) = ac + (ad + bc)ε since ε² = 0: the product rule, for free.
final eps = Dual(rationals, Rational.zero, Rational.one);
print('ε·ε = ${eps * eps}');
final u = Dual(rationals, Rational.fromInt(2), Rational.fromInt(3));
final v = Dual(rationals, Rational.fromInt(5), Rational.fromInt(7));
print('($u)($v) = ${u * v}');
}ε·ε = 0 + 0ε (2 + 3ε)(5 + 7ε) = 10 + 29ε
bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final x = Dual.at(rationals, Rational.fromInt(2));
print('x·x == 4 + 4ε: ${x * x == Dual(rationals, Rational.fromInt(4), Rational.fromInt(4))}');
print('x == 2 + 0ε: ${x == Dual(rationals, Rational.fromInt(2), Rational.zero)}');
}x·x == 4 + 4ε: true x == 2 + 0ε: false
Maxel
final class Maxel
A multiset of pixels [k, l] with integer multiplicities (negative = anti-pixels).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Maxels: multisets of pixels [k, l], multiplied like matrices.
final a = Maxel.pixels([(0, 1), (1, 2)]);
print('a = $a');
print('a * a = ${a * a}');
print('a + a = ${a + a}');
}a = {01,12}
a * a = {02}
a + a = {01,01,12,12}Constructors
factory Maxel(Map<(int, int), int> multiplicities)From pixel multiplicities (zeros dropped).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A maxel from pixel multiplicities: pixel (0,1) twice, (1,1) once.
final m = Maxel({(0, 1): 2, (1, 1): 1});
print('m = $m, size ${m.size}');
print('pixels: ${m.pixelList}');
}m = {01,01,11}, size 3
pixels: [(0, 1), (0, 1), (1, 1)]factory Maxel.pixels(Iterable<(int, int)> pixels)From a list of pixels, repeats counted: {11, 12, 12, 23} is Maxel.pixels([(1,1), (1,2), (1,2), (2,3)]).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Repeated pixels add up.
final m = Maxel.pixels([(0, 0), (0, 1), (0, 1)]);
print('m = $m');
print('multiplicity of (0,1): ${m[(0, 1)]}');
}m = {00,01,01}
multiplicity of (0,1): 2factory Maxel.identity(Iterable<int> indices)The identity on the index set indices: \{[i, i]\}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The identity on {1, 2, 3}: the diagonal pixels.
final id = Maxel.identity([1, 2, 3]);
final a = Maxel.pixels([(1, 2), (2, 3)]);
print('I = $id');
print('I·a == a: ${id * a == a}, a·I == a: ${a * id == a}');
}I = {11,22,33}
I·a == a: true, a·I == a: truefactory Maxel.zeta(List<int> elements, bool Function(int a, int b) leq)The zeta maxel of a poset: \{[a, b]: a \le b\}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The divisibility order on 1..6: zeta * mobius is the identity.
final els = [1, 2, 3, 4, 5, 6];
bool divides(int a, int b) => b % a == 0;
final z = Maxel.zeta(els, divides), m = Maxel.mobius(els, divides);
print('zeta has ${z.size} pixels');
print('mobius[1, 6] = ${m[(1, 6)]}, mobius[1, 4] = ${m[(1, 4)]}');
print('zeta * mobius == identity: ${z * m == Maxel.identity(els)}');
}zeta has 14 pixels mobius[1, 6] = 1, mobius[1, 4] = 0 zeta * mobius == identity: true
Static
static Maxel mobius(List<int> elements, bool Function(int a, int b) leq)The inverse of an upper-unitriangular maxel on a finite poset (the zeta maxel's inverse is the Möbius maxel), by the recursion \mu(a, a) = 1, \mu(a, b) = -\sum_{a \le c < b} \mu(a, c).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Divisibility on {1, 2, 3, 6}: zeta (a | b) times its Möbius maxel is the identity.
final els = [1, 2, 3, 6];
bool divides(int a, int b) => b % a == 0;
final mu = Maxel.mobius(els, divides);
print('mobius = $mu');
print('μ(1, 6) = ${mu[(1, 6)]}');
print('zeta·mobius == identity: ${Maxel.zeta(els, divides) * mu == Maxel.identity(els)}');
}mobius = {11,−12,−13,16,22,−26,33,−36,66}
μ(1, 6) = 1
zeta·mobius == identity: trueProperties
Map<(int, int), int> get multiplicitiesList<(int, int)> get pixelListThe pixels as a sorted list, repeats written out: [(1,1), (1,1), (1,4), …].
int get hashCodeMethods
Operators
int operator []((int, int) pixel)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final m = Maxel({(1, 2): 3, (2, 2): 1});
print('m[(1,2)] = ${m[(1, 2)]}');
print('m[(9,9)] = ${m[(9, 9)]} (absent pixels count 0)');
}m[(1,2)] = 3 m[(9,9)] = 0 (absent pixels count 0)
Maxel operator +(Maxel o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Maxel.pixels([(0, 1)]), b = Maxel.pixels([(0, 1), (1, 0)]);
print('$a + $b = ${a + b}');
}{01} + {01,10} = {01,01,10}Maxel operator -()import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Negation gives anti-pixels; a maxel plus its negation is empty.
final a = Maxel.pixels([(0, 1), (2, 3)]);
print('−a = ${-a}');
print('a + (−a) = ${a + (-a)}');
}−a = {−01,−23}
a + (−a) = {}Maxel operator -(Maxel o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Maxel.pixels([(0, 1), (0, 1), (1, 1)]), b = Maxel.pixels([(0, 1)]);
print('$a − $b = ${a - b}');
print('$b − $a = ${b - a}');
}{01,01,11} − {01} = {01,11}
{01} − {01,01,11} = {−01,−11}Maxel operator *(Maxel o)The product: every pixel of this times every pixel of o; blanks dropped.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// [k,l]·[l,m] = [k,m]; pixels that don't chain give nothing.
final a = Maxel.pixels([(0, 1)]), b = Maxel.pixels([(1, 2)]);
print('[01]·[12] = ${a * b}');
print('[12]·[01] = ${b * a} (empty: maxels do not commute)');
}[01]·[12] = {02}
[12]·[01] = {} (empty: maxels do not commute)bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Maxel.pixels([(0, 1), (1, 0)]);
print('a·a == identity on {0,1}: ${a * a == Maxel.identity([0, 1])}');
print('a == −a: ${a == -a}');
}a·a == identity on {0,1}: true
a == −a: falsePolynumber
final class Polynumber<T>
A polynumber a_{0} + a_{1}\alpha + \cdots + a_{n}\alpha^{n} over the field field, stored as its coefficient list (no trailing zeros).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A polynumber is its list of coefficients: p = 1 − 2α + α³ is [1, −2, 0, 1].
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
print('p = $p, degree ${p.degree}');
print('coefficients: ${p.coefficients}');
print('p(2) = ${p.evaluate(Rational.fromInt(2))}');
print('p(1/2) = ${p.evaluate(Rational.fromInt(1, 2))}');
}p = 1 − 2α + α³, degree 3 coefficients: [1, -2, 0, 1] p(2) = 5 p(1/2) = 1/8
Constructors
factory Polynumber(ScalarField<T> field, Iterable<T> coefficients)The polynumber with coefficients coefficients (a_{0} first).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Coefficients from the constant term up; trailing zeros are trimmed.
final p = Polynumber(rationals, [Rational.fromInt(1, 2), Rational.zero, Rational.fromInt(3), Rational.zero]);
print('p = $p, degree ${p.degree}');
// Over GF(5) the same constructor takes ints.
final q = Polynumber(PrimeField(5), [4, 0, 1]);
print('over GF(5): $q');
}p = 1/2 + 3α², degree 2 over GF(5): 4 + α²
factory Polynumber.ints(ScalarField<T> field, List<int> coefficients)Integer coefficients, mapped into field.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, 3]);
final q = Polynumber.ints(rationals, [5, 2]);
print('p = $p, q = $q');
print('p + q = ${p + q}');
print('p * q = ${p * q}');
print('p - q = ${p - q}');
}p = 1 + 3α, q = 5 + 2α p + q = 6 + 5α p * q = 5 + 17α + 6α² p - q = −4 + α
factory Polynumber.rationals(ScalarField<T> field, List<Rational> coefficients)Rational coefficients, mapped into field (throws when a denominator vanishes in it).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.rationals(rationals, [Rational.fromInt(-1, 4), Rational.zero, Rational.one]);
print('p = $p');
print('roots at ±1/2: ${p.evaluate(Rational.fromInt(1, 2))}, ${p.evaluate(Rational.fromInt(-1, 2))}');
}p = −1/4 + α² roots at ±1/2: 0, 0
factory Polynumber.constant(ScalarField<T> field, T c)The constant polynumber c.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final c = Polynumber.constant(rationals, Rational.fromInt(7));
print('c = $c, degree ${c.degree}, derivative ${c.derivative}');
}c = 7, degree 0, derivative 0
factory Polynumber.zero(ScalarField<T> field)The zero polynumber [ ].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final z = Polynumber.zero(rationals);
print('zero: $z, degree ${z.degree}');
final p = Polynumber.ints(rationals, [1, 2]);
print('p − p == zero: ${p - p == z}');
}zero: 0, degree -1 p − p == zero: true
factory Polynumber.alpha(ScalarField<T> field)\alpha = [0, 1].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// α, the polynumber [0, 1]: build polynomials as expressions.
final a = Polynumber.alpha(rationals);
final one = Polynumber.constant(rationals, Rational.one);
print('(α − 1)³ = ${(a - one).pow(3)}');
}(α − 1)³ = −1 + 3α − 3α² + α³
factory Polynumber.monomial(ScalarField<T> field, int n, [T? c])\alpha^{n}.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Over the field of 7 elements, α⁷ has derivative 0, yet its 7th subderivative is still 1.
final gf7 = PrimeField(7);
final a7 = Polynumber.monomial(gf7, 7);
print('α⁷ over GF(7): $a7');
print('derivative: ${a7.derivative}');
print('7th subderivative: ${a7.subderivative(7)}');
print('same function as α: ${a7.sameFunctionAs(Polynumber.alpha(gf7))}');
}α⁷ over GF(7): α⁷ derivative: 0 7th subderivative: 1 same function as α: true
Properties
final ScalarField<T> fieldbool get isZeroPolynumber<T> get derivativeThe derivative D = D_{1} (Faulhaber's D: D \alpha^{n} = n \alpha^{n-1}).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [0, 0, 0, 4]); // 4α³
print('p = $p');
print('p\' = ${p.derivative}');
print('p\'\' = ${p.derivative.derivative}');
print('D2 p = ${p.subderivative(2)} (p\'\' / 2!)');
}p = 4α³ p' = 12α² p'' = 24α D2 p = 12α (p'' / 2!)
Polynumber<T> get integralFaulhaber's integral S: S \alpha^{n} = \frac{\alpha^{n+1}}{n + 1}. Over GF(p) it throws when n + 1 \equiv 0.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
print('S p = ${p.integral}');
print('area under p from 0 to 3 = ${p.integrate(Rational.zero, Rational.fromInt(3))}');
}S p = α − α² + (1/4)α⁴ area under p from 0 to 3 = 57/4
int get hashCodeMethods
Polynumber<T> scale(T c)c \cdot p.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [2, -4, 6]);
print('p = $p');
print('p·(1/2) = ${p.scale(Rational.fromInt(1, 2))}');
}p = 2 − 4α + 6α² p·(1/2) = 1 − 2α + 3α²
Polynumber<T> pow(int n)p^{n} (n \ge 0).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, 1]);
for (var n = 0; n <= 5; n++) print('(1 + α)^$n = ${p.pow(n)}');
}(1 + α)^0 = 1 (1 + α)^1 = 1 + α (1 + α)^2 = 1 + 2α + α² (1 + α)^3 = 1 + 3α + 3α² + α³ (1 + α)^4 = 1 + 4α + 6α² + 4α³ + α⁴ (1 + α)^5 = 1 + 5α + 10α² + 10α³ + 5α⁴ + α⁵
T evaluate(T x)The value at x (Horner): the polynumber read as a function, which over GF(p) forgets information.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [-2, 0, 1]); // α² − 2
print('p(3/2) = ${p.evaluate(Rational.fromInt(3, 2))}');
print('p(17/12) = ${p.evaluate(Rational.fromInt(17, 12))} (17/12 is a close rational to the root)');
print('p(1.41421356237) in doubles = ${1.41421356237 * 1.41421356237 - 2}');
}p(3/2) = 1/4 p(17/12) = 1/144 (17/12 is a close rational to the root) p(1.41421356237) in doubles = -8.753886504564434e-12
Polynumber<T> compose(Polynumber<T> q)p \circ q, the polynumber q substituted for \alpha.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [0, 0, 1]); // α²
final q = Polynumber.ints(rationals, [1, 1]); // 1 + α
print('p(q) = (1 + α)² = ${p.compose(q)}');
print('q(p) = 1 + α² = ${q.compose(p)}');
}p(q) = (1 + α)² = 1 + 2α + α² q(p) = 1 + α² = 1 + α²
Polynumber<T> translate(T c)The translation L_{c} p = p(c + \alpha). Its coefficients are the subderivatives at c: p(c + \alpha) = D_{0}p(c) + D_{1}p(c) \cdot \alpha + D_{2}p(c) \cdot \alpha^{2} + \cdots. Computed by repeated synthetic division (+ and \times only).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
// p(2 + α): its coefficients are the subderivatives of p at 2, found with + and × only.
final t = p.translate(Rational.fromInt(2));
print('p(α) = $p');
print('p(2 + α) = $t');
print('constant term = p(2) = ${t[0]}');
}p(α) = 1 − 2α + α³ p(2 + α) = 5 + 10α + 6α² + α³ constant term = p(2) = 5
Polynumber<T> subderivative(int k)The k-th subderivative D_{k} p: D_{k} \alpha^{n} = C(n, k) \cdot \alpha^{n-k}. D_{1} is the derivative. Over GF(p) the ordinary derivatives of \alpha^{p} vanish but D_{p} \alpha^{p} = 1.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
for (var k = 0; k <= 3; k++) {
print('D$k p = ${p.subderivative(k)}');
}
print('D2 p at 2 = ${p.subderivativeAt(2, Rational.fromInt(2))}');
}D0 p = 1 − 2α + α³ D1 p = −2 + 3α² D2 p = 3α D3 p = 1 D2 p at 2 = 6
T subderivativeAt(int k, T c)D_{k} p evaluated at c, read off the translated polynumber (the k-th entry of p(c + \alpha)).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The k-th subderivative at c is the k-th coefficient of p(c + α): Taylor coefficients, no factorials.
final p = Polynumber.ints(rationals, [0, 0, 0, 0, 1]); // α⁴
final c = Rational.fromInt(2);
for (var k = 0; k <= 4; k++) print('S$k at 2: ${p.subderivativeAt(k, c)}');
}S0 at 2: 16 S1 at 2: 32 S2 at 2: 24 S3 at 2: 8 S4 at 2: 1
Polynumber<T> tangent(T c, int k)The k-th tangent T_{c}^{k} p: p(c + \beta) truncated after \beta^{k}, written back in \alpha (\beta = \alpha - c). k = 1 is the tangent line, 2 the tangent conic, 3 the tangent cubic. No factorials.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 0, 1]);
final c = Rational.fromInt(1);
final line = p.tangent(c, 1), conic = p.tangent(c, 2);
print('tangent line at 1: $line');
print('tangent conic at 1: $conic');
List<List<double>> pts(Polynumber<Rational> f) => [
for (var i = 0; i <= 32; i++)
[(-0.5 + i / 16), f.evaluate(Rational.fromInt(-8 + i, 16)).toDouble()]
];
{'label': 'p', 'points': pts(p)},
{'label': 'tangent line', 'points': pts(line)},
{'label': 'tangent conic', 'points': pts(conic)},
]})}');
}tangent line at 1: −1 + α tangent conic at 1: 2 − 5α + 3α²
Polynumber<T> truncate(int k)The truncation after \alpha^{k} (degree \le k).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, 2, 3, 4, 5]);
print('p = $p');
print('truncate(2) = ${p.truncate(2)}');
print('truncate(0) = ${p.truncate(0)}');
}p = 1 + 2α + 3α² + 4α³ + 5α⁴ truncate(2) = 1 + 2α + 3α² truncate(0) = 1
T integrate(T t, T u)The relative integral S_{t}^{u} p = (S p)(u) - (S p)(t).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The exact area under y = α² from 0 to 1 is 1/3; from 1 to 2 it is 7/3.
final sq = Polynumber.ints(rationals, [0, 0, 1]);
print('∫ α² from 0 to 1 = ${sq.integrate(Rational.zero, Rational.one)}');
print('∫ α² from 1 to 2 = ${sq.integrate(Rational.one, Rational.fromInt(2))}');
{'label': 'α²', 'points': [for (var i = 0; i <= 20; i++) [i / 10, i * i / 100]]},
]})}');
}∫ α² from 0 to 1 = 1/3 ∫ α² from 1 to 2 = 7/3
bool sameFunctionAs(Polynumber<T> o)True when the two agree as functions on every element of a finite field (\alpha and \alpha^{7} over GF(7) do, though they are different polynumbers). Only for prime fields.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Over GF(7), α and α⁷ are different polynumbers that agree on every element.
final f = PrimeField(7);
final a = Polynumber.alpha(f), a7 = Polynumber.monomial(f, 7);
print('α == α⁷: ${a == a7}');
print('same function on GF(7): ${a.sameFunctionAs(a7)}');
print('α vs α³: ${a.sameFunctionAs(Polynumber.monomial(f, 3))}');
}α == α⁷: false same function on GF(7): true α vs α³: false
String format([String letter = 'α'])import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [3, 0, -1, 2]);
print('default: ${p.format()}');
print('in x: ${p.format('x')}');
print('toString: $p');
}default: 3 − α² + 2α³ in x: 3 − x² + 2x³ toString: 3 − α² + 2α³
Operators
T operator [](int k)a_{k} (zero beyond the degree).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [5, 0, -3]);
print('p = $p');
print('p[0] = ${p[0]}, p[2] = ${p[2]}, p[10] = ${p[10]}');
}p = 5 − 3α² p[0] = 5, p[2] = -3, p[10] = 0
Polynumber<T> operator +(Polynumber<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, 2, 3]), q = Polynumber.ints(rationals, [0, -2, 0, 1]);
print('($p) + ($q) = ${p + q}');
}(1 + 2α + 3α²) + (−2α + α³) = 1 + 3α² + α³
Polynumber<T> operator -(Polynumber<T> o)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, 2, 3]), q = Polynumber.ints(rationals, [1, 2, 1]);
print('($p) − ($q) = ${p - q}');
}(1 + 2α + 3α²) − (1 + 2α + α²) = 2α²
Polynumber<T> operator -()import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final p = Polynumber.ints(rationals, [1, -2, 3]);
print('−($p) = ${-p}');
print('p + (−p) = ${p + (-p)}');
}−(1 − 2α + 3α²) = −1 + 2α − 3α² p + (−p) = 0
Polynumber<T> operator *(Polynumber<T> o)The Cauchy product: [1, 3] \cdot [5, 2] = [5, 17, 6].
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Multiplication is the Cauchy product of coefficient lists.
final q = Polynumber.ints(rationals, [1, 3]) * Polynumber.ints(rationals, [5, 2]);
print('[1,3] * [5,2] = ${q.coefficients}');
print('(1 + α)^5 = ${Polynumber.ints(rationals, [1, 1]).pow(5).coefficients}');
}[1,3] * [5,2] = [5, 17, 6] (1 + α)^5 = [1, 5, 10, 10, 5, 1]
bool operator ==(Object other)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final a = Polynumber.alpha(rationals);
final one = Polynumber.constant(rationals, Rational.one);
print('(α + 1)(α − 1) == α² − 1: ${(a + one) * (a - one) == a * a - one}');
print('α == α + 0: ${a == a + Polynumber.zero(rationals)}');
}(α + 1)(α − 1) == α² − 1: true α == α + 0: true
UniformSolid
final class UniformSolid
A vertex-transitive solid: one vertex configuration (e.g. [4, 6, 6]) and its vertex count.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// Descartes: the corner curvatures (in turns) of a convex polyhedron add up to 2.
for (final s in uniformSolids.take(5)) {
print('${s.name}: ${s.vertices} vertices × ${s.perVertex} = ${s.total}, Euler ${s.euler}');
}
}Tetrahedron: 4 vertices × 1/2 = 2, Euler 2 Cube: 8 vertices × 1/4 = 2, Euler 2 Octahedron: 6 vertices × 1/3 = 2, Euler 2 Icosahedron: 12 vertices × 1/6 = 2, Euler 2 Dodecahedron: 20 vertices × 1/10 = 2, Euler 2
Constructors
const UniformSolid(this.name, this.configuration, this.vertices)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A solid by name, vertex configuration (faces around each vertex) and vertex count.
final s = UniformSolid('Snub cube', [3, 3, 3, 3, 4], 24);
print('${s.name}: per vertex ${s.perVertex} turn, total ${s.total} (Descartes: 2 turns for every convex solid)');
final p = UniformSolid('Pentagonal prism', [4, 4, 5], 10);
print('${p.name}: per vertex ${p.perVertex}, total ${p.total}');
}Snub cube: per vertex 1/12 turn, total 2 (Descartes: 2 turns for every convex solid) Pentagonal prism: per vertex 1/5, total 2
Properties
final String namefinal List<int> configurationfinal int verticesRational get perVertexRational get totalint get eulerV - E + F counted from the configuration: E = V \cdot \frac{\deg}{2}, F = \sum_{n} \frac{V \cdot (n\text{'s at a vertex})}{n}.
FaceMesh
final class FaceMesh
A polyhedral surface given by its faces (vertex ids, each face a regular polygon).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A square pyramid as a list of faces (vertex ids): 5 vertices, 8 edges, 5 faces.
final m = FaceMesh([[0, 1, 2, 3], [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]);
print('V = ${m.vertexIds.length}, E = ${m.edgeCount}, F = ${m.faces.length}, χ = ${m.euler}');
print('curvatures: ${m.curvatures}');
print('total curvature: ${m.total} turn');
}V = 5, E = 8, F = 5, χ = 2
curvatures: {0: 5/12, 1: 5/12, 2: 5/12, 3: 5/12, 4: 1/3}
total curvature: 2 turnConstructors
FaceMesh(this.faces)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A cube: six squares on eight vertices.
final cube = FaceMesh([[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]]);
print('χ = ${cube.euler}, each vertex ${cube.curvatures[0]} turn, total ${cube.total}');
}χ = 2, each vertex 1/4 turn, total 2
Properties
final List<List<int>> facesSet<int> get vertexIdsint get edgeCountint get eulerRational get totalPolyCurve
final class PolyCurve<T>
A polynumber curve \gamma = (p, q): x = p, y = q.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The parabola x = t, y = t², and the exact signed area swept from t = 0 to 1.
final g = PolyCurve(Polynumber.ints(rationals, [0, 1]), Polynumber.ints(rationals, [0, 0, 1]));
print('curve: $g');
print('at t = 1/2: ${g.at(Rational.fromInt(1, 2))}');
print('signed area 0 → 1: ${g.signedArea(Rational.zero, Rational.one)}');
print('slice area 0 → 1: ${g.sliceArea(Rational.zero, Rational.one)}');
{'points': [for (var i = -10; i <= 10; i++) [i / 10, i * i / 100]]}
]})}');
}curve: (t, t²) at t = 1/2: (1/2, 1/4) signed area 0 → 1: 1/6 slice area 0 → 1: 1/6
Constructors
PolyCurve(this.p, this.q)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A polynomial curve (p(t), q(t)): here the cusp (t², t³).
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t * t, t * t * t);
print('g = $g');
print('g(2) = ${g.at(Rational.fromInt(2))}, velocity ${g.derivative}');
}g = (t², t³) g(2) = (4, 8), velocity (2t, 3t²)
Properties
final Polynumber<T> pfinal Polynumber<T> qScalarField<T> get fieldint get degreePolynumber<T> get areaDifferentialThe area differential p \cdot Dq - q \cdot Dp.
Methods
Pt<T> at(T t)\gamma(t).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
final t = Polynumber.alpha(rationals);
final one = Polynumber.constant(rationals, Rational.one);
final g = PolyCurve(one - t * t, t.scale(Rational.fromInt(2))); // a rational parabola
for (final s in [0, 1, 2]) print('g($s) = ${g.at(Rational.fromInt(s))}');
}g(0) = (1, 0) g(1) = (0, 2) g(2) = (-3, 4)
T signedArea(T t, T u)S_{t}^{u}(\gamma) = \tfrac{1}{2} S_{t}^{u}(p \cdot Dq - q \cdot Dp): the area subtended at the origin O by the arc from t to u.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Signed area swept from the origin by (t, t²) between t = 0 and 1: ½ S(p q' − q p').
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t, t * t);
print('signed area 0..1: ${g.signedArea(Rational.zero, Rational.one)}');
print('backwards 1..0: ${g.signedArea(Rational.one, Rational.zero)}');
}signed area 0..1: 1/6 backwards 1..0: -1/6
T signedAreaByMonomials(T t, T u)The same number from the monomial formula and bilinearity alone (the five-property extension).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The same signed area summed monomial by monomial; the two methods agree exactly.
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t * t - t, t * t * t);
final a = Rational.fromInt(-1), b = Rational.fromInt(2);
print('by monomials: ${g.signedAreaByMonomials(a, b)}');
print('direct: ${g.signedArea(a, b)}');
}by monomials: -9/20 direct: -9/20
T sliceArea(T t, T u)The signed area of the slice cut off by the chord: the arc from t to u, then back along the chord (S_{t}^{u}(\gamma) - \operatorname{edge}(\gamma(t), \gamma(u))).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// Area between the parabola y = t² and its chord from t = 0 to t = 2.
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t, t * t);
print('slice area: ${g.sliceArea(Rational.zero, Rational.fromInt(2))}');
print('(Archimedes: 4/3 of the triangle, here 4/3 × 1 = 4/3)');
}slice area: 4/3 (Archimedes: 4/3 of the triangle, here 4/3 × 1 = 4/3)
T delta(int i, int j)\Delta_{ij} = p_{i}q_{j} - p_{j}q_{i}, the coefficient determinants.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// δ_ij = p_i q_j − p_j q_i: the brackets the signed area is built from.
final g = PolyCurve(Polynumber.ints(rationals, [1, 2, 3]), Polynumber.ints(rationals, [0, 1, 0, 4]));
print('g = $g');
print('δ01 = ${g.delta(0, 1)}, δ12 = ${g.delta(1, 2)}, δ23 = ${g.delta(2, 3)}');
}g = (1 + 2t + 3t², t + 4t³) δ01 = 1, δ12 = -3, δ23 = 12
T cubicSliceArea(T t, T u)For an Archimedean curve of degree \le 3 (\Delta_{23} = 0), the slice area by Wildberger's closed form (\tfrac{1}{6}\Delta_{12} + \tfrac{1}{4}\Delta_{13}(t + u))(u - t)^{3}. Equals sliceArea exactly; a general cubic (\Delta_{23} \ne 0) has extra terms, so this throws there.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A closed-form slice area for cubic curves; it matches the general method.
final t = Polynumber.alpha(rationals);
final g = PolyCurve(t, t * t * t - t);
final a = Rational.zero, b = Rational.fromInt(2);
print('cubic formula: ${g.cubicSliceArea(a, b)}');
print('general: ${g.sliceArea(a, b)}');
}cubic formula: 4 general: 4
String toString()EllipticTriangle
final class EllipticTriangle
A spherical (elliptic) triangle with its three quadrances and spreads (q_{i} opposite vertex i, S_{i} at vertex i).
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// The octant triangle: three right angles on the sphere.
final t = EllipticTriangle(v3i(1, 0, 0), v3i(0, 1, 0), v3i(0, 0, 1));
print('quadrances: ${t.q1}, ${t.q2}, ${t.q3}');
print('spreads: ${t.s1}, ${t.s2}, ${t.s3}');
print('spread law holds: ${t.sLaw}, cross law holds: ${t.crossLaw}');
}quadrances: 1, 1, 1 spreads: 1, 1, 1 spread law holds: true, cross law holds: true
Constructors
EllipticTriangle(this.a1, this.a2, this.a3)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// A spherical triangle from three directions; quadrances and spreads computed on construction.
final t = EllipticTriangle(v3i(1, 0, 0), v3i(0, 1, 0), v3i(1, 1, 1));
print('q = ${t.q1}, ${t.q2}, ${t.q3}');
print('S = ${t.s1}, ${t.s2}, ${t.s3}');
print('spread law ${t.sLaw}, cross law ${t.crossLaw}');
}q = 2/3, 2/3, 1 S = 1/2, 1/2, 3/4 spread law true, cross law true
Properties
final V3 a1final V3 a2final V3 a3final Rational q1final Rational q2final Rational q3final Rational s1final Rational s2final Rational s3bool get sLaw\frac{S_{1}}{q_{1}} = \frac{S_{2}}{q_{2}} = \frac{S_{3}}{q_{3}} (cross-multiplied, so a zero quadrance is fine).
bool get crossLaw(q_{2}q_{3}S_{1} - q_{1} - q_{2} - q_{3} + 2)^{2} = 4(1 - q_{1})(1 - q_{2})(1 - q_{3}), and the same at every vertex.
bool get pythagorasPythagoras: when S_{1} = 1, q_{1} = q_{2} + q_{3} - q_{2}q_{3} (true when S_{1} \ne 1 too, vacuously).
QuadricKind
enum QuadricKind
What the tangent quadric looks like: a bowl (K > 0), a saddle (K < 0) or a trough (K = 0).
Values
elliptichyperbolicparabolicSurfacePoint
final class SurfacePoint
The second-order picture of z = f(x, y) at (r, s), exact.
import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
import 'raya:convert';
void main() {
// z = x²y at (1, 2): exact partial derivatives, tangent plane and Gaussian curvature.
final f = Bipolynumber.ints(rationals, {(2, 1): 1});
final p = SurfacePoint(f, Rational.one, Rational.fromInt(2));
print('z = ${p.z}, fx = ${p.fx}, fy = ${p.fy}');
print('fxx = ${p.fxx}, fxy = ${p.fxy}, fyy = ${p.fyy}');
print('Gaussian curvature: ${p.gaussian} (${p.kind})');
}z = 2, fx = 4, fy = 1 fxx = 4, fxy = 2, fyy = 0 Gaussian curvature: -1/81 (QuadricKind.hyperbolic)
Constructors
SurfacePoint(this.f, this.r, this.s)import 'package:riptide_algebraic_calculus/riptide_algebraic_calculus.ray';
void main() {
// The saddle z = xy at (1, 1): its tangent plane and quadric.
final f = Bipolynumber.ints(rationals, {(1, 1): 1});
final p = SurfacePoint(f, Rational.one, Rational.one);
print('tangent plane: ${p.tangentPlane.format()}');
print('tangent quadric: ${p.tangentQuadric.format()}');
print('kind: ${p.kind}');
}tangent plane: −1 + x + y tangent quadric: xy kind: QuadricKind.hyperbolic
Properties
final Bipolynumber<Rational> ffinal Rational rfinal Rational sfinal Bipolynumber<Rational> shiftedf(r + \alpha, s + \beta): its (1,0), (0,1) entries are the slopes, (2,0), (1,1), (0,2) the second subderivatives.
Rational get zRational get fxRational get fyRational get fxyRational get fyyBipolynumber<Rational> get tangentPlaneThe tangent plane in place: z = constant + f_{x} \cdot x + f_{y} \cdot y.
Rational get gaussianK = \frac{f_{xx} f_{yy} - f_{xy}^{2}}{(1 + f_{x}^{2} + f_{y}^{2})^{2}}.
Rational get hessianThe Hessian determinant f_{xx} f_{yy} - f_{xy}^{2} (its sign is the quadric's kind; at a horizontal point it is K).
QuadricKind get kind