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

Vectors, quaternions and matrices

Allocation-free vector, quaternion and matrix types over SIMD and typed data, spherical harmonics and low-discrepancy sequences.

import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'package:riptide_math/ratio_laws.ray';
77 classes697 functions and methods326 of 897 documented623 runnable examples
final class Int64Ratio implements RatioBackend<Frac, List<Frac>>

Exact ratio geometry in int64 rationals (Frac) in any integer metric of dimension 2 or 3. For lattices, connectors and layout, where exactness matters and coordinates are small; every operation is checked, and an overflow throws RatioOverflow rather than returning a wrong answer.

Constructors

Int64Ratioconstructor
Int64Ratio(List<int> metric)

metric is the symmetric matrix, row-major (4 or 9 entries).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('exact spread of (1, 2), (3, 1): ${b.spread(b.vector([1, 2]), b.vector([3, 1]))}');
  final l = Int64Ratio([1, 0, 0, -1]);
  print('red (Lorentzian) Q(3, 5) = ${l.quadrance(l.vector([3, 5]))}');
}
Output
exact spread of (1, 2), (3, 1): 1/2
red (Lorentzian) Q(3, 5) = -16

Properties

final int dimension
namegetter
String get name
exactgetter
bool get exact

Methods

fromIntmethod
Frac fromInt(int n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('fromInt(7) = ${b.fromInt(7)}');
}
Output
fromInt(7) = 7
addmethod
Frac add(Frac a, Frac b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 add 1/6 = ${b.add(x, y)}');
}
Output
1/3 add 1/6 = 1/2
submethod
Frac sub(Frac a, Frac b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 sub 1/6 = ${b.sub(x, y)}');
}
Output
1/3 sub 1/6 = 1/6
mulmethod
Frac mul(Frac a, Frac b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 mul 1/6 = ${b.mul(x, y)}');
}
Output
1/3 mul 1/6 = 1/18
divmethod
Frac div(Frac a, Frac b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final x = b.div(b.fromInt(1), b.fromInt(3)), y = b.div(b.fromInt(1), b.fromInt(6));
  print('1/3 div 1/6 = ${b.div(x, y)}');
}
Output
1/3 div 1/6 = 2
isZeromethod
bool isZero(Frac a, Frac scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final x = b.sub(b.div(b.fromInt(1), b.fromInt(3)), b.div(b.fromInt(2), b.fromInt(6)));
  print('1/3 − 2/6 is zero: ${b.isZero(x, b.fromInt(1))}');
}
Output
1/3 − 2/6 is zero: true
toDoublemethod
double? toDouble(Frac a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.toDouble(b.div(b.fromInt(2), b.fromInt(3))));
}
Output
0.6666666666666666
vectormethod
List<Frac> vector(List<int> c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.vector([3, 4]));
}
Output
[3, 4]
vaddmethod
List<Frac> vadd(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.vadd(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[7, 1]
vsubmethod
List<Frac> vsub(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.vsub(b.vector([3, 4]), b.vector([4, -3])));
}
Output
[-1, 7]
vscalemethod
List<Frac> vscale(Frac k, List<Frac> v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.vscale(b.div(b.fromInt(1), b.fromInt(2)), b.vector([3, 4])));
}
Output
[3/2, 2]
sizemethod
Frac size(List<Frac> v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('size of [3, 4]: ${b.size(b.vector([3, 4]))}');
}
Output
size of [3, 4]: 25
samemethod
bool same(List<Frac> a, List<Frac> b, Frac scale)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.same(b.vscale(b.fromInt(2), b.vector([1, 2])), b.vadd(b.vector([1, 2]), b.vector([1, 2])), b.size(b.vector([1, 2]))));
}
Output
true
dotmethod
Frac dot(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('dot([3, 4], [1, 2]) = ${b.dot(b.vector([3, 4]), b.vector([1, 2]))}');
  print('dot([3, 4], [4, -3]) = ${b.dot(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
dot([3, 4], [1, 2]) = 11
dot([3, 4], [4, -3]) = 0
quadrancemethod
Frac quadrance(List<Frac> a)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('Q([3, 4]) = ${b.quadrance(b.vector([3, 4]))}');
}
Output
Q([3, 4]) = 25
spreadmethod
Frac spread(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('spread([3, 4], [1, 2]) = ${b.spread(b.vector([3, 4]), b.vector([1, 2]))}');
  print('spread([3, 4], [4, -3]) = ${b.spread(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
spread([3, 4], [1, 2]) = 4/125
spread([3, 4], [4, -3]) = 1
crossmethod
Frac cross(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('cross([3, 4], [1, 2]) = ${b.cross(b.vector([3, 4]), b.vector([1, 2]))}');
  print('cross([3, 4], [4, -3]) = ${b.cross(b.vector([3, 4]), b.vector([4, -3]))}');
}
Output
cross([3, 4], [1, 2]) = 121/125
cross([3, 4], [4, -3]) = 0
twistmethod
Frac twist(List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('twist([3, 4], [1, 2]) = ${b.twist(b.vector([3, 4]), b.vector([1, 2]))}');
  print('twist([3, 4], [1, 3]) = ${b.twist(b.vector([3, 4]), b.vector([1, 3]))}');
}
Output
twist([3, 4], [1, 2]) = 4/121
twist([3, 4], [1, 3]) = 1/9
Frac solidSpread(List<Frac> a, List<Frac> b, List<Frac> c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  // Three-dimensional quantities need a 3-D metric.
  final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
  print('solid spread of the axes: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([0, 0, 1]))}');
  print('of a flat triple: ${b3.solidSpread(b3.vector([1, 0, 0]), b3.vector([0, 1, 0]), b3.vector([1, 1, 0]))}');
}
Output
solid spread of the axes: 1
of a flat triple: 0
Frac dihedralSpread(List<Frac> c, List<Frac> a, List<Frac> b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  // Three-dimensional quantities need a 3-D metric.
  final b3 = Int64Ratio([1, 0, 0, 0, 1, 0, 0, 0, 1]);
  print('dihedral spread of the xz and yz planes along z: ${b3.dihedralSpread(b3.vector([0, 0, 1]), b3.vector([1, 0, 0]), b3.vector([1, 1, 0]))}');
}
Output
dihedral spread of the xz and yz planes along z: 1/2
Frac gramDeterminant(List<List<Frac>> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print(b.gramDeterminant([b.vector([3, 4]), b.vector([1, 2])]));
}
Output
4
reflectmethod
List<Frac> reflect(List<Frac> v, List<Frac> n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  print('reflect [3, 4] in [1, 2]: ${b.reflect(b.vector([3, 4]), b.vector([1, 2]))}');
}
Output
reflect [3, 4] in [1, 2]: [-7/5, -24/5]
List<Frac> perpendicular(List<List<Frac>> vs)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final p = b.perpendicular([b.vector([3, 4])]);
  print('$p, dot ${b.dot(p, b.vector([3, 4]))}');
}
Output
[-4, 3], dot 0
rotatemethod
List<Frac> rotate(List<Frac> k, List<Frac> v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final b = Int64Ratio([1, 0, 0, 1]);
  final t = b.div(b.fromInt(1), b.fromInt(2));
  print('rotate [3, 4] by t = 1/2: ${b.rotate([t], b.vector([3, 4]))}');
}
Output
rotate [3, 4] by t = 1/2: [5, 0]
abstract interface class RatioBatch

Ratio geometry over arrays of single-precision vectors: the batched backend. Layouts are tightly packed floats (no float3 padding):

KernelInput per itemOutput per item
spreadCross2ax ay bx byspread, cross
spreadCross3ax ay az bx by bzspread, cross
solidSpreads3a b c (9 floats)solid spread
dihedralSpreads3c a b (9 floats; planes meet along c)dihedral spread
cayleyRotate3gx gy gz vx vy vzrotated v (3 floats)
exactSpreads2ax ay bx by as int32, \operatorname{abs} < 2^{15}(u \wedge v)^{2}, Q(u)Q(v) as int64: the spread, exactly

Properties

namegetter
String get name

Methods

void spreadCross2(Float32List pairs, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Float32List(4);
  k.spreadCross2(Float32List.fromList([1, 0, 1, 1, 3, 4, 4, -3]), out);
  print('(spread, cross) per pair: $out');
}
Output
(spread, cross) per pair: [0.5, 0.5, 1.0, 0.0]
void spreadCross3(Float32List pairs, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Float32List(2);
  k.spreadCross3(Float32List.fromList([1, 0, 0, 1, 1, 0]), out);
  print(out);
}
Output
[0.5, 0.5]
void solidSpreads3(Float32List triples, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Float32List(1);
  k.solidSpreads3(Float32List.fromList([1, 0, 0, 0, 1, 0, 0, 0, 1]), out);
  print('solid spread of the axes: ${out[0]}');
}
Output
solid spread of the axes: 1.0
void dihedralSpreads3(Float32List triples, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Float32List(1);
  k.dihedralSpreads3(Float32List.fromList([0, 0, 1, 1, 0, 0, 1, 1, 0]), out);
  print('dihedral spread: ${out[0]}');
}
Output
dihedral spread: 0.5
void cayleyRotate3(Float32List gv, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Float32List(3);
  k.cayleyRotate3(Float32List.fromList([0, 0, 1, 1, 0, 0]), out);
  print('x under g = (0, 0, 1): $out (a quarter turn)');
}
Output
x under g = (0, 0, 1): [0.0, 1.0, 0.0] (a quarter turn)
void exactSpreads2(Int32List pairs, Int64List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const RatioBatch k = CpuRatioBatch();
  final out = Int64List(2);
  k.exactSpreads2(Int32List.fromList([1, 2, 3, 1]), out);
  print('spread = ${out[0]}/${out[1]}');
}
Output
spread = 25/50
final class CpuRatioBatch implements RatioBatch

RatioBatch on the CPU: each item through the float core (RatioF) in double, stored as float.

Constructors

CpuRatioBatchconstructor
const CpuRatioBatch()
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  print('backend: ${k.name}');
}
Output
backend: cpu f32 batch

Properties

namegetter
String get name

Methods

void spreadCross2(Float32List p, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Float32List(4);
  k.spreadCross2(Float32List.fromList([1, 0, 1, 1, 3, 4, 4, -3]), out);
  print('(spread, cross) per pair: $out');
}
Output
(spread, cross) per pair: [0.5, 0.5, 1.0, 0.0]
void spreadCross3(Float32List p, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Float32List(2);
  k.spreadCross3(Float32List.fromList([1, 0, 0, 1, 1, 0]), out);
  print(out);
}
Output
[0.5, 0.5]
void solidSpreads3(Float32List p, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Float32List(1);
  k.solidSpreads3(Float32List.fromList([1, 0, 0, 0, 1, 0, 0, 0, 1]), out);
  print('solid spread of the axes: ${out[0]}');
}
Output
solid spread of the axes: 1.0
void dihedralSpreads3(Float32List p, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Float32List(1);
  k.dihedralSpreads3(Float32List.fromList([0, 0, 1, 1, 0, 0, 1, 1, 0]), out);
  print('dihedral spread: ${out[0]}');
}
Output
dihedral spread: 0.5
void cayleyRotate3(Float32List p, Float32List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Float32List(3);
  k.cayleyRotate3(Float32List.fromList([0, 0, 1, 1, 0, 0]), out);
  print('x under g = (0, 0, 1): $out (a quarter turn)');
}
Output
x under g = (0, 0, 1): [0.0, 1.0, 0.0] (a quarter turn)
void exactSpreads2(Int32List p, Int64List out)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const k = CpuRatioBatch();
  final out = Int64List(2);
  k.exactSpreads2(Int32List.fromList([1, 2, 3, 1]), out);
  print('spread = ${out[0]}/${out[1]}');
}
Output
spread = 25/50
abstract final class RatioBoundary

The named adapters between ratio geometry and radians: the only place in the ratio layer that calls trigonometry. Use them where a platform API, a file format, a sensor or a person speaks in angles: gesture rotation, a field of view in a settings file, a bearing from a map service, an angle shown in the UI. Everything between two boundaries stays in spreads, turns and Cayley parameters.

Static

cayleyFromRadiansstaticmethod
static double cayleyFromRadians(double radians)

The Cayley parameter \tan(\frac{\theta}{2}) of an angle in radians.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('60° -> t = ${f(RatioBoundary.cayleyFromRadians(math.pi / 3))} (tan 30°)');
}
Output
60° -> t = 0.577350 (tan 30°)
radiansFromCayleystaticmethod
static double radiansFromCayley(double t)

The angle in radians, in (-\pi, \pi), of a Cayley parameter (2 \cdot \operatorname{atan} t; infinity gives \pi).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('t = 1 -> ${f(RatioBoundary.radiansFromCayley(1))} rad (π/2)');
}
Output
t = 1 -> 1.570796 rad (π/2)
radiansFromSpreadstaticmethod
static double radiansFromSpread(double spread)

The acute angle in [0, \frac{\pi}{2}] with spread spread (\sin^{2}\theta = s), as \operatorname{atan2}(\sqrt{s}, \sqrt{1 - s}): accurate near 0 and near \frac{\pi}{2}, where \operatorname{asin}(\sqrt{s}) and \operatorname{acos}(\sqrt{1 - s}) lose digits.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('spread 1/2 -> ${f(RatioBoundary.radiansFromSpread(0.5))} rad (π/4)');
}
Output
spread 1/2 -> 0.785398 rad (π/4)
spreadFromRadiansstaticmethod
static double spreadFromRadians(double radians)

\sin^{2}\theta.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print([for (final d in [0, 30, 45, 60, 90]) f(RatioBoundary.spreadFromRadians(d * math.pi / 180))]);
}
Output
[0.000000, 0.250000, 0.500000, 0.750000, 1.000000]
turnFromRadiansstaticmethod
static Turn turnFromRadians(double radians)

The turn by an angle in radians (\cos \theta, \sin \theta).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final t = RatioBoundary.turnFromRadians(math.pi / 6);
  print('(${f(t.c)}, ${f(t.s)})');
}
Output
(0.866025, 0.500000)
radiansFromTurnstaticmethod
static double radiansFromTurn(Turn t)

The angle of a turn in (-\pi, \pi].

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(f(RatioBoundary.radiansFromTurn(Turn.unit(0, -1))));
}
Output
-1.570796
static double radiansFromDirection(double x, double y)

The angle in (-\pi, \pi] of the direction (x, y) from the x axis, for APIs that report a heading in radians. Any positive multiple of (x, y) gives the same angle, so the direction need not be normalised (exact by construction).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(f(RatioBoundary.radiansFromDirection(-1, 1)));
}
Output
2.356194
turnFromDegreesstaticmethod
static Turn turnFromDegrees(double degrees)

The turn by an angle in degrees (maps, SVG, settings, a person's input).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final t = RatioBoundary.turnFromDegrees(90);
  print('(${f(t.c)}, ${f(t.s)})');
}
Output
(0.000000, 1.000000)
turnFromFractionstaticmethod
static Turn turnFromFraction(double fraction)

The turn by fraction of a whole turn (\frac{1}{n} for the step of a regular n-gon). Multiples of a quarter turn are exact (no rounding of \pi); any other fraction goes through radians once.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final t = RatioBoundary.turnFromFraction(1 / 8);
  print('one eighth: (${f(t.c)}, ${f(t.s)})');
}
Output
one eighth: (0.707107, 0.707107)
static Quat quatFromAxisRadians(Vec3 axis, double radians)

The rotation by radians about the unit axis (the platform form of Quat.axisAngle, kept here so call sites that receive radians name the boundary).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final q = RatioBoundary.quatFromAxisRadians(Vec3.unitZ, math.pi / 2);
  final r = q.rotate(Vec3.unitX);
  print('x -> (${f(r.x)}, ${f(r.y)}, ${f(r.z)})');
}
Output
x -> (0.000000, 1.000000, 0.000000)
radiansFromDegreesstaticmethod
static double radiansFromDegrees(double degrees)

Degrees to radians and back, for formats that store degrees (maps, SVG, settings).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioBoundary.radiansFromDegrees(180));
}
Output
3.141592653589793
degreesFromRadiansstaticmethod
static double degreesFromRadians(double radians)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioBoundary.degreesFromRadians(math.pi / 4));
}
Output
45.0
class

Metric2

final class Metric2

A planar metric: the symmetric matrix B = \begin{bmatrix}a & b \\ b & c\end{bmatrix} with B(u, v) = u^{T}Bv (Wildberger, "Affine and projective universal geometry", arXiv:math/0612499). Blue is Euclidean, red is x^{2} - y^{2} (1 + 1 Minkowski), green is 2xy (the red metric in null coordinates). Every formula here is the general-metric form of the blue one, so a non-Euclidean geometry is a different constant, not different code.

Constructors

Metric2constructor
const Metric2(this.a, this.b, this.c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  for (final (n, m) in [('blue', blue), ('red', red), ('green', green)]) {
    print('$n: Q(3, 4) = ${m.quadrance(3, 4)}');
  }
}
Output
blue: Q(3, 4) = 25.0
red: Q(3, 4) = -7.0
green: Q(3, 4) = 24.0

Static

blueconstant
static const blue = Metric2(1, 0, 1)

x^{2} + y^{2}.

redconstant
static const red = Metric2(1, 0, -1)

x^{2} - y^{2}.

greenconstant
static const green = Metric2(0, 1, 0)

2xy.

Properties

afield
final double a
bfield
final double b
cfield
final double c
double get determinant
isBluegetter
bool get isBlue

Methods

dotmethod
double dot(double ux, double uy, double vx, double vy)

B(u, v).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  print('blue (1, 2)·(3, 4) = ${blue.dot(1, 2, 3, 4)}, red ${red.dot(1, 2, 3, 4)}, green ${green.dot(1, 2, 3, 4)}');
}
Output
blue (1, 2)·(3, 4) = 11.0, red -5.0, green 10.0
quadrancemethod
double quadrance(double x, double y)

Q(v) = B(v, v).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  print('red quadrance of a null vector (1, 1): ${red.quadrance(1, 1)}');
}
Output
red quadrance of a null vector (1, 1): 0.0
spreadmethod
double spread(double ux, double uy, double vx, double vy)

s(u, v) = \det B \cdot \frac{(u \wedge v)^{2}}{Q(u)Q(v)} (Goh–Wildberger, arXiv:0911.1025, p. 7): no 1 - \operatorname{cross} cancellation.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  print('blue spread of (1, 0), (1, 1): ${blue.spread(1, 0, 1, 1)}');
  print('perpendicular: ${blue.spread(1, 2, -2, 1)}');
}
Output
blue spread of (1, 0), (1, 1): 0.5
perpendicular: 1.0
crossmethod
double cross(double ux, double uy, double vx, double vy)

c(u, v) = \frac{B(u, v)^{2}}{Q(u)Q(v)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  print('blue cross (1, 0) × (0, 1) = ${blue.cross(1, 0, 0, 1)}');
}
Output
blue cross (1, 0) × (0, 1) = 0.0
(double, double) perpendicular(double x, double y)

A vector B-perpendicular to v: J \cdot Bv with J the quarter turn, so B(v, perp) = 0 in every metric.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  for (final (n, m) in [('blue', blue), ('red', red), ('green', green)]) {
    final (px, py) = m.perpendicular(2, 1);
    print('$n ⟂ of (2, 1): ($px, $py), dot ${m.dot(2, 1, px, py)}');
  }
}
Output
blue ⟂ of (2, 1): (-1.0, 2.0), dot 0.0
red ⟂ of (2, 1): (1.0, 2.0), dot 0.0
green ⟂ of (2, 1): (-2.0, 1.0), dot 0.0
cayleymethod
Float64List cayley(double t)

The Cayley rotation R = (B - K)^{-1}(B + K), K = t \cdot \begin{bmatrix}0 & 1 \\ -1 & 0\end{bmatrix}: an isometry of this metric (R^{T}BR = B) with \det R = 1, rational in t (the blue case is the rational parametrisation of the circle). Row-major [r00, r01, r10, r11]. Throws when B - K is singular (t^{2} = -\det B).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const blue = Metric2(1, 0, 1), red = Metric2(1, 0, -1), green = Metric2(0, 1, 0);
  final r = blue.cayley(0.5);
  print('blue Cayley rotation at t = 1/2: $r');
  final pts = [for (var k = -8; k <= 8; k++) (() { final m = blue.cayley(k / 4); return [m[0], m[2]]; })()];
}
Output
blue Cayley rotation at t = 1/2: [0.6000000000000001, 0.8, -0.8, 0.6000000000000001]
class

Metric3

final class Metric3

A metric on three-dimensional space: the symmetric matrix B with entries [xx, yy, zz, xy, xz, yz]. Euclidean is the identity; Minkowski 2 + 1 is \operatorname{diag}(1, 1, -1).

Constructors

Metric3constructor
const Metric3(this.xx, this.yy, this.zz, [this.xy = 0, this.xz = 0, this.yz = 0])
Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print('Euclidean Q(1, 2, 2) = ${e.quadrance(1, 2, 2)}, Lorentzian ${l.quadrance(1, 2, 2)}');
}
Output
Euclidean Q(1, 2, 2) = 9.0, Lorentzian 1.0

Static

euclideanconstant
static const euclidean = Metric3(1, 1, 1)
minkowskiconstant
static const minkowski = Metric3(1, 1, -1)

\operatorname{diag}(1, 1, -1): 2 + 1 Minkowski space (Wildberger's red metric in three dimensions).

Properties

xxfield
final double xx
yyfield
final double yy
zzfield
final double zz
xyfield
final double xy
xzfield
final double xz
yzfield
final double yz
bool get isEuclidean
double get determinant

Methods

dotmethod
double dot(double ux, double uy, double uz, double vx, double vy, double vz)

B(u, v).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print(e.dot(1, 2, 3, 4, 5, 6));
}
Output
32.0
quadrancemethod
double quadrance(double x, double y, double z)

Q(v).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print('light-like (1, 0, 1) in the Lorentzian metric: ${l.quadrance(1, 0, 1)}');
}
Output
light-like (1, 0, 1) in the Lorentzian metric: 0.0
double adjQuadrance(double x, double y, double z)

w^{T} \operatorname{adj}(B) w.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print(e.adjQuadrance(1, 2, 3));
  print(l.adjQuadrance(1, 2, 3));
}
Output
14.0
4.0
spreadmethod
double spread(double ux, double uy, double uz, double vx, double vy, double vz)

s(u, v) = (u \times v)^{T} \operatorname{adj}(B) \frac{u \times v}{Q(u)Q(v)}: Lagrange's identity for a general metric (Notowidigdo–Wildberger, arXiv:1909.08814, Theorem 8, written with adj B), so no 1 - \operatorname{cross} cancellation.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print('spread between x and (1, 1, 0): ${e.spread(1, 0, 0, 1, 1, 0)}');
}
Output
spread between x and (1, 1, 0): 0.5
crossmethod
double cross(double ux, double uy, double uz, double vx, double vy, double vz)

c(u, v) = \frac{B(u, v)^{2}}{Q(u)Q(v)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print(e.cross(1, 0, 0, 0, 1, 0));
}
Output
0.0
(double, double, double) vectorProduct(double ux, double uy, double uz, double vx, double vy, double vz)

The B-vector product \operatorname{adj}(B)(u \times v): B-perpendicular to u and v (Notowidigdo–Wildberger, arXiv:1903.08330).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  print('Euclidean x × y = ${e.vectorProduct(1, 0, 0, 0, 1, 0)}');
  print('Lorentzian x × y = ${l.vectorProduct(1, 0, 0, 0, 1, 0)}');
}
Output
Euclidean x × y = (0.0, 0.0, 1.0)
Lorentzian x × y = (0.0, 0.0, 1.0)
cayleymethod
Float64List cayley(double gx, double gy, double gz)

The Cayley rotation R = (B - K)^{-1}(B + K) with K = gₓ (the skew matrix of g): an isometry of this metric with \det R = 1, rational in g; for the Euclidean metric it is Euler–Rodrigues with Gibbs vector g. Row-major 3 \times 3. Throws when B - K is singular.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  const e = Metric3(1, 1, 1), l = Metric3(1, 1, -1);
  final r = e.cayley(0, 0, 1); // Gibbs vector along z
  print('rotation (row-major): $r');
}
Output
rotation (row-major): [0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
abstract final class RatioMsl

The batched ratio kernels in Metal Shading Language: the GPU backend of RatioBatch, item for item the same formulas and layouts as CpuRatioBatch. A plain string so any GPU path can compile it (Riptide's gpu.library(source), a Metal harness, an Evelyn GPU compute shader port).

Every planar 2 \times 2 determinant (each wedge and each component of a vector product) uses Kahan's FMA form, which is correct to about one ulp even for nearly parallel vectors, so f32 spreads keep their digits where acos and atan2 don't.

Static

kernelsconstant
static const kernels

Kernel names, in the order of RatioBatch's methods.

sourceconstant
static const source
class

RatioF

abstract final class RatioF

Rational Trigonometry on doubles. No sin, cos or \pi.

Static

quadrance2staticmethod
static double quadrance2(double x, double y)

Q(x, y) = x^{2} + y^{2}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('quadrance2(3, 4) = ${RatioF.quadrance2(3, 4)}');
}
Output
quadrance2(3, 4) = 25.0
wedge2staticmethod
static double wedge2(double ax, double ay, double bx, double by)

The wedge u \wedge v = u_{x}v_{y} - u_{yv_{x}} (twice the signed area).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('wedge2(1, 0, 1, 1) = ${RatioF.wedge2(1, 0, 1, 1)}');
}
Output
wedge2(1, 0, 1, 1) = 1.0
spread2staticmethod
static double spread2(double ax, double ay, double bx, double by)

s(u, v) = \frac{(u \wedge v)^{2}}{Q(u)Q(v)}. NaN when either vector is zero.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('spread2(1, 0, 1, 1) = ${RatioF.spread2(1, 0, 1, 1)}');
}
Output
spread2(1, 0, 1, 1) = 0.5
cross2staticmethod
static double cross2(double ax, double ay, double bx, double by)

c(u, v) = \frac{(u \cdot v)^{2}}{Q(u)Q(v)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('cross2(1, 0, 1, 1) = ${RatioF.cross2(1, 0, 1, 1)}');
}
Output
cross2(1, 0, 1, 1) = 0.5
wedge2Accuratestaticmethod
static double wedge2Accurate(double ax, double ay, double bx, double by)

The wedge as if computed in twice the precision (Ogita–Rump–Oishi Dot2 with Dekker's exact products; Raya has no FMA, so Veltkamp splitting stands in for Kahan's FMA form): correct to about one ulp even for nearly parallel vectors, where the plain wedge keeps only the digits the inputs' difference has. About 25 flops.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('wedge2Accurate(1e8 + 1, 1e8, 1e8, 1e8 - 1) = ${RatioF.wedge2Accurate(1e8 + 1, 1e8, 1e8, 1e8 - 1)}');
}
Output
wedge2Accurate(1e8 + 1, 1e8, 1e8, 1e8 - 1) = -1.0
spread2Accuratestaticmethod
static double spread2Accurate(double ax, double ay, double bx, double by)

spread2 from wedge2Accurate: about one ulp at every angle.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('spread2Accurate(1, 1e-9, 1, 0) = ${RatioF.spread2Accurate(1, 1e-9, 1, 0)}');
}
Output
spread2Accurate(1, 1e-9, 1, 0) = 1e-18
spread3Accuratestaticmethod
static double spread3Accurate(double ax, double ay, double az, double bx, double by, double bz)

spread3 with each component of u \times v from wedge2Accurate.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('spread3Accurate(1, 1e-9, 0, 1, 0, 0) = ${RatioF.spread3Accurate(1, 1e-9, 0, 1, 0, 0)}');
}
Output
spread3Accurate(1, 1e-9, 0, 1, 0, 0) = 1e-18
twist2staticmethod
static double twist2(double ax, double ay, double bx, double by)

t(u, v) = \frac{(u \wedge v)^{2}}{(u \cdot v)^{2}}. Infinite for perpendicular vectors.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('twist2(1, 0, 1, 1) = ${RatioF.twist2(1, 0, 1, 1)}');
}
Output
twist2(1, 0, 1, 1) = 1.0
quadrance3staticmethod
static double quadrance3(double x, double y, double z)

Q(x, y, z) = x^{2} + y^{2} + z^{2}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('quadrance3(1, 2, 2) = ${RatioF.quadrance3(1, 2, 2)}');
}
Output
quadrance3(1, 2, 2) = 9.0
spread3staticmethod
static double spread3(double ax, double ay, double az, double bx, double by, double bz)

s(u, v) = \frac{Q(u \times v)}{Q(u)Q(v)} (Lagrange's identity; no cancellation for nearly parallel vectors).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('spread3(1, 0, 0, 1, 1, 1) = ${RatioF.spread3(1, 0, 0, 1, 1, 1)}');
}
Output
spread3(1, 0, 0, 1, 1, 1) = 0.6666666666666666
cross3staticmethod
static double cross3(double ax, double ay, double az, double bx, double by, double bz)

c(u, v) = \frac{(u \cdot v)^{2}}{Q(u)Q(v)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('cross3(1, 0, 0, 1, 1, 1) = ${RatioF.cross3(1, 0, 0, 1, 1, 1)}');
}
Output
cross3(1, 0, 0, 1, 1, 1) = 0.3333333333333333
twist3staticmethod
static double twist3(double ax, double ay, double az, double bx, double by, double bz)

t(u, v) = \frac{Q(u \times v)}{(u \cdot v)^{2}}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('twist3(1, 0, 0, 1, 1, 1) = ${RatioF.twist3(1, 0, 0, 1, 1, 1)}');
}
Output
twist3(1, 0, 0, 1, 1, 1) = 2.0
det3staticmethod
static double det3(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz)

Det[a b c] (the triple product a \cdot (b \times c)).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('det3(1, 0, 0, 0, 1, 0, 0, 0, 1) = ${RatioF.det3(1, 0, 0, 0, 1, 0, 0, 0, 1)}');
}
Output
det3(1, 0, 0, 0, 1, 0, 0, 0, 1) = 1.0
solidSpread3staticmethod
static double solidSpread3(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz)

The solid spread \det\frac{[a b c]^{2}}{Q(a)Q(b)Q(c)} (Notowidigdo–Wildberger): 0 for coplanar vectors, 1 for mutually perpendicular ones; the rational analogue of the solid angle at a corner.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('solidSpread3(1, 0, 0, 0, 1, 0, 1, 1, 1) = ${RatioF.solidSpread3(1, 0, 0, 0, 1, 0, 1, 1, 1)}');
}
Output
solidSpread3(1, 0, 0, 0, 1, 0, 1, 1, 1) = 0.3333333333333333
dihedralSpread3staticmethod
static double dihedralSpread3(double cx, double cy, double cz, double ax, double ay, double az, double bx, double by, double bz)

The dihedral spread between the planes \operatorname{span}(c, a) and \operatorname{span}(c, b), which meet along c: Q(c) \cdot \frac{\det[c a b]^{2}}{Q(c \times a) \cdot Q(c \times b)} (Notowidigdo–Wildberger, Theorem 12). 0 for one plane, 1 for perpendicular planes. NaN when a or b is parallel to c.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('dihedralSpread3(0, 0, 1, 1, 0, 0, 1, 1, 0) = ${RatioF.dihedralSpread3(0, 0, 1, 1, 0, 0, 1, 1, 0)}');
}
Output
dihedralSpread3(0, 0, 1, 1, 0, 0, 1, 1, 0) = 0.5
linePlaneSpread3staticmethod
static double linePlaneSpread3(double vx, double vy, double vz, double ax, double ay, double az, double bx, double by, double bz)

The spread between the line through v and the plane \operatorname{span}(a, b): \frac{(v \cdot (a \times b))^{2}}{Q(v) \cdot Q(a \times b)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('linePlaneSpread3(1, 1, 1, 1, 0, 0, 0, 1, 0) = ${RatioF.linePlaneSpread3(1, 1, 1, 1, 0, 0, 0, 1, 0)}');
}
Output
linePlaneSpread3(1, 1, 1, 1, 0, 0, 0, 1, 0) = 0.3333333333333333
reflectFactorstaticmethod
static double reflectFactor(double xDotN, double nQuadrance)

The factor k of a reflection x \mapsto x - k \cdot n in the line or plane B-perpendicular to n: k = \frac{2B(x, n)}{Q(n)}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('reflectFactor(3, 2) = ${RatioF.reflectFactor(3, 2)}');
}
Output
reflectFactor(3, 2) = 3.0
cayleyCosstaticmethod
static double cayleyCos(double t)

The planar Cayley rotation with parameter t (classically t = \tan(\frac{\theta}{2})): \cos = \frac{1 - t^{2}}{1 + t^{2}}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('cayleyCos(0.5) = ${RatioF.cayleyCos(0.5)}');
}
Output
cayleyCos(0.5) = 0.6
cayleySinstaticmethod
static double cayleySin(double t)

\sin = \frac{2t}{1 + t^{2}}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('cayleySin(0.5) = ${RatioF.cayleySin(0.5)}');
}
Output
cayleySin(0.5) = 0.8
cayleyFactor3staticmethod
static double cayleyFactor3(double gx, double gy, double gz)

The factor \frac{2}{1 + g \cdot g} of the 3-D Cayley rotation with Gibbs vector g (g = \tan(\frac{\theta}{2}) \cdot \mathrm{axis}): R v = v + k \cdot (g \times v + g \times (g \times v)), rational in g (Euler–Rodrigues). Composition is composeGibbs.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('cayleyFactor3(0, 0, 1) = ${RatioF.cayleyFactor3(0, 0, 1)}');
}
Output
cayleyFactor3(0, 0, 1) = 1.0
twistParameterstaticmethod
static double twistParameter(double x, double y, double z, double w, double ax, double ay, double az)

The Cayley parameter of the twist of a unit quaternion (x, y, z, w) about the unit axis a (swing–twist decomposition): t = \frac{v \cdot a}{w}, the tangent of half the twist angle, read off the quaternion with one division. ±infinity at a half turn. It is monotonic in the angle on (-\pi, \pi), so it compares against limits directly.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print('twistParameter(0, 0, 0.7071067811865476, 0.7071067811865476, 0, 0, 1) = ${RatioF.twistParameter(0, 0, 0.7071067811865476, 0.7071067811865476, 0, 0, 1)}');
}
Output
twistParameter(0, 0, 0.7071067811865476, 0.7071067811865476, 0, 0, 1) = 1.0
extension type

Turn

extension type const Turn._raw(Float64x2 v)

A planar rotation (blue metric) held as its unit vector (c, s) = (\cos \theta, \sin \theta), never as \theta. An extension type over Float64x2, so the VM keeps it in a register like Vec2: composing, inverting and applying turns allocate nothing and call no trigonometry. Counter-clockwise for positive s. Made from a rational Cayley parameter, from a direction, or between two directions; radians only through RatioBoundary.

Constructors

Turn.unitconstructor
Turn.unit(double c, double s)

The turn with unit vector (c, s); the caller guarantees c^{2} + s^{2} = 1 (use Turn.direction otherwise).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.unit(0.6, 0.8);
  print('c = ${t.c}, s = ${t.s}');
  print('applied to (1, 0): ${t.apply(1, 0)}');
}
Output
c = 0.6, s = 0.8
applied to (1, 0): (0.6, 0.8)
factory Turn.cayley(double t)

The rotation with Cayley parameter t (classically \tan(\frac{\theta}{2})): \frac{((1 - t^{2}), 2t)}{1 + t^{2}}. Rational in t, so exact rational t gives an exact rational turn (a Pythagorean triple); every turn but the half turn arises once.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final pts = [for (var k = -10; k <= 10; k++) Turn.cayley(k / 4)];
  print('t = 1/2: (${f(Turn.cayley(0.5).c)}, ${f(Turn.cayley(0.5).s)}) = (3/5, 4/5)');
}
Output
t = 1/2: (0.6000, 0.8000) = (3/5, 4/5)
factory Turn.direction(double x, double y)

The turn taking the x axis to the direction (x, y) (one square root). The zero vector gives identity.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.direction(3, 4);
  print('direction (3, 4): c = ${t.c}, s = ${t.s}');
}
Output
direction (3, 4): c = 0.6000000000000001, s = 0.8
factory Turn.between(double ax, double ay, double bx, double by)

The turn taking the direction of a to the direction of b: \frac{(a \cdot b, a \wedge b)}{\sqrt{Q(a)Q(b)}}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.between(1, 0, 0, 1);
  print('from x to y: c = ${f(t.c)}, s = ${f(t.s)}');
}
Output
from x to y: c = 0.0000, s = 1.0000
Turn.phasefactory
factory Turn.phase(double turns)

The rational circle point at turns whole turns: periodic with period 1, exactly on the unit circle, exact at every quarter turn, with continuous velocity (C^{1}), and no trigonometry. It is the clock for anything periodic in time (a bob, a pulse, a wave, an orbit drawn at a steady rate): Turn.phase(t / period).s swings between -1 and 1 with the period and amplitude of a sine, without one.

Each quarter is the Cayley rotation by m = \frac{v(22 - v^{2})}{28 - 7v^{2}} of the quarter's own phase v in [0, 1): m runs from 0 to 1, so the point runs from one axis to the next, and m'(1) = \frac{11}{7} = 2m'(0) makes the speed 2m'/(1 + m^{2}) the same on both sides of every axis. The point leads or lags the uniform one by at most 1.5 \cdot 10^{-4} rad (0.008^{\circ}).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final xs = [for (var i = 0; i <= 64; i++) i / 64];
  print('phase(1/4) = (${f(Turn.phase(0.25).c)}, ${f(Turn.phase(0.25).s)})');
}
Output
phase(1/4) = (0.0000, 1.0000)
factory Turn.random(math.Random random)

A direction drawn uniformly from the circle with random: a point drawn uniformly from the unit disk (rejection from the square, \frac{\pi}{4} accepted) and scaled to unit length. Every direction is equally likely, as with a uniform angle, and no angle is drawn. Takes two or more draws (2.55 on average).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = math.Random(1);
  final ts = [for (var i = 0; i < 60; i++) Turn.random(r)];
  print('all unit: ${ts.every((t) => (t.c * t.c + t.s * t.s - 1).abs() < 1e-12)}');
}
Output
all unit: true

Static

identitystaticfield
static final identity
halfstaticfield
static final half

The half turn (-1, 0).

gaussianstaticmethod
static double gaussian(math.Random random)

A standard normal sample (mean 0, variance 1) by Marsaglia's polar method: the uniform disk point of Turn.random scaled by \sqrt{-2 \ln q / q}. Box–Muller without its cosine of a uniform angle.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final r = math.Random(2);
  final g = [for (var i = 0; i < 4000; i++) Turn.gaussian(r)];
  final mean = g.reduce((a, b) => a + b) / g.length;
  final v = g.map((x) => (x - mean) * (x - mean)).reduce((a, b) => a + b) / g.length;
  print('mean ${f(mean)}, variance ${f(v)}');
}
Output
mean 0.0053, variance 0.9945

Properties

cgetter
double get c
sgetter
double get s
inversegetter
Turn get inverse
spreadgetter
double get spread

The spread between a direction and its image, s^{2} (\sin^{2}\theta).

crossgetter
double get cross

The cross c^{2} (\cos^{2}\theta).

double get cayleyParameter

The Cayley parameter \frac{s}{1 + c} (\tan(\frac{\theta}{2})); infinite for the half turn.

halvedgetter
Turn get halved

The turn by half this turn's angle (the angle taken in (-\pi, \pi], so the half lies in (-\frac{\pi}{2}, \frac{\pi}{2}]): the direction of (1 + c, s), or of (s, 1 - c) when c < 0, where that sum would cancel. One square root, no angle.

Methods

powermethod
Turn power(int n)

This turn applied n times (n \ge 0) by repeated squaring: the multiple-angle formulas without angles.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.cayley(0.2);
  final pts = [for (var n = 0; n <= 12; n++) t.power(n)];
  print('t^5 = (${f(t.power(5).c)}, ${f(t.power(5).s)})');
}
Output
t^5 = (-0.3923, 0.9198)
applymethod
(double, double) apply(double x, double y)

(x, y) turned.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.unit(0, 1);
  print('quarter turn of (2, 1): ${t.apply(2, 1)}');
}
Output
quarter turn of (2, 1): (-1.0, 2.0)
rotatemethod
Vec2 rotate(Vec2 p)

p turned.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.cayley(0.3);
  final sq = [Vec2(0, 0), Vec2(1, 0), Vec2(1, 1), Vec2(0, 1)];
  final r = [for (final p in sq) t.rotate(p)];
  print('(1, 0) -> (${f(r[1].x)}, ${f(r[1].y)})');
}
Output
(1, 0) -> (0.8349, 0.5505)
fractionmethod
Turn fraction(double t)

This turn's angle (taken in (-\pi, \pi]) times t, for any real t: the whole part of t is a power, the fraction is composed from successive halved turns, one per binary digit of the fraction (at most 53). Square roots only, exact to rounding; the interpolation slerp needs, with no angle.

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final t = Turn.unit(0, 1); // a quarter turn
  final pts = [for (var i = 0; i <= 8; i++) t.fraction(i / 8)];
  print('half of a quarter turn: (${f(t.fraction(0.5).c)}, ${f(t.fraction(0.5).s)})');
}
Output
half of a quarter turn: (0.7071, 0.7071)
Turn renormalized()

Restores c^{2} + s^{2} = 1 after a long chain of products (one square root).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  var t = Turn.cayley(0.1);
  for (var i = 0; i < 100000; i++) {
    t = t * Turn.cayley(1e-3);
  }
  print('drift before: ${(t.c * t.c + t.s * t.s - 1).toStringAsExponential(2)}');
  final r = t.renormalized();
  print('after: ${(r.c * r.c + r.s * r.s - 1).abs() < 1e-15}');
}
Output
drift before: 2.21e-12
after: true

Operators

operator *operator
Turn operator *(Turn o)

This after o (complex multiplication).

Example
import 'package:riptide_math/riptide_math.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio.ray';

String f(double d) => (d.abs() < 5e-6 ? 0.0 : d).toStringAsFixed(4);
String v3(Vec3 v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';
String d3(Vec3d v) => '(${f(v.x)}, ${f(v.y)}, ${f(v.z)})';

void main() {
  final a = Turn.cayley(0.5), b = Turn.cayley(1 / 3);
  final p = a * b;
  print('(3/5, 4/5)·(4/5, 3/5) = (${f(p.c)}, ${f(p.s)}) = (0, 1)');
}
Output
(3/5, 4/5)·(4/5, 3/5) = (0.0000, 1.0000) = (0, 1)
extension

RatioVec2

extension RatioVec2 on Vec2

Ratio geometry on Vec2 (blue metric). Named …With because Vec2.cross is already the wedge.

Properties

quadrancegetter
double get quadrance

Methods

double spreadWith(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec2(3, 4), b = Vec2(4, 3);
  print('spread ${f(a.spreadWith(b))}');
}
Output
spread 0.078400
crossWithmethod
double crossWith(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec2(3, 4), b = Vec2(4, 3);
  print('cross ${f(a.crossWith(b))}');
}
Output
cross 0.921600
twistWithmethod
double twistWith(Vec2 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec2(3, 4), b = Vec2(4, 3);
  print('twist ${f(a.twistWith(b))}');
}
Output
twist 0.085069
reflectInmethod
Vec2 reflectIn(Vec2 n)

The reflection in the line through the origin perpendicular to n.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec2(3, 4), b = Vec2(4, 3);
  final r = a.reflectIn(Vec2(0, 1));
  print('reflect a in the y axis direction: (${f(r.x)}, ${f(r.y)})');
}
Output
reflect a in the y axis direction: (3.000000, -4.000000)
extension

RatioVec3

extension RatioVec3 on Vec3

Ratio geometry on Vec3 (Euclidean metric). Named …With because Vec3.cross is the vector product.

Properties

quadrancegetter
double get quadrance

Methods

double spreadWith(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  print(f(a.spreadWith(b)));
}
Output
0.500000
crossWithmethod
double crossWith(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  print(f(a.crossWith(b)));
}
Output
0.500000
twistWithmethod
double twistWith(Vec3 o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  print(f(a.twistWith(b)));
}
Output
1.000000
double solidSpreadWith(Vec3 b, Vec3 c)

The solid spread of this, b and c.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  print(f(a.solidSpreadWith(b, c)));
}
Output
0.500000
double dihedralSpreadAlong(Vec3 a, Vec3 b)

The dihedral spread between the planes span(this, a) and span(this, b), which meet along this.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  print(f(c.dihedralSpreadAlong(a, b)));
}
Output
0.500000
reflectInmethod
Vec3 reflectIn(Vec3 n)

The reflection in the plane through the origin perpendicular to n.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final a = Vec3(1, 0, 0), b = Vec3(1, 1, 0), c = Vec3(0, 0, 1);
  final r = Vec3(1, 2, 3).reflectIn(c);
  print('(${f(r.x)}, ${f(r.y)}, ${f(r.z)})');
}
Output
(1.000000, 2.000000, -3.000000)
extension

RatioVec3d

extension RatioVec3d on Vec3d

Ratio geometry on Vec3d (Euclidean metric, double storage), the twin of RatioVec3 for simulation state.

Properties

quadrancegetter
double get quadrance

Methods

double spreadWith(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(a.spreadWith(b));
}
Output
0.5
crossWithmethod
double crossWith(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(a.crossWith(b));
}
Output
0.5
twistWithmethod
double twistWith(Vec3d o)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(a.twistWith(b));
}
Output
1.0
double solidSpreadWith(Vec3d b, Vec3d c)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(a.solidSpreadWith(b, c));
}
Output
0.5
double dihedralSpreadAlong(Vec3d a, Vec3d b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(c.dihedralSpreadAlong(a, b));
}
Output
0.5
reflectInmethod
Vec3d reflectIn(Vec3d n)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  const a = Vec3d(1, 0, 0), b = Vec3d(1, 1, 0), c = Vec3d(0, 0, 1);
  print(const Vec3d(1, 2, 3).reflectIn(c));
}
Output
Vec3d(1.0, 2.0, -3.0)
abstract final class RatioQuat

Rotations without angles, as Quats: from a Gibbs (Cayley) vector, between two directions (two reflections), and the twist about an axis (swing–twist). The radians constructor Quat.axisAngle stays for platform input.

Static

fromCayleystaticmethod
static Quat fromCayley(Vec3 g)

The rotation with Gibbs vector g = \tan(\frac{\theta}{2}) \cdot \mathrm{axis}: \frac{(g, 1)}{\sqrt{1 + g \cdot g}}. One square root, no trigonometry.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final q = RatioQuat.fromCayley(Vec3(0, 0, 1)); // g = tan(θ/2)·axis: θ = 90°
  final r = q.rotate(Vec3.unitX);
  print('x -> (${f(r.x)}, ${f(r.y)}, ${f(r.z)})');
}
Output
x -> (0.000000, 1.000000, 0.000000)
cayleyRotatestaticmethod
static Vec3 cayleyRotate(Vec3 g, Vec3 v)

v rotated by the Cayley rotation with Gibbs vector g, rationally: v + k(g \times v + g \times (g \times v)), k = \frac{2}{1 + g \cdot g}. Exact for exact inputs; exact by construction.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final pts = [for (var i = -6; i <= 6; i++) RatioQuat.cayleyRotate(Vec3(0, 0, i / 3), Vec3.unitX)];
  print('g = (0, 0, 1/3): ${f(pts[7].x)}, ${f(pts[7].y)}');
}
Output from the Raya VM
g = (0, 0, 1/3): 0.800000, 0.600000
composeCayleystaticmethod
static Vec3 composeCayley(Vec3 g1, Vec3 g2)

The Gibbs vector of the rotation g_{1} after g_{2} (g_{2} applied first): \frac{g_{1} + g_{2} + g_{1} \times g_{2}}{1 - g_{1} \cdot g_{2}}. Rational; infinite when the composite is a half turn.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final g = RatioQuat.composeCayley(Vec3(0, 0, 1), Vec3(0, 0, 1));
  print('two quarter turns: g = (${f(g.x)}, ${f(g.y)}, ${f(g.z)}) (a half turn has g = ∞, so this is huge)');
  final h = RatioQuat.composeCayley(Vec3(0, 0, 0.5), Vec3(0, 0, 0.5));
  print('tan composes: ${f(h.z)} = 2·0.5/(1 − 0.25)');
}
Output
two quarter turns: g = (NaN, NaN, Infinity) (a half turn has g = ∞, so this is huge)
tan composes: 1.333333 = 2·0.5/(1 − 0.25)
betweenstaticmethod
static Quat between(Vec3 a, Vec3 b)

The shortest rotation taking the direction of a to the direction of b: the composition of the reflections in a⊥ and (a + b)⊥ (Cartan–Dieudonné), as the quaternion (a \times b, \sqrt{Q(a)Q(b)} + a \cdot b) normalised. One square root and one normalisation; no atan2, sin or cos. For opposite directions, a half turn about an axis ⊥ a.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final q = RatioQuat.between(Vec3.unitX, Vec3(1, 1, 0).normalized());
  final r = q.rotate(Vec3.unitX);
  print('x -> (${f(r.x)}, ${f(r.y)}, ${f(r.z)})');
}
Output
x -> (0.707107, 0.707107, 0.000000)
twiststaticmethod
static Quat twist(Quat q, Vec3 axis)

The twist of q about the unit axis (swing–twist decomposition): (\mathrm{axis} \cdot (v \cdot \mathrm{axis}), w) normalised. The identity when q is a pure swing (v ⊥ axis and w = 0 cannot both hold for a unit q unless it is a half-turn swing).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final q = Quat.axisAngle(Vec3(1, 1, 0).normalized(), 1.0);
  final t = RatioQuat.twist(q, Vec3.unitZ);
  print('twist about z: (${f(t.x)}, ${f(t.y)}, ${f(t.z)}, ${f(t.w)})');
}
Output
twist about z: (0.000000, 0.000000, 0.000000, 1.000000)
twistParameterstaticmethod
static double twistParameter(Quat q, Vec3 axis)

The Cayley parameter of the twist of q about the unit axis: \tan(\frac{\operatorname{twist}}{2}) = \frac{v \cdot \mathrm{axis}}{w}.

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(f(RatioQuat.twistParameter(Quat.axisAngle(Vec3.unitZ, math.pi / 2), Vec3.unitZ)));
}
Output
1.000000
abstract final class RatioQuatd

The RatioQuat operations on Quatd / Vec3d (double storage) for physics.

Static

fromCayleystaticmethod
static Quatd fromCayley(Vec3d g)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.fromCayley(const Vec3d(0, 0, 1)));
}
Output
Quatd(0.0, 0.0, 0.7071067811865475, 0.7071067811865475)
cayleyRotatestaticmethod
static Vec3d cayleyRotate(Vec3d g, Vec3d v)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.cayleyRotate(const Vec3d(0, 0, 0.5), Vec3d.unitX));
}
Output
Vec3d(0.6, 0.8, 0.0)
composeCayleystaticmethod
static Vec3d composeCayley(Vec3d g1, Vec3d g2)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.composeCayley(const Vec3d(0, 0, 0.5), const Vec3d(0, 0, 0.5)));
}
Output
Vec3d(0.0, 0.0, 1.3333333333333333)
betweenstaticmethod
static Quatd between(Vec3d a, Vec3d b)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.between(Vec3d.unitX, Vec3d.unitY).rotate(Vec3d.unitX));
}
Output
Vec3d(2.220446049250313e-16, 0.9999999999999998, 0.0)
twiststaticmethod
static Quatd twist(Quatd q, Vec3d axis)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.twist(Quatd.axisAngle(const Vec3d(1, 0, 1), 0.8), Vec3d.unitZ));
}
Output
Quatd(0.0, 0.0, 0.28643356194042396, 0.9581001067707493)
twistParameterstaticmethod
static double twistParameter(Quatd q, Vec3d axis)
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  print(RatioQuatd.twistParameter(Quatd.axisAngle(Vec3d.unitZ, 1), Vec3d.unitZ));
}
Output
0.5463024898437905
final class RatioLaws<S, V>

The laws of Rational Trigonometry as checks any RatioBackend must pass: one suite for every number system, so a new backend (a field, a precision, a GPU) is correct when this returns no failures. Inputs are random integer vectors (the same seed gives every backend the same cases). Null vectors (Q = 0, which exist in the red, green and Minkowski metrics and over finite fields) are skipped where a law divides by a quadrance.

LawCheck
\operatorname{spread} + \operatorname{cross} = 1s(a, b) + c(a, b) = 1
twistt(a, b) \cdot c(a, b) = s(a, b)
Grams(a, b) = \det \frac{G(a, b)}{Q(a)Q(b)}
Triple spread(s_{1} + s_{2} + s_{3})^{2} = 2(s_{1}^{2} + s_{2}^{2} + s_{3}^{2}) + 4s_{1}s_{2}s_{3} for three coplanar directions
Spread law\frac{s_{1}}{Q_{1}} = \frac{s_{2}}{Q_{2}} = \frac{s_{3}}{Q_{3}} in a triangle
Cross law(Q_{1} + Q_{2} - Q_{3})^{2} = 4Q_{1}Q_{2}(1 - s_{3})
Reflectionsisometries and involutions
Cayley rotationsisometries: B(Ra, Rb) = B(a, b)
Cartan–Dieudonnéevery Cayley rotation is two reflections: R = \sigma_{u} \sigma_{n}, u = Rv - v, n \perp v (and ⊥ the axis)
Solid spread (3-D)\det \frac{G(a, b, c)}{Q(a)Q(b)Q(c)}; 0 for coplanar vectors
Dihedral spread (3-D)equals the spread of the B-normals of the two planes

Constructors

RatioLawsconstructor
RatioLaws(this.b, {this.seed = 1, this.range = 9})
Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio_laws.ray';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final failures = RatioLaws(Int64Ratio([1, 0, 0, 1]), seed: 3).run(cases: 50);
  print('exact backend: ${failures.isEmpty ? 'every law holds' : failures.take(3).toList()}');
}
Output
exact backend: every law holds

Properties

bfield
final RatioBackend<S, V> b
seedfield
final int seed
rangefield
final int range

Coordinates are drawn from [−range, range].

skippedfield
int skipped

Cases abandoned in the last run because an exact backend overflowed (never a float backend).

Methods

runmethod
List<String> run({int cases = 200})

Runs cases random cases of every law; returns the failures (empty when the backend is correct).

Example
import 'package:riptide_math/riptide_math.ray';
import 'package:riptide_math/ratio.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';
import 'package:riptide_math/ratio_laws.ray';

String f(double d) => (d.abs() < 5e-9 ? 0.0 : d).toStringAsFixed(6);

void main() {
  final exact = RatioLaws(Int64Ratio([1, 0, 0, -1])).run(cases: 40);
  final float = RatioLaws(FloatRatio2(const Metric2(1, 0, -1))).run(cases: 40);
  print('red metric, int64: ${exact.length} failures; float: ${float.length} failures');
}
Output
red metric, int64: 0 failures; float: 0 failures