From 86b340302169a117217a68566b16c48363344132 Mon Sep 17 00:00:00 2001 From: Daili01 Date: Tue, 2 Oct 2018 19:58:01 +0200 Subject: [PATCH 1/4] Create rank-matrix.md --- src/linear_algebra/rank-matrix.md | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/linear_algebra/rank-matrix.md diff --git a/src/linear_algebra/rank-matrix.md b/src/linear_algebra/rank-matrix.md new file mode 100644 index 000000000..908a9807a --- /dev/null +++ b/src/linear_algebra/rank-matrix.md @@ -0,0 +1,37 @@ + + +#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; let the matrix be rectangular and have size $N \times M$. + +The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix. + +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](https://cp-algorithms.com/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 zero-rows among the rows not selected before, 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 at 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. + +##Implementation + + const double EPS = 1E-9; + + int rank = max(n,m); + vector line_used (n); + for (int i=0; i EPS) + break; + if (j == n) + --rank; + else { + line_used[j] = true; + for (int p=i+1; p EPS) + for (int p=i+1; p Date: Tue, 2 Oct 2018 20:02:45 +0200 Subject: [PATCH 2/4] Added complexity --- src/linear_algebra/rank-matrix.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/linear_algebra/rank-matrix.md b/src/linear_algebra/rank-matrix.md index 908a9807a..ff7fc4e31 100644 --- a/src/linear_algebra/rank-matrix.md +++ b/src/linear_algebra/rank-matrix.md @@ -12,6 +12,10 @@ Note that if the matrix is square and its determinant is non-zero, then the rank You can search for the rank using [Gaussian elimination](https://cp-algorithms.com/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 zero-rows among the rows not selected before, 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 at 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 const double EPS = 1E-9; From 599bbfcbeb1b94b3662e93060a34bb4facb44623 Mon Sep 17 00:00:00 2001 From: Daili01 Date: Tue, 2 Oct 2018 20:03:36 +0200 Subject: [PATCH 3/4] Added rank of a matrix --- src/index.md | 1 + 1 file changed, 1 insertion(+) 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) From ecb55657ff4fdd6316587a99ea501aa8db3d9672 Mon Sep 17 00:00:00 2001 From: Jakob Kogler Date: Tue, 2 Oct 2018 22:38:05 +0200 Subject: [PATCH 4/4] Better code formatting --- src/linear_algebra/rank-matrix.md | 58 +++++++++++++++++++------------ 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/linear_algebra/rank-matrix.md b/src/linear_algebra/rank-matrix.md index ff7fc4e31..6a7050a35 100644 --- a/src/linear_algebra/rank-matrix.md +++ b/src/linear_algebra/rank-matrix.md @@ -2,15 +2,17 @@ #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; let the matrix be rectangular and have size $N \times M$. +**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](https://cp-algorithms.com/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 zero-rows among the rows not selected before, 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 at 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. +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 @@ -18,24 +20,36 @@ This algorithm runs in $\mathcal{O}(n^3)$. ##Implementation - const double EPS = 1E-9; - - int rank = max(n,m); - vector line_used (n); - for (int i=0; i EPS) - break; - if (j == n) - --rank; - else { - line_used[j] = true; - for (int p=i+1; p EPS) - for (int p=i+1; p> 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