mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Added rank of matrix in linear algebra (#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 <cclauss@me.com> * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update rank_of_matrix.py * Update linear_algebra/src/rank_of_matrix.py Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
This commit is contained in:
parent
4a27b54430
commit
e871540e37
89
linear_algebra/src/rank_of_matrix.py
Normal file
89
linear_algebra/src/rank_of_matrix.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
"""
|
||||
Calculate the rank of a matrix.
|
||||
|
||||
See: https://en.wikipedia.org/wiki/Rank_(linear_algebra)
|
||||
"""
|
||||
|
||||
|
||||
def rank_of_matrix(matrix: list[list[int | float]]) -> int:
|
||||
"""
|
||||
Finds the rank of a matrix.
|
||||
Args:
|
||||
matrix: The matrix as a list of lists.
|
||||
Returns:
|
||||
The rank of the matrix.
|
||||
Example:
|
||||
>>> matrix1 = [[1, 2, 3],
|
||||
... [4, 5, 6],
|
||||
... [7, 8, 9]]
|
||||
>>> rank_of_matrix(matrix1)
|
||||
2
|
||||
>>> matrix2 = [[1, 0, 0],
|
||||
... [0, 1, 0],
|
||||
... [0, 0, 0]]
|
||||
>>> rank_of_matrix(matrix2)
|
||||
2
|
||||
>>> matrix3 = [[1, 2, 3, 4],
|
||||
... [5, 6, 7, 8],
|
||||
... [9, 10, 11, 12]]
|
||||
>>> rank_of_matrix(matrix3)
|
||||
2
|
||||
>>> rank_of_matrix([[2,3,-1,-1],
|
||||
... [1,-1,-2,4],
|
||||
... [3,1,3,-2],
|
||||
... [6,3,0,-7]])
|
||||
4
|
||||
>>> rank_of_matrix([[2,1,-3,-6],
|
||||
... [3,-3,1,2],
|
||||
... [1,1,1,2]])
|
||||
3
|
||||
>>> rank_of_matrix([[2,-1,0],
|
||||
... [1,3,4],
|
||||
... [4,1,-3]])
|
||||
3
|
||||
>>> rank_of_matrix([[3,2,1],
|
||||
... [-6,-4,-2]])
|
||||
1
|
||||
>>> rank_of_matrix([[],[]])
|
||||
0
|
||||
>>> rank_of_matrix([[1]])
|
||||
1
|
||||
>>> rank_of_matrix([[]])
|
||||
0
|
||||
"""
|
||||
|
||||
rows = len(matrix)
|
||||
columns = len(matrix[0])
|
||||
rank = min(rows, columns)
|
||||
|
||||
for row in range(rank):
|
||||
# Check if diagonal element is not zero
|
||||
if matrix[row][row] != 0:
|
||||
# Eliminate all the elements below the diagonal
|
||||
for col in range(row + 1, rows):
|
||||
multiplier = matrix[col][row] / matrix[row][row]
|
||||
for i in range(row, columns):
|
||||
matrix[col][i] -= multiplier * matrix[row][i]
|
||||
else:
|
||||
# Find a non-zero diagonal element to swap rows
|
||||
reduce = True
|
||||
for i in range(row + 1, rows):
|
||||
if matrix[i][row] != 0:
|
||||
matrix[row], matrix[i] = matrix[i], matrix[row]
|
||||
reduce = False
|
||||
break
|
||||
if reduce:
|
||||
rank -= 1
|
||||
for i in range(rows):
|
||||
matrix[i][row] = matrix[i][rank]
|
||||
|
||||
# Reduce the row pointer by one to stay on the same row
|
||||
row -= 1
|
||||
|
||||
return rank
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user