-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculate determinat & rank in Python.py
45 lines (34 loc) · 1.19 KB
/
Calculate determinat & rank in 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
import numpy as np
def cofactor_determinant(A):
n = len(A)
if n == 1:
determinant = A[0][0]
rankA = 1
return determinant, rankA
determinant = 0
for j in range(n):
# Calculate the cofactor of A[0][j]
sign = (-1)**j
submatrix = [[A[i][k] for k in range(n) if k != j] for i in range(1, n)]
cofactor = sign * cofactor_determinant(submatrix)[0]
# Add the contribution of this cofactor to the determinant
determinant += A[0][j] * cofactor
# Calculate the rank of the matrix
rankA = np.linalg.matrix_rank(A)
return determinant, rankA
# Prompt user to enter elements of the matrix
n = int(input('Enter the dimension of the matrix: '))
print('Enter the elements of the matrix:')
A = []
for i in range(n):
row = []
for j in range(n):
row.append(float(input(f'A({i+1},{j+1}) = ')))
A.append(row)
# Calculate the determinant and rank of the matrix using the cofactor method
determinant, rankA = cofactor_determinant(A)
# Display the results
print('The determinant of the matrix is:')
print(determinant)
print('The rank of the matrix is:')
print(rankA)