diff --git a/mathobjects.py b/mathobjects.py index c708a3d..ff02823 100755 --- a/mathobjects.py +++ b/mathobjects.py @@ -79,6 +79,7 @@ class Func: return Add(self, other) def __truediv__(self, other): # self / other return Div(self, other) + #TODO: change round to floor def __floordiv__(self, other): # self // other return Round(Div(self, other)) def __pow__(self, other): # self ** other @@ -125,7 +126,7 @@ class Const(Func): def copy(self): return Const - def simplify(self): + def simplify(self): #TODO: return copy of itself return self def __str__(self): @@ -145,7 +146,7 @@ class Var(Func): def copy(self): return Var - def simplify(self): + def simplify(self): #TODO: return copy of itself return self def __str__(self): @@ -202,7 +203,7 @@ class Round(Func): def simplify(self): f = self.function.simplify() if isinstance(f, Const): - return Const(self.solve({})) + return Const(self.solve({})) #TODO: make use of the solved version else: return Round(f) @@ -261,7 +262,7 @@ class Neg(Func): self.function = Const(function) else: self.function = function - + def solve(self, vars): return -self.function.solve(vars) @@ -273,10 +274,10 @@ class Neg(Func): if isinstance(f, Const): return Const(self.solve({})) else: - return Round(f) + return Neg(f) def __str__(self): - return "-" + addBrackets(self.function, 3) + ")" + return "-" + addBrackets(self.function, 3) # ================================================================= # == Sub ========================================================== @@ -341,7 +342,7 @@ class Eql(Func): return Eql def simplify(self): - if ( (isinstance(self.left,self.right.copy())) # same opperation + if ( (isinstance(self.left, self.right.copy())) # same opperation and (not isinstance(self.left, (Const, Var, Round))) # not const, var or Round and (self.left.right is self.right.right)): # subthing is the same return self.copy()(self.left.left.simplify(), self.right.left.simplify()) @@ -599,22 +600,34 @@ class Vec: ... def polar(self, *args): if len(args) == 2: - # xy angle xz angle yz angle - self.x = math.sin(args[0][0]) * math.sin(args[0][1]) - self.y = math.cos(args[0][0]) * math.sin(args[0][2]) + if not isinstance(args[0], (tuple, list)): + raise ValueError('Vec.polar(): first argument must be a tuple or list. ' + str(type(args[0])) + ' given') + if len(args[0]) < 3: + raise ValueError('Vec.polar(): first argument must be at least 3 items long') + if not isinstance(args[1], (int, float)): + raise ValueError('Vec.polar(): second argument must be a int or float. ' + str(type(args[1])) + ' given') + # xy angle xz angle yz angle + self.x = math.sin(args[0][0]) #* math.sin(args[0][1]) + self.y = math.cos(args[0][0]) # * math.sin(args[0][2]) self.z = math.cos(args[0][1]) * math.cos(args[0][2]) - self = self * l + self.mul(self, args[1]) elif len(args) == 0: return ( ( - math.tanh(self.y, self.x), - math.tanh(self.z, self.x), + math.atan2(self.y, self.x), + math.atan2(self.z, self.x), 0.0 ), self.length(), ) + else: + raise ValueError("Vec.polar(): Wrong number of arguments given. 2 or 0 suported") def rotate(self, angle): + if not isinstance(angle, (tuple, list)): + raise ValueError('Vec.rotate(): first argument must be a tuple or list') + if len(angle) < 3: + raise ValueError('Vec.rotate(): first argument must be at least 3 items long') p = self.polar() a = [p[0][0] + angle[0], p[0][1] + angle[1], p[0][2] + angle[2]] self.polar(a, p[1]) @@ -689,7 +702,7 @@ class Vec: print('WARNING: tried to add Vec with ' + str(type(other))) return other - def setitem(self, index, value): + def __setitem__(self, index, value): if(index == 0): self.x = value elif(index == 1): @@ -698,7 +711,7 @@ class Vec: self.z = value else: print('ERROR: tried to set index ' + str(index) + ' of a Vec') - def getitem(self, index): + def __getitem__(self, index): if(index == 0): return self.x elif(index == 1): @@ -706,8 +719,7 @@ class Vec: elif(index == 2): return self.z else: - print('ERROR: tried to get index ' + str(index) + ' of a Vec') - return None + raise IndexError('tried to get index ' + str(index) + ' of a Vec') def __str__(self): return "Vec" + str(self.cartesian())