Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions matrix_vector/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class MatrixDimensionError(BaseException):
pass

max_repr_rows = 12

class Matrix:
def __init__(self, *rows):
Expand Down Expand Up @@ -39,31 +40,31 @@ 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:
No arguments
"""
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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -275,15 +276,15 @@ 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])
else:
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(
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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:])))
16 changes: 10 additions & 6 deletions matrix_vector/tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand Down Expand Up @@ -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))')
18 changes: 18 additions & 0 deletions matrix_vector/tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)')
46 changes: 43 additions & 3 deletions matrix_vector/vector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class DifferentDimensionVectors(BaseException):
pass

max_repr_elements = 12

class Vector:
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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):

Expand Down Expand Up @@ -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:])))