// Implementation of class Matrix #include #include // #include "Matrix.h" #include "stdMatrix.h" using namespace std; // Row echelon form int Matrix::gauss(double eps /* = 1e-8 */) { // Return rank of matrix int i = 0; int j = 0; while (i < m && j < n) { // Find the maximal element in j-th column int indMax = i; for (int k = i+1; k < m; ++k) { if (fabs(at(k, j)) > fabs(at(indMax, j))) { indMax = k; } } if (fabs(at(indMax, j)) <= eps) { // Zero column for (int k = i; k < m; ++k) at(k, j) = 0.; ++j; continue; } // Maximal element is in row indMax if (indMax != i) { // Swap rows i, indMax for (int k = j; k < n; ++k) { double tmp = at(i, k); at(i, k) = at(indMax, k); at(indMax, k) = (-tmp); } } // Now, the maximal element in j-th column is a_ij for (int k = i+1; k < m; ++k) { // Subtract i-th row from k-th row double r = at(k, j)/at(i, j); at(k, j) = 0.; for (int l = j+1; l < n; ++l) { at(k, l) -= at(i, l)*r; } } ++i; ++j; } // i == rank of matrix return i; } ostream& operator<<(ostream& s, const Matrix& matrix) { for (int i = 0; i < matrix.rows(); ++i) { for (int j = 0; j < matrix.cols(); ++j) { if (j > 0) s << " "; s << matrix.at(i, j); } s << endl; } return s; } istream& operator>>(istream& s, Matrix& matrix) { for (int i = 0; i < matrix.rows(); ++i) { for (int j = 0; j < matrix.cols(); ++j) { s >> matrix.at(i, j); if (!s.good()) return s; } } return s; }