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:bachelorthesis.methods.detection.bayesian.BayesianDetection.java

private double[][] offlineCpd(Value[] data) {
    int n = data.length;
    double[] Q = new double[n];
    double[] g = new double[n];
    double[] G = new double[n];
    double[][] P = new double[n][n];

    Arrays.fill(g, Math.log(1.d / (data.length + 1)));
    G[0] = g[0];//from   w  ww  .ja v  a 2s  .c om
    for (int i = 1; i < G.length; i++) {
        G[i] = Math.log((Math.exp(G[i - 1]) + Math.exp(g[i])));
    }
    for (double[] array : P) {
        Arrays.fill(array, Double.NEGATIVE_INFINITY);
    }

    P[n - 1][n - 1] = gaussianObsLogLikelihood(data, n - 1, n);
    Q[n - 1] = P[n - 1][n - 1];

    for (int t = n - 2; t >= 0; t--) {
        double p_next_cp = Double.NEGATIVE_INFINITY;
        for (int s = t; s < n - 1; s++) {
            P[t][s] = gaussianObsLogLikelihood(data, t, s + 1);
            double summand = P[t][s] + Q[s + 1] + g[s + 1 - t];
            p_next_cp = Math.log((Math.exp(p_next_cp) + Math.exp(summand)));
            if (summand - p_next_cp < BAYESIAN_TRUNCATE) {
                break;
            }
        }
        P[t][n - 1] = gaussianObsLogLikelihood(data, t, n);
        double antiG;
        if (G[n - 1 - t] < -1e-15) {
            antiG = Math.log(1.d - Math.exp(G[n - 1 - t]));
        } else {
            antiG = Math.log(-G[n - 1 - t]);
        }
        Q[t] = Math.log((Math.exp(p_next_cp) + Math.exp(P[t][n - 1] + antiG)));
    }

    double[][] Pcp = new double[n - 1][n - 1];
    for (double[] array : Pcp) {
        Arrays.fill(array, Double.NEGATIVE_INFINITY);
    }
    for (int t = 0; t < n - 1; t++) {
        Pcp[0][t] = P[0][t] + Q[t + 1] + g[t] - Q[0];
        if (Double.isNaN(Pcp[0][t])) {
            Pcp[0][t] = Double.NEGATIVE_INFINITY;
        }
    }
    for (int j = 1; j < n - 1; j++) {
        for (int t = j; t < n - 1; t++) {
            double[] tmp_cond = copyOfRange(Pcp[j - 1], j - 1, t);

            tmp_cond = add(tmp_cond, getSameEntryOfAllArrays(copyOfRange(P, j, t + 1), t));
            double summand = Q[t + 1];
            tmp_cond = forEach(tmp_cond, value -> value + summand);
            tmp_cond = add(tmp_cond, copyOfRange(g, 0, t - j + 1));
            double[] negativePart = forEach(copyOfRange(Q, j, t + 1), value -> -value);
            tmp_cond = add(tmp_cond, negativePart);

            double[] tempArray = forEach(tmp_cond, value -> Math.exp(value));
            Pcp[j][t] = Math.log(sum(tempArray));
            if (Double.isNaN(Pcp[j][t])) {
                Pcp[j][t] = Double.NEGATIVE_INFINITY;
            }
        }
    }
    return Pcp;
}

From source file:com.sun.japex.report.ChartGenerator.java

/**
 * Create a chart for a single test case across all drivers.
 *//*  ww  w. j a  v  a2s  .c o m*/
public JFreeChart createTrendChart(String testCaseName) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    final int size = _reports.size();
    for (int i = 0; i < size; i++) {
        TestSuiteReport report = _reports.get(i);
        SimpleDateFormat formatter = _dateFormatter;

        // If previous or next are on the same day, include time
        if (i > 0 && onSameDate(report, _reports.get(i - 1))) {
            formatter = _dateTimeFormatter;
        }
        if (i + 1 < size && onSameDate(report, _reports.get(i + 1))) {
            formatter = _dateTimeFormatter;
        }

        List<TestSuiteReport.Driver> drivers = report.getDrivers();
        for (TestSuiteReport.Driver driver : drivers) {
            TestSuiteReport.TestCase testCase = driver.getTestCase(testCaseName);
            if (testCase != null) {
                double value = testCase.getResult();
                if (!Double.isNaN(value)) {
                    dataset.addValue(value, driver.getName(), formatter.format(report.getDate().getTime()));
                }
            }
        }
    }

    JFreeChart chart = ChartFactory.createLineChart(testCaseName, "", "", dataset, PlotOrientation.VERTICAL,
            true, true, false);
    configureLineChart(chart);
    chart.setAntiAlias(true);

    return chart;
}

From source file:com.ning.metrics.collector.util.Stats.java

/**
 * @return max//  w  w  w  .j  ava2s.  c  o m
 */
@Managed
@SuppressWarnings("unused")
public double getSizeMax() {
    double max = sizeStats.getMax();
    return Double.isNaN(max) ? 0.0 : max;
}

From source file:edu.uci.ics.jung.algorithms.layout.FRLayout.java

protected synchronized void calcPositions(V v) {
    FRVertexData fvd = getFRData(v);/* w w w .  ja  v  a 2  s.  com*/
    if (fvd == null)
        return;
    Point2D xyd = transform(v);
    double deltaLength = Math.max(EPSILON, fvd.norm());

    double newXDisp = fvd.getX() / deltaLength * Math.min(deltaLength, temperature);

    if (Double.isNaN(newXDisp)) {
        throw new IllegalArgumentException("Unexpected mathematical result in FRLayout:calcPositions [xdisp]");
    }

    double newYDisp = fvd.getY() / deltaLength * Math.min(deltaLength, temperature);
    xyd.setLocation(xyd.getX() + newXDisp, xyd.getY() + newYDisp);

    double borderWidth = getSize().getWidth() / 50.0;
    double newXPos = xyd.getX();
    if (newXPos < borderWidth) {
        newXPos = borderWidth + Math.random() * borderWidth * 2.0;
    } else if (newXPos > (getSize().getWidth() - borderWidth)) {
        newXPos = getSize().getWidth() - borderWidth - Math.random() * borderWidth * 2.0;
    }

    double newYPos = xyd.getY();
    if (newYPos < borderWidth) {
        newYPos = borderWidth + Math.random() * borderWidth * 2.0;
    } else if (newYPos > (getSize().getHeight() - borderWidth)) {
        newYPos = getSize().getHeight() - borderWidth - Math.random() * borderWidth * 2.0;
    }

    xyd.setLocation(newXPos, newYPos);
}

From source file:de.biomedical_imaging.ij.nanotrackj.WalkerMethodEstimator.java

private double logK(double k) {
    if (!Double.isNaN(logMapK[(int) k])) {
        return logMapK[(int) k];
    }/* w  w w.j a v  a 2  s.c  o  m*/
    logMapK[(int) k] = Math.log(k);
    return logMapK[(int) k];
}

From source file:com.opengamma.analytics.math.minimization.QuasiNewtonVectorMinimizer.java

private boolean getNextPosition(final Function1D<DoubleMatrix1D, Double> function,
        final Function1D<DoubleMatrix1D, DoubleMatrix1D> grad, final DataBundle data) {
    final DoubleMatrix1D p = getDirection(data);
    if (data.getLambda0() < 1.0) {
        data.setLambda0(1.0);/*from  www.ja  v  a 2  s  . co  m*/
    } else {
        data.setLambda0(data.getLambda0() * BETA);
    }
    updatePosition(p, function, data);
    final double g1 = data.getG1();
    // the function is invalid at the new position, try to recover
    if (Double.isInfinite(g1) || Double.isNaN(g1)) {
        bisectBacktrack(p, function, data);
    }
    if (data.getG1() > data.getG0() / (1 + ALPHA * data.getLambda0())) {
        quadraticBacktrack(p, function, data);
        int count = 0;
        while (data.getG1() > data.getG0() / (1 + ALPHA * data.getLambda0())) {
            if (count > 5) {
                return false;
            }
            cubicBacktrack(p, function, data);
            count++;
        }
    }
    final DoubleMatrix1D deltaX = data.getDeltaX();
    data.setX((DoubleMatrix1D) MA.add(data.getX(), deltaX));
    data.setG0(data.getG1());
    final DoubleMatrix1D gradNew = grad.evaluate(data.getX());
    data.setDeltaGrad((DoubleMatrix1D) MA.subtract(gradNew, data.getGrad()));
    data.setGrad(gradNew);
    return true;
}

From source file:com.joptimizer.algebra.Matrix1NornRescaler.java

/**
 * Scaling factors for symmetric (not singular) matrices.
 * Just the subdiagonal elements of the matrix are required.
 * @see Daniel Ruiz, "A scaling algorithm to equilibrate both rows and columns norms in matrices"
 * @see Philip A. Knight, Daniel Ruiz, Bora Ucar "A Symmetry Preserving Algorithm for Matrix Scaling"
 *//*from   w  w w  .  ja  v  a  2 s.  c o m*/
@Override
public DoubleMatrix1D getMatrixScalingFactorsSymm(DoubleMatrix2D A) {
    DoubleFactory1D F1 = DoubleFactory1D.dense;
    DoubleFactory2D F2 = DoubleFactory2D.sparse;
    int dim = A.columns();
    DoubleMatrix1D D1 = F1.make(dim, 1);
    DoubleMatrix2D AK = A.copy();
    DoubleMatrix2D DR = F2.identity(dim);
    DoubleMatrix1D DRInv = F1.make(dim);
    int maxIteration = 50;
    for (int k = 0; k <= maxIteration; k++) {
        double normR = -Double.MAX_VALUE;
        for (int i = 0; i < dim; i++) {
            double dri = getRowInfinityNorm(AK, i);
            DR.setQuick(i, i, Math.sqrt(dri));
            DRInv.setQuick(i, 1. / Math.sqrt(dri));
            normR = Math.max(normR, Math.abs(1 - dri));
            if (Double.isNaN(normR)) {
                throw new IllegalArgumentException("matrix is singular");
            }
        }

        if (normR < eps) {
            break;
        }

        for (int i = 0; i < dim; i++) {
            double prevD1I = D1.getQuick(i);
            double newD1I = prevD1I * DRInv.getQuick(i);
            D1.setQuick(i, newD1I);
        }

        if (k == maxIteration) {
            log.warn("max iteration reached");
        }

        AK = ColtUtils.diagonalMatrixMult(DRInv, AK, DRInv);
    }

    return D1;
}

From source file:edu.pitt.csb.mgm.IndTestMixedLrtMut.java

/**
 * @return true if the given independence question is judged true, false if not. The independence question is of the
 * form x _||_ y | z, z = <z1,...,zn>, where x, y, z1,...,zn are searchVariables in the list returned by
 * getVariableNames()./*from   ww w.j  a  va 2  s. c om*/
 */
public boolean isIndependent(Node x, Node y, List<Node> z) {
    boolean ind = isIndependentOneWay(x, y, z);

    if (ind && mode == "min")
        ind = isIndependentOneWay(y, x, z);
    else if (!ind && mode == "max")
        ind = isIndependentOneWay(y, x, z);
    else if (mode == "avg") {
        double p1 = lastP;
        isIndependentOneWay(y, x, z);
        double p2 = lastP;
        if (Double.isNaN(p1))
            p1 = p2;
        if (Double.isNaN(p2))
            p2 = p1;
        double avg = (p1 + p2) / 2.0;

        if (Double.isNaN(avg))
            ind = false;
        else
            ind = avg > alpha;
    }

    return ind;
}

From source file:gedi.util.MathUtils.java

public static double saveMax(double a, double b) {
    if (Double.isNaN(a))
        return b;
    if (Double.isNaN(b))
        return a;
    if (Double.isInfinite(a))
        return b;
    if (Double.isInfinite(b))
        return a;
    return Math.max(a, b);
}

From source file:net.semanticmetadata.lire.filters.LsaFilter.java

/**
 * @param results/*from   ww w  .  ja  va  2s  .co  m*/
 * @param query
 * @return the filtered results or null if error occurs.
 */
public ImageSearchHits filter(ImageSearchHits results, IndexReader reader, Document query) {
    // create a double[items][histogram]
    tempFeature = null;
    LinkedList<double[]> features = new LinkedList<double[]>();
    try {
        tempFeature = (LireFeature) featureClass.newInstance();
    } catch (Exception e) {
        logger.severe("Could not create feature " + featureClass.getName() + " (" + e.getMessage() + ").");
        return null;
    }
    // get all features from the result set, take care of those that do not have the respective field.
    for (int i = 0; i < results.length(); i++) {
        Document d = null;
        try {
            d = reader.document(results.documentID(i));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (d.getField(fieldName) != null) {
            tempFeature.setByteArrayRepresentation(d.getField(fieldName).binaryValue().bytes,
                    d.getField(fieldName).binaryValue().offset, d.getField(fieldName).binaryValue().length);
            features.add(tempFeature.getFeatureVector());
        }
    }
    // now go for the query
    if (query.getField(fieldName) != null) {
        tempFeature.setByteArrayRepresentation(query.getField(fieldName).binaryValue().bytes,
                query.getField(fieldName).binaryValue().offset, query.getField(fieldName).binaryValue().length);
    } else {
        logger.severe("Query document is missing the given feature " + featureClass.getName() + ".");
        return null;
    }
    double[][] matrixData = new double[features.size() + 1][tempFeature.getFeatureVector().length];
    System.arraycopy(tempFeature.getFeatureVector(), 0, matrixData[0], 0,
            tempFeature.getFeatureVector().length);
    int count = 1;
    for (Iterator<double[]> iterator = features.iterator(); iterator.hasNext();) {
        double[] next = iterator.next();
        System.arraycopy(next, 0, matrixData[count], 0, next.length);
        count++;
    }
    for (int i = 0; i < matrixData.length; i++) {
        double[] doubles = matrixData[i];
        for (int j = 0; j < doubles.length; j++) {
            if (Double.isNaN(doubles[j]))
                System.err.println("Value is NaN");
            ;
        }
    }
    // create a matrix object and do the magic
    Array2DRowRealMatrix m = new Array2DRowRealMatrix(matrixData);
    long ms = System.currentTimeMillis();
    SingularValueDecomposition svd = new SingularValueDecomposition(m);
    ms = System.currentTimeMillis() - ms;
    double[] singularValues = svd.getSingularValues();
    RealMatrix s = svd.getS();
    // if no number of dimensions is given reduce to a tenth.
    if (numberOfDimensions < 1)
        numberOfDimensions = singularValues.length / 10;
    for (int i = numberOfDimensions; i < singularValues.length; i++) {
        s.setEntry(i, i, 0);
    }
    RealMatrix mNew = svd.getU().multiply(s).multiply(svd.getVT());
    double[][] data = mNew.getData();

    // create the new result set
    TreeSet<SimpleResult> result = new TreeSet<SimpleResult>();
    double maxDistance = 0;
    double[] queryData = data[0];
    for (int i = 1; i < data.length; i++) {
        double[] doubles = data[i];
        double distance = MetricsUtils.distL1(doubles, queryData);
        result.add(new SimpleResult((float) distance, results.documentID(i - 1)));
        maxDistance = Math.max(maxDistance, distance);
    }
    ImageSearchHits hits;
    hits = new SimpleImageSearchHits(result, (float) maxDistance);
    return hits;
}