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.example.PJS.java

public static double NORMSDIST(double z) {
    double sign = 1;
    if (z < 0)
        sign = -1;/*  w w w.j  a  va  2  s  .  com*/
    return 0.5 * (1.0 + sign * erf(Math.abs(z) / Math.sqrt(2)));
}

From source file:br.unicamp.ic.recod.gpsi.applications.gpsiOVOClassifierFromFiles.java

public gpsiOVOClassifierFromFiles(String datasetPath, gpsiDatasetReader datasetReader, Byte[] classLabels,
        String outputPath, String programsPath, double errorScore) throws Exception {
    super(datasetPath, datasetReader, classLabels, outputPath, errorScore);

    int nClasses, i, j;
    gpsiClassifier[][] classifiers;/*from w ww.j a  va  2  s  . c om*/
    File dir = new File(programsPath + "5/");

    BufferedReader reader;
    File[] files = dir.listFiles((File dir1, String name) -> name.toLowerCase().endsWith(".program"));

    nClasses = (int) Math.ceil(Math.sqrt(2 * files.length));
    classifiers = new gpsiClassifier[nClasses - 1][];

    String[] labels;

    for (i = 0; i < classifiers.length; i++)
        classifiers[i] = new gpsiClassifier[classifiers.length - i];

    for (File program : files) {
        reader = new BufferedReader(new FileReader(program));
        labels = program.getName().split("[_.]");
        i = Integer.parseInt(labels[0]) - 1;
        j = Integer.parseInt(labels[1]) - i - 2;
        classifiers[i][j] = new gpsiClassifier(
                new gpsiScalarSpectralIndexDescriptor(
                        new gpsiStringParserVoxelCombiner(null, reader.readLine())),
                new gpsi1NNToMomentScalarClassificationAlgorithm(new Mean()));
        reader.close();
    }

    ensemble = new gpsiOVOEnsembleMethod(classifiers);

}

From source file:com.opengamma.analytics.financial.model.option.DistributionFromImpliedVolatility.java

public DistributionFromImpliedVolatility(final double forward, final double maturity,
        final Function1D<Double, Double> impliedVolFunction) {
    Validate.isTrue(maturity > 0.0, "maturity <= 0");
    Validate.isTrue(forward > 0.0, "forward <= 0");
    Validate.notNull(impliedVolFunction, "implied vol function");
    _f = forward;//www.  j  av  a 2s  .  com
    _volFunc = impliedVolFunction;
    _rootT = Math.sqrt(maturity);
}

From source file:Main.java

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
        int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 128
            : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.  
        return lowerBound;
    }// ww w  .  j  av a  2s  . c o m

    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

From source file:edu.byu.nlp.stats.GammaDistribution.java

/**
 * self-contained gamma generator. Multiply result with scale parameter (or
 * divide by rate parameter). After Teh (npbayes).
 * /*from w w w . j a  va2  s.c  o m*/
 * Taken From knowceans.
 */
public static double sample(double shape, RandomGenerator rnd) {
    Preconditions.checkArgument(shape > 0.0);
    Preconditions.checkNotNull(rnd);

    if (shape == 1.0) {
        /* Exponential */
        return -Math.log(rnd.nextDouble());
    } else if (shape < 1.0) {
        /* Use Johnk's generator */
        double cc = 1.0 / shape;
        double dd = 1.0 / (1.0 - shape);
        while (true) {
            double xx = Math.pow(rnd.nextDouble(), cc);
            double yy = xx + Math.pow(rnd.nextDouble(), dd);
            if (yy <= 1.0) {
                // FIXME: assertion error for rr = 0.010817814317923407
                // assert yy != 0 && xx / yy > 0 : "rr = " + rr;
                // INFO: this if is a hack
                if (yy != 0 && xx / yy > 0) {
                    return -Math.log(rnd.nextDouble()) * xx / yy;
                }
            }
        }
    } else { /* rr > 1.0 */
        /* Use bests algorithm */
        double bb = shape - 1.0;
        double cc = 3.0 * shape - 0.75;
        while (true) {
            double uu = rnd.nextDouble();
            double vv = rnd.nextDouble();
            double ww = uu * (1.0 - uu);
            double yy = Math.sqrt(cc / ww) * (uu - 0.5);
            double xx = bb + yy;
            if (xx >= 0) {
                double zz = 64.0 * ww * ww * ww * vv * vv;
                assert zz > 0 && bb != 0 && xx / bb > 0;
                if ((zz <= (1.0 - 2.0 * yy * yy / xx))
                        || (Math.log(zz) <= 2.0 * (bb * Math.log(xx / bb) - yy))) {
                    return xx;
                }
            }
        }
    }
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.SharpeRatio.java

public Object calculate(List<Map<String, Object>> rowSet)
        throws CalculationException, PrefixAndSuffixException {
    if (rowSet.size() == 0) {
        throw new CalculationException("SharpeRatio??");
    }/*from  w ww  .  j a  va2  s.  c  o m*/

    double[] values = new double[rowSet.size()];
    double sum = 0;
    for (int i = 0; i < rowSet.size(); i++) {
        Map<String, Object> row = rowSet.get(i);
        Double value = valuePas.getValue(row);
        values[i] = value;
        sum += value;
    }
    double avg = sum / (rowSet.size());
    Variance var = new Variance(false);
    Double result = Math.sqrt(var.evaluate(values, avg));
    if (log.isDebugEnabled()) {
        log.debug("FRR[" + avg + "],STDEV[" + result + "]");
    }
    return avg / result;
}

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

public static double perpendicularDistance(List<Double> pI, List<Double> pJ) {

    double[] pJarray = new double[pJ.size()];

    for (int i = 0; i < pJ.size(); i++) {
        pJarray[i] = pJ.get(i);/*  w  ww . ja  va 2 s .  c o  m*/
    }

    double pJmodule = (StatUtils.sumSq(pJarray) != 0.0 ? Math.sqrt(StatUtils.sumSq(pJarray)) : 0.0);

    return (pJmodule != 0.0 ? innerProduct(pI, pJ) / pJmodule : 0.0);

}

From source file:com.facebook.stats.cardinality.TestHyperLogLog.java

@Test(groups = "slow")
public void testError() throws Exception {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    int buckets = 2048;
    for (int i = 0; i < 10000; ++i) {
        HyperLogLog estimator = new HyperLogLog(buckets);
        Set<Long> randomSet = makeRandomSet(5 * buckets);
        for (Long value : randomSet) {
            estimator.add(value);/* www. jav a2 s  .co  m*/
        }

        double error = (estimator.estimate() - randomSet.size()) * 1.0 / randomSet.size();
        stats.addValue(error);
    }

    assertTrue(stats.getMean() < 1e-2);
    assertTrue(stats.getStandardDeviation() < 1.04 / Math.sqrt(buckets));
}

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

@Override
protected DoublesPair getCentralNodePair(final double dt, final double sigma, final double forward,
        final double centreLevel) {
    final double sigma2dt = sigma * sigma * dt;
    final double b = 2 * centreLevel;
    final double c = forward * (2 * centreLevel - forward) - sigma2dt;
    final double root = b * b - 4 * c;
    Validate.isTrue(root >= 0, "can't find upper node - root negative");
    final double upper = (b + Math.sqrt(root)) / 2;
    final double lower = 2 * centreLevel - upper;
    return new DoublesPair(lower, upper);
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.StandardDeviation.java

public Object calculate(List<Map<String, Object>> rowSet)
        throws CalculationException, PrefixAndSuffixException {
    if (rowSet.size() == 0) {
        throw new CalculationException("SharpeRatio??");
    }//from ww w .j  a  v a  2  s  .  co  m
    double[] values = new double[rowSet.size()];
    double sum = 0;
    for (int i = 0; i < rowSet.size(); i++) {
        Map<String, Object> row = rowSet.get(i);
        Double value = valuePas.getValue(row);
        values[i] = value;
        sum += value;
    }
    double avg = sum / rowSet.size();
    Variance var = new Variance(false);
    return Math.sqrt(var.evaluate(values, avg));
}