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 double calculateCosineSimilarity(List<Double> list1, List<Double> list2) {
    if (list1.size() != list2.size()) {
        System.err.println("Two lists must have the same dimensionality.");
        return 0;
    }//from   w  w w . ja v a2  s. c  om
    double dividend = 0, divisor1 = 0, divisor2 = 0;

    for (int i = 0; i < list1.size(); i++) {
        dividend += list1.get(i) * list2.get(i);
        divisor1 += Math.pow(list1.get(i), 2);
        divisor2 += Math.pow(list2.get(i), 2);
    }
    return dividend / (Math.sqrt(divisor1) * Math.sqrt(divisor2));
}

From source file:Main.java

/**
 * Get the current device physical size in inches.
 *
 * @param context valid context.//w  w w .j a  v  a 2  s. c o m
 * @return <b>double</b> device inches
 */
public static double getDeviceDiagonalSizeInInches(final Context context) {
    final DisplayMetrics metrics = getDisplayMetrics(context);

    final double xInches = (double) metrics.widthPixels / metrics.xdpi;
    final double yInches = (double) metrics.heightPixels / metrics.ydpi;
    return Math.sqrt(Math.pow(xInches, 2) + Math.pow(yInches, 2));
}

From source file:Main.java

private static double getDisplayInches(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    float widthDpi = metrics.xdpi;
    float heightDpi = metrics.ydpi;

    float widthInches = widthPixels / widthDpi;
    float heightInches = heightPixels / heightDpi;

    double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));

    return diagonalInches;
}

From source file:Main.java

public static boolean withPointRadius(PointF cur, PointF target, float radius) {
    double space_2 = Math.pow(Math.abs(cur.x - target.x), 2) + Math.pow(Math.abs(cur.y - target.y), 2);
    double space = Math.sqrt(space_2);
    if (radius > space) {
        return true;
    }/*from w  w w  .j  a  v a 2s  .c  o  m*/
    return false;
}

From source file:Util.java

/**
 * Returns distance between 3D set of coords
 * /*  w  w w  . j  ava 2  s  . c  o m*/
 * @param x1
 *            first x coord
 * @param y1
 *            first y coord
 * @param z1
 *            first z coord
 * @param x2
 *            second x coord
 * @param y2
 *            second y coord
 * @param z2
 *            second z coord
 * @return distance between coords
 */
public static double getDistance(float x1, float y1, float z1, float x2, float y2, float z2) {
    float dx = x1 - x2;
    float dy = y1 - y2;
    float dz = z1 - z2;

    // We should avoid Math.pow or Math.hypot due to perfomance reasons
    return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

From source file:Main.java

public static double[] magnititute(double accx[], double accy[], double accz[]) {
    if (!((accx.length == accy.length) && (accy.length == accz.length))) {
        return null;
    }//w w  w. j av  a  2  s.  co  m
    double result[] = new double[accx.length];
    for (int i = 0; i < accx.length; i++) {
        result[i] = 0.0;
        double grav = SensorManager.GRAVITY_EARTH;
        result[i] += Math.pow(accx[i] / grav, 2.0);
        result[i] += Math.pow(accy[i] / grav, 2.0);
        result[i] += Math.pow(accz[i] / grav, 2.0);
        result[i] = Math.sqrt(result[i]);
        result[i] *= 310;
    }

    return result;
}

From source file:Main.java

public static double norm(double[] a) {
    return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
}

From source file:controller.DistributionController.java

public static void logNormal(double mean, double stand, double size, double extra) {
    numerosGerados.clear();/*from   w w  w . j a  va2  s. c  o m*/
    double mu = Math.log(mean * mean / Math.sqrt(mean * mean + stand * stand));
    double sigma = Math.sqrt(Math.log((mean * mean + stand * stand) / (mean * mean)));
    LogNormalDistribution lg = new LogNormalDistribution(mu, sigma);
    Double n;
    for (int i = 0; i < size; i++) {
        n = extra + lg.sample();
        numerosGerados.add(n);
    }
}

From source file:Main.java

public static float mag(float a, float b) {
    return (float) Math.sqrt(a * a + b * b);
}

From source file:Main.java

public static float[] getRotationFromGyro(float[] values, float timestamp, float nowTimeStamp,
        float[] currentRotMatrix, boolean swapX, boolean swapY, boolean swapZ) {
    float[] deltaRotationVector = new float[4];
    if (timestamp != 0) {
        final float dT = (nowTimeStamp - timestamp) * NS2S;
        float axisX = swapX ? -values[0] : values[0];
        float axisY = swapY ? -values[1] : values[1];
        float axisZ = swapZ ? -values[2] : values[2];

        float omegaMagnitude = (float) Math.sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
        if (omegaMagnitude > 0.1f) {
            axisX /= omegaMagnitude;/*  w  ww  .jav a2  s. c o  m*/
            axisY /= omegaMagnitude;
            axisZ /= omegaMagnitude;
        }

        float thetaOverTwo = omegaMagnitude * dT / 2.0f;
        float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
        float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
        deltaRotationVector[0] = sinThetaOverTwo * axisX;
        deltaRotationVector[1] = sinThetaOverTwo * axisY;
        deltaRotationVector[2] = sinThetaOverTwo * axisZ;
        deltaRotationVector[3] = cosThetaOverTwo;
    }
    float[] deltaRotationMatrix = new float[16];
    SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
    return naivMatrixMultiply(currentRotMatrix, deltaRotationMatrix);
}