Question: Python Programming Implement in Python a class called NVector that represents an N-dimensional vector of real numbers The vector elements must be stored in a
Python Programming



Implement in Python a class called NVector that represents an N-dimensional vector of real numbers The vector elements must be stored in a list object. For example NVector(3,0,1, -1) represents the 4- dimensional vector [ 3,0,1,-1] and its elements are stored in a standard Python list [3,0,1,-1]. The vector class must have the following methods: a) constructor. If it has only one argument (besides self) assume it is a sequence of numbers (e.g. a tuple or list) and initialize the elements from the NVector accordingly, in a new list object. E.g. n4 NVector([3,0,1,-1]) or NVector(3,0,1, -1), the latter using a 4-tuple as parameter Hints: Look up functions with arbitrary (or variable) number of arguments in the lecture notes or textbook. Do not simply save the actual parameter object as the new object's state. It is very, very wrong to share the sequence object between the actual parameter and the object. That would enable side effects, and that is error-prone. In this case the call is list(param). If param is a scalar (not a sequence, like a list), the list0 constructor call will raise TypeError. b) the constructor can also take two or more arguments (besides self which will be the elements of the vector. E.g. NVector(3,0,1, -1) creates a vector with length 4 (i.e. 4 dimensional) and elements 3,0,1, and -1 c) method-len-that returns the length of the vector. .g. Nector(3,0,1,-1).-len-0 returns 4. Note: x. len is actually invoked when we call len(x). c) method_len that returns the length of the vector. E.g. NVector([3,0,1, -1]) len_0 returns 4 Note: x.__len0 is actually invoked when we call len(x). d) the "index operator" [i], with method_getitem (index). The argument is an index (an int) into the objects element list. E.g. if x=: NVector(3,0,1,-1]), then x[1]:-o Indexing with negative numbers should work like list indexing with standard lists: E.g. if x--NVector([3,0,1, -1]), then x[-2]-1 Note that it is not required to implement list-style slicing. e) the indexed assignment operator [], with method setitem_ (index, value), that will assign value to the element at position index. E.g. if x= NVector(3,0,1,-1). The call x[2] = 5 will modify the vector to be the same as NVector([3,0,5,-1]. Indexing with negative numbers should work ike indexing with standard lists. f) _str_ _method that returns the string representation of the NVector object. Pick an obvious format. g) methods_eq and_ne_that take a parameter and return true if self is equal (respectively, not equal) to the parameter object, and false otherwise. For_eqto return true, self must be compared to another NVector object and corresponding elements with the same index should be equal. h) method_add for addition with another NVector (done element-wise or with a number (applied to all elements), and method_radd implementing "reflected" addition, as described in the book. E.g. NVector([3,0,1, -1+NVector 1,2,3,4]) results in NVector(l4, 2, 4, 3]) NVector([3,0,1,-1)10 results in NVector([13, 10, 11,9]) 10 +NVector([3,0,1, -1]) results in NVector([13, 10, 11,9]) i) methods_mul_and_rmulfor scalar multiplication with another NVector or a number and "reflected" multiplication. If B is a number, A B 4-5 If B is another NVector, A * B = E.g. NVector([3,0,1,-1])* NVector([1,2,3,4]3"1+0 2+1-3+(-1)*42 j) method zeros(n) that returns a new NVector object with dimension n with all elements 0. k) Write a main function that uses the testif0 function from Homework 3 (also posted on the homework 4's page) to test the code from parts a) -i For example, if we want to test setitem we can do this: v = NVector(1,2,3,4) v[3] 10 testif(v[3]10, "setitem works", "setitem failed" Implementation requirements: Follow these requirements to get full credit. The methods should throw standard Python exceptions, as necessary. E.g. settem-with an invalid index should throw IndexError. In most cases, exceptions thrown by the standard list operations are acceptable. It is your job to think about error cases and raise the proper exception if necessary Write a docstring for each method that describes the method's contract (preconditions, postconditions, as needed)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
