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

Quantum mechanics

The Schrödinger equation stepped exactly over the Gaussian rationals, so probability is conserved exactly, rational SU(2) spin rotors and entanglement.

import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
32 classes248 functions and methods210 of 393 documented238 runnable examples

Overview

Quantum mechanics two ways: the Cayley (Crank-Nicolson) Schrödinger step and rational SU(2) spin rotors, exactly unitary over the Gaussian rationals, next to the classic exp(−iHt), e^{ikx} and \exp(-i\sigma \cdot B \frac{t}{2}).

Entanglement: tight-binding chains known by the quadrances of their hoppings (SSH, Krawtchouk, dimers; Sturm spectra, exact zero modes, Cayley stepping), chiral-entangled pairs, Bell/CHSH by spreads, rational SU(2) rotors, and the random-matrix ramp.

The particle in a box: densities by spread polynomials at rational spreads, exact Cayley phases on a rational clock, exact integer wave packets, the discrete box's spectrum as four times a spread.

Functions and constants

Functions

spreadOffunction
T spreadOf<T>(KeplerArith<T> f, Line<T> a, Line<T> b)

The spread between two lines, \frac{(a \times b)^{2}}{Q_{a} Q_{b}}: 0 parallel, 1 perpendicular.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The spread between two lines through the origin, as a rational number.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a = Line(r(1), r(0)), b = Line(r(3), r(4)), c = Line(r(-4), r(3));
  print('s(x-axis, (3, 4)) = ${spreadOf(f, a, b)}');
  print('s((3, 4), (-4, 3)) = ${spreadOf(f, b, c)} (perpendicular)');
  print('s((3, 4), (6, 8)) = ${spreadOf(f, b, Line(r(6), r(8)))} (the same line)');
}
Output
s(x-axis, (3, 4)) = 16/25
s((3, 4), (-4, 3)) = 1 (perpendicular)
s((3, 4), (6, 8)) = 0 (the same line)
double classicPolarisationCorrelation(double alpha, double beta)

The classic Bell correlation, \cos 2(\alpha - \beta), angles in radians (Transcendental).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The textbook correlation cos 2(α − β) next to the spread form 1 − 2s for the same two polarisers.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final alpha = 0.0, beta = math.atan2(4, 3);
  final classic = classicPolarisationCorrelation(alpha, beta);
  final exact = SpreadBell(f).correlation(Line(r(1), r(0)), Line(r(3), r(4)));
  print('classic cos 2(α − β) = $classic');
  print('spread 1 − 2s        = $exact = ${exact.toDouble()}');
  print('difference           = ${(classic - exact.toDouble()).abs()}');
}
Output
classic cos 2(α − β) = -0.2799999999999999
spread 1 − 2s        = -7/25 = -0.28
difference           = 1.1102230246251565e-16
classicChshfunction
double classicChsh(double a, double a2, double b, double b2)

The classic CHSH sum at angles (radians).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // CHSH with angles, cos and sin: the Tsirelson setting 0°, 45°, 22.5°, 67.5° reaches 2√2 in floating point.
  final s = classicChsh(0, math.pi / 4, math.pi / 8, 3 * math.pi / 8);
  print('S(0°, 45°, 22.5°, 67.5°) = $s');
  print('2√2                      = ${2 * math.sqrt(2)}');
  print('local hidden variables reach at most 2; S − 2 = ${s - 2}');
}
Output
S(0°, 45°, 22.5°, 67.5°) = 2.82842712474619
2√2                      = 2.8284271247461903
local hidden variables reach at most 2; S − 2 = 0.8284271247461898
double classicLineAngle(double dx, double dy)

The classic way names a polariser line by its angle, atan2(dy, dx) (Transcendental).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic way names a polariser line by an angle, atan2(dy, dx).
  for (final (dx, dy) in [(1.0, 0.0), (1.0, 1.0), (3.0, 4.0), (0.0, 1.0)]) {
    final a = classicLineAngle(dx, dy);
    print('line ($dx, $dy): angle = $a rad = ${a * 180 / math.pi}°');
  }
}
Output
line (1.0, 0.0): angle = 0.0 rad = 0.0°
line (1.0, 1.0): angle = 0.7853981633974483 rad = 45.0°
line (3.0, 4.0): angle = 0.9272952180016122 rad = 53.13010235415598°
line (0.0, 1.0): angle = 1.5707963267948966 rad = 90.0°
double classicSameProbability(double alpha, double beta)

The classic coincidence probability P(same) = \cos^{2}(\alpha - \beta) for the same pair (Transcendental).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // P(same) = cos²(α − β) by cosine, against the spread form 1 − s for the line (3, 4) at angle atan2(4, 3).
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final p = classicSameProbability(0, math.atan2(4, 3));
  final s = spreadOf(const ExactArith(), Line(r(1), r(0)), Line(r(3), r(4)));
  print('cos²(α − β) = ${p.toStringAsFixed(12)}');
  print('1 − s       = ${Rational.one - s} = ${(Rational.one - s).toDouble()}');
}
Output
cos²(α − β) = 0.360000000000
1 − s       = 9/25 = 0.36
List<T> spreadPolynomials<T>(KeplerArith<T> f, T s, int upTo)

S₀(s) … S_upTo, Wildberger's spread polynomials at s, by the three-term recurrence S_{n+1} = 2(1 - 2s)S_{n} - S_{n-1} + 2s.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // S₀ … S₆ at s = 1/4 (that is sin²30°): Sₙ(sin²α) = sin²(nα), so these are sin² of 0°, 30°, …, 180°.
  const f = ExactArith();
  final s = spreadPolynomials(f, Rational.fromInt(1, 4), 6);
  print('S_n(1/4), n = 0…6: ${s.join(', ')}');
}
Output
S_n(1/4), n = 0…6: 0, 1/4, 3/4, 1, 3/4, 1/4, 0
T spreadPolynomial<T>(KeplerArith<T> f, int n, T s)

S_{n}(s).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // One spread polynomial: S₃(s) = s(3 − 4s)², at a few rational spreads.
  const f = ExactArith();
  for (final (n, d) in [(1, 4), (1, 2), (3, 4), (1, 3)]) {
    final s = Rational.fromInt(n, d);
    print('S_3($s) = ${spreadPolynomial(f, 3, s)}');
  }
}
Output
S_3(1/4) = 1
S_3(1/2) = 1/2
S_3(3/4) = 0
S_3(1/3) = 25/27
int boxEnergyRatio(int n)

The energy of state n in units of E_{1} = \frac{\pi^{2}\hbar^{2}}{2mL^{2}}: exactly n^{2}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Particle in a box: Eₙ/E₁ = n², integers; π² stays inside the unit E₁.
  print([for (var n = 1; n <= 8; n++) 'E$n/E1 = ${boxEnergyRatio(n)}'].join(', '));
}
Output
E1/E1 = 1, E2/E1 = 4, E3/E1 = 9, E4/E1 = 16, E5/E1 = 25, E6/E1 = 36, E7/E1 = 49, E8/E1 = 64
cayleyTickfunction
Cx<T> cayleyTick<T>(CxArith<T> c, T tau)

e^{-i\theta} as the Cayley point \frac{1 - i\tau}{1 + i\tau} = \frac{(1 - \tau^{2}) - 2i\tau}{1 + \tau^{2}}, \theta = 2 \cdot \operatorname{atan} \tau; |z|^{2} = 1 exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // e^{−iθ} as the rational point (1 − iτ)/(1 + iτ): exactly on the unit circle.
  const c = CxArith<Rational>(ExactArith());
  for (final t in [Rational.fromInt(1, 2), Rational.fromInt(1, 3), Rational.fromInt(70, 169)]) {
    final z = cayleyTick(c, t);
    print('τ = $t: z = ${z.re} + (${z.im})i, |z|² = ${c.norm2(z)}');
  }
}
Output
τ = 1/2: z = 3/5 + (-4/5)i, |z|² = 1
τ = 1/3: z = 4/5 + (-3/5)i, |z|² = 1
τ = 70/169: z = 23661/33461 + (-23660/33461)i, |z|² = 1
powCxfunction
Cx<T> powCx<T>(CxArith<T> c, Cx<T> z, int k)

z^{k} (k \ge 0) by squaring.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // zᵏ by squaring: powers of a rational unit point stay exactly on the circle.
  const c = CxArith<Rational>(ExactArith());
  final z = cayleyTick(c, Rational.fromInt(1, 2)); // (3 − 4i)/5
  for (final k in [1, 2, 3, 10]) {
    final p = powCx(c, z, k);
    print('z^$k = ${p.re} + (${p.im})i, |z^$k|² = ${c.norm2(p)}');
  }
}
Output
z^1 = 3/5 + (-4/5)i, |z^1|² = 1
z^2 = -7/25 + (-24/25)i, |z^2|² = 1
z^3 = -117/125 + (-44/125)i, |z^3|² = 1
z^10 = -9653287/9765625 + (-1476984/9765625)i, |z^10|² = 1
minusIPowfunction
Cx<T> minusIPow<T>(CxArith<T> c, int k)

(-i)^{k}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // (−i)^k cycles 1, −i, −1, i: the quarter turns of the box's clock.
  const c = CxArith<Rational>(ExactArith());
  for (var k = 0; k < 6; k++) {
    final z = minusIPow(c, k);
    print('(−i)^$k = ${z.re} + (${z.im})i');
  }
}
Output
(−i)^0 = 1 + (0)i
(−i)^1 = 0 + (-1)i
(−i)^2 = -1 + (0)i
(−i)^3 = 0 + (1)i
(−i)^4 = 1 + (0)i
(−i)^5 = 0 + (-1)i
T discreteBoxDeterminant<T>(KeplerArith<T> f, int n, T sigma)

\det(T_{N} - 4\sigma I) for T_{N} = \operatorname{tridiag}(-1, 2, -1) of size n: p_{k} = (2 - 4\sigma)p_{k-1} - p_{k-2}, p_{0} = 1, p_{-1} = 0.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // det(T − 4σI) for T = tridiag(−1, 2, −1): zero exactly at the eigenvalues 4σ of the discrete box.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  // N = 2: T = [[2, −1], [−1, 2]], eigenvalues 1 and 3, so σ = 1/4 and 3/4.
  for (final s in [r(0), r(1, 4), r(1, 2), r(3, 4)]) print('N = 2, σ = $s: det = ${discreteBoxDeterminant(f, 2, s)}');
  // N = 3: eigenvalues 2 − √2, 2, 2 + √2; the rational one is 4σ = 2.
  print('N = 3, σ = 1/2: det = ${discreteBoxDeterminant(f, 3, r(1, 2))}');
}
Output
N = 2, σ = 0: det = 3
N = 2, σ = 1/4: det = 0
N = 2, σ = 1/2: det = -1
N = 2, σ = 3/4: det = 0
N = 3, σ = 1/2: det = 0
(T, T) discreteBoxIdentity<T>(KeplerArith<T> f, int n, T sigma)

Both sides of \det(T - 4\sigma I)^{2} \cdot 4\sigma(1 - \sigma) = S_{N + 1}(4\sigma(1 - \sigma)) at sigma.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // det(T − 4σI)²·4σ(1 − σ) = S_{N+1}(4σ(1 − σ)): both sides, exactly, at rational σ.
  const f = ExactArith();
  for (final (n, s) in [(3, Rational.fromInt(1, 5)), (5, Rational.fromInt(2, 7)), (8, Rational.fromInt(5, 9))]) {
    final (lhs, rhs) = discreteBoxIdentity(f, n, s);
    print('N = $n, σ = $s: left = $lhs, right = $rhs, equal: ${lhs == rhs}');
  }
}
Output
N = 3, σ = 1/5: left = 112896/390625, right = 112896/390625, equal: true
N = 5, σ = 2/7: left = 2998438560/13841287201, right = 2998438560/13841287201, equal: true
N = 8, σ = 5/9: left = 43534485926457680/150094635296999121, right = 43534485926457680/150094635296999121, equal: true
List<T> discreteBoxEigenvalues<T>(KeplerArith<T> f, int n, T sigma1)

The discrete box's eigenvalues of T, 4 \cdot S_{k}(\sigma_{1}) for k = 1 \cdots N, from its smallest spread \sigma_{1} = \sin^{2}(\frac{\pi}{2(N + 1)}) (irrational in general, so the classic side supplies it; the spread polynomials do the rest).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The discrete box's spectrum 4·Sₖ(σ₁) from one spread, with σ₁ = sin²(π/2(N+1)) supplied by the classic side.
  const f = F64Arith();
  const n = 6;
  final ev = discreteBoxEigenvalues(f, n, discreteBoxSigma1(n));
  print('eigenvalues of tridiag(−1, 2, −1), N = $n:');
  print(ev.map((e) => e.toStringAsFixed(10)).join(', '));
  print('sum = ${ev.reduce((a, b) => a + b).toStringAsFixed(10)} (the trace is 2N = 12)');
}
Output
eigenvalues of tridiag(−1, 2, −1), N = 6:
0.1980622642, 0.7530203963, 1.5549581321, 2.4450418679, 3.2469796037, 3.8019377358
sum = 12.0000000000 (the trace is 2N = 12)
boxSampleXfunction
double boxSampleX(int num, int den)

\frac{x}{L} of the rational sample with \tan(\frac{\pi x}{2L}) = num/den (den 0: the right wall). Drawing only.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Where the rational sample with tan(πx/2L) = num/den sits, for drawing: x/L = (2/π)·atan(num/den).
  final s = BoxSamples(4);
  for (var i = 0; i < s.length; i++) {
    final (a, b) = s.tangent(i);
    print('sample $i: tan = $a/$b, x/L = ${boxSampleX(a, b).toStringAsFixed(6)}');
  }
}
Output
sample 0: tan = 0/4, x/L = 0.000000
sample 1: tan = 1/4, x/L = 0.155958
sample 2: tan = 2/4, x/L = 0.295167
sample 3: tan = 3/4, x/L = 0.409666
sample 4: tan = 4/4, x/L = 0.500000
sample 5: tan = 4/3, x/L = 0.590334
sample 6: tan = 4/2, x/L = 0.704833
sample 7: tan = 4/1, x/L = 0.844042
sample 8: tan = 4/0, x/L = 1.000000
double boxInstantAngle(int quarter, double tau)

E_{1}t of the clock instant (-i)^{q} \cdot C(\tau): \frac{q\pi}{2} + 2 \cdot \operatorname{atan} \tau.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The clock instant (−i)^q·C(τ) stands for E₁t = qπ/2 + 2·atan τ: the drawing time of an exact tick.
  final clock = BoxClock(ticksPerQuarter: 8);
  for (var k = 0; k < 10; k++) {
    final (a, b) = clock.tau;
    print('quarter ${clock.quarter}, tick ${clock.tick}: τ = $a/$b, E1·t = ${boxInstantAngle(clock.quarter, a / b).toStringAsFixed(6)}');
    clock.advance();
  }
}
Output
quarter 0, tick 4: τ = 0/1352, E1·t = 0.000000
quarter 0, tick 5: τ = 140/1352, E1·t = 0.206365
quarter 0, tick 6: τ = 280/1352, E1·t = 0.408427
quarter 0, tick 7: τ = 420/1352, E1·t = 0.602399
quarter 1, tick 0: τ = -560/1352, E1·t = 0.785419
quarter 1, tick 1: τ = -420/1352, E1·t = 0.968398
quarter 1, tick 2: τ = -280/1352, E1·t = 1.162369
quarter 1, tick 3: τ = -140/1352, E1·t = 1.364431
quarter 1, tick 4: τ = 0/1352, E1·t = 1.570796
quarter 1, tick 5: τ = 140/1352, E1·t = 1.777161
double discreteBoxSigma1(int n)

The discrete box's smallest spread \sigma_{1} = \sin^{2}(\frac{\pi}{2(N + 1)}) (irrational for most N; the spread polynomials take it from here).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // σ₁ = sin²(π/(2(N + 1))): the smallest spread of the N-point box. N = 1 and N = 2 are rational.
  for (final n in [1, 2, 3, 5, 10]) print('N = $n: σ1 = ${discreteBoxSigma1(n).toStringAsFixed(12)}');
}
Output
N = 1: σ1 = 0.500000000000
N = 2: σ1 = 0.250000000000
N = 3: σ1 = 0.146446609407
N = 5: σ1 = 0.066987298108
N = 10: σ1 = 0.020253513193
double classicBoxDensity(List<double> re, List<double> im, double x, double phase)

The textbook: L|\psi(x, t)|^{2} for \psi = \sum c_{n} \sqrt{2} \sin(n\pi x) e^{-in^{2}E_{1}t} / \sqrt{\Sigma|c_{n}|^{2}}, at \frac{x}{L} = x, E_{1}t = phase. re, im: c_{n} (index n - 1).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The textbook density of (φ₁ + φ₂)/√2 at E₁t = 0 and a quarter period later, by sin and cos.
  final re = [1.0, 1.0], im = [0.0, 0.0];
  final xs = [for (var i = 0; i <= 50; i++) i / 50];
  final d0 = [for (final x in xs) classicBoxDensity(re, im, x, 0)];
  final d1 = [for (final x in xs) classicBoxDensity(re, im, x, math.pi / 3)];
  print('L|ψ|² at x = L/4, t = 0: ${d0[12].toStringAsFixed(9)}');
  print('L|ψ|² at x = L/4, E1·t = π/3: ${d1[12].toStringAsFixed(9)}');
}
Output
L|ψ|² at x = L/4, t = 0: 2.831054708
L|ψ|² at x = L/4, E1·t = π/3: 0.098269474
double classicStationaryDensity(int n, double x)

The textbook stationary density 2 \sin^{2}(\frac{n\pi x}{L}) (times L).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The stationary states' densities 2 sin²(nπx/L).
  print('n = 2 at x = L/4: ${classicStationaryDensity(2, 0.25).toStringAsFixed(12)} (exactly 2)');
  print('n = 3 at x = L/2: ${classicStationaryDensity(3, 0.5).toStringAsFixed(12)} (exactly 2)');
}
Output
n = 2 at x = L/4: 2.000000000000 (exactly 2)
n = 3 at x = L/2: 2.000000000000 (exactly 2)
double classicLeftHalf(List<double> re, List<double> im, double phase)

The textbook probability in the left half: \frac{1}{2} + (\frac{1}{\pi})\sum_{n \ne m} \operatorname{Re}(c_{n} \bar{c}_{m} e^{-i(n^{2} - m^{2})E_{1}t}) I_{nm}, the integral done with cos (as one does).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The textbook chance of finding (φ₁ + φ₂)/√2 in the left half, as it sloshes from side to side, by cos.
  final re = [1.0, 1.0], im = [0.0, 0.0];
  for (final k in [0, 1, 2, 3]) {
    print('E1·t = $k·π/6: P(left) = ${classicLeftHalf(re, im, k * math.pi / 6).toStringAsFixed(9)}');
  }
  print('the exact rational way: κ at t = 0 is ${BoxState(const CxArith<Rational>(ExactArith()), [Cx(Rational.one, Rational.zero), Cx(Rational.one, Rational.zero)]).leftKappa()}, so P = 1/2 + κ/π');
}
Output
E1·t = 0·π/6: P(left) = 0.924413182
E1·t = 1·π/6: P(left) = 0.500000000
E1·t = 2·π/6: P(left) = 0.075586818
E1·t = 3·π/6: P(left) = 0.500000000
the exact rational way: κ at t = 0 is 4/3, so P = 1/2 + κ/π
bigRatiofunction
double bigRatio(BigInt a, BigInt b)

\frac{a}{b} as a double for integers of any size (shifts both to 64 bits; the platform edge, for drawing).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // a/b for integers far beyond double range: both are shifted to 62 bits first, then rescaled by powers of 2.
  final a = BigInt.from(3).pow(700), b = BigInt.from(2).pow(1100);
  print('3^700 / 2^1100 ≈ ${bigRatio(a, b)}');
  print('a.toDouble() alone: ${a.toDouble()} (overflows)');
  print('1/3 = ${bigRatio(BigInt.one, BigInt.from(3))}');
}
Output
3^700 / 2^1100 ≈ 711.0220569369853
a.toDouble() alone: Infinity (overflows)
1/3 = 0.3333333333333333
binomialfunction
BigInt binomial(int n, int k)

The binomial coefficient C(n, k).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // C(n, k) in big integers: the weights of the box's integer wave packets.
  print('row 8: ${[for (var k = 0; k <= 8; k++) binomial(8, k)].join(', ')}');
  print('C(100, 50) = ${binomial(100, 50)}');
  print('C(5, 7) = ${binomial(5, 7)}');
}
Output
row 8: 1, 8, 28, 56, 70, 56, 28, 8, 1
C(100, 50) = 100891344545564193334812497256
C(5, 7) = 0
boxPacketfunction
List<GInt> boxPacket({required int centre, required int spread, required int p, required int q, bool leftward = false})

A wave packet with integer coefficients: binomial weights C(K, n - n_{0} + \frac{K}{2}) around state centre (spread = K, even), times e^{∓inθ₀} for a packet at \theta_{0} = \frac{\pi x_{0}}{L}, \tan(\frac{\theta_{0}}{2}) = p/q (moving right, or left when leftward): g_{n} = C(K, \cdot)(q \mp ip)^{2n}(q^{2} + p^{2})^{N - n}, N the top state. Index n - 1; states below 1 are dropped.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // An integer wave packet for the box: binomial weights around state 8, phase for a packet at tan(θ₀/2) = 1/2.
  final g = boxPacket(centre: 8, spread: 6, p: 1, q: 2);
  for (var n = 1; n <= g.length; n++) {
    if (!(g[n - 1] == GInt.zero)) print('g_$n = ${g[n - 1]}');
  }
  final w = ExactBoxWave(g, m: 16);
  final d = w.densities(0, 0, 1);
  print('⟨E⟩/E1 = ${w.energy} (exact), Σ|g|² has ${w.norm2.bitLength} bits');
}
Output
g_5 = -3703125 + 48687500i
g_6 = 220368750 + 193050000i
g_7 = 716653125 + -151162500i
g_8 = 412082500 + -885360000i
g_9 = -345778875 + -645661500i
g_10 = -289598610 + -44309520i
g_11 = -34867797 + 34182196i
⟨E⟩/E1 = 713/11 (exact), Σ|g|² has 61 bits
(List<GInt>, BigInt) phaseCoefficients(List<GInt> g, int top, int quarter, int a, int b)

G_{n} = g_{n} \cdot z_{1}^{n^{2}} \cdot h^{N^{2}}, z_{1} = (-i)^{q} (B − iA)²/h, h = A^{2} + B^{2} (\tau = \frac{A}{B} in lowest terms), so every G_{n} is a Gaussian integer over the one denominator h^{N^{2}} (returned second). N = top.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Gₙ = gₙ·z₁^{n²}·h^{N²}: Gaussian integers over one denominator, here for τ = 1/2, z₁ = (2 − i)²/5.
  final g = [GInt.of(1), GInt.of(1), GInt.of(1)];
  final (gp, h) = phaseCoefficients(g, 3, 0, 1, 2);
  print('G = ${gp.join(', ')}');
  print('denominator h^{N²} = $h');
  for (var n = 1; n <= 3; n++) print('|G_$n|² = |g_$n|²·h^{2N²}: ${gp[n - 1].norm2 == g[n - 1].norm2 * h * h}');
}
Output
G = 1171875 + -1562500i, -1646875 + 1050000i, -922077 + -1721764i
denominator h^{N²} = 1953125
|G_1|² = |g_1|²·h^{2N²}: true
|G_2|² = |g_2|²·h^{2N²}: true
|G_3|² = |g_3|²·h^{2N²}: true
List<BigInt> spreadNumerators(int a, int d, int upTo)

Spread-polynomial numerators: with \sigma = a/d, S_{k}(\sigma) = \frac{P_{k}}{d^{k}} where P_{0} = 0, P_{1} = a and P_{k+1} = 2(d - 2a)P_{k} - d^{2}P_{k-1} + 2a \cdot d^{k} (the spread recurrence times d^{k+1}: integers, no division).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Spread polynomials at σ = a/d in integers: Sₖ(σ) = Pₖ/dᵏ with no division at all.
  final p = spreadNumerators(1, 4, 6);
  print('P_k for σ = 1/4: ${p.join(', ')}');
  print('S_k = P_k / 4^k: ${[for (var k = 0; k <= 6; k++) Rational(p[k], BigInt.from(4).pow(k))].join(', ')}');
  print('matches spreadPolynomials: ${[for (var k = 0; k <= 6; k++) Rational(p[k], BigInt.from(4).pow(k))].join() == spreadPolynomials(const ExactArith(), Rational.fromInt(1, 4), 6).join()}');
}
Output
P_k for σ = 1/4: 0, 1, 12, 64, 192, 256, 0
S_k = P_k / 4^k: 0, 1/4, 3/4, 1, 3/4, 1/4, 0
matches spreadPolynomials: true
gridPacketfunction
List<GInt> gridPacket({required int n, required int centre, required int halfWidth, required int p, required int q})

A grid wave packet in Gaussian integers: the bump (w^{2} - d^{2})^{2} for |d| < w around centre, times the rational phase (\frac{(q + ip)^{2}}{q^{2} + p^{2}})^{j} per site (momentum \approx 2 \cdot \frac{\operatorname{atan}(\frac{p}{q})}{dx}, to the right), over the common denominator (q^{2} + p^{2})^{n}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A grid wave packet in Gaussian integers: the bump (w² − d²)² times the rational phase ((q + ip)²/h)^j.
  final psi = gridPacket(n: 20, centre: 8, halfWidth: 5, p: 1, q: 3);
  print('ψ_8 = ${psi[8]}');
  print('ψ_2 = ${psi[2]} (outside the bump)');
  var total = BigInt.zero;
  for (final z in psi) total += z.norm2;
  print('Σ|ψ|² = $total');
}
Output
ψ_8 = 26373280000000000000000 + -56663040000000000000000i
ψ_2 = 0 + 0i (outside the bump)
Σ|ψ|² = 15873330000000000000000000000000000000000000000
List<double> classicKrawtchoukProbabilities(int n, double lambdaZ)

Classic Krawtchouk transfer: P_{n}(z) = C(N, n) \sin^{2n}(\frac{\lambda z}{2}) \cos^{2(N - n)}(\frac{\lambda z}{2}) (Transcendental).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic transfer probabilities C(N, n) sin^{2n}(λz/2) cos^{2(N−n)}(λz/2); at λz = π all light is in site N.
  final p = classicKrawtchoukProbabilities(4, math.pi);
  print('λz = π: ${p.map((x) => x.toStringAsExponential(3)).join(', ')}');
  final q = classicKrawtchoukProbabilities(4, math.pi / 2);
  print('λz = π/2: ${q.map((x) => x.toStringAsFixed(6)).join(', ')}');
  print('exact at s = 1/2: ${KrawtchoukTransfer(const ExactArith(), 4).probabilities(Rational.fromInt(1, 2)).join(', ')}');
}
Output
λz = π: 1.976e-130, 2.108e-97, 8.435e-65, 1.500e-32, 1.000e+0
λz = π/2: 0.062500, 0.250000, 0.375000, 0.250000, 0.062500
exact at s = 1/2: 1/16, 1/4, 3/8, 1/4, 1/16
List<List<double>> classicPairMap(int n, double lambda, double linewidth)

The classic correlation map: modes from ClassicChain (Jacobi, roots), the same line shape.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The classic pair map (Jacobi modes with roots) next to the exact one; they agree to rounding.
  final c = classicPairMap(4, 1.0, 0.5);
  final e = ChiralPairs(const ExactArith(), n: 4, lambda: Rational.one, linewidth: Rational.fromInt(1, 2)).map;
  var worst = 0.0;
  for (var i = 0; i <= 4; i++) {
    for (var j = 0; j <= 4; j++) {
      final d = (c[i][j] - e[i][j].toDouble()).abs();
      if (d > worst) worst = d;
    }
  }
  print('largest |classic − exact| over the 25 entries: ${worst < 1e-12 ? 'below 1e-12' : worst}');
  print('classic anti-diagonal: ${[for (var i = 0; i <= 4; i++) c[i][4 - i].toStringAsFixed(6)].join(', ')}');
}
Output
largest |classic − exact| over the 25 entries: below 1e-12
classic anti-diagonal: 0.084831, 0.150810, 0.506888, 0.150810, 0.084831
bicgstabfunction
int bicgstab(void Function(Float64List x, Float64List y) apply, Float64List b, Float64List x, {double tolerance = 1e-15, int maxIterations = 100})

Solves M x = b by BiCGSTAB, M given by apply (y = M x); x holds the start and receives the solution. Returns the iterations. Only {+}\ {-}\ \times\ \div.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // BiCGSTAB solves M x = b with M given only as a function y = M x: here a 4 × 4 nonsymmetric tridiagonal matrix.
  void apply(Float64List x, Float64List y) {
    for (var i = 0; i < 4; i++) {
      y[i] = 4 * x[i] + (i > 0 ? -x[i - 1] : 0) + (i < 3 ? 2 * x[i + 1] : 0);
    }
  }
  final b = Float64List.fromList([1, 2, 3, 4]), x = Float64List(4);
  final it = bicgstab(apply, b, x);
  final r = Float64List(4);
  apply(x, r);
  print('iterations: $it');
  print('x = ${x.map((v) => v.toStringAsFixed(12)).join(', ')}');
  print('M x = ${r.map((v) => v.toStringAsFixed(12)).join(', ')}');
}
Output
iterations: 4
x = 0.067415730337, 0.365168539326, 0.303370786517, 1.075842696629
M x = 1.000000000000, 2.000000000000, 3.000000000000, 4.000000000000
void valleyHallMass(Honeycomb h, double m, {required int x1, required int y0, required int y1})

The valley-Hall mass: +m on A and -m on B above a Z-shaped wall, the opposite below. The wall runs along n_{2} = y_{0} for n_{1} < x_{1}, up along n_{1} = x_{1} (the a_{2} direction: a 60^{\circ} turn and a 120^{\circ} turn, both zigzag), along n_{2} = y_{1} after.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // The valley-Hall mass: +m on A, −m on B above a Z-shaped wall, the opposite below.
  final h = Honeycomb(8, 6);
  valleyHallMass(h, 0.5, x1: 4, y0: 2, y1: 4);
  print('onsite of A(0,0) = ${h.onsite[h.siteA(0, 0)]}, A(0,4) = ${h.onsite[h.siteA(0, 4)]}, A(6,3) = ${h.onsite[h.siteA(6, 3)]}');
}
Output
onsite of A(0,0) = -0.5, A(0,4) = 0.5, A(6,3) = -0.5
void kekuleRational<T>(Honeycomb h, KeplerArith<T> f, {required T delta0, required T ell, required T a})

The Kekulé (Wu–Hu) texture, rational: \delta t = (\frac{2}{3}) \cdot \operatorname{Re}(\Delta(R) \cdot \omega_{3}^{k + n_{1} - n_{2}}) with \Delta(R) = 2\Delta_{0} (R - a) \cdot \operatorname{conj}(R + a) / (\ell^{2} + N(R - a) + N(R + a)): a vortex at +a (winding +1) and an antivortex at -a (-1) joined by a "Dirac string", magnitude \to \Delta_{0} away from them; a = 0 is a uniform \Delta_{0} (no string). a lies along a_{1} at the torus' middle; everything in Eisenstein numbers, rational.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // The Kekulé texture in Eisenstein numbers (rational): a vortex–antivortex pair joined by a Dirac string.
  final h = Honeycomb(8, 8, torus: true);
  kekuleRational(h, const ExactArith(), delta0: Rational.fromInt(1, 4), ell: Rational.fromInt(2), a: Rational.fromInt(2));
  final g = Honeycomb(8, 8, torus: true);
  kekuleClassic(g, delta0: 0.25, ell: 2, a: 2);
  print('hop range (rational): ${h.hop.reduce((a, b) => a < b ? a : b).toStringAsFixed(6)} … ${h.hop.reduce((a, b) => a > b ? a : b).toStringAsFixed(6)}');
  print('hop range (classic):  ${g.hop.reduce((a, b) => a < b ? a : b).toStringAsFixed(6)} … ${g.hop.reduce((a, b) => a > b ? a : b).toStringAsFixed(6)}');
}
Output
hop range (rational): 0.851852 … 1.135802
hop range (classic):  0.845108 … 1.157326
void kekuleClassic(Honeycomb h, {required double delta0, required double ell, required double a})

The classic Kekulé texture: \Delta = \Delta_{0} \tanh(\frac{|z - a|}{\ell}) \tanh(\frac{|z + a|}{\ell}) e^{i(\arg(z - a) - \arg(z + a))}, positions in Cartesian form with \frac{\sqrt{3}}{2}, phases by cos and sin (Transcendental, Root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // The classic Kekulé texture: tanh magnitudes, atan2 phases, cos, and √3/2 positions.
  final g = Honeycomb(6, 6, torus: true);
  kekuleClassic(g, delta0: 0.3, ell: 1.5, a: 0);
  print('a = 0 (uniform): hops take ${g.hop.map((x) => x.toStringAsFixed(6)).toSet().length} distinct values: ${g.hop.map((x) => x.toStringAsFixed(6)).toSet().join(', ')}');
  final h = Honeycomb(6, 6, torus: true);
  kekuleRational(h, const ExactArith(), delta0: Rational.fromInt(3, 10), ell: Rational.fromInt(3, 2), a: Rational.zero);
  print('rational, a = 0: ${h.hop.map((x) => x.toStringAsFixed(6)).toSet().join(', ')}');
}
Output
a = 0 (uniform): hops take 2 distinct values: 1.200000, 0.900000
rational, a = 0: 1.200000, 0.900000
(double, double) classicValleyPhase(int n1, int n2)

The valley K's phase on cell (n_{1}, n_{2}): e^{2\pi i(n_{1} - n_{2})/3} (classic: cos and sin).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // The valley's phase e^{2πi(n₁ − n₂)/3} by cos and sin, next to the rational stand-in C(7/4)^{n₁ − n₂}.
  for (var k = 0; k < 4; k++) {
    final (cr, ci) = classicValleyPhase(k, 0);
    final (rr, ri) = rationalValleyPhase(k, 0);
    print('n1 − n2 = $k: classic (${cr.toStringAsFixed(6)}, ${ci.toStringAsFixed(6)}), rational (${rr.toStringAsFixed(6)}, ${ri.toStringAsFixed(6)})');
  }
}
Output
n1 − n2 = 0: classic (1.000000, 0.000000), rational (1.000000, 0.000000)
n1 − n2 = 1: classic (-0.500000, 0.866025), rational (-0.507692, 0.861538)
n1 − n2 = 2: classic (-0.500000, -0.866025), rational (-0.484497, -0.874793)
n1 − n2 = 3: classic (1.000000, -0.000000), rational (0.999643, 0.026713)
(double, double) rationalValleyPhase(int n1, int n2)

The rational stand-in: z^{n_{1} - n_{2}} with z = \frac{(1 - \tau^{2}) + 2i\tau}{1 + \tau^{2}}, \tau = \frac{7}{4}, a rational unit complex number whose angle 2 \cdot \operatorname{atan}(\frac{7}{4}) is within 0.4 % of \frac{2\pi}{3} (the cube roots of unity are not in \mathbb{Q}(i)).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';
import 'raya:math' as math;

void main() {
  // The rational stand-in z^{n₁ − n₂}, z = C(7/4) = (−33 + 56i)/65: |z| = 1 and its angle is within 0.4% of 2π/3.
  final (r1, i1) = rationalValleyPhase(1, 0);
  print('z = (${r1}, ${i1}) = (−33/65, 56/65): ${r1 == -33 / 65 && i1 == 56 / 65}');
  print('angle 2·atan(7/4) = ${(2 * math.atan(7 / 4)).toStringAsFixed(6)}, 2π/3 = ${(2 * math.pi / 3).toStringAsFixed(6)}');
  final pts = [for (var k = -3; k <= 3; k++) () { final (x, y) = rationalValleyPhase(k, 0); return [x, y, '$k']; }()];
}
Output
z = (-0.5076923076923077, 0.8615384615384616) = (−33/65, 56/65): true
angle 2·atan(7/4) = 2.103300, 2π/3 = 2.094395
List<double> symmetricEigenvalues(List<List<double>> m0)

Dense eigenvalues of a real symmetric matrix by Jacobi rotations (classic, roots): small spectra in tests.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Jacobi rotations for a small real symmetric matrix (classic: roots per rotation).
  final m = [[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 2.0]];
  final e = symmetricEigenvalues(m);
  print('eigenvalues: ${e.map((x) => x.toStringAsFixed(12)).join(', ')}');
  print('2 − √2, 2, 2 + √2 = 0.585786437627, 2.000000000000, 3.414213562373');
}
Output
eigenvalues: 0.585786437627, 2.000000000000, 3.414213562373
2 − √2, 2, 2 + √2 = 0.585786437627, 2.000000000000, 3.414213562373
cayleyRotorfunction
Mat2<T> cayleyRotor<T>(CxArith<T> c, T ax, T ay, T az)

The Cayley rotor of a = (ax, ay, az).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The rational SU(2) rotor U(a) = ((1 − |a|²)I − 2i a·σ)/(1 + |a|²); |a| = 1 is an exact Pauli gate.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  String s(Cx<Rational> z) => '${z.re} + (${z.im})i';
  final x = cayleyRotor(c, r(1), r(0), r(0));
  print('a = (1, 0, 0): U = [[${s(x.m11)}, ${s(x.m12)}], [${s(x.m21)}, ${s(x.m22)}]] = −iX');
  final u = cayleyRotor(c, r(1, 3), r(2, 3), r(0));
  final st = applyRotor(c, u, (c.one, c.zero));
  final (bx, by, bz) = blochOf(c, st);
  print('a = (1/3, 2/3, 0): Bloch vector of U|0⟩ = ($bx, $by, $bz), length² = ${bx * bx + by * by + bz * bz}');
}
Output
a = (1, 0, 0): U = [[0 + (0)i, 0 + (-1)i], [0 + (-1)i, 0 + (0)i]] = −iX
a = (1/3, 2/3, 0): Bloch vector of U|0⟩ = (24/49, -12/49, -41/49), length² = 1
applyRotorfunction
(Cx<T>, Cx<T>) applyRotor<T>(CxArith<T> c, Mat2<T> u, (Cx<T>, Cx<T>) s)

U \cdot (\alpha, \beta).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // applyRotor(c, U, (α, β)) = U·(α, β); the norm |α|² + |β|² stays 1 exactly.
  const c = CxArith<Rational>(ExactArith());
  final u = cayleyRotor(c, Rational.fromInt(1, 2), Rational.zero, Rational.fromInt(1, 4));
  var s = (c.one, c.zero);
  for (var k = 1; k <= 3; k++) {
    s = applyRotor(c, u, s);
    print('U^$k|0⟩: |α|² + |β|² = ${c.norm2(s.$1) + c.norm2(s.$2)}, α = ${s.$1.re} + (${s.$1.im})i');
  }
}
Output
U^1|0⟩: |α|² + |β|² = 1, α = 11/21 + (-8/21)i
U^2|0⟩: |α|² + |β|² = 1, α = -199/441 + (-176/441)i
U^3|0⟩: |α|² + |β|² = 1, α = -9229/9261 + (-344/9261)i
blochOffunction
(T, T, T) blochOf<T>(CxArith<T> c, (Cx<T>, Cx<T>) s)

The Bloch vector (2 \operatorname{Re} \bar{\alpha}\beta, 2 \operatorname{Im} \bar{\alpha}\beta, |\alpha|^{2} - |\beta|^{2}).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // blochOf: the Bloch vector (2 Re ᾱβ, 2 Im ᾱβ, |α|² − |β|²); for a rotor orbit it stays on the unit sphere exactly.
  const c = CxArith<Rational>(ExactArith());
  final u = cayleyRotor(c, Rational.zero, Rational.fromInt(1, 4), Rational.zero);
  var s = (c.one, c.zero);
  final pts = <List<Object>>[];
  for (var k = 0; k < 10; k++) {
    final (x, y, z) = blochOf(c, s);
    if (k < 3) print('step $k: ($x, $y, $z), x² + y² + z² = ${x * x + y * y + z * z}');
    pts.add([x.toDouble(), z.toDouble(), '$k']);
    s = applyRotor(c, u, s);
  }
}
Output
step 0: (0, 0, 1), x² + y² + z² = 1
step 1: (240/289, 0, 161/289), x² + y² + z² = 1
step 2: (77280/83521, 0, -31679/83521), x² + y² + z² = 1
List<double> classicRotate(List<double> s, double nx, double ny, double nz, double theta)

The classic rotor \exp(-i\theta n \cdot \frac{\sigma}{2}) applied to (\alpha, \beta) given as (re, im) pairs; n need not be unit (normalised by a root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic rotor exp(−iθ n·σ/2): a half turn (θ = π) about x sends |0⟩ to −i|1⟩, to rounding.
  final s = classicRotate([1, 0, 0, 0], 1, 0, 0, math.pi);
  print('result (re α, im α, re β, im β) = ${s.map((x) => x.toStringAsFixed(12)).join(', ')}');
  final n = s.fold(0.0, (a, x) => a + x * x);
  print('norm² − 1 = ${(n - 1).abs() < 1e-15 ? 'below 1e-15' : n - 1}');
}
Output
result (re α, im α, re β, im β) = 0.000000000000, 0.000000000000, 0.000000000000, -1.000000000000
norm² − 1 = below 1e-15
(double, double) classicMassDirection(double theta)

The classic mass-space direction for a gate of angle \theta: (\cos \frac{\theta}{2}, \sin \frac{\theta}{2}) (Transcendental). The rational way draws the rotor's own circle point \frac{((1 - t^{2}), 2t)}{1 + t^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic mass-space direction (cos θ/2, sin θ/2); the rational way uses the rotor's circle point.
  for (final th in [0.0, math.pi / 2, math.pi]) {
    final (c, s) = classicMassDirection(th);
    print('θ = ${th.toStringAsFixed(6)}: (${c.toStringAsFixed(9)}, ${s.toStringAsFixed(9)})');
  }
  final t = Rational.fromInt(1, 2), d = Rational.one + t * t;
  print('rational, t = 1/2: (${(Rational.one - t * t) / d}, ${Rational.fromInt(2) * t / d})');
}
Output
θ = 0.000000: (1.000000000, 0.000000000)
θ = 1.570796: (0.707106781, 0.707106781)
θ = 3.141593: (0.000000000, 1.000000000)
rational, t = 1/2: (3/5, 4/5)
List<Cx<T>> rationalPacket<T>(CxArith<T> c, {required int n, required int center, required int halfWidth, required T tau})

A wave packet with no transcendental function: a rational bump (1 - (\frac{x - x_{0}}{w})^{2})^{2} inside |x - x_{0}| < w, times the rational phase (\frac{1 + i\tau}{1 - i\tau})^{j} along the grid (momentum per step \approx 2 \cdot \operatorname{atan} \tau / dx), instead of e^{-(x - x_{0})^{2}/2\sigma^{2}} e^{ikx}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A wave packet with no exponentials: the bump (1 − (d/w)²)² times the rational phase C(τ)^j.
  const q = CxArith<Rational>(ExactArith());
  final psi = rationalPacket<Rational>(q, n: 21, center: 10, halfWidth: 6, tau: Rational.fromInt(1, 3));
  print('ψ_10 = ${psi[10].re} + ${psi[10].im}i');
  print('ψ_12 = ${psi[12].re} + (${psi[12].im})i');
}
Output
ψ_10 = 9653287/9765625 + 1476984/9765625i
ψ_12 = 2056025152/19775390625 + (1721015296/2197265625)i
(List<double>, List<double>) betaEnsembleSample(int n, int beta, ClickSource rng)

One sample of the tridiagonal Gaussian \beta-ensemble (\beta = 1 orthogonal, 2 unitary) of size n: (diagonal, hopping quadrances).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // One tridiagonal Gaussian β-ensemble matrix (Dumitriu–Edelman), held by the quadrances of its hoppings.
  final (diag, q) = betaEnsembleSample(8, 2, ClickSource(5));
  print('diagonal:    ${diag.map((x) => x.toStringAsFixed(4)).join(', ')}');
  print('quadrances:  ${q.map((x) => x.toStringAsFixed(4)).join(', ')}');
  final e = QuadranceChain<double>(const F64Arith(), quadrances: q, onsite: diag).eigenvalues();
  print('eigenvalues: ${e.map((x) => x.toStringAsFixed(4)).join(', ')}');
}
Output
diagonal:    -0.4094, -1.4233, -0.9084, -0.4072, 2.0443, 0.3936, -0.7870, 0.9525
quadrances:  6.9595, 12.1163, 3.1438, 5.8346, 4.0945, 1.9038, 0.6658
eigenvalues: -5.7016, -2.7707, -1.7677, -0.3898, 0.6676, 1.4560, 3.3618, 4.5992
double irwinHallNormal(ClickSource rng)

A normal sample with no transcendental function: twelve uniforms \frac{k}{2^{32}} summed, less six (Irwin–Hall).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A normal sample with no transcendental function: twelve uniforms summed, less six (Irwin–Hall).
  final rng = ClickSource(17);
  final xs = [for (var i = 0; i < 4000; i++) irwinHallNormal(rng)];
  final mean = xs.reduce((a, b) => a + b) / xs.length;
  final v = xs.map((x) => (x - mean) * (x - mean)).reduce((a, b) => a + b) / xs.length;
  print('mean ${mean.toStringAsFixed(4)}, variance ${v.toStringAsFixed(4)} (target 0 and 1)');
  final h = List<int>.filled(12, 0);
  for (final x in xs) {
    final b = ((x + 3) * 2).floor();
    if (b >= 0 && b < 12) h[b]++;
  }
}
Output
mean -0.0109, variance 1.0262 (target 0 and 1)
double boxMullerNormal(ClickSource rng)

A normal sample by Box–Muller (Transcendental, Root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Box–Muller: √(−2 ln u₁)·cos 2πu₂, the classic normal sample (logarithm, root, cosine).
  final rng = ClickSource(17);
  final xs = [for (var i = 0; i < 4000; i++) boxMullerNormal(rng)];
  final mean = xs.reduce((a, b) => a + b) / xs.length;
  final v = xs.map((x) => (x - mean) * (x - mean)).reduce((a, b) => a + b) / xs.length;
  print('mean ${mean.toStringAsFixed(4)}, variance ${v.toStringAsFixed(4)}');
}
Output
mean -0.0125, variance 0.9720
(double, double, double, double) tfdQuadratures(double k, double g1, double g2, double g3, double g4)

A sample of the TFD's quadratures (x_{L}, p_{L}, x_{R}, p_{R}) from four normals, with no root: rational coefficients \frac{k}{2} and \frac{1}{2k}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A TFD sample (x_L, p_L, x_R, p_R) from four normals with rational coefficients k/2 and 1/(2k).
  final rng = ClickSource(21);
  var sxx = 0.0, sxy = 0.0;
  const k = 2.0, n = 4000;
  for (var i = 0; i < n; i++) {
    final (xl, _, xr, _) = tfdQuadratures(k, irwinHallNormal(rng), irwinHallNormal(rng), irwinHallNormal(rng), irwinHallNormal(rng));
    sxx += xl * xl;
    sxy += xl * xr;
  }
  print('sample Var x_L = ${(sxx / n).toStringAsFixed(3)} (cosh 2r / 2 = ${17 / 16})');
  print('sample Cov(x_L, x_R) = ${(sxy / n).toStringAsFixed(3)} (sinh 2r / 2 = ${15 / 16})');
}
Output
sample Var x_L = 1.074 (cosh 2r / 2 = 1.0625)
sample Cov(x_L, x_R) = 0.954 (sinh 2r / 2 = 0.9375)
int geometricSample(double q, double u)

The number n both sides find, from one uniform: the least n with 1 - q^{n + 1} > u (only {+}\ {-}\ \times and a comparison).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The number n both TFD sides find, from one uniform: the least n with 1 − q^{n+1} > u.
  final rng = ClickSource(8);
  const q = 9 / 25;
  final h = List<int>.filled(6, 0);
  for (var i = 0; i < 5000; i++) {
    final n = geometricSample(q, rng.next());
    if (n < 6) h[n]++;
  }
  print('counts of n = 0…5 over 5000 draws: $h');
  print('expected (1 − q)qⁿ·5000: ${[for (var n = 0; n < 6; n++) (5000 * (1 - q) * _pow(q, n)).round()]}');
}

double _pow(double x, int k) {
  var p = 1.0;
  for (var i = 0; i < k; i++) p *= x;
  return p;
}
Output
counts of n = 0…5 over 5000 draws: [3168, 1174, 398, 163, 65, 20]
expected (1 − q)qⁿ·5000: [3200, 1152, 415, 149, 54, 19]
List<int> isingRingCoefficients(int n)

Z_{N}(x) = (x + 1)^{N} + (x - 1)^{N} for an Ising ring of n spins with Boltzmann weight x per aligned bond, as polynomial coefficients c_{j} (Z = \sum c_{j} x^{j}): 2 \cdot C(N, j) for N - j even, 0 otherwise.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Z_N(x) = (x + 1)^N + (x − 1)^N as polynomial coefficients: 2·C(N, j) where N − j is even.
  for (final n in [2, 3, 4, 6]) print('N = $n: ${isingRingCoefficients(n)}');
}
Output
N = 2: [2, 0, 2]
N = 3: [0, 6, 0, 2]
N = 4: [2, 0, 12, 0, 2]
N = 6: [2, 0, 30, 0, 30, 0, 2]
double classicIsingZ(int n, double k)

The classic Ising ring, Z = (2 \cosh K)^{N} + (2 \sinh K)^{N} (Transcendental).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic Ising ring Z = (2 cosh K)^N + (2 sinh K)^N; with x = e^{2K} it is e^{−NK}·Z_N(x).
  const n = 4, k = 0.3;
  final z = classicIsingZ(n, k);
  final x = math.exp(2 * k);
  final poly = isingRingCoefficients(n).indexed.fold(0.0, (s, e) => s + e.$2 * math.pow(x, e.$1));
  print('classic Z = ${z.toStringAsFixed(9)}');
  print('e^{−NK}·Z_N(e^{2K}) = ${(math.exp(-n * k) * poly).toStringAsFixed(9)}');
}
Output
classic Z = 19.242622269
e^{−NK}·Z_N(e^{2K}) = 19.242622269

Constants and variables

final boxCheckPrimes

The three largest primes below 2^{61} (2^{61} - 1, 2^{61} - 31, 2^{61} - 45; Miller–Rabin with the first twelve prime bases, deterministic below 3 \cdot 10^{24}), for the modular checks.

Classes

class

Line

final class Line<T>

A line through the origin in the plane, by a direction (dx, dy) (any nonzero multiple is the same line).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A line through the origin by a direction; any nonzero multiple is the same line, so spreads ignore the scale.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a = Line(r(1), r(2)), b = Line(r(-3), r(-6)), c = Line(r(2), r(-1));
  print('a = (${a.dx}, ${a.dy}), b = (${b.dx}, ${b.dy}), c = (${c.dx}, ${c.dy})');
  print('s(a, b) = ${spreadOf(f, a, b)} (same line)');
  print('s(a, c) = ${spreadOf(f, a, c)} (perpendicular)');
}
Output
a = (1, 2), b = (-3, -6), c = (2, -1)
s(a, b) = 0 (same line)
s(a, c) = 1 (perpendicular)

Constructors

Lineconstructor
const Line(this.dx, this.dy)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Line(dx, dy) works over any field: exact rationals or doubles.
  final q = Line(Rational.fromInt(5), Rational.fromInt(12));
  final d = Line(5.0, 12.0);
  print('exact: ${spreadOf(const ExactArith(), Line(Rational.one, Rational.zero), q)}');
  print('float: ${spreadOf(const F64Arith(), const Line(1.0, 0.0), d)}');
}
Output
exact: 144/169
float: 0.8520710059171598

Properties

dxfield
final T dx
dyfield
final T dy
final class SpreadBell<T>

Polarisation-entangled photon pairs, measured by two polarisers: everything from spreads.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // A Bell test done with spreads: the CHSH sum for rational lines approaching the Tsirelson setting.
  const f = ExactArith();
  final bell = SpreadBell<Rational>(f);
  final pts = <List<double>>[];
  for (var k = 0; k < 6; k++) {
    final t = bell.pellSlope(k);
    final (a, a2, b, b2) = bell.tsirelsonLines(t);
    final s = bell.chsh(a, a2, b, b2);
    print('t = $t: S = $s ≈ ${s.toDouble()}, S > 2: ${s > Rational.fromInt(2)}, S² < 8: ${s * s < Rational.fromInt(8)}');
    pts.add([k.toDouble(), s.toDouble()]);
  }
  print('2√2 = ${2 * math.sqrt(2)}');
}
Output
t = 1/2: S = 14/5 ≈ 2.8, S > 2: true, S² < 8: true
t = 2/5: S = 82/29 ≈ 2.8275862068965516, S > 2: true, S² < 8: true
t = 5/12: S = 478/169 ≈ 2.828402366863905, S > 2: true, S² < 8: true
t = 12/29: S = 2786/985 ≈ 2.8284263959390863, S > 2: true, S² < 8: true
t = 29/70: S = 16238/5741 ≈ 2.8284271032921096, S > 2: true, S² < 8: true
t = 70/169: S = 94642/33461 ≈ 2.828427124114641, S > 2: true, S² < 8: true
2√2 = 2.8284271247461903

Constructors

SpreadBellconstructor
const SpreadBell(this.f)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // SpreadBell(f) over the rationals and over doubles: same formulas, exact versus rounded.
  final q = SpreadBell<Rational>(const ExactArith());
  final d = SpreadBell<double>(const F64Arith());
  final e1 = q.correlation(Line(Rational.one, Rational.zero), Line(Rational.fromInt(1), Rational.fromInt(3)));
  final e2 = d.correlation(const Line(1.0, 0.0), const Line(1.0, 3.0));
  print('exact E = $e1');
  print('float E = $e2');
}
Output
exact E = -4/5
float E = -0.8

Properties

ffield
final KeplerArith<T> f

Methods

(T, T, T, T) probabilities(Line<T> a, Line<T> b)

(P(+ +), P(+ -), P(- +), P(- -)) for polariser lines a and b.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The four coincidence probabilities from one spread: they add to 1 exactly.
  final bell = SpreadBell<Rational>(const ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final (pp, pm, mp, mm) = bell.probabilities(Line(r(1), r(0)), Line(r(2), r(1)));
  print('P(++) = $pp, P(+−) = $pm, P(−+) = $mp, P(−−) = $mm');
  print('sum = ${pp + pm + mp + mm}');
}
Output
P(++) = 2/5, P(+−) = 1/10, P(−+) = 1/10, P(−−) = 2/5
sum = 1
T correlation(Line<T> a, Line<T> b)

E(a, b) = P(same) - P(different) = 1 - 2s.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // E(a, b) = 1 − 2s as the second polariser turns through rational lines (1, t).
  final bell = SpreadBell<Rational>(const ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a = Line(r(1), r(0));
  final pts = <List<double>>[];
  for (final t in [r(0), r(1, 3), r(1, 2), r(1), r(2), r(3)]) {
    final e = bell.correlation(a, Line(r(1), t));
    print('b = (1, $t): E = $e');
  }
  for (var i = 0; i <= 40; i++) {
    final t = r(i - 20, 5);
    pts.add([t.toDouble(), bell.correlation(a, Line(r(1), t)).toDouble()]);
  }
}
Output
b = (1, 0): E = 1
b = (1, 1/3): E = 4/5
b = (1, 1/2): E = 3/5
b = (1, 1): E = 0
b = (1, 2): E = -3/5
b = (1, 3): E = -4/5
chshmethod
T chsh(Line<T> a, Line<T> a2, Line<T> b, Line<T> b2)

The CHSH sum E(a, b) - E(a, b') + E(a', b) + E(a', b').

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // CHSH from four rational lines: an exact rational S, next to the classic cos form at the same lines' angles.
  final bell = SpreadBell<Rational>(const ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a = Line(r(1), r(0)), a2 = Line(r(1), r(1)), b = Line(r(5), r(2)), b2 = Line(r(3), r(7));
  final s = bell.chsh(a, a2, b, b2);
  final c = classicChsh(0, math.pi / 4, math.atan2(2, 5), math.atan2(7, 3));
  print('exact S   = $s ≈ ${s.toDouble()}');
  print('classic S = $c');
  print('above the local bound 2: ${s > r(2)}');
}
Output
exact S   = 82/29 ≈ 2.8275862068965516
classic S = 2.8275862068965516
above the local bound 2: true
(Line<T>, Line<T>, Line<T>, Line<T>) tsirelsonLines(T t)

The Tsirelson setting approached with the rational slope t \approx \sqrt{2} - 1: a at 0^{\circ}, a' at 45^{\circ}, b at slope t, b' = b turned by 45^{\circ} (direction (1 - t, 1 + t)). S = \frac{2(1 + 2t - t^{2})}{1 + t^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The four polariser lines for slope t = 5/12 (close to √2 − 1).
  final bell = SpreadBell<Rational>(const ExactArith());
  final t = Rational.fromInt(5, 12);
  final (a, a2, b, b2) = bell.tsirelsonLines(t);
  for (final (n, l) in [('a', a), ("a'", a2), ('b', b), ("b'", b2)]) print('$n = (${l.dx}, ${l.dy})');
  print('S = ${bell.chsh(a, a2, b, b2)}');
  double d(Rational x) => x.toDouble();
}
Output
a = (1, 0)
a' = (1, 1)
b = (1, 5/12)
b' = (7/12, 17/12)
S = 478/169
pellSlopemethod
T pellSlope(int k)

The k-th Pell convergent of \sqrt{2} - 1: \frac{1}{2}, \frac{2}{5}, \frac{5}{12}, \frac{12}{29}, \frac{29}{70}, … (p_{k + 1} = q_{k}, q_{k + 1} = 2q_{k} + p_{k}).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // Pell convergents of √2 − 1: rational slopes closing in on the irrational best angle 22.5°.
  final bell = SpreadBell<Rational>(const ExactArith());
  final target = math.sqrt(2) - 1;
  for (var k = 0; k < 7; k++) {
    final t = bell.pellSlope(k);
    print('k = $k: t = $t, t − (√2 − 1) = ${(t.toDouble() - target).toStringAsExponential(2)}');
  }
}
Output
k = 0: t = 1/2, t − (√2 − 1) = 8.58e-2
k = 1: t = 2/5, t − (√2 − 1) = -1.42e-2
k = 2: t = 5/12, t − (√2 − 1) = 2.45e-3
k = 3: t = 12/29, t − (√2 − 1) = -4.20e-4
k = 4: t = 29/70, t − (√2 − 1) = 7.22e-5
k = 5: t = 70/169, t − (√2 − 1) = -1.24e-5
k = 6: t = 169/408, t − (√2 − 1) = 2.12e-6
(T, T, T) spherePoint(T u, T v)

The rational point of the unit sphere over (u, v): \frac{(2u, 2v, u^{2} + v^{2} - 1)}{1 + u^{2} + v^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Rational points of the unit sphere from (u, v): x² + y² + z² = 1 exactly.
  final bell = SpreadBell<Rational>(const ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  for (final (u, v) in [(r(1), r(0)), (r(1, 2), r(1, 3)), (r(2), r(-1))]) {
    final (x, y, z) = bell.spherePoint(u, v);
    print('(u, v) = ($u, $v) → ($x, $y, $z), x² + y² + z² = ${x * x + y * y + z * z}');
  }
}
Output
(u, v) = (1, 0) → (1, 0, 0), x² + y² + z² = 1
(u, v) = (1/2, 1/3) → (36/49, 24/49, -23/49), x² + y² + z² = 1
(u, v) = (2, -1) → (2/3, -1/3, 2/3), x² + y² + z² = 1
T singletCorrelation((T, T, T) a, (T, T, T) b)

The singlet correlation -a \cdot b for unit axes a, b.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Spin singlets: E = −a·b for two rational unit axes.
  final bell = SpreadBell<Rational>(const ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final a = bell.spherePoint(r(0), r(0)); // the south pole (0, 0, −1)
  final b = bell.spherePoint(r(1, 2), r(0));
  print('a = $a, b = $b');
  print('E = −a·b = ${bell.singletCorrelation(a, b)}');
  print('same axis: E = ${bell.singletCorrelation(a, a)}');
}
Output
a = (0, 0, -1), b = (4/5, 0, -3/5)
E = −a·b = -3/5
same axis: E = -1
int hiddenOutcome(Line<T> polariser, Line<T> hidden)

A local-hidden-variable model, for comparison: each pair carries a hidden line \ell; each side reports + when its polariser's spread to \ell is below \tfrac{1}{2}. The correlation it gives is linear in the angle (a triangle wave), so its CHSH sum never passes 2. Returns +1 or -1 for one side.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A local hidden-variable model: each pair carries a hidden line; its CHSH sum comes out at 2 here, short of 2√2.
  final bell = SpreadBell<double>(const F64Arith());
  final rng = ClickSource(11);
  const a = Line(1.0, 0.0), a2 = Line(1.0, 1.0), b = Line(1.0, 0.41421356), b2 = Line(0.58578644, 1.41421356);
  double e(Line<double> x, Line<double> y, List<Line<double>> hs) =>
      hs.map((h) => bell.hiddenOutcome(x, h) * bell.hiddenOutcome(y, h)).reduce((p, q) => p + q) / hs.length;
  final hidden = [for (var i = 0; i < 4000; i++) () { final u = rng.next() * 2 - 1; return Line(1 - u * u, 2 * u); }()];
  final s = e(a, b, hidden) - e(a, b2, hidden) + e(a2, b, hidden) + e(a2, b2, hidden);
  print('one pair, hidden line (1, 0.3): A = ${bell.hiddenOutcome(a, const Line(1.0, 0.3))}, B = ${bell.hiddenOutcome(b, const Line(1.0, 0.3))}');
  print('hidden-variable CHSH over 4000 pairs = $s (quantum mechanics: up to 2√2 ≈ 2.828)');
}
Output
one pair, hidden line (1, 0.3): A = 1, B = 1
hidden-variable CHSH over 4000 pairs = 2.0 (quantum mechanics: up to 2√2 ≈ 2.828)
final class ClickSource

A small deterministic generator of rational uniforms \frac{k}{2^{32}} (xorshift32): detector clicks for both ways, the same stream, so the two ways can be compared click for click.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // One seeded stream of uniforms k/2³²: both ways of a lab read the same clicks.
  final a = ClickSource(42), b = ClickSource(42);
  final xs = [for (var i = 0; i < 5; i++) a.next()];
  print('first five: $xs');
  print('a second source with the same seed agrees: ${[for (var i = 0; i < 5; i++) b.next()].join(',') == xs.join(',')}');
  final h = List<int>.filled(10, 0);
  final c = ClickSource(1);
  for (var i = 0; i < 5000; i++) h[(c.next() * 10).floor()]++;
  print('5000 draws in 10 bins: $h');
}
Output
first five: [0.002643892541527748, 0.660311977379024, 0.11095708678476512, 0.8493769019842148, 0.8754393914714456]
a second source with the same seed agrees: true
5000 draws in 10 bins: [538, 542, 482, 491, 455, 480, 510, 507, 477, 518]

Constructors

ClickSourceconstructor
ClickSource([int seed = 0x2545F491])
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ClickSource([seed]): the default seed, a chosen seed, and seed 0 (treated as 1).
  print('default seed: ${ClickSource().nextBits()}');
  print('seed 7:       ${ClickSource(7).nextBits()}');
  print('seed 0 = seed 1: ${ClickSource(0).nextBits() == ClickSource(1).nextBits()}');
}
Output
default seed: 3777279546
seed 7:       1892583
seed 0 = seed 1: true

Methods

nextBitsmethod
int nextBits()

The next 32-bit value.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // nextBits: the raw xorshift32 state, an integer in [1, 2³²).
  final s = ClickSource(1);
  print([for (var i = 0; i < 6; i++) s.nextBits()].join(', '));
}
Output
270369, 67634689, 2647435461, 307599695, 2398689233, 745495504
nextmethod
double next()

A uniform in [0, 1) as a double (it is \frac{k}{2^{32}} exactly).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // next(): nextBits / 2³², an exact dyadic rational in [0, 1) given as a double.
  final s = ClickSource(3), t = ClickSource(3);
  for (var i = 0; i < 4; i++) {
    final u = s.next(), bits = t.nextBits();
    print('u = $u = $bits / 4294967296 exactly: ${Rational.fromInt(bits, 4294967296).toDouble() == u}');
  }
}
Output
u = 0.00018885056488215923 = 811107 / 4294967296 exactly: true
u = 0.047005296451970935 = 201886211 / 4294967296 exactly: true
u = 0.7044134305324405 = 3025432647 / 4294967296 exactly: true
u = 0.4445362261030823 = 1909268553 / 4294967296 exactly: true
final class BoxSamples

The box's rational sample points: t = \tan(\frac{\pi x}{2L}) = j/m for j = 0 \cdots m (the left half) and their mirrors, so 2m + 1 points from wall to wall with \sigma = \frac{j^{2}}{j^{2} + m^{2}} on the left and 1 - \sigma on the right.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The box's rational sample points: tan(πx/2L) = j/m, so σ = j²/(j² + m²) exactly, mirrored on the right.
  const f = ExactArith();
  final s = BoxSamples(3);
  print('${s.length} samples from wall to wall:');
  for (var i = 0; i < s.length; i++) {
    final (j, mirrored) = s.at(i);
    print('  i = $i: j = $j${mirrored ? ' (mirror)' : ''}, σ = ${s.halfSpread(f, i)}, x/L ≈ ${boxSampleX(s.tangent(i).$1, s.tangent(i).$2).toStringAsFixed(4)}');
  }
}
Output
7 samples from wall to wall:
  i = 0: j = 0, σ = 0, x/L ≈ 0.0000
  i = 1: j = 1, σ = 1/10, x/L ≈ 0.2048
  i = 2: j = 2, σ = 4/13, x/L ≈ 0.3743
  i = 3: j = 3, σ = 1/2, x/L ≈ 0.5000
  i = 4: j = 2 (mirror), σ = 9/13, x/L ≈ 0.6257
  i = 5: j = 1 (mirror), σ = 9/10, x/L ≈ 0.7952
  i = 6: j = 0 (mirror), σ = 1, x/L ≈ 1.0000

Constructors

BoxSamplesconstructor
const BoxSamples(this.m)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // BoxSamples(m): 2m + 1 points, j = 0 … m on the left and their mirrors.
  for (final m in [1, 2, 8]) print('m = $m: ${BoxSamples(m).length} points');
}
Output
m = 1: 3 points
m = 2: 5 points
m = 8: 17 points

Properties

mfield
final int m
lengthgetter
int get length

Methods

atmethod
(int, bool) at(int i)

Point i (0 = left wall, 2m = right wall) as (j, mirrored): t = \frac{j}{m}, mirrored ones at L - x.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // at(i) gives (j, mirrored): point i sits at tan = j/m, or at its mirror L − x.
  final s = BoxSamples(2);
  print([for (var i = 0; i < s.length; i++) 'i=$i → ${s.at(i)}'].join('  '));
}
Output
i=0 → (0, false)  i=1 → (1, false)  i=2 → (2, false)  i=3 → (1, true)  i=4 → (0, true)
T halfSpread<T>(KeplerArith<T> f, int i)

\sigma = \sin^{2}(\frac{\pi x}{2L}) at point i, exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // σ = sin²(πx/2L) at each sample, exactly; it runs 0 → 1 across the box and σ + σ(mirror) = 1.
  const f = ExactArith();
  final s = BoxSamples(4);
  final sig = [for (var i = 0; i < s.length; i++) s.halfSpread(f, i)];
  print('σ: ${sig.join(', ')}');
  print('σ(1) + σ(7) = ${sig[1] + sig[7]}');
}
Output
σ: 0, 1/17, 1/5, 9/25, 1/2, 16/25, 4/5, 16/17, 1
σ(1) + σ(7) = 1
tangentmethod
(int, int) tangent(int i)

\tan(\frac{\pi x}{2L}) at point i as (numerator, denominator); the right wall is (1, 0).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // tan(πx/2L) at each sample as a fraction; the right wall is (1, 0), i.e. tan = ∞.
  final s = BoxSamples(3);
  print([for (var i = 0; i < s.length; i++) s.tangent(i)].join(', '));
}
Output
(0, 3), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1), (3, 0)
final class BoxState<T>

A box state \sum c_{n} \varphi_{n} by its coefficients c_{n} (index n - 1), not necessarily normalised: densities are divided by \Sigma|c_{n}|^{2}, which time evolution keeps exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // (φ₁ + φ₂)/√2 by spread polynomials: exact densities at rational samples, and the same state a quarter period on.
  const c = CxArith<Rational>(ExactArith());
  final f = c.f;
  final psi = BoxState(c, [c.of(Rational.one), c.of(Rational.one)]);
  final later = psi.at(cayleyTick(c, Rational.fromInt(1, 2)));
  final s = BoxSamples(12);
  final d0 = <List<double>>[], d1 = <List<double>>[];
  for (var i = 0; i < s.length; i++) {
    final sp = spreadPolynomials(f, s.halfSpread(f, i), 2 * psi.top);
    final x = boxSampleX(s.tangent(i).$1, s.tangent(i).$2);
    d0.add([x, psi.density(sp).toDouble()]);
    d1.add([x, later.density(sp).toDouble()]);
  }
  print('norm Σ|c|² = ${psi.norm2}, ⟨E⟩/E1 = ${psi.energy}');
  print('after the tick: norm ${later.norm2}, ⟨E⟩/E1 = ${later.energy} (unchanged, exactly)');
  print('L|ψ|² at the middle sample: t = 0: ${psi.density(spreadPolynomials(f, s.halfSpread(f, 12), 4))}, after: ${later.density(spreadPolynomials(f, s.halfSpread(f, 12), 4))}');
}
Output
norm Σ|c|² = 2, ⟨E⟩/E1 = 5/2
after the tick: norm 2, ⟨E⟩/E1 = 5/2 (unchanged, exactly)
L|ψ|² at the middle sample: t = 0: 1, after: 1

Constructors

BoxStateconstructor
BoxState(this.c, List<Cx<T>> coefficients)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // BoxState(c, coefficients): cₙ at index n − 1, not normalised; norm2, energy and top read it.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = BoxState(c, [Cx(r(1), r(0)), Cx(r(0), r(0)), Cx(r(1, 2), r(1, 2))]);
  print('top = ${s.top}, Σ|c|² = ${s.norm2}, ⟨E⟩/E1 = ${s.energy}');
}
Output
top = 3, Σ|c|² = 3/2, ⟨E⟩/E1 = 11/3

Properties

cfield
final CxArith<T> c
final List<Cx<T>> coefficients
topgetter
int get top

The highest n present.

norm2getter
T get norm2

\Sigma|c_{n}|^{2} (the probability, before dividing).

energygetter
T get energy

\frac{\langle E\rangle}{E_{1}} = \sum n^{2}|c_{n}|^{2} / \Sigma|c_{n}|^{2}.

Methods

atmethod
BoxState<T> at(Cx<T> z1)

The state at the clock instant z_{1} = e^{−iE₁t}: c_{n} \cdot z_{1}^{n^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // at(z₁): cₙ·z₁^{n²}. With z₁ = −1 (a mirror revival) odd states keep their sign and even states flip.
  const c = CxArith<Rational>(ExactArith());
  final s = BoxState(c, [c.of(Rational.one), c.of(Rational.one), c.of(Rational.one)]);
  final m = s.at(minusIPow(c, 2));
  print('at z1 = −1: ${[for (final x in m.coefficients) '${x.re}'].join(', ')}');
  final q = s.at(cayleyTick(c, Rational.fromInt(1, 3)));
  print('at z1 = C(1/3): c2 = ${q.coefficients[1].re} + (${q.coefficients[1].im})i, |c2|² = ${c.norm2(q.coefficients[1])}');
}
Output
at z1 = −1: -1, 1, -1
at z1 = C(1/3): c2 = -527/625 + (-336/625)i, |c2|² = 1
pairsmethod
List<(int, int, T)> pairs()

The pair weights \operatorname{Re}(c_{n} \bar{c}_{m}) for n \le m (n, m from 1), the only thing a density needs.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // pairs(): the weights Re(cₙ c̄ₘ), n ≤ m, the only numbers a density needs.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = BoxState(c, [Cx(r(1), r(0)), Cx(r(0), r(1)), Cx(r(1, 2), r(0))]);
  for (final (n, m, w) in s.pairs()) print('(n, m) = ($n, $m): Re(c_n c̄_m) = $w');
}
Output
(n, m) = (1, 1): Re(c_n c̄_m) = 1
(n, m) = (1, 2): Re(c_n c̄_m) = 0
(n, m) = (1, 3): Re(c_n c̄_m) = 1/2
(n, m) = (2, 2): Re(c_n c̄_m) = 1
(n, m) = (2, 3): Re(c_n c̄_m) = 0
(n, m) = (3, 3): Re(c_n c̄_m) = 1/4
densitymethod
T density(List<T> s, {List<(int, int, T)>? pairs, T? norm})

L \cdot |\psi|^{2} at the point of half-spread \sigma given s = S_{0}(\sigma) \cdots S_{2 \cdot top}(\sigma): 2 \sum_{n,m} \operatorname{Re}(c_{n} \bar{c}_{m})[S_{n + m}(\sigma) - S_{|n - m|}(\sigma)] / \Sigma|c_{k}|^{2}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // L|ψ|² for φ₂ alone at rational samples: 2 sin²(2θ) = 2·S₄(σ), exact.
  const c = CxArith<Rational>(ExactArith());
  final f = c.f;
  final s = BoxState(c, [c.of(Rational.zero), c.of(Rational.one)]);
  final samples = BoxSamples(2);
  for (var i = 0; i < samples.length; i++) {
    final sig = samples.halfSpread(f, i);
    print('σ = $sig: L|ψ|² = ${s.density(spreadPolynomials(f, sig, 4))}');
  }
}
Output
σ = 0: L|ψ|² = 0
σ = 1/5: L|ψ|² = 1152/625
σ = 1/2: L|ψ|² = 0
σ = 4/5: L|ψ|² = 1152/625
σ = 1: L|ψ|² = 0
leftKappamethod
T leftKappa({List<(int, int, T)>? pairs, T? norm})

The probability in the left half, P = \frac{1}{2} + \frac{\kappa}{\pi}: returns \kappa, exactly. \int_{0}^{L/2} 2 \sin(n\theta) \sin(m\theta) \frac{dx}{L} = \frac{[\frac{s(n - m)}{n - m} - \frac{s(n + m)}{n + m}]}{\pi} for n \ne m, with s(k) = \sin(\frac{k\pi}{2}) \in \{0, 1, 0, -1\}; \frac{1}{2} for n = m.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The chance of the left half, P = 1/2 + κ/π, with κ exact; the classic integral by sin and cos agrees.
  const c = CxArith<Rational>(ExactArith());
  final s = BoxState(c, [c.of(Rational.one), c.of(Rational.one)]);
  final k = s.leftKappa();
  print('κ = $k, P(left) = 1/2 + κ/π = ${(0.5 + k.toDouble() / math.pi).toStringAsFixed(12)}');
  print('classic P(left)          = ${classicLeftHalf([1, 1], [0, 0], 0).toStringAsFixed(12)}');
}
Output
κ = 4/3, P(left) = 1/2 + κ/π = 0.924413181578
classic P(left)          = 0.924413181578
final class BoxClock

The rational clock: instants z_{1} = e^{−iE₁t} = (-i)^{q} \cdot C(\tau), \tau = \frac{70}{169} \cdot \frac{2k - J}{J} for tick k = 0 \cdots J - 1 of quarter q. Tick \frac{J}{2} of quarter 0 is t = 0; four quarters are one revival period T_{\mathrm{rev}} = \frac{2\pi}{E_{1}} = \frac{4mL^{2}}{\pi\hbar}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The rational clock: exact instants z₁ = (−i)^q·C(τ); four quarters are one revival period.
  const c = CxArith<Rational>(ExactArith());
  final clock = BoxClock(ticksPerQuarter: 4);
  final pts = <List<Object>>[];
  for (var k = 0; k < 16; k++) {
    final z = clock.z(c);
    if (k < 6) print('q = ${clock.quarter}, tick = ${clock.tick}: z1 = ${z.re} + (${z.im})i, |z1|² = ${c.norm2(z)}${clock.onRevival ? '  (revival instant)' : ''}');
    pts.add([z.re.toDouble(), z.im.toDouble(), '$k']);
    clock.advance();
  }
  print('reach τ = ${BoxClock.reachNum}/${BoxClock.reachDen} < tan(π/8) = ${math.tan(math.pi / 8).toStringAsFixed(6)}');
}
Output
q = 0, tick = 2: z1 = 1 + (0)i, |z1|² = 1  (revival instant)
q = 0, tick = 3: z1 = 13668/14893 + (-5915/14893)i, |z1|² = 1
q = 1, tick = 0: z1 = 23660/33461 + (-23661/33461)i, |z1|² = 1
q = 1, tick = 1: z1 = 5915/14893 + (-13668/14893)i, |z1|² = 1
q = 1, tick = 2: z1 = 0 + (-1)i, |z1|² = 1  (revival instant)
q = 1, tick = 3: z1 = -5915/14893 + (-13668/14893)i, |z1|² = 1
reach τ = 70/169 < tan(π/8) = 0.414214

Constructors

BoxClockconstructor
BoxClock({this.ticksPerQuarter = 96})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // BoxClock({ticksPerQuarter}): J must be even so that τ = 0 is a tick; it starts at t = 0.
  final c = BoxClock(ticksPerQuarter: 6);
  print('J = ${c.ticksPerQuarter}, quarter = ${c.quarter}, tick = ${c.tick}, τ = ${c.tau}');
  print('default J = ${BoxClock().ticksPerQuarter}');
}
Output
J = 6, quarter = 0, tick = 3, τ = (0, 1014)
default J = 96

Static

reachNumconstant
static const reachNum = 70

\frac{70}{169}: a Pell convergent of \sqrt{2} - 1 just under \tan(\frac{\pi}{8}), so the charts meet with a sliver of a gap.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // reachNum/reachDen = 70/169, a Pell convergent of √2 − 1 just below tan(π/8).
  final r = BoxClock.reachNum / BoxClock.reachDen;
  print('70/169 = ${r.toStringAsFixed(9)}, tan(π/8) = ${math.tan(math.pi / 8).toStringAsFixed(9)}');
}
Output
70/169 = 0.414201183, tan(π/8) = 0.414213562
reachDenconstant
static const reachDen = 169

\frac{70}{169}: a Pell convergent of \sqrt{2} - 1 just under \tan(\frac{\pi}{8}), so the charts meet with a sliver of a gap.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // reachDen = 169 = 13²: the quarter chart ends at τ = ±70/169, where z₁ = C(70/169).
  const c = CxArith<Rational>(ExactArith());
  final z = cayleyTick(c, Rational.fromInt(BoxClock.reachNum, BoxClock.reachDen));
  print('reachDen = ${BoxClock.reachDen}; C(70/169) = ${z.re} + (${z.im})i, |z|² = ${c.norm2(z)}');
}
Output
reachDen = 169; C(70/169) = 23661/33461 + (-23660/33461)i, |z|² = 1

Properties

final int ticksPerQuarter

J: ticks per quarter period (even, so \tau = 0 is a tick).

quarterfield
int quarter

Quarter periods since t = 0 (any integer) and the tick in the current one.

tickfield
int tick

Quarter periods since t = 0 (any integer) and the tick in the current one.

taugetter
(int, int) get tau

\tau of this instant as (numerator, denominator), lowest terms not required.

onRevivalgetter
bool get onRevival

At a revival (z_{1} = 1), a mirror revival (z_{1} = -1) or a quarter revival (z_{1} = \mp i).

periodsgetter
int get periods

Whole revival periods since t = 0 (floor) and the quarter within it.

Methods

resetmethod
void reset()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // reset() returns the clock to t = 0 (quarter 0, tick J/2).
  final c = BoxClock(ticksPerQuarter: 4)..advance(7);
  print('after 7 ticks: quarter ${c.quarter}, tick ${c.tick}');
  c.reset();
  print('after reset:   quarter ${c.quarter}, tick ${c.tick}, τ = ${c.tau}');
}
Output
after 7 ticks: quarter 2, tick 1
after reset:   quarter 0, tick 2, τ = (0, 676)
advancemethod
void advance([int n = 1])

One tick forward.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // advance([n]) moves n ticks, rolling into the next quarter after J ticks.
  final c = BoxClock(ticksPerQuarter: 4);
  for (final n in [1, 1, 3, 8]) {
    c.advance(n);
    print('advance($n): quarter ${c.quarter}, tick ${c.tick}, periods ${c.periods}');
  }
}
Output
advance(1): quarter 0, tick 3, periods 0
advance(1): quarter 1, tick 0, periods 0
advance(3): quarter 1, tick 3, periods 0
advance(8): quarter 3, tick 3, periods 0
tauInmethod
T tauIn<T>(KeplerArith<T> f)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // tauIn(f): the instant's τ in a chosen field, exact or float.
  final c = BoxClock(ticksPerQuarter: 4)..advance(1);
  print('exact τ = ${c.tauIn(const ExactArith())}, float τ = ${c.tauIn(const F64Arith())}');
}
Output
exact τ = 35/169, float τ = 0.20710059171597633
zmethod
Cx<T> z<T>(CxArith<T> c)

z_{1} = e^{−iE₁t} at this instant, exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // z(c): the instant as a Gaussian rational, exactly on the unit circle.
  const c = CxArith<Rational>(ExactArith());
  final clock = BoxClock(ticksPerQuarter: 2);
  for (var k = 0; k < 5; k++) {
    final z = clock.z(c);
    print('q = ${clock.quarter}, tick ${clock.tick}: z1 = ${z.re} + (${z.im})i, |z1|² = ${c.norm2(z)}');
    clock.advance();
  }
}
Output
q = 0, tick 1: z1 = 1 + (0)i, |z1|² = 1
q = 1, tick 0: z1 = 23660/33461 + (-23661/33461)i, |z1|² = 1
q = 1, tick 1: z1 = 0 + (-1)i, |z1|² = 1
q = 2, tick 0: z1 = -23661/33461 + (-23660/33461)i, |z1|² = 1
q = 2, tick 1: z1 = -1 + (0)i, |z1|² = 1
final class CayleyBoxStepper<T>

A box state stepped by a fixed rational phase per step: c_{n} \leftarrow c_{n} \cdot w^{n^{2}}, w = C(\tau). Exactly unitary over the Gaussian rationals (|w|^{2} = 1), so \Sigma|c_{n}|^{2} and ⟨E⟩ never change; the price is the size of the numbers, which grows with every step (digits).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // Exact versus float, 200 steps each: the exact stepper keeps Σ|c|² and ⟨E⟩ exactly; the float one drifts.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final coeffs = [Cx(r(1), r(0)), Cx(r(1, 2), r(1, 2)), Cx(r(0), r(1, 3))];
  final exact = CayleyBoxStepper(c, coeffs, tau: r(1, 10));
  final e1dt = 2 * math.atan(0.1); // the same angle per step
  final float = FloatBoxStepper([1, 0.5, 0], [0, 0.5, 1 / 3], e1dt: e1dt);
  final n0 = exact.state.norm2, e0 = exact.state.energy, f0 = float.norm2;
  final drift = <List<double>>[];
  for (var k = 1; k <= 200; k++) {
    exact.step();
    float.step();
    if (k % 10 == 0) drift.add([k.toDouble(), float.norm2 / f0 - 1]);
  }
  print('exact after ${exact.steps} steps: Σ|c|² = ${exact.state.norm2} (start $n0), ⟨E⟩/E1 = ${exact.state.energy} (start $e0)');
  print('float after ${float.steps} steps: Σ|c|²/start − 1 = ${(float.norm2 / f0 - 1).toStringAsExponential(2)}');
  print('size of the exact numbers: c3 denominator has ${exact.coefficients[2].re.denominator.bitLength} bits');
}
Output
exact after 200 steps: Σ|c|² = 29/18 (start 29/18), ⟨E⟩/E1 = 72/29 (start 72/29)
float after 200 steps: Σ|c|²/start − 1 = 1.33e-15
size of the exact numbers: c3 denominator has 11985 bits

Constructors

CayleyBoxStepperconstructor
CayleyBoxStepper(this.c, List<Cx<T>> coefficients, {required T tau})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // CayleyBoxStepper(c, coefficients, tau: τ): precomputes w^{n²} for w = C(τ) conjugated, i.e. e^{−iθ}.
  const c = CxArith<Rational>(ExactArith());
  final s = CayleyBoxStepper(c, [c.of(Rational.one), c.of(Rational.one)], tau: Rational.fromInt(1, 2));
  s.step();
  print('after one step: c1 = ${s.coefficients[0].re} + (${s.coefficients[0].im})i, c2 = ${s.coefficients[1].re} + (${s.coefficients[1].im})i');
}
Output
after one step: c1 = 3/5 + (-4/5)i, c2 = -527/625 + (336/625)i

Properties

cfield
final CxArith<T> c
stepsfield
int steps
List<Cx<T>> get coefficients
stategetter
BoxState<T> get state

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): one exact phase step; every |cₙ|², so the norm and ⟨E⟩, stays exactly where it was.
  const c = CxArith<Rational>(ExactArith());
  final s = CayleyBoxStepper(c, [c.of(Rational.one), c.of(Rational.zero), c.of(Rational.one)], tau: Rational.fromInt(1, 3));
  for (var k = 0; k < 4; k++) {
    s.step();
    print('step ${s.steps}: Σ|c|² = ${s.state.norm2}, ⟨E⟩/E1 = ${s.state.energy}, |c3|² = ${c.norm2(s.coefficients[2])}');
  }
}
Output
step 1: Σ|c|² = 2, ⟨E⟩/E1 = 5, |c3|² = 1
step 2: Σ|c|² = 2, ⟨E⟩/E1 = 5, |c3|² = 1
step 3: Σ|c|² = 2, ⟨E⟩/E1 = 5, |c3|² = 1
step 4: Σ|c|² = 2, ⟨E⟩/E1 = 5, |c3|² = 1
final class FloatBoxStepper

Coefficients stepped in f64 by e^{−iEₙΔt} (cos and sin once, then a complex multiply per step): the usual float time-stepper. |e^{−iEₙΔt}|² is 1 only to rounding, so the norm and ⟨E⟩ walk away as the steps add up.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The usual float stepper: multiply by e^{−iEₙΔt} (cos and sin) every step; the norm walks off by rounding.
  final s = FloatBoxStepper([1, 1, 1], [0, 0, 0], e1dt: 0.25);
  final n0 = s.norm2, e0 = s.energySum;
  final pts = <List<double>>[];
  for (var k = 0; k < 20; k++) {
    s.step(500);
    pts.add([s.steps.toDouble(), s.norm2 / n0 - 1]);
  }
  print('after ${s.steps} steps: Σ|c|²/start − 1 = ${(s.norm2 / n0 - 1).toStringAsExponential(2)}');
  print('Σn²|c|²/start − 1 = ${(s.energySum / e0 - 1).toStringAsExponential(2)}');
  print('|e^{−iΔt}|² − 1 = ${(math.cos(0.25) * math.cos(0.25) + math.sin(0.25) * math.sin(0.25) - 1).toStringAsExponential(2)}');
}
Output
after 10000 steps: Σ|c|²/start − 1 = -1.66e-13
Σn²|c|²/start − 1 = 5.97e-14
|e^{−iΔt}|² − 1 = -1.11e-16

Constructors

FloatBoxStepperconstructor
FloatBoxStepper(List<double> re, List<double> im, {required double e1dt})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // FloatBoxStepper(re, im, e1dt: E₁Δt): cos and sin are taken once per state, then reused each step.
  final s = FloatBoxStepper([1, 0], [0, 1], e1dt: 0.5);
  print('Σ|c|² = ${s.norm2}, Σn²|c|² = ${s.energySum}');
}
Output
Σ|c|² = 2.0, Σn²|c|² = 5.0

Properties

refield
final Float64List re
imfield
final Float64List im
stepsfield
int steps
norm2getter
double get norm2
energySumgetter
double get energySum

\sum n^{2}|c_{n}|^{2} (not divided: the drift of the norm shows in it too).

Methods

stepmethod
void step([int k = 1])
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step([k]): k float steps; compare one stepper doing 1000 single steps with one doing step(1000).
  final a = FloatBoxStepper([1, 1], [0, 0], e1dt: 0.25), b = FloatBoxStepper([1, 1], [0, 0], e1dt: 0.25);
  for (var i = 0; i < 1000; i++) a.step();
  b.step(1000);
  print('same result: ${a.re[1] == b.re[1] && a.im[1] == b.im[1]}, steps ${a.steps} and ${b.steps}');
  print('Σ|c|² − 2 = ${(a.norm2 - 2).toStringAsExponential(2)}');
}
Output
same result: true, steps 1000 and 1000
Σ|c|² − 2 = -4.97e-14
class

GInt

final class GInt

A Gaussian integer a + bi.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Gaussian integers a + bi: exact, any size, the box's amplitudes before any division.
  final z = GInt.of(3, 4), w = GInt.of(1, -2);
  print('z = $z, w = $w');
  print('z + w = ${z + w}, z − w = ${z - w}, z·w = ${z * w}');
  print('|z|² = ${z.norm2}, conj z = ${z.conj}, z^8 = ${z.pow(8)}');
  print('|z·w|² = |z|²|w|²: ${(z * w).norm2 == z.norm2 * w.norm2}');
}
Output
z = 3 + 4i, w = 1 + -2i
z + w = 4 + 2i, z − w = 2 + 6i, z·w = 11 + -2i
|z|² = 25, conj z = 3 + -4i, z^8 = 164833 + 354144i
|z·w|² = |z|²|w|²: true

Constructors

GIntconstructor
const GInt(this.re, this.im)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // GInt(re, im) from BigInts: no size limit.
  final z = GInt(BigInt.parse('123456789012345678901234567890'), BigInt.from(-1));
  print('z = $z');
  print('|z|² = ${z.norm2}');
}
Output
z = 123456789012345678901234567890 + -1i
|z|² = 15241578753238836750495351562536198787501905199875019052101
GInt.ofconstructor
GInt.of(int re, [int im = 0])
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // GInt.of(re, [im = 0]) from small ints.
  print('${GInt.of(5)}, ${GInt.of(2, -7)}, equal to GInt(BigInt, BigInt): ${GInt.of(2, -7) == GInt(BigInt.two, BigInt.from(-7))}');
}
Output
5 + 0i, 2 + -7i, equal to GInt(BigInt, BigInt): true

Static

zerostaticfield
static final zero
onestaticfield
static final one

Properties

refield
final BigInt re
imfield
final BigInt im
conjgetter
GInt get conj
norm2getter
BigInt get norm2
a + bi² = a² + b².
isZerogetter
bool get isZero
hashCodegetter
int get hashCode

Methods

scalemethod
GInt scale(BigInt k)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // scale(k): multiply both parts by a big integer k.
  final z = GInt.of(2, 3);
  print('${z}·10 = ${z.scale(BigInt.from(10))}');
  print('|kz|² = k²|z|²: ${z.scale(BigInt.from(10)).norm2 == BigInt.from(100) * z.norm2}');
}
Output
2 + 3i·10 = 20 + 30i
|kz|² = k²|z|²: true
GInt timesMinusIPow(int k)

(-i)^{k} · this.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // timesMinusIPow(k): (−i)^k·z, a quarter turn clockwise per k.
  final z = GInt.of(3, 1);
  print([for (var k = 0; k < 5; k++) '(−i)^$k·z = ${z.timesMinusIPow(k)}'].join('\n'));
}
Output
(−i)^0·z = 3 + 1i
(−i)^1·z = 1 + -3i
(−i)^2·z = -3 + -1i
(−i)^3·z = -1 + 3i
(−i)^4·z = 3 + 1i
powmethod
GInt pow(int k)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // pow(k) by squaring: (2 + i)^k; the norms are 5^k exactly.
  final z = GInt.of(2, 1);
  for (final k in [0, 1, 2, 5, 20]) print('(2 + i)^$k = ${z.pow(k)}, |·|² = ${z.pow(k).norm2}');
}
Output
(2 + i)^0 = 1 + 0i, |·|² = 1
(2 + i)^1 = 2 + 1i, |·|² = 5
(2 + i)^2 = 3 + 4i, |·|² = 25
(2 + i)^5 = -38 + 41i, |·|² = 3125
(2 + i)^20 = -9653287 + 1476984i, |·|² = 95367431640625
toStringmethod
String toString()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // toString: "re + imi", with the sign carried by im.
  print(GInt.of(3, 4).toString());
  print(GInt.of(3, -4).toString());
  print('$GInt.zero → ${GInt.zero}');
}
Output
3 + 4i
3 + -4i
GInt.zero → 0 + 0i

Operators

operator +operator
GInt operator +(GInt o)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // z + w, part by part.
  final z = GInt.of(3, 4), w = GInt.of(-1, 2);
  print('($z) + ($w) = ${z + w}');
}
Output
(3 + 4i) + (-1 + 2i) = 2 + 6i
operator -operator
GInt operator -(GInt o)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // z − w, part by part; z − z = 0.
  final z = GInt.of(3, 4), w = GInt.of(-1, 2);
  print('($z) − ($w) = ${z - w}, z − z = ${z - z}');
}
Output
(3 + 4i) − (-1 + 2i) = 4 + 2i, z − z = 0 + 0i
operator *operator
GInt operator *(GInt o)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // z·w; (q − ip)² is the box's integer phase step for τ = p/q.
  final z = GInt.of(3, 4), w = GInt.of(-1, 2);
  print('($z) · ($w) = ${z * w}');
  final u = GInt.of(2, -1) * GInt.of(2, -1);
  print('(2 − i)² = $u, |u|² = ${u.norm2} = 5²');
}
Output
(3 + 4i) · (-1 + 2i) = -11 + 2i
(2 − i)² = 3 + -4i, |u|² = 25 = 5²
operator ==operator
bool operator ==(Object other)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ==: equal real and imaginary parts.
  print('GInt.of(1, 2) == GInt(1, 2): ${GInt.of(1, 2) == GInt(BigInt.one, BigInt.two)}');
  print('(1 + i)² == 2i: ${GInt.of(1, 1) * GInt.of(1, 1) == GInt.of(0, 2)}');
  print('1 + 2i == 2 + i: ${GInt.of(1, 2) == GInt.of(2, 1)}');
}
Output
GInt.of(1, 2) == GInt(1, 2): true
(1 + i)² == 2i: true
1 + 2i == 2 + i: false
final class ExactBoxWave

\psi = \sum g_{n} \varphi_{n} evolved exactly on the box's rational samples t = j/m and their mirrors (2m + 1 points).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A wave packet in the box, evolved exactly in integers; the doubles are made only to draw.
  final w = ExactBoxWave(boxPacket(centre: 6, spread: 4, p: 1, q: 2), m: 12);
  final s = BoxSamples(12);
  final xs = [for (var i = 0; i < w.length; i++) boxSampleX(s.tangent(i).$1, s.tangent(i).$2)];
  final clock = BoxClock(ticksPerQuarter: 8);
  final series = <Map<String, Object>>[];
  for (var k = 0; k < 3; k++) {
    final (a, b) = clock.tau;
    final d = w.densities(clock.quarter, a, b);
    series.add({'label': 'tick ${clock.tick}', 'points': [for (var i = 0; i < w.length; i++) [xs[i], d[i]]]});
    clock.advance(2);
  }
  print('${w.length} samples, top state ${w.top}, ⟨E⟩/E1 = ${w.energy}');
  print('density at the middle, t = 0: ${w.density(12, 0, 0, 1)}');
}
Output
25 samples, top state 8, ⟨E⟩/E1 = 256/7
density at the middle, t = 0: 1024/875

Constructors

ExactBoxWaveconstructor
ExactBoxWave(List<GInt> g, {required this.m})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ExactBoxWave(g, m: m): precomputes sin(nθⱼ) in integers for the 2m + 1 samples.
  final w = ExactBoxWave([GInt.of(1), GInt.of(0, 1)], m: 4);
  print('length = ${w.length}, top = ${w.top}, Σ|g|² = ${w.norm2}, ⟨E⟩/E1 = ${w.energy}');
}
Output
length = 9, top = 2, Σ|g|² = 2, ⟨E⟩/E1 = 5/2

Static

sameDensitiesstaticmethod
static bool sameDensities((List<GInt>, BigInt) x, (List<GInt>, BigInt) y)

Whether two instants (numerators of each) have the same density at every sample, exactly: |num|² over h^{2} compared by cross-multiplying, no division.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Revivals checked exactly: at z₁ = 1 (one full period, quarter 4) the densities equal t = 0, sample for sample.
  final w = ExactBoxWave(boxPacket(centre: 4, spread: 4, p: 1, q: 3), m: 6);
  final t0 = w.numerators(0, 0, 1);
  print('full period (q = 4):   same: ${ExactBoxWave.sameDensities(t0, w.numerators(4, 0, 1))}');
  print('half period (q = 2):   same: ${ExactBoxWave.sameDensities(t0, w.numerators(2, 0, 1))} (z₁ = −1)');
  print('τ = 1/5 (q = 0):       same: ${ExactBoxWave.sameDensities(t0, w.numerators(0, 1, 5))}');
}
Output
full period (q = 4):   same: true
half period (q = 2):   same: false (z₁ = −1)
τ = 1/5 (q = 0):       same: false

Properties

gfield
final List<GInt> g

The coefficients (index n - 1) and the samples per half.

mfield
final int m
topfield
late final int top
norm2field
late final BigInt norm2

\Sigma|g_{n}|^{2}: the probability before dividing, kept exactly by every instant.

lengthgetter
int get length
energygetter
Rational get energy

\frac{\langle E\rangle}{E_{1}} = \sum n^{2}|g_{n}|^{2} / \Sigma|g_{n}|^{2}, exactly (no time in it).

Methods

(List<GInt>, BigInt) numerators(int quarter, int a, int b)

The exact numerators at every sample (left wall to right wall) and the shared time denominator h^{N^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // numerators(q, a, b): the exact Gaussian-integer amplitude at every sample and the shared denominator h^{N²}.
  final w = ExactBoxWave([GInt.of(1), GInt.of(1)], m: 2);
  final (nums, h) = w.numerators(0, 1, 2);
  print('h^{N²} = $h');
  for (final (i, z) in nums.indexed) print('sample $i: $z');
}
Output
h^{N²} = 625
sample 0: 0 + 0i
sample 1: -5148 + -1936i
sample 2: 24000 + -32000i
sample 3: 20148 + -18064i
sample 4: 0 + 0i
densitiesmethod
Float64List densities(int quarter, int a, int b)

L|\psi|^{2} at every sample, as doubles to draw (the numbers are exact until this division).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // densities(q, a, b): L|ψ|² at all samples as doubles (exact until the final division).
  final w = ExactBoxWave([GInt.of(1), GInt.of(1)], m: 8);
  final d = w.densities(0, 0, 1);
  print('L|ψ|² at t = 0: ${d.map((x) => x.toStringAsFixed(4)).join(' ')}');
  final s = BoxSamples(8);
}
Output
L|ψ|² at t = 0: 0.0000 0.5232 1.6927 2.7170 3.0976 2.8448 2.2428 1.5732 1.0000 0.5300 0.1784 0.0123 0.0256 0.1111 0.1295 0.0534 0.0000
densitymethod
Rational density(int i, int quarter, int a, int b)

L|\psi|^{2} at sample i, exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // density(i, q, a, b): one exact rational L|ψ|².
  final w = ExactBoxWave([GInt.of(1), GInt.of(1)], m: 2);
  for (var i = 0; i < w.length; i++) print('sample $i: L|ψ|² = ${w.density(i, 0, 0, 1)}');
}
Output
sample 0: L|ψ|² = 0
sample 1: L|ψ|² = 1936/625
sample 2: L|ψ|² = 1
sample 3: L|ψ|² = 16/625
sample 4: L|ψ|² = 0
List<Rational> exactDensities(int quarter, int a, int b)

Every L|\psi|^{2}, exactly (tests and the revival check).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // exactDensities: every L|ψ|² as a Rational, here for φ₁ + iφ₂ at τ = 1/3.
  final w = ExactBoxWave([GInt.of(1), GInt.of(0, 1)], m: 3);
  final d = w.exactDensities(0, 1, 3);
  print(d.join(', '));
}
Output
0, 36873/15625, 1844496/714025, 1, 92304/714025, 3177/15625, 0
bitsAtmethod
int bitsAt(int a, int b)

The size of the numbers an instant takes (bits of the time denominator h^{N^{2}}).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // bitsAt(a, b): how big the numbers of an instant are: bits(A² + B²)·N².
  final w = ExactBoxWave(boxPacket(centre: 6, spread: 4, p: 1, q: 2), m: 8);
  for (final (a, b) in [(0, 1), (1, 2), (70, 169), (35, 676)]) print('τ = $a/$b: ${w.bitsAt(a, b)} bits');
}
Output
τ = 0/1: 64 bits
τ = 1/2: 192 bits
τ = 70/169: 1024 bits
τ = 35/676: 1216 bits
final class SpreadBoxWave

The spread-polynomial density of BoxState in integers, for every sample, at every clock instant: L|\psi|^{2} = 2 \sum_{n\le m} (2 - \delta_{nm}) \operatorname{Re}(G_{n} \bar{G}_{m})[S_{n + m}(\sigma) - S_{m - n}(\sigma)] / (h^{2N^{2}} \Sigma|g_{k}|^{2}), each S over d^{2N}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The same exact densities by spread polynomials in integers; it agrees with ExactBoxWave sample for sample.
  final g = boxPacket(centre: 5, spread: 4, p: 1, q: 3);
  final s = SpreadBoxWave(g, m: 6), e = ExactBoxWave(g, m: 6);
  final a = s.exactDensities(0, 1, 4), b = e.exactDensities(0, 1, 4);
  print('equal at all ${a.length} samples: ${a.join() == b.join()}');
  final k = s.leftKappa(0, 1, 4);
  print('left-half κ ≈ ${k.toDouble().toStringAsFixed(12)} (exact rational, ${k.denominator.bitLength}-bit denominator)');
  final d = s.densities(0, 1, 4);
  final bs = BoxSamples(6);
}
Output
equal at all 13 samples: true
left-half κ ≈ 0.952244286812 (exact rational, 150-bit denominator)

Constructors

SpreadBoxWaveconstructor
SpreadBoxWave(List<GInt> g, {required this.m})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // SpreadBoxWave(g, m: m): spread-polynomial numerators for every sample, over d^{2N}.
  final s = SpreadBoxWave([GInt.of(0), GInt.of(1)], m: 2);
  print('length ${s.length}, top ${s.top}, Σ|g|² ${s.norm2}');
  print('φ2 densities: ${s.exactDensities(0, 0, 1).join(', ')}');
}
Output
length 5, top 2, Σ|g|² 1
φ2 densities: 0, 1152/625, 0, 1152/625, 0

Properties

gfield
final List<GInt> g
mfield
final int m
topfield
late final int top
norm2field
late final BigInt norm2
lengthgetter
int get length

Methods

densitiesmethod
Float64List densities(int quarter, int a, int b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // densities: the spread-polynomial L|ψ|² as doubles.
  final s = SpreadBoxWave([GInt.of(1), GInt.of(1)], m: 4);
  print(s.densities(0, 0, 1).map((x) => x.toStringAsFixed(6)).join(' '));
}
Output
0.000000 1.692700 3.097600 2.242806 1.000000 0.178422 0.025600 0.129500 0.000000
List<Rational> exactDensities(int quarter, int a, int b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // exactDensities: the same numbers as Rationals.
  final s = SpreadBoxWave([GInt.of(1), GInt.of(1)], m: 2);
  print(s.exactDensities(0, 0, 1).join(', '));
}
Output
0, 1936/625, 1, 16/625, 0
bitsAtmethod
int bitsAt(int a, int b, int top)

Bits of the time denominator h^{N^{2}} at \tau = \frac{a}{b} for top state top.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // bitsAt(a, b, top): the size of the time denominator h^{N²}.
  final s = SpreadBoxWave([GInt.of(1)], m: 2);
  for (final top in [2, 4, 8]) print('τ = 1/2, top $top: ${s.bitsAt(1, 2, top)} bits');
}
Output
τ = 1/2, top 2: 12 bits
τ = 1/2, top 4: 48 bits
τ = 1/2, top 8: 192 bits
leftKappamethod
Rational leftKappa(int quarter, int a, int b)

\kappa of the left-half probability \frac{1}{2} + \frac{\kappa}{\pi}, exactly (see BoxState.leftKappa).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // κ of P(left) = 1/2 + κ/π as the packet sloshes: exact κ at a few instants of the clock.
  final s = SpreadBoxWave(boxPacket(centre: 3, spread: 2, p: 1, q: 2), m: 4);
  final clock = BoxClock(ticksPerQuarter: 4);
  for (var k = 0; k < 5; k++) {
    final (a, b) = clock.tau;
    final kappa = s.leftKappa(clock.quarter, a, b);
    print('q ${clock.quarter}, tick ${clock.tick}: κ ${kappa.denominator.bitLength < 40 ? '= $kappa' : '≈ ${kappa.toDouble().toStringAsFixed(9)} (exact, ${kappa.denominator.bitLength}-bit denominator)'}, P(left) = ${(0.5 + kappa.toDouble() / math.pi).toStringAsFixed(9)}');
    clock.advance();
  }
}
Output
q 0, tick 2: κ = 136/175, P(left) = 0.747372254
q 0, tick 3: κ ≈ -1.134427284 (exact, 105-bit denominator), P(left) = 0.138900580
q 1, tick 0: κ ≈ 0.829711795 (exact, 113-bit denominator), P(left) = 0.764105467
q 1, tick 1: κ ≈ -0.233974117 (exact, 105-bit denominator), P(left) = 0.425523725
q 1, tick 2: κ = 32/175, P(left) = 0.558205236
final class ExactBoxStepper

Coefficients stepped exactly by w = C(\frac{p}{q}) = \frac{(q - ip)^{2}}{q^{2} + p^{2}}: state n by w^{n^{2}} each step. Kept as Gaussian integers N_{n} over h^{n^{2}k} (h = q^{2} + p^{2}, k steps), so a step is one multiplication per state and no gcd. Exactly unitary: |N_{n}|^{2} = |g_{n}|^{2} \cdot h^{2n^{2}k} for every n and k (verify checks it in integers), so \Sigma|c_{n}|^{2} and ⟨E⟩ never move. The price: the numbers grow by about 2n^{2} \cdot \log_{2}h bits a step (bits).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // Integer stepping, exactly unitary; checked in integers. Next to it, the float stepper over the same 60 steps.
  final g = [GInt.of(2), GInt.of(1, 1), GInt.of(0, 1)];
  final ex = ExactBoxStepper(g, p: 1, q: 3);
  final fl = FloatBoxStepper([2, 1, 0], [0, 1, 1], e1dt: 2 * math.atan(1 / 3));
  final f0 = fl.norm2;
  for (var k = 0; k < 60; k++) {
    ex.step();
    fl.step();
  }
  print('exact: ${ex.steps} steps, verify() = ${ex.verify()}, largest numerator ${ex.bits} bits');
  print('float: ${fl.steps} steps, Σ|c|²/start − 1 = ${(fl.norm2 / f0 - 1).toStringAsExponential(2)}');
  final (re, im) = ex.doubles;
  print('c3 now ≈ ${re[2].toStringAsFixed(9)} + ${im[2].toStringAsFixed(9)}i');
}
Output
exact: 60 steps, verify() = true, largest numerator 1794 bits
float: 60 steps, Σ|c|²/start − 1 = 1.78e-15
c3 now ≈ 0.941207100 + -0.337830128i

Constructors

ExactBoxStepperconstructor
ExactBoxStepper(List<GInt> g, {required this.p, required this.q})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ExactBoxStepper(g, p: p, q: q): the step w = (q − ip)²/(q² + p²) kept as integers over h^{n²k}.
  final s = ExactBoxStepper([GInt.of(1), GInt.of(1)], p: 1, q: 2);
  s.step();
  print('h = ${s.h}; after one step c = ${s.exact.map((c) => '${c.$1} + (${c.$2})i').join(', ')}');
}
Output
h = 5; after one step c = 3/5 + (-4/5)i, -527/625 + (336/625)i

Properties

gfield
final List<GInt> g
pfield
final int p
qfield
final int q
hfield
final BigInt h
stepsfield
int steps
bitsgetter
int get bits

The bits of the largest numerator now.

exactgetter
List<(Rational, Rational)> get exact

c_{n} now, exactly.

doublesgetter
(List<double>, List<double>) get doubles

c_{n} now, as doubles (drawing and comparing).

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): one multiplication per state; the numbers grow by about n²·log₂h bits a step.
  final s = ExactBoxStepper([GInt.of(1), GInt.of(1), GInt.of(1)], p: 1, q: 2);
  final pts = <List<double>>[];
  for (var k = 1; k <= 20; k++) {
    s.step();
    pts.add([k.toDouble(), s.bits.toDouble()]);
  }
  print('after ${s.steps} steps: largest numerator ${s.bits} bits');
}
Output
after 20 steps: largest numerator 418 bits
verifymethod
bool verify()
N_{n}² =g_{n}²·h^{2n²k} for every state: probability and energy exactly where they started.
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // verify(): |Nₙ|² = |gₙ|²·h^{2n²k} for every state, checked in integers.
  final s = ExactBoxStepper([GInt.of(3, 1), GInt.of(-1, 2)], p: 2, q: 3);
  for (var k = 0; k < 25; k++) s.step();
  print('after ${s.steps} steps: verify() = ${s.verify()}');
}
Output
after 25 steps: verify() = true
verifyModmethod
bool verifyMod(BigInt p)

The same identity modulo the prime p: linear in the size of the numbers, so it can run every frame.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // verifyMod(p): the same identity modulo a 61-bit prime, cheap enough to run every frame.
  final s = ExactBoxStepper([GInt.of(3, 1), GInt.of(-1, 2), GInt.of(1)], p: 1, q: 4);
  for (var k = 0; k < 40; k++) s.step();
  final p = BigInt.parse('2305843009213693951'); // 2^61 − 1
  print('after ${s.steps} steps: verifyMod(2^61 − 1) = ${s.verifyMod(p)}, verify() = ${s.verify()}');
}
Output
after 40 steps: verifyMod(2^61 − 1) = true, verify() = true
final class ExactCayleyGrid

The discrete box's Cayley (Crank–Nicolson) step in Gaussian integers, fraction-free. With H = \frac{T}{2dx^{2}} + V and A = (\frac{\Delta t}{2})H = \alpha T + \operatorname{diag} \beta_{j}, \alpha = a/b, \beta_{j} = kⱼ/b: M± = bI \pm i(aT + \operatorname{diag} k) are integer matrices and \psi_{k+1} = M_{+}^{-1}M_{-}\psi_{k}, \Phi_{k+1} = \operatorname{adj}(M_{+}) \cdot M_{-} \cdot \Phi_{k}, \psi_{k} = \Phi_{k} / D^{k}, D = \det M_{+}, with adj of the tridiagonal M_{+} from its leading and trailing minors \theta, \varphi (no division anywhere). Exactly unitary: \Vert\Phi_{k}\Vert^{2} = |D|^{2k}\Vert\Phi_{0}\Vert^{2} (verify). It is CayleySchrodinger over the Gaussian rationals without the gcds.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Crank–Nicolson on a 24-point grid in Gaussian integers, 30 steps, next to the same scheme in doubles, 30 steps.
  const n = 24, steps = 30;
  final psi0 = gridPacket(n: n, centre: 7, halfWidth: 5, p: 1, q: 2);
  final k = [for (var j = 0; j < n; j++) j >= 14 && j < 17 ? 1 : 0]; // a barrier, V = k/b
  final ex = ExactCayleyGrid(psi0, a: 1, b: 2, k: k);
  // The float twin: dx = 1, dt = 4a/b = 2, V = k/b.
  var total = BigInt.zero;
  for (final z in psi0) total += z.norm2;
  final fl = CayleySchrodinger<double>(const CxArith(F64Arith()),
      psi: [for (final z in psi0) Cx(bigRatio(z.re, total), bigRatio(z.im, total))],
      potential: [for (final x in k) x / 2], dx: 1, dt: 2);
  final f0 = fl.norm2;
  final start = ex.densities();
  for (var s = 0; s < steps; s++) {
    ex.step();
    fl.step();
  }
  print('exact: ${ex.steps} steps, ‖Φ‖² = |D|^{2k}‖Φ₀‖²: ${ex.verify()}, entries up to ${ex.bits} bits');
  print('float: ${fl.steps} steps, Σ|ψ|²/start − 1 = ${(fl.norm2 / f0 - 1).toStringAsExponential(2)}');
  final end = ex.densities();
}
Output
exact: 30 steps, ‖Φ‖² = |D|^{2k}‖Φ₀‖²: true, entries up to 1192 bits
float: 30 steps, Σ|ψ|²/start − 1 = 1.11e-15

Constructors

ExactCayleyGridconstructor
ExactCayleyGrid(List<GInt> psi, {required this.a, required this.b, List<int>? k})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ExactCayleyGrid(psi, a: a, b: b, k: k): α = a/b, βⱼ = kⱼ/b; it precomputes D = det M₊ from the minors.
  final g = ExactCayleyGrid([GInt.of(1), GInt.of(0), GInt.of(0)], a: 1, b: 1);
  print('n = ${g.n}, det M₊ = ${g.det}, |D|² = ${g.det.norm2}, ‖Φ₀‖² = ${g.norm0}');
}
Output
n = 3, det M₊ = -9 + 2i, |D|² = 85, ‖Φ₀‖² = 1

Properties

nfield
final int n
afield
final int a
bfield
final int b
kfield
final List<int> k
detfield
late final GInt det

D = \det M_{+} and \Vert\Phi_{0}\Vert^{2}.

norm0field
late final BigInt norm0
stepsfield
int steps
phigetter
List<GInt> get phi
bitsgetter
int get bits

Bits of the largest entry now.

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): Φ ← adj(M₊)·M₋·Φ, fraction-free; the bits grow by about log₂|D|² per step.
  final g = ExactCayleyGrid([GInt.of(0), GInt.of(1), GInt.of(0), GInt.of(0)], a: 1, b: 2);
  for (var s = 0; s < 4; s++) {
    g.step();
    print('step ${g.steps}: Φ = ${g.phi.join(', ')}');
  }
}
Output
step 1: Φ = -32 + 4i, 7 + 48i, -32 + 0i, -8 + -8i
step 2: Φ = -896 + -2952i, -223 + 544i, 128 + -2624i, 1696 + -1008i
step 3: Φ = 130784 + -99572i, -12921 + 162448i, 67296 + 40192i, 82616 + 165352i
step 4: Φ = 7746048 + -610064i, -13166335 + 956480i, -6192640 + -6897280i, -9330048 + 4370464i
verifymethod
bool verify()

\Vert\Phi_{k}\Vert^{2} = |D|^{2k}\Vert\Phi_{0}\Vert^{2}: \Sigma|\psi|^{2} exactly where it started (a check in integers; it costs a squaring).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // verify(): Σ|ψ|² exactly where it started, checked as ‖Φₖ‖² = |D|^{2k}‖Φ₀‖² in integers.
  final g = ExactCayleyGrid(gridPacket(n: 12, centre: 5, halfWidth: 3, p: 1, q: 3), a: 1, b: 3, k: [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]);
  for (var s = 0; s < 15; s++) g.step();
  print('after ${g.steps} steps: verify() = ${g.verify()}');
}
Output
after 15 steps: verify() = true
verifyModmethod
bool verifyMod(BigInt p)

The same identity modulo the prime p (linear in the size of the numbers).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // verifyMod(p): the conservation identity modulo a 61-bit prime.
  final g = ExactCayleyGrid(gridPacket(n: 12, centre: 5, halfWidth: 3, p: 1, q: 3), a: 1, b: 3);
  for (var s = 0; s < 15; s++) g.step();
  print('verifyMod(2^61 − 31) = ${g.verifyMod(BigInt.parse('2305843009213693921'))}');
}
Output
verifyMod(2^61 − 31) = true
densitiesmethod
Float64List densities()
\psi_{j}²/Σ\psi² as doubles, to draw.
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // densities(): |ψⱼ|²/Σ|ψ|² to draw; the packet moves right with momentum ≈ 2·atan(1/2).
  final g = ExactCayleyGrid(gridPacket(n: 30, centre: 8, halfWidth: 5, p: 1, q: 2), a: 1, b: 2);
  final series = <Map<String, Object>>[];
  for (var s = 0; s <= 20; s++) {
    if (s % 10 == 0) {
      final d = g.densities();
      series.add({'label': 'step $s', 'points': [for (var j = 0; j < 30; j++) [j.toDouble(), d[j]]]});
      var mean = 0.0;
      for (var j = 0; j < 30; j++) mean += j * d[j];
      print('step $s: ⟨j⟩ = ${mean.toStringAsFixed(4)}');
    }
    g.step();
  }
}
Output
step 0: ⟨j⟩ = 8.0000
step 10: ⟨j⟩ = 19.9550
step 20: ⟨j⟩ = 25.6273
final class QuadranceChain<T>

A chain known by its hopping quadrances q (length L - 1) and on-site energies a (length L), over T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A Krawtchouk waveguide chain known by its hopping quadrances; light put in site 0 hops to site N.
  // Exact Cayley steps (Gaussian rationals) and float Cayley steps, 12 each, same dt.
  const qa = CxArith<Rational>(ExactArith()), fa = CxArith<double>(F64Arith());
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 4, lambda: Rational.one);
  final fch = QuadranceChain.krawtchouk(const F64Arith(), n: 4, lambda: 1.0);
  var phi = ch.unitAt(qa, 0), fphi = fch.unitAt(fa, 0);
  final dt = Rational.fromInt(1, 4);
  final rows = <List<double>>[];
  for (var k = 0; k < 12; k++) {
    phi = ch.cayleyStep(qa, phi, dt);
    fphi = fch.cayleyStep(fa, fphi, 0.25);
    rows.add([for (final p in ch.normalized(qa, phi)) p.toDouble()]);
  }
  print('quadrances q = ${ch.q.join(', ')}; weights w = ${ch.weights.join(', ')}');
  print('exact: Σ w|φ|² after 12 steps = ${ch.norm2(qa, phi)} (started at 1)');
  print('float: Σ w|φ|² after 12 steps − 1 = ${(fch.norm2(fa, fphi) - 1).toStringAsExponential(2)}');
  print('eigenvalues (Sturm): ${ch.eigenvalues(iterations: 40).map((e) => e.toStringAsFixed(6)).join(', ')}');
}
Output
quadrances q = 1, 3/2, 3/2, 1; weights w = 1, 1, 3/2, 9/4, 9/4
exact: Σ w|φ|² after 12 steps = 1 (started at 1)
float: Σ w|φ|² after 12 steps − 1 = -4.44e-16
eigenvalues (Sturm): -2.000000, -1.000000, 0.000000, 1.000000, 2.000000

Constructors

QuadranceChainconstructor
QuadranceChain(this.f, {required List<T> quadrances, List<T>? onsite})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // QuadranceChain(f, quadrances:, onsite:): any chain by q_n = J_n² and on-site energies a_n.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final ch = QuadranceChain(f, quadrances: [r(1), r(2), r(1, 2)], onsite: [r(0), r(1), r(0), r(-1)]);
  print('${ch.length} sites, q = ${ch.q.join(', ')}, a = ${ch.a.join(', ')}');
  print('H′ applied to (1, 0, 0, 0): ${ch.apply([r(1), r(0), r(0), r(0)]).join(', ')}');
}
Output
4 sites, q = 1, 2, 1/2, a = 0, 1, 0, -1
H′ applied to (1, 0, 0, 0): 0, 1, 0, 0
factory QuadranceChain.ssh(KeplerArith<T> f, {required int cells, required T v2, required T w2, int? wallAt, bool extraSite = false})

The SSH chain with cells cells (2·cells sites, or one more with extraSite): intra-cell hopping quadrance v2 and inter-cell w2. With wallAt (a cell index) the two swap from that cell on: a domain wall.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The SSH chain: intra-cell v², inter-cell w². With w > v the open chain has two near-zero edge modes.
  final triv = QuadranceChain.ssh(const F64Arith(), cells: 6, v2: 1.0, w2: 0.25);
  final topo = QuadranceChain.ssh(const F64Arith(), cells: 6, v2: 0.25, w2: 1.0);
  final et = triv.eigenvalues(), eo = topo.eigenvalues();
  print('trivial (v² = 1, w² = 1/4): two middle eigenvalues ${et[5].toStringAsFixed(6)}, ${et[6].toStringAsFixed(6)}');
  print('topological (v² = 1/4, w² = 1): ${eo[5].toStringAsExponential(3)}, ${eo[6].toStringAsExponential(3)}');
}
Output
trivial (v² = 1, w² = 1/4): two middle eigenvalues -0.573320, 0.573320
topological (v² = 1/4, w² = 1): -1.173e-2, 1.173e-2
factory QuadranceChain.krawtchouk(KeplerArith<T> f, {required int n, required T lambda})

The Krawtchouk (Kravchuk) chain of N + 1 sites: q_{n} = (\frac{\lambda}{2})^{2}(n + 1)(N - n), the hoppings of the spin-\frac{N}{2} operator \lambda J_{x}. Its spectrum is exactly \lambda \cdot (-\frac{N}{2}, \ldots, \frac{N}{2}): equidistant, and light hops from site 0 to site N.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Krawtchouk chain: q_n = (λ/2)²(n + 1)(N − n); spectrum exactly λ·(−N/2 … N/2), equally spaced.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 6, lambda: Rational.fromInt(1, 2));
  print('q = ${ch.q.join(', ')}');
  for (var k = -3; k <= 3; k++) {
    final e = Rational.fromInt(k, 2);
    print('λk = $e: eigenvalues below λk + 1/4: ${ch.countBelow(e + Rational.fromInt(1, 4))}');
  }
}
Output
q = 3/8, 5/8, 3/4, 3/4, 5/8, 3/8
λk = -3/2: eigenvalues below λk + 1/4: 1
λk = -1: eigenvalues below λk + 1/4: 2
λk = -1/2: eigenvalues below λk + 1/4: 3
λk = 0: eigenvalues below λk + 1/4: 4
λk = 1/2: eigenvalues below λk + 1/4: 5
λk = 1: eigenvalues below λk + 1/4: 6
λk = 3/2: eigenvalues below λk + 1/4: 7
factory QuadranceChain.towardDimers(KeplerArith<T> f, {required int n, required T lambda, required T dimer, required T s})

The chain on the way from Krawtchouk (s = 0) to isolated dimers (s = 1): the quadrances move linearly, q(s) = (1 - s)q_{K} + s \cdot q_{D}, with the dimer bonds (2k, 2k + 1) at dimer and the others 0. Khanikaev's readout: the adiabatic passage turns chiral partners into symmetric/antisymmetric dimer modes (parity entanglement).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // From the Krawtchouk chain (s = 0) to isolated dimers (s = 1): the quadrances move linearly; the spectrum follows.
  const f = F64Arith();
  final series = <Map<String, Object>>[];
  final eig = [for (var i = 0; i <= 10; i++) QuadranceChain.towardDimers(f, n: 4, lambda: 1.0, dimer: 1.0, s: i / 10).eigenvalues()];
  for (var k = 0; k < 5; k++) series.add({'label': 'E$k', 'points': [for (var i = 0; i <= 10; i++) [i / 10, eig[i][k]]]});
  print('s = 0: ${eig[0].map((e) => e.toStringAsFixed(4)).join(', ')}');
  print('s = 1: ${eig[10].map((e) => e.toStringAsFixed(4)).join(', ')}');
  final half = QuadranceChain.towardDimers(const ExactArith(), n: 4, lambda: Rational.one, dimer: Rational.one, s: Rational.fromInt(1, 2));
  print('exact q at s = 1/2: ${half.q.join(', ')}');
}
Output
s = 0: -2.0000, -1.0000, 0.0000, 1.0000, 2.0000
s = 1: -1.0000, -1.0000, 0.0000, 1.0000, 1.0000
exact q at s = 1/2: 1, 3/4, 5/4, 1/2

Properties

ffield
final KeplerArith<T> f
qfield
final List<T> q
afield
final List<T> a
lengthgetter
int get length
weightsfield
late final List<T> weights

w_{n} = s_{n}^{2} = \prod_{k < n} q_{k} (restarting at 1 after a cut).

double get spectralBound

A bound on every eigenvalue's size: \max_{n} |a_{n}| + q_{n} + 1 (the row sums of H', which has the same spectrum).

Methods

lowermethod
T lower(int n)

H'_{n+1,n}: 1, or 0 across a cut.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // lower(n): H′ below the diagonal is 1, or 0 where q_n = 0 (the chain falls apart there).
  const f = ExactArith();
  final ch = QuadranceChain(f, quadrances: [Rational.one, Rational.zero, Rational.fromInt(2)]);
  print([for (var n = 0; n < 3; n++) 'lower($n) = ${ch.lower(n)}'].join(', '));
}
Output
lower(0) = 1, lower(1) = 0, lower(2) = 1
applymethod
List<T> apply(List<T> phi)

H'\varphi (real).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // apply(φ) = H′φ. The Krawtchouk mode for E = λ is mapped to λ times itself, exactly.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 4, lambda: Rational.one);
  final v = ch.eigenvector(Rational.one);
  print('φ = ${v.join(', ')}');
  print('H′φ = ${ch.apply(v).join(', ')}');
}
Output
φ = 1, 1, 0, -2/3, -2/3
H′φ = 1, 1, 0, -2/3, -2/3
List<T> probabilities(CxArith<T> c, List<Cx<T>> phi)
\psi_{n}² = w_n\varphi_{n}² for a complex state in the rational basis.
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // probabilities: |ψ_n|² = w_n|φ_n|² from the rational basis.
  const c = CxArith<Rational>(ExactArith());
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 2, lambda: Rational.one);
  final phi = ch.cayleyStep(c, ch.unitAt(c, 0), Rational.one);
  print('weights ${ch.weights.join(', ')}');
  print('|ψ_n|² = ${ch.probabilities(c, phi).join(', ')}, total ${ch.norm2(c, phi)}');
}
Output
weights 1, 1/2, 1/4
|ψ_n|² = 16/25, 8/25, 1/25, total 1
norm2method
T norm2(CxArith<T> c, List<Cx<T>> phi)

\sum w_{n} |\varphi_{n}|^{2}: the probability, conserved exactly by cayleyStep.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // norm2: Σ w_n|φ_n|², kept exactly by every Cayley step over the Gaussian rationals.
  const c = CxArith<Rational>(ExactArith());
  final ch = QuadranceChain.ssh(const ExactArith(), cells: 2, v2: Rational.fromInt(1, 4), w2: Rational.one);
  var phi = ch.unitAt(c, 1);
  final n0 = ch.norm2(c, phi);
  for (var k = 0; k < 8; k++) phi = ch.cayleyStep(c, phi, Rational.fromInt(1, 2));
  print('Σ w|φ|²: start $n0, after 8 steps ${ch.norm2(c, phi)}');
}
Output
Σ w|φ|²: start 1/4, after 8 steps 1/4
unitAtmethod
List<Cx<T>> unitAt(CxArith<T> c, int site)

A photon on one site: \varphi = e_{\mathrm{site}} (\psi = s_{\mathrm{site}} \cdot e_{\mathrm{site}}, weight w_{\mathrm{site}}, so read it with normalized).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // unitAt(site): light in one waveguide, φ = e_site (its probability weight is w_site; read with normalized).
  const c = CxArith<Rational>(ExactArith());
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 3, lambda: Rational.one);
  final phi = ch.unitAt(c, 2);
  print('φ = ${phi.map((z) => '${z.re}').join(', ')}, weight w_2 = ${ch.weights[2]}, normalized ${ch.normalized(c, phi).join(', ')}');
}
Output
φ = 0, 0, 1, 0, weight w_2 = 3/4, normalized 0, 0, 1, 0
List<T> normalized(CxArith<T> c, List<Cx<T>> phi)
\psi_{n}² / Σ\psi²: the probabilities of any state in the rational basis, normalised by its (exactly conserved)

Weighted norm.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // normalized: |ψ_n|²/Σ|ψ|² for any state in the rational basis.
  const c = CxArith<Rational>(ExactArith());
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 3, lambda: Rational.one);
  final phi = ch.cayleyStep(c, ch.unitAt(c, 0), Rational.fromInt(1, 2));
  final p = ch.normalized(c, phi);
  print('p = ${p.join(', ')}');
  print('sum = ${p.reduce((a, b) => a + b)}');
}
Output
p = 18861649/22515025, 3447552/22515025, 196608/22515025, 9216/22515025
sum = 1
List<Cx<T>> cayleyStep(CxArith<T> c, List<Cx<T>> phi, T dt)

One Cayley (Crank–Nicolson) step of length dt: (I + i(\frac{dt}{2})H')\varphi' = (I - i(\frac{dt}{2})H')\varphi, by the Thomas algorithm. Over the Gaussian rationals the weighted norm is conserved exactly.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // cayleyStep: (I + i(dt/2)H′)φ′ = (I − i(dt/2)H′)φ by the Thomas algorithm. Exact and float, 40 steps each.
  const qa = CxArith<Rational>(ExactArith()), fa = CxArith<double>(F64Arith());
  final ch = QuadranceChain.ssh(const ExactArith(), cells: 2, v2: Rational.fromInt(1, 4), w2: Rational.one, extraSite: true);
  final fch = QuadranceChain.ssh(const F64Arith(), cells: 2, v2: 0.25, w2: 1.0, extraSite: true);
  var phi = ch.unitAt(qa, 0), fphi = fch.unitAt(fa, 0);
  final pts = <List<double>>[];
  for (var k = 1; k <= 40; k++) {
    phi = ch.cayleyStep(qa, phi, Rational.fromInt(1, 2));
    fphi = fch.cayleyStep(fa, fphi, 0.5);
    pts.add([k.toDouble(), fch.norm2(fa, fphi) - 1]);
  }
  print('exact norm after 40 steps: ${ch.norm2(qa, phi)}');
  print('float norm after 40 steps − 1: ${(fch.norm2(fa, fphi) - 1).toStringAsExponential(2)}');
}
Output
exact norm after 40 steps: 1
float norm after 40 steps − 1: -2.66e-15
int countBelow(T x)

The number of eigenvalues below x, by the Sturm sequence of the leading minors (only {+}\ {-}\ \times\ \div and signs).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // countBelow(x): eigenvalues below x from the Sturm sequence of pivots, with + − × ÷ and signs only.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 4, lambda: Rational.one); // spectrum −2, −1, 0, 1, 2
  for (final x in [-3, -1, 0, 1, 3]) print('x = $x: ${ch.countBelow(Rational.fromInt(x))} eigenvalues below');
}
Output
x = -3: 0 eigenvalues below
x = -1: 1 eigenvalues below
x = 0: 2 eigenvalues below
x = 1: 3 eigenvalues below
x = 3: 5 eigenvalues below
(T, T) eigenvalueBracket(int k, {int iterations = 60})

The k-th eigenvalue (ascending, 0-based) pinned to an interval by iterations bisections, in T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // eigenvalueBracket(k): bisection to an exact rational interval holding the k-th eigenvalue.
  final ch = QuadranceChain(const ExactArith(), quadrances: [Rational.one, Rational.fromInt(2)]); // eigenvalues 0, ±√3
  final (lo, hi) = ch.eigenvalueBracket(2, iterations: 20);
  print('E_2 ∈ [$lo, $hi]');
  print('width ${hi - lo}; √3 ≈ 1.7320508, inside: ${lo.toDouble() < 1.7320508 && 1.7320508 < hi.toDouble()}');
}
Output
E_2 ∈ [227023/131072, 14189/8192]
width 1/131072; √3 ≈ 1.7320508, inside: true
List<double> eigenvalues({int iterations = 60})

All eigenvalues (ascending) by Sturm bisection, as doubles.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // eigenvalues(): every eigenvalue by Sturm bisection, as doubles; next to the classic Jacobi rotations.
  final q = [0.5, 1.5, 0.25, 2.0, 1.0];
  final ch = QuadranceChain(const F64Arith(), quadrances: q);
  final e = ch.eigenvalues();
  final c = ClassicChain(q).energies;
  print('Sturm:  ${e.map((x) => x.toStringAsFixed(10)).join(', ')}');
  print('Jacobi: ${c.map((x) => x.toStringAsFixed(10)).join(', ')}');
}
Output
Sturm:  -1.8353366950, -1.3644066034, -0.1411872392, 0.1411872392, 1.3644066034, 1.8353366950
Jacobi: -1.8353366950, -1.3644066034, -0.1411872392, 0.1411872392, 1.3644066034, 1.8353366950
List<T> eigenvector(T e)

The eigenvector of H' for the exact eigenvalue e, by the recursion \varphi_{n + 1} = ((e - a_{n})\varphi_{n} − lower \varphi_{n - 1})/q_{n} from \varphi_{0} = 1 (needs every q_{n} \ne 0). Exact over \mathbb{Q} when e is.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // eigenvector(E): the three-term recursion for an exact eigenvalue; exact rational components.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 4, lambda: Rational.one);
  for (final e in [-2, 0, 2]) {
    final v = ch.eigenvector(Rational.fromInt(e));
    print('E = $e: φ = ${v.join(', ')}; H′φ = Eφ: ${ch.apply(v).join() == [for (final x in v) x * Rational.fromInt(e)].join()}');
  }
}
Output
E = -2: φ = 1, -2, 2, -4/3, 2/3; H′φ = Eφ: true
E = 0: φ = 1, 0, -2/3, 0, 2/3; H′φ = Eφ: true
E = 2: φ = 1, 2, 2, 4/3, 2/3; H′φ = Eφ: true
zeroModemethod
List<T> zeroMode()

The zero mode of a bipartite chain (a = 0) with an odd number of sites: \varphi_{2k + 2} = -\frac{\varphi_{2k}}{q_{2k + 1}}, odd sites 0. H'\varphi = 0 exactly. It lives on one sublattice and is its own chiral partner (Khanikaev, 16:47).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // zeroMode(): the SSH edge state with H′φ = 0 exactly, living on one sublattice.
  final ch = QuadranceChain.ssh(const ExactArith(), cells: 3, v2: Rational.fromInt(1, 4), w2: Rational.one, extraSite: true);
  final z = ch.zeroMode();
  print('φ = ${z.join(', ')}');
  print('H′φ = ${ch.apply(z).join(', ')}');
  final p = [for (var n = 0; n < ch.length; n++) ch.weights[n] * z[n] * z[n]];
  final tot = p.reduce((a, b) => a + b);
  print('|ψ_n|²/Σ = ${[for (final x in p) x / tot].join(', ')}');
}
Output
φ = 1, 0, -1, 0, 1, 0, -1
H′φ = 0, 0, 0, 0, 0, 0, 0
|ψ_n|²/Σ = 64/85, 0, 16/85, 0, 4/85, 0, 1/85
realNorm2method
T realNorm2(List<T> phi)

\sum w_{n} \varphi_{n}^{2} of a real vector in the rational basis (its squared norm).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // realNorm2(φ): Σ w_n φ_n² for a real vector in the rational basis.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 2, lambda: Rational.one);
  final v = ch.eigenvector(Rational.one);
  print('φ = ${v.join(', ')}, w = ${ch.weights.join(', ')}, Σ wφ² = ${ch.realNorm2(v)}');
}
Output
φ = 1, 2, 2, w = 1, 1/2, 1/4, Σ wφ² = 4
partnermethod
List<T> partner(List<T> phi)

The chiral partner \Gamma\varphi (the sign flips on odd sites): an eigenvector for -E when \varphi is one for E (a = 0).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // partner(φ): the chiral partner Γφ (odd sites flipped) is an eigenvector for −E.
  final ch = QuadranceChain.krawtchouk(const ExactArith(), n: 4, lambda: Rational.one);
  final v = ch.eigenvector(Rational.one), p = ch.partner(v);
  print('φ(E = 1) = ${v.join(', ')}');
  print('Γφ       = ${p.join(', ')}');
  print('H′Γφ = −Γφ: ${ch.apply(p).join() == [for (final x in p) -x].join()}');
}
Output
φ(E = 1) = 1, 1, 0, -2/3, -2/3
Γφ       = 1, -1, 0, 2/3, -2/3
H′Γφ = −Γφ: true
final class KrawtchoukTransfer<T>

Krawtchouk perfect state transfer in closed form: light put into site 0 of the (N + 1)-site Krawtchouk chain.

Classic: the evolution is the spin-\frac{N}{2} rotation \exp(-i\lambda zJ_{x}); P_{n}(z) = C(N, n) \sin^{2n}(\frac{\lambda z}{2}) \cos^{2(N - n)}(\frac{\lambda z}{2}). Rational: the same rotation as the Cayley rotor u = (I − iA)²/(1 + |A|²), A = t\sigma_{x}, whose spread is s = |u_{21}|^{2} = \frac{4t^{2}}{(1 + t^{2})^{2}}; then P_{n} = C(N, n) s^{n}(1 - s)^{N - n} exactly, and at t = 1 (s = 1) all the light is in site N: perfect transfer as an exact rational statement.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // Perfect state transfer: at t = 1 (spread s = 1) all light sits in site N, exactly; classic sin/cos alongside.
  const f = ExactArith();
  final kt = KrawtchoukTransfer(f, 4);
  for (final t in [Rational.fromInt(1, 3), Rational.fromInt(1, 2), Rational.one]) {
    final s = kt.spread(t);
    print('t = $t: s = $s, P = ${kt.probabilities(s).join(', ')}');
  }
  final series = [for (var site = 0; site <= 4; site++) {'label': 'site $site', 'points': [for (var i = 0; i <= 40; i++) [i / 40 * math.pi, classicKrawtchoukProbabilities(4, i / 40 * math.pi)[site]]]}];
}
Output
t = 1/3: s = 9/25, P = 65536/390625, 147456/390625, 124416/390625, 46656/390625, 6561/390625
t = 1/2: s = 16/25, P = 6561/390625, 46656/390625, 124416/390625, 147456/390625, 65536/390625
t = 1: s = 1, P = 0, 0, 0, 0, 1

Constructors

const KrawtchoukTransfer(this.f, this.n)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // KrawtchoukTransfer(f, N) over the rationals or doubles.
  final q = KrawtchoukTransfer(const ExactArith(), 2), d = KrawtchoukTransfer(const F64Arith(), 2);
  print('exact P at s = 1/2: ${q.probabilities(Rational.fromInt(1, 2)).join(', ')}');
  print('float P at s = 0.5: ${d.probabilities(0.5).join(', ')}');
}
Output
exact P at s = 1/2: 1/4, 1/2, 1/4
float P at s = 0.5: 0.25, 0.5, 0.25

Properties

ffield
final KeplerArith<T> f
nfield
final int n

Methods

spreadmethod
T spread(T t)

The rotor's spread at the half-tangent t: \frac{4t^{2}}{(1 + t^{2})^{2}}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // spread(t) = 4t²/(1 + t²)²: the Cayley rotor's spread at half-tangent t; t = 1 gives 1.
  final kt = KrawtchoukTransfer(const ExactArith(), 3);
  print([for (final t in [0, 1, 2, 3, 4]) 's(${t}/4) = ${kt.spread(Rational.fromInt(t, 4))}'].join(', '));
}
Output
s(0/4) = 0, s(1/4) = 64/289, s(2/4) = 16/25, s(3/4) = 576/625, s(4/4) = 1
List<T> probabilities(T s)

P_{n} for every site at spread s: C(N, k) s^{k} (1 - s)^{N - k}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // probabilities(s): binomial site probabilities; they sum to 1 exactly.
  final kt = KrawtchoukTransfer(const ExactArith(), 6);
  final p = kt.probabilities(Rational.fromInt(1, 3));
  print('P = ${p.join(', ')}');
  print('sum = ${p.reduce((a, b) => a + b)}');
}
Output
P = 64/729, 64/243, 80/243, 160/729, 20/243, 4/243, 1/729
sum = 1
final class ClassicChain

The classic counterpart: the chain with hoppings J_{n} = \sqrt{q}_{n}, diagonalised by Jacobi rotations (a root per rotation), evolved by e^{−iEt} = cos Et - i sin Et (badges Transcendental, Root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The classic chain: hoppings √q, Jacobi diagonalisation, e^{−iEt} by cos and sin.
  final c = ClassicChain([1.0, 1.5, 1.5, 1.0]); // Krawtchouk, N = 4, λ = 1
  print('energies: ${c.energies.map((e) => e.toStringAsFixed(10)).join(', ')}');
  final series = <Map<String, Object>>[];
  for (var site = 0; site < 5; site++) {
    series.add({'label': 'site $site', 'points': [for (var i = 0; i <= 40; i++) () { final p = c.evolve([1, 0, 0, 0, 0], i * 0.1)[site]; return [i * 0.1, p.$1 * p.$1 + p.$2 * p.$2]; }()]});
  }
  final end = c.evolve([1, 0, 0, 0, 0], 3.141592653589793);
  print('at t = π: P(site 4) = ${(end[4].$1 * end[4].$1 + end[4].$2 * end[4].$2).toStringAsFixed(12)}');
}
Output
energies: -2.0000000000, -1.0000000000, 0.0000000000, 1.0000000000, 2.0000000000
at t = π: P(site 4) = 1.000000000000

Constructors

ClassicChainconstructor
ClassicChain(List<double> quadrances, {List<double>? onsite})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ClassicChain(quadrances, onsite:): diagonalised at construction; eigenvectors in vectors[i][k].
  final c = ClassicChain([1.0], onsite: [0.0, 0.0]);
  print('energies ${c.energies}, mode 0 = (${c.vectors[0][0].toStringAsFixed(12)}, ${c.vectors[1][0].toStringAsFixed(12)})');
}
Output
energies [-0.9999999999999998, 0.9999999999999998], mode 0 = (0.707106781187, -0.707106781187)

Properties

nfield
final int n
hopfield
final List<double> hop
onsitefield
final List<double> onsite
late List<double> energies

Eigenvalues (ascending) and eigenvectors (columns of vectors, vectorsk = component i of mode k).

vectorsfield
late List<List<double>> vectors

Methods

evolvemethod
List<(double, double)> evolve(List<double> psi0, double t)

\psi(t) = V e^{-i\Lambda t} V^{T} \psi(0) for a real start psi0; returns (re, im) per site.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // evolve(ψ₀, t): two coupled waveguides swap light as cos² and sin².
  final c = ClassicChain([1.0]);
  for (final t in [0.0, 0.5, 1.0, 1.5707963267948966]) {
    final p = c.evolve([1, 0], t);
    print('t = $t: |ψ0|² = ${(p[0].$1 * p[0].$1 + p[0].$2 * p[0].$2).toStringAsFixed(9)}, |ψ1|² = ${(p[1].$1 * p[1].$1 + p[1].$2 * p[1].$2).toStringAsFixed(9)}');
  }
}
Output
t = 0.0: |ψ0|² = 1.000000000, |ψ1|² = 0.000000000
t = 0.5: |ψ0|² = 0.770151153, |ψ1|² = 0.229848847
t = 1.0: |ψ0|² = 0.291926582, |ψ1|² = 0.708073418
t = 1.5707963267948966: |ψ0|² = 0.000000000, |ψ1|² = 1.000000000
class

Cx

final class Cx<T>

a + bi over T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Cx(re, im): a + bi over any field. Over the rationals these are the Gaussian rationals.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final z = Cx(r(1, 2), r(-3, 4)), w = Cx(r(2), r(1));
  print('z = ${z.re} + (${z.im})i, w = ${w.re} + (${w.im})i');
  final p = c.mul(z, w), q = c.div(z, w);
  print('z·w = ${p.re} + (${p.im})i, z/w = ${q.re} + (${q.im})i');
  print('(z/w)·w == z: ${c.mul(q, w).re == z.re && c.mul(q, w).im == z.im}');
}
Output
z = 1/2 + (-3/4)i, w = 2 + (1)i
z·w = 7/4 + (-1)i, z/w = 1/20 + (-2/5)i
(z/w)·w == z: true

Constructors

Cxconstructor
const Cx(this.re, this.im)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Cx(re, im) holds the two parts; double or Rational.
  const a = Cx(0.5, -1.0);
  final b = Cx(Rational.fromInt(1, 3), Rational.fromInt(2, 3));
  print('a = ${a.re} + (${a.im})i');
  print('b = ${b.re} + (${b.im})i, |b|² = ${const CxArith<Rational>(ExactArith()).norm2(b)}');
}
Output
a = 0.5 + (-1.0)i
b = 1/3 + (2/3)i, |b|² = 5/9

Properties

refield
final T re
imfield
final T im
class

CxArith

final class CxArith<T>

Arithmetic on Cx built from a KeplerArith (no roots, no transcendental functions).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Complex arithmetic from a field: + − × ÷, conjugate, |·|², and the Cayley phase. Exact over the rationals.
  const c = CxArith<Rational>(ExactArith());
  final z = c.cayleyPhase(Rational.fromInt(1, 2));
  print('C(1/2) = ${z.re} + (${z.im})i, |C|² = ${c.norm2(z)}');
  final pts = <List<Object>>[];
  var p = c.one;
  for (var k = 0; k < 8; k++) {
    pts.add([p.re.toDouble(), p.im.toDouble(), '$k']);
    p = c.mul(p, z);
  }
  print('C(1/2)^8 = ${p.re} + (${p.im})i, |·|² = ${c.norm2(p)}');
}
Output
C(1/2) = 3/5 + (4/5)i, |C|² = 1
C(1/2)^8 = 164833/390625 + (354144/390625)i, |·|² = 1

Constructors

CxArithconstructor
const CxArith(this.f)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // CxArith(f) over ExactArith or F64Arith.
  const q = CxArith<Rational>(ExactArith()), d = CxArith<double>(F64Arith());
  print('exact one = ${q.one.re} + (${q.one.im})i, zero = ${q.zero.re} + (${q.zero.im})i');
  print('float i·i = ${d.mul(const Cx(0.0, 1.0), const Cx(0.0, 1.0)).re}');
}
Output
exact one = 1 + (0)i, zero = 0 + (0)i
float i·i = -1.0

Properties

ffield
final KeplerArith<T> f
zerogetter
Cx<T> get zero
onegetter
Cx<T> get one

Methods

ofmethod
Cx<T> of(T re, [T? im])
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // of(re, [im]): a complex number, im defaulting to 0.
  const c = CxArith<Rational>(ExactArith());
  final a = c.of(Rational.fromInt(3)), b = c.of(Rational.fromInt(1), Rational.fromInt(-2));
  print('${a.re} + (${a.im})i, ${b.re} + (${b.im})i');
}
Output
3 + (0)i, 1 + (-2)i
addmethod
Cx<T> add(Cx<T> a, Cx<T> b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = c.add(Cx(r(1, 2), r(1, 3)), Cx(r(1, 4), r(-1, 6)));
  print('(1/2 + i/3) + (1/4 − i/6) = ${s.re} + (${s.im})i');
}
Output
(1/2 + i/3) + (1/4 − i/6) = 3/4 + (1/6)i
submethod
Cx<T> sub(Cx<T> a, Cx<T> b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = c.sub(Cx(r(1, 2), r(1, 3)), Cx(r(1, 4), r(-1, 6)));
  print('(1/2 + i/3) − (1/4 − i/6) = ${s.re} + (${s.im})i');
}
Output
(1/2 + i/3) − (1/4 − i/6) = 1/4 + (1/2)i
mulmethod
Cx<T> mul(Cx<T> a, Cx<T> b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // mul: (3 + 4i)(3 − 4i) = 25, and (1 + i)² = 2i.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n) => Rational.fromInt(n);
  final a = c.mul(Cx(r(3), r(4)), Cx(r(3), r(-4))), b = c.mul(Cx(r(1), r(1)), Cx(r(1), r(1)));
  print('(3 + 4i)(3 − 4i) = ${a.re} + (${a.im})i');
  print('(1 + i)² = ${b.re} + (${b.im})i');
}
Output
(3 + 4i)(3 − 4i) = 25 + (0)i
(1 + i)² = 0 + (2)i
scalemethod
Cx<T> scale(Cx<T> a, T s)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // scale(a, s): multiply by a real number.
  const c = CxArith<Rational>(ExactArith());
  final z = c.scale(Cx(Rational.fromInt(3), Rational.fromInt(4)), Rational.fromInt(1, 5));
  print('(3 + 4i)/5 = ${z.re} + (${z.im})i, |·|² = ${c.norm2(z)}');
}
Output
(3 + 4i)/5 = 3/5 + (4/5)i, |·|² = 1
norm2method
T norm2(Cx<T> a)
a² = re² + im² (a quadrance; no root).
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // norm2: |a|² = re² + im², a quadrance (exact).
  const c = CxArith<Rational>(ExactArith());
  for (final (a, b) in [(3, 4), (5, 12), (1, 1)]) print('|$a + ${b}i|² = ${c.norm2(Cx(Rational.fromInt(a), Rational.fromInt(b)))}');
}
Output
|3 + 4i|² = 25
|5 + 12i|² = 169
|1 + 1i|² = 2
conjmethod
Cx<T> conj(Cx<T> a)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // conj: a − bi; z·z̄ = |z|².
  const c = CxArith<Rational>(ExactArith());
  final z = Cx(Rational.fromInt(2), Rational.fromInt(-5, 3));
  final zb = c.conj(z), p = c.mul(z, zb);
  print('z̄ = ${zb.re} + (${zb.im})i, z·z̄ = ${p.re} + (${p.im})i = |z|² = ${c.norm2(z)}');
}
Output
z̄ = 2 + (5/3)i, z·z̄ = 61/9 + (0)i = |z|² = 61/9
divmethod
Cx<T> div(Cx<T> a, Cx<T> b)

A / b = a \cdot \bar{b} / |b|^{2}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // div(a, b) = a·b̄/|b|², exact.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n) => Rational.fromInt(n);
  final q = c.div(Cx(r(1), r(0)), Cx(r(1), r(2)));
  print('1/(1 + 2i) = ${q.re} + (${q.im})i');
}
Output
1/(1 + 2i) = 1/5 + (-2/5)i
timesImethod
Cx<T> timesI(Cx<T> a)

i \cdot a.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // timesI: i·a, a quarter turn; four of them return to a.
  const c = CxArith<Rational>(ExactArith());
  var z = Cx(Rational.fromInt(2), Rational.fromInt(1));
  for (var k = 0; k < 4; k++) {
    z = c.timesI(z);
    print('i^${k + 1}·(2 + i) = ${z.re} + (${z.im})i');
  }
}
Output
i^1·(2 + i) = -1 + (2)i
i^2·(2 + i) = -2 + (-1)i
i^3·(2 + i) = 1 + (-2)i
i^4·(2 + i) = 2 + (1)i
Cx<T> cayleyPhase(T tau)

The rational unit complex number \frac{1 + i\tau}{1 - i\tau} = \frac{(1 - \tau^{2}) + 2i\tau}{1 + \tau^{2}}: the Cayley form of e^{i\theta}, \theta = 2 \cdot \operatorname{atan} \tau, exactly on the unit circle.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // cayleyPhase(τ) = (1 + iτ)/(1 − iτ): the rational form of e^{iθ}, θ = 2·atan τ, exactly on the unit circle.
  const c = CxArith<Rational>(ExactArith());
  for (final t in [Rational.fromInt(1, 2), Rational.fromInt(2, 3), Rational.fromInt(3)]) {
    final z = c.cayleyPhase(t);
    print('τ = $t: ${z.re} + (${z.im})i, |z|² = ${c.norm2(z)}, θ = 2·atan τ = ${(2 * math.atan(t.toDouble())).toStringAsFixed(9)}');
  }
}
Output
τ = 1/2: 3/5 + (4/5)i, |z|² = 1, θ = 2·atan τ = 0.927295218
τ = 2/3: 5/13 + (12/13)i, |z|² = 1, θ = 2·atan τ = 1.176005207
τ = 3: -4/5 + (3/5)i, |z|² = 1, θ = 2·atan τ = 2.498091545
final class ChiralPairs<T>

The correlation map of the pairs from a Krawtchouk array of N + 1 waveguides (N even), over T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Photon pairs from a Krawtchouk array: strong pairs sit on the anti-diagonal (m, −m); exact over ℚ.
  final cp = ChiralPairs(const ExactArith(), n: 4, lambda: Rational.one, linewidth: Rational.fromInt(1, 2));
  final m = cp.map;
  print('Schmidt number K = ${cp.schmidtNumber} ≈ ${cp.schmidtNumber.toDouble().toStringAsFixed(6)}');
  print('anti-diagonal: ${[for (var i = 0; i <= 4; i++) m[i][4 - i]].join(', ')}');
}
Output
Schmidt number K = 436178/144425 ≈ 3.020100
anti-diagonal: 89505/1055104, 9945/65944, 133705/263776, 9945/65944, 89505/1055104

Constructors

ChiralPairsconstructor
ChiralPairs(this.f, {required this.n, required T lambda, required this.linewidth})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ChiralPairs(f, n:, lambda:, linewidth:): N must be even so the pump has a zero mode.
  final cp = ChiralPairs(const ExactArith(), n: 2, lambda: Rational.one, linewidth: Rational.one);
  print('modes: ${cp.modes.map((m) => '(${m.join(', ')})').join(' ')}; norms² ${cp.norms2.join(', ')}');
}
Output
modes: (1, -2, 2) (1, 0, -2) (1, 2, 2); norms² 4, 2, 4

Properties

ffield
final KeplerArith<T> f
nfield
final int n
lambdafield
final T lambda
final T linewidth

\gamma of the line shape.

chainfield
final QuadranceChain<T> chain
modesfield
late final List<List<T>> modes

Mode k (energy \lambda(k - \frac{N}{2})) in the rational basis, and its squared norm \sum w \varphi^{2}.

norms2field
late final List<T> norms2
mapfield
late final List<List<T>> map

The correlation map, normalised to sum 1 (rows: signal mode, columns: idler mode).

T get schmidtNumber

The Schmidt number of the chiral-partner pairs (the anti-diagonal j = N - i, renormalised): \frac{(\Sigma p)^{2}}{\Sigma p^{2}}.

Methods

overlap2method
T overlap2(int i, int j)

\kappa(m, m')^{2} for modes i, j (indices 0…N): (\sum_{n} w_{n}^{2} \varphi_{0}^{2} \varphi_{i} \varphi_{j})^{2} / (\Vert0\Vert^{4} \Vert i\Vert^{2} \Vert j\Vert^{2}).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // overlap2(i, j): κ² for modes i and j, exact (norms enter squared).
  final cp = ChiralPairs(const ExactArith(), n: 4, lambda: Rational.one, linewidth: Rational.one);
  print('κ²(0, 4) = ${cp.overlap2(0, 4)}, κ²(1, 3) = ${cp.overlap2(1, 3)}, κ²(2, 2) = ${cp.overlap2(2, 2)}, κ²(0, 1) = ${cp.overlap2(0, 1)}');
}
Output
κ²(0, 4) = 81/4096, κ²(1, 3) = 9/256, κ²(2, 2) = 121/1024, κ²(0, 1) = 0
ratemethod
T rate(int i, int j)

The unnormalised pair rate for (i, j): \kappa^{2} / (1 + (\frac{\Delta}{\gamma})^{2}), \Delta = \lambda(i + j - N).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // rate(i, j) = κ²/(1 + (Δ/γ)²): pairs with Δ ≠ 0 are suppressed by the line shape.
  final cp = ChiralPairs(const ExactArith(), n: 4, lambda: Rational.one, linewidth: Rational.fromInt(1, 2));
  for (final (i, j) in [(1, 3), (0, 4), (0, 2), (2, 4), (1, 1)]) {
    print('(i, j) = ($i, $j): Δ = ${i + j - 4}λ, κ² = ${cp.overlap2(i, j)}, rate = ${cp.rate(i, j)}');
  }
}
Output
(i, j) = (1, 3): Δ = 0λ, κ² = 9/256, rate = 9/256
(i, j) = (0, 4): Δ = 0λ, κ² = 81/4096, rate = 81/4096
(i, j) = (0, 2): Δ = -2λ, κ² = 3/2048, rate = 3/34816
(i, j) = (2, 4): Δ = 2λ, κ² = 3/2048, rate = 3/34816
(i, j) = (1, 1): Δ = -2λ, κ² = 9/256, rate = 9/4352
class

Eis

final class Eis<T>

x + y\omega_{6} with \omega_{6} = e^{i\pi/3} (\omega_{6}^{2} = \omega_{6} - 1), over T: the honeycomb's own number field, with no \sqrt{3}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Eisenstein numbers x + yω₆ (ω₆ = e^{iπ/3}): the honeycomb's own numbers, with rational norms and no √3.
  const e = EisArith<Rational>(ExactArith());
  Rational r(int n) => Rational.fromInt(n);
  final w = Eis(r(0), r(1)); // ω₆
  final w2 = e.mul(w, w), w3 = e.mul(w2, w);
  print('ω² = ${w2.x} + (${w2.y})ω, ω³ = ${w3.x} + (${w3.y})ω (= −1)');
  print('N(2 + 3ω) = ${e.norm(Eis(r(2), r(3)))}');
}
Output
ω² = -1 + (1)ω, ω³ = -1 + (0)ω (= −1)
N(2 + 3ω) = 19

Constructors

Eisconstructor
const Eis(this.x, this.y)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Eis(x, y) = x + yω₆.
  final a = Eis(Rational.fromInt(1), Rational.fromInt(-1, 2));
  print('a = ${a.x} + (${a.y})ω, Re a = ${const EisArith<Rational>(ExactArith()).re(a)}');
}
Output
a = 1 + (-1/2)ω, Re a = 3/4

Properties

xfield
final T x
yfield
final T y
final class EisArith<T>
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Arithmetic in ℚ(ω₆): N is multiplicative, and the conjugate gives a·ā = N(a).
  const e = EisArith<Rational>(ExactArith());
  Rational r(int n) => Rational.fromInt(n);
  final a = Eis(r(2), r(1)), b = Eis(r(-1), r(3));
  final ab = e.mul(a, b);
  print('a·b = ${ab.x} + (${ab.y})ω');
  print('N(a) = ${e.norm(a)}, N(b) = ${e.norm(b)}, N(ab) = ${e.norm(ab)}');
  final aa = e.mul(a, e.conj(a));
  print('a·ā = ${aa.x} + (${aa.y})ω');
}
Output
a·b = -5 + (8)ω
N(a) = 7, N(b) = 7, N(ab) = 49
a·ā = 7 + (0)ω

Constructors

EisArithconstructor
const EisArith(this.f)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // EisArith(f) over the rationals or doubles.
  const q = EisArith<Rational>(ExactArith()), d = EisArith<double>(F64Arith());
  print('exact N(1 + ω) = ${q.norm(Eis(Rational.one, Rational.one))}, float N(1 + ω) = ${d.norm(const Eis(1.0, 1.0))}');
}
Output
exact N(1 + ω) = 3, float N(1 + ω) = 3.0

Properties

ffield
final KeplerArith<T> f

Methods

addmethod
Eis<T> add(Eis<T> a, Eis<T> b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  const e = EisArith<Rational>(ExactArith());
  final s = e.add(Eis(Rational.fromInt(1), Rational.fromInt(2)), Eis(Rational.fromInt(3), Rational.fromInt(-1)));
  print('(1 + 2ω) + (3 − ω) = ${s.x} + (${s.y})ω');
}
Output
(1 + 2ω) + (3 − ω) = 4 + (1)ω
submethod
Eis<T> sub(Eis<T> a, Eis<T> b)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  const e = EisArith<Rational>(ExactArith());
  final s = e.sub(Eis(Rational.fromInt(1), Rational.fromInt(2)), Eis(Rational.fromInt(3), Rational.fromInt(-1)));
  print('(1 + 2ω) − (3 − ω) = ${s.x} + (${s.y})ω');
}
Output
(1 + 2ω) − (3 − ω) = -2 + (3)ω
mulmethod
Eis<T> mul(Eis<T> a, Eis<T> b)

(a + b\omega)(c + d\omega) = ac - bd + (\operatorname{ad} + bc + bd)\omega.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // mul uses ω² = ω − 1.
  const e = EisArith<Rational>(ExactArith());
  final w = Eis(Rational.zero, Rational.one);
  final s = e.mul(w, w);
  print('ω·ω = ${s.x} + (${s.y})ω');
  final p = e.mul(Eis(Rational.fromInt(2), Rational.fromInt(1)), Eis(Rational.fromInt(1), Rational.fromInt(-1)));
  print('(2 + ω)(1 − ω) = ${p.x} + (${p.y})ω');
}
Output
ω·ω = -1 + (1)ω
(2 + ω)(1 − ω) = 3 + (-2)ω
conjmethod
Eis<T> conj(Eis<T> a)

The conjugate: x + y\bar{\omega} = (x + y) - y\omega.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // conj: x + yω̄ = (x + y) − yω.
  const e = EisArith<Rational>(ExactArith());
  final a = Eis(Rational.fromInt(2), Rational.fromInt(3));
  final c = e.conj(a), p = e.mul(a, c);
  print('conj(2 + 3ω) = ${c.x} + (${c.y})ω; a·ā = ${p.x} + (${p.y})ω = N(a) = ${e.norm(a)}');
}
Output
conj(2 + 3ω) = 5 + (-3)ω; a·ā = 19 + (0)ω = N(a) = 19
normmethod
T norm(Eis<T> a)

N(x + y\omega) = x^{2} + xy + y^{2} (= | \cdot |^{2}).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // norm: N(x + yω) = x² + xy + y², the squared length.
  const e = EisArith<Rational>(ExactArith());
  for (final (x, y) in [(1, 0), (0, 1), (1, 1), (2, -1), (3, 5)]) print('N($x + ${y}ω) = ${e.norm(Eis(Rational.fromInt(x), Rational.fromInt(y)))}');
}
Output
N(1 + 0ω) = 1
N(0 + 1ω) = 1
N(1 + 1ω) = 3
N(2 + -1ω) = 3
N(3 + 5ω) = 49
remethod
T re(Eis<T> a)

\operatorname{Re}(x + y\omega) = x + \frac{y}{2}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // re: the real part x + y/2.
  const e = EisArith<Rational>(ExactArith());
  print('Re ω = ${e.re(Eis(Rational.zero, Rational.one))}, Re(2 − 3ω) = ${e.re(Eis(Rational.fromInt(2), Rational.fromInt(-3)))}');
}
Output
Re ω = 1/2, Re(2 − 3ω) = 1/2
scalemethod
Eis<T> scale(Eis<T> a, T s)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // scale(a, s): multiply by a rational; N scales by s².
  const e = EisArith<Rational>(ExactArith());
  final a = Eis(Rational.fromInt(1), Rational.fromInt(1));
  final b = e.scale(a, Rational.fromInt(2, 3));
  print('(2/3)(1 + ω) = ${b.x} + (${b.y})ω, N = ${e.norm(b)} = (4/9)·${e.norm(a)}');
}
Output
(2/3)(1 + ω) = 2/3 + (2/3)ω, N = 4/3 = (4/9)·3
omega3method
Eis<T> omega3(int n)

\omega_{3}^{n} = \omega_{6}^{2n}: 1, -1 + \omega, -\omega.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // omega3(n): the cube roots of unity 1, −1 + ω, −ω, in Eisenstein form, exactly.
  const e = EisArith<Rational>(ExactArith());
  for (var n = 0; n < 4; n++) {
    final z = e.omega3(n);
    print('ω₃^$n = ${z.x} + (${z.y})ω, N = ${e.norm(z)}, Re = ${e.re(z)}');
  }
  final c = e.mul(e.omega3(1), e.mul(e.omega3(1), e.omega3(1)));
  print('ω₃³ = ${c.x} + (${c.y})ω');
}
Output
ω₃^0 = 1 + (0)ω, N = 1, Re = 1
ω₃^1 = -1 + (1)ω, N = 1, Re = -1/2
ω₃^2 = 0 + (-1)ω, N = 1, Re = -1/2
ω₃^3 = 1 + (0)ω, N = 1, Re = 1
ω₃³ = 1 + (0)ω
final class Honeycomb

A honeycomb of l1 × l2 unit cells (cell (n_{1}, n_{2}) at R = n_{1} + n_{2}\omega_{6}, sites A = R and B = R + \frac{1 + \omega_{6}}{3}), open or a torus. Each A site bonds to B in its own cell (bond 0), the cell at -a_{1} (bond 1) and at -a_{2} (bond 2).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Light on a honeycomb torus: Cayley steps (BiCGSTAB, to rounding) and classic RK4, 60 steps each, same dt.
  final h = Honeycomb(4, 4, torus: true);
  final a = Float64List(2 * h.sites), b = Float64List(2 * h.sites);
  a[h.siteA(1, 1)] = 1;
  b[h.siteA(1, 1)] = 1;
  final ca = <List<double>>[], rk = <List<double>>[];
  for (var k = 1; k <= 60; k++) {
    h.cayleyStep(a, 0.3);
    h.rk4Step(b, 0.3);
    ca.add([k.toDouble(), Honeycomb.norm2(a) - 1]);
    rk.add([k.toDouble(), Honeycomb.norm2(b) - 1]);
  }
  print('${h.sites} sites, ${h.bonds.length} bonds');
  print('Cayley (BiCGSTAB) after 60 steps: Σ|ψ|² − 1 = ${(Honeycomb.norm2(a) - 1).toStringAsExponential(2)}');
  print('RK4 after 60 steps:               Σ|ψ|² − 1 = ${(Honeycomb.norm2(b) - 1).toStringAsExponential(2)}');
}
Output
32 sites, 48 bonds
Cayley (BiCGSTAB) after 60 steps: Σ|ψ|² − 1 = -6.66e-16
RK4 after 60 steps:               Σ|ψ|² − 1 = -4.69e-2

Constructors

Honeycombconstructor
Honeycomb(this.l1, this.l2, {this.torus = false})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Honeycomb(l1, l2, torus:): A and B sites per cell; three bonds per A site (fewer at open edges).
  final open = Honeycomb(3, 2), torus = Honeycomb(3, 2, torus: true);
  print('open:  ${open.sites} sites, ${open.bonds.length} bonds');
  print('torus: ${torus.sites} sites, ${torus.bonds.length} bonds');
}
Output
open:  12 sites, 13 bonds
torus: 12 sites, 18 bonds

Static

norm2staticmethod
static double norm2(Float64List psi)

\sum |\psi|^{2}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // Honeycomb.norm2(ψ): Σ|ψ|² over the stacked (re | im) vector.
  final psi = Float64List.fromList([0.6, 0, 0, 0, 0, 0.8, 0, 0]);
  print('Σ|ψ|² = ${Honeycomb.norm2(psi)}');
}
Output
Σ|ψ|² = 1.0

Properties

l1field
final int l1
l2field
final int l2
torusfield
final bool torus
bondsfield
final bonds

(A site, B site, bond kind 0…2, n_{1}, n_{2}).

hopfield
late Float64List hop

Hoppings per bond (-t ⋅ these enter H) and on-site energies.

onsitefield
late Float64List onsite

Hoppings per bond (-t ⋅ these enter H) and on-site energies.

sitesgetter
int get sites

Methods

siteAmethod
int siteA(int n1, int n2)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // siteA(n1, n2): the A site of a cell; its B partner is siteA + 1.
  final h = Honeycomb(3, 2);
  print([for (var n2 = 0; n2 < 2; n2++) for (var n1 = 0; n1 < 3; n1++) 'A($n1,$n2) = ${h.siteA(n1, n2)}'].join(', '));
}
Output
A(0,0) = 0, A(1,0) = 2, A(2,0) = 4, A(0,1) = 6, A(1,1) = 8, A(2,1) = 10
cellOfmethod
(int, int, int) cellOf(int site)

(n_{1}, n_{2}, sublattice \frac{0}{1}) of a site.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // cellOf(site): (n1, n2, sublattice).
  final h = Honeycomb(3, 2);
  print([for (final s in [0, 1, 5, 10, 11]) 'site $s → ${h.cellOf(s)}'].join(', '));
}
Output
site 0 → (0, 0, 0), site 1 → (0, 0, 1), site 5 → (2, 0, 1), site 10 → (2, 1, 0), site 11 → (2, 1, 1)
positionmethod
(double, double) position(int site)

The site's position for drawing (display: \frac{\sqrt{3}}{2} for the triangular lattice).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // position(site): where to draw it (display only: here √3/2 enters).
  final h = Honeycomb(2, 2);
  for (var s = 0; s < h.sites; s++) {
    final (x, y) = h.position(s);
    print('site $s: (${x.toStringAsFixed(4)}, ${y.toStringAsFixed(4)})');
  }
}
Output
site 0: (0.0000, 0.0000)
site 1: (0.5000, 0.2887)
site 2: (1.0000, 0.0000)
site 3: (1.5000, 0.2887)
site 4: (0.5000, 0.8660)
site 5: (1.0000, 1.1547)
site 6: (1.5000, 0.8660)
site 7: (2.0000, 1.1547)
applymethod
void apply(Float64List x, Float64List y, {int offset = 0})

y = H x for a real vector: H = \sum_{\mathrm{bonds}}-hop (|A⟩⟨B| + |B⟩⟨A|) + Σ onsite.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // apply(x, y): y = Hx. On a uniform torus, H·(1, 1, …) = −3·(1, 1, …): every site has three bonds.
  final h = Honeycomb(3, 3, torus: true);
  final x = Float64List(h.sites)..fillRange(0, h.sites, 1), y = Float64List(h.sites);
  h.apply(x, y);
  print('H·1 = ${y.toSet()}');
}
Output
H·1 = {-3.0}
int cayleyStep(Float64List psi, double dt, {Float64List? guess})

One Cayley step of dt on (re \mid im) stacked: (I + i(\frac{dt}{2})H)\psi' = (I - i(\frac{dt}{2})H)\psi. Returns the BiCGSTAB iterations. In real form: (a, b) ↦ (a − hHb, b + hHa) on the left, (a + hHb, b − hHa) on the right.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // cayleyStep(ψ, dt): (I + i(dt/2)H)ψ′ = (I − i(dt/2)H)ψ by BiCGSTAB; returns the iterations.
  final h = Honeycomb(3, 3, torus: true);
  final psi = Float64List(2 * h.sites)..[0] = 1;
  for (var k = 0; k < 5; k++) {
    final it = h.cayleyStep(psi, 0.5);
    print('step ${k + 1}: $it iterations, Σ|ψ|² − 1 = ${(Honeycomb.norm2(psi) - 1).toStringAsExponential(1)}');
  }
}
Output
step 1: 4 iterations, Σ|ψ|² − 1 = -6.7e-16
step 2: 4 iterations, Σ|ψ|² − 1 = 4.4e-16
step 3: 4 iterations, Σ|ψ|² − 1 = 2.2e-16
step 4: 4 iterations, Σ|ψ|² − 1 = -2.2e-16
step 5: 4 iterations, Σ|ψ|² − 1 = 4.4e-16
rk4Stepmethod
void rk4Step(Float64List psi, double dt)

One RK4 step of i\dot{\psi} = H\psi (classic): \dot{\psi} = −iHψ, in real form (\dot{a}, \dot{b}) = (Hb, -Ha).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // rk4Step(ψ, dt): classic Runge–Kutta in time; not unitary, so the norm creeps away step by step.
  final h = Honeycomb(3, 3, torus: true);
  final psi = Float64List(2 * h.sites)..[0] = 1;
  final pts = <List<double>>[];
  for (var k = 1; k <= 50; k++) {
    h.rk4Step(psi, 0.5);
    pts.add([k.toDouble(), Honeycomb.norm2(psi) - 1]);
  }
  print('after 50 RK4 steps (dt = 0.5): Σ|ψ|² − 1 = ${(Honeycomb.norm2(psi) - 1).toStringAsExponential(3)}');
}
Output
after 50 RK4 steps (dt = 0.5): Σ|ψ|² − 1 = -2.667e-1
void inGapFilter(Float64List psi, {int power = 120, double e2 = 16})

Keeps the in-gap part of a state: \psi ← (1 - H^{2}/e2)^power \psi, then normalised. A polynomial in H, so only

  • {-}\ \times (and one root to normalise for display): states with |E| near 0 pass, the bands fade. e2 must exceed the largest E^{2} (the honeycomb's is below (3 + |m| + \tfrac{2}{3}|\Delta|)^{2}).
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // inGapFilter: ψ ← (1 − H²/e2)^power ψ keeps states near E = 0; here an edge state of a Z-shaped valley wall.
  final h = Honeycomb(10, 8);
  valleyHallMass(h, 0.6, x1: 5, y0: 3, y1: 5);
  final psi = Float64List(2 * h.sites);
  for (var s = 0; s < h.sites; s++) psi[s] = ((s * 7919) % 13) / 13 - 0.5;
  h.inGapFilter(psi, power: 200, e2: 16);
  final p = [for (var s = 0; s < h.sites; s++) psi[s] * psi[s] + psi[h.sites + s] * psi[h.sites + s]];
  var near = 0.0;
  for (var s = 0; s < h.sites; s++) {
    final (n1, n2, _) = h.cellOf(s);
    if ((n2 - (n1 < 5 ? 3 : 5)).abs() <= 1 || (n1 - 5).abs() <= 1 && n2 >= 3 && n2 <= 5) near += p[s];
  }
  print('weight within one cell of the wall after filtering: ${near.toStringAsFixed(4)}');
}
Output
weight within one cell of the wall after filtering: 0.9296
densemethod
List<List<double>> dense()

The dense matrix (tests and small spectra).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // dense(): the full matrix, for small spectra: the 2 × 2 torus.
  final h = Honeycomb(2, 2, torus: true);
  final m = h.dense();
  print('eigenvalues: ${symmetricEigenvalues(m).map((e) => e.toStringAsFixed(6)).join(', ')}');
}
Output
eigenvalues: -3.000000, -1.000000, -1.000000, -1.000000, 1.000000, 1.000000, 1.000000, 3.000000
final class PseudospinRing

A ring of n sites carrying a two-component pseudospin; hopping n \to n + 1 carries the SU(2) link U_{n}. Around the ring the links multiply to the holonomy: +I (a plain ring) or -I (a Möbius ring: one lap turns the pseudospin by 2\pi, which is -1 on a spinor). Links rotate about y, so they are real 2 \times 2 matrices \begin{bmatrix}c & -s \\ s & c\end{bmatrix}.

Rational: the first n - 1 links are the Cayley rotor of half-tangent t each (c, s) = \frac{((1 - t^{2}), 2t)}{1 + t^{2}}; the last closes the ring exactly: U_{\mathrm{last}} = \pm(U_{n - 2}\cdots U_{0})^{-1}. Classic: every link \exp(-\frac{i\theta\sigma_{y}}{2}) with \theta = \frac{2\pi}{n} (Möbius) by cos and sin, the product -I to rounding.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // A Möbius ring of pseudospin: Cayley links with the last one chosen to close the ring to −I, versus classic
  // cos/sin links. Both rings are held in doubles, so both holonomies are −I to rounding.
  final r = PseudospinRing.rational(12, mobius: true, t: 0.25), c = PseudospinRing.classic(12, mobius: true);
  print('rational holonomy = ${r.holonomy}');
  print('classic holonomy  = (${c.holonomy.$1.toStringAsFixed(15)}, ${c.holonomy.$2.toStringAsExponential(2)})');
  // A packet runs round both rings, Cayley (rational ring) and RK4 (classic ring), 40 steps each.
  final a = r.packet(3, 3), b = c.classicPacket(3, 3);
  final na = Honeycomb.norm2(a), nb = Honeycomb.norm2(b);
  for (var k = 0; k < 40; k++) {
    r.cayleyStep(a, 0.5);
    c.rk4Step(b, 0.5);
  }
  print('after 40 steps: Cayley Σ|ψ|²/start − 1 = ${(Honeycomb.norm2(a) / na - 1).toStringAsExponential(1)}, RK4 ${(Honeycomb.norm2(b) / nb - 1).toStringAsExponential(1)}');
}
Output
rational holonomy = (-1.0, 5.551115123125783e-17)
classic holonomy  = (-1.000000000000000, 4.72e-16)
after 40 steps: Cayley Σ|ψ|²/start − 1 = -5.6e-16, RK4 -4.8e-2

Constructors

factory PseudospinRing.rational(int n, {required bool mobius, double t = 0})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // PseudospinRing.rational(n, mobius:, t:): n − 1 Cayley links, the last one closes the ring to ±I.
  final m = PseudospinRing.rational(6, mobius: true, t: 0.5), p = PseudospinRing.rational(6, mobius: false, t: 0.5);
  print('first link (c, s) = ${m.links[0]} = (3/5, 4/5)');
  print('Möbius holonomy ${m.holonomy}, plain ring holonomy ${p.holonomy}');
}
Output
first link (c, s) = (0.6, 0.8) = (3/5, 4/5)
Möbius holonomy (-1.0, 0.0), plain ring holonomy (1.0, 0.0)
factory PseudospinRing.classic(int n, {required bool mobius})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // PseudospinRing.classic(n, mobius:): every link exp(−iθσ_y/2), θ = 2π/n, by cos and sin.
  final c = PseudospinRing.classic(7, mobius: true);
  print('link (c, s) = (${c.links[0].$1.toStringAsFixed(9)}, ${c.links[0].$2.toStringAsFixed(9)})');
  print('holonomy = (${c.holonomy.$1.toStringAsFixed(15)}, ${c.holonomy.$2.toStringAsExponential(1)})');
}
Output
link (c, s) = (0.900968868, 0.433883739)
holonomy = (-1.000000000000000, 5.6e-17)

Static

overlapstaticmethod
static double overlap(Float64List a, Float64List b)

Re⟨a|b⟩ / ⟨a|a⟩ for two states.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // PseudospinRing.overlap(a, b) = Re⟨a|b⟩/⟨a|a⟩: how much of the starting packet is left after 16 steps.
  final r = PseudospinRing.rational(16, mobius: true, t: 0.25);
  final a = r.packet(4, 3);
  final b = Float64List.fromList(a);
  print('overlap with itself: ${PseudospinRing.overlap(a, b)}');
  for (var k = 0; k < 16; k++) r.cayleyStep(b, 1);
  print('after 16 Cayley steps of dt = 1: overlap = ${PseudospinRing.overlap(a, b).toStringAsFixed(6)}');
}
Output
overlap with itself: 1.0
after 16 Cayley steps of dt = 1: overlap = 0.016071

Properties

nfield
final int n
holonomygetter
(double, double) get holonomy

The holonomy: the product of all links, (c, s) (-1, 0) for Möbius.

sizegetter
int get size

Methods

applymethod
void apply(Float64List x, int xo, Float64List y, int yo)

y = H x on one real part (2n values: up/down per site): H = −Σ (U_{n} |n + 1⟩⟨n| + h.c.).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // apply(x, xo, y, yo): y = Hx on one real part (up/down per site), H = −Σ(U_n|n+1⟩⟨n| + h.c.).
  final r = PseudospinRing.rational(4, mobius: false, t: 0);
  final x = Float64List(8)..[0] = 1, y = Float64List(8);
  r.apply(x, 0, y, 0);
  print('H e_(site 0, up) = $y');
}
Output
H e_(site 0, up) = [0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0]
int cayleyStep(Float64List psi, double dt)

One Cayley step (rational way), BiCGSTAB to rounding.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // cayleyStep(ψ, dt): the Cayley step solved by BiCGSTAB (to rounding); returns the iterations.
  final r = PseudospinRing.rational(10, mobius: true, t: 0.3);
  final psi = r.packet(2, 3), n0 = Honeycomb.norm2(psi);
  for (var k = 0; k < 4; k++) print('step ${k + 1}: ${r.cayleyStep(psi, 0.5)} iterations, Σ|ψ|²/start − 1 = ${(Honeycomb.norm2(psi) / n0 - 1).toStringAsExponential(1)}');
}
Output
step 1: 4 iterations, Σ|ψ|²/start − 1 = -2.2e-16
step 2: 4 iterations, Σ|ψ|²/start − 1 = 4.4e-16
step 3: 4 iterations, Σ|ψ|²/start − 1 = -2.2e-16
step 4: 4 iterations, Σ|ψ|²/start − 1 = 4.4e-16
rk4Stepmethod
void rk4Step(Float64List psi, double dt)

One RK4 step (classic).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // rk4Step(ψ, dt): the classic RK4 step; its norm moves a little every step.
  final c = PseudospinRing.classic(10, mobius: true);
  final psi = c.classicPacket(2, 3), n0 = Honeycomb.norm2(psi);
  final pts = <List<double>>[];
  for (var k = 1; k <= 40; k++) {
    c.rk4Step(psi, 0.5);
    pts.add([k.toDouble(), Honeycomb.norm2(psi) / n0 - 1]);
  }
  print('after 40 RK4 steps: Σ|ψ|²/start − 1 = ${(Honeycomb.norm2(psi) / n0 - 1).toStringAsExponential(3)}');
}
Output
after 40 RK4 steps: Σ|ψ|²/start − 1 = -5.257e-2
packetmethod
Float64List packet(int center, int w)

A packet at center, half-width w sites, pseudospin up, moving with k = \frac{\pi}{2}: the phase i^{n} (exact: powers of i), envelope (1 - \frac{d^{2}}{w^{2}})^{2}. Where the dispersion -2\cos k is straightest, so it keeps its shape for laps.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // packet(center, w): envelope (1 − d²/w²)², phase iᵈ (exact powers of i), pseudospin up.
  final r = PseudospinRing.rational(8, mobius: true);
  final p = r.packet(3, 3);
  for (var k = 0; k < 8; k++) print('site $k: up = ${p[2 * k]} + ${p[16 + 2 * k]}i');
}
Output
site 0: up = 0.0 + 0.0i
site 1: up = -0.308641975308642 + 0.0i
site 2: up = 0.0 + -0.7901234567901234i
site 3: up = 1.0 + 0.0i
site 4: up = 0.0 + 0.7901234567901234i
site 5: up = -0.308641975308642 + 0.0i
site 6: up = 0.0 + 0.0i
site 7: up = 0.0 + 0.0i
Float64List classicPacket(int center, int w)

The classic packet: the same envelope with the phase e^{i\pi d/2} by cos and sin.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // classicPacket(center, w): the same envelope with e^{iπd/2} by cos and sin; it matches packet to rounding.
  final r = PseudospinRing.classic(8, mobius: true);
  final a = r.classicPacket(3, 3), b = r.packet(3, 3);
  var worst = 0.0;
  for (var i = 0; i < a.length; i++) {
    if ((a[i] - b[i]).abs() > worst) worst = (a[i] - b[i]).abs();
  }
  print('largest difference from the exact-phase packet: ${worst < 1e-15 ? 'below 1e-15' : worst}');
}
Output
largest difference from the exact-phase packet: below 1e-15
class

Mat2

final class Mat2<T>

A 2 \times 2 complex matrix [[m11, m12], [m21, m22]].

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // A 2 × 2 complex matrix: here the rational Cayley rotor about z, and U·U† = I exactly.
  const c = CxArith<Rational>(ExactArith());
  final u = cayleyRotor(c, Rational.zero, Rational.zero, Rational.fromInt(1, 2));
  String s(Cx<Rational> z) => '${z.re} + (${z.im})i';
  print('U = [[${s(u.m11)}, ${s(u.m12)}], [${s(u.m21)}, ${s(u.m22)}]]');
  final a = c.add(c.mul(u.m11, c.conj(u.m11)), c.mul(u.m12, c.conj(u.m12)));
  final b = c.add(c.mul(u.m11, c.conj(u.m21)), c.mul(u.m12, c.conj(u.m22)));
  print('(UU†)₁₁ = ${s(a)}, (UU†)₁₂ = ${s(b)}');
}
Output
U = [[3/5 + (-4/5)i, 0 + (0)i], [0 + (0)i, 3/5 + (4/5)i]]
(UU†)₁₁ = 1 + (0)i, (UU†)₁₂ = 0 + (0)i

Constructors

Mat2constructor
const Mat2(this.m11, this.m12, this.m21, this.m22)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Mat2(m11, m12, m21, m22): the Pauli X gate as a matrix, applied to |0⟩.
  const c = CxArith<Rational>(ExactArith());
  final x = Mat2(c.zero, c.one, c.one, c.zero);
  final (a, b) = applyRotor(c, x, (c.one, c.zero));
  print('X|0⟩ = (${a.re}, ${b.re})');
}
Output
X|0⟩ = (0, 1)

Properties

m11field
final Cx<T> m11
m12field
final Cx<T> m12
m21field
final Cx<T> m21
m22field
final Cx<T> m22
final class CayleySchrodinger<T>

A wave function on n grid points stepped by the Cayley (Crank-Nicolson) map, over T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The Schrödinger equation by the Cayley (Crank–Nicolson) map: exact over the Gaussian rationals, then in doubles.
  // Both run 12 steps on the same 9-point grid; the exact total probability is 1 exactly, the float one drifts.
  const q = CxArith<Rational>(ExactArith()), f = CxArith<double>(F64Arith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final psi = rationalPacket<Rational>(q, n: 9, center: 3, halfWidth: 3, tau: r(1, 3));
  final v = [for (var j = 0; j < 9; j++) j == 6 ? r(1) : r(0)];
  final ex = CayleySchrodinger<Rational>(q, psi: psi, potential: v, dx: r(1), dt: r(1, 2));
  final fl = CayleySchrodinger<double>(f, psi: [for (final z in psi) Cx(z.re.toDouble(), z.im.toDouble())],
      potential: [for (final x in v) x.toDouble()], dx: 1, dt: 0.5);
  final p0 = ex.norm2, f0 = fl.norm2;
  final dens = <List<List<double>>>[];
  for (var k = 0; k <= 12; k++) {
    if (k % 6 == 0) dens.add([for (var j = 0; j < 9; j++) [j.toDouble(), (q.norm2(ex.psi[j]) / p0).toDouble()]]);
    if (k < 12) {
      ex.step();
      fl.step();
    }
  }
  print('exact, after ${ex.steps} steps: total probability = ${ex.norm2 / p0}');
  print('float, after ${fl.steps} steps: total probability − 1 = ${(fl.norm2 / f0 - 1).toStringAsExponential(2)}');
  print('ψ₃ real part: denominator of ${ex.psi[3].re.denominator.bitLength} bits');
}
Output
exact, after 12 steps: total probability = 1
float, after 12 steps: total probability − 1 = -2.22e-16
ψ₃ real part: denominator of 632 bits

Constructors

CayleySchrodinger(this.c, {required List<Cx<T>> psi, required List<T> potential, required T dx, required T dt})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // CayleySchrodinger(c, psi:, potential:, dx:, dt:) over any field.
  const q = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = CayleySchrodinger<Rational>(q, psi: [q.zero, q.one, q.zero], potential: [r(0), r(0), r(0)], dx: r(1), dt: r(1));
  print('n = ${s.n}, Σ|ψ|² = ${s.norm2}, steps = ${s.steps}');
}
Output
n = 3, Σ|ψ|² = 1, steps = 0

Properties

cfield
final CxArith<T> c
nfield
final int n
stepsfield
int steps
psigetter
List<Cx<T>> get psi
norm2getter
T get norm2

\sum |\psi_{j}|^{2} (times dx is the probability; the factor is constant, so the sum itself is conserved).

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): one exact Crank–Nicolson step; a particle on the middle of 3 sites spreads, the total stays 1.
  const q = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final s = CayleySchrodinger<Rational>(q, psi: [q.zero, q.one, q.zero], potential: [r(0), r(0), r(0)], dx: r(1), dt: r(1));
  for (var k = 0; k < 3; k++) {
    s.step();
    print('step ${s.steps}: |ψ|² = ${[for (final z in s.psi) q.norm2(z)].join(', ')}, total ${s.norm2}');
  }
}
Output
step 1: |ψ|² = 16/113, 81/113, 16/113, total 1
step 2: |ψ|² = 5184/12769, 2401/12769, 5184/12769, total 1
step 3: |ψ|² = 712336/1442897, 18225/1442897, 712336/1442897, total 1
final class SplitStepSchrodinger

The classic split-step Fourier method in f64 (a direct O(n^{2}) DFT; fine for the grids a lab draws).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The classic split-step Fourier method: e^{−iVΔt/2}, FFT-style kinetic phase, e^{−iVΔt/2}, all cos and sin.
  // Next to it the exact Cayley map on the same packet; both 12 steps.
  const q = CxArith<Rational>(ExactArith());
  final psi = rationalPacket<Rational>(q, n: 16, center: 5, halfWidth: 4, tau: Rational.fromInt(1, 2));
  final v = [for (var j = 0; j < 16; j++) j >= 10 && j < 12 ? 0.5 : 0.0];
  final ss = SplitStepSchrodinger(psi: [for (final z in psi) (z.re.toDouble(), z.im.toDouble())], potential: v, dx: 1, dt: 0.5);
  final ex = CayleySchrodinger<Rational>(q, psi: psi, potential: [for (final x in v) Rational.fromInt((x * 2).round(), 2)], dx: Rational.one, dt: Rational.fromInt(1, 2));
  final s0 = ss.norm2, e0 = ex.norm2;
  for (var k = 0; k < 12; k++) {
    ss.step();
    ex.step();
  }
  final d = ss.norm2 / s0 - 1;
  print('split-step after ${ss.steps} steps: Σ|ψ|²/start − 1 = ${d == 0 ? '0' : d.abs() < 1e-13 ? 'nonzero, below 1e-13 (rounding)' : d}');
  print('Cayley (exact) after ${ex.steps} steps: Σ|ψ|²/start = ${ex.norm2 / e0}');
}
Output
split-step after 12 steps: Σ|ψ|²/start − 1 = nonzero, below 1e-13 (rounding)
Cayley (exact) after 12 steps: Σ|ψ|²/start = 1

Constructors

SplitStepSchrodinger({required List<(double, double)> psi, required List<double> potential, required this.dx, required this.dt})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // SplitStepSchrodinger(psi:, potential:, dx:, dt:): (re, im) pairs in doubles.
  final s = SplitStepSchrodinger(psi: [(0.0, 0.0), (1.0, 0.0), (0.0, 0.0), (0.0, 0.0)], potential: [0, 0, 0, 0], dx: 1, dt: 0.1);
  print('n = ${s.n}, Σ|ψ|² = ${s.norm2}');
}
Output
n = 4, Σ|ψ|² = 1.0

Properties

nfield
final int n
refield
final List<double> re
imfield
final List<double> im
vfield
final List<double> v
dxfield
final double dx
dtfield
final double dt
stepsfield
int steps
norm2getter
double get norm2

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): one split-step; with V = 0 a delta on 8 periodic sites spreads symmetrically.
  final s = SplitStepSchrodinger(psi: [for (var j = 0; j < 8; j++) (j == 4 ? 1.0 : 0.0, 0.0)], potential: List.filled(8, 0.0), dx: 1, dt: 0.5);
  s.step();
  print('|ψ|² after one step: ${[for (var j = 0; j < 8; j++) (s.re[j] * s.re[j] + s.im[j] * s.im[j]).toStringAsFixed(6)].join(' ')}');
  print('Σ|ψ|² − 1 = ${(s.norm2 - 1).toStringAsExponential(1)}');
}
Output
|ψ|² after one step: 0.007541 0.011131 0.035613 0.183851 0.531269 0.183851 0.035613 0.011131
Σ|ψ|² − 1 = -5.6e-16
final class FormFactor

The spectral form factor averaged over samples, two ways, built up a sample at a time.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The spectral form factor g(t) = ⟨|Tr e^{−iHt}|²⟩/N²: dip, ramp, plateau at 1/N. Rational way (Sturm + Cayley
  // phases) and classic way (Jacobi + cos/sin), averaged over the same 30 sampled matrices. Step n is the time n·h
  // only up to the Cayley angle 2·atan(Eh/2) ≈ Eh, so the two curves differ slightly.
  final ff = FormFactor(n: 10, steps: 120, h: 0.1, seed: 3);
  for (var s = 0; s < 30; s++) ff.addSample();
  print('${ff.samples} samples of N = ${ff.n}, β = ${ff.beta}');
  for (final s in [0, 10, 40, 119]) print('step $s: rational ${ff.rational(s).toStringAsFixed(4)}, classic ${ff.classic(s).toStringAsFixed(4)}');
  print('plateau 1/N = ${1 / ff.n}');
}
Output
30 samples of N = 10, β = 2
step 0: rational 1.0000, classic 1.0000
step 10: rational 0.0232, classic 0.0213
step 40: rational 0.0611, classic 0.0626
step 119: rational 0.0909, classic 0.0966
plateau 1/N = 0.1

Constructors

FormFactorconstructor
FormFactor({this.n = 24, this.beta = 2, this.steps = 400, this.h = 0.05, int seed = 7})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // FormFactor({n, beta, steps, h, seed}): empty averages until samples are added.
  final ff = FormFactor(n: 6, beta: 1, steps: 50, h: 0.2, seed: 1);
  print('n = ${ff.n}, β = ${ff.beta}, steps = ${ff.steps}, h = ${ff.h}, samples = ${ff.samples}, g(0) = ${ff.rational(0)}');
}
Output
n = 6, β = 1, steps = 50, h = 0.2, samples = 0, g(0) = 0.0

Properties

nfield
final int n
betafield
final int beta
stepsfield
final int steps
hfield
final double h

The time per step.

samplesfield
int samples
late List<double> sumRational
late List<double> sumClassic
List<double> lastRational

The eigenvalues of the last sample, two ways (for the spectrum strip).

List<double> lastClassic

The eigenvalues of the last sample, two ways (for the spectrum strip).

Methods

addSamplemethod
void addSample()

Adds one sample to both averages.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // addSample(): one more matrix into both averages; lastRational / lastClassic hold its spectrum two ways.
  final ff = FormFactor(n: 6, steps: 20, seed: 9)..addSample();
  print('Sturm:  ${ff.lastRational.map((e) => e.toStringAsFixed(8)).join(', ')}');
  print('Jacobi: ${ff.lastClassic.map((e) => e.toStringAsFixed(8)).join(', ')}');
  print('g at step 0 = ${ff.rational(0)} (|Tr I|²/N² = 1)');
}
Output
Sturm:  -2.60148588, -1.67535744, -0.81295879, 0.05441545, 0.94377884, 2.78414100
Jacobi: -2.60148588, -1.67535744, -0.81295879, 0.05441545, 0.94377884, 2.78414100
g at step 0 = 1.0 (|Tr I|²/N² = 1)
rationalmethod
double rational(int s)

g(t_{s}) averaged so far.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // rational(s): the average with Cayley phases z_k^s (exactly on the unit circle in exact arithmetic).
  final ff = FormFactor(n: 8, steps: 60, h: 0.25, seed: 4);
  for (var i = 0; i < 10; i++) ff.addSample();
  print([for (final s in [0, 5, 20, 59]) 'g($s) = ${ff.rational(s).toStringAsFixed(5)}'].join(', '));
}
Output
g(0) = 1.00000, g(5) = 0.04014, g(20) = 0.10216, g(59) = 0.12110
classicmethod
double classic(int s)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // classic(s): the same average with e^{−iE t} by cos and sin at t = s·h.
  final ff = FormFactor(n: 8, steps: 60, h: 0.25, seed: 4);
  for (var i = 0; i < 10; i++) ff.addSample();
  print([for (final s in [0, 5, 20, 59]) 'g($s) = ${ff.classic(s).toStringAsFixed(5)}'].join(', '));
}
Output
g(0) = 1.00000, g(5) = 0.05304, g(20) = 0.14182, g(59) = 0.10732
resetmethod
void reset()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // reset(): clear the averages (the random stream carries on).
  final ff = FormFactor(n: 4, steps: 10)..addSample()..addSample();
  print('before: ${ff.samples} samples');
  ff.reset();
  print('after:  ${ff.samples} samples, g(3) = ${ff.rational(3)}');
}
Output
before: 2 samples
after:  0 samples, g(3) = 0.0
final class CayleySpin<T>

A spin-1/2 stepped by the rational Cayley rotor of a field (bx, by, bz), over T.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // Spin-½ precession by the rational Cayley rotor: |α|² + |β|² = 1 exactly; next to exp(−iσ·B Δt/2) in doubles.
  // Both 40 steps.
  const c = CxArith<Rational>(ExactArith());
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final ex = CayleySpin<Rational>(c, alpha: c.one, beta: c.zero, bx: r(1), by: r(0), bz: r(1, 2), dt: r(1, 4));
  final fl = ExpSpin(ar: 1, ai: 0, br: 0, bi: 0, bx: 1, by: 0, bz: 0.5, dt: 0.25);
  final pts = <List<double>>[];
  for (var k = 0; k < 40; k++) {
    ex.step();
    fl.step();
    final (x, y, _) = ex.bloch;
    pts.add([x.toDouble(), y.toDouble()]);
  }
  final (x, y, z) = ex.bloch;
  print('exact after ${ex.steps} steps: |α|² + |β|² = ${ex.norm2}, |r|² = ${x * x + y * y + z * z}');
  print('float after 40 steps: |α|² + |β|² − 1 = ${(fl.norm2 - 1).toStringAsExponential(2)}');
  print('Bloch z: exact ${z.toDouble().toStringAsFixed(9)}, float ${fl.bloch.$3.toStringAsFixed(9)}');
  print('angle per step: Cayley 4·atan(|b|Δt/4) = ${(4 * math.atan(math.sqrt(1.25) / 16)).toStringAsFixed(9)}, exp |b|Δt = ${(math.sqrt(1.25) / 4).toStringAsFixed(9)}');
}
Output
exact after 40 steps: |α|² + |β|² = 1, |r|² = 1
float after 40 steps: |α|² + |β|² − 1 = 3.11e-15
Bloch z: exact 0.332681320, float 0.346972903
angle per step: Cayley 4·atan(|b|Δt/4) = 0.279054896, exp |b|Δt = 0.279508497

Constructors

CayleySpinconstructor
CayleySpin(this.c, {required Cx<T> alpha, required Cx<T> beta, required T bx, required T by, required T bz, required T dt})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // CayleySpin(c, alpha:, beta:, bx:, by:, bz:, dt:): the rotor is built once from the field.
  const c = CxArith<Rational>(ExactArith());
  final s = CayleySpin<Rational>(c, alpha: c.one, beta: c.zero, bx: Rational.zero, by: Rational.zero, bz: Rational.one, dt: Rational.one);
  print('norm ${s.norm2}, Bloch ${s.bloch}');
}
Output
norm 1, Bloch (0, 0, 1)

Properties

cfield
final CxArith<T> c
stepsfield
int steps
norm2getter
T get norm2
\alpha² +\beta² (1 for a normalised state; conserved exactly over the rationals).
blochgetter
(T, T, T) get bloch

The Bloch vector (2 \operatorname{Re}(\bar{\alpha}\beta), 2 \operatorname{Im}(\bar{\alpha}\beta), |\alpha|^{2} - |\beta|^{2}).

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): a field along x turns spin-up toward −y; every Bloch vector is a rational point of the sphere.
  const c = CxArith<Rational>(ExactArith());
  final s = CayleySpin<Rational>(c, alpha: c.one, beta: c.zero, bx: Rational.one, by: Rational.zero, bz: Rational.zero, dt: Rational.one);
  for (var k = 0; k < 4; k++) {
    s.step();
    final (x, y, z) = s.bloch;
    print('step ${s.steps}: r = ($x, $y, $z), |r|² = ${x * x + y * y + z * z}');
  }
}
Output
step 1: r = (0, -240/289, 161/289), |r|² = 1
step 2: r = (0, -77280/83521, -31679/83521), |r|² = 1
step 3: r = (0, -4839120/24137569, -23647519/24137569), |r|² = 1
step 4: r = (0, 4896306240/6975757441, -4968639359/6975757441), |r|² = 1
class

ExpSpin

final class ExpSpin

The classic exponential, U = \cos(\frac{|b|\Delta t}{2}) I - i \sin(\frac{|b|\Delta t}{2})(\hat{b} \cdot \sigma), in f64 (Transcendental, Root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // The classic spin step exp(−i(b·σ)Δt/2) in doubles: the norm drifts by rounding over many steps.
  final s = ExpSpin(ar: 1, ai: 0, br: 0, bi: 0, bx: 0.3, by: 0.4, bz: 1.2, dt: 0.25);
  final pts = <List<double>>[];
  for (var k = 1; k <= 20000; k++) {
    s.step();
    if (k % 1000 == 0) pts.add([k.toDouble(), s.norm2 - 1]);
  }
  print('after 20000 steps: |α|² + |β|² − 1 = ${(s.norm2 - 1).toStringAsExponential(2)}');
}
Output
after 20000 steps: |α|² + |β|² − 1 = -1.69e-12

Constructors

ExpSpinconstructor
ExpSpin({required this.ar, required this.ai, required this.br, required this.bi, required double bx, required double by, required double bz, required double dt})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ExpSpin(ar:, ai:, br:, bi:, bx:, by:, bz:, dt:): one cos, one sin and one root at construction.
  final s = ExpSpin(ar: 1, ai: 0, br: 0, bi: 0, bx: 0, by: 0, bz: 1, dt: 1);
  print('norm ${s.norm2}, Bloch ${s.bloch}');
}
Output
norm 1.0, Bloch (0.0, 0.0, 1.0)

Properties

arfield
double ar
aifield
double ai
brfield
double br
bifield
double bi
norm2getter
double get norm2
blochgetter
(double, double, double) get bloch

Methods

stepmethod
void step()
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // step(): a field along x turns spin-up; compare one float step with the exact Cayley step of the same field.
  final s = ExpSpin(ar: 1, ai: 0, br: 0, bi: 0, bx: 1, by: 0, bz: 0, dt: 1);
  s.step();
  final (x, y, z) = s.bloch;
  print('float exp: r = (${x.toStringAsFixed(9)}, ${y.toStringAsFixed(9)}, ${z.toStringAsFixed(9)})');
  const c = CxArith<Rational>(ExactArith());
  final e = CayleySpin<Rational>(c, alpha: c.one, beta: c.zero, bx: Rational.one, by: Rational.zero, bz: Rational.zero, dt: Rational.one)..step();
  print('exact Cayley: r = ${e.bloch} (turned by 4·atan(1/4) instead of 1)');
}
Output
float exp: r = (0.000000000, -0.841470985, 0.540302306)
exact Cayley: r = (0, -240/289, 161/289) (turned by 4·atan(1/4) instead of 1)
final class RationalTfd<T>

The thermofield double with e^{r} = k, rational.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The thermofield double with e^r = k = 2: tanh r, q = e^{−βω}, cosh 2r, sinh 2r all rational; thermal p_n exact.
  final t = RationalTfd(const ExactArith(), Rational.fromInt(2));
  print('tanh r = ${t.tanhR}, q = ${t.q}, cosh 2r = ${t.cosh2r}, sinh 2r = ${t.sinh2r}');
  print('cosh² 2r − sinh² 2r = ${t.cosh2r * t.cosh2r - t.sinh2r * t.sinh2r}');
  final p = [for (var n = 0; n < 8; n++) t.thermal(n)];
  print('p_0…p_3 = ${p.take(4).join(', ')}');
  final c = ClassicTfd(2 * math.log(5 / 3)); // the β with e^{−β} = 9/25
  print('classic at the same q: sinh 2r = ${c.sinh2r.toStringAsFixed(12)} vs ${t.sinh2r.toDouble()}');
}
Output
tanh r = 3/5, q = 9/25, cosh 2r = 17/8, sinh 2r = 15/8
cosh² 2r − sinh² 2r = 1
p_0…p_3 = 16/25, 144/625, 1296/15625, 11664/390625
classic at the same q: sinh 2r = 1.875000000000 vs 1.875

Constructors

RationalTfdconstructor
const RationalTfd(this.f, this.k)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // RationalTfd(f, k): pick e^r = k rational.
  final t = RationalTfd(const ExactArith(), Rational.fromInt(3, 2));
  print('k = 3/2: tanh r = ${t.tanhR}, q = ${t.q}');
}
Output
k = 3/2: tanh r = 5/13, q = 25/169

Properties

ffield
final KeplerArith<T> f
kfield
final T k
tanhRgetter
T get tanhR

\tanh r = \frac{k^{2} - 1}{k^{2} + 1}.

qgetter
T get q

The Boltzmann factor q = e^{-\beta\omega} = \tanh^{2} r.

cosh2rgetter
T get cosh2r

\cosh 2r = \frac{k^{2} + k^{-2}}{2} and \sinh 2r = \frac{k^{2} - k^{-2}}{2}.

sinh2rgetter
T get sinh2r

Methods

thermalmethod
T thermal(int n)

p_{n} = (1 - q)q^{n}.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // thermal(n) = (1 − q)qⁿ; the probabilities sum toward 1 (geometric series).
  final t = RationalTfd(const ExactArith(), Rational.fromInt(2));
  var s = Rational.zero;
  for (var n = 0; n < 6; n++) {
    s += t.thermal(n);
    print('p_$n = ${t.thermal(n)}, partial sum = $s');
  }
}
Output
p_0 = 16/25, partial sum = 16/25
p_1 = 144/625, partial sum = 544/625
p_2 = 1296/15625, partial sum = 14896/15625
p_3 = 11664/390625, partial sum = 384064/390625
p_4 = 104976/9765625, partial sum = 9706576/9765625
p_5 = 944784/244140625, partial sum = 243609184/244140625
T covariance(T c, T s, {required bool bothForward})

\operatorname{Cov}(x_{L}(t), x_{R}(t)) with both clocks run by the rotor (c, s): (c^{2} - s^{2}) \cdot \sinh 2r / 2; with the left clock run backwards (H_{R} - H_{L}, the boost) it is (c^{2} + s^{2}) \cdot \sinh 2r / 2, unchanged.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Cov(x_L, x_R) under a rotor (c, s): unchanged by the boost H_R − H_L, turning as c² − s² under H_L + H_R.
  final t = RationalTfd(const ExactArith(), Rational.fromInt(2));
  for (final tau in [0, 1, 2, 3]) {
    final tt = Rational.fromInt(tau, 4), d = Rational.one + tt * tt;
    final c = (Rational.one - tt * tt) / d, s = Rational.fromInt(2) * tt / d;
    print('rotor t = ${tt}: both forward ${t.covariance(c, s, bothForward: true)}, boost ${t.covariance(c, s, bothForward: false)}');
  }
}
Output
rotor t = 0: both forward 15/16, boost 15/16
rotor t = 1/4: both forward 2415/4624, boost 15/16
rotor t = 1/2: both forward -21/80, boost 15/16
rotor t = 3/4: both forward -1581/2000, boost 15/16
final class ClassicTfd

The classic TFD at inverse temperature beta (\omega = 1): q = e^{-\beta}, r = \operatorname{atanh} \sqrt{q} (Transcendental, Root).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // The classic TFD at inverse temperature β: q = e^{−β}, r = atanh √q, cosh, sinh, cos.
  final t = ClassicTfd(1.0);
  print('q = ${t.q.toStringAsFixed(12)}, r = ${t.r.toStringAsFixed(12)}');
  print('cosh 2r = ${t.cosh2r.toStringAsFixed(12)}, sinh 2r = ${t.sinh2r.toStringAsFixed(12)}');
}
Output
q = 0.367879441171, r = 0.703414556874
cosh 2r = 2.163953413739, sinh 2r = 1.919034751335

Constructors

ClassicTfdconstructor
ClassicTfd(this.beta)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ClassicTfd(β): exp, sqrt and log at construction.
  for (final b in [0.5, 1.0, 2.0]) print('β = $b: q = ${ClassicTfd(b).q.toStringAsFixed(9)}, r = ${ClassicTfd(b).r.toStringAsFixed(9)}');
}
Output
β = 0.5: q = 0.606530660, r = 1.042315485
β = 1.0: q = 0.367879441, r = 0.703414557
β = 2.0: q = 0.135335283, r = 0.385968416

Static

clockstaticmethod
static (double, double) clock(double t)

The quadrature rotation by angle t: (\cos t, \sin t).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ClassicTfd.clock(t) = (cos t, sin t): the quadrature rotation, the classic way.
  for (final t in [0.0, 0.5, 1.0]) {
    final (c, s) = ClassicTfd.clock(t);
    print('t = $t: (${c.toStringAsFixed(9)}, ${s.toStringAsFixed(9)})');
  }
}
Output
t = 0.0: (1.000000000, 0.000000000)
t = 0.5: (0.877582562, 0.479425539)
t = 1.0: (0.540302306, 0.841470985)

Properties

betafield
final double beta
qfield
final double q
rfield
late final double r
cosh2rgetter
double get cosh2r
sinh2rgetter
double get sinh2r

Methods

thermalmethod
double thermal(int n)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // thermal(n) = (1 − q)qⁿ in doubles.
  final t = ClassicTfd(1.0);
  print([for (var n = 0; n < 5; n++) t.thermal(n).toStringAsFixed(9)].join(', '));
}
Output
0.632120559, 0.232544158, 0.085548215, 0.031471429, 0.011577692
double covariance(double t, {required bool bothForward})

\operatorname{Cov}(x_{L}(t), x_{R}(t)): \cos 2t \cdot \sinh 2r / 2 with both clocks forward, \sinh 2r / 2 under the boost.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  // covariance(t): cos 2t·sinh 2r/2 with both clocks forward; constant under the boost.
  final c = ClassicTfd(1.0);
  for (final t in [0.0, math.pi / 4, math.pi / 2]) {
    print('t = ${t.toStringAsFixed(4)}: forward ${c.covariance(t, bothForward: true).toStringAsFixed(9)}, boost ${c.covariance(t, bothForward: false).toStringAsFixed(9)}');
  }
}
Output
t = 0.0000: forward 0.959517376, boost 0.959517376
t = 0.7854: forward 0.000000000, boost 0.959517376
t = 1.5708: forward -0.959517376, boost 0.959517376
(double, double, double, double) quadratures(double g1, double g2, double g3, double g4)

(x_{L}, p_{L}, x_{R}, p_{R}) from four normals: e^{\pm r} by exp.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // quadratures(g1…g4): the TFD sample with e^r by exp.
  final t = ClassicTfd(1.0);
  final (xl, pl, xr, pr) = t.quadratures(1, 0, 0, 1);
  print('g = (1, 0, 0, 1): x_L = ${xl.toStringAsFixed(9)}, p_L = ${pl.toStringAsFixed(9)}, x_R = ${xr.toStringAsFixed(9)}, p_R = ${pr.toStringAsFixed(9)}');
}
Output
g = (1, 0, 0, 1): x_L = 1.010320267, p_L = 1.010320267, x_R = 1.010320267, p_R = -1.010320267
final class ColemanEnsemble<T>

Coleman's ensemble, rational: two universes (Ising rings of n1 and n2 spins) sharing one random weight x, uniform on [x_{0} - a, x_{0} + a]. The averages are exact.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // Coleman's random couplings: two Ising-ring universes share one random weight x ~ U[x₀ − a, x₀ + a].
  // ⟨Z₁Z₂⟩ − ⟨Z₁⟩⟨Z₂⟩ ≠ 0 (the wormhole); with a = 0 it is 0. All averages exact.
  const f = ExactArith();
  Rational r(int n, [int d = 1]) => Rational.fromInt(n, d);
  final pts = <List<double>>[];
  for (var i = 0; i <= 8; i++) {
    final e = ColemanEnsemble(f, n1: 2, n2: 3, x0: r(2), a: r(i, 8));
    if (i % 4 == 0) print('a = ${r(i, 8)}: ⟨Z1⟩ = ${e.mean(2)}, ⟨Z2⟩ = ${e.mean(3)}, ⟨Z1 Z2⟩ = ${e.meanProduct()}, connected = ${e.connected()}');
    pts.add([i / 8, e.connected().toDouble()]);
  }
}
Output
a = 0: ⟨Z1⟩ = 10, ⟨Z2⟩ = 28, ⟨Z1 Z2⟩ = 280, connected = 0
a = 1/2: ⟨Z1⟩ = 61/6, ⟨Z2⟩ = 29, ⟨Z1 Z2⟩ = 1891/6, connected = 61/3
a = 1: ⟨Z1⟩ = 32/3, ⟨Z2⟩ = 32, ⟨Z1 Z2⟩ = 1280/3, connected = 256/3

Constructors

ColemanEnsembleconstructor
const ColemanEnsemble(this.f, {required this.n1, required this.n2, required this.x0, required this.a})
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // ColemanEnsemble(f, n1:, n2:, x0:, a:).
  final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 2, x0: Rational.fromInt(3), a: Rational.one);
  print('rings of ${e.n1} and ${e.n2} spins, x ~ U[${e.x0 - e.a}, ${e.x0 + e.a}]');
}
Output
rings of 2 and 2 spins, x ~ U[2, 4]

Properties

ffield
final KeplerArith<T> f
n1field
final int n1
n2field
final int n2
x0field
final T x0
afield
final T a

Methods

momentmethod
T moment(int k)

\langle x^{k}\rangle for the uniform measure (x_{0} when a = 0).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // moment(k) = ⟨x^k⟩ for the uniform measure, exact.
  final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 2, x0: Rational.fromInt(3), a: Rational.one);
  print([for (var k = 0; k <= 4; k++) '⟨x^$k⟩ = ${e.moment(k)}'].join(', '));
}
Output
⟨x^0⟩ = 1, ⟨x^1⟩ = 3, ⟨x^2⟩ = 28/3, ⟨x^3⟩ = 30, ⟨x^4⟩ = 496/5
zmethod
T z(int n, T x)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // z(n, x): the partition function of one ring at a fixed weight x.
  final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 3, x0: Rational.fromInt(2), a: Rational.zero);
  for (final x in [Rational.fromInt(1), Rational.fromInt(2), Rational.fromInt(1, 2)]) print('Z_3($x) = ${e.z(3, x)}');
}
Output
Z_3(1) = 8
Z_3(2) = 28
Z_3(1/2) = 13/4
meanmethod
T mean(int n)
Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // mean(n) = ⟨Z_n⟩ from the moments.
  final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 3, x0: Rational.fromInt(2), a: Rational.fromInt(1, 2));
  print('⟨Z_2⟩ = ${e.mean(2)}, ⟨Z_3⟩ = ${e.mean(3)}');
}
Output
⟨Z_2⟩ = 61/6, ⟨Z_3⟩ = 29
T meanProduct()

\langle Z_{1} Z_{2}\rangle exactly: the product's coefficients against the moments.

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // meanProduct() = ⟨Z₁Z₂⟩: the product polynomial against the moments.
  final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 3, x0: Rational.fromInt(2), a: Rational.fromInt(1, 2));
  print('⟨Z1 Z2⟩ = ${e.meanProduct()}, ⟨Z1⟩⟨Z2⟩ = ${e.mean(2) * e.mean(3)}');
}
Output
⟨Z1 Z2⟩ = 1891/6, ⟨Z1⟩⟨Z2⟩ = 1769/6
connectedmethod
T connected()

The wormhole: \langle Z_{1}Z_{2}\rangle - \langle Z_{1}\rangle\langle Z_{2}\rangle (zero when a = 0: one fixed coupling, everything factorises).

Example
import 'package:riptide_kepler/riptide_kepler.ray' show ExactArith, F64Arith;
import 'package:riptide_precision/riptide_precision.ray' show Rational;
import 'package:riptide_rational_quantum/riptide_rational_quantum.ray';
import 'raya:convert';

void main() {
  // connected() = ⟨Z₁Z₂⟩ − ⟨Z₁⟩⟨Z₂⟩: zero for one fixed coupling (a = 0), nonzero once it varies.
  for (final a in [Rational.zero, Rational.fromInt(1, 4), Rational.one]) {
    final e = ColemanEnsemble(const ExactArith(), n1: 2, n2: 3, x0: Rational.fromInt(2), a: a);
    print('a = $a: connected = ${e.connected()}');
  }
}
Output
a = 0: connected = 0
a = 1/4: connected = 241/48
a = 1: connected = 256/3