Skip to content

Commit e871540

Browse files
rudransh61pre-commit-ci[bot]cclaussCaedenPH
authored
Added rank of matrix in linear algebra (TheAlgorithms#8687)
* Added rank of matrix in linear algebra * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Corrected name of function * Corrected Rank_of_Matrix.py * Completed rank_of_matrix.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * delete to rename Rank_of_Matrix.py * created rank_of_matrix * added more doctests in rank_of_matrix.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed some issues in rank_of_matrix.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added moreeee doctestsss in rank_of_mtrix.py and fixed some bugss * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Christian Clauss <[email protected]> * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Christian Clauss <[email protected]> * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Christian Clauss <[email protected]> * Update rank_of_matrix.py * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Caeden Perelli-Harris <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: Caeden Perelli-Harris <[email protected]>
1 parent 4a27b54 commit e871540

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

linear_algebra/src/rank_of_matrix.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Calculate the rank of a matrix.
3+
4+
See: https://en.wikipedia.org/wiki/Rank_(linear_algebra)
5+
"""
6+
7+
8+
def rank_of_matrix(matrix: list[list[int | float]]) -> int:
9+
"""
10+
Finds the rank of a matrix.
11+
Args:
12+
matrix: The matrix as a list of lists.
13+
Returns:
14+
The rank of the matrix.
15+
Example:
16+
>>> matrix1 = [[1, 2, 3],
17+
... [4, 5, 6],
18+
... [7, 8, 9]]
19+
>>> rank_of_matrix(matrix1)
20+
2
21+
>>> matrix2 = [[1, 0, 0],
22+
... [0, 1, 0],
23+
... [0, 0, 0]]
24+
>>> rank_of_matrix(matrix2)
25+
2
26+
>>> matrix3 = [[1, 2, 3, 4],
27+
... [5, 6, 7, 8],
28+
... [9, 10, 11, 12]]
29+
>>> rank_of_matrix(matrix3)
30+
2
31+
>>> rank_of_matrix([[2,3,-1,-1],
32+
... [1,-1,-2,4],
33+
... [3,1,3,-2],
34+
... [6,3,0,-7]])
35+
4
36+
>>> rank_of_matrix([[2,1,-3,-6],
37+
... [3,-3,1,2],
38+
... [1,1,1,2]])
39+
3
40+
>>> rank_of_matrix([[2,-1,0],
41+
... [1,3,4],
42+
... [4,1,-3]])
43+
3
44+
>>> rank_of_matrix([[3,2,1],
45+
... [-6,-4,-2]])
46+
1
47+
>>> rank_of_matrix([[],[]])
48+
0
49+
>>> rank_of_matrix([[1]])
50+
1
51+
>>> rank_of_matrix([[]])
52+
0
53+
"""
54+
55+
rows = len(matrix)
56+
columns = len(matrix[0])
57+
rank = min(rows, columns)
58+
59+
for row in range(rank):
60+
# Check if diagonal element is not zero
61+
if matrix[row][row] != 0:
62+
# Eliminate all the elements below the diagonal
63+
for col in range(row + 1, rows):
64+
multiplier = matrix[col][row] / matrix[row][row]
65+
for i in range(row, columns):
66+
matrix[col][i] -= multiplier * matrix[row][i]
67+
else:
68+
# Find a non-zero diagonal element to swap rows
69+
reduce = True
70+
for i in range(row + 1, rows):
71+
if matrix[i][row] != 0:
72+
matrix[row], matrix[i] = matrix[i], matrix[row]
73+
reduce = False
74+
break
75+
if reduce:
76+
rank -= 1
77+
for i in range(rows):
78+
matrix[i][row] = matrix[i][rank]
79+
80+
# Reduce the row pointer by one to stay on the same row
81+
row -= 1
82+
83+
return rank
84+
85+
86+
if __name__ == "__main__":
87+
import doctest
88+
89+
doctest.testmod()

0 commit comments

Comments
 (0)