From d03173ecb236fc915b77e1ada6c4c781105b1e00 Mon Sep 17 00:00:00 2001 From: Keith Calise Date: Mon, 11 May 2015 22:32:02 -0400 Subject: [PATCH] Almost finished --- matrix_math.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/matrix_math.py b/matrix_math.py index 7c55a83..03002ea 100644 --- a/matrix_math.py +++ b/matrix_math.py @@ -1,2 +1,52 @@ class ShapeException(Exception): pass +m = [3, 4 ,5] +n = [5, 0, 1] +z = [9,8] +w = [0, 2, 4] +y = [10, 20, 30] + + +def shape_of(vec1): + if type(vec1[0])==int: + return (len(vec1),) + else: + return(len(vec1),len(vec1[1])) + +def shape_vectors(x): + return (len(x), ) + + +def vector_add(vec1, vec2): + if shape_of(vec1)!=shape_of(vec2): + raise ShapeException() + return [(vec1[i]+vec2[i]) for i in range(len(vec1))] + + + + +def vector_add_is_communicative(vec1, vec2): + if vector_add(vec1,vec2) == vector_add(vec2,vec1): + return True + + +def vector_sub(vec1,vec2): + if shape_of(vec1)!=shape_of(vec2): + raise ShapeException() + return [(vec1[i]-vec2[i]) for i in range(len(vec1))] + + +def vector_sum(*vectors): + vec_len = len(vectors[1]) + for i in vectors: + if len(i)!=vec_len: + raise ShapeException() + return [sum(i) for i in zip(*vectors)] + +def dot(vec1,vec2): + if shape_of(vec1)!=shape_of(vec2): + raise ShapeException() + return sum([(vec1[i]*vec2[i]) for i in range(len(vec1))]) + +def vector_multiply(vec1, scalar): + return [vec1[i] * scalar for i in range(len(vec1))]