Java Array Min Value minkowskiDistance(double[] coord1, double[] coord2)

Here you can find the source of minkowskiDistance(double[] coord1, double[] coord2)

Description

Calculates the Minkowski distance between two coordinates.

License

Open Source License

Parameter

Parameter Description
coord1 First Coordinate
coord2 Second Coordinate

Return

Minkowski Distance between coord1 and coord2

Declaration

public static double minkowskiDistance(double[] coord1, double[] coord2) 

Method Source Code

//package com.java2s;
/**/*from w w w .j a  v a  2  s. c  o  m*/
 * Copyright (C) 2014 Aniruddh Fichadia
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you use or enhance the code, please let me know using the provided author information or via
 * email Ani.Fichadia@gmail.com.
 */

public class Main {
    /**
     * Calculates the Minkowski distance between two coordinates.
     * 
     * Coordinates are represented as an array where the value for each dimension is in each index
     * 
     * @param coord1 First Coordinate
     * @param coord2 Second Coordinate
     * 
     * @return Minkowski Distance between coord1 and coord2
     */
    public static double minkowskiDistance(double[] coord1, double[] coord2) {
        return distanceBase(coord1, coord2, coord1.length);
    }

    /**
     * Calculates the distance between two coordinates. Is used as a base function for
     * euclideanDistance(double[], double[]) and minkowskiDistance(double[], double[])
     * 
     * Coordinates are represented as an array where the value for each dimension is in each index
     * 
     * @param coord1 First Coordinate
     * @param coord2 Second Coordinate
     * @param order Order for the distance calculation. Used to calculate the power of each
     *            dimension and the n-th root for the final distance calculation
     * 
     * @return Distance between coord1 and coord2
     */
    private static final double distanceBase(double[] coord1, double[] coord2, int order) {
        if (coord1.length != coord2.length)
            throw new IllegalArgumentException("Number of dimensions is not equal");

        final int NUM_DIMENSIONS = coord1.length;
        double distance = 0;

        for (int d = 0; d < NUM_DIMENSIONS; d++) {
            double absDiff = Math.abs(coord2[d] - coord1[d]);
            distance += Math.pow(absDiff, order);
        }

        distance = Math.pow(distance, (1.0 / order));
        ;

        return distance;
    }
}

Related

  1. minIndex(int[] values)
  2. minIndex(Number[] array)
  3. minIndexGreaterThan(final double[] array, final double p)
  4. minInRowIndex(double[][] M, int row)
  5. minInt(int... values)
  6. minLengthCheck(final byte[] buffer, final byte length)
  7. minList(int[] listA, int[] listB)
  8. minLocation(double[] list)
  9. minmax(double[] a)