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

/** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal;
 * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */
public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
        String fieldName, BigDecimal stdDevMultiplier) {
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal squaredTotal = BigDecimal.ZERO;
    int count = 0;
    for (Map<String, Object> dataMap : dataList) {
        if (dataMap == null)
            continue;
        BigDecimal value = (BigDecimal) dataMap.get(fieldName);
        if (value == null)
            continue;
        total = total.add(value);//from w ww. j  a  v a2 s . co m
        squaredTotal = squaredTotal.add(value.multiply(value));
        count++;
    }
    if (count < 2)
        return null;

    BigDecimal countBd = new BigDecimal(count);
    BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP);
    double totalDouble = total.doubleValue();
    BigDecimal stdDev = new BigDecimal(Math
            .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1)));

    Map<String, BigDecimal> retMap = new HashMap<>(6);
    retMap.put("total", total);
    retMap.put("squaredTotal", squaredTotal);
    retMap.put("count", countBd);
    retMap.put("average", average);
    retMap.put("stdDev", stdDev);

    if (stdDevMultiplier != null)
        retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier)));

    return retMap;
}

From source file:com.freevariable.lancer.stat.Probability.java

public static double normalInverse(double mean, double variance, double a) {
    // XXX: sure would be smarter to memoize these
    return (new NormalDistribution(mean, Math.sqrt(variance))).inverseCumulativeProbability(a);
}

From source file:ch.unil.genescore.vegas.DistributionMethods.java

public static double chiSquared1dfCumulativeProbabilityUpperTail(double q) {
    double p;/*from  ww w  . j  a v a  2s.  c o  m*/
    if (q > 50) {
        double q2 = Math.sqrt(q);
        p = normalCumulativeProbabilityUpperTailApprox(q2) * 2;
    } else {
        p = 1 - chiSquared1df_.cumulativeProbability(q);
    }
    return (p);
}

From source file:drpc.KMeansDrpcQuery.java

private static double computeRootMeanSquareDeviation(double[] v, double[] w) {
    double distance = 0;
    for (int i = 0; i < v.length && i < w.length; i++) {
        distance += Math.pow((v[i] - w[i]), 2);
    }//w  ww  .j a v  a  2 s  .  co m
    return Math.sqrt(distance / (Math.min(v.length, w.length)));
}

From source file:com.opengamma.analytics.financial.model.option.pricing.tree.LeisenReimerLatticeSpecification.java

@Override
public double[] getParameters(final double spot, final double strike, final double timeToExpiry,
        final double volatility, final double interestRate, final int nSteps, final double dt) {
    Validate.isTrue((nSteps % 2 == 1), "The number of steps should be odd");
    final double sigmaRootT = volatility * Math.sqrt(timeToExpiry);
    final double d1 = (Math.log(spot / strike) + interestRate * timeToExpiry) / sigmaRootT + 0.5 * sigmaRootT;
    final double d2 = d1 - sigmaRootT;
    final double sig1 = d1 >= 0. ? 1. : -1.;
    final double sig2 = d2 >= 0. ? 1. : -1.;
    final double coef1 = d1 / (nSteps + 1. / 3. + 0.1 / (nSteps + 1.));
    final double coef2 = d2 / (nSteps + 1. / 3. + 0.1 / (nSteps + 1.));
    final double p1 = 0.5 + sig1 * 0.5 * Math.sqrt(1. - Math.exp(-coef1 * coef1 * (nSteps + 1. / 6.)));
    final double p2 = 0.5 + sig2 * 0.5 * Math.sqrt(1. - Math.exp(-coef2 * coef2 * (nSteps + 1. / 6.)));
    final double rr = Math.exp(interestRate * dt);
    final double upFactor = rr * p1 / p2;
    final double downFactor = (rr - p2 * upFactor) / (1 - p2);

    return new double[] { upFactor, downFactor, p2, 1 - p2 };
}

From source file:Main.java

/**
 * Gets the great circle distance in kilometers between two geographical points, using
 * the <a href="http://en.wikipedia.org/wiki/Haversine_formula">haversine formula</a>.
 *
 * @param latitude1 the latitude of the first point
 * @param longitude1 the longitude of the first point
 * @param latitude2 the latitude of the second point
 * @param longitude2 the longitude of the second point
 * @return the distance, in kilometers, between the two points
 *//*from   ww w.  j  a  v a 2  s  . c o m*/
public static float getDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
    double dLat = Math.toRadians(latitude2 - latitude1);
    double dLon = Math.toRadians(longitude2 - longitude1);
    double lat1 = Math.toRadians(latitude1);
    double lat2 = Math.toRadians(latitude2);
    double sqrtHaversineLat = Math.sin(dLat / 2);
    double sqrtHaversineLon = Math.sin(dLon / 2);
    double a = sqrtHaversineLat * sqrtHaversineLat
            + sqrtHaversineLon * sqrtHaversineLon * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return (float) (EARTH_RADIUS_KM * c);
}

From source file:Engine.BoundingBox.java

public BoundingBox(double x, double y, double z, double x1, double y1, double z1, boolean useDiagonals) {
    if (useDiagonals) {
        double sideXLen = x1 - x;
        double sideYLen = y1 - y;
        double halfDiagonal = Math.sqrt((sideXLen * sideXLen) + (sideYLen * sideYLen)) / 2;
        double centerX = x + (sideXLen / 2.0);
        double centerY = y + (sideYLen / 2.0);
        this.bottomLeftFront = new Vector3D(centerX - halfDiagonal, centerY - halfDiagonal, z);
        this.topRightBack = new Vector3D(centerX + halfDiagonal, centerY + halfDiagonal, z1);
    } else {/*  ww w  .  j  av  a  2s  .c  o m*/
        this.bottomLeftFront = new Vector3D(x, y, z);
        this.topRightBack = new Vector3D(x1, y1, z1);
    }
}

From source file:Main.java

public static double[] gcj02towgs84(double lng, double lat) {
    if (out_of_china(lng, lat)) {
        return new double[] { lng, lat };
    }/*  ww w .  j a va  2s  . co  m*/
    double dlat = transformlat(lng - 105.0, lat - 35.0);
    double dlng = transformlng(lng - 105.0, lat - 35.0);
    double radlat = lat / 180.0 * pi;
    double magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    double sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
    double mglat = lat + dlat;
    double mglng = lng + dlng;
    return new double[] { lng * 2 - mglng, lat * 2 - mglat };
}

From source file:Main.java

/**
 * Removes NoData cells from the DEM by passing a window over each cell and estimating the value using inverse-distance weighting.   
 * @param array: array to remove NoData cells from 
 * @param noDataVal: NoData value to search for and remove from array.  Should be an odd number so that the window is of equal size on both sides.
 * @param windowSize: Size of the search window
 * @return DEM data/*ww w  . j a  v a2 s  .c o m*/
 */
private static float[][] removeDemNoData(float[][] array, float noDataVal, int windowSize) {
    int half = (windowSize - 1) / 2;
    float distance;
    float weight;
    float[][] arrayOut = array;
    boolean noDataCellsRemaining = true;
    while (noDataCellsRemaining == true) {
        noDataCellsRemaining = false;
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[0].length; c++) {
                if (array[r][c] == noDataVal) {
                    float weightsum = 0;
                    float weightedvalsum = 0;
                    for (int x = 0 - half; x < 1 + half; x++) {
                        for (int y = 0 - half; y < 1 + half; y++) {
                            //skip the current cell
                            if (x == 0 && y == 0) {
                                continue;
                            }
                            //verify that the cell is in the DEM range
                            if (r + y >= array.length || r + y < 0 || c + x >= array[0].length || c + x < 0) {
                                continue;
                            }
                            //verify that the neighbor cell is not NoDATA, as this will break the IDW computation
                            if (array[r + y][c + x] != noDataVal) {
                                distance = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
                                weight = 1 / distance;
                                weightsum += weight;
                                weightedvalsum += array[r + y][c + x] * weight;
                                arrayOut[r][c] = weightedvalsum / weightsum;

                            }
                        }
                    }
                    if (arrayOut[r][c] == noDataVal) {
                        noDataCellsRemaining = true;
                    }
                }
            }
        }
    }
    return arrayOut;
}

From source file:Main.java

public static void getDisplayDpi(Context ctx) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
    double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);
    int screenInch = (int) Math.round(screenInches);
    Log.d("screeninch", String.valueOf(screenInch));
    int dapi = dm.densityDpi;
    Log.d("dapi", String.valueOf(dapi));
    try {/*from  w w w . j  av a2  s.  c  o  m*/
        switch (dm.densityDpi) {

        case DisplayMetrics.DENSITY_LOW:
            UI_DENSITY = 120;
            if (screenInch <= 7) {
                UI_SIZE = 4;

            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            UI_DENSITY = 160;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_HIGH:
            UI_DENSITY = 240;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;

        default:
            break;
        }
    } catch (Exception e) {
        // Caught exception here
    }
}