Skip to content

Instantly share code, notes, and snippets.

@theodox
Created November 27, 2014 09:09
Show Gist options
  • Save theodox/9db0fac6f613276e4ca4 to your computer and use it in GitHub Desktop.
Save theodox/9db0fac6f613276e4ca4 to your computer and use it in GitHub Desktop.
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