Java Distance Calculate distance(int[] a, int[] b)

Here you can find the source of distance(int[] a, int[] b)

Description

distance

License

LGPL

Declaration

public static double distance(int[] a, int[] b) 

Method Source Code

//package com.java2s;
/*//from ww w. j  a v a 2 s.  com
This code is licensed under the LGPL v3 or greater with the classpath exception, 
with the following additions and exceptions.  
    
packages cern.* have retained the original cern copyright notices.
    
packages at.mabs.cmdline and at.mabs.util.* 
have the option to be licensed under a BSD(simplified) or Apache 2.0 or greater  license 
in addition to LGPL. 
    
Note that you have permission to replace this license text to any of the permitted licenses. 
    
Main text for LGPL can be found here:
http://www.opensource.org/licenses/lgpl-license.php
    
For BSD:
http://www.opensource.org/licenses/bsd-license.php
    
for Apache:
http://www.opensource.org/licenses/apache2.0.php
    
classpath exception:
http://www.gnu.org/software/classpath/license.html
*/

public class Main {
    public static double distance(int[] a, int[] b) {
        double d = 0;
        if (b == null || b.length != a.length)
            throw new RuntimeException("Wa?" + a.length + "\t" + b.length);// return
        // Double.MAX_VALUE;
        for (int i = 0; i < a.length; i++) {
            d += (a[i] - b[i]) * (a[i] - b[i]);
        }
        return Math.sqrt(d);
    }

    public static double distance(int[][][] a, int[][][] b) {
        double d = 0;
        if (b == null || b.length != a.length)
            throw new RuntimeException("Wa?" + a + "\t" + b);

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                for (int k = 0; k < a[i][j].length; k++) {
                    d += (a[i][j][k] - b[i][j][k]) * (a[i][j][k] - b[i][j][k]);
                }
            }
        }
        return Math.sqrt(d);
    }
}

Related

  1. Distance(int X1, int X2, int Y1, int Y2)
  2. distance(int x1, int y1, int x2, int y2)
  3. distance(int x1, int y1, int x2, int y2)
  4. distance(int x1, int y1, int x2, int y2)
  5. distance(int xa, int ya, int xb, int yb)
  6. distance(String a, String b)
  7. distance(String coord1, String coord2, char unit)
  8. distance(String seq1, String seq2)
  9. distance2(float x, float y, float x1, float y1)