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:Main.java

public static float getNormalizeScaling(final float x, final float y, final float z, final float w) {
    return (float) (1.0 / Math.sqrt(x * x + y * y + z * z + w * w));
}

From source file:Main.java

private static boolean isLight(int color) {
    return Math.sqrt(Color.red(color) * Color.red(color) * .241 + Color.green(color) * Color.green(color) * .691
            + Color.blue(color) * Color.blue(color) * .068) > 130;
}

From source file:Main.java

public static double computeDistance(ArrayList<Double> d1, ArrayList<Double> d2) {
    double squareSum = 0;
    for (int i = 0; i < d1.size() - 1; i++) {
        squareSum += (d1.get(i) - d2.get(i)) * (d1.get(i) - d2.get(i));
    }//from  w w  w. j ava2  s . c  om
    return Math.sqrt(squareSum);
}

From source file:Main.java

/**
 * Calculates the angle between two points on a cartesian plane
 *//*from   www . j  av a2s.c o  m*/
public static float getAngle(float center_x, float center_y, float post_x, float post_y) {
    float tmpv_x = post_x - center_x;
    float tmpv_y = post_y - center_y;
    float d = (float) Math.sqrt(tmpv_x * tmpv_x + tmpv_y * tmpv_y);
    float cos = tmpv_x / d;
    float angle = (float) Math.toDegrees(Math.acos(cos));

    angle = (tmpv_y < 0) ? angle * -1 : angle;

    return angle;
}

From source file:Main.java

private static double[] bdToGaoDe(double bd_lat, double bd_lon) {
    double[] gd_lat_lon = new double[2];
    double PI = 3.1415926535897932384626 * 3000.0 / 180.0;
    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
    double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
    double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
    gd_lat_lon[0] = z * Math.cos(theta);
    gd_lat_lon[1] = z * Math.sin(theta);
    return gd_lat_lon;
}

From source file:Main.java

public static void normalize2(double[] x) {
    double sum = 0;
    for (int i = 0; i < x.length; i++)
        if (!Double.isNaN(x[i]))
            sum += x[i] * x[i];//ww  w  .  ja v  a 2s .  co m
    if (sum == 0)
        return;
    double f = 1.0 / Math.sqrt(sum);
    for (int i = 0; i < x.length; i++)
        if (!Double.isNaN(x[i]))
            x[i] *= f;
}

From source file:Main.java

public static double getScreenPhysicalSize(Context ctx) {
    DisplayMetrics dm = getDisplayMetrics(ctx);
    double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
    return diagonalPixels / (160 * dm.density);
}

From source file:Main.java

public static double deviation(double[] data) {
    if (data.length < 2) {
        return 0;
    }/*from   ww w . jav  a2 s  . com*/
    double avg = average(data);
    double sum = 0;
    for (double element : data) {
        sum += (element - avg) * (element - avg);
    }

    return Math.sqrt(sum / (data.length - 1));
}

From source file:Main.java

/**
 * Normalizes (in-place) the specified vector.
 * //from  ww w .ja  va 2  s  .  c om
 * @param vector The vector array to normalize.
 * @param vIdx Vector's starting array index.
 */
public static void normalize(float[] vector, int vIdx) {
    // calculate vector's length
    float length = (float) Math.sqrt(vector[vIdx] * vector[vIdx] + vector[vIdx + 1] * vector[vIdx + 1]
            + vector[vIdx + 2] * vector[vIdx + 2]);

    // divide all components by the vector's length
    vector[vIdx] /= length;
    vector[vIdx + 1] /= length;
    vector[vIdx + 2] /= length;
}

From source file:Main.java

private static void findConstants(double[] n_p, double[] n_m, double[] d_p, double[] d_m, double[] bd_p,
        double[] bd_m, double std_dev) {
    double div = Math.sqrt(2 * 3.141593) * std_dev;
    double x0 = -1.783 / std_dev;
    double x1 = -1.723 / std_dev;
    double x2 = 0.6318 / std_dev;
    double x3 = 1.997 / std_dev;
    double x4 = 1.6803 / div;
    double x5 = 3.735 / div;
    double x6 = -0.6803 / div;
    double x7 = -0.2598 / div;
    int i;/*from ww  w.j  a v a 2  s .c  o  m*/

    n_p[0] = x4 + x6;
    n_p[1] = (Math.exp(x1) * (x7 * Math.sin(x3) - (x6 + 2 * x4) * Math.cos(x3))
            + Math.exp(x0) * (x5 * Math.sin(x2) - (2 * x6 + x4) * Math.cos(x2)));
    n_p[2] = (2 * Math.exp(x0 + x1) * ((x4 + x6) * Math.cos(x3) * Math.cos(x2)
            - x5 * Math.cos(x3) * Math.sin(x2) - x7 * Math.cos(x2) * Math.sin(x3)) + x6 * Math.exp(2 * x0)
            + x4 * Math.exp(2 * x1));
    n_p[3] = (Math.exp(x1 + 2 * x0) * (x7 * Math.sin(x3) - x6 * Math.cos(x3))
            + Math.exp(x0 + 2 * x1) * (x5 * Math.sin(x2) - x4 * Math.cos(x2)));
    n_p[4] = 0.0;

    d_p[0] = 0.0;
    d_p[1] = -2 * Math.exp(x1) * Math.cos(x3) - 2 * Math.exp(x0) * Math.cos(x2);
    d_p[2] = 4 * Math.cos(x3) * Math.cos(x2) * Math.exp(x0 + x1) + Math.exp(2 * x1) + Math.exp(2 * x0);
    d_p[3] = -2 * Math.cos(x2) * Math.exp(x0 + 2 * x1) - 2 * Math.cos(x3) * Math.exp(x1 + 2 * x0);
    d_p[4] = Math.exp(2 * x0 + 2 * x1);

    for (i = 0; i <= 4; i++) {
        d_m[i] = d_p[i];
    }

    n_m[0] = 0.0;
    for (i = 1; i <= 4; i++) {
        n_m[i] = n_p[i] - d_p[i] * n_p[0];
    }

    double sum_n_p, sum_n_m, sum_d;
    double a, b;

    sum_n_p = 0.0;
    sum_n_m = 0.0;
    sum_d = 0.0;

    for (i = 0; i <= 4; i++) {
        sum_n_p += n_p[i];
        sum_n_m += n_m[i];
        sum_d += d_p[i];
    }

    a = sum_n_p / (1.0 + sum_d);
    b = sum_n_m / (1.0 + sum_d);

    for (i = 0; i <= 4; i++) {
        bd_p[i] = d_p[i] * a;
        bd_m[i] = d_m[i] * b;
    }
}