Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 2: Defining Vectors (7 points) Define class Vector for n-dimensional ve

ID: 3725082 • Letter: Q

Question

Question 2: Defining Vectors (7 points)

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 are of type int or float.

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

v. __getitem__(i): Returns the i-th component of the vector, where components are indexed starting from 1; raises IndexError if i is less than 1 or greater than the dimension of the vector.

v. __setitem__(i, x): Sets the i-th component of vector v to x, where components are indexed starting from 1; raises IndexError if i is less than 1 or 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 of v and other; raises ValueError if other is not of type Vector or if other is of a different dimension.

v.__mul__(other): If other is of type int or float, returns a new vector resulting from the scalar multiplication of v with other, , i.e. with each component of v multiplied by scalar. If other is of type Vector, returns the dot product of v and other, which is the sum of the products of the corresponding components; raises ValueError if other is of different dimension in this case. If the type of other is none of Vector, int, float, raises AssertionError.

v.__rmul__(other): Defined exactly like v.__mul__(other)

Python uses following equivalent notations:

Question 2: Defining Vectors (7 points) Define class Vector for n-dimensional vectors as follows Vector(1) Creates a new vector with dimension len(1) from list 1 of numbers, raises TypeError if 1 is not a list or not all of its elements are of type int or float . v.dim Returns the dimension (length) of the vector .v. getitem__(i) Returns the i -th component of the vector, where components are indexed starting from 1 raises IndexError if i is less than 1 or greater than the dimension of the vector . v. setitem (i, x) Sets the i-th component of vector v to x, where components are indexed starting from 1 raises IndexError if i is less than 1 or 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 of v and other, raises ValueError if other is not of type Vector or if other is of a different dimension v. component of v multiplied by scalar. If other is of type Vector, returns the dot product of v and other, which is the sum of the products of the corresponding components, raises ValueError if other is of different dimension in this case. If the type of other is none of Vector, int, float raises AssertionError . v. rmul (other) Defined exactly like v. mul (other) mul (other): If other is of type int or float , returns a new vector resulting from the scalar multiplication of v with otherie, with each . Python uses following equivalent notations get item--(1) v. v + otherv. add (other) v * other = v.-, mul (other) other * v v·_rmul_(other) if other. mul-(v) 1s not defined

Explanation / Answer

Vector

We can define vector as vector(a,b,c).vecor returns an object to a floating point (that means 1 is converted to 1.0)Vector can perform basic operations like addition,multiplications etc.

v1 = vector(2,4,6)

v2 = vector(1,2,3)

print(v1+v2) # it displays <3 6 9>

print(3*v1) # displays <6 12 18>

We have e Vector functions like

mag(X) = X.mag = |X|, this a magnitude of a vector

mag2(Y) = Y.mag2 = |Y|*|Y|, for the vector's magnitude squared

norm(A1) = A1.norm() = A1/|A1|,this is for a unit vector in the direction of the vector

dot(A1,B1) = A1.dot(B1) = A1 dot B1, this is for the scalar dot product between two vectors

cross(A1,B1) = A1.cross(B1),it is for the vector cross product between two vectors

proj(A1,B1) = A1.proj(B1) = dot(A1,norm(B1))*norm(B1), this is for the vector projection of these vecotrs

comp(A1,B1) = A1.comp(B1) = dot(A1,norm(B1)),It is for the scalar projection of A1 along B1

This is for setitem and getitem

v__setitem__(i, x):

v.vector[i] = x

def __getitem__(i, x):

   return vector[i]

For string vectors

Str[] = ["" for i in range(size)]

Str[v] = v__str__(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])

#String operations

v__str__.len()

v__str__.lower()

v__str__.upper()

v__str__.split()

for addition

take list1 and list2 and use this command

map(add__, list1, list2)

or

[add__(i) for i in zip(list1, list2)]

Multiplication can be use this command

[a*b for a,b in zip(lista,listb)]

vector string operations

Str__[v]= Str__= ([["String1",11,12]], dtype=object)

Str__2[v]= [[" string2", 13, 14]]

Str__ = np.vstack((Str__,np.asarray(Str__2,object)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote