Created
November 27, 2014 09:09
-
-
Save theodox/9db0fac6f613276e4ca4 to your computer and use it in GitHub Desktop.
This file contains 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
def add(vect, other): | |
return tuple(a + b for a, b in zip(vect, other)) | |
def sub(vect, other): | |
return tuple(a - b for a, b in zip(vect, other)) | |
def mul(vect, other): | |
if hasattr(other, '__iter__'): | |
return tuple(a * b for a, b in zip(vect, other)) | |
return tuple(a * b for a, b in zip(vect, [other] * len(vect))) | |
def div(vect, other): | |
if hasattr(other, '__iter__'): | |
return tuple(a / b for a, b in zip(vect, other)) | |
return tuple(a / b for a, b in zip(vect, [other] * len(vect))) | |
def length(vect): | |
total = sum(map(lambda a: operator.pow(a, 2), vect)) | |
divisor = math.sqrt(total) | |
return divisor | |
def normalized(vect): | |
divisor = [length(vect)] * len(vect) | |
return div(vect, divisor) | |
def dot(left, right): | |
return sum(mul(left, right)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment