Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions matrix_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


class Vector:
def __init__(self, vec1):
self.vec1=vec1

def shape_of(self):
return (len(self.vec1), )


def __eq__(self, other):
return self.vec1 == other.vec1


def __add__(self, other):
# if shape_of(self.vec1) != shape_of(self.vec1):
# raise ShapeException("That ain't gonna work")
return Vector([self.vec1[i]+other.vec1[i] for i in range(len(self.vec1))])


def __sub__(self,other):
return Vector([self.vec1[i]-other.vec1[i] for i in range(len(self.vec1))])

def __mul__(self,other):
return Vector([self.vec1[i] * other for i in range(len(self.vec1))])


class Matrix:
def __init__(self,mat1):
self.mat1 = mat1

32 changes: 32 additions & 0 deletions test_matrix-objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from matrix_objects import *
from nose.tools import raises

m = Vector([1,2,3])
n = Vector([4,5,6])
y = Vector([2, 3])
z = Vector([10,100])


def test_shape_of():
v3 = Vector([1])
assert m.shape_of()==(3,)
assert y.shape_of() == (2,)
assert v3.shape_of() == (1,)



def test_vector_add():


assert m + n == Vector([5,7,9])
assert y + z == Vector([12, 103])

def test_vector_sub():
assert m-n == Vector([-3,-3,-3])
assert z-y == Vector([8,97])

def test_vec_multiply():

assert m * 3 == Vector([3,6,9])
assert z * 5 == Vector([50, 500])
#assert v2.vector_add(w,y) == [10,22,34]