Skip to content

Commit 822ceb2

Browse files
author
Ferenc Gerlits
committed
Change equals() to approximatelyEquals()
Because it was not an equivalence relation and it was not compatible with the hashCode() method. Also to make it like the Vertex class.
1 parent ad0f23a commit 822ceb2

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

app/src/main/java/ferenc_gerlits/hypercube_viewer/FourMatrix.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import androidx.annotation.NonNull;
44

5-
import java.util.Arrays;
6-
75
public class FourMatrix {
86
private double[][] elements;
97

@@ -62,20 +60,17 @@ public double get(int i, int j) {
6260
}
6361
}
6462

65-
@Override
66-
public int hashCode() {
67-
return Arrays.hashCode(elements);
68-
}
69-
70-
@Override
71-
public boolean equals(Object o) {
72-
if (this == o) return true;
73-
if (o == null || getClass() != o.getClass()) return false;
74-
FourMatrix that = (FourMatrix) o;
63+
public boolean approximatelyEquals(FourMatrix other) {
64+
if (this == other) {
65+
return true;
66+
}
67+
if (other == null) {
68+
return false;
69+
}
7570

7671
for (int i = 0; i < 4; ++i) {
7772
for (int j = 0; j < 4; ++j) {
78-
double diff = this.elements[i][j] - that.elements[i][j];
73+
double diff = this.elements[i][j] - other.elements[i][j];
7974
if (Math.abs(diff) >= Utility.EPSILON) {
8075
return false;
8176
}

app/src/test/java/ferenc_gerlits/hypercube_viewer/FourMatrixTest.java

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
67

78
public class FourMatrixTest {
89

@@ -11,37 +12,41 @@ public class FourMatrixTest {
1112

1213
@Test
1314
public void fromTwoMatrixWithFixedCoordinates() {
14-
assertEquals(createMatrix(
15-
row(1, 0, 0, 0),
16-
row(0, 1, 0, 0),
17-
row(0, 0, SQRT_3_PER_2, -0.5),
18-
row(0, 0, 0.5, SQRT_3_PER_2)),
15+
assertTrue(matricesAreEqual(
16+
createMatrix(
17+
row(1, 0, 0, 0),
18+
row(0, 1, 0, 0),
19+
row(0, 0, SQRT_3_PER_2, -0.5),
20+
row(0, 0, 0.5, SQRT_3_PER_2)),
1921
FourMatrix.fromTwoMatrixWithFixedCoordinates(TwoMatrix.rotationBy(Math.PI / 6), 0, 1)
20-
);
21-
22-
assertEquals(createMatrix(
23-
row(SQRT_2_PER_2, 0, 0, -SQRT_2_PER_2),
24-
row(0, 1, 0, 0),
25-
row(0, 0, 1, 0),
26-
row(SQRT_2_PER_2, 0, 0, SQRT_2_PER_2)),
22+
));
23+
24+
assertTrue(matricesAreEqual(
25+
createMatrix(
26+
row(SQRT_2_PER_2, 0, 0, -SQRT_2_PER_2),
27+
row(0, 1, 0, 0),
28+
row(0, 0, 1, 0),
29+
row(SQRT_2_PER_2, 0, 0, SQRT_2_PER_2)),
2730
FourMatrix.fromTwoMatrixWithFixedCoordinates(TwoMatrix.rotationBy(Math.PI / 4), 1, 2)
28-
);
29-
30-
assertEquals(createMatrix(
31-
row(1, 0, 0, 0),
32-
row(0, 0.5, -SQRT_3_PER_2, 0),
33-
row(0, SQRT_3_PER_2, 0.5, 0),
34-
row(0, 0, 0, 1)),
31+
));
32+
33+
assertTrue(matricesAreEqual(
34+
createMatrix(
35+
row(1, 0, 0, 0),
36+
row(0, 0.5, -SQRT_3_PER_2, 0),
37+
row(0, SQRT_3_PER_2, 0.5, 0),
38+
row(0, 0, 0, 1)),
3539
FourMatrix.fromTwoMatrixWithFixedCoordinates(TwoMatrix.rotationBy(Math.PI / 3), 0, 3)
36-
);
37-
38-
assertEquals(createMatrix(
39-
row(0, -1, 0, 0),
40-
row(1, 0, 0, 0),
41-
row(0, 0, 1, 0),
42-
row(0, 0, 0, 1)),
40+
));
41+
42+
assertTrue(matricesAreEqual(
43+
createMatrix(
44+
row(0, -1, 0, 0),
45+
row(1, 0, 0, 0),
46+
row(0, 0, 1, 0),
47+
row(0, 0, 0, 1)),
4348
FourMatrix.fromTwoMatrixWithFixedCoordinates(TwoMatrix.rotationBy(Math.PI / 2), 2, 3)
44-
);
49+
));
4550
}
4651

4752
private FourMatrix createMatrix(double[]... rows) {
@@ -52,6 +57,10 @@ private double[] row(double... elements) {
5257
return elements;
5358
}
5459

60+
private boolean matricesAreEqual(FourMatrix first, FourMatrix second) {
61+
return first.approximatelyEquals(second);
62+
}
63+
5564
@Test
5665
public void times() {
5766
FourMatrix unitMatrix = createMatrix(
@@ -60,7 +69,7 @@ public void times() {
6069
row(0, 0, 1, 0),
6170
row(0, 0, 0, 1));
6271

63-
assertEquals(unitMatrix, unitMatrix.times(unitMatrix));
72+
assertTrue(matricesAreEqual(unitMatrix, unitMatrix.times(unitMatrix)));
6473

6574
FourMatrix someMatrix = createMatrix(
6675
row(1, 1, 1, 1),
@@ -74,16 +83,18 @@ public void times() {
7483
row(3, 0, 3, 0),
7584
row(4, 0, 4, 0));
7685

77-
assertEquals(someMatrix, someMatrix.times(unitMatrix));
86+
assertTrue(matricesAreEqual(someMatrix, someMatrix.times(unitMatrix)));
7887

79-
assertEquals(someOtherMatrix, unitMatrix.times(someOtherMatrix));
88+
assertTrue(matricesAreEqual(someOtherMatrix, unitMatrix.times(someOtherMatrix)));
8089

81-
assertEquals(createMatrix(
82-
row(7, 3, 7, 3),
83-
row(14, 6, 14, 6),
84-
row(21, 9, 21, 9),
85-
row(28, 12, 28, 12)),
86-
someMatrix.times(someOtherMatrix));
90+
assertTrue(matricesAreEqual(
91+
createMatrix(
92+
row(7, 3, 7, 3),
93+
row(14, 6, 14, 6),
94+
row(21, 9, 21, 9),
95+
row(28, 12, 28, 12)),
96+
someMatrix.times(someOtherMatrix)
97+
));
8798
}
8899

89100
@Test

0 commit comments

Comments
 (0)