Question: R-2.12Implement the mul method for the Vector class of Section 2.3.3, so that the expression v 3 returns a new vector with coordinates that are

R-2.12Implement the mul method for the Vector class of Section 2.3.3, so that the expression v 3 returns a new vector with coordinates that are 3 times the respective coordinates of v.

R-2.13 Exercise R-2.12 asks for an implementation of mul , for the Vector class of Section 2.3.3, to provide support for the syntax v 3. Implement the rmul method, to provide additional support for syntax 3 v.

by python program

this is section 2.3.3

class Vector:

2 Represent a vector in a multidimensional space.

3

4 def init (self, d):

5 Create d-dimensional vector of zeros.

6 self. coords = [0] d

7

8 def len (self):

9 Return the dimension of the vector.

10 return len(self. coords)

11

12 def getitem (self, j):

13 Return jth coordinate of vector.

14 return self. coords[j]

15

16 def setitem (self, j, val):

17 Set jth coordinate of vector to given value.

18 self. coords[j] = val

19

20 def add (self, other):

21 Return sum of two vectors.

22 if len(self) != len(other): # relies on len method

23 raise ValueError( dimensions must agree )

24 result = Vector(len(self)) # start with vector of zeros

25 for j in range(len(self)):

26 result[j] = self[j] + other[j]

27 return result

28

29 def eq (self, other):

30 Return True if vector has same coordinates as other.

31 return self. coords == other. coords

32

33 def ne (self, other):

34 Return True if vector differs from other.

35 return not self == other # rely on existing eq definition

36

37 def str (self):

38 Produce string representation of vector.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!