diff --git a/matrix_vector/matrix.py b/matrix_vector/matrix.py index ea03dec..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): @@ -39,12 +40,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 +53,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 +97,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 +243,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 +276,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 +284,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 +294,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 +304,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 +328,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 +355,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( @@ -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/tests/test_matrix.py b/matrix_vector/tests/test_matrix.py index e3bfbc7..701c781 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], @@ -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..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) @@ -131,3 +145,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)') diff --git a/matrix_vector/vector.py b/matrix_vector/vector.py index 95d0bd7..514bfec 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: """ @@ -230,7 +231,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,11 +248,11 @@ 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: - >> Vector(3, 9, 6) / 3 + >> Vector(3, 9, 8) / 3 => Vector(1, 3, 2) Arguments: @@ -260,6 +261,38 @@ def __itruediv__(self, other): self = self / other return self + def __floordiv__(self, other): + """ + Finds the floor when dividing 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): + """ + Finds the floor when dividing 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): @@ -318,3 +351,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:])))