List of usage examples for java.lang Double isNaN
public static boolean isNaN(double v)
From source file:gedi.util.datastructure.collections.doublecollections.DoubleArrayList.java
public boolean addIfNotNaN(double i) { if (Double.isNaN(i)) return false; return add(i); }
From source file:gedi.util.math.stat.testing.DirichletLikelihoodRatioTest.java
public static int getNumAffected(double[]... samples) { assert samples.length > 1; BitVector nans = new BitVector(samples[0].length); for (int i = 0; i < samples.length; i++) { assert samples[i].length == samples[0].length; for (int j = 0; j < samples[i].length; j++) if (Double.isNaN(samples[i][j])) nans.putQuick(j, true);/*from w w w . j a v a 2 s. c om*/ } nans.not(); return nans.cardinality(); }
From source file:it.unibo.alchemist.model.implementations.timedistributions.ExponentialTime.java
@SuppressFBWarnings("FE_FLOATING_POINT_EQUALITY") private void update(final double newpropensity, final boolean isMu, final Time curTime) { assert !Double.isNaN(newpropensity); assert !Double.isNaN(oldPropensity); if (isMu) {//from w w w. jav a2 s.c o m final Time dt = genTime(newpropensity); setTau(curTime.sum(dt)); } else { if (oldPropensity != newpropensity) { final Time sub = getNextOccurence().subtract(curTime); final Time mul = sub.multiply(oldPropensity / newpropensity); setTau(mul.sum(curTime)); } } }
From source file:beast.math.distribution.GammaDistributionTest.java
@Test public void testPdf() { final int numberOfTests = 100; double totErr = 0; double ptotErr = 0; int np = 0;// ww w . j av a 2s .c om double qtotErr = 0; Random random = new Random(37); for (int i = 0; i < numberOfTests; i++) { final double mean = .01 + (3 - 0.01) * random.nextDouble(); final double var = .01 + (3 - 0.01) * random.nextDouble(); final double scale = var / mean; final double shape = mean / scale; final GammaDistribution gamma = new GammaDistribution(shape, scale); final double value = gamma.nextGamma(); final double mypdf = mypdf(value, shape, scale); final double pdf = gamma.pdf(value); if (Double.isInfinite(mypdf) && Double.isInfinite(pdf)) { continue; } assertFalse(Double.isNaN(mypdf)); assertFalse(Double.isNaN(pdf)); totErr += mypdf != 0 ? Math.abs((pdf - mypdf) / mypdf) : pdf; assertFalse("nan", Double.isNaN(totErr)); //assertEquals("" + shape + "," + scale + "," + value, mypdf,gamma.pdf(value),1e-10); final double cdf = gamma.cdf(value); UnivariateFunction f = new UnivariateFunction() { public double value(double v) { return mypdf(v, shape, scale); } }; final UnivariateIntegrator integrator = new RombergIntegrator(MachineAccuracy.SQRT_EPSILON, 1e-14, 1, 16); double x; try { x = integrator.integrate(16, f, 0.0, value); ptotErr += cdf != 0.0 ? Math.abs(x - cdf) / cdf : x; np += 1; //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6); //System.out.println(shape + "," + scale + " " + value); } catch (MaxCountExceededException e) { // can't integrate , skip test // System.out.println(shape + "," + scale + " skipped"); } final double q = gamma.quantile(cdf); qtotErr += q != 0 ? Math.abs(q - value) / q : value; // assertEquals("" + shape + "," + scale + "," + value + " " + Math.abs(q-value)/value, q, value, 1e-6); } //System.out.println( !Double.isNaN(totErr) ); // System.out.println(totErr); // bad test, but I can't find a good threshold that works for all individual cases assertTrue("failed " + totErr / numberOfTests, totErr / numberOfTests < 1e-7); assertTrue("failed " + ptotErr / np, np > 0 ? (ptotErr / np < 1e-5) : true); assertTrue("failed " + qtotErr / numberOfTests, qtotErr / numberOfTests < 1e-7); }
From source file:juicebox.track.EigenvectorTrack.java
private void setData(int chrIdx, double[] data) { if (data != null && data.length > 0) { DoubleArrayList tmp = new DoubleArrayList(data.length); for (double datum : data) { if (!Double.isNaN(datum)) { tmp.add(datum);//w w w . j a va 2 s.c o m } } /* for (int i = 0; i < data.length; i++) { if (!Double.isNaN(data[i])) { tmp.add(data[i]); } } */ double[] tmpArray = tmp.toArray(); medianCache.put(chrIdx, StatUtils.percentile(tmpArray, 50)); double max = 0; for (double aData : tmpArray) { if (Math.abs(aData) > max) max = Math.abs(aData); } dataMaxCache.put(chrIdx, max); } }
From source file:com.opengamma.analytics.math.interpolation.data.RadialBasisFunctionInterpolatorDataBundle.java
private double[] calculateWeights() { final List<Pair<double[], Double>> data = getData(); final int n = data.size(); double sum;/*from w w w . ja va 2 s. c o m*/ final double[][] radii = new double[n][n]; final double[] y = new double[n]; double phi; double[] x1, x2; final double zeroValue = _basisFunction.evaluate(0.0); for (int i = 0; i < n; i++) { x1 = data.get(i).getFirst(); radii[i][i] = zeroValue; for (int j = i + 1; j < n; j++) { x2 = data.get(j).getFirst(); phi = _basisFunction.evaluate(DistanceCalculator.getDistance(x1, x2)); Validate.isTrue(!Double.isNaN(phi) || !Double.isInfinite(phi), "basis function return invalide number"); radii[i][j] = phi; radii[j][i] = phi; // matrix symmetric since basis function depends on distance only } if (_useNormalized) { sum = 0.0; for (int j = 0; j < n; j++) { sum += radii[i][j]; } y[i] = sum * data.get(i).getSecond(); } else { y[i] = data.get(i).getSecond(); } } _decompRes = _decomp.evaluate(new com.opengamma.analytics.math.matrix.DoubleMatrix2D(radii)); final DoubleMatrix1D res = _decompRes.solve(new DoubleMatrix1D(y)); return res.toArray(); }
From source file:cpcc.core.utils.GeoJsonUtils.java
/** * Extends an existing bounding box by checking if or if not another bounding box is within the existing bounding * box. If the other bounding box is not entirely within the existing bounding box, the existing box will be * enlarged accordingly./*from w w w. j a v a 2 s.co m*/ * * @param boundingBox an existing bounding box. * @param other another bounding box. */ public static void mergeBoundingBoxes(double[] boundingBox, double[] other) { if (Double.isNaN(boundingBox[0]) || other[0] < boundingBox[0]) { boundingBox[0] = other[0]; } if (Double.isNaN(boundingBox[1]) || other[1] < boundingBox[1]) { boundingBox[1] = other[1]; } if (Double.isNaN(boundingBox[2]) || other[2] > boundingBox[2]) { boundingBox[2] = other[2]; } if (Double.isNaN(boundingBox[3]) || other[3] > boundingBox[3]) { boundingBox[3] = other[3]; } }
From source file:com.opengamma.analytics.math.interpolation.MonotonicityPreservingQuinticSplineInterpolator.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 + 2 == yValues.length, "(xValues length = yValues length) or (xValues length + 2 = yValues length)"); ArgumentChecker.isTrue(xValues.length > 2, "Data points should be more than 2"); final int nDataPts = xValues.length; final int yValuesLen = yValues.length; for (int i = 0; i < nDataPts; ++i) { ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xValues containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xValues containing Infinity"); }// w w w .j av a 2 s . c o m for (int i = 0; i < yValuesLen; ++i) { ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yValues containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yValues containing Infinity"); } for (int i = 0; i < nDataPts - 1; ++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 = new double[nDataPts]; if (nDataPts == yValuesLen) { yValuesSrt = Arrays.copyOf(yValues, nDataPts); } else { yValuesSrt = Arrays.copyOfRange(yValues, 1, nDataPts + 1); } ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt); final double[] intervals = _solver.intervalsCalculator(xValuesSrt); final double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals); final PiecewisePolynomialResult result = _method.interpolate(xValues, yValues); ArgumentChecker.isTrue(result.getOrder() >= 3, "Primary interpolant should be degree >= 2"); final double[] initialFirst = _function.differentiate(result, xValuesSrt).getData()[0]; final double[] initialSecond = _function.differentiateTwice(result, xValuesSrt).getData()[0]; double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst); boolean modFirst = false; int k; double[] aValues = aValuesCalculator(slopes, first); double[] bValues = bValuesCalculator(slopes, first); double[][] intervalsA = getIntervalsA(intervals, slopes, first, bValues); double[][] intervalsB = getIntervalsB(intervals, slopes, first, aValues); while (modFirst == false) { k = 0; for (int i = 0; i < nDataPts - 2; ++i) { if (first[i + 1] > 0.) { if (intervalsA[i + 1][1] + Math.abs(intervalsA[i + 1][1]) * ERROR < intervalsB[i][0] - Math.abs(intervalsB[i][0]) * ERROR | intervalsA[i + 1][0] - Math.abs(intervalsA[i + 1][0]) * ERROR > intervalsB[i][1] + Math.abs(intervalsB[i][1]) * ERROR) { ++k; first[i + 1] = firstDerivativesRecalculator(intervals, slopes, aValues, bValues, i + 1); } } } if (k == 0) { modFirst = true; } aValues = aValuesCalculator(slopes, first); bValues = bValuesCalculator(slopes, first); intervalsA = getIntervalsA(intervals, slopes, first, bValues); intervalsB = getIntervalsB(intervals, slopes, first, aValues); } final double[] second = secondDerivativeCalculator(initialSecond, intervalsA, intervalsB); final double[][] coefs = _solver.solve(yValuesSrt, intervals, slopes, first, second); for (int i = 0; i < nDataPts - 1; ++i) { for (int j = 0; j < 6; ++j) { ArgumentChecker.isFalse(Double.isNaN(coefs[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefs[i][j]), "Too large input"); } } return new PiecewisePolynomialResult(new DoubleMatrix1D(xValuesSrt), new DoubleMatrix2D(coefs), 6, 1); }
From source file:MathFunc.java
/** * Returns the arc tangent of an angle, in the range of <code>-Math.PI/2</code> * through <code>Math.PI/2</code>. Special cases: * <ul>/*w ww . jav a 2 s .co m*/ * <li>If the argument is <code>NaN</code>, 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> * <p> * A result must be within 1 ulp of the correctly rounded result. Results * must be semi-monotonic. * * @param a - the value whose arc tangent is to be returned. * @return the arc tangent of the argument. */ public static double atan(double a) { // Special cases. if (Double.isNaN(a)) { return Double.NaN; } if (a == 0.0) { return a; } // Compute the arc tangent. boolean negative = false; boolean greaterThanOne = false; int i = 0; if (a < 0.0) { a = -a; negative = true; } if (a > 1.0) { a = 1.0 / a; greaterThanOne = true; } double t; for (; a > PIover12; a *= t) { i++; t = a + ATAN_CONSTANT; t = 1.0 / t; a *= ATAN_CONSTANT; a--; } double aSquared = a * a; double arcTangent = aSquared + 1.4087812; arcTangent = 0.55913709 / arcTangent; arcTangent += 0.60310578999999997; arcTangent -= 0.051604539999999997 * aSquared; arcTangent *= a; for (; i > 0; i--) { arcTangent += PIover6; } if (greaterThanOne) { arcTangent = PIover2 - arcTangent; } if (negative) { arcTangent = -arcTangent; } return arcTangent; }
From source file:org.opennms.ng.dao.support.RrdStatisticAttributeVisitor.java
/** {@inheritDoc} */ @Override/* www . ja v a2s . c om*/ public void visit(OnmsAttribute attribute) { if (!RrdGraphAttribute.class.isAssignableFrom(attribute.getClass())) { // Nothing to do if we can't cast to an RrdGraphAttribute return; } double statistic = m_rrdDao.getPrintValue(attribute, m_consolidationFunction, m_startTime, m_endTime); /* * We don't want to do anything with NaN data, since * it means there is no data. We especially want to * stay away from it, because it will be sorted as * being higher than any numeric value, which will * leave our TopN report with most, if not all NaN * values at the top. */ if (Double.isNaN(statistic)) { return; } m_statisticVisitor.visit(attribute, statistic); }