Overview
Kepler orbits with our mathematics (Levi-Civita + Cayley: Rational, Geometric) and the classic ways (leapfrog, Newton on Kepler's equation: Transcendental, Root).
Functions and constants
Functions
double solveKeplerEquation(double meanAnomaly, double e, {int iterations = 30})Kepler's equation M = E - e \sin E solved for E by Newton's method (Transcendental: sin, cos).
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
for (final e in [0.1, 0.5, 0.9]) {
final ea = solveKeplerEquation(1.0, e);
print('e = $e: E = ${ea.toStringAsFixed(10)}, check E − e sin E = ${(ea - e * math.sin(ea)).toStringAsFixed(10)}');
}
for (final e in [0.0, 0.5, 0.9])
{'label': 'e = $e', 'points': [for (var i = 0; i <= 60; i++) [2 * math.pi * i / 60, solveKeplerEquation(2 * math.pi * i / 60, e)]]},
]})}');
}e = 0.1: E = 1.0885977524, check E − e sin E = 1.0000000000 e = 0.5: E = 1.4987011335, check E − e sin E = 1.0000000000 e = 0.9: E = 1.8620866869, check E − e sin E = 1.0000000000
double meanAnomalyFromHalfTangent(double u, double e)Kepler's equation in the half-angle tangent u = \tan(\frac{E}{2}): M = 2 \cdot \operatorname{atan}(u) - \frac{2eu}{1 + u^{2}}. The first term (an area, Transcendental) is all that is not rational.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
final e = 0.6;
final ea = 1.2;
final u = math.tan(ea / 2);
print('from u = tan(E/2): M = ${meanAnomalyFromHalfTangent(u, e).toStringAsFixed(12)}');
print('classic E − e sin E: ${(ea - e * math.sin(ea)).toStringAsFixed(12)}');
{'label': 'M', 'points': [for (var i = -40; i <= 40; i++) [i / 10, meanAnomalyFromHalfTangent(i / 10, e)]]},
]})}');
}from u = tan(E/2): M = 0.640776548420 classic E − e sin E: 0.640776548420
Vec3<T> cross3<T>(KeplerArith<T> f, Vec3<T> a, Vec3<T> b)x \times v.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final a = (f.fromInt(1), f.fromInt(2), f.fromInt(0)), b = (f.fromInt(0), f.fromInt(1, 2), f.fromInt(3));
final c = cross3(f, a, b);
print('a × b = (${c.$1}, ${c.$2}, ${c.$3})');
print('in doubles: ${cross3(const F64Arith(), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0))}');
}a × b = (6, -3, 1/2) in doubles: (0.0, 0.0, 1.0)
(Vec3<T>, Vec3<T>) keplerInvariants<T>(KeplerArith<T> f, Vec3<T> x, Vec3<T> v, T r, T mu)Angular momentum x \times \dot{x} and the Laplace-Runge-Lenz vector \frac{\dot{x} \times L}{\mu} - \frac{x}{r} of a 3-D state.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
// At perihelion (1, 0, 0) with speed 4 along y, μ = 10: e = |A|.
final (l, a) = keplerInvariants(f, (f.fromInt(1), f.fromInt(0), f.fromInt(0)), (f.fromInt(0), f.fromInt(4), f.fromInt(0)), f.fromInt(1), f.fromInt(10));
print('angular momentum L = (${l.$1}, ${l.$2}, ${l.$3})');
print('LRL vector A = (${a.$1}, ${a.$2}, ${a.$3}) → e = ${a.$1}');
}angular momentum L = (0, 0, 4) LRL vector A = (3/5, 0, 0) → e = 3/5
T spreadBetween<T>(KeplerArith<T> f, (T, T) u, (T, T) v)Spread between two plane vectors in rational trigonometry: (u \wedge v)^{2} / (Q(u) Q(v)); 0 for parallel, 1 for perpendicular.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final s = spreadBetween(f, (f.fromInt(1), f.fromInt(0)), (f.fromInt(1), f.fromInt(1)));
print('spread between (1, 0) and (1, 1) = $s (the 45° diagonal)');
print('(3, 4) and (4, −3): ${spreadBetween(f, (f.fromInt(3), f.fromInt(4)), (f.fromInt(4), f.fromInt(-3)))}');
print('parallel: ${spreadBetween(const F64Arith(), (1.0, 2.0), (2.0, 4.0))}');
}spread between (1, 0) and (1, 1) = 1/2 (the 45° diagonal) (3, 4) and (4, −3): 1 parallel: 0.0
Typedefs
typedef Vec3<T> = (T, T, T)Classes
KeplerArith
abstract interface class KeplerArith<T>
- {-}\ \times\ \div on
T. Nothing else: no roots, no transcendental functions.
Methods
T fromInt(int n, [int d = 1])import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact n/d: ${exact.fromInt(1, 3)}');
print('f64 n/d: ${float.fromInt(1, 3)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact n/d: 1/3 f64 n/d: 0.3333333333333333 generic energy, exact: -2 generic energy, f64: -2.0
T add(T a, T b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact a + b: ${exact.add(a, b)}');
print('f64 a + b: ${float.add(1 / 3, 2 / 7)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact a + b: 13/21 f64 a + b: 0.6190476190476191 generic energy, exact: -2 generic energy, f64: -2.0
T sub(T a, T b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact a − b: ${exact.sub(a, b)}');
print('f64 a − b: ${float.sub(1 / 3, 2 / 7)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact a − b: 1/21 f64 a − b: 0.047619047619047616 generic energy, exact: -2 generic energy, f64: -2.0
T mul(T a, T b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact a × b: ${exact.mul(a, b)}');
print('f64 a × b: ${float.mul(1 / 3, 2 / 7)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact a × b: 2/21 f64 a × b: 0.09523809523809523 generic energy, exact: -2 generic energy, f64: -2.0
T div(T a, T b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact a ÷ b: ${exact.div(a, b)}');
print('f64 a ÷ b: ${float.div(1 / 3, 2 / 7)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact a ÷ b: 7/6 f64 a ÷ b: 1.1666666666666667 generic energy, exact: -2 generic energy, f64: -2.0
double toDouble(T a)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
// One function, two number systems: the energy of a state, v²/2 − μ/r with r given.
T energy<T>(KeplerArith<T> f, T vx, T vy, T mu, T r) =>
f.sub(f.div(f.add(f.mul(vx, vx), f.mul(vy, vy)), f.fromInt(2)), f.div(mu, r));
void main() {
const exact = ExactArith(), float = F64Arith();
final a = exact.fromInt(1, 3), b = exact.fromInt(2, 7);
print('exact as a double: ${exact.toDouble(a)}');
print('f64 as a double: ${float.toDouble(1 / 3)}');
print('generic energy, exact: ${energy(exact, exact.fromInt(0), exact.fromInt(4), exact.fromInt(10), exact.fromInt(1))}');
print('generic energy, f64: ${energy(float, 0.0, 4.0, 10.0, 1.0)}');
}exact as a double: 0.3333333333333333 f64 as a double: 0.3333333333333333 generic energy, exact: -2 generic energy, f64: -2.0
ExactArith
final class ExactArith implements KeplerArith<Rational>
Exact: Rational (riptide_precision), lowest terms, arbitrary size.
Constructors
const ExactArith()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
// The same Levi-Civita orbit over exact fractions: energy stays exactly −2 for every step.
final o = LeviCivitaKepler<Rational>.rationalEccentricity(f, k: f.fromInt(2), q: f.fromInt(1), ds: f.fromInt(1, 8));
for (var i = 0; i < 6; i++) {
o.step();
}
print('energy after 6 steps: ${o.energy}');
print('x = ${o.position.$1.toDouble().toStringAsFixed(6)} (a ${o.position.$1.numerator.bitLength}-bit numerator)');
}energy after 6 steps: -2 x = -1.318298 (a 97-bit numerator)
Methods
Rational fromInt(int n, [int d = 1])import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
print('fromInt(3, 4) = ${f.fromInt(3, 4)}');
print('fromInt(-5) = ${f.fromInt(-5)}');
}fromInt(3, 4) = 3/4 fromInt(-5) = -5
Rational add(Rational a, Rational b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('add(1/10, 2/10) = ${f.add(a, b)}');
var s = f.fromInt(0);
for (var i = 0; i < 10; i++) {
s = f.add(s, f.fromInt(1, 10));
}
print('ten tenths: $s');
}add(1/10, 2/10) = 3/10 ten tenths: 1
Rational sub(Rational a, Rational b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('sub(1/10, 2/10) = ${f.sub(a, b)}');
}sub(1/10, 2/10) = -1/10
Rational mul(Rational a, Rational b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('mul(1/10, 2/10) = ${f.mul(a, b)}');
}mul(1/10, 2/10) = 1/50
Rational div(Rational a, Rational b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('div(1/10, 2/10) = ${f.div(a, b)}');
}div(1/10, 2/10) = 1/2
double toDouble(Rational a)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
print('toDouble(1/3) = ${f.toDouble(f.fromInt(1, 3))}');
}toDouble(1/3) = 0.3333333333333333
F64Arith
final class F64Arith implements KeplerArith<double>
IEEE binary64.
Constructors
const F64Arith()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
final o = LeviCivitaKepler<double>.rationalEccentricity(f, k: 2, q: 1, ds: 0.125);
for (var i = 0; i < 1000; i++) {
o.step();
}
print('energy after 1000 f64 steps: ${o.energy}');
print('drift from −2 below 1e-12: ${(o.energy + 2).abs() < 1e-12}');
}energy after 1000 f64 steps: -1.9999999999997846 drift from −2 below 1e-12: true
Methods
double fromInt(int n, [int d = 1])import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
print('fromInt(3, 4) = ${f.fromInt(3, 4)}');
print('fromInt(-5) = ${f.fromInt(-5)}');
}fromInt(3, 4) = 0.75 fromInt(-5) = -5.0
double add(double a, double b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('add(1/10, 2/10) = ${f.add(a, b)}');
var s = 0.0;
for (var i = 0; i < 10; i++) {
s = f.add(s, 0.1);
}
print('ten tenths: $s');
}add(1/10, 2/10) = 0.30000000000000004 ten tenths: 0.9999999999999999
double sub(double a, double b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('sub(1/10, 2/10) = ${f.sub(a, b)}');
}sub(1/10, 2/10) = -0.1
double mul(double a, double b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('mul(1/10, 2/10) = ${f.mul(a, b)}');
}mul(1/10, 2/10) = 0.020000000000000004
double div(double a, double b)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
final a = f.fromInt(1, 10), b = f.fromInt(2, 10);
print('div(1/10, 2/10) = ${f.div(a, b)}');
}div(1/10, 2/10) = 0.5
double toDouble(double a)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = F64Arith();
print('toDouble(1/3) = ${f.toDouble(f.fromInt(1, 3))}');
}toDouble(1/3) = 0.3333333333333333
LeapfrogKepler
final class LeapfrogKepler
Störmer-Verlet (leapfrog) in physical time t: a = -\mu z / r^{3} with r = \sqrt{x^{2} + y^{2}} (Root). Symplectic, second order: the standard good integrator for orbits.
Constructors
LeapfrogKepler({required this.x, required this.y, required this.vx, required this.vy, required this.mu, required this.dt})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'raya:math' as math;
List<List<double>> orbitOf(void Function() step, (double, double) Function() pos, int n) {
final out = <List<double>>[];
for (var i = 0; i < n; i++) {
final (x, y) = pos();
out.add([x, y]);
step();
}
return out;
}
void main() {
final o = LeapfrogKepler(x: 1, y: 0, vx: 0, vy: 4, mu: 10, dt: 0.02);
print('start: energy ${o.energy}, L ${o.angularMomentum}, e = ${o.lrl.$1}');
final pts = orbitOf(o.step, () => (o.x, o.y), 400);
print('after ${o.steps} steps: energy ${o.energy.toStringAsFixed(6)}');
}start: energy -2.0, L 4.0, e = 0.6000000000000001 after 400 steps: energy -1.998928
Properties
double xdouble ydouble vxdouble vyfinal double mufinal double dtint stepsdouble timedouble get energydouble get angularMomentumMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'raya:math' as math;
void main() {
// Coarse steps: the energy oscillates instead of staying at −2.
final o = LeapfrogKepler(x: 1, y: 0, vx: 0, vy: 4, mu: 10, dt: 0.05);
final e = <List<double>>[];
for (var i = 0; i < 200; i++) {
o.step();
e.add([o.time, o.energy]);
}
print('energy after 200 steps: ${o.energy.toStringAsFixed(6)} (exact orbit: −2)');
}energy after 200 steps: -1.976837 (exact orbit: −2)
LeviCivitaKeplerF64
final class LeviCivitaKeplerF64
LeviCivitaKepler in plain doubles.
Constructors
LeviCivitaKeplerF64({required this.w1, required this.w2, required this.p1, required this.p2, required this.mu, required double ds})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
List<List<double>> orbitOf(void Function() step, (double, double) Function() pos, int n) {
final out = <List<double>>[];
for (var i = 0; i < n; i++) {
final (x, y) = pos();
out.add([x, y]);
step();
}
return out;
}
void main() {
// w = (1, 0), w' = (0, 2), μ = 10: the orbit with perihelion 1 and e = 3/5.
final o = LeviCivitaKeplerF64(w1: 1, w2: 0, p1: 0, p2: 2, mu: 10, ds: 0.02);
print('energy ${o.energy}, L ${o.angularMomentum}, LRL ${o.lrl}');
final pts = orbitOf(o.step, () => o.position, 160);
}energy -2.0, L 4.0, LRL (0.6000000000000001, -0.0)
factory LeviCivitaKeplerF64.rationalEccentricity({required double k, required double q, required double ds})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
List<List<double>> orbitOf(void Function() step, (double, double) Function() pos, int n) {
final out = <List<double>>[];
for (var i = 0; i < n; i++) {
final (x, y) = pos();
out.add([x, y]);
step();
}
return out;
}
void main() {
final views = <List<List<double>>>[];
for (final k in [1.0, 1.5, 2.0, 3.0]) {
final o = LeviCivitaKeplerF64.rationalEccentricity(k: k, q: 1, ds: 0.03);
print('k = $k: e = (k² − 1)/(k² + 1) = ${((k * k - 1) / (k * k + 1)).toStringAsFixed(4)}, LRL x = ${o.lrl.$1.toStringAsFixed(4)}');
if (k < 3) views.add(orbitOf(o.step, () => o.position, 220));
}
}k = 1.0: e = (k² − 1)/(k² + 1) = 0.0000, LRL x = 0.0000 k = 1.5: e = (k² − 1)/(k² + 1) = 0.3846, LRL x = 0.3846 k = 2.0: e = (k² − 1)/(k² + 1) = 0.6000, LRL x = 0.6000 k = 3.0: e = (k² − 1)/(k² + 1) = 0.8000, LRL x = 0.8000
Properties
double w1double w2double p1double p2final double mulate final double cdouble timeint stepsdouble get radius(double, double) get position(double, double) get velocitydouble get energydouble get angularMomentum(double, double) get lrlMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
final o = LeviCivitaKeplerF64.rationalEccentricity(k: 2, q: 1, ds: 0.05);
final h = <List<double>>[];
for (var i = 0; i < 300; i++) {
o.step();
h.add([o.time, o.energy + 2]);
}
print('after ${o.steps} steps, time ${o.time.toStringAsFixed(4)}: energy error ${(o.energy + 2).abs() < 1e-12 ? 'below 1e-12' : (o.energy + 2)}');
}after 300 steps, time 38.2413: energy error below 1e-12
KsKepler
final class KsKepler<T>
3-D Kepler in Kustaanheimo-Stiefel variables, Cayley-stepped, over T.
Constructors
KsKepler(this.f, {required List<T> u, required List<T> du, required this.mu, required T ds})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
const f = F64Arith();
// u = (1, 0, 0, 0) puts the body at x = (1, 0, 0); u' sets the velocity (the constraint holds: 0).
final o = KsKepler<double>(f, u: [1, 0, 0, 0], du: [0, 1.6, 0.8, 0], mu: 10, ds: 0.02);
print('position ${o.position}, radius ${o.radius}');
print('velocity ${o.velocity}, constraint ${o.constraint}, energy ${o.energy}');
final xy = <List<double>>[], xz = <List<double>>[];
for (var i = 0; i < 200; i++) {
final (x, y, z) = o.position;
xy.add([x, y]);
xz.add([x, z]);
o.step();
}
}position (1.0, 0.0, 0.0), radius 1.0 velocity (0.0, 3.2, 1.6), constraint 0.0, energy -3.5999999999999988
Properties
final KeplerArith<T> ffinal T mulate final T cint stepsT get constraintThe KS bilinear constraint u4 u1' - u_{3} u2' + u2 u3' - u_{1} u4' (zero on physical states).
T get energyT get timeMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final o = KsKepler<Rational>(f,
u: [f.fromInt(1), f.fromInt(0), f.fromInt(0), f.fromInt(0)],
du: [f.fromInt(0), f.fromInt(8, 5), f.fromInt(4, 5), f.fromInt(0)],
mu: f.fromInt(10), ds: f.fromInt(1, 16));
final h0 = o.energy;
final (l0, a0) = keplerInvariants(f, o.position, o.velocity, o.radius, o.mu);
for (var i = 0; i < 6; i++) {
o.step();
}
final (l, a) = keplerInvariants(f, o.position, o.velocity, o.radius, o.mu);
print('6 exact steps: energy ${o.energy == h0}, L ${l == l0}, LRL ${a == a0}, constraint ${o.constraint}');
}6 exact steps: energy true, L true, LRL true, constraint 0
Multivector
final class Multivector<T>
A multivector of G3 over T.
Constructors
Multivector(this.f, List<T> c)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
Multivector<Rational> mv(Map<int, int> c) => Multivector(f, [for (var k = 0; k < 8; k++) f.fromInt(c[k] ?? 0)]);
final e1 = mv({Multivector.e1: 1}), e2 = mv({Multivector.e2: 1});
print('e1 e2 = ${(e1 * e2).c} (the bivector e12)');
print('e2 e1 = ${(e2 * e1).c} (−e12)');
print('e1 e1 = ${(e1 * e1).scalar}');
}e1 e2 = [0, 0, 0, 1, 0, 0, 0, 0] (the bivector e12) e2 e1 = [0, 0, 0, -1, 0, 0, 0, 0] (−e12) e1 e1 = 1
factory Multivector.zero(KeplerArith<T> f)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final z = Multivector<Rational>.zero(f);
print('zero: ${z.c}');
z.c[0] = f.fromInt(3);
z.c[Multivector.e12] = f.fromInt(4);
print('3 + 4e12 times its reverse: ${(z * z.reverse).scalar}');
}zero: [0, 0, 0, 0, 0, 0, 0, 0] 3 + 4e12 times its reverse: 25
Static
Properties
final KeplerArith<T> ffinal List<T> cVec3<T> get vectorT get scalarMethods
Multivector<T> grade(int g)The grade-g part.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
Multivector<Rational> mv(Map<int, int> c) => Multivector(f, [for (var k = 0; k < 8; k++) f.fromInt(c[k] ?? 0)]);
final m = mv({0: 2, Multivector.e1: 3, Multivector.e23: 5, 7: 7});
for (var g = 0; g <= 3; g++) {
print('grade $g: ${m.grade(g).c}');
}
}grade 0: [2, 0, 0, 0, 0, 0, 0, 0] grade 1: [0, 3, 0, 0, 0, 0, 0, 0] grade 2: [0, 0, 0, 0, 0, 0, 5, 0] grade 3: [0, 0, 0, 0, 0, 0, 0, 7]
Operators
Multivector<T> operator *(Multivector<T> o)import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
Multivector<Rational> mv(Map<int, int> c) => Multivector(f, [for (var k = 0; k < 8; k++) f.fromInt(c[k] ?? 0)]);
// R = 1 + e12 turns by 90° and scales by R R̃ = 2: R e1 R̃ = −2 e2.
final r = mv({0: 1, Multivector.e12: 1}), e1 = mv({Multivector.e1: 1});
final x = r * e1 * r.reverse;
print('R e1 R̃ = ${x.vector}');
print('R R̃ = ${(r * r.reverse).scalar}');
}R e1 R̃ = (0, -2, 0) R R̃ = 2
SpinorKepler
final class SpinorKepler<T>
3-D Kepler in Hestenes' spinor form: x = U e1 \tilde{U} for an even multivector U (scalar + bivectors), r = U \tilde{U}, dx/ds = 2⟨U' e1 \tilde{U}⟩₁, U'' = (\frac{h}{2}) U with the gauge ⟨U' e1 \tilde{U}⟩₃ = 0, Cayley-stepped. Badges: Geometric, Rational.
Constructors
SpinorKepler(this.f, {required Multivector<T> u, required Multivector<T> du, required this.mu, required T ds})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
const f = F64Arith();
final u = Multivector<double>(f, [1, 0, 0, 0, 0, 0, 0, 0]);
final du = Multivector<double>(f, [0, 0, 0, 1.0, 0.5, 0, 0, 0]); // the e12 and e13 parts
final o = SpinorKepler<double>(f, u: u, du: du, mu: 10, ds: 0.02);
print('position ${o.position}, radius ${o.radius}, gauge ${o.gauge}');
print('energy ${o.energy}');
final xy = <List<double>>[];
for (var i = 0; i < 200; i++) {
xy.add([o.position.$1, o.position.$2]);
o.step();
}
}position (1.0, 0.0, 0.0), radius 1.0, gauge 0.0 energy -7.5
Properties
final KeplerArith<T> ffinal T mulate final T cint stepsT get energyT get timeMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final c = List.filled(8, f.fromInt(0));
final u = Multivector<Rational>(f, [f.fromInt(1), ...c.skip(1)]);
final du = Multivector<Rational>(f, [for (var k = 0; k < 8; k++) k == 3 ? f.fromInt(2) : f.fromInt(0)]);
final o = SpinorKepler<Rational>(f, u: u, du: du, mu: f.fromInt(10), ds: f.fromInt(1, 10));
final h0 = o.energy;
for (var i = 0; i < 5; i++) {
o.step();
}
print('after 5 exact steps: energy ${o.energy} (start $h0), gauge ${o.gauge}');
print('position x ≈ ${o.position.$1.toDouble().toStringAsFixed(6)}');
}after 5 exact steps: energy -2 (start -2), gauge 0 position x ≈ -0.147494
LogHKepler
final class LogHKepler<T>
Planar Kepler by the logarithmic-Hamiltonian leapfrog (drift-kick-drift), over T (exact or f64).
Constructors
LogHKepler(this.f, {required T x, required T y, required T vx, required T vy, required this.mu, required T ds})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
// Perihelion (1, 0), speed 4, μ = 10: h = 8 − 10 = −2, so B = −h = 2.
final o = LogHKepler<Rational>(f, x: f.fromInt(1), y: f.fromInt(0), vx: f.fromInt(0), vy: f.fromInt(4), mu: f.fromInt(10), ds: f.fromInt(1, 10));
o.b = f.fromInt(2);
print('angular momentum ${o.angularMomentum}, energy residual at h = −2: ${o.energyResidual(f.fromInt(-2))}');
}angular momentum 4, energy residual at h = −2: 0
Properties
final KeplerArith<T> ffinal T mulate T bint steps(T, T) get position(T, T) get velocityT get timeMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final o = LogHKepler<Rational>(f, x: f.fromInt(1), y: f.fromInt(0), vx: f.fromInt(0), vy: f.fromInt(4), mu: f.fromInt(10), ds: f.fromInt(1, 10));
o.b = f.fromInt(2);
final l0 = o.angularMomentum;
for (var i = 0; i < 4; i++) {
o.step();
}
print('4 exact steps: angular momentum unchanged: ${o.angularMomentum == l0}');
print('energy residual now: ${o.energyResidual(f.fromInt(-2))} (exactly zero means exactly on the h = −2 shell)');
print('time ≈ ${o.time.toDouble().toStringAsFixed(6)}');
}4 exact steps: angular momentum unchanged: true energy residual now: 0 (exactly zero means exactly on the h = −2 shell) time ≈ 0.040066
T energyResidual(T h)(T - h)^{2} \cdot r^{2} - \mu^{2}: zero exactly when the energy is exactly h (no root needed to check it).
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
const f = F64Arith();
final o = LogHKepler<double>(f, x: 1, y: 0, vx: 0, vy: 4, mu: 10, ds: 0.05);
o.b = 2;
final res = <List<double>>[];
for (var i = 0; i < 150; i++) {
o.step();
res.add([o.time, o.energyResidual(-2)]);
}
print('largest |residual| over 150 steps: ${res.map((p) => p[1].abs()).reduce((a, b) => a > b ? a : b).toStringAsExponential(2)}');
}largest |residual| over 150 steps: 1.85e-13
LogHKeplerF64
final class LogHKeplerF64
LogHKepler in plain doubles, for honest timing.
Constructors
LogHKeplerF64({required this.x, required this.y, required this.vx, required this.vy, required this.mu, required this.ds, required this.b})import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
final o = LogHKeplerF64(x: 1, y: 0, vx: 0, vy: 4, mu: 10, ds: 0.05, b: 2);
final pts = <List<double>>[];
for (var i = 0; i < 1200; i++) {
if (i % 10 == 0) pts.add([o.x, o.y]);
o.step();
}
print('after ${o.steps} steps: time ${o.time.toStringAsFixed(4)}, position (${o.x.toStringAsFixed(4)}, ${o.y.toStringAsFixed(4)})');
}after 1200 steps: time 15.4025, position (0.6095, -1.0733)
Properties
double xdouble ydouble vxdouble vydouble timefinal double mufinal double dsfinal double bint stepsMethods
void step()import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
void main() {
// Big steps in the fictitious time still trace the right ellipse; time steps shrink near perihelion.
final o = LogHKeplerF64(x: 1, y: 0, vx: 0, vy: 4, mu: 10, ds: 0.2, b: 2);
final dt = <List<double>>[];
var last = 0.0;
for (var i = 0; i < 60; i++) {
o.step();
dt.add([i.toDouble(), o.time - last]);
last = o.time;
}
print('time per step: smallest ${dt.map((p) => p[1]).reduce((a, b) => a < b ? a : b).toStringAsFixed(4)}, largest ${dt.map((p) => p[1]).reduce((a, b) => a > b ? a : b).toStringAsFixed(4)}');
}time per step: smallest 0.0200, largest 0.0717
LeviCivitaKepler
final class LeviCivitaKepler<T>
A planar Kepler orbit in Levi-Civita variables, stepped by Cayley maps over T (exact Rational or f64).
Constructors
LeviCivitaKepler(this.f, {required T w1, required T w2, required T p1, required T p2, required this.mu, required T ds})Starts at w = (w1, w2), w' = (p1, p2) about a centre of strength mu, with clock step ds.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final o = LeviCivitaKepler<Rational>(f, w1: f.fromInt(1), w2: f.fromInt(0), p1: f.fromInt(0), p2: f.fromInt(2), mu: f.fromInt(10), ds: f.fromInt(1, 10));
print('energy ${o.energy}, angular momentum ${o.angularMomentum}, LRL ${o.lrl}');
final pts = <List<double>>[];
for (var i = 0; i < 40; i++) {
pts.add([o.position.$1.toDouble(), o.position.$2.toDouble()]);
o.step();
}
print('after 40 exact steps: energy ${o.energy}, LRL ${o.lrl}');
}energy -2, angular momentum 4, LRL (3/5, 0) after 40 exact steps: energy -2, LRL (3/5, 0)
factory LeviCivitaKepler.rationalEccentricity(KeplerArith<T> f, {required T k, required T q, required T ds})An orbit whose eccentricity is e = \frac{k^{2} - 1}{k^{2} + 1} for a rational k, chosen so the speed at perihelion is rational: \mu = 2(k^{2} + 1), w = (q, 0), w' = (0, k). Then \sqrt{\frac{1 + e}{1 - e}} = k exactly, so the true anomaly is rational in the eccentric anomaly's half-angle tangent: \tan(\frac{\nu}{2}) = k \tan(\frac{E}{2}).
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
for (final k in [f.fromInt(2), f.fromInt(3, 2)]) {
final o = LeviCivitaKepler<Rational>.rationalEccentricity(f, k: k, q: f.fromInt(1), ds: f.fromInt(1, 8));
final (ax, ay) = o.lrl;
print('k = $k: μ = ${o.mu}, e² = |A|² = ${ax * ax + ay * ay}');
}
}k = 2: μ = 10, e² = |A|² = 9/25 k = 3/2: μ = 13/2, e² = |A|² = 25/169
Properties
final KeplerArith<T> ffinal T mulate final T cint stepsT get energyThe orbital energy h = \frac{|\dot{z}|^{2}}{2} - \frac{\mu}{r} = \frac{2|w'|^{2} - \mu}{|w|^{2}}.
T get angularMomentumThe angular momentum per unit mass, L = x \dot{y} - y \dot{x} = 2(w_{1} p_{2} - w_{2} p_{1}).
(T, T) get lrlThe Laplace-Runge-Lenz vector divided by \mu: A = \frac{\dot{z} \times L \hat{z}}{\mu} - \frac{z}{r}. It points at perihelion and |A| = e.
T get timeThe physical time reached (a rational trapezoid sum of r ds; the only approximated quantity).
Methods
void step()One Cayley step of the clock s, and the trapezoid sum of physical time t + = \Delta s (r_{n} + r_{n + 1}) / 2.
import 'package:riptide_kepler/riptide_kepler.ray';
import 'raya:convert';
import 'package:riptide_precision/riptide_precision.ray';
void main() {
const f = ExactArith();
final o = LeviCivitaKepler<Rational>.rationalEccentricity(f, k: f.fromInt(2), q: f.fromInt(1), ds: f.fromInt(1, 8));
final h0 = o.energy, l0 = o.angularMomentum, a0 = o.lrl;
final bits = <List<num>>[];
var same = true;
for (var i = 1; i <= 10; i++) {
o.step();
same &= o.energy == h0 && o.angularMomentum == l0 && o.lrl == a0;
bits.add([i, o.position.$1.numerator.bitLength]);
}
print('10 steps: energy, momentum and LRL vector exactly unchanged: $same');
print('physical time ≈ ${o.time.toDouble().toStringAsFixed(6)}');
}10 steps: energy, momentum and LRL vector exactly unchanged: true physical time ≈ 2.675958