-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixInverse.py
More file actions
54 lines (42 loc) · 1.3 KB
/
MatrixInverse.py
File metadata and controls
54 lines (42 loc) · 1.3 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
"""
Inverse of a Matrix
Overview
The inverse of a matrix is a matrix such that when it is multiplied by the original matrix,
the result is the identity matrix
Only square matrices (matrices with the same number of rows and columns) that are non-singular
(i.e., have a non-zero determinant) have an inverse.
Applications:
Solving systems of linear equations.
Cryptography (e.g., encoding and decoding messages).
Control theory and other engineering fields.
"""
# Inverse of a Matrix
# Astro Pema Software (c)
# Oba Ozai Nov 2024
import numpy as np
def calculate_inverse(matrix):
"""
Calculate the inverse of a matrix.
"""
try:
return np.linalg.inv(matrix)
except np.linalg.LinAlgError:
return "Matrix is singular and cannot be inverted."
if __name__ == "__main__":
# Define a 2x2 matrix
A_2x2 = np.array([[4, 7], [2, 6]])
print("2x2 Matrix A:")
print(A_2x2)
# Calculate the inverse
A_inv_2x2 = calculate_inverse(A_2x2)
print("\nInverse of 2x2 Matrix A:")
print(A_inv_2x2)
# Define a 3x3 matrix
A_3x3 = np.array([[1, 2, 3], [0, 1, 4], [5, 6, 0]])
print("\n3x3 Matrix A:")
print(A_3x3)
# Calculate the inverse
A_inv_3x3 = calculate_inverse(A_3x3)
print("\nInverse of 3x3 Matrix A:")
print(A_inv_3x3)
# EOF