compute the angle between two vectors, i.e. theta = arccos( / (||v1||_2 * ||v2||_2)) Args: v1 (np.ndarray): first vector v2 (np.ndarray): second vector of same length as first vector Returns: float: angle in radians, between [0, pi]
(v1: np.ndarray, v2: np.ndarray)
| 3 | |
| 4 | |
| 5 | def angle_between(v1: np.ndarray, v2: np.ndarray) -> float: |
| 6 | """compute the angle between two vectors, i.e. |
| 7 | theta = arccos(<v1, v2> / (||v1||_2 * ||v2||_2)) |
| 8 | |
| 9 | Args: |
| 10 | v1 (np.ndarray): first vector |
| 11 | v2 (np.ndarray): second vector of same length as first vector |
| 12 | |
| 13 | Returns: |
| 14 | float: angle in radians, between [0, pi] |
| 15 | """ |
| 16 | if np.all(v1 <= 1e-8) or np.all(v2 <= 1e-8): |
| 17 | return 0.0 |
| 18 | return np.arccos(np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2) + 1e-9)) |
| 19 | |
| 20 | |
| 21 | def project(v1: np.ndarray, v2: np.ndarray) -> float: |
nothing calls this directly
no outgoing calls
no test coverage detected