add matrix and vector print functions#1214
Conversation
| namespace NetworKit { | ||
| // print functions for test debugging / output | ||
| inline std::ostream &operator<<(std::ostream &os, const DynamicMatrix &M) { | ||
| for (index i = 0; i < M.numberOfRows(); i++) { | ||
| if (i != 0) | ||
| os << std::endl; | ||
| for (index j = 0; j < M.numberOfColumns(); j++) { | ||
| if (j != 0) | ||
| os << ", "; | ||
| os << M(i, j); | ||
| } | ||
| } | ||
| return os; | ||
| } | ||
| } // namespace NetworKit |
There was a problem hiding this comment.
Since all matrix classes support a (row, col) operator, we should implement this function only once in a MatrixUtil.hpp, e.g.:
// MatrixUtil.hpp
template <class MatrixType>
std::ostream &printMatrix(std::ostream &ostream, const MatrixType& matrix) {
// ...
return os;
}There was a problem hiding this comment.
Also, I suggest using row and col instead of i and j, and use pre-increment instead of post-increment.
There was a problem hiding this comment.
renamed the variables.
Considering that there are many other duplicate functions in our three matrix classes, I think a more useful change would be to create a Matrix superclass for all of these functions, and define the print function in this superclass. That is a project for another time though and for now I think it is fine to implement the print function in all three classes.
There was a problem hiding this comment.
Yes having a Matrix superclass would be much better.
No description provided.