Skip to content

Commit

Permalink
Make equals work properly - the exception is converted to a runtime e…
Browse files Browse the repository at this point in the history
…xception, maybe return false is better.
  • Loading branch information
johnmay authored and egonw committed Feb 15, 2022
1 parent c6b9bc6 commit 1ec73c2
Showing 1 changed file with 19 additions and 4 deletions.
Expand Up @@ -23,6 +23,9 @@
import org.openscience.cdk.graph.invariant.exception.IndexOutOfBoundsException;
import org.openscience.cdk.graph.invariant.exception.MatrixNotInvertibleException;

import java.util.Arrays;
import java.util.Objects;

/**
* This class is intended to provide the user an efficient way of implementing matrix of double number and
* using normal operations (linear operations, addition, subtraction, multiplication, inversion, concatenation)
Expand Down Expand Up @@ -321,19 +324,31 @@ public static GIMatrix zero(int m, int n) {
/**
* Verifies if two given matrix are equal or not. The matrix must be of the same size and dimensions,
* otherwise an exception will be thrown.
* @param matrix the Matrix object to be compared to
* @param obj the Matrix object to be compared to
* @return true if both matrix are equal element to element
* @exception BadMatrixFormatException if the given matrix doesn't have the same dimensions as this one
*/
public boolean equals(GIMatrix matrix) throws BadMatrixFormatException {
if ((height() != matrix.height()) || (width() != matrix.width())) throw new BadMatrixFormatException();
@Override
public boolean equals(Object obj) {
if (!(obj instanceof GIMatrix))
return false;
GIMatrix matrix = (GIMatrix) obj;
if ((height() != matrix.height()) || (width() != matrix.width()))
throw new RuntimeException(new BadMatrixFormatException());
// return false?
double[][] temp = matrix.getArrayValue();
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (!(array[i][j] == temp[i][j])) return false;
return true;
} // method equals(Matrix)

@Override
public int hashCode() {
int result = Objects.hash(m, n);
result = 31 * result + Arrays.hashCode(array);
return result;
}

/**
* Verifies if the matrix is square, that is if it has an equal number of lines and columns.
* @return true if this matrix is square
Expand Down

0 comments on commit 1ec73c2

Please sign in to comment.