From 3d8f4a4aedf8e0f8c2155dd8848927ef73fe54e7 Mon Sep 17 00:00:00 2001 From: Manish Patel Date: Mon, 11 May 2015 22:55:18 -0400 Subject: [PATCH] Partially working file --- matrix_math.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ test_matrix_math.py | 9 ------ 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/matrix_math.py b/matrix_math.py index 7c55a83..383374d 100644 --- a/matrix_math.py +++ b/matrix_math.py @@ -1,2 +1,72 @@ +import math + + class ShapeException(Exception): pass + + +def shape(matrix): + if isinstance(matrix[0], list): + return (len(matrix), len(matrix[1])) + elif isinstance(matrix[0], int): + return (len(matrix), ) + + +def vector_add(v1, v2): + if shape(v1) != shape(v2): + raise ShapeException + return [x + y for x, y in zip(v1, v2)] + + +def vector_sub(v1, v2): + if shape(v1) != shape(v2): + raise ShapeException + return [x - y for x, y in zip(v1, v2)] + + +def vector_sum(*args): + vec_len = len(args[1]) + for vector in args: + if len(vector) != vec_len: + raise ShapeException + return [sum(i) for i in zip(*args)] + + +def dot(v1, v2): + if shape(v1) != shape(v2): + raise ShapeException + return sum([v1[i] * v2[i] for i in range(len(v1))]) + + +def vector_multiply(vector, scalar): + return [idx * scalar for idx in vector] + + +def vector_mean(*args): + return [sum(vectors)/len(vectors) for vectors in zip(*args)] + + +def magnitude(vector): + return math.sqrt(sum([vector[i]**2 for i in range(len(vector))])) + + +def matrix_row(matrix, row): + return matrix[row] + + +def matrix_col(matrix, col): + return [i[col] for i in matrix] + + +def matrix_scalar_multiply(matrix, scalar): + pass + + +def matrix_vector_multiply(m1, v1): + if len(v1) != len(m1[0]): + raise ShapeException + + +def matrix_matrix_multiply(m1, m2): + if len(m1[0]) != len(m2): + raise ShapeException diff --git a/test_matrix_math.py b/test_matrix_math.py index a71ad36..4c63ac3 100644 --- a/test_matrix_math.py +++ b/test_matrix_math.py @@ -30,7 +30,6 @@ def test_shape_vectors(): def test_vector_add(): """ [a b] + [c d] = [a+c b+d] - Matrix + Matrix = Matrix """ assert vector_add(v, w) == [1, 5, 4] @@ -51,7 +50,6 @@ def test_vector_add_checks_shapes(): def test_vector_sub(): """ [a b] - [c d] = [a-c b-d] - Matrix + Matrix = Matrix """ assert vector_sub(v, w) == [1, 1, -4] @@ -80,7 +78,6 @@ def test_vector_sum_checks_shapes(): def test_dot(): """ dot([a b], [c d]) = a * c + b * d - dot(Vector, Vector) = Scalar """ assert dot(w, y) == 160 @@ -97,7 +94,6 @@ def test_dot_checks_shapes(): def test_vector_multiply(): """ [a b] * Z = [a*Z b*Z] - Vector * Scalar = Vector """ assert vector_multiply(v, 0.5) == [0.5, 1.5, 0] @@ -107,7 +103,6 @@ def test_vector_multiply(): def test_vector_mean(): """ mean([a b], [c d]) = [mean(a, c) mean(b, d)] - mean(Vector) = Vector """ assert vector_mean(m, n) == [4, 2] @@ -120,7 +115,6 @@ def test_vector_mean(): def test_magnitude(): """ magnitude([a b]) = sqrt(a^2 + b^2) - magnitude(Vector) = Scalar """ assert magnitude(m) == 5 @@ -181,7 +175,6 @@ def test_matrix_scalar_multiply(): """ [[a b] * Z = [[a*Z b*Z] [c d]] [c*Z d*Z]] - Matrix * Scalar = Matrix """ assert matrix_scalar_multiply(C, 3) == [[3, 6], @@ -194,7 +187,6 @@ def test_matrix_vector_multiply(): [[a b] * [x = [a*x+b*y [c d] y] c*x+d*y [e f] e*x+f*y] - Matrix * Vector = Vector """ assert matrix_vector_multiply(A, [2, 5, 4]) == [2, 5, 4] @@ -215,7 +207,6 @@ def test_matrix_matrix_multiply(): [[a b] * [[w x] = [[a*w+b*y a*x+b*z] [c d] [y z]] [c*w+d*y c*x+d*z] [e f] [e*w+f*y e*x+f*z]] - Matrix * Matrix = Matrix """ assert matrix_matrix_multiply(A, B) == A