private void checkAnswer (SparseVector actual, double[] ans) { assertEquals ("Wrong number of locations:", ans.length, actual.numLocations()); for (int i = 0; i < actual.numLocations(); i++) { assertEquals ("Value incorrect at location "+i+": ", ans[i], actual.valueAtLocation (i) , 0.0); } }
private void checkAnswer (SparseVector actual, double[] ans) { assertEquals ("Wrong number of locations:", ans.length, actual.numLocations()); for (int i = 0; i < actual.numLocations(); i++) { assertEquals ("Value incorrect at location "+i+": ", ans[i], actual.valueAtLocation (i) , 0.0); } }
public void testDotProduct () { SparseVector t1 = new SparseVector (new int[] { 7 }, new double[] { 0.2 }); assertEquals (0.6, t1.dotProduct (s1), 0.00001); assertEquals (0.6, s1.dotProduct (t1), 0.00001); assertEquals (19.0, s1.dotProduct (s2), 0.00001); assertEquals (19.0, s2.dotProduct (s1), 0.00001); assertEquals (11.9, s1.dotProduct (d1), 0.00001); assertEquals (10.1, s2.dotProduct (d1), 0.00001); // test dotproduct when vector with more locations has a lower // max-index than short vector SparseVector t2 = new SparseVector (new int[] { 3, 30 }, new double[] { 0.2, 3.5 }); SparseVector t3 = new SparseVector (null, new double[] { 1, 1, 1, 1, }); assertEquals (0.2, t3.dotProduct (t2), 0.00001); }
public void testDotProduct () { SparseVector t1 = new SparseVector (new int[] { 7 }, new double[] { 0.2 }); assertEquals (0.6, t1.dotProduct (s1), 0.00001); assertEquals (0.6, s1.dotProduct (t1), 0.00001); assertEquals (19.0, s1.dotProduct (s2), 0.00001); assertEquals (19.0, s2.dotProduct (s1), 0.00001); assertEquals (11.9, s1.dotProduct (d1), 0.00001); assertEquals (10.1, s2.dotProduct (d1), 0.00001); // test dotproduct when vector with more locations has a lower // max-index than short vector SparseVector t2 = new SparseVector (new int[] { 3, 30 }, new double[] { 0.2, 3.5 }); SparseVector t3 = new SparseVector (null, new double[] { 1, 1, 1, 1, }); assertEquals (0.2, t3.dotProduct (t2), 0.00001); }
public void testPrint () { ByteArrayOutputStream baos = new ByteArrayOutputStream (); PrintStream out = new PrintStream (baos); PrintStream oldOut = System.out; System.setOut (out); SparseVector standard = new SparseVector (idxs, dbl2); standard.print (); assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.5\nSparseVector[7] = 2.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ()); baos.reset (); SparseVector dense = new SparseVector (null, dbl2); dense.print (); assertEquals ("SparseVector[0] = 1.0\nSparseVector[1] = 1.5\nSparseVector[2] = 2.0\nSparseVector[3] = 1.0\nSparseVector[4] = 1.0\n", baos.toString ()); baos.reset (); SparseVector binary = new SparseVector (idxs, null, idxs.length, idxs.length, false, false, false); binary.print (); assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.0\nSparseVector[7] = 1.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ()); baos.reset (); }
public void testPrint () { ByteArrayOutputStream baos = new ByteArrayOutputStream (); PrintStream out = new PrintStream (baos); PrintStream oldOut = System.out; System.setOut (out); SparseVector standard = new SparseVector (idxs, dbl2); standard.print (); assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.5\nSparseVector[7] = 2.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ()); baos.reset (); SparseVector dense = new SparseVector (null, dbl2); dense.print (); assertEquals ("SparseVector[0] = 1.0\nSparseVector[1] = 1.5\nSparseVector[2] = 2.0\nSparseVector[3] = 1.0\nSparseVector[4] = 1.0\n", baos.toString ()); baos.reset (); SparseVector binary = new SparseVector (idxs, null, idxs.length, idxs.length, false, false, false); binary.print (); assertEquals ("SparseVector[3] = 1.0\nSparseVector[5] = 1.0\nSparseVector[7] = 1.0\nSparseVector[13] = 1.0\nSparseVector[15] = 1.0\n", baos.toString ()); baos.reset (); }
public void testBinaryVector () { SparseVector binary1 = new SparseVector (idxs, null, idxs.length, idxs.length, false, false, false); SparseVector binary2 = new SparseVector (idx2, null, idx2.length, idx2.length, false, false, false); assertEquals (3, binary1.dotProduct (binary2), 0.0001); assertEquals (3, binary2.dotProduct (binary1), 0.0001); assertEquals (15.0, binary1.dotProduct (s1), 0.0001); assertEquals (15.0, s1.dotProduct (binary1), 0.0001); assertEquals (9.0, binary2.dotProduct (s1), 0.0001); assertEquals (9.0, s1.dotProduct (binary2), 0.0001); SparseVector dblVec = (SparseVector) s1.cloneMatrix (); dblVec.plusEqualsSparse (binary1); checkAnswer (dblVec, new double[] { 2, 3, 4, 5, 6 }); SparseVector dblVec2 = (SparseVector) s1.cloneMatrix (); dblVec2.plusEqualsSparse (binary2); checkAnswer (dblVec2, new double[] { 2, 2, 4, 4, 6 }); }
public void testBinaryVector () { SparseVector binary1 = new SparseVector (idxs, null, idxs.length, idxs.length, false, false, false); SparseVector binary2 = new SparseVector (idx2, null, idx2.length, idx2.length, false, false, false); assertEquals (3, binary1.dotProduct (binary2), 0.0001); assertEquals (3, binary2.dotProduct (binary1), 0.0001); assertEquals (15.0, binary1.dotProduct (s1), 0.0001); assertEquals (15.0, s1.dotProduct (binary1), 0.0001); assertEquals (9.0, binary2.dotProduct (s1), 0.0001); assertEquals (9.0, s1.dotProduct (binary2), 0.0001); SparseVector dblVec = (SparseVector) s1.cloneMatrix (); dblVec.plusEqualsSparse (binary1); checkAnswer (dblVec, new double[] { 2, 3, 4, 5, 6 }); SparseVector dblVec2 = (SparseVector) s1.cloneMatrix (); dblVec2.plusEqualsSparse (binary2); checkAnswer (dblVec2, new double[] { 2, 2, 4, 4, 6 }); }
s5.plusEqualsSparse (s1); for (int i = 0; i < s5.numLocations(); i++) { assertEquals (7, s5.indexAtLocation (i)); assertEquals (3.2, s5.valueAtLocation (i), 0.0); s6.plusEqualsSparse (s1, 3.5); for (int i = 0; i < s6.numLocations(); i++) { assertEquals (7, s6.indexAtLocation (i)); assertEquals (10.7, s6.valueAtLocation (i), 0.0);
s5.plusEqualsSparse (s1); for (int i = 0; i < s5.numLocations(); i++) { assertEquals (7, s5.indexAtLocation (i)); assertEquals (3.2, s5.valueAtLocation (i), 0.0); s6.plusEqualsSparse (s1, 3.5); for (int i = 0; i < s6.numLocations(); i++) { assertEquals (7, s6.indexAtLocation (i)); assertEquals (10.7, s6.valueAtLocation (i), 0.0);
public void testDenseSparseVector () { SparseVector svDense = new SparseVector (null, dbl3); double sdot = svDense.dotProduct (svDense); double ddot = d1.dotProduct (d1); assertEquals (sdot, ddot, 0.0001); svDense.plusEqualsSparse (s1); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 5.7, 3.5, 5.6, 0, 3, 0, 0, 0, 0, 0, 4, 0, 5, }); svDense.plusEqualsSparse (s1, 2.0); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 7.7, 3.5, 9.6, 0, 9, 0, 0, 0, 0, 0, 12, 0, 15, }); double[] dbl4 = new double [dbl3.length + 1]; for (int i = 0; i < dbl4.length; i++) dbl4[i] = 2.0; SparseVector sv4 = new SparseVector (null, dbl4); svDense.plusEqualsSparse (sv4); checkAnswer (svDense, new double[] { 4.0, 4.5, 5.0, 9.7, 5.5, 11.6, 2.0, 11.0, 2.0, 2.0, 2, 2, 2, 14, 2.0, 17, }); }
public void testDenseSparseVector () { SparseVector svDense = new SparseVector (null, dbl3); double sdot = svDense.dotProduct (svDense); double ddot = d1.dotProduct (d1); assertEquals (sdot, ddot, 0.0001); svDense.plusEqualsSparse (s1); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 5.7, 3.5, 5.6, 0, 3, 0, 0, 0, 0, 0, 4, 0, 5, }); svDense.plusEqualsSparse (s1, 2.0); checkAnswer (svDense, new double[] { 2.0, 2.5, 3.0, 7.7, 3.5, 9.6, 0, 9, 0, 0, 0, 0, 0, 12, 0, 15, }); double[] dbl4 = new double [dbl3.length + 1]; for (int i = 0; i < dbl4.length; i++) dbl4[i] = 2.0; SparseVector sv4 = new SparseVector (null, dbl4); svDense.plusEqualsSparse (sv4); checkAnswer (svDense, new double[] { 4.0, 4.5, 5.0, 9.7, 5.5, 11.6, 2.0, 11.0, 2.0, 2.0, 2, 2, 2, 14, 2.0, 17, }); }