Skip to content

Instantly share code, notes, and snippets.

@mtimkovich

mtimkovich/vector.py

Created Aug 30, 2012
Embed
What would you like to do?
I program I made for Physics class to calculate vectors and unit vector things
#!/usr/bin/env python
import math
class Vector(tuple):
def __add__(self, other):
return Vector(x + y for x, y in zip(self, other))
def __sub__(self, other):
return Vector(x - y for x, y in zip(self, other))
def __mul__(self, c):
return Vector(c * x for x in self)
def __rmul__(self, c):
return Vector(c * x for x in self)
def magnitude(self):
return math.sqrt(sum(i**2 for i in self))
def unit(self):
return Vector(map(lambda x: x / self.magnitude(), self))
v = Vector((45, -55, -40))
print v
print v.magnitude()
print v.unit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment