Created
March 5, 2013 16:13
-
-
Save ojacobson/5091443 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
>>> 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