RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Geometry, physics and speed

Kepler orbits

Kepler orbits turned into a harmonic oscillator and stepped exactly, next to the classic leapfrog and Kepler-equation methods.

import 'package:riptide_kepler/riptide_kepler.ray';
11 classes81 functions and methods46 of 142 documented46 runnable examples

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).

Example
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)]]},
      ]})}');
}
Output
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.

Example
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)]]},
      ]})}');
}
Output
from u = tan(E/2): M = 0.640776548420
classic E − e sin E:  0.640776548420
cross3function
Vec3<T> cross3<T>(KeplerArith<T> f, Vec3<T> a, Vec3<T> b)

x \times v.

Example
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))}');
}
Output
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.

Example
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}');
}
Output
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.

Example
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))}');
}
Output
spread between (1, 0) and (1, 1) = 1/2  (the 45° diagonal)
(3, 4) and (4, −3): 1
parallel: 0.0

Typedefs

Vec3typedef
typedef Vec3<T> = (T, T, T)

Classes

abstract interface class KeplerArith<T>
  • {-}\ \times\ \div on T. Nothing else: no roots, no transcendental functions.

Methods

fromIntmethod
T fromInt(int n, [int d = 1])
Example
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)}');
}
Output
exact n/d: 1/3
f64   n/d: 0.3333333333333333
generic energy, exact: -2
generic energy, f64:   -2.0
addmethod
T add(T a, T b)
Example
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)}');
}
Output
exact a + b: 13/21
f64   a + b: 0.6190476190476191
generic energy, exact: -2
generic energy, f64:   -2.0
submethod
T sub(T a, T b)
Example
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)}');
}
Output
exact a − b: 1/21
f64   a − b: 0.047619047619047616
generic energy, exact: -2
generic energy, f64:   -2.0
mulmethod
T mul(T a, T b)
Example
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)}');
}
Output
exact a × b: 2/21
f64   a × b: 0.09523809523809523
generic energy, exact: -2
generic energy, f64:   -2.0
divmethod
T div(T a, T b)
Example
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)}');
}
Output
exact a ÷ b: 7/6
f64   a ÷ b: 1.1666666666666667
generic energy, exact: -2
generic energy, f64:   -2.0
toDoublemethod
double toDouble(T a)
Example
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)}');
}
Output
exact as a double: 0.3333333333333333
f64   as a double: 0.3333333333333333
generic energy, exact: -2
generic energy, f64:   -2.0
final class ExactArith implements KeplerArith<Rational>

Exact: Rational (riptide_precision), lowest terms, arbitrary size.

Constructors

ExactArithconstructor
const ExactArith()
Example
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)');
}
Output
energy after 6 steps: -2
x = -1.318298 (a 97-bit numerator)

Methods

fromIntmethod
Rational fromInt(int n, [int d = 1])
Example
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)}');
}
Output
fromInt(3, 4) = 3/4
fromInt(-5) = -5
addmethod
Rational add(Rational a, Rational b)
Example
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');
}
Output
add(1/10, 2/10) = 3/10
ten tenths: 1
submethod
Rational sub(Rational a, Rational b)
Example
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)}');
}
Output
sub(1/10, 2/10) = -1/10
mulmethod
Rational mul(Rational a, Rational b)
Example
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)}');
}
Output
mul(1/10, 2/10) = 1/50
divmethod
Rational div(Rational a, Rational b)
Example
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)}');
}
Output
div(1/10, 2/10) = 1/2
toDoublemethod
double toDouble(Rational a)
Example
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))}');
}
Output
toDouble(1/3) = 0.3333333333333333
final class F64Arith implements KeplerArith<double>

IEEE binary64.

Constructors

F64Arithconstructor
const F64Arith()
Example
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}');
}
Output
energy after 1000 f64 steps: -1.9999999999997846
drift from −2 below 1e-12: true

Methods

fromIntmethod
double fromInt(int n, [int d = 1])
Example
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)}');
}
Output
fromInt(3, 4) = 0.75
fromInt(-5) = -5.0
addmethod
double add(double a, double b)
Example
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');
}
Output
add(1/10, 2/10) = 0.30000000000000004
ten tenths: 0.9999999999999999
submethod
double sub(double a, double b)
Example
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)}');
}
Output
sub(1/10, 2/10) = -0.1
mulmethod
double mul(double a, double b)
Example
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)}');
}
Output
mul(1/10, 2/10) = 0.020000000000000004
divmethod
double div(double a, double b)
Example
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)}');
}
Output
div(1/10, 2/10) = 0.5
toDoublemethod
double toDouble(double a)
Example
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))}');
}
Output
toDouble(1/3) = 0.3333333333333333
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

LeapfrogKeplerconstructor
LeapfrogKepler({required this.x, required this.y, required this.vx, required this.vy, required this.mu, required this.dt})
Example
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)}');
}
Output
start: energy -2.0, L 4.0, e = 0.6000000000000001
after 400 steps: energy -1.998928

Properties

xfield
double x
yfield
double y
vxfield
double vx
vyfield
double vy
mufield
final double mu
dtfield
final double dt
stepsfield
int steps
timefield
double time
energygetter
double get energy
double get angularMomentum
lrlgetter
(double, double) get lrl

The Laplace-Runge-Lenz vector divided by \mu.

Methods

stepmethod
void step()
Example
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)');
}
Output
energy after 200 steps: -1.976837 (exact orbit: −2)
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})
Example
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);
}
Output
energy -2.0, L 4.0, LRL (0.6000000000000001, -0.0)
factory LeviCivitaKeplerF64.rationalEccentricity({required double k, required double q, required double ds})
Example
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));
  }
}
Output
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

w1field
double w1
w2field
double w2
p1field
double p1
p2field
double p2
mufield
final double mu
cfield
late final double c
timefield
double time
stepsfield
int steps
radiusgetter
double get radius
positiongetter
(double, double) get position
velocitygetter
(double, double) get velocity
energygetter
double get energy
double get angularMomentum
lrlgetter
(double, double) get lrl

Methods

stepmethod
void step()
Example
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)}');
}
Output
after 300 steps, time 38.2413: energy error below 1e-12
final class KsKepler<T>

3-D Kepler in Kustaanheimo-Stiefel variables, Cayley-stepped, over T.

Constructors

KsKeplerconstructor
KsKepler(this.f, {required List<T> u, required List<T> du, required this.mu, required T ds})
Example
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();
  }
}
Output
position (1.0, 0.0, 0.0), radius 1.0
velocity (0.0, 3.2, 1.6), constraint 0.0, energy -3.5999999999999988

Properties

ffield
final KeplerArith<T> f
mufield
final T mu
cfield
late final T c
stepsfield
int steps
positiongetter
Vec3<T> get position

x = L(u) u.

radiusgetter
T get radius

r = |u|^{2}.

velocitygetter
Vec3<T> get velocity

\dot{x} = 2 L(u) u' / r.

T get constraint

The KS bilinear constraint u4 u1' - u_{3} u2' + u2 u3' - u_{1} u4' (zero on physical states).

energygetter
T get energy
timegetter
T get time

Methods

stepmethod
void step()
Example
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}');
}
Output
6 exact steps: energy true, L true, LRL true, constraint 0
final class Multivector<T>

A multivector of G3 over T.

Constructors

Multivectorconstructor
Multivector(this.f, List<T> c)
Example
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}');
}
Output
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)
Example
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}');
}
Output
zero: [0, 0, 0, 0, 0, 0, 0, 0]
3 + 4e12 times its reverse: 25

Static

e1constant
static const e1 = 1

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

e2constant
static const e2 = 2

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

e3constant
static const e3 = 4

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

e12constant
static const e12 = 3

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

e13constant
static const e13 = 5

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

e23constant
static const e23 = 6

E1, e2, e3, e12, e13, e23, e123 are bitmaps 1, 2, 4, 3, 5, 6, 7.

Properties

ffield
final KeplerArith<T> f
cfield
final List<T> c
reversegetter
Multivector<T> get reverse

The reverse: grades 2 and 3 change sign.

vectorgetter
Vec3<T> get vector
scalargetter
T get scalar

Methods

grademethod
Multivector<T> grade(int g)

The grade-g part.

Example
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}');
  }
}
Output
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

operator *operator
Multivector<T> operator *(Multivector<T> o)
Example
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}');
}
Output
R e1 R̃ = (0, -2, 0)
R R̃ = 2
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

SpinorKeplerconstructor
SpinorKepler(this.f, {required Multivector<T> u, required Multivector<T> du, required this.mu, required T ds})
Example
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();
  }
}
Output
position (1.0, 0.0, 0.0), radius 1.0, gauge 0.0
energy -7.5

Properties

ffield
final KeplerArith<T> f
mufield
final T mu
cfield
late final T c
stepsfield
int steps
positiongetter
Vec3<T> get position

x = U e1 \tilde{U} (a vector).

radiusgetter
T get radius

r = U \tilde{U} (a scalar: the quadrance of the spinor).

velocitygetter
Vec3<T> get velocity

\dot{x} = 2⟨U' e1 \tilde{U}⟩₁ / r.

gaugegetter
T get gauge

The gauge ⟨U' e1 \tilde{U}⟩₃ (zero on physical states).

energygetter
T get energy
timegetter
T get time

Methods

stepmethod
void step()
Example
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)}');
}
Output
after 5 exact steps: energy -2 (start -2), gauge 0
position x ≈ -0.147494
final class LogHKepler<T>

Planar Kepler by the logarithmic-Hamiltonian leapfrog (drift-kick-drift), over T (exact or f64).

Constructors

LogHKeplerconstructor
LogHKepler(this.f, {required T x, required T y, required T vx, required T vy, required this.mu, required T ds})
Example
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))}');
}
Output
angular momentum 4, energy residual at h = −2: 0

Properties

ffield
final KeplerArith<T> f
mufield
final T mu
bfield
late T b
stepsfield
int steps
positiongetter
(T, T) get position
velocitygetter
(T, T) get velocity
timegetter
T get time
T get angularMomentum

x \dot{y} - y \dot{x}.

Methods

stepmethod
void step()
Example
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)}');
}
Output
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).

Example
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)}');
}
Output
largest |residual| over 150 steps: 1.85e-13
final class LogHKeplerF64

LogHKepler in plain doubles, for honest timing.

Constructors

LogHKeplerF64constructor
LogHKeplerF64({required this.x, required this.y, required this.vx, required this.vy, required this.mu, required this.ds, required this.b})
Example
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)})');
}
Output
after 1200 steps: time 15.4025, position (0.6095, -1.0733)

Properties

xfield
double x
yfield
double y
vxfield
double vx
vyfield
double vy
timefield
double time
mufield
final double mu
dsfield
final double ds
bfield
final double b
stepsfield
int steps

Methods

stepmethod
void step()
Example
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)}');
}
Output
time per step: smallest 0.0200, largest 0.0717
final class LeviCivitaKepler<T>

A planar Kepler orbit in Levi-Civita variables, stepped by Cayley maps over T (exact Rational or f64).

Constructors

LeviCivitaKeplerconstructor
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.

Example
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}');
}
Output
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}).

Example
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}');
  }
}
Output
k = 2: μ = 10, e² = |A|² = 9/25
k = 3/2: μ = 13/2, e² = |A|² = 25/169

Properties

ffield
final KeplerArith<T> f
mufield
final T mu
cfield
late final T c
stepsfield
int steps
positiongetter
(T, T) get position

The position z = w^{2} = (w_{1}^{2} - w_{2}^{2}, 2 w1 w2).

radiusgetter
T get radius

The distance to the centre, r = |w|^{2}: a quadrance, no root.

velocitygetter
(T, T) get velocity

The velocity \dot{z} = 2 w w' / |w|^{2} (complex product).

energygetter
T get energy

The orbital energy h = \frac{|\dot{z}|^{2}}{2} - \frac{\mu}{r} = \frac{2|w'|^{2} - \mu}{|w|^{2}}.

T get angularMomentum

The angular momentum per unit mass, L = x \dot{y} - y \dot{x} = 2(w_{1} p_{2} - w_{2} p_{1}).

lrlgetter
(T, T) get lrl

The 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.

timegetter
T get time

The physical time reached (a rational trapezoid sum of r ds; the only approximated quantity).

Methods

stepmethod
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.

Example
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)}');
}
Output
10 steps: energy, momentum and LRL vector exactly unchanged: true
physical time ≈ 2.675958