-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_matrix_operations.py
104 lines (77 loc) · 2.09 KB
/
4_matrix_operations.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
import numpy as np
def main():
# addition, subtraction, multiplication, division by scalar
matrix = np.array([[3, 9], [21, -3]])
sum_result = matrix + 5
print(sum_result)
print()
diff_result = matrix - 1
print(diff_result)
print()
mult_result = matrix * 2
print(mult_result)
print()
div_result = matrix / 3
print(div_result)
print()
# addition, subtraction, multiplication, division by vector
vector = np.array([1, 2])
sum_result = matrix + vector
print(sum_result)
print()
diff_result = matrix - vector
print(diff_result)
print()
mult_result = matrix * vector
print(mult_result)
print()
div_result = matrix / vector
print(div_result)
print()
# addition, subtraction, multiplication, division by matrix
matrix_b = np.array([[4, 9], [25, 16]])
sum_result = matrix + matrix_b
print(sum_result)
print()
diff_result = matrix - matrix_b
print(diff_result)
print()
mult_result = matrix * matrix_b
print(mult_result)
print()
div_result = matrix_b / matrix
print(div_result)
print()
# transpose
transpose_a = np.transpose(matrix)
print(transpose_a)
print()
transpose_b = np.transpose(matrix_b)
print(transpose_b)
print()
# determinant
determinant = np.linalg.det(matrix)
print(determinant)
print()
# rank
rank = np.linalg.matrix_rank(matrix)
print(f"The rank of the matrix: {rank}")
# inverse
inverse = np.linalg.inv(matrix)
print("The inveres of the matrix")
print(inverse)
print()
product = np.dot(matrix, inverse)
print("The dot product of the matrix and its inverse.")
print(product.astype(int))
print()
# sparsity
def count_sparsity(matrix):
temp = np.nan_to_num(matrix, 0)
sparsity = 1.0 - (np.count_nonzero(temp) / temp.size)
return sparsity
matrix = np.array([[1, 1, 0, 1, 0, 0], [1, 0, 2, 0, 0, 1], [99, 0, 0, 2, 0, 0]])
sparsity = count_sparsity(matrix)
print(sparsity)
if __name__ == "__main__":
main()