Java Euclidean Distance euclidean(double[] _a, double[] _b)

Here you can find the source of euclidean(double[] _a, double[] _b)

Description

Computes the euclidean distance between two vector of doubles with equal size.

License

Open Source License

Parameter

Parameter Description
_a First vector
_b Second vector

Return

A double that is the euclidean distance.

Declaration

public static double euclidean(double[] _a, double[] _b) 

Method Source Code

//package com.java2s;
/***********************************************************************

 This file is part of KEEL-software, the Data Mining tool for regression, 
 classification, clustering, pattern mining and so on.

 Copyright (C) 2004-2010//ww  w  .j  a v a 2s.  co m
    
 F. Herrera (herrera@decsai.ugr.es)
 L. S?nchez (luciano@uniovi.es)
 J. Alcal?-Fdez (jalcala@decsai.ugr.es)
 S. Garc?a (sglopez@ujaen.es)
 A. Fern?ndez (alberto.fernandez@ujaen.es)
 J. Luengo (julianlm@decsai.ugr.es)

 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/
    
 **********************************************************************/

public class Main {
    /**
     * <p>
     * Computes the euclidean distance between two vector of doubles with equal size.
     * </p>
     * @param _a First vector
     * @param _b Second vector
     * @return A double that is the euclidean distance.
     */
    public static double euclidean(double[] _a, double[] _b) {
        double toRet = 0;
        if (_a.length != _b.length) {
            System.out
                    .println("ERROR: Euclidean can not be used if vectors have different size!");
            toRet = -1;
        } else {
            for (int i = 0; i < _a.length; ++i) {
                toRet += (_a[i] - _b[i]) * (_a[i] - _b[i]);
            }
            toRet = Math.sqrt(toRet);
        }
        return toRet;
    }
}

Related

  1. euclidean(double[] a, double[] b)
  2. euclidean(Double[] a, Double[] b)
  3. euclidean(double[] features1, double[] features2)
  4. euclidean(double[] x1, double[] x2)