Here you can find the source of distance(final double[] p, final double[] q)
static final double distance(final double[] p, final double[] q)
//package com.java2s; /*// ww w. j a v a 2 s .c om * ==================================================== * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved. * * Developed at Idylwood Technologies, LLC. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * The License should have been distributed to you with the source tree. * If not, it can be found at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author: Charles Cooper * Date: 2013 * ==================================================== */ public class Main { static final double distance(final double[] p, final double[] q) { // TODO make this faster if needed, as it is it is going to loop like three times ;) return Math.sqrt(sum(pow(subtract(p, q), 2))); } /** * Implementation of sum which is both more numerically * stable _and faster_ than the naive implementation * which is used in all standard numerical libraries I know of: * Colt, OpenGamma, Apache Commons Math, EJML. * * Implementation uses variant of Kahan's algorithm keeping a running error * along with the accumulator to try to cancel out the error at the end. * This is much faster than Schewchuk's algorithm but not * guaranteed to be perfectly precise * In most cases, however, it is just as precise. * Due to optimization it is about 30% faster * even than the naive implementation on my machine. * @param values * @return * Side Effects: none */ public static final double sum(final double... values) { double sum = 0; double err = 0; final int unroll = 6; // empirically it doesn't get much better than this final int len = values.length - values.length % unroll; // unroll the loop. due to IEEE 754 restrictions // the JIT shouldn't be allowed to unroll it dynamically, so it's // up to us to do it by hand ;) int i = 0; for (; i < len; i += unroll) { final double val = values[i] + values[i + 1] + values[i + 2] + values[i + 3] + values[i + 4] + values[i + 5]; final double partial = val - err; final double hi = sum + val; err = (hi - sum) - partial; sum = hi; } for (; i < values.length; i++) { final double val = values[i]; final double partial = val - err; final double hi = sum + val; err = (hi - sum) - partial; sum = hi; } return sum; } /** * Performs termwise Math.pow(double,double) on the elements. * @param values * @param exp * @return Newly allocated array whose elements are * termwise exponentiations of the input. * Side Effects: Allocation of new array */ public static final double[] pow(final double[] values, final double exp) { final double[] ret = new double[values.length]; for (int i = values.length; i-- != 0;) ret[i] = Math.pow(values[i], exp); return ret; } final public static double[] subtract(final double[] first, final double[] second) { final int len = Math.min(first.length, second.length); final double ret[] = new double[len]; for (int i = 0; i < len; i++) ret[i] = first[i] - second[i]; return ret; } public final static double min(double... values) { if (values.length == 0) return Double.NaN; double ret = values[0]; for (int i = 1; i < values.length; i++) if (values[i] < ret) ret = values[i]; return ret; } }