Use NumPy arrays in class Vec2D. The internal code in class Vec2D from Chapter 7.4 can be valid for vectors in any space dimension if we represent the vector as a NumPy array in the class instead of...


Use NumPy arrays in class Vec2D.


The internal code in class Vec2D from Chapter 7.4 can be valid for vectors in any space dimension if we represent the vector as a NumPy array in the class instead of seperate variables x and y for the vector components. Make a new class Vec where you apply NumPy functionality in the methods. The constructor should be able to treat all the following ways of initializing a vector:


a = array([1, -1, 4], float) # numpy array


v = Vec(a)


v = Vec([1, -1, 4]) # list


v = Vec((1, -1, 4)) # tuple


v = Vec(1, -1) # coordinates


We will provide some helpful advice. In the constructor, use variable number of arguments as described in Appendix G.5. All arguments are then available as a tuple, and if there is only one element in the tuple, it should be an array, list, or tuple you can send through asarray to get a NumPy array. If there are many arguments, these are coordinates, and the tuple of arguments can be transformed by array to a NumPy array. Assume in all operations that the involved vectors have equal dimension (typically that other has the same dimension as self). Recall to return Vec objects from all arithmetic operations, not NumPy arrays, because the next operation with the vector will then not take place in Vec but in NumPy. If self.v is the attribute holding the vector as a NumPy array, the addition operator will typically be implemented as


class Vec:


...


def __add__(self, other):


return Vec(selv.v + other.v)


Name of program file: Vec.py.

Nov 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here