Python classes Question A2: Defining Vectors (9 points) Define class Vector for
ID: 3901335 • Letter: P
Question
Python classes
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 l 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 ï-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_(ì, x) . Sets the ?-th component of vector v to x, where components are indexed starting from 1; raises IndexError if i isless 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. mul (other): If other is of type ?nt or float , returns a new vector resulting from the scalar multiplication of v with other·ie. with each . v. 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 orderExplanation / Answer
***** Source Code *****
import math
class Vector(object):
def __init__(self, *l):
if len(l)==0: self.values = (0,0)
else: self.values = l
def inner(self, other):
return sum(a * b for a, b in zip(self, other))
def __mul__(self, other):
if type(other) == type(self):
return self.inner(other)
elif type(other) == type(1) or type(other) == type(1.0):
product = tuple( a * other for a in self )
return Vector(*product)
def __rmul__(self, other):
return self.__mul__(other)
def __add__(self, other):
added = tuple( a + b for a, b in zip(self, other) )
return Vector(*added)
def __sub__(self, other):
subbed = tuple( a - b for a, b in zip(self, other) )
return Vector(*subbed)
def dim(self):
return len(self.values)
def __getitem__(self, i):
return self.values[i]
def extend(self, newlength):
self.values.extend([0] * (newlength - self.dim()))
def __setitem__(self, i, x):
if i >= self.dim():
self.values.extend(i+1)
self.values[key] = item
def __repr__(self):
return str(self.values)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.