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.anhth12.util.WeightInitUtil.java

public static INDArray uniformBasedOnInAndOut(int[] shape, int nIn, int nOut) {
    double min = -4.0 * Math.sqrt(6.0 / (double) (nOut + nIn));
    double max = 4.0 * Math.sqrt(6.0 / (double) (nOut + nIn));
    return Nd4j.rand(shape, Distributions.uniform(new MersenneTwister(123), min, max));
}

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

@Test
public void test() {
    final int n = 15;
    final DoubleFunction1D[] f1 = HERMITE.getPolynomials(n);
    final DoubleFunction1D[] f2 = ORTHONORMAL.getPolynomials(n);
    final double x = 3.4;
    for (int i = 0; i < f1.length; i++) {
        assertEquals(/*w  ww.jav  a  2  s.  c  o m*/
                f1[i].evaluate(x)
                        / Math.sqrt(MathUtils.factorialDouble(i) * Math.pow(2, i) * Math.sqrt(Math.PI)),
                f2[i].evaluate(x), EPS);
    }
}

From source file:com.opengamma.strata.math.impl.function.special.OrthonormalHermitePolynomialFunctionTest.java

@Test
public void test() {
    final int n = 15;
    final DoubleFunction1D[] f1 = HERMITE.getPolynomials(n);
    final DoubleFunction1D[] f2 = ORTHONORMAL.getPolynomials(n);
    final double x = 3.4;
    for (int i = 0; i < f1.length; i++) {
        assertEquals(/*from   w w w  .j a  v a 2 s  .  c o m*/
                f1[i].applyAsDouble(x) / Math
                        .sqrt(CombinatoricsUtils.factorialDouble(i) * Math.pow(2, i) * Math.sqrt(Math.PI)),
                f2[i].applyAsDouble(x), EPS);
    }
}

From source file:com.cloudera.oryx.common.math.VectorMath.java

/**
 * @param x vector for whom norm to be calculated
 * @return the L2 norm of vector x/* w  w  w.  j  a v  a2s .  c  o m*/
 * @throws IllegalArgumentException if x is of 0 length
 */
public static double norm(float[] x) {
    double total = 0.0;
    for (float f : x) {
        total += (double) f * (double) f;
    }
    return Math.sqrt(total);
}

From source file:com.opengamma.strata.math.impl.regression.WeightedLeastSquaresRegressionTest.java

@Test
public void test() {
    final double a0 = 2.3;
    final double a1 = -4.5;
    final double a2 = 0.76;
    final double a3 = 3.4;
    final int n = 30;
    final double[][] x = new double[n][3];
    final double[] yIntercept = new double[n];
    final double[] yNoIntercept = new double[n];
    final double[][] w1 = new double[n][n];
    final double[] w2 = new double[n];
    double y, x1, x2, x3;
    for (int i = 0; i < n; i++) {
        x1 = i;//from w w w .  j  av a  2s .c o  m
        x2 = x1 * x1;
        x3 = Math.sqrt(x1);
        x[i] = new double[] { x1, x2, x3 };
        y = x1 * a1 + x2 * a2 + x3 * a3;
        yNoIntercept[i] = y;
        yIntercept[i] = y + a0;
        for (int j = 0; j < n; j++) {
            w1[i][j] = RANDOM.nextDouble();
        }
        w1[i][i] = 1.;
        w2[i] = 1.;
    }
    final WeightedLeastSquaresRegression wlsRegression = new WeightedLeastSquaresRegression();
    final OrdinaryLeastSquaresRegression olsRegression = new OrdinaryLeastSquaresRegression();
    try {
        wlsRegression.regress(x, (double[]) null, yNoIntercept, false);
        Assert.fail();
    } catch (final IllegalArgumentException e) {
        // Expected
    }
    LeastSquaresRegressionResult wls = wlsRegression.regress(x, w1, yIntercept, true);
    LeastSquaresRegressionResult ols = olsRegression.regress(x, yIntercept, true);
    assertRegressions(n, 4, wls, ols);
    wls = wlsRegression.regress(x, w1, yNoIntercept, false);
    ols = olsRegression.regress(x, yNoIntercept, false);
    assertRegressions(n, 3, wls, ols);
    wls = wlsRegression.regress(x, w2, yIntercept, true);
    ols = olsRegression.regress(x, yIntercept, true);
    assertRegressions(n, 4, wls, ols);
    wls = wlsRegression.regress(x, w2, yNoIntercept, false);
    ols = olsRegression.regress(x, yNoIntercept, false);
    assertRegressions(n, 3, wls, ols);
}

From source file:main.Draft_text_categorization.java

/**
 * /* www.j  a v a 2  s. co  m*/
 * http://stackoverflow.com/questions/3622112/vector-space-model-algorithm-in-java-to-get-the-similarity-score-between-two-peo
 * 
 * @param v1
 * @param v2
 * @return
 */
static double cosine_similarity(Map<String, Integer> v1, Map<String, Integer> v2) {
    Set<String> both = new HashSet<String>(v1.keySet());
    both.retainAll(v2.keySet());
    double sclar = 0, norm1 = 0, norm2 = 0;

    for (String k : both)
        sclar += v1.get(k) * v2.get(k);
    for (String k : v1.keySet())
        norm1 += v1.get(k) * v1.get(k);
    for (String k : v2.keySet())
        norm2 += v2.get(k) * v2.get(k);
    return sclar / Math.sqrt(norm1 * norm2);
}

From source file:conceptor.chaos.Lyapunov.java

private static double[] getTestPoint(DynamicalSystem system, double d0) {
    double[] testPoint = new double[system.getDimension()];
    double denom = Math.sqrt((new Integer(system.getDimension())).doubleValue());
    double[] x = system.getState();

    for (int i = 0; i < x.length; i++) {
        testPoint[i] = x[i] + d0 / denom;
    }/*  w ww  . j av a  2s .c o  m*/

    return testPoint;
}

From source file:com.opengamma.analytics.financial.var.EmpiricalDistributionVaRParameters.java

public EmpiricalDistributionVaRParameters(final double horizon, final double periods, final double quantile) {
    Validate.isTrue(horizon > 0, "horizon");
    Validate.isTrue(periods > 0, "periods");
    if (!ArgumentChecker.isInRangeInclusive(0, 1, quantile)) {
        throw new IllegalArgumentException("Quantile must be between 0 and 1");
    }/*from  ww  w .j a  v  a2s  .co  m*/
    _percentileCalculator = new PercentileCalculator(1 - quantile);
    _horizon = horizon;
    _periods = periods;
    _quantile = quantile;
    _mult = Math.sqrt(horizon / periods);
}

From source file:com.javachen.grab.common.math.VectorMath.java

/**
 * @param x vector for whom norm to be calculated
 * @return the L2 norm of vector x//ww w . j  a v  a2  s . co m
 * @throws IllegalArgumentException if x is of 0 length
 */
public static double norm(float[] x) {
    double total = 0.0;
    for (float f : x) {
        double d = (double) f;
        total += d * d;
    }
    return Math.sqrt(total);
}

From source file:net.adamjak.thomas.graph.application.commons.StatisticsUtils.java

public static DescriptiveStatistics statisticsWithoutExtremes(DescriptiveStatistics inputStatistics,
        GrubbsLevel grubbsLevel) throws IllegalArgumentException {
    if (inputStatistics == null || grubbsLevel == null)
        throw new IllegalArgumentException("Params inputStatistics and grubbsLevel can not be null.");

    int countInput = inputStatistics.getValues().length;
    Double avgInput = inputStatistics.getMean();
    Double stdInput = inputStatistics.getStandardDeviation();
    Double s = stdInput * Math.sqrt((countInput - 1.0) / countInput);
    Double criticalValue = grubbsLevel.getCriticalValue(countInput);

    DescriptiveStatistics outputStatistic = new DescriptiveStatistics();

    for (double inpVal : inputStatistics.getValues()) {
        double test = Math.abs(inpVal - avgInput) / s;

        if (test <= criticalValue) {
            outputStatistic.addValue(inpVal);
        }/* www  .j a v a  2  s .  c  o  m*/
    }

    return outputStatistic;
}