-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlink_sparsematrix.py
More file actions
executable file
·213 lines (154 loc) · 5.91 KB
/
link_sparsematrix.py
File metadata and controls
executable file
·213 lines (154 loc) · 5.91 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
204
205
206
207
208
209
210
211
212
213
__author__ = "streethacker"
#/usr/bin/python
#-*- coding:utf-8 -*-
# Data Structures and Algorithms Using Python
# CHAPTER 6: Linked Structures
# Listing 6.11: link_sparsematrix.py
from array import Array
class _MatrixElementNode:
"""
The non-zero elements for a given row will be stored in the corresponding linked
list sorted by column index.The row index is not needed since it corresponds to a
specific linked list within the array of linked lists.
"""
def __init__(self, col, value):
self.col = col
self.value = value
self.next = None
class SparseMatrix:
def __init__(self, numRows, numCols):
self._numCols = numCols
self._listOfRows = Array(numRows)
def numRows(self):
return len(self._listOfRows)
def numCols(self):
return self._numCols
def __setitem__(self, ndxTuple, value):
row, col = ndxTuple[0], ndxTuple[1]
assert row < self.numRows() and col < self.numCols(), \
"The index is out of range."
preNode = None
curNode = self._listOfRows[row]
while curNode is not None and curNode.col != col:
preNode = curNode
curNode = curNode.next
if curNode is not None and curNode.col == col:
if value == 0.0:
if curNode == self._listOfRows[row]:
self._listOfRows[row] = curNode.next
else:
preNode.next = curNode.next
else:
curNode.value = value
elif value != 0.0:
newNode = _MatrixElementNode(col, value)
newNode.next = curNode
if curNode == self._listOfRows[row]:
self._listOfRows[row] = newNode
else:
preNode.next = newNode
def __getitem__(self, ndxTuple):
row, col = ndxTuple[0], ndxTuple[1]
assert row < self.numRows() and col < self.numCols(), \
"The index is out of range."
curNode = self._listOfRows[row]
while curNode is not None and curNode.col != col:
curNode = curNode.next
if curNode is not None and curNode.col == col:
return curNode.value
else:
return 0
def scaleBy(self, scalar):
for row in range(self.numRows()):
curNode = self._listOfRows[row]
while curNode is not None:
curNode.value *= scalar
curNode = curNode.next
def __add__(self, rhsMatrix):
assert rhsMatrix.numRows() == self.numRows() and \
rhsMatrix.numCols() == self.numCols(), \
"Matrix sizes not compatable for adding."
newMatrix = SparseMatrix(self.numRows(), self.numCols())
for row in range(self.numRows()):
curNode = self._listOfRows[row]
while curNode is not None:
newMatrix[row, curNode.col] = curNode.value
curNode = curNode.next
for row in range(rhsMatrix.numRows()):
curNode = rhsMatrix._listOfRows[row]
while curNode is not None:
value = newMatrix[row, curNode.col]
value += curNode.value
newMatrix[row, curNode.col] = value
curNode = curNode.next
return newMatrix
def __sub__(self, rhsMatrix):
assert rhsMatrix.numRows() == self.numRows() and \
rhsMatrix.numCols() == self.numCols(), \
"Matrix sizes not compatable for adding."
newMatrix = SparseMatrix(self.numRows(), self.numCols())
for row in range(self.numRows()):
curNode = self._listOfRows[row]
while curNode is not None:
newMatrix[row, curNode.col] = curNode.value
curNode = curNode.next
for row in range(rhsMatrix.numRows()):
curNode = rhsMatrix._listOfRows[row]
while curNode is not None:
value = newMatrix[row, curNode.col]
value -= curNode.value
newMatrix[row, curNode.col] = value
curNode = curNode.next
return newMatrix
def transpose(self):
newMatrix = SparseMatrix(self.numCols(), self.numRows())
for row in range(self.numRows()):
curNode = self._listOfRows[row]
while curNode is not None:
newMatrix[curNode.col, row] = curNode.value
curNode = curNode.next
return newMatrix
def __mul__(self, rhsMatrix):
assert self.numCols() == rhsMatrix.numRows(), \
"Matrix sizes not compatable for multipy."
newMatrix = SparseMatrix(self.numRows(), rhsMatrix.numCols())
for r in range(self.numRows()):
for c in range(rhsMatrix.numCols()):
for cx in range(self.numCols()):
newMatrix[r, c] += self[r, cx] * rhsMatrix[cx, c]
return newMatrix
def printMatrix(self):
for row in range(self.numRows()):
for col in range(self.numCols()):
print self[row, col], "\t",
print
if __name__ == "__main__":
lspMtx, rspMtx = SparseMatrix(3, 4), SparseMatrix(3, 4)
newMtx = SparseMatrix(3, 4)
transMtx = SparseMatrix(4, 3)
lspMtx[0, 1] = 1
lspMtx[1, 2] = 2
rspMtx[0, 0] = 4
rspMtx[1, 2] = 1
rspMtx[2, 3] = 3
newMtx = lspMtx + rspMtx
print "The left Matrix contents:"
lspMtx.printMatrix()
print
print "The right Matrix contents:"
rspMtx.printMatrix()
print
print "The new Matrix contents:"
newMtx.printMatrix()
transMtx = newMtx.transpose()
print
print "The new Matrix after transpose:"
transMtx.printMatrix()
mul_lMtx = SparseMatrix(3, 3)
mul_rMtx = SparseMatrix(3, 2)
mul_lMtx[0, 2], mul_lMtx[1, 1] = 1, 1
mul_rMtx[1, 0], mul_rMtx[2, 1] = 1, 1
mul_newMtx = mul_lMtx * mul_rMtx
print
print "The two matrices multipication is:"
mul_newMtx.printMatrix()