programdemos/sphere-hello/sphere-hello.xtl
Sphere hello: a handful of points on the unit sphere. Two ways to measure how far apart two of them are, the straight line through the sphere and the great circle along it, order every point's neighbors the same way; and the plain average of the points is not on the sphere, where the mean along it is.
The points
P : Float
Six directions in three dimensions, chosen by hand (nothing is random), each put onto the unit sphere: one point per row.
P ← ᵐᶠs̲phereProject 6 3 r̲eshape 1.0 0.2 0.1 0.9 0.5 0.0 0.3 1.0 0.2 -0.4 0.8 0.5 0.1 0.3 1.0 -0.6 -0.5 0.6
Two distances
ᵘc̲hords : Float -> Float
The straight line through the sphere (the chord) between every pair: the length of the difference, summed one coordinate at a time.
ᵘc̲hords ← { Q → n ← f̲irst s̲hape Q (D, _) ← (2 s̲elect s̲hape Q) '{ (d, k) → c ← k s̲elect₂ Q◆ (d + (c '− t̲able c) ^ 2.0, k + 1) } p̲ower ((n c̲at n) r̲eshape 0.0, 1) s̲qrt D }
arc : Float
The great circle along the sphere between every pair: the angle, in radians (Manifold).
arc ← ᵐᶠs̲phereDistances P
The same neighbors
ᵘo̲rders : Ord a => a -> Int
Each point's neighbors, nearest first (itself first of all, at distance 0): one row per point, the points' numbers in order of that row of the distance matrix.
ᵘo̲rders ← { D → n ← f̲irst s̲hape D (R, _) ← n '{ (acc, i) → (acc c̲at (1 c̲at n) r̲eshape g̲rade i s̲elect D, i + 1) } p̲ower ((0 c̲at n) r̲eshape 0, 1) R }
Two means
plain : Float
The plain mean of the points, coordinate by coordinate: it lies inside the sphere, and its length says how far in.
plain ← ('+ r̲/ P) ÷ 6.0
onSphere : Float
The mean on the sphere (Manifold): the point along the sphere whose summed squared arcs to the points is least. It is on the sphere, and it is not the plain mean put onto it.
onSphere ← ᵐᶠs̲phereMean P