Skip to content

Added rank of matrix in linear algebra #8687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 31, 2023
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
52ec964
Added rank of matrix in linear algebra
rudransh61 Apr 24, 2023
df86e10
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 24, 2023
a1e7bb7
Corrected name of function
rudransh61 Apr 25, 2023
6cca1cd
Corrected Rank_of_Matrix.py
rudransh61 Apr 25, 2023
cb96700
Completed rank_of_matrix.py
rudransh61 Apr 25, 2023
c027189
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2023
d157ec0
delete to rename Rank_of_Matrix.py
rudransh61 Apr 26, 2023
9451960
created rank_of_matrix
rudransh61 Apr 26, 2023
1d026c9
added more doctests in rank_of_matrix.py
rudransh61 Apr 27, 2023
b37b262
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2023
c8af526
fixed some issues in rank_of_matrix.py
rudransh61 Apr 27, 2023
1b5a31a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2023
80de4d3
added moreeee doctestsss in rank_of_mtrix.py and fixed some bugss
rudransh61 Apr 27, 2023
9b3fa92
Merge branch 'TheAlgorithms:master' into master
rudransh61 Apr 28, 2023
afb5191
Update linear_algebra/src/rank_of_matrix.py
rudransh61 Apr 30, 2023
301726a
Update linear_algebra/src/rank_of_matrix.py
rudransh61 Apr 30, 2023
e683139
Update linear_algebra/src/rank_of_matrix.py
rudransh61 Apr 30, 2023
cd8d209
Update rank_of_matrix.py
rudransh61 Apr 30, 2023
0b13df6
Merge branch 'TheAlgorithms:master' into master
rudransh61 May 22, 2023
8d6caaf
Update linear_algebra/src/rank_of_matrix.py
cclauss May 30, 2023
fc66a5b
Merge branch 'TheAlgorithms:master' into master
rudransh61 May 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions linear_algebra/src/rank_of_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
Calculate the rank of a matrix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to give name.

See: https://en.wikipedia.org/wiki/Rank_(linear_algebra)
"""


def rank_of_matrix(matrix: list[list[int]]) -> 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([[],[]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add: >>> rank_of_matrix([[]])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this is ready to merge
and thanks to @rohan472000 and @cclauss for their feedbacks and suggestions

0
>>> rank_of_matrix([[1]])
1
>>> rank_of_matrix([[]])
0
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more unique doctests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed my name and added more unique doctests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add doctest for two empty lists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oook
i have also added for [[1]]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any more corrections

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your support sir


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()