I frequently work with 2D matrices, and frequently find myself having to check validity (validity depends on the use case, but for the purpose of this question, it may not matter) of rows and columns. In such cases the validity of a row/column is the defined the same way for rows as it is for columns.
I usually end up writing 2 utility functions:
bool check_rows_valid() {
for (int i = 0; i < NUM_ROWS; ++i) {
// check row i, mat[i] is valid depending on how validity is defined
}
}
bool check_cols_valid() {
for (int j = 0; j < NUM_COLS; ++j) {
// check column j mat[:][j] is valid depending on how validity is defined
}
}
So I end up writing essentially the same utility for column and row. Is there a cleaner way to do this with a single utility function that can cover both rows & columns?
That looks good, but to avoid a large code block in the for loops, try doing something similar to this example:
Then, operate on the
Matrixobject in the case of checking row validity, and operate on theInverseMatrixobject in the case of checking column validity.NOTE: This code is not complete! This is just a model to illustrate the point and you need to provide the rest yourself, so I did not try as hard to rigorously hold to C++ standards, especially since this problem is language agnostic.