-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_10(python).py
108 lines (84 loc) · 3.31 KB
/
Practical_10(python).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
# Write a python program for sparse matrix realization and operations on it- Transpose,
# Fast Transpose and addition of two matrices
class SparseMatrix:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.elements = []
def insert(self, row, col, value):
self.elements.append((row, col, value))
def display(self):
for row in range(self.rows):
for col in range(self.cols):
element = 0
for r, c, v in self.elements:
if r == row and c == col:
element = v
break
print(element, end="\t")
print()
def transpose(self):
transposed_matrix = SparseMatrix(self.cols, self.rows)
for r, c, v in self.elements:
transposed_matrix.insert(c, r, v)
return transposed_matrix
def fast_transpose(self):
row_terms = [0] * self.cols
starting_pos = [0] * self.cols
for r, _, _ in self.elements:
row_terms[r] += 1
starting_pos[0] = 0
for i in range(1, self.cols):
starting_pos[i] = starting_pos[i - 1] + row_terms[i - 1]
transposed_matrix = SparseMatrix(self.cols, self.rows)
for r, c, v in self.elements:
transposed_matrix.insert(c, r, v)
starting_pos[c] += 1
return transposed_matrix
def add_matrices(self, other_matrix):
if self.rows != other_matrix.rows or self.cols != other_matrix.cols:
raise ValueError("Matrices must have the same dimensions for addition.")
result_matrix = SparseMatrix(self.rows, self.cols)
combined_elements = self.elements + other_matrix.elements
for r in range(self.rows):
for c in range(self.cols):
total = 0
for rr, cc, v in combined_elements:
if rr == r and cc == c:
total += v
if total != 0:
result_matrix.insert(r, c, total)
return result_matrix
def main():
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
matrix = SparseMatrix(rows, cols)
print("Enter the elements of the matrix:")
for r in range(rows):
row_data = list(map(int, input().split()))
for c, value in enumerate(row_data):
if value != 0:
matrix.insert(r, c, value)
print("Original Matrix:")
matrix.display()
transpose_matrix = matrix.transpose()
print("\nTranspose of the Matrix:")
transpose_matrix.display()
fast_transpose_matrix = matrix.fast_transpose()
print("\nFast Transpose of the Matrix:")
fast_transpose_matrix.display()
print("\nEnter another matrix for addition:")
other_matrix = SparseMatrix(rows, cols)
print("Enter the elements of the other matrix:")
for r in range(rows):
row_data = list(map(int, input().split()))
for c, value in enumerate(row_data):
if value != 0:
other_matrix.insert(r, c, value)
print("\nSecond Matrix:")
other_matrix.display()
addition_result = matrix.add_matrices(other_matrix)
print("\nAddition of the two matrices:")
addition_result.display()
if __name__ == "__main__":
main()