Matrix Comparison and Relational Operations
Learn about matrix comparison and relational operations.
In programming, relation and comparison are two terms that are often used interchangeably, but they have slightly different meanings:
Relation: A relation is a general term used to describe any relationship between two or more values, such as equality, inequality, less than, greater than, and so on.
Comparison: A comparison is a specific type of relation that involves testing the relationship between two values to determine their relative order, such as whether one value is greater than, less than, or equal to another value.
In other words, all comparisons are relations, but not all relations are comparisons. For example, the “equal to” relation is a comparison because it tests whether two values are equal or not. However, the “not equal to” relation is not a comparison because it simply tests for inequality without specifying any particular order between the two values. Other relations that are not comparisons include the “is a subset of” and the “implies” relations.
In programming, the following operators are often used to compare values:
<
>
<=
>=
==
!=
However, other relations can also be used in different contexts, depending on the program’s needs.
Exercise playground
4 2 2 1 2 4 5 2 3 1 2 3 4 5 4 2 2 1 2 4 5 5 3 4 6 5 3 3 4 5 8 9 4 5 3 7 2 3
We’ll now explore and provide an opportunity for you to exercise and practice implementing these operators in the given playground. We’ll define each operator based on our requirements for matrices and proceed to implement them step by step.
The <
operator
Next, we’ll define the operator <
, which allows us to check if a matrix is a proper subset of another matrix.
The implementation of this operator involves performing sliding window comparisons to check for matching elements. If a proper subset relationship is found, the operator returns true
; otherwise, it returns false
.
Here’s a complete implementation of the <
operator:
bool Matrix::operator<(const Matrix& M)const{if(this->rows > M.rows || this->cols > M.cols)return false;if(this->rows == M.rows && this->cols == M.cols)return false;// If the Left matrix has dimensions greater// than M, then there’s no need to check the proper subset possibility// Coming here means: The Dimension(*this)<=Dimension(M)for(int ri=0; ri<=M.rows-this->rows; ri++){ // How many times should it slide?// downward (row-wise): = M.rows-this->row + 1 timesfor(int ci=0; ci<=M.cols-this->cols; ci++){// Now (ri, ci) is the anchored position where we need to check// whether the left matrix is inside the right matrixbool keepChecking = true, found = true;for(int r=0; r<this->rows; r++){for(int c=0; (c<this->cols) && keepChecking; c++){if(M[ri+r][ci+c] != (*this)[r][c]){keepChecking = false;found = false;}}}if(found)return true;}}return false;}
-
Line 1: The
operator<()
function takesconst Matrix& M
as a parameter, representing the matrix to be compared with the current matrix. -
Lines 3–6: The implementation checks if the number of rows or columns in the current matrix is greater than or equal to the corresponding dimensions of matrix
M
. If either of these conditions is met, it indicates that the current matrix cannot be a proper subset ofM
. In such cases, the function immediately returnsfalse
. Additionally, if both matrices have equal dimensions, implying they are identical, there is no need to check for a proper subset relationship. Therefore, the function should also returnfalse
in this scenario. -
Line 11–31: The outermost loop (
ri
) controls the vertical position of the sliding window. It iterates from0
toM.rows - this->rows
, ensuring that the window does not go beyond the vertical boundaries of the larger matrix.-
Similarly, the inner loop (
ci
) controls the horizontal position of the sliding window. It iterates from0
toM.cols - this->cols
, ensuring that the window does not exceed the horizontal boundaries of the larger matrix. -
Within the nested loops, each position of the sliding window is considered the anchor position. The elements of the current matrix are compared with the corresponding elements in the submatrix of the larger matrix. If all the elements match, a proper subset relationship is found, and the function returns
true
. If no match is found, the function continues to slide the window and check other positions. -
In the innermost two loops, the variables
keepChecking
andfound
are used to track the progress of the subset check. ThekeepChecking
variable ensures that the loop continues only if no mismatches are found, improving efficiency by avoiding unnecessary iterations. If a mismatch is encountered,keepChecking
is set tofalse
, causing the loop to terminate. Thefound
variable indicates whether the current matrix is a proper subset, remainingtrue
if no mismatches are found. If found istrue
, the function returnstrue
, indicating a successful subset match. These variables work together to control the loop iterations and determine the subset relationship between matrices.
-
Write the above code in the Exercise playground in Matrix.cpp
and uncomment to test the correct output.
The ==
operator
The ==
operator for the Matrix
class checks if the dimensions of the two matrices are the same. If they are, it iterates over each element and compares the corresponding elements of the matrices. If all elements are equal, it returns true
. Otherwise, it returns false
.
Write your own code in the Exercise playground. Try to correctly implement the function. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
Exercise: Implemment the <=
operator
The operator <=
checks if the current matrix is less than or equal to another matrix M
. It combines the comparisons using operator<
and operator==
to determine if the current matrix is either a proper subset of M
or equal to M
. If either condition is true
, the function returns true
; otherwise, it returns false
.
Write your own code in the Exercise playground. Try to correctly implement the function. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
Now that we’ve implemented the <
and ==
operators, we can leverage the power of reusability to implement the remaining operators by utilizing these two operators.
Exercise: Implement the >
operator
The operator >
checks if the current matrix is a superset containing the other matrix.
One possibility is to write the entire operator function all over again. But we can look at the same problem in another way if is equivalent to . So, all we need to do is to reuse the operator<()
function with flipped order.
Write your own code in the Exercise playground. Try to correctly implement the function. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
Exercise: Implement >=
operator
The >=
operator for the Matrix
class is equivalent to having two sets and , and checking wether . In terms of matrices, what we can assume is that the left matrix contains the other matrix (even if they could be the same matrices).
Hint: We can reuse the two implementations we’ve already done.
Write your own code in theExercise playground. Try to correctly implement the function. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
Exercise: Implement the !=
operator
The !=
operator for the Matrix
class checks if two matrices are not equal.
Hint: This can be done by utilizing the equality operator
==
and negating its result using the logical NOT operator (!
). If the matrices are not equal, it returnstrue
, indicating inequality. If the matrices are equal, it returnsfalse
, indicating equality.
Write your own code in the Exercise playground. Try to correctly implement the function. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.