That does look wrong. The origin of the optimization is that (x-y)^2 = x^2 + y^2 - 2 x y = x^2 + y (y - x). If x^2 is pre-computed and y is sparse, then this second form can be much faster than the first. Whether the third form is better still depends on implementation, but if it is done without an extra vector allocation, then it should be better because it only iterates through y once.
On Wed, Jan 27, 2010 at 11:16 AM, Jake Mannix <jake.man...@gmail.com> wrote: > On Wed, Jan 27, 2010 at 10:50 AM, Jake Mannix <jake.man...@gmail.com> > wrote: > > > > If you're calling this as distance(centroid.getLengthSquared(), centroid, > > v), then > > this resolves to > > > > centroid.getLengthSquared() + v.getDistanceSquared(centroid) > > > > why is this supposed to be equal to > > > > centroid.minus(v).getLengthSquared()? > > > > Like, why are we even using the centroidLengthSquared at all? Isn't this > method > really just doing centroid.getDistanceSquared(v)? > > -jake > -- Ted Dunning, CTO DeepDyve