From 458c83502414bbbeb685bd04fcc5981e60150386 Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Thu, 14 May 2015 06:00:50 -0400 Subject: [PATCH 1/6] Committing what I have of HW7 --- README.txt | 6 ++ matrix_math.py | 37 +++++++ test_matrix_math.py | 244 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 287 insertions(+) create mode 100644 README.txt create mode 100644 matrix_math.py create mode 100644 test_matrix_math.py diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..10dbd80 --- /dev/null +++ b/README.txt @@ -0,0 +1,6 @@ +#Hello again! Still not finished, not very close either. I'm starting to get the +#concepts though. My moment of triumph today was going back and actually under- +#standing the code that we did in class on Monday (The game of Pig). In addition, +#I'm digging the math related content, even though I haven't gotten to that +#portion of the assignment. More to come this morning; I believe I'll get a good +#bit more done on this before class. See you there! diff --git a/matrix_math.py b/matrix_math.py new file mode 100644 index 0000000..74d409d --- /dev/null +++ b/matrix_math.py @@ -0,0 +1,37 @@ + + + +class Vector: + def __init__(self, vector): + self.vector = vector + + + def shape(self): + try: + return (len(self.vector), len(self.vector[0])) + except TypeError: + return (len(self.vector),) + + def vector_add(self): + if shape(self.vector1) != shape(vector2): + raise ShapeException() + return [vector1[i] + vector2[i] for i in range(len(a))] + + def vector_sub(self): + if shape(a) != shape(b): + raise ShapeException() + return [a[i] - b[i] for i in range(len(a))] + + def vector_mult_by_scaler + + + + + + + + + +class Matrix: + def __init__(self, matrix): + self.matrix = matrix diff --git a/test_matrix_math.py b/test_matrix_math.py new file mode 100644 index 0000000..018ef2b --- /dev/null +++ b/test_matrix_math.py @@ -0,0 +1,244 @@ + +from matrix_math import * +from nose.tools import raises + + +def is_equal(x, y, tolerance=0.001): + """Helper function to compare floats, which are often not quite equal + even when they should be.""" + return abs(x - y) <= tolerance + + + + + + + + + + +m = Vector([3, 4]) +n = Vector([5,0]) + +v = Vector([1,3,0]) +w = Vector([0,2,4]) +u = Vector([1,1,1]) +y = Vector([10,20,30]) +z = Vector([0,0,0]) + + +def test_shape_vectors(): + """shape should take a vector or matrix and return a tuple with the + number of rows (for a vector) or the number of rows and columns + (for a matrix.)""" + assert m.shape() == (2,) + assert v.shape() == (3,) + assert shape([1]) == (1,) + + +def test_vector_add(): + """ + [a b] + [c d] = [a+c b+d] + + Matrix + Matrix = Matrix + """ + assert vector_add(v, w) == [1, 5, 4] + assert vector_add(u, y) == [11, 21, 31] + assert vector_add(u, z) == u + + +def test_vector_add_is_communicative(): + assert vector_add(w, y) == vector_add(y, w) + + +@raises(ShapeException) +def test_vector_add_checks_shapes(): + """Shape rule: the vectors must be the same size.""" + vector_add(m, v) + + +def test_vector_sub(): + """ + [a b] - [c d] = [a-c b-d] + + Matrix + Matrix = Matrix + """ + assert vector_sub(v, w) == [1, 1, -4] + assert vector_sub(w, v) == [-1, -1, 4] + assert vector_sub(y, z) == y + assert vector_sub(w, u) == vector_sub(z, vector_sub(u, w)) + + +@raises(ShapeException) +def test_vector_sub_checks_shapes(): + """Shape rule: the vectors must be the same size.""" + vector_sub(m, v) + + +def test_vector_sum(): + """vector_sum can take any number of vectors and add them together.""" + assert vector_sum(v, w, u, y, z) == [12, 26, 35] + + +@raises(ShapeException) +def test_vector_sum_checks_shapes(): + """Shape rule: the vectors must be the same size.""" + vector_sum(v, w, m, y) + + +def test_dot(): + """ + dot([a b], [c d]) = a * c + b * d + + dot(Vector, Vector) = Scalar + """ + assert dot(w, y) == 160 + assert dot(m, n) == 15 + assert dot(u, z) == 0 + + +@raises(ShapeException) +def test_dot_checks_shapes(): + """Shape rule: the vectors must be the same size.""" + dot(v, m) + + +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] + assert vector_multiply(m, 2) == [6, 8] + + +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] + assert vector_mean(v, w) == [0.5, 2.5, 2] + assert is_equal(vector_mean(v, w, u)[0], 2 / 3) + assert is_equal(vector_mean(v, w, u)[1], 2) + assert is_equal(vector_mean(v, w, u)[2], 5 / 3) + + +def test_magnitude(): + """ + magnitude([a b]) = sqrt(a^2 + b^2) + + magnitude(Vector) = Scalar + """ + assert magnitude(m) == 5 + assert magnitude(v) == math.sqrt(10) + assert magnitude(y) == math.sqrt(1400) + assert magnitude(z) == 0 + + +A = Matrix([[1, 0, 0], + [0, 1, 0], + [0, 0, 1]]) +B = Matrix([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]]) +C = [[1, 2], + [2, 1], + [1, 2]] +D = [[1, 2, 3], + [3, 2, 1]] + + +def test_shape_matrices(): + """shape should take a vector or matrix and return a tuple with the + number of rows (for a vector) or the number of rows and columns + (for a matrix.)""" + assert shape(A) == (3, 3) + assert shape(C) == (3, 2) + assert shape(D) == (2, 3) + + +def test_matrix_row(): + """ + 0 1 <- rows + 0 [[a b]] + 1 [[c d]] + ^ + columns + """ + assert matrix_row(A, 0) == [1, 0, 0] + assert matrix_row(B, 1) == [4, 5, 6] + assert matrix_row(C, 2) == [1, 2] + + +def test_matrix_col(): + """ + 0 1 <- rows + 0 [[a b]] + 1 [[c d]] + ^ + columns + """ + assert matrix_col(A, 0) == [1, 0, 0] + assert matrix_col(B, 1) == [2, 5, 8] + assert matrix_col(D, 2) == [3, 1] + + +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], + [6, 3], + [3, 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] + assert matrix_vector_multiply(B, [1, 2, 3]) == [14, 32, 50] + assert matrix_vector_multiply(C, [3, 4]) == [11, 10, 11] + assert matrix_vector_multiply(D, [0, 1, 2]) == [8, 4] + + +@raises(ShapeException) +def test_matrix_vector_multiply_checks_shapes(): + """Shape Rule: The number of rows of the vector must equal the number of + columns of the matrix.""" + matrix_vector_multiply(C, [1, 2, 3]) + + +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 + assert matrix_matrix_multiply(B, C) == [[8, 10], + [20, 25], + [32, 40]] + assert matrix_matrix_multiply(C, D) == [[7, 6, 5], + [5, 6, 7], + [7, 6, 5]] + assert matrix_matrix_multiply(D, C) == [[8, 10], [8, 10]] + + +@raises(ShapeException) +def test_matrix_matrix_multiply_checks_shapes(): + """Shape Rule: The number of columns of the first matrix must equal the + number of rows of the second matrix.""" + matrix_matrix_multiply(A, D) From 784f27e769716ce7b326105d3d35a7abb0dcdb6b Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Thu, 14 May 2015 06:28:51 -0400 Subject: [PATCH 2/6] vector side --- matrix_math.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/matrix_math.py b/matrix_math.py index 74d409d..c6117c7 100644 --- a/matrix_math.py +++ b/matrix_math.py @@ -1,4 +1,6 @@ +class ShapeException(Exception): + pass class Vector: @@ -6,26 +8,38 @@ def __init__(self, vector): self.vector = vector - def shape(self): + + def shape(self, other): try: return (len(self.vector), len(self.vector[0])) except TypeError: return (len(self.vector),) - def vector_add(self): - if shape(self.vector1) != shape(vector2): + + def vector_add(self, other): + if shape(self.vector) != shape(other.vector): raise ShapeException() - return [vector1[i] + vector2[i] for i in range(len(a))] + return [self.vector[i] + other.vector[i] for i in range(len(self.vector))] - def vector_sub(self): - if shape(a) != shape(b): + + def vector_sub(self, other): + if shape(self.vector) != shape(self.other): raise ShapeException() - return [a[i] - b[i] for i in range(len(a))] + return [self.vector[i] - other.vector[i] for i in range(len(self.vector))] - def vector_mult_by_scaler + def vector_mult_by_scaler(self, scaler): + return [self.vector[i] * scalar for i in range(len(self.vector))] + def vector_dot(self, other): + if shape(self.vector) != shape(self.other): + raise ShapeException() + return [self.vector[i] * other.vector[i] for i in range(len(self.vector))] + + + def vector_mag(self): + return math.sqrt(sum(self.vector[i]**2 for i in range(len(self.vector))])) @@ -35,3 +49,5 @@ def vector_mult_by_scaler class Matrix: def __init__(self, matrix): self.matrix = matrix + + def sha From 30b87ca97f3deeea393919277c848136f58a24dc Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Thu, 14 May 2015 07:30:48 -0400 Subject: [PATCH 3/6] passed first test --- matrix_math.py | 36 ++-- test_matrix_math.py | 396 ++++++++++++++++++++++---------------------- 2 files changed, 222 insertions(+), 210 deletions(-) diff --git a/matrix_math.py b/matrix_math.py index c6117c7..ff1c565 100644 --- a/matrix_math.py +++ b/matrix_math.py @@ -9,11 +9,11 @@ def __init__(self, vector): - def shape(self, other): + def shape(self): try: - return (len(self.vector), len(self.vector[0])) - except TypeError: - return (len(self.vector),) + return (len(self.vector), len(self.vector[0])) + except TypeError: + return (len(self.vector),) def vector_add(self, other): @@ -23,7 +23,7 @@ def vector_add(self, other): def vector_sub(self, other): - if shape(self.vector) != shape(self.other): + if Vshape(self.vector) != shape(self.other): raise ShapeException() return [self.vector[i] - other.vector[i] for i in range(len(self.vector))] @@ -33,21 +33,33 @@ def vector_mult_by_scaler(self, scaler): def vector_dot(self, other): - if shape(self.vector) != shape(self.other): + if shape(self.vector) != shape(other.vector): raise ShapeException() return [self.vector[i] * other.vector[i] for i in range(len(self.vector))] def vector_mag(self): - return math.sqrt(sum(self.vector[i]**2 for i in range(len(self.vector))])) - + return math.sqrt(sum(self.vector[i]**2 for i in range(len(self.vector)))) -class Matrix: - def __init__(self, matrix): - self.matrix = matrix - def sha +# class Matrix: +# def __init__(self, matrix): +# self.matrix = matrix +# +# def shape(self): +# +# def matrix_add(self, other): +# +# def matrix_subtract(self, other): +# +# def matrix_mult_scaler(self, scaler): +# return [[value * scaler for value in row] for row in matrix] +# +# +# def matrix_mult_vector(self, vector): +# +# def matrix_mult(self, other): diff --git a/test_matrix_math.py b/test_matrix_math.py index 018ef2b..a0f10ba 100644 --- a/test_matrix_math.py +++ b/test_matrix_math.py @@ -33,7 +33,7 @@ def test_shape_vectors(): (for a matrix.)""" assert m.shape() == (2,) assert v.shape() == (3,) - assert shape([1]) == (1,) + def test_vector_add(): @@ -42,203 +42,203 @@ def test_vector_add(): Matrix + Matrix = Matrix """ - assert vector_add(v, w) == [1, 5, 4] + assert add(v, w) == [1, 5, 4] assert vector_add(u, y) == [11, 21, 31] assert vector_add(u, z) == u - -def test_vector_add_is_communicative(): - assert vector_add(w, y) == vector_add(y, w) - - -@raises(ShapeException) -def test_vector_add_checks_shapes(): - """Shape rule: the vectors must be the same size.""" - vector_add(m, v) - - -def test_vector_sub(): - """ - [a b] - [c d] = [a-c b-d] - - Matrix + Matrix = Matrix - """ - assert vector_sub(v, w) == [1, 1, -4] - assert vector_sub(w, v) == [-1, -1, 4] - assert vector_sub(y, z) == y - assert vector_sub(w, u) == vector_sub(z, vector_sub(u, w)) - - -@raises(ShapeException) -def test_vector_sub_checks_shapes(): - """Shape rule: the vectors must be the same size.""" - vector_sub(m, v) - - -def test_vector_sum(): - """vector_sum can take any number of vectors and add them together.""" - assert vector_sum(v, w, u, y, z) == [12, 26, 35] - - -@raises(ShapeException) -def test_vector_sum_checks_shapes(): - """Shape rule: the vectors must be the same size.""" - vector_sum(v, w, m, y) - - -def test_dot(): - """ - dot([a b], [c d]) = a * c + b * d - - dot(Vector, Vector) = Scalar - """ - assert dot(w, y) == 160 - assert dot(m, n) == 15 - assert dot(u, z) == 0 - - -@raises(ShapeException) -def test_dot_checks_shapes(): - """Shape rule: the vectors must be the same size.""" - dot(v, m) - - -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] - assert vector_multiply(m, 2) == [6, 8] - - -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] - assert vector_mean(v, w) == [0.5, 2.5, 2] - assert is_equal(vector_mean(v, w, u)[0], 2 / 3) - assert is_equal(vector_mean(v, w, u)[1], 2) - assert is_equal(vector_mean(v, w, u)[2], 5 / 3) - - -def test_magnitude(): - """ - magnitude([a b]) = sqrt(a^2 + b^2) - - magnitude(Vector) = Scalar - """ - assert magnitude(m) == 5 - assert magnitude(v) == math.sqrt(10) - assert magnitude(y) == math.sqrt(1400) - assert magnitude(z) == 0 - - -A = Matrix([[1, 0, 0], - [0, 1, 0], - [0, 0, 1]]) -B = Matrix([[1, 2, 3], - [4, 5, 6], - [7, 8, 9]]) -C = [[1, 2], - [2, 1], - [1, 2]] -D = [[1, 2, 3], - [3, 2, 1]] - - -def test_shape_matrices(): - """shape should take a vector or matrix and return a tuple with the - number of rows (for a vector) or the number of rows and columns - (for a matrix.)""" - assert shape(A) == (3, 3) - assert shape(C) == (3, 2) - assert shape(D) == (2, 3) - - -def test_matrix_row(): - """ - 0 1 <- rows - 0 [[a b]] - 1 [[c d]] - ^ - columns - """ - assert matrix_row(A, 0) == [1, 0, 0] - assert matrix_row(B, 1) == [4, 5, 6] - assert matrix_row(C, 2) == [1, 2] - - -def test_matrix_col(): - """ - 0 1 <- rows - 0 [[a b]] - 1 [[c d]] - ^ - columns - """ - assert matrix_col(A, 0) == [1, 0, 0] - assert matrix_col(B, 1) == [2, 5, 8] - assert matrix_col(D, 2) == [3, 1] - - -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], - [6, 3], - [3, 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] - assert matrix_vector_multiply(B, [1, 2, 3]) == [14, 32, 50] - assert matrix_vector_multiply(C, [3, 4]) == [11, 10, 11] - assert matrix_vector_multiply(D, [0, 1, 2]) == [8, 4] - - -@raises(ShapeException) -def test_matrix_vector_multiply_checks_shapes(): - """Shape Rule: The number of rows of the vector must equal the number of - columns of the matrix.""" - matrix_vector_multiply(C, [1, 2, 3]) - - -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 - assert matrix_matrix_multiply(B, C) == [[8, 10], - [20, 25], - [32, 40]] - assert matrix_matrix_multiply(C, D) == [[7, 6, 5], - [5, 6, 7], - [7, 6, 5]] - assert matrix_matrix_multiply(D, C) == [[8, 10], [8, 10]] - - -@raises(ShapeException) -def test_matrix_matrix_multiply_checks_shapes(): - """Shape Rule: The number of columns of the first matrix must equal the - number of rows of the second matrix.""" - matrix_matrix_multiply(A, D) +# +# def test_vector_add_is_communicative(): +# assert vector_add(w, y) == vector_add(y, w) +# +# +# @raises(ShapeException) +# def test_vector_add_checks_shapes(): +# """Shape rule: the vectors must be the same size.""" +# vector_add(m, v) +# +# +# def test_vector_sub(): +# """ +# [a b] - [c d] = [a-c b-d] +# +# Matrix + Matrix = Matrix +# """ +# assert vector_sub(v, w) == [1, 1, -4] +# assert vector_sub(w, v) == [-1, -1, 4] +# assert vector_sub(y, z) == y +# assert vector_sub(w, u) == vector_sub(z, vector_sub(u, w)) +# +# +# @raises(ShapeException) +# def test_vector_sub_checks_shapes(): +# """Shape rule: the vectors must be the same size.""" +# vector_sub(m, v) +# +# +# def test_vector_sum(): +# """vector_sum can take any number of vectors and add them together.""" +# assert vector_sum(v, w, u, y, z) == [12, 26, 35] +# +# +# @raises(ShapeException) +# def test_vector_sum_checks_shapes(): +# """Shape rule: the vectors must be the same size.""" +# vector_sum(v, w, m, y) +# +# +# def test_dot(): +# """ +# dot([a b], [c d]) = a * c + b * d +# +# dot(Vector, Vector) = Scalar +# """ +# assert dot(w, y) == 160 +# assert dot(m, n) == 15 +# assert dot(u, z) == 0 +# +# +# @raises(ShapeException) +# def test_dot_checks_shapes(): +# """Shape rule: the vectors must be the same size.""" +# dot(v, m) +# +# +# 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] +# assert vector_multiply(m, 2) == [6, 8] +# +# +# 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] +# assert vector_mean(v, w) == [0.5, 2.5, 2] +# assert is_equal(vector_mean(v, w, u)[0], 2 / 3) +# assert is_equal(vector_mean(v, w, u)[1], 2) +# assert is_equal(vector_mean(v, w, u)[2], 5 / 3) +# +# +# def test_magnitude(): +# """ +# magnitude([a b]) = sqrt(a^2 + b^2) +# +# magnitude(Vector) = Scalar +# """ +# assert magnitude(m) == 5 +# assert magnitude(v) == math.sqrt(10) +# assert magnitude(y) == math.sqrt(1400) +# assert magnitude(z) == 0 +# +# +# A = Matrix([[1, 0, 0], +# [0, 1, 0], +# [0, 0, 1]]) +# B = Matrix([[1, 2, 3], +# [4, 5, 6], +# [7, 8, 9]]) +# C = [[1, 2], +# [2, 1], +# [1, 2]] +# D = [[1, 2, 3], +# [3, 2, 1]] +# +# +# def test_shape_matrices(): +# """shape should take a vector or matrix and return a tuple with the +# number of rows (for a vector) or the number of rows and columns +# (for a matrix.)""" +# assert shape(A) == (3, 3) +# assert shape(C) == (3, 2) +# assert shape(D) == (2, 3) +# +# +# def test_matrix_row(): +# """ +# 0 1 <- rows +# 0 [[a b]] +# 1 [[c d]] +# ^ +# columns +# """ +# assert matrix_row(A, 0) == [1, 0, 0] +# assert matrix_row(B, 1) == [4, 5, 6] +# assert matrix_row(C, 2) == [1, 2] +# +# +# def test_matrix_col(): +# """ +# 0 1 <- rows +# 0 [[a b]] +# 1 [[c d]] +# ^ +# columns +# """ +# assert matrix_col(A, 0) == [1, 0, 0] +# assert matrix_col(B, 1) == [2, 5, 8] +# assert matrix_col(D, 2) == [3, 1] +# +# +# 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], +# [6, 3], +# [3, 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] +# assert matrix_vector_multiply(B, [1, 2, 3]) == [14, 32, 50] +# assert matrix_vector_multiply(C, [3, 4]) == [11, 10, 11] +# assert matrix_vector_multiply(D, [0, 1, 2]) == [8, 4] +# +# +# @raises(ShapeException) +# def test_matrix_vector_multiply_checks_shapes(): +# """Shape Rule: The number of rows of the vector must equal the number of +# columns of the matrix.""" +# matrix_vector_multiply(C, [1, 2, 3]) +# +# +# 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 +# assert matrix_matrix_multiply(B, C) == [[8, 10], +# [20, 25], +# [32, 40]] +# assert matrix_matrix_multiply(C, D) == [[7, 6, 5], +# [5, 6, 7], +# [7, 6, 5]] +# assert matrix_matrix_multiply(D, C) == [[8, 10], [8, 10]] +# +# +# @raises(ShapeException) +# def test_matrix_matrix_multiply_checks_shapes(): +# """Shape Rule: The number of columns of the first matrix must equal the +# number of rows of the second matrix.""" +# matrix_matrix_multiply(A, D) From 31772109681a4963368097fe1ac2f79bfa351e83 Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Thu, 14 May 2015 08:04:39 -0400 Subject: [PATCH 4/6] AttributeError --- matrix_math.py | 6 +++--- test_matrix_math.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/matrix_math.py b/matrix_math.py index ff1c565..4f4064b 100644 --- a/matrix_math.py +++ b/matrix_math.py @@ -13,17 +13,17 @@ def shape(self): try: return (len(self.vector), len(self.vector[0])) except TypeError: - return (len(self.vector),) + return (len(self.vector), ) def vector_add(self, other): - if shape(self.vector) != shape(other.vector): + if Vector.shape(self.vector) != Vector.shape(other.vector): raise ShapeException() return [self.vector[i] + other.vector[i] for i in range(len(self.vector))] def vector_sub(self, other): - if Vshape(self.vector) != shape(self.other): + if shape(self.vector) != shape(self.other): raise ShapeException() return [self.vector[i] - other.vector[i] for i in range(len(self.vector))] diff --git a/test_matrix_math.py b/test_matrix_math.py index a0f10ba..a99c8e7 100644 --- a/test_matrix_math.py +++ b/test_matrix_math.py @@ -18,13 +18,20 @@ def is_equal(x, y, tolerance=0.001): m = Vector([3, 4]) +ms = m.shape() n = Vector([5,0]) +ns = n.shape() v = Vector([1,3,0]) +vs = v.shape() w = Vector([0,2,4]) +ws = w.shape() u = Vector([1,1,1]) +us = u.shape() y = Vector([10,20,30]) +ys = y.shape() z = Vector([0,0,0]) +zs = z.shape() def test_shape_vectors(): @@ -42,9 +49,9 @@ def test_vector_add(): Matrix + Matrix = Matrix """ - assert add(v, w) == [1, 5, 4] - assert vector_add(u, y) == [11, 21, 31] - assert vector_add(u, z) == u + assert v.vector_add(w) == [1, 5, 4] + assert u.vector_add(y) == [11, 21, 31] + assert u.vector_add(z) == u # # def test_vector_add_is_communicative(): From fd5a7eafbc64ccd3a83c0cc1dffcb289f9edcc04 Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Thu, 14 May 2015 08:27:32 -0400 Subject: [PATCH 5/6] README update --- README.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.txt b/README.txt index 10dbd80..ca02d02 100644 --- a/README.txt +++ b/README.txt @@ -4,3 +4,7 @@ #I'm digging the math related content, even though I haven't gotten to that #portion of the assignment. More to come this morning; I believe I'll get a good #bit more done on this before class. See you there! + +#Part of my problem is I'm not working with people and asking my peers questions +#as much I should be. I have a "I'll just figure it out myself/at home" mentality +#Which I'm quickly learning is not the best strategy. From 780c27d52b1d404c9557863bcbff0877759fee2a Mon Sep 17 00:00:00 2001 From: Will Flowers Date: Fri, 20 May 2016 13:44:41 -0400 Subject: [PATCH 6/6] Delete old message --- README.txt | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 README.txt diff --git a/README.txt b/README.txt deleted file mode 100644 index ca02d02..0000000 --- a/README.txt +++ /dev/null @@ -1,10 +0,0 @@ -#Hello again! Still not finished, not very close either. I'm starting to get the -#concepts though. My moment of triumph today was going back and actually under- -#standing the code that we did in class on Monday (The game of Pig). In addition, -#I'm digging the math related content, even though I haven't gotten to that -#portion of the assignment. More to come this morning; I believe I'll get a good -#bit more done on this before class. See you there! - -#Part of my problem is I'm not working with people and asking my peers questions -#as much I should be. I have a "I'll just figure it out myself/at home" mentality -#Which I'm quickly learning is not the best strategy.