Created
May 22, 2026 09:49
-
-
Save macleginn/a11968018460f0db0242b96db164ab58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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