MinHashLSH extends BaseLSH { @Override public DenseVector hashFunction(Vector vec) { double[] minHashSet = new double[randCoefficientsA.length]; if (randCoefficientsA.length > 0) { int[] hashValues = new int[randCoefficientsA[0].length]; if (vec instanceof SparseVector) { SparseVector elem = (SparseVector)vec; int[] indices = elem.getIndices(); for (int i = 0; i < minHashSet.length; i++) { for (int j = 0; j < hashValues.length; j++) { int tmp = HASH_PRIME, cur; for (int index : indices) { cur = (int)((1L + index) * randCoefficientsA[i][j] + randCoefficientsB[i][j]) % HASH_PRIME; tmp = Math.min(tmp, cur); } hashValues[j] = tmp; } minHashSet[i] = tableHash(hashValues); } } else if (vec instanceof DenseVector) { double[] elem = ((DenseVector)vec).getData(); for (int i = 0; i < minHashSet.length; i++) { for (int j = 0; j < hashValues.length; j++) { int tmp = HASH_PRIME, cur; for (int m = 0; m < elem.length; m++) { if (elem[m] != 0) { cur = (int)((1L + m) * randCoefficientsA[i][j] + randCoefficientsB[i][j]) % HASH_PRIME; tmp = Math.min(tmp, cur); } } hashValues[j] = tmp; } minHashSet[i] = tableHash(hashValues); } } } return new DenseVector(minHashSet); } MinHashLSH(long seed, int numProjectionsPerTable, int numHashTables);  MinHashLSH(int[][] randCoefficientsA, int[][] randCoefficientsB); @Override DenseVector hashFunction(Vector vec);  }
@Test public void testHashFunction() { MinHashLSH lsh = new MinHashLSH(0, 2, 2); Vector vec1 = new DenseVector(new double[] {1, 2, 3, 4, 5}); Assert.assertEquals(new DenseVector(new double[] {478212008, -1798305157}), lsh.hashFunction(vec1)); Vector vec2 = new SparseVector(5, new int[] {0, 4}, new double[] {1.0, 4.0}); Assert.assertEquals(new DenseVector(new double[] {-967745172, -594675602}), lsh.hashFunction(vec2)); }