Skip to content

Instantly share code, notes, and snippets.

@ojacobson
Created March 5, 2013 16:13
Show Gist options
  • Save ojacobson/5091443 to your computer and use it in GitHub Desktop.
Save ojacobson/5091443 to your computer and use it in GitHub Desktop.
>>> import math
>>> def length(v):
... return math.sqrt(sum(term * term for term in v))
...
>>> def dot(v1, v2):
... return sum(term1 * term2 for (term1, term2) in zip(v1, v2))
...
>>> def vcos(v1, v2):
... return dot(v1, v2) / (length(v1) * length(v2))
...
>>> vcos((1, 0, 0), (0, 1, 0))
0.0
>>> vcos((1, 0, 0), (0, 5, 0))
0.0
>>> vcos((50, 1, 0), (0, 5, 0))
0.01999600119960014
>>> vcos((1, 50, 0), (0, 5, 0))
0.9998000599800071
>>> vcos((1, 5000000, 2), (0, 1, 0))
0.9999999999999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment