Question: Python Programming Reflected addition examples available under (h) Problem 2. Implement in Python a class called NVector that represents an N-dimensional vector of real numbers.

Python Programming

Python Programming Reflected addition examples available under (h) Problem 2. 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. ForReflected addition examples available under (h)

Problem 2. 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 that takes as parameter sequence of numbers (e.g. a tuple or list) and initializes the elements from the NVector accordingly in a new list object. E.g. n4 NVector(13,0,1, -1]) or NVector(3,0,1, -1), the latter using a 4-tuple as parameter. Hint: 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. b) constructor with an arbitrary number of arguments that takes as parameters the elements in 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. E.g. NVector([3,0,1, -1])_ len_0 returns 4. Note: x._len_0 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]0 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 like list 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_eq_ to 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([4, 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_rmul_, for scalar multiplication with another NVector or a number and "reflected" multiplication. If B is a number, A * B- -o If B is another N Vector, A * B E.g. NVector([3,0,1, -1]) * NVector([1,2,3,4])- 3*1+0*2+1*3+(-1)*42. ien A)-1 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 testif) 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 f(vI3]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. setitem_ 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). Problem 2. 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 that takes as parameter sequence of numbers (e.g. a tuple or list) and initializes the elements from the NVector accordingly in a new list object. E.g. n4 NVector(13,0,1, -1]) or NVector(3,0,1, -1), the latter using a 4-tuple as parameter. Hint: 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. b) constructor with an arbitrary number of arguments that takes as parameters the elements in 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. E.g. NVector([3,0,1, -1])_ len_0 returns 4. Note: x._len_0 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]0 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 like list 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_eq_ to 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([4, 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_rmul_, for scalar multiplication with another NVector or a number and "reflected" multiplication. If B is a number, A * B- -o If B is another N Vector, A * B E.g. NVector([3,0,1, -1]) * NVector([1,2,3,4])- 3*1+0*2+1*3+(-1)*42. ien A)-1 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 testif) 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 f(vI3]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. setitem_ 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

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!