Example usage for java.lang Double isNaN

List of usage examples for java.lang Double isNaN

Introduction

In this page you can find the example usage for java.lang Double isNaN.

Prototype

public static boolean isNaN(double v) 

Source Link

Document

Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

Usage

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

@Override
public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues) {

    ArgumentChecker.notNull(xValues, "xValues");
    ArgumentChecker.notNull(yValues, "yValues");

    ArgumentChecker.isTrue(xValues.length == yValues.length, "xValues length = yValues length");
    ArgumentChecker.isTrue(xValues.length > 1, "Data points should be more than 1");

    final int nDataPts = xValues.length;

    for (int i = 0; i < nDataPts; ++i) {
        ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity");
        ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity");
    }/*from  w  w  w .  j a  v  a 2s.  com*/

    for (int i = 0; i < nDataPts; ++i) {
        for (int j = i + 1; j < nDataPts; ++j) {
            ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
        }
    }

    double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
    double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
    ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt);

    final DoubleMatrix2D coefMatrix = solve(xValuesSrt, yValuesSrt);

    for (int i = 0; i < coefMatrix.getNumberOfRows(); ++i) {
        for (int j = 0; j < coefMatrix.getNumberOfColumns(); ++j) {
            ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input");
        }
        double ref = 0.;
        final double interval = xValuesSrt[i + 1] - xValuesSrt[i];
        for (int j = 0; j < 2; ++j) {
            ref += coefMatrix.getData()[i][j] * Math.pow(interval, 1 - j);
            ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input");
        }
        final double bound = Math.max(Math.abs(ref) + Math.abs(yValuesSrt[i + 1]), 1.e-1);
        ArgumentChecker.isTrue(Math.abs(ref - yValuesSrt[i + 1]) < ERROR * bound,
                "Input is too large/small or data are not distinct enough");
    }

    return new PiecewisePolynomialResult(new DoubleMatrix1D(xValuesSrt), coefMatrix,
            coefMatrix.getNumberOfColumns(), 1);
}

From source file:Main.java

static double distanceFromArc(double dA, double dB, double dAB) {
    // In spherical trinagle ABC
    // a is length of arc BC, that is dB
    // b is length of arc AC, that is dA
    // c is length of arc AB, that is dAB
    // We rename parameters so following formulas are more clear:
    double a = dB;
    double b = dA;
    double c = dAB;

    // First, we calculate angles alpha and beta in spherical triangle ABC
    // and based on them we decide how to calculate the distance:
    if (Math.sin(b) * Math.sin(c) == 0.0 || Math.sin(c) * Math.sin(a) == 0.0) {
        // It probably means that one of distance is n*pi, which gives around 20000km for n = 1,
        // unlikely for Denmark, so we should be fine.
        return -1.0;
    }/*from w w w . j ava2  s . c o  m*/

    double alpha = Math.acos((Math.cos(a) - Math.cos(b) * Math.cos(c)) / (Math.sin(b) * Math.sin(c)));
    double beta = Math.acos((Math.cos(b) - Math.cos(c) * Math.cos(a)) / (Math.sin(c) * Math.sin(a)));

    // It is possible that both sinuses are too small so we can get nan when dividing with them
    if (Double.isNaN(alpha) || Double.isNaN(beta)) {
        // double cosa = cos(a);
        // double cosbc = cos(b) * cos(c);
        // double minus1 = cosa - cosbc;
        // double sinbc = sin(b) * sin(c);
        // double div1 = minus1 / sinbc;
        //
        // double cosb = cos(b);
        // double cosca = cos(a) * cos(c);
        // double minus2 = cosb - cosca;
        // double sinca = sin(a) * sin(c);
        // double div2 = minus2 / sinca;

        return -1.0;
    }

    // If alpha or beta are zero or pi, it means that C is on the same circle as arc AB,
    // we just need to figure out if it is between AB:
    if (alpha == 0.0 || beta == 0.0) {
        return (dA + dB > dAB) ? Math.min(dA, dB) : 0.0;
    }

    // If alpha is obtuse and beta is acute angle, then
    // distance is equal to dA:
    if (alpha > Math.PI / 2 && beta < Math.PI / 2)
        return dA;

    // Analogously, if beta is obtuse and alpha is acute angle, then
    // distance is equal to dB:
    if (beta > Math.PI / 2 && alpha < Math.PI / 2)
        return dB;

    // If both alpha and beta are acute or both obtuse or one of them (or both) are right,
    // distance is the height of the spherical triangle ABC:

    // Again, unlikely, since it would render at least pi/2*EARTH_RADIUS_IN_METERS, which is too much.
    if (Math.cos(a) == 0.0)
        return -1;

    double x = Math.atan(-1.0 / Math.tan(c) + (Math.cos(b) / (Math.cos(a) * Math.sin(c))));

    // Similar to previous edge cases...
    if (Math.cos(x) == 0.0)
        return -1.0;

    return Math.acos(Math.cos(a) / Math.cos(x));
}

From source file:com.opengamma.analytics.math.function.PiecewisePolynomialWithSensitivityFunction1D.java

/** 
 * @param pp {@link PiecewisePolynomialResultsWithSensitivity}
 * @param xKey /* w  w w .  j  av a 2  s.  c om*/
 * @return Node sensitivity value at x=xKey
 */
public DoubleMatrix1D nodeSensitivity(final PiecewisePolynomialResultsWithSensitivity pp, final double xKey) {
    ArgumentChecker.notNull(pp, "null pp");
    ArgumentChecker.isFalse(Double.isNaN(xKey), "xKey containing NaN");
    ArgumentChecker.isFalse(Double.isInfinite(xKey), "xKey containing Infinity");

    if (pp.getDimensions() > 1) {
        throw new NotImplementedException();
    }

    final double[] knots = pp.getKnots().getData();
    final int nKnots = knots.length;
    int interval = FunctionUtils.getLowerBoundIndex(knots, xKey);
    if (interval == nKnots - 1) {
        interval--; // there is 1 less interval that knots
    }

    final double s = xKey - knots[interval];
    final DoubleMatrix2D a = pp.getCoefficientSensitivity(interval);
    final int nCoefs = a.getNumberOfRows();

    DoubleMatrix1D res = a.getRowVector(0);
    for (int i = 1; i < nCoefs; i++) {
        res = (DoubleMatrix1D) MA.scale(res, s);
        res = (DoubleMatrix1D) MA.add(res, a.getRowVector(i));
    }

    return res;
}

From source file:com.github.mobile.gauges.ui.airtraffic.AirTrafficPusherCallback.java

@Override
public void onEvent(final JSONObject eventData) {
    double longitude = eventData.optDouble("longitude");
    if (Double.isNaN(longitude))
        return;/*ww  w. j  av a2  s .c  om*/
    double latitude = eventData.optDouble("latitude");
    if (Double.isNaN(latitude))
        return;

    String id = getString(eventData, "site_id");
    if (id == null || id.length() == 0)
        return;

    String city = getString(eventData, "city");
    String region = getString(eventData, "region");
    String country = getString(eventData, "country");
    String title = getString(eventData, "title");
    onHit(new Hit(id, title, (float) longitude, (float) latitude, city, region, country));
}

From source file:de.hs.mannheim.modUro.model.StatisticValues.java

private void init(double[] array) {
    for (int i = 0; i < array.length; i++) {
        if (!Double.isNaN(array[i])) {
            stats.addValue(array[i]);/* ww w .  j a v a 2s . co  m*/
        }
    }

    this.mean = stats.getMean();
    this.variance = stats.getVariance();
    this.stdDev = stats.getStandardDeviation();
    this.firstPercentile = stats.getPercentile(25);
    this.secondPercentile = stats.getPercentile(50);
    this.lastPercentile = stats.getPercentile(75);
    this.min = stats.getMin();
    this.max = stats.getMax();
}

From source file:net.sf.mzmine.modules.peaklistmethods.dataanalysis.rtmzplots.RTMZRenderer.java

@Override
public Paint getItemPaint(int series, int item) {

    double cv = dataset.getZValue(series, item);
    if (Double.isNaN(cv))
        return new Color(255, 0, 0);

    return paintScale.getPaint(cv);

}

From source file:com.clust4j.kernel.LogKernel.java

@Override
public double getSimilarity(final double[] a, final double[] b) {
    final double sup = -(super.getSimilarity(a, b)); // super returns negative, so reverse it
    final double answer = -FastMath.log(sup + 1);
    return Double.isNaN(answer) ? Double.NEGATIVE_INFINITY : answer;
}

From source file:com.davidsoergel.stats.SimpleXYZSeries.java

public void addPoint(double x, double y, double z) throws StatsException {
    if (Double.isNaN(x) || Double.isInfinite(x)) {
        //throw new StatsException("Invalid x value in SimpleXYZSeries: " + x);
        logger.warn("Invalid x value in SimpleXYZSeries: " + x);
        return;/*from   www  . j a  v a2s . c o m*/
    }
    if (Double.isNaN(y) || Double.isInfinite(y)) {
        //throw new StatsException("Invalid y value in SimpleXYZSeries: " + y);
        logger.warn("Invalid y value in SimpleXYZSeries: " + y);
        return;
    }
    if (Double.isNaN(z) || Double.isInfinite(z)) {
        //throw new StatsException("Invalid z value in SimpleXYZSeries: " + z);
        logger.warn("Invalid z value in SimpleXYZSeries: " + z);
        return;
    }
    points.put(x, y, new XYZPoint(x, y, z));
    updateBounds(x, y, z);
}

From source file:dr.math.distributions.PoissonDistribution.java

public double logPdf(double x) {

    double pdf = distribution.probability(x);
    if (pdf == 0 || Double.isNaN(pdf)) { // bad estimate
        final double mean = mean();
        return x * Math.log(mean) - Poisson.gammln(x + 1) - mean;
    }//from   w  w w .ja  v  a2s  . co m
    return Math.log(pdf);

}

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>// ww w  . jav  a 2 s .  co  m
 *  <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;
}