Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Question Question A2: Defining Vectors (9 points) Define class Vector for n-dimensional vectors as follows: Vector(1): Creates a new vector with dimension len (1)

Python Question

image text in transcribedimage text in transcribed

Question A2: Defining Vectors (9 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 i 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._repr_(): Returns the canonical string 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. _sub_(other) : Returns a new vector that is the component-wise substraction 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) v._equal_(self, other) : Returns True if the values of the vector equals the values of other and are in the same order. Python uses the following equivalent notations: v[i] V[i] = x str(V) v + other v - other v * other other * v v==other = v._getitem_(i) = v._setitem_(i, x) = v._str_() = v._add_ (other) = v._sub_(other) = v._mul_(other) = v._rmul_ (other) if other._mul_ (V) is not defined = v._equal_ (other) : # Define your Vector class here raise Not ImplementedError()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago