Python Question Question A2: Defining Vectors (9 points) Define class Vector for
ID: 3903769 • Letter: P
Question
Python Question
Question A2: Defining Vectors (9 points) Define class Vector for n-dimensional vectors as follows: • Vector(1): Creates a new vector with dimension len (1) from list 1 of numbers; raises TypeError if i is not a list or not all of its elements are of type int or float. • v.dim(): Returns the dimension (length) of the vector. • v. _getitem_(i): Returns the i-th component of the vector, where components are indexed starting from 1; raises IndexError if i is less than 1 or greater than the dimension of the vector. • v. _setitem_(i, x): Sets the i -th component of vector v to x, where components are indexed starting from 1; raises IndexError if i is less than 1 or greater than the dimension of the vector. • v._repr_(): Returns the canonical string representation of the vector, see the example below. • v. add_(other) : Returns a new vector that is the component-wise sum of v and other; raises ValueError if other is not of type Vector or if other is of a different dimension. • v. _sub_(other) : Returns a new vector that is the component-wise substraction of v and other; raises ValueError if other is not of type Vector or if other is of a different dimension. v.___mul_(other) : If other is of type int or float , returns a new vector resulting from the scalar multiplication of v with other, , i.e. with each component of v multiplied by scalar. If other is of type Vector , returns the dot product of v and other, which is the sum of the products of the corresponding components; raises ValueError if other is of different dimension in this case. If the type of other is none of vector, int , float, raises AssertionError • v.__rmul_(other) : Defined exactly like v.__mul_ (other) • v._equal_(self, other) : Returns True if the values of the vector equals the values of other and are in the same order. Python uses the following equivalent notations: v[i] V[i] = x str(V) v + other v - other v * other other * v v==other = v._getitem_(i) = v._setitem_(i, x) = v._str_() = v._add_ (other) = v._sub_(other) = v._mul_(other) = v._rmul_ (other) if other._mul_ (V) is not defined = v._equal_ (other) : # Define your Vector class here raise Not ImplementedError()Explanation / Answer
class Vector:
def __init__(self,l):
self.list = l
def dim(self):
return len(self.list)
def __getitem__(self,i):
try:
a = self.list[i-1]
return a
except IndexError:
print ("Out of index error")
def __setitem__(self,i,x):
try:
self.list[i-1] = x
except IndexError:
print ("Out of index error")
def __repr__(self):
str1 = "Vector(["
for i in range(len(self.list)):
if i < len(self.list)-1
str1 = str1 + self.list[i] + ", "
else:
str1 = str1 + self.list[i] + ", "
return str1
def __add__(self,other):
try:
if self.dim() != other.dim() or type(self) == type(other):
raise ValueError
for i in range(self.dim()):
self[i] = self[i] + other[i]
except ValueError:
print("type mismatch")
def __sub__(self,other):
try:
if self.dim() != other.dim() or type(self) == type(other):
raise ValueError
for i in range(len(self.list)):
self[i] = self[i] - other[i]
except ValueError:
print("type mismatch")
def __mul__(self,other):
try:
if self.dim() != other.dim():
raise ValueError
v = 9
v1 = 9.0
if type(other) != type(v) and type(other) != type(v1) && type(other) != type(self):
raise AssertionError
else:
if type(other) != type(v) or type(other) != type(v1):
self[i] = self[i] * other
if type(self) == type(other):
prod = 1
for i in range(len(self.list)):
self[i] = prod + self[i] * other[i]
except ValueError:
print("type mismatch")
except AssertionError:
print("type mismatch")
def __equal__(self,other):
try:
if type(other) != type(self):
raise ValueError
else:
for i in range(self.dim()):
if self[i] != other[i]:
return False
return True
except AssertionError:
print("type mismatch")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.