# Vector Add/Sub/Mult/Mag # class Vector(object): def __init__(self, x, y): self.x = x self.y = y def __str__(self): string = "(" +str(self.x) +"," +str(self.y) + ")" return string def __add__(self, vector): newx = self.x + vector.x newy = self.y + vector.y newVector = Vector(newx,newy) return newVector def __sub__(self, vector): newx = self.x - vector.x newy = self.y - vector.y newVector =Vector(newx,newy) return newVector def __mul__(self, vector): if type(vector) == int: newx =(self.x)*(vector) newy =(self.y)*(vector) newVector = Vector(newx,newy) return newVector else: newx = (self.x)*(vector.x) newy = (self.y)*(vector.y) newScalar = newx + newy return newScalar def __mag__(self): import math newx = (self.x)*(self.x) newy = (self.y)*(self.y) mag = math.sqrt(newx + newy) return mag v1 = Vector(5,4) v2 = Vector(6,7) v3 = v1+v2 #v3 = v1.__add__(v2) #Same as line above v4 = v1-v2 #v4 = v1.__sub__(v2) #Same as line above v5 = v1*5 v6 = v1*v2 #v6 = v1.__mul__(v2) #Same as line above v7 = v1.__mag__() v8 = v2.__mag__() print("This program follows the rules and correctly") print("calculates vector addition, subtraction,") print("multiplication, and magnitude of two vectors") print("="*45) print("Vector 1 =",v1) print("Vector 2 =",v2) print("="*17) print("Addition") print("="*9) print("Vector 3 = Vector 1 + Vector 2") print("Vector 3 =",v3) print("="*40) print("Subtraction") print("="*12) print("Vector 4 = Vector 1 - Vector 2") print("Vector 4 =",v4) print("="*40) print("Multiplication") print("="*15) print("Vector 5 = Vector 1 * 5") print("Vector 5 =",v5) print("Vector 6 = Vector 1 * Vector 2") print("Vector 6 =",v6) print("="*40) print("Magnitude") print("="*10) print("Vector 1 =",v7) print("Vector 2 =",v8)