from math import sqrt
class vector:
def __init__(self, data):
self.data = data
def __repr__(self):
return repr(self.data)
def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return vector(data)
def __sub__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] - other.data[j])
return vector(data)
def __mul__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] * other.data[j])
return vector(data)
def __getitem__(self, data):
return vector(self.data[data])
def length(self):
coll = 0
for j in range(len(self.data)):
coll += self.data[j] * self.data[j]
return sqrt(coll)
###### Examples ######
a = [1, 2, 3]
b = [5, 6, 7]
c = vector(a) + vector(b)
print c
print c[0]
print c.length()
Feb
25


