Week 2, Mon, 4/7#
from math import sqrt
class Vector():
dim = 2
def __init__(self, input_x, input_y):
self.x = input_x
self.y = input_y
def normalize(self):
l = self.length()
self.scale(1/l)
def length(self):
l = sqrt(self.x**2+self.y**2)
return l
def scale(self, c):
self.x = self.x * c
self.y = self.y * c
def __repr__(self):
return f'({self.x}, {self.y})'
def add(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x,y)
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x,y)
v = Vector(3,4)
print(v)
(3, 4)
v.normalize()
print(v)
(0.6000000000000001, 0.8)
Exercise from the note, implement matrix class
Determinant = ad-bc
transpose: swap b and c
Introduce random variable, PMF, PDF