### Question:
DenseVectors { public static double sim(float[] left, float[] right) { assert left.length == N; assert right.length == N; double ab = 0.0, aa = 0.0, bb = 0.0; for (int i=0; i<Math.min(left.length, right.length); i++) { ab += left [i] * right[i]; aa += left [i] * left [i]; bb += right[i] * right[i]; } if (aa == 0.0 || bb == 0.0) return 0; else return ab / (Math.sqrt(aa) * Math.sqrt(bb)); }  static Optional<float[]> vectorFor(String word); static double sim(float[] left, float[] right); static double sim(Optional<float[]> left, Optional<float[]> right); static float[] mean(List<float[]> vecs); static float[] logproduct(List<float[]> vecs); static final int N; }### Answer:
@Test public void testSim() { assertEquals(sim(vectorFor("diabetes"), vectorFor("retinopathy")), 0.54, 0.01); assertEquals(sim(vectorFor("diabetes"), vectorFor("diabetic")), 0.78, 0.01); assertEquals(sim(vectorFor("(*&(*&^(*&^"), vectorFor("diabetic")), 0.00, 0.01); assertEquals(sim(vectorFor("diabetes"), vectorFor("")), 0.00, 0.01); assertEquals(sim(vectorFor("diabetes"), Optional.of(new float[300])), 0.00, 0.01); float[] X = new float[300]; X[0] = (float) 0.5; float[] Y = new float[300]; Y[1] = (float) 0.5; float[] Z = new float[300]; Z[0] = (float) 0.5; Z[1] = (float) 0.5; assertEquals(sim(X, Y), 0.0, 0.01); assertEquals(sim(X, Z), 0.707, 0.01); assertEquals(sim(X, X), 1.0, 0.01); }