From 3d3293b6fe0ce2cdf4ff4eee65e6cf7a5c247e2a Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 22:16:17 +0000 Subject: [PATCH 1/8] colum -> column --- matrix_vector/matrix.py | 32 +++++++++++++++--------------- matrix_vector/tests/test_matrix.py | 12 +++++------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/matrix_vector/matrix.py b/matrix_vector/matrix.py index ea03dec..6792086 100644 --- a/matrix_vector/matrix.py +++ b/matrix_vector/matrix.py @@ -39,12 +39,12 @@ def rows(self): """ return len(self.elements) - def colums(self): + def columns(self): """ - Returns the number of colums of the matrix. + Returns the number of columns of the matrix. Example: - >> Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]).colums() + >> Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]).columns() => 3 Arguments: @@ -52,18 +52,18 @@ def colums(self): """ return self[0].size - def get_colum(self, number): + def get_column(self, number): """ - Returns the n-th colum of the matrix as an object of class Vector. + Returns the n-th column of the matrix as an object of class Vector. Example: - >> Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]).get_colum(1) + >> Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]).get_column(1) => Vector(2, 5, 8) Arguments: number : (int) """ - if (number in range(self.colums())): + if (number in range(self.columns())): return Vector(*[element[number] for element in self.elements]) else: raise IndexError("Matix index out of range") @@ -96,7 +96,7 @@ def is_same_dimension(self, matrix): matrix : (Matrix) """ rows = (self.rows() == matrix.rows()) - cols = (self.colums() == matrix.colums()) + cols = (self.columns() == matrix.columns()) return rows and cols def __add_matrix(self, matrix): @@ -242,7 +242,7 @@ def transposed(self): Arguments: No arguments """ - return Matrix(*[self.get_colum(i) for i in range(self.colums())]) + return Matrix(*[self.get_column(i) for i in range(self.columns())]) def transpose(self): """ @@ -275,7 +275,7 @@ def __mul__(self, other): matrix : (Matrix) """ if (type(other) is Matrix): - if (self.colums() == other.rows()): + if (self.columns() == other.rows()): transposed_other = other.transposed() return Matrix(*[[x * y for y in transposed_other] for x in self.elements]) @@ -283,7 +283,7 @@ def __mul__(self, other): raise MatrixDimensionError( "Can't multiply matrices with unsutable dimensions") elif (type(other) is Vector): - if (self.colums() == other.size): + if (self.columns() == other.size): return Vector(*[x * other for x in self.elements]) else: raise MatrixDimensionError( @@ -293,7 +293,7 @@ def __mul__(self, other): def minor(self, i, j): """ - Returns a matrix without the row and the colum given as arguments. + Returns a matrix without the row and the column given as arguments. Example: >> Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]).minor(0, 1) @@ -303,7 +303,7 @@ def minor(self, i, j): number1 : (int) number2 : (int) """ - if (i < self.rows() and j < self.colums()): + if (i < self.rows() and j < self.columns()): minor = [list(el) for el in self.elements] del minor[i] for k in range(self.rows() - 1): @@ -327,12 +327,12 @@ def determinant(self): Arguments: no arguments """ - if (self.rows() == self.colums()): + if (self.rows() == self.columns()): if (self.rows() == 1): return self[0][0] else: det = 0 - for x in range(self.colums()): + for x in range(self.columns()): minor_det = self.minor(0, x).determinant() det += self[0][x] * (-1) ** (2 + x) * minor_det return det @@ -354,7 +354,7 @@ def inversed(self): if (self.determinant() != 0): m = Matrix(*[[(-1) ** (i + j) * self.minor(i, j).determinant() for j in range(self.rows())] - for i in range(self.colums())]) + for i in range(self.columns())]) return (m.transposed() * (1 / self.determinant())) else: raise ZeroDivisionError( diff --git a/matrix_vector/tests/test_matrix.py b/matrix_vector/tests/test_matrix.py index e3bfbc7..15f2010 100644 --- a/matrix_vector/tests/test_matrix.py +++ b/matrix_vector/tests/test_matrix.py @@ -8,10 +8,10 @@ def test_rows(self): [5, 6, 7], [8, 9, 10]).rows(), 3) - def test_colums(self): + def test_columns(self): self.assertEqual(m.Matrix([1, 2, 3], [5, 6, 7], - [8, 9, 10]).colums(), 3) + [8, 9, 10]).columns(), 3) def test_get_row(self): a = m.Matrix([1, 2, 3], @@ -26,18 +26,18 @@ def test_get_row_out_of_range(self): with self.assertRaises(IndexError): a.get_row(3) - def test_get_colum(self): + def test_get_column(self): a = m.Matrix([1, 2, 3], [5, 6, 7], [8, 9, 10]) - self.assertEqual(a.get_colum(1), m.Vector(2, 6, 9)) + self.assertEqual(a.get_column(1), m.Vector(2, 6, 9)) - def test_get_colum_out_of_range(self): + def test_get_column_out_of_range(self): a = m.Matrix([1, 2, 3], [5, 6, 7], [8, 9, 10]) with self.assertRaises(IndexError): - a.get_colum(3) + a.get_column(3) def test_same_dimension_true(self): a1 = m.Matrix([1, 2, 3], From 0aacbd7d2c6a0fd9b7dbbc2f9c6a5150f74c995a Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 22:51:57 +0000 Subject: [PATCH 2/8] nubmer -> number --- matrix_vector/vector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index 95d0bd7..7ccc2a1 100644 --- a/matrix_vector/vector.py +++ b/matrix_vector/vector.py @@ -230,7 +230,7 @@ def __ixor__(self, other): def __truediv__(self, other): """ - Divides the elements of the vector by a nubmer. + Divides the elements of the vector by a number. Returns new object. Example: @@ -247,7 +247,7 @@ def __truediv__(self, other): def __itruediv__(self, other): """ - Divides the elements of the vector by a nubmer. + Divides the elements of the vector by a number. Changes the object. Example: From ad2fc22be990ae9abd675034e41297f1fd7a3dba Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 23:36:36 +0000 Subject: [PATCH 3/8] __repr__ for Matrix and Vector --- matrix_vector/matrix.py | 8 ++++++++ matrix_vector/vector.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/matrix_vector/matrix.py b/matrix_vector/matrix.py index 6792086..fc9ef81 100644 --- a/matrix_vector/matrix.py +++ b/matrix_vector/matrix.py @@ -4,6 +4,7 @@ class MatrixDimensionError(BaseException): pass +max_repr_rows = 12 class Matrix: def __init__(self, *rows): @@ -378,3 +379,10 @@ def round(self, number): """ self.elements = [x.round(number) for x in self.elements] return self + + def __repr__(self): + if self.rows() <= max_repr_rows: + return 'Matrix({0})'.format(',\n '.join(map(str, self.elements))) + else: + return 'Matrix({0})'.format(',\n '.join( + map(str, self[:3] + ['...'] + self[-3:]))) diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index 7ccc2a1..fd1d02e 100644 --- a/matrix_vector/vector.py +++ b/matrix_vector/vector.py @@ -1,6 +1,7 @@ class DifferentDimensionVectors(BaseException): pass +max_repr_elements = 12 class Vector: """ @@ -318,3 +319,10 @@ def round(self, number): def __eq__(self, vector): return self.coordinates == vector.coordinates + + def __repr__(self): + if self.size <= max_repr_elements: + return 'Vector({0})'.format(', '.join(map(str, self.coordinates))) + else: + return 'Vector({0})'.format(', '.join( + map(str, self[:3] + ['...'] + self[-3:]))) From 034edcf003f8e5cdb9a18a693dc3813387a2e6a3 Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 23:44:21 +0000 Subject: [PATCH 4/8] test __repr__ --- matrix_vector/tests/test_matrix.py | 4 ++++ matrix_vector/tests/test_vector.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/matrix_vector/tests/test_matrix.py b/matrix_vector/tests/test_matrix.py index 15f2010..701c781 100644 --- a/matrix_vector/tests/test_matrix.py +++ b/matrix_vector/tests/test_matrix.py @@ -314,3 +314,7 @@ def test_round(self): self.assertEqual(a.round(2), m.Matrix([-0.09, 0.13, 0.32], [-0.09, 0.24, 0.21], [0.27, -0.17, -0.19])) + + def test_repr(self): + a = m.Matrix([1, 2], [3, 4]) + self.assertEqual(repr(a), 'Matrix(Vector(1, 2),\n Vector(3, 4))') diff --git a/matrix_vector/tests/test_vector.py b/matrix_vector/tests/test_vector.py index 63257e2..41b1849 100644 --- a/matrix_vector/tests/test_vector.py +++ b/matrix_vector/tests/test_vector.py @@ -131,3 +131,7 @@ def test_round(self): v = m.Vector(1.345, 2.438, 3.535) v.round(2) self.assertEqual(v, m.Vector(1.34, 2.44, 3.54)) + + def test_repr(self): + v = m.Vector(1, 2, 3) + self.assertEqual(repr(v), 'Vector(1, 2, 3)') From a2b5ddc578545de1640054dba176cc916e519357 Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 23:57:17 +0000 Subject: [PATCH 5/8] __[i]floordiv__ for Vector --- matrix_vector/vector.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index fd1d02e..0f3d771 100644 --- a/matrix_vector/vector.py +++ b/matrix_vector/vector.py @@ -252,7 +252,7 @@ def __itruediv__(self, other): Changes the object. Example: - >> Vector(3, 9, 6) / 3 + >> Vector(3, 9, 8) / 3 => Vector(1, 3, 2) Arguments: @@ -261,6 +261,38 @@ def __itruediv__(self, other): self = self / other return self + def __floordiv__(self, other): + """ + GetsDivides the elements of the vector by a number. + Returns new object. + + Example: + >> Vector(3, 9, 8) // 3 + => Vector(1, 3, 2) + + Arguments: + number : (Numeric) + """ + try: + return Vector(*[_ // other for _ in self.coordinates]) + except ZeroDivisionError: + raise + + def __ifloordiv__(self, other): + """ + Divides the elements of the vector by a number. + Changes the object. + + Example: + >> Vector(3, 9, 6) // 3 + => Vector(1, 3, 2) + + Arguments: + number : (Numeric) + """ + self = self // other + return self + @property def length(self): From 4d9f4a40926f8cb90850e4d3b4c28ad4cf1e6663 Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Tue, 29 Dec 2015 23:59:20 +0000 Subject: [PATCH 6/8] __floordiv__ docstring --- matrix_vector/vector.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index 0f3d771..37372bc 100644 --- a/matrix_vector/vector.py +++ b/matrix_vector/vector.py @@ -263,7 +263,8 @@ def __itruediv__(self, other): def __floordiv__(self, other): """ - GetsDivides the elements of the vector by a number. + Finds the floor when dividing the elements of the vector + by a number. Returns new object. Example: @@ -280,7 +281,8 @@ def __floordiv__(self, other): def __ifloordiv__(self, other): """ - Divides the elements of the vector by a number. + Finds the floor when dividing the elements of the vector + by a number. Changes the object. Example: From f90851983834cd21a6ca524a6029228954bc1cbb Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Wed, 30 Dec 2015 00:01:27 +0000 Subject: [PATCH 7/8] test __floordiv__ --- matrix_vector/tests/test_vector.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/matrix_vector/tests/test_vector.py b/matrix_vector/tests/test_vector.py index 41b1849..6738406 100644 --- a/matrix_vector/tests/test_vector.py +++ b/matrix_vector/tests/test_vector.py @@ -112,6 +112,20 @@ def test_zero_division(self): with self.assertRaises(ZeroDivisionError): m.Vector(4, 3, 2, 1) / 0 + def test_floor_division(self): + v = m.Vector(2, 4, 7) + self.assertEqual(v // 2, m.Vector(1, 2, 3)) + self.assertEqual(v, m.Vector(2, 4, 7)) + + def test_i_floor_division(self): + v = m.Vector(2, 4, 7) + v //= 2 + self.assertEqual(v, m.Vector(1, 2, 3)) + + def test_zero_floor_division(self): + with self.assertRaises(ZeroDivisionError): + m.Vector(4, 3, 2, 1) // 0 + def test_length(self): v = m.Vector(1, 2, 3) self.assertEqual(round(v.length, 4), 3.7417) From 23aae9ca4f165105a13a6b6085eaa8c7bd6c5ffb Mon Sep 17 00:00:00 2001 From: Serin Donnelly Date: Wed, 30 Dec 2015 00:56:37 +0000 Subject: [PATCH 8/8] remove unnecessary linebreaks in docstrings --- matrix_vector/vector.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index 37372bc..514bfec 100644 --- a/matrix_vector/vector.py +++ b/matrix_vector/vector.py @@ -264,8 +264,7 @@ def __itruediv__(self, other): def __floordiv__(self, other): """ Finds the floor when dividing the elements of the vector - by a number. - Returns new object. + by a number. Returns new object. Example: >> Vector(3, 9, 8) // 3 @@ -282,8 +281,7 @@ def __floordiv__(self, other): def __ifloordiv__(self, other): """ Finds the floor when dividing the elements of the vector - by a number. - Changes the object. + by a number. Changes the object. Example: >> Vector(3, 9, 6) // 3