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:eu.cassandra.utils.Utils.java

/**
 * This function is estimating the absolute euclidean distance of the active
 * and reactive power vector distance of two points of interest in the form of
 * arrays.//from w ww.ja va 2  s.  c  o m
 * 
 * @param a1
 *          The first array of values
 * @param a2
 *          The second array of values
 * 
 * @return the estimated absolute euclidean distance.
 */
public static double absoluteEuclideanDistance(double[] a1, double[] a2) {
    return Math.sqrt(Math.pow(a1[0] - a2[0], 2) + Math.pow(a1[1] - a2[1], 2));
}

From source file:hivemall.utils.math.StatsUtils.java

/**
 * @return value of probabilistic density function
 *//*from   w w w .  ja  v  a2  s  .c  o m*/
public static double pdf(final double x, final double x_hat, final double sigma) {
    if (sigma == 0.d) {
        return 0.d;
    }
    double diff = x - x_hat;
    double numerator = Math.exp(-0.5d * diff * diff / sigma);
    double denominator = Math.sqrt(2.d * Math.PI) * Math.sqrt(sigma);
    return numerator / denominator;
}

From source file:com.davidbracewell.math.distribution.NormalDistribution.java

@Override
public double probability(double x) {
    return 1d / Math.sqrt(TWO_PI * var) * Math.exp(-Math.pow(x - mean, 2) / (2 * var));
}

From source file:net.sf.maltcms.chromaui.features.spi.FeatureTable.java

public static RealMatrix normalize(RealMatrix sourceMatrix, boolean center, boolean normalize) {
    RealMatrix normalized = MatrixUtils.createRealMatrix(sourceMatrix.getRowDimension(),
            sourceMatrix.getColumnDimension());
    for (int col = 0; col < sourceMatrix.getColumnDimension(); col++) {
        double[] columnVector = sourceMatrix.getColumn(col);
        double mean = StatUtils.mean(columnVector);
        double stdev = Math.sqrt(StatUtils.variance(columnVector, mean));
        Logger.getLogger(FeatureTable.class.getName()).log(Level.INFO, "column {0}, mean={1} stdev={2}",
                new Object[] { col, mean, stdev });
        for (int j = 0; j < columnVector.length; j++) {
            normalized.setEntry(j, col, (sourceMatrix.getEntry(j, col) - mean) / stdev);
        }/*from w w w . j av  a 2s  .c om*/
    }
    return normalized;
}

From source file:com.itemanalysis.psychometrics.polycor.PearsonCorrelation.java

/**
 * Correct correlation for spuriousness. This method assumes that
 * the test item is Y and the test score is X. Used for the
  * point-biserial and biserial correlation in an item analysis.
 *
 * @return correlation corrected for spuriousness
 *//* ww  w.j  ava 2 s .c  om*/
public Double correctedValue() {
    double testSd = sdX.getResult();
    double itemSd = sdY.getResult();
    double rOld = this.value();
    double denom = Math.sqrt(itemSd * itemSd + testSd * testSd - 2 * rOld * itemSd * testSd);
    if (denom == 0.0)
        return Double.NaN;
    return (rOld * testSd - itemSd) / denom;
}

From source file:edu.illinois.cs.cogcomp.saul.test.TestReal.java

public static double getPearsonCorrelation(double[] scores1, double[] scores2) {
    double result;
    double sum_sq_x = 0;
    double sum_sq_y = 0;
    double sum_coproduct = 0;
    double mean_x = scores1[0];
    double mean_y = scores2[0];

    for (int i = 2; i < scores1.length + 1; i += 1) {
        double sweep = (double) (i - 1) / i;
        double delta_x = scores1[i - 1] - mean_x;
        double delta_y = scores2[i - 1] - mean_y;
        sum_sq_x += delta_x * delta_x * sweep;
        sum_sq_y += delta_y * delta_y * sweep;
        sum_coproduct += delta_x * delta_y * sweep;
        mean_x += delta_x / i;// ww w  .  java2 s .  c  om
        mean_y += delta_y / i;
    }

    double pop_sd_x = Math.sqrt(sum_sq_x / scores1.length);
    double pop_sd_y = Math.sqrt(sum_sq_y / scores1.length);

    double cov_x_y = sum_coproduct / scores1.length;
    result = cov_x_y / (pop_sd_x * pop_sd_y);

    return result;
}

From source file:net.atos.ari.vital.taastrustcalculator.StatisticsCalculator.java

public boolean calculateNumericVariance(double[] values) {
    double alpha = 0.05;
    double mean = StatUtils.mean(values);
    double variance = StatUtils.variance(values);
    double expected = Math.pow(mean * 0.05, 2);
    //double expected = 0.01;
    double degFreedom = values.length - 1.0;

    double T = (degFreedom * variance) / expected;
    logger.debug("Mean = " + mean);
    logger.debug("Standard Deviation = " + Math.sqrt(variance));
    logger.debug("Test Statistic calculated T = " + T);

    if (degFreedom <= 0)
        return false;
    ChiSquaredDistribution myDist = new ChiSquaredDistribution(degFreedom);
    double myTLeft = myDist.inverseCumulativeProbability(alpha / 2.0);
    double myTRight = myDist.inverseCumulativeProbability(1.0 - alpha / 2.0);

    logger.debug("Boundaries: " + myTLeft + " to " + myTRight);

    // Determine if z score is in the region of acceptance
    if ((myTLeft <= T) && (T <= myTRight)) {
        // H0 -> Variance of the data is equal to the expected one
        return true;
    }//from   w  ww.  j av a 2  s .c  o m

    // H1 -> Variance of the data is different to the expected one
    return false;
}

From source file:Main.java

public 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;
    }/*from   ww  w  . j  ava  2s.c  o  m*/

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

From source file:com.google.location.lbs.gnss.gps.pseudorange.EcefToTopocentricConverter.java

/**
 * Transformation of {@code inputVectorMeters} with origin at {@code originECEFMeters} into
 * topocentric coordinate system. The result is {@code TopocentricAEDValues} containing azimuth
 * from north +ve clockwise, radians; elevation angle, radians; distance, vector length meters
 *
 * <p>Source: http://www.navipedia.net/index.php/Transformations_between_ECEF_and_ENU_coordinates
 * http://kom.aau.dk/~borre/life-l99/topocent.m
 *
 *//*from w  ww.  j av  a2 s  .c o m*/
public static TopocentricAEDValues convertCartesianToTopocentericRadMeters(final double[] originECEFMeters,
        final double[] inputVectorMeters) {

    GeodeticLlaValues latLngAlt = Ecef2LlaConverter.convertECEFToLLACloseForm(originECEFMeters[0],
            originECEFMeters[1], originECEFMeters[2]);

    RealMatrix rotationMatrix = Ecef2EnuConverter
            .getRotationMatrix(latLngAlt.latitudeRadians, latLngAlt.longitudeRadians).transpose();
    double[] eastNorthUpVectorMeters = GpsMathOperations
            .matrixByColVectMultiplication(rotationMatrix.transpose().getData(), inputVectorMeters);
    double eastMeters = eastNorthUpVectorMeters[EAST_IDX];
    double northMeters = eastNorthUpVectorMeters[NORTH_IDX];
    double upMeters = eastNorthUpVectorMeters[UP_IDX];

    // calculate azimuth, elevation and height from the ENU values
    double horizontalDistanceMeters = Math.hypot(eastMeters, northMeters);
    double azimuthRadians;
    double elevationRadians;

    if (horizontalDistanceMeters < MIN_DISTANCE_MAGNITUDE_METERS) {
        elevationRadians = Math.PI / 2.0;
        azimuthRadians = 0;
    } else {
        elevationRadians = Math.atan2(upMeters, horizontalDistanceMeters);
        azimuthRadians = Math.atan2(eastMeters, northMeters);
    }
    if (azimuthRadians < 0) {
        azimuthRadians += 2 * Math.PI;
    }

    double distanceMeters = Math.sqrt(Math.pow(inputVectorMeters[0], 2) + Math.pow(inputVectorMeters[1], 2)
            + Math.pow(inputVectorMeters[2], 2));
    return new TopocentricAEDValues(elevationRadians, azimuthRadians, distanceMeters);
}

From source file:com.xy.inc.service.impl.POIServiceImpl.java

@Override
public List<POI> buscarPOISProximos(Integer coordenadaX, Integer coordenadaY, Integer distMax)
        throws ServiceException {
    try {//from  w ww.java 2  s .co m
        List<POI> poiList = new ArrayList<>();
        for (POI poi : poiRepository.findAll()) {
            if (Math.sqrt(Math.pow(coordenadaX - poi.getCoordenadaX(), 2)
                    + Math.pow(coordenadaY - poi.getCoordenadaY(), 2)) <= 10) {
                poiList.add(poi);
            }
        }
        return poiList;
    } catch (Exception e) {
        throw new ServiceException("No foi possivel buscar os POIs proximos");
    }
}