Skip to content

Commit c284c3c

Browse files
Daili01jakobkogler
authored andcommitted
Translation rank of a matrix in O(n^3) (#275)
1 parent 3dbf87f commit c284c3c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ especially popular in field of competitive programming.*
6363
- [Gauss & System of Linear Equations](./linear_algebra/linear-system-gauss.html)
6464
- [Gauss & Determinant](./linear_algebra/determinant-gauss.html)
6565
- [Kraut & Determinant](./linear_algebra/determinant-kraut.html)
66+
- [Rank of a matrix](./linear_algebra/rank-matrix.html)
6667

6768
### Combinatorics
6869
- [The Inclusion-Exclusion Principle](./combinatorics/inclusion-exclusion.html)

src/linear_algebra/rank-matrix.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!--?title Rank of a matrix-->
2+
3+
#Finding the rank of a matrix
4+
5+
**The rank of a matrix** is the largest number of linearly independent rows/columns of the matrix. The rank is not only defined for square matrices.
6+
7+
The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix.
8+
9+
Let the matrix be rectangular and have size $N \times M$.
10+
Note that if the matrix is square and its determinant is non-zero, then the rank is $N$ ($=M$); otherwise it will be less. Generally, the rank of a matrix does not exceed $\min (N, M)$.
11+
12+
##Algorithm
13+
14+
You can search for the rank using [Gaussian elimination](./linear_algebra/linear-system-gauss.html). We will perform the same operations as when solving the system or finding its determinant. But if at any step in the $i$-th column there are no rows with an non-empty entry among those that we didn't selected already, then we skip this step and decrease the rank by one (initially the rank is set equal to $\max (N, M)$).
15+
Otherwise, if we have found a row with a non-zero element in the $i$-th column during the $i$-th step, then we mark this row as a selected one and perform the usual operations of taking this row away from the rest.
16+
17+
##Complexity
18+
19+
This algorithm runs in $\mathcal{O}(n^3)$.
20+
21+
##Implementation
22+
23+
```cpp
24+
const double EPS = 1E-9;
25+
26+
int compute_rank(vector<vector<int>> A) {
27+
int n = A.size();
28+
int m = A[0].size();
29+
30+
int rank = max(n, m);
31+
vector<bool> row_selected(n, false);
32+
for (int i = 0; i < m; ++i) {
33+
int j;
34+
for (j = 0; j < n; ++j) {
35+
if (!row_selected[j] && abs(A[j][i]) > EPS)
36+
break;
37+
}
38+
39+
if (j == n) {
40+
--rank;
41+
} else {
42+
row_selected[j] = true;
43+
for (int p = i + 1; p < m; ++p)
44+
A[j][p] /= A[j][i];
45+
for (int k = 0; k < n; ++k) {
46+
if (k != j && abs(A[k][i]) > EPS) {
47+
for (int p = i + 1; p < m; ++p)
48+
A[k][p] -= A[j][p] * A[k][i];
49+
}
50+
}
51+
}
52+
}
53+
return rank;
54+
}
55+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy