Define class Vector for n-dimensional vectors as follows: Vector(l) : Creates a new vector with dimension len(l) from list l of numbers; raises TypeError if l is not a list or not all of its elements...


Define classVectorfor n-dimensional vectors as follows:




  • Vector(l): Creates a new vector with dimensionlen(l)from listlof numbers; raisesTypeErroriflis not a list or not all of its elements are of typeintorfloat.


  • v.dim(): Returns the dimension (length) of the vector.


  • v. __getitem__(i): Returns thei-th component of the vector,where components are indexed starting from1
    ; raisesIndexErrorifiis less than1or greater than the dimension of the vector.


  • v. __setitem__(i, x): Sets thei-th component of vectorvtox, where components are indexed starting from1; raisesIndexErrorifiis less than1or greater than the dimension of the vector.


  • v.__str__(): Returns a string with a readable representation of the vector, see the example below.


  • v. __add__(other): Returns a new vector that is the component-wise sum ofvand other; raisesValueErrorifotheris not of typeVectoror ifotheris of a different dimension.


  • v.__mul__(other): Ifotheris of typeintorfloat, returns a new vector resulting from the scalar multiplication ofvwithother, , i.e. with each component ofvmultiplied by scalar. Ifotheris of typeVector, returns the dot product ofvandother, which is the sum of the products of the corresponding components; raisesValueErrorifotheris of different dimension in this case. If the type ofotheris none ofVector,int,float, raisesAssertionError.


  • v.__rmul__(other): Defined exactly likev.__mul__(other)


Python uses following equivalent notations:



v[i] = v.__getitem__(i) v[i] = x = v.__setitem__(i, x) str(v) = v.__str__() v + other = v.__add__(other) v * other = v.__mul__(other) other * v = v.__rmul__(other) if other.__mul__(v) is not defined

Mar 27, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here