Fixed some bugs in span and rank and added a print method

parent 5fa47d72
......@@ -111,7 +111,7 @@ public:
};
/// Transforms the matrix to reduced row echelon form
Matrix echelon() const {
Matrix echelonForm() const {
Matrix result = Matrix(*this);
int pivot = 0;
for (int row = 0; row < height; row++) {
......@@ -136,7 +136,7 @@ public:
/// The rank of the matrix
int getRank() const {
Matrix e = echelon();
Matrix e = echelonForm();
int rank = 0;
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
......@@ -153,12 +153,13 @@ public:
/// Basis of the linear span of the column vectors
static Matrix<T> span(Matrix<T> const& matrix) {
vector<vector<T>> columns;
int rank = matrix.getRank();
for (int col = 0; col < rank; col++) {
columns.push_back(matrix.column(col));
vector<vector<T>> rows;
Matrix<T> te = matrix.transpose().echelonForm();
int rank = te.getRank();
for (int row = 0; row < rank; row++) {
rows.push_back(te.row(row));
}
return Matrix(columns).transpose();
return Matrix(rows).transpose();
}
/// Computes the null space for the column vectors
......@@ -215,6 +216,11 @@ public:
return vectors[i];
};
vector<T> row(int i) const {
assert(i < getHeight());
return vectors[i];
};
/// 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 {
......@@ -306,6 +312,10 @@ public:
return rhs.vectors == rhs.vectors && width == rhs.width && height == rhs.height;
};
void print() const {
dbgs(4) << *this;
}
protected:
// MARK: - Echelon helpers
......
......@@ -165,7 +165,7 @@ bool MatrixTest<T>::runTestEchelon1() {
};
auto matrix = Matrix(a);
auto actual = matrix.echelon();
auto actual = matrix.echelonForm();
auto x = Matrix(expected);
result = actual == x;
......@@ -192,7 +192,7 @@ bool MatrixTest<T>::runTestEchelon2() {
};
auto matrix = Matrix(a);
auto actual = matrix.echelon();
auto actual = matrix.echelonForm();
auto x = Matrix(expected);
result = actual == x;
......@@ -219,7 +219,7 @@ bool MatrixTest<T>::runTestEchelon3() {
};
auto matrix = Matrix(a);
auto actual = matrix.echelon();
auto actual = matrix.echelonForm();
auto x = Matrix(expected);
result = actual == x;
......
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