public void testAssignVector() throws Exception { Vector other = new DenseVector(test.size()); test.assign(other); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i)); } }
public void testIterator() throws Exception { Iterator<Vector.Element> iterator = test.iterateNonZero(); checkIterator(iterator, gold); iterator = test.iterator(); checkIterator(iterator, gold); double[] doubles = {0.0, 5.0, 0, 3.0}; RandomAccessSparseVector zeros = new RandomAccessSparseVector(doubles.length); for (int i = 0; i < doubles.length; i++) { zeros.setQuick(i, doubles[i]); } iterator = zeros.iterateNonZero(); checkIterator(iterator, doubles); iterator = zeros.iterator(); checkIterator(iterator, doubles); doubles = new double[] {0.0, 0.0, 0, 0.0}; zeros = new RandomAccessSparseVector(doubles.length); for (int i = 0; i < doubles.length; i++) { zeros.setQuick(i, doubles[i]); } iterator = zeros.iterateNonZero(); checkIterator(iterator, doubles); iterator = zeros.iterator(); checkIterator(iterator, doubles); }
public void testTimesVectorCardinality() { try { test.times(new DenseVector(test.size() + 1)); fail("expected exception"); } catch (CardinalityException e) { } }
public void testGetOver() { try { test.get(test.size()); fail("expected exception"); } catch (IndexException e) { } }
public void testAssignDoubleArray() throws Exception { double[] array = new double[test.size()]; test.assign(array); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i)); } }
public void testSparseDoubleVectorInt() throws Exception { Vector val = new RandomAccessSparseVector(4); assertEquals("size", 4, val.size()); for (int i = 0; i < 4; i++) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } }
public void testAssignDoubleArrayCardinality() { double[] array = new double[test.size() + 1]; try { test.assign(array); fail("cardinality exception expected"); } catch (CardinalityException e) { } }
public void testAssignVectorCardinality() { Vector other = new DenseVector(test.size() - 1); try { test.assign(other); fail("cardinality exception expected"); } catch (CardinalityException e) { } }
public void testGet() throws Exception { for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2], test.get(i)); } } }
public void testAssignBinaryFunction3() throws Exception { test.assign(mult(4)); for (int i = 0; i < values.length; i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i)); } else { assertEquals("value[" + i + ']', values[i - 1] * 4, test.getQuick(i)); } } }
public void testNormalize() throws Exception { Vector val = test.normalize(); double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("dot", values[i / 2] / mag, val.get(i)); } } }
public void testSet() throws Exception { test.set(3, 4.5); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, test.get(i)); } else if (i == 3) { assertEquals("set [" + i + ']', 4.5, test.get(i)); } else { assertEquals("set [" + i + ']', values[i / 2], test.get(i)); } } }
public void testCrossProduct() { Matrix result = test.cross(test); assertEquals("row size", test.size(), result.size()[0]); assertEquals("col size", test.size(), result.size()[1]); for (int row = 0; row < result.size()[0]; row++) { for (int col = 0; col < result.size()[1]; col++) { assertEquals( "cross[" + row + "][" + col + ']', test.getQuick(row) * test.getQuick(col), result.getQuick(row, col)); } } }
public void testGetUnder() { try { test.get(-1); fail("expected exception"); } catch (IndexException e) { } }
public void testViewPartUnder() { try { test.viewPart(-1, values.length); fail("no exception"); } catch (IndexException e) { } }
public void testViewPartOver() { try { test.viewPart(2, 7); fail("no exception"); } catch (IndexException e) { } }
public void testViewPartCardinality() { try { test.viewPart(1, 8); fail("no exception"); } catch (IndexException e) { } }
public void testZSum() { double expected = 0; for (double value : values) { expected += value; } assertEquals("wrong zSum", expected, test.zSum()); }
@Override protected void setUp() throws Exception { super.setUp(); test = generateTestVector(2 * values.length + 1); for (int i = 0; i < values.length; i++) { test.set(2 * i + 1, values[i]); } }
public void testViewPart() throws Exception { Vector part = test.viewPart(1, 2); assertEquals("part size", 2, part.getNumNondefaultElements()); for (int i = 0; i < part.size(); i++) { assertEquals("part[" + i + ']', test.get(i + 1), part.get(i)); } }
public void testPlusDouble() throws Exception { Vector val = test.plus(1); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 1.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] + 1.0, val.get(i)); } } }
public void testDivideDouble() throws Exception { Vector val = test.divide(3); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] / 3.0, val.get(i), EPSILON); } } }
public void testTimesVector() throws Exception { Vector val = test.times(test); assertEquals("size", test.size(), val.size()); for (int i = 0; i < test.size(); i++) { if (i % 2 == 0) { assertEquals("get [" + i + ']', 0.0, val.get(i)); } else { assertEquals("get [" + i + ']', values[i / 2] * values[i / 2], val.get(i)); } } }
public void testGetDistanceSquared() { Vector other = new RandomAccessSparseVector(test.size()); other.set(1, -2); other.set(2, -5); other.set(3, -9); other.set(4, 1); double expected = test.minus(other).getLengthSquared(); assertTrue( "a.getDistanceSquared(b) != a.minus(b).getLengthSquared", Math.abs(expected - test.getDistanceSquared(other)) < 10.0E-7); }
public void testIteratorSet() throws CloneNotSupportedException { Vector clone = test.clone(); Iterator<Vector.Element> it = clone.iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); e.set(e.get() * 2.0); } it = clone.iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); assertEquals(test.get(e.index()) * 2.0, e.get()); } clone = test.clone(); it = clone.iterator(); while (it.hasNext()) { Vector.Element e = it.next(); e.set(e.get() * 2.0); } it = clone.iterator(); while (it.hasNext()) { Vector.Element e = it.next(); assertEquals(test.get(e.index()) * 2.0, e.get()); } }
public void testCardinality() { assertEquals("size", 7, test.size()); }
public void testLike() { Vector other = test.like(); assertTrue("not like", test.getClass().isAssignableFrom(other.getClass())); assertEquals("size", test.size(), other.size()); }
public void testAsFormatString() { String formatString = test.asFormatString(); Vector vec = AbstractVector.decodeVector(formatString); assertEquals(vec, test); }
public void testAssignUnaryFunction() { test.assign(NEGATE); for (int i = 1; i < values.length; i += 2) { assertEquals("value[" + i + ']', -values[i], test.getQuick(i + 2)); } }
public void testAssignDouble() { test.assign(0); for (int i = 0; i < values.length; i++) { assertEquals("value[" + i + ']', 0.0, test.getQuick(i)); } }