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.insightml.math.distributions.GaussianDistribution.java

public GaussianDistribution(final double[] values, final boolean biasedVariance) {
    final int n = values.length;
    double sum = 0;
    double sumSquared = 0;
    for (final double value : values) {
        sum += value;//from ww  w .j ava2s. co  m
        sumSquared += value * value;
    }

    mean = sum / values.length;
    if (biasedVariance) {
        sigmaSquare = sumSquared / n - mean * mean;
    } else {
        sigmaSquare = sumSquared / (n - 1) - n * mean * mean / (n - 1);
    }
    stddev = Math.sqrt(sigmaSquare);

    factor = 1. / Math.sqrt(2 * Math.PI * sigmaSquare);
}

From source file:com.itemanalysis.psychometrics.irt.estimation.RaschScaleQualityStatistics.java

/**
 * Observed standard deviation of the estimate.
 *
 * @return stanmdard deviation./* w  w  w  . j a  va 2 s  .  c  om*/
 */
public double observedStandardDeviation() {
    return Math.sqrt(var.getResult());
}

From source file:Main.java

/**
 * Normalizes elements in 'u' by dividing by max and computes the norm2 of the normalized
 * array u.  Adjust the sign of the returned value depending on the size of the first
 * element in 'u'. Normalization is done to avoid overflow.
 *
 * <pre>//w  w w .j ava2  s .co  m
 * for i=j:numRows
 *   u[i] = u[i] / max
 *   tau = tau + u[i]*u[i]
 * end
 * tau = sqrt(tau)
 * if( u[j] < 0 )
 *    tau = -tau;
 * </pre>
 *
 * @param j Element in 'u' that it starts at.
 * @param numRows Element in 'u' that it stops at.
 * @param u Array
 * @param max Max value in 'u' that is used to normalize it.
 * @return norm2 of 'u'
 */
public static double computeTauAndDivide(final int j, final int numRows, final double[] u, final double max) {
    double tau = 0;
    //        double div_max = 1.0/max;
    //        if( Double.isInfinite(div_max)) {
    for (int i = j; i < numRows; i++) {
        double d = u[i] /= max;
        tau += d * d;
    }
    //        } else {
    //            for( int i = j; i < numRows; i++ ) {
    //                double d = u[i] *= div_max;
    //                tau += d*d;
    //            }
    //        }
    tau = Math.sqrt(tau);

    if (u[j] < 0)
        tau = -tau;

    return tau;
}

From source file:ComplexDemo.java

/** Return the magnitude of a complex number */
public double magnitude() {
    return Math.sqrt(r * r + i * i);
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.Utils.java

public static int calculateBrightness(final Color color) {
    return (int) Math.sqrt(color.getRed() * color.getRed() * .241d + color.getGreen() * color.getGreen() * .691d
            + color.getBlue() * color.getBlue() * .068d);
}

From source file:fr.ign.cogit.geoxygene.sig3d.gui.window.result.DissimilarityCalculationDialog.java

/**
 * Affiche un graphique  l'aide de 2 nuages de points
 * /*from   w  w  w.  j  a v  a 2  s. c  o  m*/
 * @param title the frame title.
 */
public DissimilarityCalculationDialog(final String title, IDirectPositionList dpl1, IDirectPositionList dpl2) {

    super();
    final XYSeries series = new XYSeries("Objet 1");
    int nbElem = dpl1.size();

    for (int i = 0; i < nbElem - 1; i++) {

        series.add((dpl1.get(i + 1).getX() + dpl1.get(i).getX()) / 2, dpl1.get(i).getY());

    }

    final XYSeries series2 = new XYSeries("Objet 2");
    int nbElem2 = dpl2.size();

    for (int i = 0; i < nbElem2 - 1; i++) {

        series2.add((dpl2.get(i + 1).getX() + dpl2.get(i).getX()) / 2, dpl2.get(i).getY());

    }

    double valeur = 0;
    // Affiche la diffrence en norme L2 des 2 graphiques
    for (int i = 0; i < nbElem; i++) {

        valeur = valeur + Math.pow(dpl1.get(i).getY() - dpl2.get(i).getY(), 2);

    }

    valeur = Math.sqrt(valeur) / (1024 * 512);

    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);
    dataset.addSeries(series2);

    final JFreeChart chart = ChartFactory.createXYLineChart("XY Series Demo", "Distance : " + valeur,
            Messages.getString("Result.PointFD"), dataset, PlotOrientation.VERTICAL, true, true, false);

    final ChartPanel chartPanel = new ChartPanel(chart);

    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    this.setContentPane(chartPanel);

}

From source file:ardufocuser.starfocusing.Utils.java

/**
 * Calcula la distancia euclidea de dos puntos, dado sus coordenadas.
 *///  w  ww .jav a  2 s  .  com
public static double computeDistance(int p1x, int p1y, int p2x, int p2y) {

    int cat1 = Math.abs(p1x - p2x);
    int cat2 = Math.abs(p1y - p2y);
    double dis = Math.sqrt((Math.pow(cat1, 2) + Math.pow(cat2, 2)));
    return dis;

}

From source file:com.opengamma.analytics.math.statistics.estimation.StudentTDistributionMaximumLikelihoodEstimator.java

@Override
public ProbabilityDistribution<Double> evaluate(final double[] x) {
    Validate.notNull(x, "x");
    ArgumentChecker.notEmpty(x, "x");
    final double[] standardized = getStandardizedData(x);
    final Function1D<Double, Double> f = new Function1D<Double, Double>() {

        @SuppressWarnings("synthetic-access")
        @Override//from  w  w  w . j ava2s. c o  m
        public Double evaluate(final Double nu) {
            double sum = 0;
            for (final double t : standardized) {
                sum += Math.log(_gamma.evaluate((nu + 1) / 2.) * Math.pow(1 + t * t / (nu - 2), -(nu + 1) / 2.)
                        / Math.sqrt(Math.PI * (nu - 2)) / _gamma.evaluate(nu / 2.));
            }
            return -sum;
        }

    };
    return new StudentTDistribution(_minimizer.minimize(f, 0.0, 3., 10.));
}

From source file:net.sf.dsp4j.octave.packages.signal_1_2_0.NCauer.java

public NCauer(double Rp, double Rs, int n) {

    // Cutoff frequency = 1:
    double wp = 1;

    // Stop band edge ws:
    double ws = __ellip_ws(n, Rp, Rs);

    double k = wp / ws;
    double k1 = Math.sqrt(1.0 - Math.pow(k, 2));
    double q0 = (1.0 / 2.0) * ((1.0 - Math.sqrt(k1)) / (1 + Math.sqrt(k1)));
    double q = q0 + 2.0 * Math.pow(q0, 5) + 15.0 * Math.pow(q0, 9) + 150.0 * Math.pow(q0, 13); //%(....)
    double D = (Math.pow(10, 0.1 * Rs) - 1.0) / (Math.pow(10, (0.1 * Rp)) - 1.0);

    //Filter order maybe this, but not used now:
    //n=ceil(log10(16*D)/log10(1/q))

    double l = (1.0 / (2.0 * n)) * Math.log((Math.pow(10, 0.05 * Rp) + 1.0) / (Math.pow(10, 0.05 * Rp) - 1.0));
    double sig01 = 0;
    double sig02 = 0;
    for (int m = 0; m <= 30; m++) {
        sig01 = sig01 + Math.pow((-1), m) * Math.pow(q, (m * (m + 1))) * Math.sinh((2 * m + 1) * l);
    }/*  ww w  . j  av  a  2  s  .c  om*/
    for (int m = 1; m <= 30; m++) {
        sig02 = sig02 + Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2))) * Math.cosh(2 * m * l);
    }
    double sig0 = Math.abs((2.0 * Math.pow(q, (1.0 / 4.0)) * sig01) / (1.0 + 2.0 * sig02));

    double w = Math.sqrt((1.0 + k * Math.pow(sig0, 2)) * (1.0 + Math.pow(sig0, 2) / k));

    int r;
    if (n % 2 != 0) {
        r = (n - 1) / 2;
    } else {
        r = n / 2;
    }

    double[] wi = new double[r];
    for (int ii = 1; ii <= r; ii++) {
        double mu;
        if (n % 2 != 0) {
            mu = ii;
        } else {
            mu = (double) ii - 0.5;
        }
        double soma1 = 0;
        for (int m = 0; m <= 30; m++) {
            soma1 = soma1 + 2.0 * Math.pow(q, (1.0 / 4.0)) * (Math.pow(-1.0, m) * Math.pow(q, (m * (m + 1)))
                    * Math.sin(((2.0 * m + 1.0) * Math.PI * mu) / n));
        }
        double soma2 = 0;
        for (int m = 1; m <= 30; m++) {
            soma2 = soma2 + 2.0 * (Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2)))
                    * Math.cos((2.0 * m * Math.PI * mu) / n));
        }
        wi[ii - 1] = (soma1 / (1.0 + soma2));
    }

    double[] Vi = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        Vi[i] = Math.sqrt((1.0 - (k * (Math.pow(wi[i], 2)))) * (1.0 - (Math.pow(wi[i], 2)) / k));
    }
    double[] A0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        A0i[i] = 1.0 / (Math.pow(wi[i], 2));
    }
    double[] sqrA0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        sqrA0i[i] = 1.0 / wi[i];
    }

    double[] B0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        B0i[i] = (Math.pow((sig0 * Vi[i]), 2) + Math.pow((w * wi[i]), 2))
                / Math.pow((1.0 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)), 2);
    }

    double C01;
    if (wi.length == 0) {
        C01 = 1.0;
    } else {
        C01 = B0i[0] / A0i[0];
        for (int i = 1; i < wi.length; i++) {
            C01 *= B0i[i] / A0i[i];
        }
    }

    //Gain T0:
    if (n % 2 != 0) {
        T0 = sig0 * C01 * Math.sqrt(ws);
    } else {
        T0 = Math.pow(10, (-0.05 * Rp)) * C01;
    }

    //zeros:
    zer = new Complex[sqrA0i.length * 2];
    for (int i = 0; i < sqrA0i.length; i++) {
        zer[i] = Complex.valueOf(0.0, sqrA0i[i]);
        zer[i + sqrA0i.length] = Complex.valueOf(0.0, -sqrA0i[i]);
    }

    //poles:
    pol = new Complex[Vi.length * 2];
    for (int i = 0; i < Vi.length; i++) {
        pol[i] = new Complex(-2.0 * sig0 * Vi[i], 2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
        pol[i + Vi.length] = new Complex(-2.0 * sig0 * Vi[i], -2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
    }

    //If n odd, there is a real pole  -sig0:
    if (n % 2 != 0) {
        pol = Arrays.copyOf(pol, pol.length + 1);
        pol[pol.length - 1] = new Complex(-sig0);
    }

    for (int i = 0; i < pol.length; i++) {
        pol[i] = pol[i].multiply(Math.sqrt(ws));
    }
    for (int i = 0; i < zer.length; i++) {
        zer[i] = zer[i].multiply(Math.sqrt(ws));
    }

}

From source file:MathFunc.java

/**
 * Returns the arc sine of an angle, in the range of <code>-Math.PI/2</code> through
 * <code>Math.PI/2</code>.  Special cases:
 * <ul>/*from   ww w  .  j  a  v  a  2 s.c om*/
 *  <li>If the argument is <code>NaN</code> or its absolute value is greater than 1,
 *      then the result is <code>NaN</code>.
 *  <li>If the argument is zero, then the result is a zero with the same sign
 *      as the argument.
 * </ul>
 * 
 * @param a - the value whose arc sine is to be returned.
 * @return the arc sine of the argument.
 */
public static double asin(double a) {
    // Special cases.
    if (Double.isNaN(a) || Math.abs(a) > 1.0) {
        return Double.NaN;
    }

    if (a == 0.0) {
        return a;
    }

    // Calculate the arc sine.
    double aSquared = a * a;
    double arcSine = atan2(a, Math.sqrt(1 - aSquared));
    return arcSine;
}