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:Main.java

public static double calculatePearsonCorrelation(List<Double> list1, List<Double> list2) {
    if (list1.size() != list2.size()) {
        System.err.println("Two lists must have the same dimensionality.");
        return 0;
    }/*from w ww.  ja v  a 2  s.c om*/
    double mean1 = calculateMean(list1);
    double mean2 = calculateMean(list2);

    double std1 = Math.sqrt(calculateVariance(list1, mean1));
    double std2 = Math.sqrt(calculateVariance(list2, mean2));

    double dividend = 0;
    for (int i = 0; i < list1.size(); i++) {
        dividend += (list1.get(i) - mean1) * (list2.get(i) - mean2);
    }
    dividend /= list1.size() - 1;

    //System.out.println(mean1+" "+std1+" "+mean2+" "+std2+" "+dividend);
    return dividend / (std1 * std2);
}

From source file:Main.java

public static double getDevicePhysicalSize(Context context) {
    int[] size = getRealScreenSize(context);
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    double x = Math.pow(size[0] / dm.xdpi, 2);
    double y = Math.pow(size[1] / dm.ydpi, 2);

    return Math.sqrt(x + y);
}

From source file:Main.java

public static boolean scaleImage(String origin_path, String result_path, int target_size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// ww w. ja v  a  2 s .c  o  m
    BitmapFactory.decodeFile(origin_path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int scale = (int) (Math.sqrt(imageWidth * imageHeight * 4 / target_size)) + 1;
    Bitmap bmp = null;
    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(origin_path, options);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(result_path);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            bmp.recycle();
            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Double getDistance(double lat1, double lat2, double lon1, double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth

    Double latDistance = Math.toRadians(lat2 - lat1);
    Double lonDistance = Math.toRadians(lon2 - lon1);
    Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c; // convert to meters

    double height = el1 - el2;

    distance = Math.pow(distance, 2) + Math.pow(height, 2);

    return Math.sqrt(distance);
}

From source file:Main.java

public static double gps2km(final double lat_a, final double lng_a, final double lat_b, final double lng_b) {
    if (!isGpsValid(lng_a, lat_a) || !isGpsValid(lng_b, lat_b)) {
        return -1;
    }/*from   w w  w.  j  a v  a  2 s .c  o  m*/
    final double radLat1 = (lat_a * Math.PI / 180.0);
    final double radLat2 = (lat_b * Math.PI / 180.0);
    final double a = radLat1 - radLat2;
    final double b = (lng_a - lng_b) * Math.PI / 180.0;
    final double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    return s * EARTH_RADIUS;
}

From source file:edu.oregonstate.eecs.mcplan.ml.ConstrainedKMeans.java

/**
 * @param args//from  w  ww.  ja v  a  2s.co m
 */
public static void main(final String[] args) {
    final RandomGenerator rng = new MersenneTwister(42);
    final int K = 2;
    final int d = 2;
    final ArrayList<RealVector> X = new ArrayList<RealVector>();
    final double u = 2.0;
    final double ell = 8.0;
    final double gamma = 10.0;

    for (final int s : new int[] { 0, 5, 10 }) {
        for (int x = -1; x <= 1; ++x) {
            for (int y = -1; y <= 1; ++y) {
                X.add(new ArrayRealVector(new double[] { x + s, y }));
            }
        }
    }

    final TIntObjectMap<Pair<int[], double[]>> M = new TIntObjectHashMap<Pair<int[], double[]>>();
    M.put(16, Pair.makePair(new int[] { 20 }, new double[] { 1.0 }));
    M.put(0, Pair.makePair(new int[] { 8 }, new double[] { 1.0 }));

    final TIntObjectMap<Pair<int[], double[]>> C = new TIntObjectHashMap<Pair<int[], double[]>>();
    C.put(13, Pair.makePair(new int[] { 20 }, new double[] { 1.0 }));
    C.put(10, Pair.makePair(new int[] { 16 }, new double[] { 1.0 }));

    final ArrayList<double[]> S = new ArrayList<double[]>();
    M.forEachKey(new TIntProcedure() {
        @Override
        public boolean execute(final int i) {
            final Pair<int[], double[]> p = M.get(i);
            if (p != null) {
                for (final int j : p.first) {
                    S.add(new double[] { i, j });
                }
            }
            return true;
        }
    });

    final ArrayList<double[]> D = new ArrayList<double[]>();
    C.forEachKey(new TIntProcedure() {
        @Override
        public boolean execute(final int i) {
            final Pair<int[], double[]> p = C.get(i);
            if (p != null) {
                for (final int j : p.first) {
                    D.add(new double[] { i, j });
                }
            }
            return true;
        }
    });

    final ConstrainedKMeans kmeans = new ConstrainedKMeans(K, d, X, M, C, rng) {
        RealMatrix A_ = MatrixUtils.createRealIdentityMatrix(d);
        double Dmax_ = 1.0;

        @Override
        public double distance(final RealVector x1, final RealVector x2) {
            final RealVector diff = x1.subtract(x2);
            return Math.sqrt(HilbertSpace.inner_prod(diff, A_, diff));
        }

        @Override
        public double distanceMax() {
            return Dmax_;
        }

        @Override
        protected void initializeDistanceFunction() {
            double max_distance = 0.0;
            for (int i = 0; i < X.size(); ++i) {
                for (int j = i + 1; j < X.size(); ++j) {
                    final double d = distance(X.get(i), X.get(j));
                    if (d > max_distance) {
                        max_distance = d;
                    }
                }
            }
            Dmax_ = max_distance;
        }

        @Override
        protected boolean updateDistanceFunction() {
            final InformationTheoreticMetricLearner itml = new InformationTheoreticMetricLearner(S, D, u, ell,
                    A_, gamma, rng_);
            itml.run();
            final double delta = A_.subtract(itml.A()).getFrobeniusNorm();
            A_ = itml.A();
            initializeDistanceFunction();
            return delta > convergence_tolerance_;
        }
    };

    kmeans.run();
    for (int i = 0; i < kmeans.mu().length; ++i) {
        System.out.println("Mu " + i + ": " + kmeans.mu()[i]);
        for (int j = 0; j < kmeans.assignments().length; ++j) {
            if (kmeans.assignments()[j] == i) {
                System.out.println("\tPoint " + X.get(j));
            }
        }
    }

}

From source file:Main.java

public static void normalize(double[] vector) {
    double sum = 0.0;
    for (double x : vector) {
        sum += x * x;/*from  w  w w  .ja  va 2 s  .co m*/
    }
    if (sum == 0) {
        return;
    }
    sum = Math.sqrt(sum);
    for (int i = 0; i < vector.length; i++) {
        vector[i] /= sum;
    }
}

From source file:Main.java

/**
 * Calculates the distance between two locations in KM
 *//*from w  w w. java 2s .c  om*/
public static double checkDistance(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 6371;

    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);

    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);

    double a = Math.pow(sindLat, 2)
            + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    double dist = earthRadius * c * 1000;

    return dist; // in meter
}

From source file:Main.java

public static double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    // Implementation of the Haversine distance formula
    lat1 = Math.toRadians(lat1);/*from w w w . j  a v a  2s. com*/
    lng1 = Math.toRadians(lng1);
    lat2 = Math.toRadians(lat2);
    lng2 = Math.toRadians(lng2);

    double dlon = lng2 - lng1;
    double dlat = lat2 - lat1;

    double a = Math.pow((Math.sin(dlat / 2)), 2)
            + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return 3958.75 * c; // 3958: Earth radius in miles
}

From source file:com.opengamma.analytics.math.interpolation.DistanceCalculator.java

public static double getDistance(final double[] x1, final double[] x2) {
    final int dim = x1.length;
    Validate.isTrue(dim == x2.length, "different dimensions");
    double sum = 0;
    double diff;/*w  w w .  ja va 2  s. c  o  m*/
    for (int i = 0; i < dim; i++) {
        diff = x1[i] - x2[i];
        sum += diff * diff;
    }
    return Math.sqrt(sum);
}