"""Checks of the formulas of Section 6 of paper.tex (the latitude estimate on S^2). 1. The four forms of Q in (6.1) agree (exact, sympy). 2. d_t Theta, d_delta Theta and d_s d_t Theta = (1 - delta^2) Q^{-3/2} (exact, sympy). 3. The kernel formula of Lemma 6.2: points y on the circle = delta, generated from the definition, have height t and azimuth phi +- Theta(s,t;delta) (numerical, random samples). 4. The bound Q >= 49/128 on I^2 x [-3/4, 3/4], I = [-1/8, 1/8] (grid search; exact bound is in the paper). Numerical evidence for 3 and 4 only; the paper gives the proofs. """ import numpy as np import sympy as sp s, t, d = sp.symbols('s t delta', real=True) Q = 1 - d**2 - s**2 - t**2 + 2*d*s*t forms = [(1 - s**2)*(1 - t**2) - (d - s*t)**2, (1 - d**2)*(1 - s**2) - (t - d*s)**2, (1 - d**2)*(1 - t**2) - (s - d*t)**2] tp = d*s + sp.sqrt(1 - d**2)*sp.sqrt(1 - s**2) tm = d*s - sp.sqrt(1 - d**2)*sp.sqrt(1 - s**2) forms.append(sp.expand((tp - t)*(t - tm))) print("1. forms of Q agree:", all(sp.simplify(sp.expand(f) - Q) == 0 for f in forms)) c = (d - s*t)/(sp.sqrt(1 - s**2)*sp.sqrt(1 - t**2)) Theta = sp.acos(c) pt = sp.diff(Theta, t) pd = sp.diff(Theta, d) pst = sp.diff(Theta, s, t) rng = np.random.default_rng(1) err = 0.0 for _ in range(400): sv, tv, dv = rng.uniform(-0.3, 0.3), rng.uniform(-0.3, 0.3), rng.uniform(-0.7, 0.7) sub = {s: sv, t: tv, d: dv} Qv = float(Q.subs(sub)) e1 = abs(float(pt.subs(sub)) - (sv - dv*tv)/((1 - tv**2)*np.sqrt(Qv))) e2 = abs(float(pd.subs(sub)) + 1/np.sqrt(Qv)) e3 = abs(float(pst.subs(sub)) - (1 - dv**2)/Qv**1.5) err = max(err, e1, e2, e3) print("2. derivatives of Theta: largest error %.1e (400 random points)" % err) def x_of(sv, ph): r = np.sqrt(1 - sv**2) return np.array([r*np.cos(ph), r*np.sin(ph), sv]) err = 0.0 for _ in range(20000): sv, ph, dv, th = rng.uniform(-0.9, 0.9), rng.uniform(0, 2*np.pi), rng.uniform(-0.9, 0.9), rng.uniform(0, 2*np.pi) x = x_of(sv, ph) e1 = np.array([-sv*np.cos(ph), -sv*np.sin(ph), np.sqrt(1 - sv**2)]) e2 = np.array([-np.sin(ph), np.cos(ph), 0.0]) y = dv*x + np.sqrt(1 - dv**2)*(np.cos(th)*e1 + np.sin(th)*e2) tv = y[2] phy = np.arctan2(y[1], y[0]) Th = np.arccos(np.clip((dv - sv*tv)/(np.sqrt(1 - sv**2)*np.sqrt(1 - tv**2)), -1, 1)) sig = 1 if np.sin(th) > 0 else -1 diff = (phy - (ph + sig*Th) + np.pi) % (2*np.pi) - np.pi if abs(np.sin(th)) > 1e-3: err = max(err, abs(diff), abs(np.dot(x, y) - dv)) print("3. kernel formula (azimuth phi + sigma Theta, sign of sin theta): largest error %.1e (20000 samples)" % err) g = np.linspace(-1/8, 1/8, 81) dd = np.linspace(-0.75, 0.75, 121) S, T, D = np.meshgrid(g, g, dd, indexing='ij') Qg = 1 - D**2 - S**2 - T**2 + 2*D*S*T print("4. min Q on the grid: %.5f (bound in the paper 49/128 = %.5f); max d_s d_t Theta = %.3f (< 8)" % (Qg.min(), 49/128, ((1 - D**2)/Qg**1.5).max()))