Riemannian distance measures the shortest route between points while staying on a curved space. On a sphere, it is the great-circle or geodesic path, not a straight line through the sphere’s interior. section 11.6
For points represented as unit vectors, their distance equals the arccosine of their dot product. This turns spherical geometry into a compact calculation usable inside trainable optimization models. section 11.6
Celestial points on a sphere
Right ascension and declination locate objects on the celestial sphere. Converting these coordinates to three-dimensional unit vectors allows sphere_dist to measure angular separation along the sphere. section 11.6
Listing 11.3 Solving a function optimization problem using supervised learning
import torch
from torch import nn
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
def celestial_to_euclidean(ra, dec): #1
x = np.cos(dec)*np.cos(ra)
y = np.cos(dec)*np.sin(ra)
z = np.sin(dec)
return x, y, z
def euclidean_to_celestial(x, y, z): #2
sindec = z
cosdec = (x*x + y*y).sqrt()
sinra = y / cosdec
cosra = x / cosdec
ra = torch.atan2(sinra, cosra)
dec = torch.atan2(sindec, cosdec)
return ra, dec
def sphere_dist(x,y): #3
if x.ndim == 1:
x = x.unsqueeze(0)
if y.ndim == 1:
y = y.unsqueeze(0)
assert x.ndim == y.ndim == 2
inner = (x*y).sum(-1)
return torch.arccos(inner)
Inspect coordinate conversions and the PyTorch implementation of spherical distance. section 11.6
