-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
executable file
·205 lines (146 loc) · 5.81 KB
/
matrix.py
File metadata and controls
executable file
·205 lines (146 loc) · 5.81 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
""" A sample python module for working with matrices.
Abraham D. Smith """
import numbers
class Matrix:
def __init__(self, listOfLists):
""" Construct a Matrix object from a list-of-lists, packed by row.
Some minimal sanity checks are in place. """
if not isinstance(listOfLists,list):
raise ValueError("listOfLists must be a list of lists")
if not all([ isinstance(row,list) for row in listOfLists]):
raise ValueError("listOfLists must be a list of lists")
nRows = len(listOfLists)
nCols = len(listOfLists[0])
if not all( [ len(row) == nCols for row in listOfLists ] ):
raise ValueError("List-of-lists has mismatched rows, so it cannot be a Matrix.")
if not all( [ all([ isinstance(elem,numbers.Number) for elem in row ])
for row in listOfLists ]):
raise(ValueError("A Matrix must contain Numbers"))
## give ourselves some attributes.
self._nRows=nRows
self._nCols=nCols
self._dict = dict()
for i in range(self._nRows):
for j in range(self._nCols):
self._dict[(i,j)]=listOfLists[i][j]
def __getitem__(self,key):
rowKey,colKey=key
""" Get row or element """
if not (rowKey,colKey) in self:
raise IndexError("Row or Column out of range for a {r} by {c} Matrix".format(r=self._nRows,c=self._nCols))
return self._dict[(rowKey,colKey)]
def __setitem__(self,key,value):
rowKey,colKey=key
if not (rowKey,colKey) in self:
raise IndexError("Row or Column out of range for a {r} by {c} Matrix".format(r=self._nRows,c=self._nCols))
if not isinstance(value,numbers.Number):
raise ValueError("Matrix entry must be a Number")
self._dict[(rowKey,colKey)]=value
def __iter__(self):
return ( keypair for keypair in sorted(self._dict) )
def __contains__(self,(rowKey,colKey)):
return (rowKey,colKey) in self._dict.keys()
def dimension(self):
""" compute the dimension of a Matrix, with minimal error correction.
To us, matrices are lists-of-lists, packed row-by-row """
return (self._nRows,self._nCols) ## note, this is a tuple
def __add__(self,other):
""" compute the element-by-element sum of two matrices. """
if not other.dimension() == self.dimension():
raise ValueError("Cannot add matrices of different dimensions.")
nRows,nCols=self.dimension()
newMatrix=Matrix( [ [0]*nCols ]*nRows )
for i,j in newMatrix:
newMatrix[i,j] = self[i,j] + other[i,j]
return newMatrix
def __rmul__(self,num):
""" scalar multplication of a matrix """
nRows,nCols=self.dimension()
newMatrix=Matrix( [ [0]*nCols ]*nRows )
for i,j in newMatrix:
newMatrix[i,j] = num*self[i,j]
return newMatrix
def __equals__(self,other):
if not other.dimension() == self.dimension():
return False
return all( [ self[i,j] == other[i,j] for (i,j) in self ])
def __mul__(self, other):
""" compute the matrix product """
nRows,nInner=self.dimension()
nInnerB,nCols=other.dimension()
if not nInner == nInnerB:
raise ValueError("Dimension mismatch for matrix multiplication.")
newMatrix=Matrix( [ [0]*nCols ]*nRows )
for i,j in newMatrix:
products=[self[i,k]*other[k,j] for k in range(nInner)]
newMatrix[i,j] = sum(products)
return newMatrix
def transpose(self):
""" construct the transpose of a matrix. This could be cleverer. """
nCols,nRows=self.dimension() ## SWAPPED!
newMatrix=Matrix( [ [0]*nCols ]*nRows )
for i,j in newMatrix:
newMatrix[i,j] = self[j,i]
return newMatrix
def __pow__(self,num):
""" scalar multplication of a matrix """
nRows,nCols=self.dimension()
if not nRows==nCols:
raise ValueError("Cannot take a power of a non-square matrix.")
if not type(num) == int and num >= 1:
raise ValueError("Can only take positive integer powers.")
if num==1:
return self
#print "... descending to {nn}".format(nn=num-1)
return self.__pow__(num-1)*self
def __str__(self):
""" pretty-print the matrix """
nRows,nCols=self.dimension()
string=""
for i in range(nRows):
string+="|"
for j in range(nCols):
string+="\t{}\t".format(self[i,j])
string+="|\n"
string+="\n"
return string
if __name__ == "__main__":
# This is called if we run as "./test.py"
print "Running example tests for Matrix class."
A = Matrix([[ 12, 45, 167], [3, 6, 2]])
try:
A1= Matrix("abcdef")
A2= Matrix( [["a", "b", "c"], ["d", "e", "f"]] )
A3= Matrix( [["a", "b", "c"], ["d", "e"]] )
except Exception as e:
print e
pass
B=Matrix([[ 4, 3, 1], [6, 7, 3] ])
C=Matrix([[ 1, 2, 3, 4], [9, 2, 2, 1], [ 5, 4, 3, 2]])
D=Matrix([[ 1,0],[0,2]])
print A.dimension()
print "A"
print A
print "B"
print B
print "A+B"
print A+B
#print add_matrix(A1,B1) ## I find this hillarious,too
#print add_matrix(A2,B2) ## I find this hillarious
#print mult_matrix(A,B) ## throws exception
#print mult_matrix(A,C)
#print mult_matrix(A2,C) ## throws exception
print "3A"
print 3*A
print "At"
print A.transpose()
print "C"
print C
print "AC"
print A*C
#print_matrix(transpose(A))
print "D"
print D
print "D**10"
print D**10