SPB Git

Repository Public

CosmosMath.md 5.8 KB

# CosmosMath

Double-precision numerics shared by every module. Depends on CosmosCore only; built on simd (Mat3d = simd_double3x3, Quatd = simd_quatd, Vec3d = SIMD3<Double>). No third-party code.

# What is implemented

File API Notes
Angles.swift Angles.normalizeTwoPi, normalizePi, unit factors, separation(_:_:), separation(lon1:lat1:lon2:lat2:), positionAngle(...); Spherical.unitVector, cartesian, spherical, cartesianState, sphericalState Separation uses `atan2(
Rotations.swift Rotation.x/y/z, frame(axis:angle:), activeX/Y/Z, active(axis:angle:), rows, row, element, orthonormalityError, orthonormalized, angle(of:), quaternion(_:), matrix(_:), slerp See sign convention below.
Chebyshev.swift Chebyshev.value, valueAndDerivative(_:at:), valueAndDerivative(_:at:midpoint:radius:) Clenshaw recurrence; series Σ cₖTₖ with no halved c₀ (SPK convention).
Interpolation.swift Interpolation.lagrange, hermite, cubicHermite, linear Neville (value + derivative); Hermite by divided differences on doubled nodes.
Solvers.swift RootFinder.brent, Minimizer.brent, Minimizer.bracket, SolverResult Brent (1973) root finder and minimiser.
Quadrature.swift Quadrature.integrate, integrateToInfinity, QuadratureResult Globally adaptive Gauss–Kronrod (7, 15).
Statistics.swift CompensatedSum, Statistics.sum/mean/variance/standardDeviation/median/percentile/medianAbsoluteDeviation Neumaier summation; percentiles of type 7 (Hyndman & Fan).

# Rotation sign convention (binding for all modules)

Rotation.x(a), .y(a), .z(a) are frame (passive) rotations: they give the components of a fixed vector in axes turned by +a about the named axis. This is exactly SPICE ROTATE / [a]_i and SOFA/ERFA Rx/Ry/Rz:

text
z(ψ) = |  cos ψ   sin ψ   0 |
       | −sin ψ   cos ψ   0 |
       |    0       0     1 |

The active rotation of a vector is the transpose (Rotation.activeZ(a) == Rotation.z(-a)). simd_double3x3 is column-major; use Rotation.rows(...) to transcribe a published matrix and Rotation.element(m, row, column) to read one. Quaternions follow simd: q.act(v) is an active rotation and Rotation.quaternion(m).act(v) == m * v.

# References

  • IAU SOFA / ERFA (liberfa, BSD 3-clause): eraAnp, eraAnpm, eraSepp, eraSeps, eraPas, eraS2c, eraC2s, eraS2pv, eraPv2s, eraRx/Ry/Rz — algorithms followed and check values taken from t_erfa_c.c.
  • NAIF SPICE, Rotations Required Reading (meaning of [a]_i).
  • R. P. Brent, Algorithms for Minimization without Derivatives, Prentice-Hall, 1973 (root finder ch. 4, minimiser ch. 5).
  • R. Piessens et al., QUADPACK, Springer, 1983 (qk15 nodes and weights).
  • A. Neumaier, "Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen", ZAMM 54 (1974).
  • R. J. Hyndman & Y. Fan, "Sample quantiles in statistical packages", Am. Stat. 50 (1996).

# Validation and tolerances (tests: Packages/CosmosMath/Tests, 22 tests)

Check Reference Tolerance asserted Why
normalizeTwoPi(-0.1), normalizePi(-4) ERFA t_anp, t_anpm 1e-12 ERFA's own tolerance
separation, position angle ERFA t_seps, t_pas 1e-14, 1e-12 ERFA's own tolerances
unit vector, spherical, pv from spherical ERFA t_s2c, t_c2s, t_s2pv 1e-12 … 1e-16 ERFA's own tolerances
Rotation.x/y/z applied to a matrix ERFA t_rx, t_ry, t_rz 1e-12 ERFA's own tolerance
orthonormality of products of 3 rotations (500 random) property 4e-15 a few ulp
vector round trip Rᵀ(Rv), quaternion ↔ matrix property 8e-15 / 1e-14 a few ulp
1 µas separation analytic 1e-4 relative shows atan2 form keeps tiny angles (acos would return 0)
Chebyshev value / derivative cos(k·acos x) definition, 200 random series up to degree 13 1e-12 / 1e-9 relative derivative reference divides by sin θ
Lagrange / Hermite exact polynomials (degree 4 and 5) 1e-12 value, 1e-11 derivative rounding only
Brent root Dottie number 0.7390851332151607, Kepler equation residual 1e-15 machine precision
Brent minimum parabola, cos 1e-7 in x √ε limit of a minimiser
K15 rule ∫₀¹ xⁿ dx, n = 0…22 exact 4e-16 proves the tabulated nodes/weights (a (7,15) rule is exact to degree 22)
adaptive quadrature ∫sin, Gaussian, sharp Lorentzian, EdS distance to z = 1100 1e-11 … 1e-14 requested tolerance reached
semi-infinite ∫₀^∞ e⁻ˣ, 1/(1+x²), (1+z)^(−3/2) 1e-10, 1e-11, 1e-7 last one decays slowly (algebraic end-point behaviour after mapping)
Neumaier sum [1, 1e100, 1, −1e100] = 2; 10⁷ × 0.1 exact; 1e-9 naive sum gives 0

# Known limits

  • Minimizer.brent finds a local minimum; abscissa accuracy is bounded by √ε·|x| (≈1.5e-8 relative).
  • Quadrature targets smooth integrands. Integrable end-point singularities converge slowly and are reported with converged == false when maxPanels is reached; integrateToInfinity needs decay faster than 1/x. Refinement stops at a round-off floor of 50 ε |I| even if a tighter tolerance is requested.
  • Interpolation.lagrange on many equally spaced nodes oscillates (Runge); keep windows small (≤ ~15).
  • Percentiles do not support NaN input.
  • Not implemented (listed in ARCHITECTURE.md for this module but outside the current scope): ODE integrators. The laboratory module owns its integrators.
Files main
Menu

Theme