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

private static boolean unprotectedTest(int i) {
    for (int n = 2, max = (int) Math.ceil(Math.sqrt(i)); n <= max; n++) {
        if (i % n == 0) {
            return false;
        }/*from w w w. ja  va2  s.  c  om*/
    }
    return true;
}

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

@Override
public ProbabilityDistribution<Double> evaluate(final double[] x) {
    Validate.notNull(x, "x");
    ArgumentChecker.notEmpty(x, "x");
    final double m1 = _first.evaluate(x);
    return new NormalDistribution(m1, Math.sqrt(_second.evaluate(x) - m1 * m1));
}

From source file:com.rapidminer.tools.math.LinearRegression.java

/** Performs a weighted linear ridge regression. */
public static double[] performRegression(Matrix x, Matrix y, double[] weights, double ridge) {
    Matrix weightedIndependent = new Matrix(x.getRowDimension(), x.getColumnDimension());
    Matrix weightedDependent = new Matrix(x.getRowDimension(), 1);
    for (int i = 0; i < weights.length; i++) {
        double sqrtWeight = Math.sqrt(weights[i]);
        for (int j = 0; j < x.getColumnDimension(); j++) {
            weightedIndependent.set(i, j, x.get(i, j) * sqrtWeight);
        }//from   ww w  .  j  a  v  a2  s  . c  o m
        weightedDependent.set(i, 0, y.get(i, 0) * sqrtWeight);
    }
    return performRegression(weightedIndependent, weightedDependent, ridge);
}

From source file:org.apache.usergrid.client.QueryTestCase.java

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return (float) (earthRadius * c);
}

From source file:com.fpuna.preproceso.util.Util.java

public static double[] transform(double[] input) {
    //double[] tempConversion = new double[input.length];
    double[] tempConversion = new double[2048];

    FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    try {//from www .j a v a 2s . com
        Complex[] complx = transformer.transform(input, TransformType.FORWARD);

        for (int i = 0; i < complx.length; i++) {
            double rr = (complx[i].getReal());
            double ri = (complx[i].getImaginary());

            tempConversion[i] = Math.sqrt((rr * rr) + (ri * ri));
        }

    } catch (IllegalArgumentException e) {
        System.out.println(e);
    }

    return tempConversion;
}

From source file:MathFunc.java

/**
 * Returns the arc cosine of an angle, in the range of 0.0 through <code>Math.PI</code>.
 * Special case:// w ww  .  j av a  2 s . c  o  m
 * <ul>
 *  <li>If the argument is <code>NaN</code> or its absolute value is greater than 1,
 *      then the result is <code>NaN</code>.
 * </ul>
 * 
 * @param a - the value whose arc cosine is to be returned.
 * @return the arc cosine of the argument.
 */
public static double acos(double a) {
    // Special case.
    if (Double.isNaN(a) || Math.abs(a) > 1.0) {
        return Double.NaN;
    }

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

From source file:com.atomiton.watermanagement.ngo.util.WaterMgmtNGOUtility.java

public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
    int R = 6371;
    double phi1 = deg2rad(lat1);
    double phi2 = deg2rad(lat2);
    double lambda1 = deg2rad(lon1);
    double lambda2 = deg2rad(lon2);

    double x = (lambda2 - lambda1) * Math.cos((phi1 + phi2) / 2.0);
    double y = phi2 - phi1;
    double distKm = Math.sqrt(x * x + y * y) * R;

    //return distKm / 1.60934; // in miles
    return distKm;
}

From source file:joinery.impl.Display.java

public static <C extends Container, V> C draw(final DataFrame<V> df, final C container, final PlotType type) {
    final List<XChartPanel> panels = new LinkedList<>();
    final DataFrame<Number> numeric = df.numeric().fillna(0);
    final int rows = (int) Math.ceil(Math.sqrt(numeric.size()));
    final int cols = numeric.size() / rows + 1;

    final List<Object> xdata = new ArrayList<>(df.length());
    final Iterator<Object> it = df.index().iterator();
    for (int i = 0; i < df.length(); i++) {
        final Object value = it.hasNext() ? it.next() : i;
        if (value instanceof Number || value instanceof Date) {
            xdata.add(value);//  w  ww .j  a  v a  2s  . co m
        } else if (PlotType.BAR.equals(type)) {
            xdata.add(String.valueOf(value));
        } else {
            xdata.add(i);
        }
    }

    if (EnumSet.of(PlotType.GRID, PlotType.GRID_WITH_TREND).contains(type)) {
        for (final Object col : numeric.columns()) {
            final Chart chart = new ChartBuilder().chartType(chartType(type)).width(800 / cols)
                    .height(800 / cols).title(String.valueOf(col)).build();
            final Series series = chart.addSeries(String.valueOf(col), xdata, numeric.col(col));
            if (type == PlotType.GRID_WITH_TREND) {
                addTrend(chart, series, xdata);
                series.setLineStyle(SeriesLineStyle.NONE);
            }
            chart.getStyleManager().setLegendVisible(false);
            chart.getStyleManager().setDatePattern(dateFormat(xdata));
            panels.add(new XChartPanel(chart));
        }
    } else {
        final Chart chart = new ChartBuilder().chartType(chartType(type)).build();

        chart.getStyleManager().setDatePattern(dateFormat(xdata));
        switch (type) {
        case SCATTER:
        case SCATTER_WITH_TREND:
        case LINE_AND_POINTS:
            break;
        default:
            chart.getStyleManager().setMarkerSize(0);
            break;
        }

        for (final Object col : numeric.columns()) {
            final Series series = chart.addSeries(String.valueOf(col), xdata, numeric.col(col));
            if (type == PlotType.SCATTER_WITH_TREND) {
                addTrend(chart, series, xdata);
                series.setLineStyle(SeriesLineStyle.NONE);
            }
        }

        panels.add(new XChartPanel(chart));
    }

    if (panels.size() > 1) {
        container.setLayout(new GridLayout(rows, cols));
    }
    for (final XChartPanel p : panels) {
        container.add(p);
    }

    return container;
}

From source file:org.jfree.chart.demo.NormalDistributionDemo2.java

public static XYDataset createDataset() {
    XYSeriesCollection xyseriescollection = new XYSeriesCollection();
    NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(0.0D, 1.0D);
    org.jfree.data.xy.XYSeries xyseries = DatasetUtilities.sampleFunction2DToSeries(
            normaldistributionfunction2d, -5.0999999999999996D, 5.0999999999999996D, 121, "N1");
    xyseriescollection.addSeries(xyseries);
    NormalDistributionFunction2D normaldistributionfunction2d1 = new NormalDistributionFunction2D(0.0D,
            Math.sqrt(0.20000000000000001D));
    org.jfree.data.xy.XYSeries xyseries1 = DatasetUtilities.sampleFunction2DToSeries(
            normaldistributionfunction2d1, -5.0999999999999996D, 5.0999999999999996D, 121, "N2");
    xyseriescollection.addSeries(xyseries1);
    NormalDistributionFunction2D normaldistributionfunction2d2 = new NormalDistributionFunction2D(0.0D,
            Math.sqrt(5D));// ww  w . j a v  a2  s  . c om
    org.jfree.data.xy.XYSeries xyseries2 = DatasetUtilities.sampleFunction2DToSeries(
            normaldistributionfunction2d2, -5.0999999999999996D, 5.0999999999999996D, 121, "N3");
    xyseriescollection.addSeries(xyseries2);
    NormalDistributionFunction2D normaldistributionfunction2d3 = new NormalDistributionFunction2D(-2D,
            Math.sqrt(0.5D));
    org.jfree.data.xy.XYSeries xyseries3 = DatasetUtilities.sampleFunction2DToSeries(
            normaldistributionfunction2d3, -5.0999999999999996D, 5.0999999999999996D, 121, "N4");
    xyseriescollection.addSeries(xyseries3);
    return xyseriescollection;
}

From source file:ufmotionsuite.SpiralGraph.java

public void getData(double[] x, double[] y, int length, double[] theta1) {
    for (int i = 0; i < length; i++) {
        xCoord[i] = x[i];/*from   w w  w .  jav  a 2 s. c  o  m*/
        yCoord[i] = y[i];
    }
    arrLength = length;

    for (int i = 3; i < arrLength; i++) {
        //CHANGE TO BE DISTANCE FROM ORIGIN TO POINT
        r[i] = Math.sqrt(Math.pow(xCoord[i] - xCoord[2], 2.0) + Math.pow(yCoord[i] - yCoord[2], 2.0));
        theta[i] = theta1[i];
        System.out.println("R is: " + r[i]);
        System.out.println(" Theta is: " + theta[i]);
    }

    Graph();

}