SPB Git

Repository Public

CosmosMath.md 5.8 KB 81 lines markdown

1<!--2ORIX Cosmos3File: Documentation/modules/CosmosMath.md4Description: What CosmosMath implements, the conventions it fixes (rotation signs), references, tolerances measured in the tests, and known limits.5Author: Simon-Pierre Boucher6Contact: contact@spboucher.ai7-->8# CosmosMath910Double-precision numerics shared by every module. Depends on `CosmosCore` only; built on `simd`11(`Mat3d = simd_double3x3`, `Quatd = simd_quatd`, `Vec3d = SIMD3<Double>`). No third-party code.1213## What is implemented1415| File | API | Notes |16|---|---|---|17| `Angles.swift` | `Angles.normalizeTwoPi`, `normalizePi`, unit factors, `separation(_:_:)`, `separation(lon1:lat1:lon2:lat2:)`, `positionAngle(...)`; `Spherical.unitVector`, `cartesian`, `spherical`, `cartesianState`, `sphericalState` | Separation uses `atan2(|a×b|, a·b)` (stable near 0 and π). Position angle is north through east. |18| `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. |19| `Chebyshev.swift` | `Chebyshev.value`, `valueAndDerivative(_:at:)`, `valueAndDerivative(_:at:midpoint:radius:)` | Clenshaw recurrence; series Σ cₖTₖ with no halved c₀ (SPK convention). |20| `Interpolation.swift` | `Interpolation.lagrange`, `hermite`, `cubicHermite`, `linear` | Neville (value + derivative); Hermite by divided differences on doubled nodes. |21| `Solvers.swift` | `RootFinder.brent`, `Minimizer.brent`, `Minimizer.bracket`, `SolverResult` | Brent (1973) root finder and minimiser. |22| `Quadrature.swift` | `Quadrature.integrate`, `integrateToInfinity`, `QuadratureResult` | Globally adaptive Gauss–Kronrod (7, 15). |23| `Statistics.swift` | `CompensatedSum`, `Statistics.sum/mean/variance/standardDeviation/median/percentile/medianAbsoluteDeviation` | Neumaier summation; percentiles of type 7 (Hyndman & Fan). |2425### Rotation sign convention (binding for all modules)2627`Rotation.x(a)`, `.y(a)`, `.z(a)` are **frame (passive) rotations**: they give the components of a fixed vector28in axes turned by `+a` about the named axis. This is exactly SPICE `ROTATE` / `[a]_i` and SOFA/ERFA `Rx/Ry/Rz`:2930```31z(ψ) = |  cos ψ   sin ψ   0 |32       | −sin ψ   cos ψ   0 |33       |    0       0     1 |34```3536The active rotation of a vector is the transpose (`Rotation.activeZ(a) == Rotation.z(-a)`).37`simd_double3x3` is column-major; use `Rotation.rows(...)` to transcribe a published matrix and38`Rotation.element(m, row, column)` to read one. Quaternions follow simd: `q.act(v)` is an active rotation and39`Rotation.quaternion(m).act(v) == m * v`.4041## References4243- IAU SOFA / ERFA (liberfa, BSD 3-clause): `eraAnp`, `eraAnpm`, `eraSepp`, `eraSeps`, `eraPas`, `eraS2c`, `eraC2s`,44  `eraS2pv`, `eraPv2s`, `eraRx/Ry/Rz` — algorithms followed and check values taken from `t_erfa_c.c`.45- NAIF SPICE, *Rotations Required Reading* (meaning of `[a]_i`).46- R. P. Brent, *Algorithms for Minimization without Derivatives*, Prentice-Hall, 1973 (root finder ch. 4, minimiser ch. 5).47- R. Piessens et al., *QUADPACK*, Springer, 1983 (`qk15` nodes and weights).48- A. Neumaier, "Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen", ZAMM 54 (1974).49- R. J. Hyndman & Y. Fan, "Sample quantiles in statistical packages", Am. Stat. 50 (1996).5051## Validation and tolerances (tests: `Packages/CosmosMath/Tests`, 22 tests)5253| Check | Reference | Tolerance asserted | Why |54|---|---|---|---|55| `normalizeTwoPi(-0.1)`, `normalizePi(-4)` | ERFA `t_anp`, `t_anpm` | 1e-12 | ERFA's own tolerance |56| separation, position angle | ERFA `t_seps`, `t_pas` | 1e-14, 1e-12 | ERFA's own tolerances |57| unit vector, spherical, pv from spherical | ERFA `t_s2c`, `t_c2s`, `t_s2pv` | 1e-12 … 1e-16 | ERFA's own tolerances |58| `Rotation.x/y/z` applied to a matrix | ERFA `t_rx`, `t_ry`, `t_rz` | 1e-12 | ERFA's own tolerance |59| orthonormality of products of 3 rotations (500 random) | property | 4e-15 | a few ulp |60| vector round trip `Rᵀ(Rv)`, quaternion ↔ matrix | property | 8e-15 / 1e-14 | a few ulp |61| 1 µas separation | analytic | 1e-4 relative | shows `atan2` form keeps tiny angles (acos would return 0) |62| 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 θ |63| Lagrange / Hermite | exact polynomials (degree 4 and 5) | 1e-12 value, 1e-11 derivative | rounding only |64| Brent root | Dottie number 0.7390851332151607, Kepler equation residual | 1e-15 | machine precision |65| Brent minimum | parabola, cos | 1e-7 in x | √ε limit of a minimiser |66| K15 rule | ∫₀¹ xⁿ dx, n = 0…22 exact | 4e-16 | proves the tabulated nodes/weights (a (7,15) rule is exact to degree 22) |67| adaptive quadrature | ∫sin, Gaussian, sharp Lorentzian, EdS distance to z = 1100 | 1e-11 … 1e-14 | requested tolerance reached |68| 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) |69| Neumaier sum | `[1, 1e100, 1, −1e100]` = 2; 10⁷ × 0.1 | exact; 1e-9 | naive sum gives 0 |7071## Known limits7273- `Minimizer.brent` finds a local minimum; abscissa accuracy is bounded by √ε·|x| (≈1.5e-8 relative).74- `Quadrature` targets smooth integrands. Integrable end-point singularities converge slowly and are reported with75  `converged == false` when `maxPanels` is reached; `integrateToInfinity` needs decay faster than 1/x.76  Refinement stops at a round-off floor of 50 ε |I| even if a tighter tolerance is requested.77- `Interpolation.lagrange` on many equally spaced nodes oscillates (Runge); keep windows small (≤ ~15).78- Percentiles do not support NaN input.79- Not implemented (listed in ARCHITECTURE.md for this module but outside the current scope): ODE integrators.80  The laboratory module owns its integrators.81
Files main
Menu

Theme