FixedCosineSimilarity implements SimilarityFunction { public double distance(Map<String, Double> vector) { double magnitude = CosineSimilarity.magnitude(vector); return (magnitude == 0.0 || fixedMagnitude == 0) ? 0.0 : CosineSimilarity.dotProduct( vector, this.fixedVector) / (magnitude * fixedMagnitude); } FixedCosineSimilarity(Map<String, Double> fixedVector); double distance(Map<String, Double> vector); @Override double distance(Map<String, Double> vector1, Map<String, Double> vector2);  }
@Test public void testBasic() throws Exception { Map<String, Double> vector1 = new HashMap<String, Double>(); Map<String, Double> vector2 = new HashMap<String, Double>(); vector1.put("a", 2d); vector1.put("b", 3d); vector1.put("c", 4d); vector1.put("d", 5d); vector2.put("a", 2d); vector2.put("b", 3d); vector2.put("d", 4d); vector2.put("e", 6d); SimilarityFunction similarity = new FixedCosineSimilarity(vector1); assertEquals(0.557006652, similarity.distance(vector2, vector1), 0.0001d); assertEquals(0.557006652, ((FixedCosineSimilarity) similarity).distance(vector2), 0.0001d); }