Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 2: Defining Vectors (7 points) Define class Vector for n-dimensional vectors as follows: (Actual python code needed) Question 2: Defining Vectors (7 points) Define
Question 2: Defining Vectors (7 points) Define class Vector for n-dimensional vectors as follows: (Actual python code needed)
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 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 definedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started