Advertisement
Guest User

Untitled

a guest
Mar 4th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import numpy as np
  4. from scipy import linalg
  5.  
  6. def vector_angles(X, Y):
  7.  
  8.  
  9. dot_product = np.sum(X * Y, axis=1)
  10. x_norm = linalg.norm(X, axis=1)
  11. y_norm = linalg.norm(Y, axis=1)
  12.  
  13. norm = x_norm * y_norm
  14. norm_divide = dot_product / norm
  15. # Clip the dot product to ensure it's within [-1, 1]
  16. dot_product = np.clip(dot_product, -1.0, 1.0)
  17.  
  18. # Handle the case where vectors are identical
  19. dot_product = np.where(np.isclose(norm, 0), 1.0, dot_product)
  20. angle_alpha = np.arccos(dot_product)
  21. angle_alpha_degrees = np.degrees(angle_alpha)
  22. return angle_alpha_degrees
  23.  
  24.  
  25. def main():
  26. X = np.array([[0, 0, 1], [-1, 1, 0]])
  27. Y = np.array([[0, 1, 0], [1, 1, 0]])
  28. print(vector_angles(X, Y))
  29.  
  30. if __name__ == "__main__":
  31. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement