52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
# -*- coding: iso-8859-15 -*
|
|
|
|
class Vector(object):
|
|
def __init__(self, coordinates):
|
|
try:
|
|
if not coordinates:
|
|
raise ValueError
|
|
self.coordinates = tuple(coordinates)
|
|
self.dimension = len(coordinates)
|
|
|
|
except ValueError:
|
|
raise ValueError('Die Koordinaten dürfen nicht leer sein')
|
|
|
|
except TypeError:
|
|
raise TypeError('Die Koordinaten müssen iterierbar sein')
|
|
|
|
|
|
def __str__(self):
|
|
return 'Vector: {}'.format(self.coordinates)
|
|
|
|
|
|
def __eq__(self, v):
|
|
return self.coordinates == v.coordinates
|
|
|
|
def plus(self,v):
|
|
new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
|
|
return Vector(new_coordinates)
|
|
|
|
def minus(self,v):
|
|
new_coordinates = [x-y for x,y in zip(self.coordinates, v.coordinates)]
|
|
return Vector(new_coordinates)
|
|
|
|
def times_scalar(self, c):
|
|
new_coordinates = [c*x for x in self.coordinates]
|
|
return Vector(new_coordinates)
|
|
|
|
# Tests
|
|
|
|
vektor_1 = Vector([1,2,3])
|
|
print vektor_1
|
|
|
|
vektor_2 = Vector([1,2,3])
|
|
vektor_3 = Vector([-1,2,3])
|
|
|
|
print vektor_1 == vektor_2
|
|
print vektor_2 == vektor_3
|
|
|
|
print vektor_1.plus(vektor_2)
|
|
|
|
print vektor_2.minus(vektor_3)
|
|
|
|
print vektor_3.times_scalar(3) |