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

in Python http://www.cse.msu.edu/~cse231/Online/Labs/Lab14/Lab14.pdf Assignment

ID: 3820231 • Letter: I

Question

in Python

http://www.cse.msu.edu/~cse231/Online/Labs/Lab14/Lab14.pdf

Assignment overview We are going to work with overloaded operators and making our own class. We are going to make a 2D vector class. Some Background So if you don't remember, here is a little background on two-dimensional vectors. A vector is basically an arrow that has a magnitude (a length) and a direction (an angle with respect to typically the x axis It usually is represented as an x,y pair, where the origin of the vector is a 0,0 and the head of the vector is at the listed pair. x,y 0.0 A vector V*2 V W V-W Here are some of the operations you can perform on a vector. vector addition. If V1 is (x,y) and V2 is (a,b), the V+W is (xtra, y+b, a vector vector multiplication by a scalar. if vi is (x,y), the V n is (x n,y n), a vector vector subtraction V-W is the same as V+(W -1), a vector

Explanation / Answer

I have written down python class for vector.

Each line of code is self explanatory as well as accompanied by explanatory comments. Also copy paste the code into some python beautifier before you paste it into python editor. There may be a case where "indent error" is raised. So beautify first.

To use it as a module:

Happy Coding.

import math
class Vector(object):
  
#constructor to initialize i and j cap components
def __init__(self,x=0.0,y=0.0):

self.i=float(x)
self.j=float(y)

#str method rounding of string upto two decimal places
def __str__(self):

return str('%.2f' % round(self.i,2))+' i + '+str('%.2f' % round(self.j,2))+' j'

#repr method to display in the shell
def __repr__(self):

return str('%.2f' % round(self.i,2))+' i + '+str('%.2f' % round(self.j,2))+' j'

#add method for python
def __add__(self,v):

return Vector(self.i+v.i,self.j+v.j)

#subtract method
def __sub__(self,v):

return Vector(self.i-v.i,self.j-v.j)

#scalar multiply method vector*float
def __mul__(self,a):

  
if type(a)== type(self) : #using introspection : if type is vector then do dot product
return float(self.i*a.i+self.j*a.j)

else: #scalar multiplication

a=float(a) #change a to float if it is integer

return Vector(self.i*a,self.j*a)

#scalar multiply method float*vector
def __rmul__(self,a):

if type(a)== type(self) : #using introspection : if type is vector then do dot product
return float(self.i*a.i+self.j*a.j)

else: #scalar multiplication

a=float(a) #change a to float if it is integer

return Vector(self.i*a,self.j*a)
  
  
#vector equal method
def __eq__(self,v):

if self.i==v.i and self.j==v.j:
return True

return False

#magnitude of a vector
def magnitude(self):

mag_sq= self.i*self.i+self.j*self.j #square of the magnitude

return math.sqrt(mag_sq)

#scales the vector to the unit vector
def unit(self):

if self.i==float(0) and self.j==float(0): #test if unit vector

raise ValueError('cannot convert zero vector to a unit vector') #raise exception

mag=float(self.magnitude()) #get magnitude
  
inv_mag=1/mag #inverse of magnitude

self.i=self.i*inv_mag #scale the x component
self.j=self.j*inv_mag #scale the y component