I'm using linear algebra as my basis. One very specific change I'd like to make is to allow for equality tests between points and vectors with an unequal number of components by assuming that all undefined components are zero (all points and vectors have infinite components; equality tests stop testing once a check for both components return 'undefined'). Mostly I'd like to do this just because it seems kind of cool to do, so it is not remarkably important.
But in essence, I really just want someone to rip this code apart and tell me why I'm doing everything wrong (or tell me that I'm doing things pretty alright). The goal is just to have a nice, sane Point
and Vector
system that is very flexible and can be understood by anyone--and used by anyone--for any purpose they'd like. Everything I'm building will rely on the following code, so I want to make sure that my design patterns are reasonable and flexible.
import math, sys, os
TOLERANCE = 0.00001
class SizeError(Exception):
def __init__(self, *args):
self.args = args
print "Size Error between " + str(args)
class P(object):
def __init__(self, *args):
if len(args) == 1:
try:
self.components = tuple(args[0])
self.size = len(args[0])
except TypeError:
self.components = tuple(args)
self.size = len(args)
else:
self.components = args
self.size = len(args)
def __getitem__(self, key):
return self.components[key]
def __repr__(self):
result = ""
for i in range(0, self.size):
result += str(self[i]) + ", "
return "<Point (" + str(self.size) + ") : " + result[:-2] + ">"
def __eq__(self, other):
if self.size != other.size:
raise SizeError(self, other)
for i in range(0, self.size):
result = self[i] - other[i]
if math.fabs(result) > TOLERANCE:
return False
return True
def __add__(self, other):
if other.size != self.size:
raise SizeError(self, other)
result = []
for i in range(0, self.size):
result.append(self[i] + other[i])
return P(result)
def __sub__(self, other):
if other.size != self.size:
raise SizeError(self, other)
result = []
for i in range(0, self.size):
result.append(self[i] - other[i])
return V(result)
class V(P):
def __repr__(self):
result = ""
for i in range(0, self.size):
result += str(self[i]) + ", "
return "<Vector (" + str(self.size) + ") : " + result[:-2] + ">"
def __add__(self, other):
if other.size != self.size:
raise SizeError(self, other)
result = []
for i in range(0, self.size):
result.append(self[i] + other[i])
return V(result)
def __mul__(self, scalar):
result = []
for i in range(0, self.size):
result.append(self[i] * scalar)
return V(result)
def __neg__(self):
return self * -1
def dotProduct(self, other):
result = 0
if self.size != other.size:
raise SizeError(self, other)
for i in range(0, self.size):
result += self[i] * other[i]
return result
def normalize(self):
result = []
length = self.getLength()
if math.fabs(length) < TOLERANCE: return self.getZero()
for i in range(0, self.size):
result.append(self[i] / length)
return V(result)
def getLength(self):
result = 0
for i in range(0, self.size):
result += self[i]**2
return math.sqrt(result)
def getZero(self):
return self * 0
def isZero(self):
zero = self.getZero()
return self == zero