Verified Commit 9e842c6d authored by Tim Gymnich's avatar Tim Gymnich
Browse files

add getRank() and span()

parent 9b53b2cd
......@@ -105,15 +105,38 @@ public:
return result;
};
/// The rank of the matrix
int getRank() const {
Matrix e = echelon();
int rank = 0;
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
if ((e.vectors[row][column] == 0) && (column == width - 1)) {
return rank;
} else if (e.vectors[row][column] != 0) {
break;
}
}
rank++;
}
return rank;
}
/// Linear span of the matrix ... fixme
Matrix span() const;
Matrix span() const {
vector<vector<T>> columns;
int rank = getRank();
for (int col = 0; col<rank; col++) {
columns.push_back(column(col));
}
return Matrix(columns).transpose();
}
/// Returns a vector with the elements of the row at index i. The returned row cannot be modified.
/// Returns a vector with the elements of the row at index i. The returned row can be modified.
/// @param i Index of the row to return.
vector<T>& row(int i) { return vectors[i]; };
/// Returns the column at index i. The returned column can be modified.
/// Returns the column at index i. The returned column cannot be modified.
/// @param i Index of the column to return
vector<T> column(int i) const {
vector<T> row;
......
......@@ -20,6 +20,10 @@ public:
static bool runTestEchelon1();
static bool runTestEchelon2();
static bool runTestEchelon3();
static bool runTestRank1();
static bool runTestRank2();
static bool runTestRank3();
static bool runTestSpan1();
};
template <typename T>
......@@ -224,6 +228,98 @@ bool MatrixTest<T>::runTestEchelon3() {
return result;
}
template<typename T>
bool MatrixTest<T>::runTestRank1() {
std::cout << "Testing rank 1: ";
bool result = false;
std::vector<std::vector<T>> a = {
{1,4,7},
{2,5,8},
{3,6,9}
};
int expected = 2;
auto matrix = Matrix(a);
auto actual = matrix.getRank();
result = actual == expected;
std::cout << (result? "success" : "failed") << "\n";
return result;
}
template<typename T>
bool MatrixTest<T>::runTestRank2() {
std::cout << "Testing rank 2: ";
bool result = false;
std::vector<std::vector<T>> a = {
{1,2,4},
{2,4,8},
{4,8,16}
};
int expected = 1;
auto matrix = Matrix(a);
auto actual = matrix.getRank();
result = actual == expected;
std::cout << (result? "success" : "failed") << "\n";
return result;
}
template<typename T>
bool MatrixTest<T>::runTestRank3() {
std::cout << "Testing rank 3: ";
bool result = false;
std::vector<std::vector<T>> a = {
{1,2,1},
{1,4,8},
{1,6,3}
};
int expected = 3;
auto matrix = Matrix(a);
auto actual = matrix.getRank();
result = actual == expected;
std::cout << (result? "success" : "failed") << "\n";
return result;
}
template<typename T>
bool MatrixTest<T>::runTestSpan1() {
std::cout << "Testing span 1: ";
bool result = false;
std::vector<std::vector<T>> a = {
{1,1,4},
{0,1,4},
{1,0,0}
};
std::vector<std::vector<T>> expected = {
{1,1},
{0,1},
{1,0}
};
auto matrix = Matrix(a);
auto actual = matrix.span();
result = actual == Matrix(expected);
std::cout << (result? "success" : "failed") << "\n";
return result;
}
int main() {
return !(MatrixTest<int>::runTestMul1()
......@@ -233,6 +329,10 @@ int main() {
&& MatrixTest<int>::runTestEchelon1()
&& MatrixTest<int>::runTestEchelon2()
&& MatrixTest<int>::runTestEchelon3()
&& MatrixTest<int>::runTestRank1()
&& MatrixTest<int>::runTestRank2()
&& MatrixTest<int>::runTestRank3()
&& MatrixTest<int>::runTestSpan1()
);
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment