Java Distance Calculate distance(final double[] a, final double[] b)

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

Description

get the length of (a - b).

License

Open Source License

Parameter

Parameter Description
a a parameter
b a parameter

Declaration

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

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w w w  .  ja va  2s. c o  m
     * get the length of (a - b).
     * 
     * @param a
     * @param b
     */
    public static double distance(final double[] a, final double[] b) {
        return Math.sqrt(squareDistance(a, b));
    }

    /**
     * get the squared length of (a - b).
     * 
     * @param a
     * @param b
     */
    public static double squareDistance(final double[] a, final double[] b) {
        assert rows(a) == rows(b);
        final int rows = rows(a);
        double squ_len = 0.0;
        for (int i = 0; i < rows; ++i)
            squ_len += (a[i] - b[i]) * (a[i] - b[i]);
        return squ_len;
    }

    public static int rows(final double[] a) {
        return a.length;
    }

    public static int rows(final double[][] A) {
        return A.length;
    }
}

Related

  1. distance(double[] x, int p, int q, int dim)
  2. distance(final Class child, final Class parent)
  3. distance(final double x, final double y)
  4. distance(final double x1, final double y1, final double x2, final double y2)
  5. distance(final double x1, final double y1, final double x2, final double y2)
  6. Distance(final double[] minCorner, final double[] maxCorner)
  7. distance(final double[] p, final double[] q)
  8. distance(final double[] t1, final double[] t2)
  9. distance(final String c1, final String c2, final String c1Prev, final String c2Prev)