RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Exact and higher mathematics

Exact special functions

Special functions for rational trigonometry: spread polynomials, spread periods, the Cayley circle and spread series, next to their classic counterparts.

import 'package:riptide_rational_special/riptide_rational_special.ray';
9 classes93 functions and methods98 of 136 documented86 runnable examples

Overview

Special functions for rational trigonometry, two ways.

Functions and constants

Functions

List<Rational> padeExpCoefficients(int m)

The coefficients p_{j} of the [m/m] Padé approximant of e^{x} = \frac{P(x)}{P(-x)}: p_{j} = (2m - j)! m! / ((2m)! j! (m - j)!) (standard; Baker and Graves-Morris, Padé Approximants, 1996).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('[3/3] Padé coefficients of eˣ: ${padeExpCoefficients(3)}');
}
Output
[3/3] Padé coefficients of eˣ: [1, 1/2, 1/10, 1/120]
padeExpfunction
Rational padeExp(Rational x, int m)

e^{x} \approx \frac{P(x)}{P(-x)}, the [m/m] Padé approximant, exactly at a rational x.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final a = padeExp(r(1), 4);
  print('[4/4] Padé at 1: $a ≈ ${a.toDouble()} (e = 2.718281828459045)');
}
Output
[4/4] Padé at 1: 2721/1001 ≈ 2.7182817182817183 (e = 2.718281828459045)
padeExp64function
double padeExp64(double x, int m)

padeExp in doubles.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${padeExp64(1, 4)}, ${padeExp64(1, 8)}');
}
Output
2.7182817182817183, 2.718281828459045
mitchellLog2function
Rational mitchellLog2(Rational x)

Mitchell's \log_{2} (J. N. Mitchell, "Computer multiplication and division using binary logarithms", IRE Trans. Electronic Computers EC-11 (1962) 512–517): x = 2^{e} \cdot m with 1 \le m < 2, \log_{2} x \approx e + (m - 1). Exact at powers of two, piecewise linear between; an approximation (at most 0.0861 low).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // log₂ x ≈ e + (m − 1) for x = 2ᵉ·m: exact at powers of two, the chord in between.
  print('${mitchellLog2(r(8))}, ${mitchellLog2(r(3))} (log₂ 3 ≈ 1.585)');
}
Output
3, 3/2 (log₂ 3 ≈ 1.585)
mitchellExp2function
Rational mitchellExp2(Rational y)

Mitchell's 2^{y}, the inverse of mitchellLog2: 2\wedge\lfloor y\rfloor \cdot (1 + \operatorname{frac} y). An approximation (at most 6.15% high: the chord lies above 2^{f}).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${mitchellExp2(r(3, 2))} (2^1.5 ≈ 2.828)');
}
Output
3 (2^1.5 ≈ 2.828)
double mitchellLog264(double x)

mitchellLog2 in doubles: the exponent field plus the mantissa's fraction (positive normal x).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${mitchellLog264(3)}, ${mitchellLog264(10)}');
}
Output
1.5, 3.25
double mitchellExp264(double y)

mitchellExp2 in doubles.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${mitchellExp264(1.5)}');
}
Output
3.0
double angleOfSpread(double s)

The angle \theta in [0, \frac{\pi}{2}] whose \sin^{2} is the spread s: \theta = \operatorname{asin} \sqrt{s} (the classic way in from a spread).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('spread 1/4 → ${angleOfSpread(0.25)} rad (π/6 = ${3.141592653589793 / 6})');
}
Output from the Raya VM
spread 1/4 → 0.5235987755982988 rad (π/6 = 0.5235987755982988)
double angleOfSpread32(double s)

angleOfSpread in single precision (each operation correctly rounded to f32: the best an f32 unit can do).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${angleOfSpread32(0.25)} in single precision');
}
Output
0.5235987901687622 in single precision
double sinSquaredTurn(int n, double theta)

\sin^{2}(n\theta), the spread of the n-fold turn, the classic way: multiply the angle, take the sine, square it.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('sin²(3 · π/7) = ${sinSquaredTurn(3, 3.141592653589793 / 7)}');
}
Output
sin²(3 · π/7) = 0.9504844339512096
double sinSquaredTurn32(int n, double theta32)

sinSquaredTurn in single precision: n\theta rounded to f32, its sine correctly rounded to f32, squared in f32.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${sinSquaredTurn32(3, 0.4487989505128276)}');
}
Output
0.9504843950271606
double classicSpreadTurn(int n, double s)

The classic pipeline from a spread: \theta = \operatorname{asin} \sqrt{s}, then \sin^{2}(n\theta).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // The classic way: asin √s, multiply the angle, sin². The exact answer is S₅(1/3).
  print('classic: ${classicSpreadTurn(5, 1 / 3)}');
  print('exact:   ${spreadExact(5, r(1, 3)).rational}');
}
Output from the Raya VM
classic: 0.004115226337448598
exact:   1/243
double classicSpreadTurn32(int n, double s)

The same in single precision.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('f32 classic S₅₀(1/3): ${classicSpreadTurn32(50, 1 / 3)}, exact ≈ ${spreadExact(50, r(1, 3)).toDouble()}');
}
Output
f32 classic S₅₀(1/3): 0.3585035502910614, exact ≈ 0.3585030776846559
double cosTurnBySteps(int n, double theta)

\cos(n\theta) by n repeated angle steps (the turn applied n times with \cos \theta and \sin \theta): the textbook loop.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${cosTurnBySteps(1000, 0.001)} vs cos(1) = 0.5403023058681398');
}
Output
0.5403023058681451 vs cos(1) = 0.5403023058681398
double cosTurnChebyshev(int n, double theta)

\cos(n\theta) by Chebyshev's recurrence T_{k+1} = 2\cos \theta \cdot T_{k} - T_{k-1} from one cosine.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${cosTurnChebyshev(1000, 0.001)} vs cos(1) = 0.5403023058681398');
}
Output
0.5403023058754524 vs cos(1) = 0.5403023058681398
(double, double) directionOfAngle(double phi)

The direction of the line at angle \varphi (to draw the classic side's lines).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${directionOfAngle(3.141592653589793 / 3)}');
}
Output
(0.5000000000000001, 0.8660254037844386)
(double, double) directionOfAngle32(double phi32)

The direction in single precision (cos and sin of the f32 angle, rounded to f32).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${directionOfAngle32(1.0471975511965976)}');
}
Output
(0.4999999701976776, 0.866025447845459)
List<double> fourierCoefficients(SeriesTarget target, int n)

The Fourier cosine coefficients a_{0} \cdots a_{N} of a target as a function of u = 2\theta (g(u) = f(s) with \cos u = 1 - 2s): g(u) \approx \sum a_{m} \cos(mu). Closed forms with \pi for the kink and the step (standard), the trapezoid rule on 4096 points for the bump (spectrally accurate for a smooth periodic function).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${fourierCoefficients(SeriesTarget.kink, 4).map((c) => c.toStringAsFixed(6)).toList()}');
}
Output
[0.636620, 0.000000, 0.424413, 0.000000, -0.084883]
fourierAtfunction
double fourierAt(List<double> a, double s)

The truncated Fourier series at the spread s: \sum a_{m} \cos(m \cdot 2\theta) with \theta = \operatorname{asin} \sqrt{s}.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final a = fourierCoefficients(SeriesTarget.bump, 12);
  print('Fourier at s = 1/2: ${fourierAt(a, 0.5)}, exact ${SeriesTarget.bump.exact(r(1, 2))}');
}
Output
Fourier at s = 1/2: 0.9611239271339362, exact 1
classicExpfunction
double classicExp(double x)

e^{x}, the classic way (the library's exp).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${classicExp(1)}');
}
Output
2.718281828459045
classicLog2function
double classicLog2(double x)

\log_{2} x, the classic way.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${classicLog2(8)}, ${classicLog2(3)}');
}
Output from the Raya VM
3.0, 1.5849625007211563
double degreesOfSpread(double s)

The angle in degrees whose \sin^{2} is the spread s (to read a spread as an angle, at the edge).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('spread 1/2 → ${degreesOfSpread(0.5)}°, 3/4 → ${degreesOfSpread(0.75)}°');
}
Output
spread 1/2 → 45.00000000000001°, 3/4 → 59.99999999999999°
(double, double) directionOfDegrees(double degrees)

The direction of the line at degrees.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${directionOfDegrees(45)}');
}
Output
(0.7071067811865476, 0.7071067811865475)
double ratioToDouble(BigInt p, BigInt q)

\frac{p}{q} as a double, within 2^{-58} relative, for numbers of any size: both are cut to their top 62 bits first, so a million-bit fraction costs two shifts, not a million-bit division.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // A fraction with thousand-digit parts, to double without building a BigFloat.
  final p = BigInt.from(10).pow(1000) + BigInt.one, q = BigInt.from(3) * BigInt.from(10).pow(1000);
  print('${ratioToDouble(p, q)}');
}
Output
0.33333333333333337
pow2function
double pow2(int k)

2^{k} as a double, exactly (0 below the smallest subnormal, \infty above the largest double).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${pow2(10)}, ${pow2(-1074)}, ${pow2(1024)}');
}
Output
1024.0, 5e-324, Infinity
int bitsOfFraction(BigInt p, BigInt q)

The cost of an exact fraction: bits of numerator and denominator together.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${bitsOfFraction(BigInt.from(355), BigInt.from(113))} bits for 355/113');
}
Output
16 bits for 355/113
bitsOffunction
int bitsOf(Rational r)

The cost of a rational: bits of numerator and denominator together.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('S₁₀(1/3) costs ${bitsOf(spreadExact(10, r(1, 3)).rational)} bits, S₁₀₀(1/3) ${bitsOf(spreadExact(100, r(1, 3)).rational)}');
}
Output
S₁₀(1/3) costs 26 bits, S₁₀₀(1/3) 318
int decimalDigits(BigInt n)

The decimal digits of a big integer (its length when written out), without writing it out (\pm1 when huge).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${decimalDigits(BigInt.from(3).pow(1000))} digits in 3^1000');
}
Output
478 digits in 3^1000
f32function
double f32(double x)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('0.1 in single precision: ${f32(0.1)}');
}
Output
0.1 in single precision: 0.10000000149011612
List<BigInt> legendreInSpread(int n)

The integer coefficients of P_{n}(1 - 2s) in powers of s.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('P₃(1 − 2s) = ${legendreInSpread(3)} (coefficients of 1, s, s², s³)');
}
Output
P₃(1 − 2s) = [1, -12, 30, -20] (coefficients of 1, s, s², s³)
Rational legendreOfSpread(int n, Rational s)

P_{n}(1 - 2s) at a rational spread, exactly.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('P₄(1 − 2·(1/4)) = ${legendreOfSpread(4, r(1, 4))}');
}
Output
P₄(1 − 2·(1/4)) = -37/128
chebyshevUfunction
Rational chebyshevU(int k, Rational x)

U_{k}(x), Chebyshev's second kind, exactly: U_{0} = 1, U_{1} = 2x, U_{k+1} = 2xU_{k} - U_{k-1} (\sin 2(k + 1)\theta = \sin 2\theta \cdot U_{k}(\cos 2\theta)).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('U₃(1/2) = ${chebyshevU(3, r(1, 2))}');
}
Output
U₃(1/2) = -1
Rational quarterTurnStep(int n)

The rational spread \sigma of one step of an n-step quarter turn: near the smallest root of U_{n-1}(1 - 2\sigma) (where the line after n steps is first perpendicular, \sin 2n\theta = 0), bracketed by bisection on its sign, then the simplest fraction within 0.1% of it (Stern–Brocot), so the nodes stay small. Only {+}\ {-}\ \times\ \div and comparisons. The root is \sin^{2}(\frac{\pi}{2n}), irrational; with \sigma the n steps turn the line by 90^{\circ} to within about 0.05%, and every step is the same spread exactly.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('one step of a 3-step quarter turn: σ = ${quarterTurnStep(3)}');
}
Output
one step of a 3-step quarter turn: σ = 1/4
Rational simplestBetween(Rational x, Rational y)

The fraction with the smallest denominator in [x, y] (0 \le x \le y), by continued fractions (the Stern–Brocot descent).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${simplestBetween(r(3, 10), r(1, 3))}, ${simplestBetween(r(314, 100), r(315, 100))}');
}
Output
1/3, 22/7
quadrancefunction
BigInt quadrance(BigInt x, BigInt y)

The quadrance of the vector (x, y): x^{2} + y^{2} (the square of the distance, with no root taken).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${quadrance(BigInt.from(3), BigInt.from(4))}');
}
Output
25
crossOffunction
BigInt crossOf(BigInt ax, BigInt ay, BigInt bx, BigInt by)

The cross of two vectors, x_{1}y_{2} - x_{2}y_{1} (twice the signed area of their triangle).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${crossOf(BigInt.from(3), BigInt.one, BigInt.one, BigInt.two)}');
}
Output
5
spreadOffunction
Rational spreadOf(BigInt ax, BigInt ay, BigInt bx, BigInt by)

The spread between the lines along (ax, ay) and (bx, by): \frac{(x_{1}y_{2} - x_{2}y_{1})^{2}}{Q_{1}Q_{2}}. Classically \sin^{2} of the angle between them, but computed with no angle: 0 for parallel lines, 1 for perpendicular ones.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('spread between (1, 0) and (3, 4): ${spreadOf(BigInt.one, BigInt.zero, BigInt.from(3), BigInt.from(4))}');
}
Output
spread between (1, 0) and (3, 4): 16/25
(BigInt, BigInt, BigInt) chebyshevLadder(int n, BigInt c, BigInt b)

The homogeneous Chebyshev ladder: (b^{n} \cdot T_{n}(\frac{c}{b}), b^{n+1} \cdot T_{n+1}(\frac{c}{b}), b^{n}) in O(\log n) big multiplications, by T_{2k} = 2T_{k}^{2} - 1 and T_{2k+1} = 2T_{k}T_{k+1} - T_{1}, each multiplied through by the matching power of b so that everything stays an integer.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${chebyshevLadder(5, BigInt.one, BigInt.two)} (bⁿTₙ(c/b), bⁿ⁺¹Tₙ₊₁(c/b), bⁿ for c/b = 1/2)');
}
Output
(16, 64, 32) (bⁿTₙ(c/b), bⁿ⁺¹Tₙ₊₁(c/b), bⁿ for c/b = 1/2)
chebyshevTfunction
Rational chebyshevT(int n, Rational x)

The Chebyshev polynomial T_{n}(x) at a rational x, exactly (T_{n}(\cos \theta) = \cos n\theta classically).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('T₅(1/2) = ${chebyshevT(5, r(1, 2))}, T₁₀₀(1/3) has ${bitsOf(chebyshevT(100, r(1, 3)))} bits');
}
Output
T₅(1/2) = 1/2, T₁₀₀(1/3) has 318 bits
spreadExactfunction
SpreadValue spreadExact(int n, Rational s)

S_{n}(s), the spread of the n-fold turn whose single turn has spread s, exactly, in O(\log n) big multiplications: S_{n}(s) = \frac{1 - T_{n}(1 - 2s)}{2} (Goh–Wildberger §1; Wildberger, Divine Proportions). For s = \frac{a}{b} the value is \frac{P_{n}}{b^{n}} with P_{n} = \frac{b^{n} - b^{n}T_{n}}{2} an integer, because S_{n} has integer coefficients and degree n.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // The spread of 1000 turns of a spread-1/3 turn: exact, in O(log n) multiplications.
  final v = spreadExact(1000, r(1, 3));
  print('S₁₀₀₀(1/3) ≈ ${v.toDouble()}, exact fraction of ${v.bits} bits');
  print('the classic pipeline in doubles: ${classicSpreadTurn(1000, 1 / 3)}');
}
Output from the Raya VM
S₁₀₀₀(1/3) ≈ 0.07241117872536346, exact fraction of 3167 bits
the classic pipeline in doubles: 0.07241117872540873
SpreadValue spreadRecurrence(int n, Rational s)

S_{n}(s) exactly by the recurrence S_{k+1} = 2(1 - 2s)S_{k} - S_{k-1} + 2s (S_{0} = 0, S_{1} = s; Goh–Wildberger eq. 1), O(n) steps, as integers over b^{k}: P_{k+1} = 2(b - 2a)P_{k} - b^{2}P_{k-1} + 2a \cdot b^{k}.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${spreadRecurrence(5, r(1, 4)).rational} = ${spreadExact(5, r(1, 4)).rational}');
}
Output
1/4 = 1/4
List<BigInt> spreadPolynomial(int n)

The coefficients of S_{n}(s) = c_{0} + c_{1}s + \cdots + c_{n}s^{n}, all integers (S_{2} = 4s - 4s^{2}, S_{3} = 9s - 24s^{2} + 16s^{3}).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  for (var n = 1; n <= 4; n++) print('S$n: ${spreadPolynomial(n)}');
}
Output
S1: [0, 1]
S2: [0, 4, -4]
S3: [0, 9, -24, 16]
S4: [0, 16, -80, 128, -64]
Rational evaluatePolynomial(List<BigInt> coefficients, Rational x)

Evaluates integer coefficients at a rational, exactly (Horner).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${evaluatePolynomial([BigInt.one, BigInt.zero, BigInt.from(-2)], r(1, 3))}');
}
Output
7/9
turnLineAtfunction
({BigInt x, BigInt y, bool rootOnX}) turnLineAt(int n, Rational s)

The line l_{n} of the n-fold turn of spread s = \frac{a}{b} at any n, exactly, in O(\log n) big multiplications (no stepping through l_{1} \cdots l_{n-1}): from the ladder's b^{n}T_{n} and b^{n+1}T_{n+1}, \tan n\theta = \frac{c \cdot b^{n}T_{n} - b^{n+1}T_{n+1}}{2r(b^{n} + b^{n}T_{n})} with c = b - 2a and r = \sqrt{a(b - a)}, because (1 - x^{2})U_{n-1} = xTₙ - T_{n+1}. Returned as integers (x, y) and the coordinate r multiplies (rootOnX); when \cos n\theta < 0 the equivalent form through 1 - \cos 2n\theta is used so nothing cancels. s = 0 and s = 1 (r = 0) are the x-axis and, for odd n at s = 1, the y-axis.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${turnLineAt(3, r(1, 4))}');
}
Output
(rootOnX: false, x: 0, y: 256)
turnAtfunction
({SpreadValue spread, ({BigInt x, BigInt y, bool rootOnX}) line}) turnAt(int n, Rational s)

S_{n}(s) and the line l_{n} from one ladder (see spreadExact and turnLineAt).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final t = turnAt(4, r(1, 2));
  print('spread ${t.spread.rational}, line ${t.line}');
}
Output
spread 0, line (rootOnX: true, x: 64, y: 0)
(double, double) lineDirection(({BigInt x, BigInt y, bool rootOnX}) p, double root)

A line's integer parts as a double direction, with r passed in (the one root, at the edge).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${lineDirection(turnLineAt(2, r(1, 4)), 1.7320508075688772)}');
}
Output
(48.0, 83.13843876330611)
(double, double) turnLineDirection(int n, Rational s, double root)

turnLineAt as a double direction for drawing, with r = \sqrt{a(b - a)} passed in by the caller (the one root, at the screen's edge).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';
import 'raya:math' as math;

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // s = 1/4: r = √(1·3); the first six turned lines.
  final dirs = [for (var n = 0; n < 6; n++) turnLineDirection(n, r(1, 4), 1.7320508075688772)];
  print(dirs.map((d) => '(${d.$1.toStringAsFixed(3)}, ${d.$2.toStringAsFixed(3)})').join(' '));
}
Output
(6.928, 0.000) (20.785, 12.000) (48.000, 83.138) (0.000, 443.405) (-768.000, 1330.215) (5320.860, -3072.000)
spreadModfunction
int spreadMod(int n, Rational s, int p)

S_{n}(s) \bmod p for a prime p not dividing s's denominator, in O(\log n) integer steps (p < 2^{31}): the finite-field shadow of the exact value, which never grows.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('S_(10^18)(1/3) mod 1000003 = ${spreadMod(1000000000000000000, r(1, 3), 1000003)}');
}
Output
S_(10^18)(1/3) mod 1000003 = 135499
spreadPeriodfunction
int? spreadPeriod(Rational s, int p)

The spread period of s at the prime p (Goh–Wildberger, Spread periodicity theorem): the least m \ge 1 with p \mid S_{m}(s). Then p \mid S_{n}(s) exactly when m \mid n, and m divides p - 1 or p + 1. Found by stepping the recurrence mod p (at most p + 1 steps); null if p divides the denominator.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('period of 1/3 at p = 13: ${spreadPeriod(r(1, 3), 13)}');
}
Output
period of 1/3 at p = 13: 14
double spreadLadder64(int n, double s)

S_{n}(s) in f64 by the O(\log n) ladder in spread form: S_{2k} = 4S_{k}(1 - S_{k}) (S_{2} is the logistic map) and S_{2k+1} = 2S_{k} + 2S_{k+1} - 4S_{k}S_{k+1} - s (from S_{m+n} + S_{m-n} = 2S_{m} + 2S_{n} - 4S_{m}S_{n}, our derivation from the classical \sin^{2}(\alpha + \beta) + \sin^{2}(\alpha - \beta) = 2\sin^{2}\alpha + 2\sin^{2}\beta - 4\sin^{2}\alpha \sin^{2}\beta).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${spreadLadder64(50, 1 / 3)} vs exact ${spreadExact(50, r(1, 3)).toDouble()}');
}
Output
0.3585030776846616 vs exact 0.3585030776846559
double spreadRecurrence64(int n, double s)

S_{n}(s) in f64 by the recurrence S_{k+1} = 2(1 - 2s)S_{k} - S_{k-1} + 2s: n - 1 multiply-adds.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${spreadRecurrence64(50, 1 / 3)} vs exact ${spreadExact(50, r(1, 3)).toDouble()}');
}
Output
0.3585030776846573 vs exact 0.3585030776846559
double spreadLadder32(int n, double s0)

spreadLadder64 with every operation rounded to single precision.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${spreadLadder32(50, 1 / 3)}');
}
Output
0.35850319266319275
double spreadRecurrence32(int n, double s0)

spreadRecurrence64 with every operation rounded to single precision.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${spreadRecurrence32(50, 1 / 3)}');
}
Output
0.35850250720977783

Classes

class

Cayley

abstract final class Cayley
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // Rational turns by the half-angle tangent t: points (1 − t², 2t)/(1 + t²).
  print('${Cayley.point(r(1, 2))}, spread ${Cayley.spread(r(1, 2))}');
}
Output
(3/5, 4/5), spread 16/25

Static

pointstaticmethod
static (Rational, Rational) point(Rational t)

The point of the unit circle at parameter t.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${Cayley.point(r(1, 2))}, ${Cayley.point(r(2, 3))}');
}
Output
(3/5, 4/5), (5/13, 12/13)
composestaticmethod
static Rational? compose(Rational t1, Rational t2)

The parameter of "turn by t₁, then by t₂"; null for a half turn (t = \infty, the point (-1, 0)).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${Cayley.compose(r(1, 2), r(1, 3))} (tangent addition: 1); half turn: ${Cayley.compose(r(1), r(1))}');
}
Output
1 (tangent addition: 1); half turn: null
gaussianPowerstaticmethod
static (BigInt, BigInt) gaussianPower(BigInt q, BigInt p, int n)

(q + ip)^{n} = x + iy, by squaring: O(\log n) Gaussian multiplications.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('(2 + i)^5 = ${Cayley.gaussianPower(BigInt.two, BigInt.one, 5)}');
}
Output
(2 + i)^5 = (-38, 41)
nFoldstaticmethod
static Rational? nFold(Rational t, int n)

The parameter of the n-fold turn of t, exactly; null when it is the half turn.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${Cayley.nFold(r(1, 2), 3)}, ${Cayley.nFold(r(1), 2)}');
}
Output
11/2, null
spreadstaticmethod
static Rational spread(Rational t)

The spread a turn by t makes (between any line and its turned image): \frac{4t^{2}}{(1 + t^{2})^{2}}.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print('${Cayley.spread(r(1, 2))}');
}
Output
16/25
enum RaceMethod

The contenders.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(RaceMethod.values);
}
Output
[RaceMethod.classic, RaceMethod.classicAngleGiven, RaceMethod.ladder, RaceMethod.recurrence, RaceMethod.exact]

Values

classicvalue
classic

\theta = \operatorname{asin} \sqrt{s}, then \sin^{2}(n\theta): the classic way in from a spread (f64).

classicAngleGiven

\sin^{2}(n\theta) with \theta already known (the classic cost when the angle comes for free) (f64).

laddervalue
ladder

S_{n}(s) by the O(\log n) ladder (f64): {+}\ {-}\ \times only.

recurrence

S_{n}(s) by the recurrence, n - 1 multiply-adds (f64).

exactvalue
exact

S_{n}(s) exactly over Q by the integer ladder (fewer inputs: it is slow, and says so).

final class RaceLane

One lane's tally.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final l = RaceLane(RaceMethod.exact, 0);
  print('${l.method}, max error ${l.maxError}');
}
Output
RaceMethod.exact, max error null

Constructors

RaceLaneconstructor
RaceLane(this.method, this.total)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(RaceLane(RaceMethod.ladder, 4).method);
}
Output
RaceMethod.ladder

Properties

methodfield
final RaceMethod method
totalfield
final int total

Inputs to run, inputs done, the time they took (µs, the loop only) and a checksum the loop must produce.

donefield
int done
microsfield
int micros
double checksum
double? maxError

The worst |value − exact| over the sampled inputs (null until sampled; 0 for the exact lane).

finishedgetter
bool get finished
nsPergetter
double? get nsPer

Nanoseconds per input so far.

final class SpreadRace

A race at one n over points inputs s_{i} = \frac{2i + 1}{2 \cdot \text{points}}, i < \text{points}: dyadic, so the double each float lane reads is the input itself and the exact value of that very input is the reference.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final race = SpreadRace(n: 10, points: 64, exactPoints: 8, samples: 4);
  var t = 0;
  while (!race.step(1000, clock: () => t += 100)) {}
  print('done: lanes ${race.lanes.keys.map((m) => m.name).toList()}');
}
Output
done: lanes [classic, classicAngleGiven, ladder, recurrence, exact]

Constructors

SpreadRaceconstructor
SpreadRace({required this.n, this.points = 1 << 20, this.exactPoints = 256, this.samples = 64})
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final race = SpreadRace(n: 5, points: 16);
  print('${race.n}, ${race.points} points');
}
Output
5, 16 points

Static

orderconstant
static const order

The order lanes run in (interleaved by chunks so drift in the machine's clock hits all alike).

Properties

nfield
final int n
pointsfield
final int points
final int exactPoints
samplesfield
final int samples
lanesfield
final Map<RaceMethod, RaceLane> lanes
finishedgetter
bool get finished
progressgetter
double get progress

Share done, 0 … 1.

Methods

inputmethod
double input(int i)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadRace(n: 5, points: 8).input(0));
}
Output
0.0625
Rational exactInput(int i)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadRace(n: 5, points: 8).exactInput(3));
}
Output
7/16
int sampleIndex(int k)

The sampled inputs' indices.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadRace(n: 5, points: 64, samples: 4).sampleIndex(1));
}
Output
24
stepmethod
bool step(int budgetMicros, {int Function()? clock})

Runs pieces of the race for about budgetMicros; true when everything is done.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final race = SpreadRace(n: 10, points: 32, exactPoints: 4, samples: 4);
  var t = 0, steps = 0;
  while (!race.step(500, clock: () => t += 100)) steps++;
  print('finished after ${steps + 1} steps');
}
Output
finished after 6 steps
enum SeriesTarget

Shapes to approximate, each a function of the spread s on [0, 1] that is rational at rational s (so it can be compared exactly): classically functions of \cos 2\theta = 1 - 2s.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  for (final t in SeriesTarget.values) print('$t at s = 1/4: ${t.exact(r(1, 4))}');
}
Output
SeriesTarget.kink at s = 1/4: 1/2
SeriesTarget.step at s = 1/4: 1
SeriesTarget.bump at s = 1/4: 1/5

Values

kinkvalue
kink
1 - 2s=\cos 2\theta: a kink at s = \frac{1}{2}.
stepvalue
step

\operatorname{sign}(1 - 2s) = \operatorname{sign}(\cos 2\theta): a jump at s = \frac{1}{2} (0 there).

bumpvalue
bump

\frac{1}{1 + 16(1 - 2s)^{2}}: smooth, a peak at s = \frac{1}{2} (Runge's bump, in the spread).

Methods

exactmethod
Rational exact(Rational s)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SeriesTarget.bump.exact(r(1, 3)));
}
Output
9/25
atmethod
double at(double s)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SeriesTarget.bump.at(1 / 3));
}
Output
0.35999999999999993
enum SeriesNodes

Where the exact series samples the target.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  for (final k in SeriesNodes.values) print('$k: ${k.of(4)}');
}
Output
SeriesNodes.turns: [0, 6/41, 840/1681, 58806/68921, 2825760/2825761]
SeriesNodes.cayley: [0, 64/289, 16/25, 576/625, 1]
SeriesNodes.equalSpread: [0, 1/4, 1/2, 3/4, 1]

Values

turnsvalue
turns

The spreads of a line turning evenly: s_{j} = S_{j}(\sigma) for j = 0 \cdots N, where \sigma is the rational spread of one step that makes N steps a quarter turn (to within 2^{-24}), found by bisection on the sign of U_{n-1}(1 - 2\sigma): exact, evenly spaced in angle with no angle computed. These are Chebyshev–Lobatto points in disguise.

cayleyvalue
cayley

The rational points of the circle at Cayley t = \frac{j}{N}: spreads \frac{4t^{2}}{(1 + t^{2})^{2}}, close to evenly spread in angle.

equalSpread

Evenly spaced spreads \frac{j}{N} (crowded near neither end: Runge's phenomenon, honestly shown).

Methods

ofmethod
List<Rational> of(int n)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SeriesNodes.equalSpread.of(5));
}
Output
[0, 1/5, 2/5, 3/5, 4/5, 1]
final class SpreadInterpolant

The polynomial of degree \le N through (s_{j}, f(s_{j})) at N + 1 rational nodes, exactly (Newton's divided differences), and its coefficients in the spread basis \{1, S_{1}, \ldots, S_{N}\}.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final p = SpreadInterpolant.of(SeriesTarget.bump, SeriesNodes.cayley, 6);
  print('at 1/2: ${p.evaluate(r(1, 2))}, exact ${SeriesTarget.bump.exact(r(1, 2))}, ${p.bits} bits');
}
Output
at 1/2: 867318745756519854178433/1534485230550566910558337, exact 1, 969 bits

Constructors

SpreadInterpolant(this.nodes, List<Rational> values)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final p = SpreadInterpolant([r(0), r(1, 2), r(1)], [r(0), r(1), r(0)]);
  print('${p.monomial}, spread basis ${p.spreadCoefficients}');
}
Output
[0, 4, -4], spread basis [0, 0, 1]
factory SpreadInterpolant.of(SeriesTarget target, SeriesNodes kind, int degree)

The series for target with degree + 1 nodes of kind.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadInterpolant.of(SeriesTarget.kink, SeriesNodes.turns, 4).spreadCoefficients);
}
Output
[1, 659017298099/2680676353732608, -549673717954369919/550000838093414400, -660264489499/2680676353732608, 382800820866236324119/1850202819346246041600]

Properties

nodesfield
final List<Rational> nodes
newtonfield
late final List<Rational> newton
degreegetter
int get degree
late final List<Rational> monomial

Its coefficients in powers of s, exactly.

late final List<Rational> spreadCoefficients

c_{0}, c_{1}, …, c_{N} with the interpolant = c_{0} + \sum c_{k}S_{k}(s), exactly: back-substitution from the top degree (S_{k} has degree k and leading coefficient (-1)^{k+1}4^{k-1}, and S_{k}(0) = 0 for k \ge 1).

bitsgetter
int get bits

The bits of all spread-basis coefficients: the cost of writing the series down exactly.

Methods

evaluatemethod
Rational evaluate(Rational s)

The interpolant at s, exactly.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadInterpolant.of(SeriesTarget.kink, SeriesNodes.turns, 4).evaluate(r(1, 3)));
}
Output
452481019002055324529/2341662943235092646400
atmethod
double at(double s)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(SpreadInterpolant.of(SeriesTarget.kink, SeriesNodes.turns, 4).at(1 / 3));
}
Output
0.19323063565114812
final class SpreadValue

An exact value of a spread polynomial: S_{n}(\frac{a}{b}) = numerator/denominator with denominator = b^{n}, kept unreduced: its size is the honest cost of exactness (n \cdot \log_{2} b bits).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final v = spreadExact(7, r(1, 5));
  print('n ${v.n}, ${v.rational}, ${v.bits} bits');
}
Output
n 7, 841/78125, 27 bits

Constructors

SpreadValueconstructor
const SpreadValue(this.n, this.numerator, this.denominator)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final v = SpreadValue(2, BigInt.from(3), BigInt.from(4));
  print('${v.rational} ≈ ${v.toDouble()}');
}
Output
3/4 ≈ 0.75

Properties

nfield
final int n
final BigInt numerator
final BigInt denominator
rationalgetter
Rational get rational

The value as a reduced rational (a gcd of the two: costly when they are huge).

bitsgetter
int get bits

Bits of numerator and denominator.

Methods

toDoublemethod
double toDouble()

The value as a double (within 2^{-58} relative; an edge, for drawing and for measuring floats against it).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(spreadExact(10000, r(1, 7)).toDouble());
}
Output
0.4730828331857074
toStringmethod
String toString()
final class TurnLines

The lines of an n-fold turn, exactly, with no angle: l_{0} is the x-axis, l_{1} makes spread s = \frac{a}{b} with it, and l_{k+1} is l_{k-1} reflected in l_{k} (Goh–Wildberger §1.3), so the spread between l_{0} and l_{k} is S_{k}(s).

A spread fixes a line only up to its mirror image in l_{0}; the sign comes from Chebyshev's second kind, \sin 2k\theta = \sin 2\theta \cdot U_{k-1}(\cos 2\theta), which is again polynomial. So l_{k} runs along (b^{k} + b^{k}T_{k}, 2 \cdot b^{k-1}U_{k-1} \cdot r) with r = \sqrt{a(b - a)}, one fixed number for the whole family: everything but r is an integer, and r is a whole number when s(1 - s) is a square (s = \frac{16}{25}: r = 12). direction takes the root only at the edge, to draw.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  // Turn a line again and again by a spread-1/5 turn; the spreads stay exact.
  final t = TurnLines(r(1, 5));
  for (var k = 0; k < 4; k++) {
    t.next();
    print('S_${k + 1} = ${t.spread.rational}');
  }
}
Output
S_1 = 1/5
S_2 = 16/25
S_3 = 121/125
S_4 = 576/625

Constructors

TurnLinesconstructor
TurnLines(Rational s)
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  print(TurnLines(r(1, 4)).spread.rational);
}
Output
0

Properties

afield
final BigInt a
bfield
final BigInt b
cfield
late final BigInt c
b2field
late final BigInt b2
kgetter
int get k

The index of the newest line.

tgetter
BigInt get t

b^{k}T_{k}(1 - 2s) and b^{k-1}U_{k-1}(1 - 2s) for the newest line, and b^{k}.

ugetter
BigInt get u
bkgetter
BigInt get bk
spreadgetter
SpreadValue get spread

The spread between l_{0} and the newest line: S_{k}(s) = \frac{b^{k} - b^{k}T_{k}}{2b^{k}}, exactly.

({BigInt x, BigInt y, bool rootOnX}) get directionParts

The newest line's direction as integers (x, y) and where the fixed root r = \sqrt{a(b - a)} multiplies in: (b^{k} + b^{k}T_{k}, 2b^{k-1}U_{k-1} \cdot r) = 2\cos k\theta \cdot b^{k}(\cos k\theta, \sin k\theta), or, when \cos k\theta < 0 (so that the first form could cancel to nothing), the same line through (2b^{k-1}U_{k-1} \cdot r, b^{k} - b^{k}T_{k}) = 2\sin k\theta \cdot b^{k}(\cos k\theta, \sin k\theta). rootOnX says which coordinate r multiplies.

bitsgetter
int get bits

The bits the newest line costs (T and U together).

Methods

resetmethod
void reset()
Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final t = TurnLines(r(1, 4))..next()..next();
  t.reset();
  print(t.spread.rational);
}
Output
0
nextmethod
void next()

Turns once more (k \to k + 1): two big multiply-adds, no angle.

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final t = TurnLines(r(1, 4));
  t.next();
  t.next();
  print('${t.spread.rational} = S₂(1/4)');
}
Output
3/4 = S₂(1/4)
directionmethod
(double, double) direction(double root)

The newest line as a double direction for drawing. r = \sqrt{a(b - a)} is the one root in the family, taken by the caller at the screen's edge and passed in as root (so this file has no root function).

Example
import 'package:riptide_rational_special/riptide_rational_special.ray';
import 'package:riptide_precision/riptide_precision.ray';
import 'raya:convert';

void main() {
  Rational r(int a, [int b = 1]) => Rational.fromInt(a, b);
  final t = TurnLines(r(1, 4))..next();
  print(t.direction(1.7320508075688772));
}
Output
(6.0, 3.4641016151377544)