array Converged - Java java.lang

Java examples for java.lang:Math Operation

Description

array Converged

Demo Code


//package com.java2s;

public class Main {
    /**//from  w  ww. j  a  v a  2 s.  c  o m
     * 
     * @param a
     *            first array
     * @param b
     *            second array
     * @param threshold
     * @return true if |a - b|/|a| < threshold false otherwise
     */
    public static boolean arrayConverged(double a[], double b[],
            double threshold) {

        double a2sum = 0;
        double difference2 = 0;

        for (int i = 0; i < a.length; i++) {
            a2sum += a[i] * a[i];
            difference2 += (a[i] - b[i]) * (a[i] - b[i]);
        }
        /**
         * sqrt ( (a-b)^2/a^2 )< threshold
         */
        // System.out.println("A2sum::: "+a2sum);
        if (Math.sqrt(difference2 / a2sum) < threshold)
            return true;
        else
            return false;
    }
}

Related Tutorials