Skip to content

Instantly share code, notes, and snippets.

@macleginn
Created May 22, 2026 09:49
Show Gist options
  • Select an option

  • Save macleginn/a11968018460f0db0242b96db164ab58 to your computer and use it in GitHub Desktop.

Select an option

Save macleginn/a11968018460f0db0242b96db164ab58 to your computer and use it in GitHub Desktop.
from math import pi
import numpy as np
x = np.array([1, 0, 0])
y = np.array([1, 0, 0.001]) # very similar to
z = np.array([0, 1, 0]) # orthogonal to x
q = np.array([-1, 0, 0]) # looks in the opposite direction to x
def angular_distance(v1, v2):
cos_of_angle = np.dot(v1, v2) / np.linalg.norm(v1) / np.linalg.norm(v2)
# We can divide by pi to make the distance range from 0 to 1
return np.acos(cos_of_angle) / pi
print(f'{angular_distance(x, y)=}')
print(f'{angular_distance(x, z)=}')
print(f'{angular_distance(x, q)=}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment