0

I working through a few exercises from an academic programing book. The task is to implement 2 vectors and calculate the Euclidean distance between thereof. No this is not Home Work, rather self studying.

I'm seeking some feedback on the correctness of my distance implementation.

public class EuclideanDist
{
    public static void main(String[] args)
    {   
        EuclideanDist euc = new EuclideanDist();
        Random rnd = new Random();        

        int N = Integer.parseInt(args[0]);

        double[] a = new double[N];
        double[] b = new double[N];
        double[] x = new double[N];

        euc.print(euc.init(a, rnd));        
        euc.print(euc.init(b, rnd));        
        print(euc.distance(a, b, x));
    }

    private double[] init(double[] src, Random rnd)
    {
        for(int i = 0; i < src.length; i++)
        {
            src[i] = rnd.nextDouble();
        }
        return src;
    }
    private double[] distance(double[] a, double[] b, double[] x)
    {
        double diff; 
        int N = a.length;

        for(int i = 0; i < N; i++)
        {
            diff = a[i] - b[i];
            x[i] = Math.sqrt(diff * diff);
        }
        return x;
    }
    private static void print(double[] x)
    {
        int N = x.length;
        for(int j = 0; j < N; j++)
            System.out.print(" " + x[j] + " ");
        System.out.println();
    }
}
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • 2
    what issue do you have? – Abdelhak Jan 05 '16 at 22:35
  • 1
    This is Java; why did you tag it C++? – Alan Stokes Jan 05 '16 at 22:38
  • 2
    Distance should be a scalar, not a vector; the square root of the sum of the squares of the distances along each axis. `sqrt(diff * diff)` is obviously just `diff`. – Alan Stokes Jan 05 '16 at 22:41
  • 1
    I think OP is using `sqrt(diff*diff)` instead of `abs` to ensure `diff` is +ve. Not commenting on the optimality or otherwise, but `sqrt(diff*diff)` is not always equal to `diff` – J Richard Snape Jan 06 '16 at 01:12
  • 1
    OP - I think you have a conceptual problem, rather than a programming one. Try reading [some resources](https://en.wikipedia.org/wiki/Euclidean_distance) on what Euclidian distance means - your implementation and the comment on the answer imply that you have misunderstood. – J Richard Snape Jan 06 '16 at 01:20
  • 1
    @JRichardSnape thanks for the feedback I will do so. – dcrearer Jan 06 '16 at 02:27

1 Answers1

4

Based on the suggestions of @AlanStokes, the following codes seems to be one solution (I have tested it):

import java.util.Random;

public class EuclideanDist {
    public static void main(String[] args) {
        EuclideanDist euc = new EuclideanDist();
        Random rnd = new Random();

        int N = Integer.parseInt(args[0]);

        double[] a = new double[N];
        double[] b = new double[N];

        euc.print(euc.init(a, rnd));
        euc.print(euc.init(b, rnd));
        System.out.println(euc.distance(a, b));
    }

    private double[] init(double[] src, Random rnd) {
        for (int i = 0; i < src.length; i++) {
            src[i] = rnd.nextDouble();
        }
        return src;
    }

    private double distance(double[] a, double[] b) {
        double diff_square_sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            diff_square_sum += (a[i] - b[i]) * (a[i] - b[i]);
        }
        return Math.sqrt(diff_square_sum);
    }

    private void print(double[] x) {
        for (int j = 0; j < x.length; j++) {
            System.out.print(" " + x[j] + " ");
        }
        System.out.println();
    }
}
Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
  • 1
    It seems your solution gives an aggregate distance. In the case where I would like to determine the distance between entries of the arrays it doesn't seem this solution would work. Your feedback is totally welcomed.. – dcrearer Jan 06 '16 at 00:54
  • My thought is close to @JRichardSnape; or, could you kindly provide the mathematical formula about the distance you would like to compute? – Tsung-Ting Kuo Jan 06 '16 at 02:24
  • @d_blk Thanks a lot! – Tsung-Ting Kuo Jan 06 '16 at 02:29