Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import numpy as np
- from scipy import linalg
- def vector_angles(X, Y):
- dot_product = np.sum(X * Y, axis=1)
- x_norm = linalg.norm(X, axis=1)
- y_norm = linalg.norm(Y, axis=1)
- norm = x_norm * y_norm
- norm_divide = dot_product / norm
- # Clip the dot product to ensure it's within [-1, 1]
- dot_product = np.clip(dot_product, -1.0, 1.0)
- # Handle the case where vectors are identical
- dot_product = np.where(np.isclose(norm, 0), 1.0, dot_product)
- angle_alpha = np.arccos(dot_product)
- angle_alpha_degrees = np.degrees(angle_alpha)
- return angle_alpha_degrees
- def main():
- X = np.array([[0, 0, 1], [-1, 1, 0]])
- Y = np.array([[0, 1, 0], [1, 1, 0]])
- print(vector_angles(X, Y))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement