SequentialAccessSparseVector extends AbstractVector { @Override public double dot(Vector x) { if (size() != x.size()) { throw new CardinalityException(size(), x.size()); } if (this == x) { return dotSelf(); } if (x instanceof SequentialAccessSparseVector) { Iterator<Element> myIter = iterateNonZero(); Iterator<Element> otherIter = x.iterateNonZero(); if (!myIter.hasNext() || !otherIter.hasNext()) { return 0.0; } Element myCurrent = myIter.next(); Element otherCurrent = otherIter.next(); double result = 0.0; while (true) { int myIndex = myCurrent.index(); int otherIndex = otherCurrent.index(); if (myIndex == otherIndex) { result += myCurrent.get() * otherCurrent.get(); } if (myIndex <= otherIndex) { if (!myIter.hasNext()) { break; } myCurrent = myIter.next(); } if (myIndex >= otherIndex) { if (!otherIter.hasNext()) { break; } otherCurrent = otherIter.next(); } } return result; } else { double result = 0.0; Iterator<Element> iter = iterateNonZero(); while (iter.hasNext()) { Element element = iter.next(); result += element.get() * x.getQuick(element.index()); } return result; } } SequentialAccessSparseVector();  SequentialAccessSparseVector(int cardinality);  SequentialAccessSparseVector(int cardinality, int size);  SequentialAccessSparseVector(Vector other);  SequentialAccessSparseVector(SequentialAccessSparseVector other, boolean shallowCopy);  SequentialAccessSparseVector(SequentialAccessSparseVector other); private  SequentialAccessSparseVector(int cardinality, OrderedIntDoubleMapping values); @Override SequentialAccessSparseVector clone(); @Override Vector assign(Vector other); @Override String toString(); @Override boolean isDense(); @Override boolean isSequentialAccess(); @Override double getQuick(int index); @Override void setQuick(int index, double value); @Override int getNumNondefaultElements(); @Override SequentialAccessSparseVector like(); @Override Iterator<Element> iterateNonZero(); @Override Iterator<Element> iterator(); @Override double dot(Vector x); @Override Vector minus(Vector that);  }
@Override @Test public void testDot2() { Vector w = new SequentialAccessSparseVector(Integer.MAX_VALUE, 12); w.set(1, 0.4); w.set(2, 0.4); w.set(3, -0.666666667); Vector v = new SequentialAccessSparseVector(Integer.MAX_VALUE, 12); v.set(3, 1); assertEquals("dot2", -0.666666667, v.dot(w), EPSILON); }