AbstractVector implements Vector { @Override public double getDistanceSquared(Vector v) { if (size != v.size()) { throw new CardinalityException(size, v.size()); } if (lengthSquared >= 0 && v instanceof AbstractVector && ((AbstractVector)v).lengthSquared >= 0) { return lengthSquared + v.getLengthSquared() - 2 * this.dot(v); } Vector randomlyAccessed; Iterator<Element> it; double d = 0.0; if (lengthSquared >= 0.0) { it = v.iterateNonZero(); randomlyAccessed = this; d += lengthSquared; } else { it = iterateNonZero(); randomlyAccessed = v; d += v.getLengthSquared(); } while (it.hasNext()) { Element e = it.next(); double value = e.get(); d += value * (value - 2.0 * randomlyAccessed.getQuick(e.index())); } return Math.abs(d); } protected  AbstractVector(int size); @Override double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map); @Override double aggregate(Vector other, DoubleDoubleFunction aggregator, DoubleDoubleFunction combiner); @Override Vector viewPart(int offset, int length); @Override Vector clone(); @Override Vector divide(double x); @Override double dot(Vector x); double dotSelf(); @Override double get(int index); @Override Element getElement(int index); @Override Vector minus(Vector that); @Override Vector normalize(); @Override Vector normalize(double power); @Override Vector logNormalize(); @Override Vector logNormalize(double power); Vector logNormalize(double power, double normLength); @Override double norm(double power); @Override double getLengthSquared(); @Override double getDistanceSquared(Vector v); @Override double maxValue(); @Override int maxValueIndex(); @Override double minValue(); @Override int minValueIndex(); @Override Vector plus(double x); @Override Vector plus(Vector x); @Override void set(int index, double value); @Override Vector times(double x); @Override Vector times(Vector x); @Override double zSum(); @Override Vector assign(double value); @Override Vector assign(double[] values); @Override Vector assign(Vector other); @Override Vector assign(DoubleDoubleFunction f, double y); @Override Vector assign(DoubleFunction function); @Override Vector assign(Vector other, DoubleDoubleFunction function); @Override Matrix cross(Vector other); @Override final int size(); @Override String asFormatString(); @Override int hashCode(); @Override boolean equals(Object o); @Override String toString(); String toString(String[] dictionary);  }
@Test 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); }