Skip to content

Commit f9369b4

Browse files
committed
addressing some comments from #3677
1 parent c77bc5f commit f9369b4

File tree

7 files changed

+67
-37
lines changed

7 files changed

+67
-37
lines changed

fdb-extensions/src/main/java/com/apple/foundationdb/async/hnsw/AbstractNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected AbstractNode(@Nonnull final Tuple primaryKey,
6161

6262
/**
6363
* Gets the primary key that uniquely identifies this object.
64-
* @return the primary key {@link Tuple}, which will never be {@code null}.
64+
* @return the primary key {@link Tuple}, which will never be {@code null}.
6565
*/
6666
@Nonnull
6767
@Override

fdb-extensions/src/main/java/com/apple/foundationdb/linear/ColumnMajorRealMatrix.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public ColumnMajorRealMatrix(@Nonnull final double[][] data) {
4141
}
4242

4343
@Nonnull
44-
@Override
45-
public double[][] getData() {
44+
private double[][] getData() {
4645
return data;
4746
}
4847

@@ -68,7 +67,7 @@ public double[] getColumn(final int column) {
6867

6968
@Nonnull
7069
@Override
71-
public RealMatrix transpose() {
70+
public ColumnMajorRealMatrix transpose() {
7271
int n = getRowDimension();
7372
int m = getColumnDimension();
7473
double[][] result = new double[n][m];
@@ -82,7 +81,7 @@ public RealMatrix transpose() {
8281

8382
@Nonnull
8483
@Override
85-
public RealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
84+
public ColumnMajorRealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
8685
Preconditions.checkArgument(getColumnDimension() == otherMatrix.getRowDimension());
8786
int n = getRowDimension();
8887
int m = otherMatrix.getColumnDimension();
@@ -100,7 +99,8 @@ public RealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
10099

101100
@Nonnull
102101
@Override
103-
public RealMatrix subMatrix(final int startRow, final int lengthRow, final int startColumn, final int lengthColumn) {
102+
public ColumnMajorRealMatrix subMatrix(final int startRow, final int lengthRow,
103+
final int startColumn, final int lengthColumn) {
104104
final double[][] subData = new double[lengthColumn][lengthRow];
105105

106106
for (int j = startColumn; j < startColumn + lengthColumn; j ++) {
@@ -113,7 +113,13 @@ public RealMatrix subMatrix(final int startRow, final int lengthRow, final int s
113113
@Nonnull
114114
@Override
115115
public RowMajorRealMatrix toRowMajor() {
116-
return new RowMajorRealMatrix(transpose().getData());
116+
return new RowMajorRealMatrix(getRowMajorData());
117+
}
118+
119+
@Nonnull
120+
@Override
121+
public double[][] getRowMajorData() {
122+
return transpose().getData();
117123
}
118124

119125
@Nonnull
@@ -124,10 +130,22 @@ public ColumnMajorRealMatrix toColumnMajor() {
124130

125131
@Nonnull
126132
@Override
127-
public RealMatrix quickTranspose() {
133+
public double[][] getColumnMajorData() {
134+
return getData();
135+
}
136+
137+
@Nonnull
138+
@Override
139+
public RowMajorRealMatrix quickTranspose() {
128140
return new RowMajorRealMatrix(data);
129141
}
130142

143+
@Nonnull
144+
@Override
145+
public RowMajorRealMatrix flipMajor() {
146+
return (RowMajorRealMatrix)RealMatrix.super.flipMajor();
147+
}
148+
131149
@Override
132150
public final boolean equals(final Object o) {
133151
if (o instanceof ColumnMajorRealMatrix) {

fdb-extensions/src/main/java/com/apple/foundationdb/linear/QRDecomposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static Result decomposeMatrix(@Nonnull final RealMatrix matrix) {
6363
Preconditions.checkArgument(matrix.isSquare());
6464

6565
final double[] rDiagonal = new double[matrix.getRowDimension()];
66-
final double[][] qrt = matrix.toRowMajor().transpose().getData();
66+
final double[][] qrt = matrix.transpose().getRowMajorData();
6767

6868
for (int minor = 0; minor < matrix.getRowDimension(); minor++) {
6969
performHouseholderReflection(minor, qrt, rDiagonal);

fdb-extensions/src/main/java/com/apple/foundationdb/linear/RealMatrix.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import javax.annotation.Nullable;
2727

2828
public interface RealMatrix extends LinearOperator {
29-
@Nonnull
30-
double[][] getData();
31-
3229
double getEntry(int row, int column);
3330

3431
@Override
@@ -78,12 +75,23 @@ default RealVector operateTranspose(@Nonnull final RealVector vector) {
7875
@Nonnull
7976
RowMajorRealMatrix toRowMajor();
8077

78+
@Nonnull
79+
double[][] getRowMajorData();
80+
8181
@Nonnull
8282
ColumnMajorRealMatrix toColumnMajor();
8383

84+
@Nonnull
85+
double[][] getColumnMajorData();
86+
8487
@Nonnull
8588
RealMatrix quickTranspose();
8689

90+
@Nonnull
91+
default RealMatrix flipMajor() {
92+
return transpose().quickTranspose();
93+
}
94+
8795
default boolean valueEquals(@Nullable final Object o) {
8896
if (!(o instanceof RealMatrix)) {
8997
return false;

fdb-extensions/src/main/java/com/apple/foundationdb/linear/RowMajorRealMatrix.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public RowMajorRealMatrix(@Nonnull final double[][] data) {
4141
}
4242

4343
@Nonnull
44-
@Override
45-
public double[][] getData() {
44+
private double[][] getData() {
4645
return data;
4746
}
4847

@@ -68,7 +67,7 @@ public double[] getRow(final int row) {
6867

6968
@Nonnull
7069
@Override
71-
public RealMatrix transpose() {
70+
public RowMajorRealMatrix transpose() {
7271
int n = getRowDimension();
7372
int m = getColumnDimension();
7473
double[][] result = new double[m][n];
@@ -82,7 +81,7 @@ public RealMatrix transpose() {
8281

8382
@Nonnull
8483
@Override
85-
public RealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
84+
public RowMajorRealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
8685
Preconditions.checkArgument(getColumnDimension() == otherMatrix.getRowDimension());
8786
final int n = getRowDimension();
8887
final int m = otherMatrix.getColumnDimension();
@@ -100,7 +99,8 @@ public RealMatrix multiply(@Nonnull final RealMatrix otherMatrix) {
10099

101100
@Nonnull
102101
@Override
103-
public RealMatrix subMatrix(final int startRow, final int lengthRow, final int startColumn, final int lengthColumn) {
102+
public RowMajorRealMatrix subMatrix(final int startRow, final int lengthRow,
103+
final int startColumn, final int lengthColumn) {
104104
final double[][] subData = new double[lengthRow][lengthColumn];
105105

106106
for (int i = startRow; i < startRow + lengthRow; i ++) {
@@ -116,18 +116,36 @@ public RowMajorRealMatrix toRowMajor() {
116116
return this;
117117
}
118118

119+
@Nonnull
120+
@Override
121+
public double[][] getRowMajorData() {
122+
return getData();
123+
}
124+
119125
@Nonnull
120126
@Override
121127
public ColumnMajorRealMatrix toColumnMajor() {
122-
return new ColumnMajorRealMatrix(transpose().getData());
128+
return new ColumnMajorRealMatrix(getColumnMajorData());
123129
}
124130

125131
@Nonnull
126132
@Override
127-
public RealMatrix quickTranspose() {
133+
public double[][] getColumnMajorData() {
134+
return transpose().getData();
135+
}
136+
137+
@Nonnull
138+
@Override
139+
public ColumnMajorRealMatrix quickTranspose() {
128140
return new ColumnMajorRealMatrix(data);
129141
}
130142

143+
@Nonnull
144+
@Override
145+
public ColumnMajorRealMatrix flipMajor() {
146+
return (ColumnMajorRealMatrix)RealMatrix.super.flipMajor();
147+
}
148+
131149
@Override
132150
public final boolean equals(final Object o) {
133151
if (o instanceof RowMajorRealMatrix) {

fdb-extensions/src/test/java/com/apple/foundationdb/linear/FhtKacRotatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void testRotationIsStable(final long seed, final int numDimensions) {
7474
@MethodSource("randomSeedsWithNumDimensions")
7575
void testOrthogonality(final long seed, final int numDimensions) {
7676
final FhtKacRotator rotator = new FhtKacRotator(seed, numDimensions, 10);
77-
final ColumnMajorRealMatrix p = new ColumnMajorRealMatrix(rotator.computeP().transpose().getData());
77+
final ColumnMajorRealMatrix p = rotator.computeP().transpose().quickTranspose();
7878

7979
for (int j = 0; j < numDimensions; j ++) {
8080
final RealVector rotated = rotator.operateTranspose(new DoubleRealVector(p.getColumn(j)));

fdb-extensions/src/test/java/com/apple/foundationdb/linear/RealMatrixTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void testTranspose(final long seed, final int numDimensions) {
4848
final int numRows = random.nextInt(numDimensions) + 1;
4949
final int numColumns = random.nextInt(numDimensions) + 1;
5050
final RealMatrix matrix = MatrixHelpers.randomGaussianMatrix(random, numRows, numColumns);
51-
final RealMatrix otherMatrix = flip(matrix);
51+
final RealMatrix otherMatrix = matrix.flipMajor();
5252
assertThat(otherMatrix).isEqualTo(matrix);
53-
final RealMatrix anotherMatrix = flip(otherMatrix);
53+
final RealMatrix anotherMatrix = otherMatrix.flipMajor();
5454
assertThat(anotherMatrix).isEqualTo(otherMatrix);
5555
assertThat(anotherMatrix).isEqualTo(matrix);
5656
assertThat(anotherMatrix.getClass()).isSameAs(matrix.getClass());
@@ -87,20 +87,6 @@ void testDifferentMajor(final long seed, final int numDimensions) {
8787
assertThat(anotherMatrix).isEqualTo(matrix);
8888
}
8989

90-
91-
@Nonnull
92-
private static RealMatrix flip(@Nonnull final RealMatrix matrix) {
93-
assertThat(matrix)
94-
.satisfiesAnyOf(m -> assertThat(m).isInstanceOf(RowMajorRealMatrix.class),
95-
m -> assertThat(m).isInstanceOf(ColumnMajorRealMatrix.class));
96-
final double[][] data = matrix.transpose().getData();
97-
if (matrix instanceof RowMajorRealMatrix) {
98-
return new ColumnMajorRealMatrix(data);
99-
} else {
100-
return new RowMajorRealMatrix(data);
101-
}
102-
}
103-
10490
@ParameterizedTest
10591
@MethodSource("randomSeedsWithNumDimensions")
10692
void testOperateAndBack(final long seed, final int numDimensions) {
@@ -141,7 +127,7 @@ void testMultiplyRowMajorMatrix(final long seed, final int d) {
141127
@MethodSource("randomSeedsWithNumDimensions")
142128
void testMultiplyColumnMajorMatrix(final long seed, final int d) {
143129
final Random random = new Random(seed);
144-
final RealMatrix r = flip(MatrixHelpers.randomOrthogonalMatrix(random, d));
130+
final RealMatrix r = MatrixHelpers.randomOrthogonalMatrix(random, d).flipMajor();
145131
assertMultiplyMxMT(d, random, r);
146132
}
147133

0 commit comments

Comments
 (0)