diff --git a/src/index.md b/src/index.md index 6fa21e2bf..9e7b913a3 100644 --- a/src/index.md +++ b/src/index.md @@ -63,6 +63,7 @@ especially popular in field of competitive programming.* - [Gauss & System of Linear Equations](./linear_algebra/linear-system-gauss.html) - [Gauss & Determinant](./linear_algebra/determinant-gauss.html) - [Kraut & Determinant](./linear_algebra/determinant-kraut.html) +- [Rank of a matrix](./linear_algebra/rank-matrix.html) ### Combinatorics - [The Inclusion-Exclusion Principle](./combinatorics/inclusion-exclusion.html) diff --git a/src/linear_algebra/rank-matrix.md b/src/linear_algebra/rank-matrix.md new file mode 100644 index 000000000..6a7050a35 --- /dev/null +++ b/src/linear_algebra/rank-matrix.md @@ -0,0 +1,55 @@ + + +#Finding the rank of a matrix + +**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. + +The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix. + +Let the matrix be rectangular and have size $N \times M$. +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)$. + +##Algorithm + +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)$). +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. + +##Complexity + +This algorithm runs in $\mathcal{O}(n^3)$. + +##Implementation + +```cpp +const double EPS = 1E-9; + +int compute_rank(vector> A) { + int n = A.size(); + int m = A[0].size(); + + int rank = max(n, m); + vector row_selected(n, false); + for (int i = 0; i < m; ++i) { + int j; + for (j = 0; j < n; ++j) { + if (!row_selected[j] && abs(A[j][i]) > EPS) + break; + } + + if (j == n) { + --rank; + } else { + row_selected[j] = true; + for (int p = i + 1; p < m; ++p) + A[j][p] /= A[j][i]; + for (int k = 0; k < n; ++k) { + if (k != j && abs(A[k][i]) > EPS) { + for (int p = i + 1; p < m; ++p) + A[k][p] -= A[j][p] * A[k][i]; + } + } + } + } + return rank; +} +``` 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