Example usage for java.lang Math sqrt

List of usage examples for java.lang Math sqrt

Introduction

In this page you can find the example usage for java.lang Math sqrt.

Prototype

@HotSpotIntrinsicCandidate
public static double sqrt(double a) 

Source Link

Document

Returns the correctly rounded positive square root of a double value.

Usage

From source file:com.medlog.webservice.util.MathUtl.java

/**
 *
 * Generate Correlation from values./*from  ww  w. ja va2s  .  c o m*/
 *
 * @param ax
 * @param by
 * @return r
 */
public static double correlation(double[] ax, double[] by) {
    // TODO: check here that arrays are not null, of the same length etc

    double sx = 0.0;
    double sy = 0.0;
    double sxx = 0.0;
    double syy = 0.0;
    double sxy = 0.0;

    int n = ax.length;

    for (int i = 0; i < n; ++i) {
        double x = ax[i];
        double y = by[i];

        sx += x;
        sy += y;
        sxx += x * x;
        syy += y * y;
        sxy += x * y;
    }
    double corr = new org.apache.commons.math3.stat.correlation.PearsonsCorrelation().correlation(ax, by);

    System.out.println(corr);
    // coV
    double cov = sxy / n - sx * sy / n / n;
    // std(x)
    double sigmax = Math.sqrt(sxx / n - sx * sx / n / n);
    // std(y)
    double sigmay = Math.sqrt(syy / n - sy * sy / n / n);
    // cor
    return cov / sigmax / sigmay;
}

From source file:Main.java

/**
 * Computes the Euclidean distance between the two colors
 *
 * @param color1 the first color//from   ww  w .  j a  v a2s.co m
 * @param color2 the color to compare with
 * @return the distance between the two colors
 */
private static double getColorDistanceRgb(int color1, int color2) {
    double r, g, b;

    r = Math.pow(Color.red(color2) - Color.red(color1), 2.0);
    g = Math.pow(Color.green(color2) - Color.green(color1), 2.0);
    b = Math.pow(Color.blue(color2) - Color.blue(color1), 2.0);

    return Math.sqrt(b + g + r);
}

From source file:edu.gmu.csd.processor.DataProcessor.java

public static WinningResult calculateMeanStandardDeviation(String raffle) {
    String[] dataFieldsArr = null;
    if (StringUtils.isNotEmpty(raffle)) {
        dataFieldsArr = StringUtils.split(raffle, ",");
    }//  w  w w  . j  av a  2 s .c om
    WinningResult winningResult = null;

    if (null != dataFieldsArr) {
        double mean;
        double sum = 0;
        double squareSum = 0;
        double stndrdDeviation;

        // First for loop to calculate the mean.
        for (String meanData : dataFieldsArr) {
            sum = sum + Integer.parseInt(meanData);
        }

        mean = sum / dataFieldsArr.length;

        // Second for loop to calculate the standard deviation.
        for (String sdData : dataFieldsArr) {
            double square = Math.pow((Integer.parseInt(sdData) - mean), 2);

            squareSum = squareSum + square;
        }

        stndrdDeviation = Math.sqrt(squareSum / (dataFieldsArr.length - 1));

        winningResult = new WinningResult();
        winningResult.setMean(mean);
        winningResult.setStandardDeviation(stndrdDeviation);
    }

    return winningResult;
}

From source file:Main.java

/**
 * Computes distance between points in any dimension.
 * /*from   www.j a v  a2 s .c  o  m*/
 * @param p0 point 0
 * @param p1 point 1
 * 
 * @return distance between points
 */
private static Double distance(double[] p0, double[] p1) {
    // Must be of same dimension
    if (p0.length != p1.length)
        return null;

    // Calculate distance
    double val = 0;
    for (int n = 0; n < p0.length; n++)
        val += Math.pow(p1[n] - p0[n], 2);
    return Math.sqrt(val);
}

From source file:Main.java

public static double accurateDistanceMeters(double lat1, double lng1, double lat2, double lng2) {
    double dlat = Math.sin(0.5 * (lat2 - lat1));
    double dlng = Math.sin(0.5 * (lng2 - lng1));
    double x = dlat * dlat + dlng * dlng * Math.cos(lat1) * Math.cos(lat2);
    return (2 * Math.atan2(Math.sqrt(x), Math.sqrt(Math.max(0.0, 1.0 - x)))) * EARTH_RADIUS_METERS;
}

From source file:Main.java

public static double normal(double u, double s)
// Random normal distribution with mean u and standard deviation s
{
    double fac, rsq, v1, v2;

    if (iset == 0) {
        do {/* w w  w. j  ava  2  s .c  o m*/
            v1 = 2.0 * Math.random() - 1.0;
            v2 = 2.0 * Math.random() - 1.0;
            rsq = v1 * v1 + v2 * v2;
        } while (rsq >= 1.0 || rsq == 0.0);
        fac = Math.sqrt(-2.0 * Math.log(rsq) / rsq);
        gset = v1 * fac;
        iset = 1;
        return (u + s * (v2 * fac));
    } else {
        iset = 0;
        return (u + s * gset);
    }
}

From source file:es.udc.gii.common.eaf.util.EAFMath.java

public static double distance(double[] pI, double[] pJ) {
    double[] auxArray = new double[pI.length];

    for (int i = 0; i < pI.length; i++) {
        auxArray[i] = pI[i] - pJ[i];//from   w w  w  . j ava 2s. c  o m
    }
    double sumSq = StatUtils.sumSq(auxArray);

    return (sumSq == 0.0 ? 0.0 : Math.sqrt(sumSq));
}

From source file:Main.java

public static double stdarr(double[] a, double avg) // std given the average
{
    double res = 0.0, r = 0.0;

    for (int i = 0; i < a.length; i++)
        res += (a[i] - avg) * (a[i] - avg);

    if (res > 0.0)
        r = Math.sqrt(res / (a.length - 1));

    return r;/*from   w  ww  .j a v  a2s. co  m*/

}

From source file:com.ibm.bluej.commonutil.DenseVectors.java

public static double stdDev(double[] v, double m) {
    double sum = 0;
    for (double vi : v) {
        sum += (vi - m) * (vi - m);/* w  w w  .j  av a2s .c o m*/
    }
    return Math.sqrt(sum / v.length);
}

From source file:testes.Histograma.java

private static IntervalXYDataset crearDataset() {
    //guarda os dados do histograma
    HistogramDataset dados = new HistogramDataset();
    int classes, min, max;

    //valores da amos
    //double amostra[] = {7,8,9,10,10,10.2,10.8,11,12,12};
    double amostra[] = { 89, 85, 93, 81, 77, 83, 87, 81, 83, 89, 81, 86, 80, 84, 82 };
    if (amostra.length <= 25) {
        classes = 5;//from  w  w w .ja  va 2 s.  com
    } else {
        classes = (int) Math.round(Math.sqrt(amostra.length));
    }

    dados.addSeries("Frequncias da Amostra", amostra, classes, 77, 95);
    return dados;
}