-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrix.py
108 lines (89 loc) · 2.33 KB
/
SparseMatrix.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
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
#
# @Name: SparseMatrix In Python
# @Author: Max Base
# @Date: 2022/11/01
# @Repository: https://github.com/BaseMax/SparseMatrixPy
#
class SparseMatrix:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.data = {}
def __getitem__(self, index):
return self.data.get(index, 0)
def __setitem__(self, index, value):
self.data[index] = value
def __add__(self, other):
assert self.rows == other.rows and self.cols == other.cols
result = SparseMatrix(self.rows, self.cols)
for index in self.data:
result[index] = self[index]
for index in other.data:
result[index] += other[index]
return result
def __mul__(self, other):
assert self.cols == other.rows
result = SparseMatrix(self.rows, other.cols)
for i in range(self.rows):
for j in range(other.cols):
for k in range(self.cols):
result[i, j] += self[i, k] * other[k, j]
return result
def __str__(self):
result = []
for i in range(self.rows):
for j in range(self.cols):
result.append(str(self[i, j]).rjust(8))
result.append("\n")
return ''.join(result)
def __repr__(self):
return str(self)
def __eq__(self, other):
if self.rows != other.rows or self.cols != other.cols:
return False
for index in self.data:
if self[index] != other[index]:
return False
for index in other.data:
if self[index] != other[index]:
return False
return True
def __ne__(self, other):
return not self == other
def __hash__(self):
result = 0
for index in self.data:
result ^= hash(index) ^ hash(self[index])
return result
def __iter__(self):
for i in range(self.rows):
for j in range(self.cols):
yield self[i, j]
def __contains__(self, value):
for index in self.data:
if self[index] == value:
return True
return False
def __len__(self):
return self.rows * self.cols
def __bool__(self):
return len(self.data) > 0
def __delitem__(self, index):
del self.data[index]
def to_matrix(self):
result = []
for i in range(self.rows):
result.append([])
for j in range(self.cols):
result[i].append(self[i, j])
return result
# public methods
def from_matrix(matrix):
rows = len(matrix)
cols = len(matrix[0])
result = SparseMatrix(rows, cols)
for i in range(rows):
for j in range(cols):
if matrix[i][j] != 0:
result[i, j] = matrix[i][j]
return result