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:hivemall.utils.math.StatsUtils.java

/**
 * probit(p)=sqrt(2)erf^-1(2p-1)/*ww  w . j  a va 2  s. c o m*/
 * 
 * <pre>
 * probit(1)=INF, probit(0)=-INF, probit(0.5)=0
 * </pre>
 * 
 * @param p must be in [0,1]
 * @link http://en.wikipedia.org/wiki/Probit
 */
public static double probit(double p) {
    if (p < 0 || p > 1) {
        throw new IllegalArgumentException("p must be in [0,1]");
    }
    return Math.sqrt(2.d) * MathUtils.inverseErf(2.d * p - 1.d);
}

From source file:com.opengamma.analytics.financial.timeseries.analysis.DifferenceSignIIDHypothesis.java

@Override
public boolean testIID(final DoubleTimeSeries<?> x) {
    Validate.notNull(x, "x");
    final double[] data = x.valuesArrayFast();
    final int n = data.length;
    int t = 0;//from   ww  w.j  av a2s .c o m
    for (int i = 1; i < n; i++) {
        if (data[i] > data[i - 1]) {
            t++;
        }
    }
    final double mean = (n - 1) / 2.;
    final double std = Math.sqrt((n + 1) / 12.);
    return Math.abs(t - mean) / std < _criticalValue;
}

From source file:com.opengamma.analytics.math.function.special.OrthonormalHermitePolynomialFunction.java

@Override
public DoubleFunction1D[] getPolynomials(final int n) {
    Validate.isTrue(n >= 0);/*w w w .jav  a  2 s  . c om*/
    final DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1];
    for (int i = 0; i <= n; i++) {
        if (i == 0) {
            polynomials[i] = F0;
        } else if (i == 1) {
            polynomials[i] = polynomials[0].multiply(Math.sqrt(2)).multiply(getX());
        } else {
            polynomials[i] = polynomials[i - 1].multiply(getX()).multiply(Math.sqrt(2. / i))
                    .subtract(polynomials[i - 2].multiply(Math.sqrt((i - 1.) / i)));
        }
    }
    return polynomials;
}

From source file:IK.G.java

public static double dist(double x1, double y1, double x2, double y2) {
    return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

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

public static double distance(List<Double> pI, List<Double> pJ) {
    double[] auxArray = new double[pI.size()];

    for (int i = 0; i < pI.size(); i++) {
        auxArray[i] = pI.get(i) - pJ.get(i);
    }/*w  ww.  j  a va2 s.  c  o  m*/

    double sumSq = StatUtils.sumSq(auxArray);

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

From source file:Util.java

/**
 * Returns distance between two sets of coords
 * /*from  w  w  w.j ava2s .  c  om*/
 * @param x1
 *            first x coord
 * @param y1
 *            first y coord
 * @param x2
 *            second x coord
 * @param y2
 *            second y coord
 * @return distance between sets of coords
 */
public static double getDistance(float x1, float y1, float x2, float y2) {
    // using long to avoid possible overflows when multiplying
    double dx = x2 - x1;
    double dy = y2 - y1;

    // return Math.hypot(x2 - x1, y2 - y1); // Extremely slow
    // return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); // 20 times faster than hypot
    return Math.sqrt(dx * dx + dy * dy); // 10 times faster then previous line
}

From source file:com.raghav.plot.XYSeriesDemo.java

/**
 * A demonstration application showing an XY series containing a null value.
 *
 * @param title  the frame title.//from  ww  w  . j  a v a2 s  .  com
 */
public XYSeriesDemo(final String title) {

    super(title);
    final XYSeries series = new XYSeries("Random Data");
    //    float x=1;
    //    float y=(float)((Math.sqrt(2))*x);
    //    

    for (int i = 1; i < 200000; i++) {
        double x = (((double) (i)) / 1000);
        System.out.print("x = " + x);
        double xdb = 10 * (Math.log10(x));
        System.out.print("\t 10logx=" + xdb);

        double y = Erf.erfc(Math.sqrt(x));
        System.out.print("\t y=" + y);
        System.out.println("----------------------");
        series.add(xdb, y);
    }

    final XYSeriesCollection data = new XYSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart("XY Series Demo", "X", "Y", data,
            PlotOrientation.VERTICAL, true, true, false);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);

}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiHellingerDistanceScore.java

@Override
public double score(double[][][] input) {

    double dist[][] = new double[2][];

    int bins = 1000;

    dist[0] = MatrixUtils.createRealMatrix(input[0]).getColumn(0);
    dist[1] = MatrixUtils.createRealMatrix(input[1]).getColumn(0);

    gpsiHistogram hist = new gpsiHistogram();
    double globalMin = (new Min()).evaluate(ArrayUtils.addAll(dist[0], dist[1]));
    double globalMax = (new Max()).evaluate(ArrayUtils.addAll(dist[0], dist[1]));

    double[] h0 = hist.distribution(dist[0], bins, globalMin, globalMax);
    double[] h1 = hist.distribution(dist[1], bins, globalMin, globalMax);

    double BC = 0.0;

    for (int i = 0; i < bins; i++)
        BC += Math.sqrt(h0[i] * h1[i]);

    return Math.sqrt(1 - BC);

}

From source file:com.insightml.math.distributions.GaussianDistribution.java

public GaussianDistribution(final double mean, final double stddev) {
    this.mean = mean;
    this.stddev = stddev;

    sigmaSquare = stddev * stddev;/* w w w  . j  a v a2s  .  co  m*/
    factor = 1. / Math.sqrt(2 * Math.PI * sigmaSquare);
}

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

/**
 * Overloaded getParameters method /*from  ww  w .j  ava 2 s .  c o m*/
 * @param vol Volatility
 * @param nu Computed by getShiftedDrift method
 * @param spaceStep Space step
 * @return {(modified time step), (up probability)} 
 */
public double[] getParameters(final double vol, final double nu, final double spaceStep) {
    final double[] res = new double[2];

    final double volSq = vol * vol;
    final double nuSq = nu * nu;
    res[0] = 0.5 * (-volSq + Math.sqrt(volSq * volSq + 4. * nuSq * spaceStep * spaceStep)) / nuSq;
    res[1] = 0.5 + 0.5 * nu * res[0] / spaceStep;

    return res;
}