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.caseystella.analytics.outlier.batch.rpca.RPCAOutlierAlgorithm.java

@Override
public Outlier analyze(Outlier outlierCandidate, List<DataPoint> context, DataPoint dp) {
    double score = outlierScore(context, dp);
    Severity severity = Severity.NOT_ENOUGH_DATA;
    if (!Double.isNaN(score)) {
        severity = Math.abs(score) > threshold ? Severity.SEVERE_OUTLIER : Severity.NORMAL;
    }/*from   w  ww . ja  v a2  s  .c  o  m*/
    outlierCandidate.setSeverity(severity);
    if (severity == Severity.SEVERE_OUTLIER) {
        if (dp.getMetadata() == null) {
            dp.setMetadata(new HashMap<String, String>());
        }
        dp.getMetadata().put(OutlierMetadataConstants.REAL_OUTLIER_SCORE.toString(), Math.abs(score) + "");
    }
    return outlierCandidate;
}

From source file:com.rapidminer.gui.plotter.charts.BubbleChartPlotter.java

private void prepareNumericalData() {
    DataTable dataTable = getDataTable();
    this.nominal = false;
    xyzDataSet = new DefaultXYZDataset();

    if (axis[X_AXIS] >= 0 && axis[Y_AXIS] >= 0 && axis[BUBBLE_SIZE_AXIS] >= 0) {

        this.bubbleSizeMin = Double.POSITIVE_INFINITY;
        this.bubbleSizeMax = Double.NEGATIVE_INFINITY;
        this.xAxisMin = Double.POSITIVE_INFINITY;
        this.xAxisMax = Double.NEGATIVE_INFINITY;
        this.yAxisMin = Double.POSITIVE_INFINITY;
        this.yAxisMax = Double.NEGATIVE_INFINITY;
        this.minColor = Double.POSITIVE_INFINITY;
        this.maxColor = Double.NEGATIVE_INFINITY;

        List<double[]> dataList = new LinkedList<>();
        synchronized (dataTable) {
            Iterator<DataTableRow> i = dataTable.iterator();
            while (i.hasNext()) {
                DataTableRow row = i.next();

                double xValue = row.getValue(axis[X_AXIS]);
                double yValue = row.getValue(axis[Y_AXIS]);
                double bubbleSizeValue = row.getValue(axis[BUBBLE_SIZE_AXIS]);

                double colorValue = Double.NaN;
                if (colorColumn >= 0) {
                    colorValue = row.getValue(colorColumn);
                }/*  w ww . jav a2 s . c om*/

                if (!Double.isNaN(xValue) && !Double.isNaN(yValue) && !Double.isNaN(bubbleSizeValue)) {
                    double[] data = new double[4];
                    data[X_AXIS] = xValue;
                    data[Y_AXIS] = yValue;
                    data[BUBBLE_SIZE_AXIS] = bubbleSizeValue;
                    data[3] = colorValue;

                    this.bubbleSizeMin = MathFunctions.robustMin(this.bubbleSizeMin, bubbleSizeValue);
                    this.bubbleSizeMax = MathFunctions.robustMax(this.bubbleSizeMax, bubbleSizeValue);
                    this.xAxisMin = MathFunctions.robustMin(this.xAxisMin, xValue);
                    this.yAxisMin = MathFunctions.robustMin(this.yAxisMin, yValue);
                    this.xAxisMax = MathFunctions.robustMax(this.xAxisMax, xValue);
                    this.yAxisMax = MathFunctions.robustMax(this.yAxisMax, yValue);
                    this.minColor = MathFunctions.robustMin(this.minColor, colorValue);
                    this.maxColor = MathFunctions.robustMax(this.maxColor, colorValue);

                    dataList.add(data);
                }
            }
        }

        double[][] data = new double[3][dataList.size()];
        this.colors = new double[dataList.size()];

        int index = 0;
        double scaleFactor = Math.min(this.xAxisMax - this.xAxisMin, this.yAxisMax - this.yAxisMin) / 4.0d;
        for (double[] d : dataList) {
            data[X_AXIS][index] = d[X_AXIS];
            data[Y_AXIS][index] = d[Y_AXIS];
            data[BUBBLE_SIZE_AXIS][index] = ((d[BUBBLE_SIZE_AXIS] - bubbleSizeMin)
                    / (bubbleSizeMax - bubbleSizeMin) + 0.1) * scaleFactor;
            this.colors[index] = d[3];
            index++;
        }

        xyzDataSet.addSeries("All", data);
    }
}

From source file:com.github.tteofili.calabrize.impl.RNN.java

public void learn() {

    int currentEpoch = 0;

    int n = 0;//from w  ww  .  j  a  v  a2s  .c  o m
    int p = 0;

    // memory variables for Adagrad
    INDArray mWxh = Nd4j.zerosLike(wxh);
    INDArray mWhh = Nd4j.zerosLike(whh);
    INDArray mWhy = Nd4j.zerosLike(why);

    INDArray mbh = Nd4j.zerosLike(bh);
    INDArray mby = Nd4j.zerosLike(by);

    // loss at iteration 0
    double smoothLoss = -Math.log(1.0 / vocabSize) * seqLength;

    while (true) {
        // prepare inputs (we're sweeping from left to right in steps seqLength long)
        if (p + seqLength + 1 >= data.size() || n == 0) {
            hPrev = Nd4j.zeros(hiddenLayerSize, 1); // reset RNN memory
            p = 0; // go from start of data
            currentEpoch++;
            if (currentEpoch == epochs) {
                System.out.println("training finished: e:" + epochs + ", l: " + smoothLoss + ", h:("
                        + learningRate + ", " + seqLength + ", " + hiddenLayerSize + ")");
                break;
            }
        }

        INDArray inputs = getSequence(p, true);
        INDArray targets = getSequence(p + 1, false);

        // sample from the model every now and then
        if (n % 1000 == 0 && n > 0) {
            String txt = sample(inputs.getInt(0));
            System.out.printf("\n---\n %s \n----\n", txt);
        }

        INDArray dWxh = Nd4j.zerosLike(wxh);
        INDArray dWhh = Nd4j.zerosLike(whh);
        INDArray dWhy = Nd4j.zerosLike(why);

        INDArray dbh = Nd4j.zerosLike(bh);
        INDArray dby = Nd4j.zerosLike(by);

        // forward seqLength characters through the net and fetch gradient
        double loss = lossFun(inputs, targets, dWxh, dWhh, dWhy, dbh, dby);
        smoothLoss = smoothLoss * 0.999 + loss * 0.001;
        if (Double.isNaN(smoothLoss)) {
            System.out.println("loss is NaN (over/underflow occured, try adjusting hyperparameters)");
            break;
        }
        if (n % 100 == 0) {
            System.out.printf("iter %d, loss: %f\n", n, smoothLoss); // print progress
        }

        if (n % batch == 0) {

            // perform parameter update with RMSprop
            mWxh = mWxh.mul(decay).add(1 - decay).mul((dWxh).mul(dWxh));
            wxh.subi(dWxh.mul(learningRate).div(Transforms.sqrt(mWxh).add(eps)));

            mWhh = mWhh.mul(decay).add(1 - decay).mul((dWhh).mul(dWhh));
            whh.subi(dWhh.mul(learningRate).div(Transforms.sqrt(mWhh).add(eps)));

            mWhy = mWhy.mul(decay).add(1 - decay).mul((dWhy).mul(dWhy));
            why.subi(dWhy.mul(learningRate).div(Transforms.sqrt(mWhy).add(eps)));

            mbh = mbh.mul(decay).add(1 - decay).mul((dbh).mul(dbh));
            bh.subi(dbh.mul(learningRate).div(Transforms.sqrt(mbh).add(eps)));

            mby = mby.mul(decay).add(1 - decay).mul((dby).mul(dby));
            by.subi(dby.mul(learningRate).div(Transforms.sqrt(mby).add(eps)));
        }

        p += seqLength; // move data pointer
        n++; // iteration counter
    }
}

From source file:com.rapidminer.operator.preprocessing.discretization.FrequencyDiscretization.java

@Override
public PreprocessingModel createPreprocessingModel(ExampleSet exampleSet) throws OperatorException {
    HashMap<Attribute, double[]> ranges = new HashMap<Attribute, double[]>();
    // Get and check parametervalues
    boolean useSqrt = getParameterAsBoolean(PARAMETER_USE_SQRT_OF_EXAMPLES);
    int numberOfBins = 0;
    if (!useSqrt) {
        // if not automatic sizing of bins, use parametervalue
        numberOfBins = getParameterAsInt(PARAMETER_NUMBER_OF_BINS);
        if (numberOfBins >= (exampleSet.size() - 1)) {
            throw new UserError(this, 116, PARAMETER_NUMBER_OF_BINS,
                    "number of bins must be smaller than number of examples (here: " + exampleSet.size() + ")");
        }//  ww  w.j  a  v  a  2  s  . c  om
    } else {
        exampleSet.recalculateAllAttributeStatistics();
    }

    for (Attribute currentAttribute : exampleSet.getAttributes()) {
        if (useSqrt) {
            numberOfBins = (int) Math.round(Math.sqrt(
                    exampleSet.size() - (int) exampleSet.getStatistics(currentAttribute, Statistics.UNKNOWN)));
        }
        double[] attributeRanges = new double[numberOfBins];
        ExampleSet sortedSet = new SortedExampleSet(exampleSet, currentAttribute, SortedExampleSet.INCREASING);

        // finding ranges
        double examplesPerBin = exampleSet.size() / (double) numberOfBins;
        double currentBinSpace = examplesPerBin;
        double lastValue = Double.NaN;
        int currentBin = 0;

        for (Example example : sortedSet) {
            double value = example.getValue(currentAttribute);
            if (!Double.isNaN(value)) {
                // change bin if full and not last
                if (currentBinSpace < 1 && currentBin < numberOfBins && value != lastValue) {
                    if (!Double.isNaN(lastValue)) {
                        attributeRanges[currentBin] = (lastValue + value) / 2;
                        currentBin++;
                        currentBinSpace += examplesPerBin; // adding because same values might
                        // cause binspace to be negative
                        if (currentBinSpace < 1) {
                            throw new UserError(this, 944, currentAttribute.getName());
                        }
                    }
                }
                currentBinSpace--;
                lastValue = value;
            }
        }
        attributeRanges[numberOfBins - 1] = Double.POSITIVE_INFINITY;
        ranges.put(currentAttribute, attributeRanges);
    }
    DiscretizationModel model = new DiscretizationModel(exampleSet);

    // determine number of digits
    int numberOfDigits = -1;
    if (getParameterAsBoolean(PARAMETER_AUTOMATIC_NUMBER_OF_DIGITS) == false) {
        numberOfDigits = getParameterAsInt(PARAMETER_NUMBER_OF_DIGITS);
    }

    model.setRanges(ranges, "range", getParameterAsInt(PARAMETER_RANGE_NAME_TYPE), numberOfDigits);
    return model;
}

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

/**
 * 99.9th percentile//w ww  .  j av  a2 s.com
 *
 * @return 99.9th percentile
 */
@Managed
@SuppressWarnings("unused")
public double getSizeTP999() {
    double percentile = sizeStats.getPercentile(99.9);
    return Double.isNaN(percentile) ? 0.0 : percentile;
}

From source file:MathFunc.java

/**
 * Converts rectangular coordinates (x, y) to polar (r, <i>theta</i>).  This method
 * computes the phase <i>theta</i> by computing an arc tangent of y/x in the range
 * of <i>-pi</i> to <i>pi</i>.  Special cases:
 * <ul>/*  ww  w .ja  v a 2 s. c  o  m*/
 *  <li>If either argument is <code>NaN</code>, then the result is <code>NaN</code>.
 *  <li>If the first argument is positive zero and the second argument is
 *      positive, or the first argument is positive and finite and the second
 *      argument is positive infinity, then the result is positive zero.
 *  <li>If the first argument is negative zero and the second argument is
 *      positive, or the first argument is negative and finite and the second
 *      argument is positive infinity, then the result is negative zero.
 *  <li>If the first argument is positive zero and the second argument is 
 *      negative, or the first argument is positive and finite and the second
 *      argument is negative infinity, then the result is the <code>double</code> value 
 *      closest to <i>pi</i>.
 *  <li>If the first argument is negative zero and the second argument is 
 *      negative, or the first argument is negative and finite and the second
 *      argument is negative infinity, then the result is the <code>double</code> value
 *      closest to <i>-pi</i>.
 *  <li>If the first argument is positive and the second argument is positive
 *      zero or negative zero, or the first argument is positive infinity and
 *      the second argument is finite, then the result is the <code>double</code> value 
 *      closest to <i>pi</i>/2.
 *  <li>If the first argument is negative and the second argument is positive
 *      zero or negative zero, or the first argument is negative infinity and
 *      the second argument is finite, then the result is the <code>double</code> value
 *      closest to <i>-pi</i>/2.
 *  <li>If both arguments are positive infinity, then the result is the double
 *      value closest to <i>pi</i>/4.
 *  <li>If the first argument is positive infinity and the second argument is
 *      negative infinity, then the result is the double value closest to 3*<i>pi</i>/4.
 *  <li>If the first argument is negative infinity and the second argument is
 *      positive infinity, then the result is the double value closest to -<i>pi</i>/4.
 *  <li>If both arguments are negative infinity, then the result is the double
 *      value closest to -3*<i>pi</i>/4.
 * </ul>
 * <p>
 * A result must be within 2 ulps of the correctly rounded result.  Results
 * must be semi-monotonic.
 * 
 * @param y - the ordinate coordinate
 * @param x - the abscissa coordinate 
 * @return the <i>theta</i> component of the point (r, <i>theta</i>) in polar
 *   coordinates that corresponds to the point (x, y) in Cartesian coordinates.
 */
public static double atan2(double y, double x) {
    // Special cases.
    if (Double.isNaN(y) || Double.isNaN(x)) {
        return Double.NaN;
    } else if (Double.isInfinite(y)) {
        if (y > 0.0) // Positive infinity
        {
            if (Double.isInfinite(x)) {
                if (x > 0.0) {
                    return PIover4;
                } else {
                    return 3.0 * PIover4;
                }
            } else if (x != 0.0) {
                return PIover2;
            }
        } else // Negative infinity
        {
            if (Double.isInfinite(x)) {
                if (x > 0.0) {
                    return -PIover4;
                } else {
                    return -3.0 * PIover4;
                }
            } else if (x != 0.0) {
                return -PIover2;
            }
        }
    } else if (y == 0.0) {
        if (x > 0.0) {
            return y;
        } else if (x < 0.0) {
            return Math.PI;
        }
    } else if (Double.isInfinite(x)) {
        if (x > 0.0) // Positive infinity
        {
            if (y > 0.0) {
                return 0.0;
            } else if (y < 0.0) {
                return -0.0;
            }
        } else // Negative infinity
        {
            if (y > 0.0) {
                return Math.PI;
            } else if (y < 0.0) {
                return -Math.PI;
            }
        }
    } else if (x == 0.0) {
        if (y > 0.0) {
            return PIover2;
        } else if (y < 0.0) {
            return -PIover2;
        }
    }

    // Implementation a simple version ported from a PASCAL implementation:
    //   http://everything2.com/index.pl?node_id=1008481

    double arcTangent;

    // Use arctan() avoiding division by zero.
    if (Math.abs(x) > Math.abs(y)) {
        arcTangent = atan(y / x);
    } else {
        arcTangent = atan(x / y); // -PI/4 <= a <= PI/4

        if (arcTangent < 0) {
            arcTangent = -PIover2 - arcTangent; // a is negative, so we're adding
        } else {
            arcTangent = PIover2 - arcTangent;
        }
    }

    // Adjust result to be from [-PI, PI]
    if (x < 0) {
        if (y < 0) {
            arcTangent = arcTangent - Math.PI;
        } else {
            arcTangent = arcTangent + Math.PI;
        }
    }

    return arcTangent;
}

From source file:fastcall.FArrayUtils.java

/**
 * Remove NaN from a two dimension array
 * @param value/*w  ww  . jav a 2  s .  c  o m*/
 * @return 
 */
public static double[][] removeNaN(double[][] value) {
    TDoubleArrayList[] vList = new TDoubleArrayList[value.length];
    for (int i = 0; i < vList.length; i++)
        vList[i] = new TDoubleArrayList();
    for (int i = 0; i < value[0].length; i++) {
        boolean flag = false;
        for (int j = 0; j < value.length; j++) {
            if (Double.isNaN(value[j][i])) {
                flag = true;
                break;
            }
        }
        if (flag)
            continue;
        for (int j = 0; j < vList.length; j++) {
            vList[j].add(value[j][i]);
        }
    }
    double[][] v = new double[vList.length][];
    for (int i = 0; i < v.length; i++)
        v[i] = vList[i].toArray();
    return v;
}

From source file:es.uvigo.ei.sing.laimages.core.operations.Interpolator.java

private final static int lastValueIndex(double[] values) {
    for (int i = values.length - 1; i >= 0; i--) {
        if (!Double.isNaN(values[i]))
            return i;
    }/*  w w  w  . j  ava  2  s  . co  m*/

    throw new IllegalArgumentException("No value found");
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookLoader.java

/**
 * Parse value.//from w  w w.  j a v a  2  s .c  o m
 * 
 * @param jsonFolder
 * @return
 */
protected static Value parseValue(JSONObject jsonValue) {
    Value res = new Value();
    if (jsonValue != null) {
        double val = jsonValue.optDouble("startOfPeriod");
        if (Double.isNaN(val)) {
            val = 0.0;
        }
        res.setStartOfPeriod(val);
        val = jsonValue.optDouble("credit");
        if (Double.isNaN(val)) {
            val = 0.0;
        }
        res.setCredit(val);
        val = jsonValue.optDouble("debit");
        if (Double.isNaN(val)) {
            val = 0.0;
        }
        res.setDebit(val);
        val = jsonValue.optDouble("endOfPeriod");
        if (Double.isNaN(val)) {
            val = 0.0;
        }
        res.setEndOfPeriod(val);
        val = jsonValue.optDouble("control");
        if (Double.isNaN(val)) {
            val = 0.0;
        }
        res.setControl(val);
        if (jsonValue.optString("manual").equals("y")) {
            res.setManual(true);
        }
    }
    return res;
}

From source file:ste.travian.world.TileRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device./*from  ww w .  j a va  2s  . c  o m*/
 * @param state  the renderer state.
 * @param dataArea  the area within which the data is being drawn.
 * @param info  collects information about the drawing.
 * @param plot  the plot (can be used to obtain standard color 
 *              information etc).
 * @param domainAxis  the domain (horizontal) axis.
 * @param rangeAxis  the range (vertical) axis.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param crosshairState  crosshair information for the plot 
 *                        (<code>null</code> permitted).
 * @param pass  the pass index.
 */
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    // get the data point...
    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double adjx = (this.dotWidth - 1) / 2.0;
    double adjy = (this.dotHeight - 1) / 2.0;
    if (Double.isNaN(y)) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX = domainAxis.valueToJava2D(x, dataArea, xAxisLocation) - adjx;
    double transY = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation) - adjy;

    g2.setPaint(getItemPaint(series, item));
    if (orientation == PlotOrientation.HORIZONTAL) {
        g2.fillRect((int) transY, (int) transX, this.dotHeight, this.dotWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        g2.fillRect((int) transX, (int) transY, this.dotWidth, this.dotHeight);
    }

    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x, y, domainAxisIndex, rangeAxisIndex, transX, transY, orientation);

    // add an entity for the item, but only if it falls within the data
    // area...
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }
    int xx = (int) transX;
    int yy = (int) transY;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = (int) transY;
        yy = (int) transX;
    }
    if (entities != null && dataArea.contains(xx, yy)) {
        addEntity(entities, null, dataset, series, item, (int) xx, (int) yy);
    }

}