-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
77 lines (59 loc) · 2.02 KB
/
matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
""" Example Matrix class for docstrings and testing in python.
Basic math classes used for demonstrating how code can be documented
and tested in python
"""
class Matrix:
""" Represents a matrix of real numbers
Very slow and basic matrix operations are performed. Use NumPy instead!
Also this is not tested very well, there are bugs, write tests to find them
It is purposely written in a way to introduce bugs that can be found
via testing
Attributes:
rows: The number of rows in the matrix
cols: The number of columns in the matrix
"""
def __init__(self, n, m):
""" Create an n x m matrix, filled with zeros
Args:
n: the number of rows
m: the number of columns
"""
self.__rows = n
self.__cols = m
self.__data = [0] * n * m
@property
def rows(self):
return self.__rows
@property
def cols(self):
return self.__cols
def __setitem__(self, key, value):
""" Set matrix element [i,j] to value
Args:
key: tuple (i,j) with row and column indices
value: value to set
"""
self.__data[key[0] + key[1] * self.rows] = elem
def __getitem__(self, key):
""" Get element [i,j] from the matrix
Args:
key: tuple(i,j) with row and column indices
"""
return self.__data[key[0] + key[1] * self.cols]
def __mul__(self, rhs):
""" Multiply this matrix by another matrix
Args:
rhs - the right hand side of the matrix multiplication
Returns:
A matrix that is self * rhs
"""
if self.cols != rhs.rows:
raise Exception("Dimension mismatch")
result = Matrix(self.rows, rhs.cols)
for i in range(self.rows):
for j in range(rhs.cols):
rowsum = 0
for k in range(self.cols):
rowsum += self[(i,k)] * rhs[(k,i)]
result[(i,j)] = rowsum
return result