Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Created August 30, 2012 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtimkovich/3532392 to your computer and use it in GitHub Desktop.
Save mtimkovich/3532392 to your computer and use it in GitHub Desktop.
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