-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
89 lines (73 loc) · 1.82 KB
/
example.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
import SparseMatrix as sm
# Create main function
def main():
print("Hello World")
# Define a sparse matrix
m1 = sm.SparseMatrix(3, 3)
m1[0, 0] = 1
m1[0, 1] = 2
m1[1, 1] = 3
m1[2, 2] = 4
print("M1 Sparse Matrix:")
print(m1)
# Define another sparse matrix
m2 = sm.SparseMatrix(3, 3)
m2[0, 1] = 5
m2[1, 0] = 6
m2[1, 2] = 7
m2[2, 0] = 8
print("M2 Sparse Matrix:")
print(m2)
# Add the two sparse matrices
m3 = m1 + m2
print("Addition of M1 and M2:")
print(m3)
# Multiply the two sparse matrices
m4 = m1 * m2
print("Multiplication of M1 and M2:")
print(m4)
# Check if a value is in the sparse matrix
print("Is 5 in M1?")
print(5 in m1, "\n")
# Iterate over the sparse matrix
print("Iterate over M1:")
for value in m1:
print(value)
# Check if two sparse matrices are equal
print("\nAre M1 and M2 equal?")
print(m1 == m2, "\n")
# Check if two sparse matrices are not equal
print("Are M1 and M3 not equal?")
print(m1 != m3, "\n")
# Length of the sparse matrix
print("Length of M1:")
print(len(m1), "\n")
# Convert the sparse matrix to hash
print("Hash of M1:")
print(hash(m1), "\n")
# Cast to bool
print("Bool of M1:")
print(bool(m1), "\n")
# Convert a sparse matrix to a normal matrix
print("Convert M1 to a normal matrix:")
print(m1.to_matrix(), "\n")
# Delete an element from the sparse matrix
print("Delete M1[0, 0]:")
del m1[0, 0]
del m1[0, 1]
del m1[1, 1]
print(m1)
# Conovert a nXn matrix to a sparse matrix
matrix = [
[0, 0, 1, 3],
[0, 0, 0, 0],
[1, 0, 0, 1],
[0, 4, 2, 0]
]
m5 = sm.SparseMatrix.from_matrix(matrix)
print("Sparse Matrix from Matrix:")
print(m5)
# This is the standard boilerplate that calls the main() function.
if __name__ == "__main__":
# Call the main function
main()