Created
August 30, 2012 16:27
-
-
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
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
#!/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