Java Vector vectorDistance(double[] vec1, double[] vec2, double power)

Here you can find the source of vectorDistance(double[] vec1, double[] vec2, double power)

Description

Calculate the p-norm (i.e.

License

Apache License

Parameter

Parameter Description
vec1 The first vector
vec2 The second vector
power The power (2 for cartesian distance, 1 for manhattan, etc.)

Return

The length. <p/> See http://en.wikipedia.org/wiki/Lp_space

Declaration

public static double vectorDistance(double[] vec1, double[] vec2, double power) 

Method Source Code

//package com.java2s;
/*/* w  w w .j  a  v a2s . com*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License 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.
 */

public class Main {
    /**
     * Calculate the p-norm (i.e. length) between two vectors
     *
     * @param vec1  The first vector
     * @param vec2  The second vector
     * @param power The power (2 for cartesian distance, 1 for manhattan, etc.)
     * @return The length.
     *         <p/>
     *         See http://en.wikipedia.org/wiki/Lp_space
     * @see #vectorDistance(double[], double[], double, double)
     */
    public static double vectorDistance(double[] vec1, double[] vec2, double power) {
        return vectorDistance(vec1, vec2, power, 1.0 / power);
    }

    /**
     * Calculate the p-norm (i.e. length) between two vectors
     *
     * @param vec1         The first vector
     * @param vec2         The second vector
     * @param power        The power (2 for cartesian distance, 1 for manhattan, etc.)
     * @param oneOverPower If you've precalculated oneOverPower and cached it, use this method to save one division operation over {@link #vectorDistance(double[], double[], double)}.
     * @return The length.
     */
    public static double vectorDistance(double[] vec1, double[] vec2, double power, double oneOverPower) {
        double result = 0;

        if (power == 0) {
            for (int i = 0; i < vec1.length; i++) {
                result += vec1[i] - vec2[i] == 0 ? 0 : 1;
            }

        } else if (power == 1.0) {
            for (int i = 0; i < vec1.length; i++) {
                result += vec1[i] - vec2[i];
            }
        } else if (power == 2.0) {
            result = Math.sqrt(distSquaredCartesian(vec1, vec2));
        } else if (power == Integer.MAX_VALUE || Double.isInfinite(power)) {//infinite norm?
            for (int i = 0; i < vec1.length; i++) {
                result = Math.max(result, Math.max(vec1[i], vec2[i]));
            }
        } else {
            for (int i = 0; i < vec1.length; i++) {
                result += Math.pow(vec1[i] - vec2[i], power);
            }
            result = Math.pow(result, oneOverPower);
        }
        return result;
    }

    /**
     * The square of the cartesian Distance.  Not really a distance, but useful if all that matters is
     * comparing the result to another one.
     *
     * @param vec1 The first point
     * @param vec2 The second point
     * @return The squared cartesian distance
     */
    public static double distSquaredCartesian(double[] vec1, double[] vec2) {
        double result = 0;
        for (int i = 0; i < vec1.length; i++) {
            double v = vec1[i] - vec2[i];
            result += v * v;
        }
        return result;
    }
}

Related

  1. vector_sum(double[] v, double[] w)
  2. vectorAbsoluteValue(double X, double Y, double Z)
  3. vectorCos(int[] d1, int[] d2)
  4. vectorDiff(final double[] vecOne, final double[] vecTwo)
  5. vectorDir(int vert, int horiz)
  6. vectorIndexToUpperTriangularIndices(int numberOfRows, int index)
  7. vectorKLDivergence(double v1[], double v2[])
  8. vectorL2Norm(double[] v)
  9. vectorLog10(double v1[])