List of usage examples for java.lang Double isInfinite
public static boolean isInfinite(double v)
From source file:org.eclipse.january.dataset.Stats.java
/** * @param a dataset// w w w . j a v a 2 s . c om * @param dtype * @param axis * @param ignoreInvalids see {@link IDataset#max(boolean...)} * @return typed product of items along axis in dataset * @since 2.0 */ public static Dataset typedProduct(final Dataset a, final int dtype, int axis, final boolean... ignoreInvalids) { axis = a.checkAxis(axis); final int[] oshape = a.getShape(); final int is = a.getElementsPerItem(); final int alen = oshape[axis]; oshape[axis] = 1; final boolean ignoreNaNs; final boolean ignoreInfs; if (a.hasFloatingPointElements()) { ignoreNaNs = ignoreInvalids != null && ignoreInvalids.length > 0 ? ignoreInvalids[0] : false; ignoreInfs = ignoreInvalids != null && ignoreInvalids.length > 1 ? ignoreInvalids[1] : ignoreNaNs; } else { ignoreNaNs = false; ignoreInfs = false; } @SuppressWarnings("deprecation") Dataset result = DatasetFactory.zeros(is, oshape, dtype); IndexIterator qiter = result.getIterator(true); int[] qpos = qiter.getPos(); int[] spos; // TODO add getLongArray(long[], int...) to CompoundDataset while (qiter.hasNext()) { spos = qpos.clone(); if (a.isComplex()) { double rv = 1, iv = 0; switch (dtype) { case Dataset.COMPLEX64: ComplexFloatDataset af = (ComplexFloatDataset) a; for (int j = 0; j < alen; j++) { spos[axis] = j; final float r1 = af.getReal(spos); final float i1 = af.getImag(spos); if (ignoreNaNs && (Float.isNaN(r1) || Float.isNaN(i1))) { continue; } if (ignoreInfs && (Float.isInfinite(r1) || Float.isInfinite(i1))) { continue; } final double tv = r1 * rv - i1 * iv; iv = r1 * iv + i1 * rv; rv = tv; if (Double.isNaN(rv) && Double.isNaN(iv)) { break; } } break; case Dataset.COMPLEX128: ComplexDoubleDataset ad = (ComplexDoubleDataset) a; for (int j = 0; j < alen; j++) { spos[axis] = j; final double r1 = ad.getReal(spos); final double i1 = ad.getImag(spos); if (ignoreNaNs && (Double.isNaN(r1) || Double.isNaN(i1))) { continue; } if (ignoreInfs && (Double.isInfinite(r1) || Double.isInfinite(i1))) { continue; } final double tv = r1 * rv - i1 * iv; iv = r1 * iv + i1 * rv; rv = tv; if (Double.isNaN(rv) && Double.isNaN(iv)) { break; } } break; } result.set(new Complex(rv, iv), qpos); } else { final long[] lresults; final double[] dresults; switch (dtype) { case Dataset.BOOL: case Dataset.INT8: case Dataset.INT16: case Dataset.INT32: case Dataset.INT64: long lresult = 1; for (int j = 0; j < alen; j++) { spos[axis] = j; lresult *= a.getInt(spos); } result.set(lresult, qpos); break; case Dataset.ARRAYINT8: lresults = new long[is]; for (int k = 0; k < is; k++) { lresults[k] = 1; } for (int j = 0; j < alen; j++) { spos[axis] = j; final byte[] va = (byte[]) a.getObject(spos); for (int k = 0; k < is; k++) { lresults[k] *= va[k]; } } result.set(lresults, qpos); break; case Dataset.ARRAYINT16: lresults = new long[is]; for (int k = 0; k < is; k++) { lresults[k] = 1; } for (int j = 0; j < alen; j++) { spos[axis] = j; final short[] va = (short[]) a.getObject(spos); for (int k = 0; k < is; k++) { lresults[k] *= va[k]; } } result.set(lresults, qpos); break; case Dataset.ARRAYINT32: lresults = new long[is]; for (int k = 0; k < is; k++) { lresults[k] = 1; } for (int j = 0; j < alen; j++) { spos[axis] = j; final int[] va = (int[]) a.getObject(spos); for (int k = 0; k < is; k++) { lresults[k] *= va[k]; } } result.set(lresults, qpos); break; case Dataset.ARRAYINT64: lresults = new long[is]; for (int k = 0; k < is; k++) { lresults[k] = 1; } for (int j = 0; j < alen; j++) { spos[axis] = j; final long[] va = (long[]) a.getObject(spos); for (int k = 0; k < is; k++) { lresults[k] *= va[k]; } } result.set(lresults, qpos); break; case Dataset.FLOAT32: case Dataset.FLOAT64: double dresult = 1.; for (int j = 0; j < alen; j++) { spos[axis] = j; final double x = a.getDouble(spos); if (ignoreNaNs && Double.isNaN(x)) { continue; } if (ignoreInfs && Double.isInfinite(x)) { continue; } dresult *= x; if (Double.isNaN(dresult)) { break; } } result.set(dresult, qpos); break; case Dataset.ARRAYFLOAT32: case Dataset.ARRAYFLOAT64: CompoundDataset da = (CompoundDataset) a; double[] dvalues = new double[is]; dresults = new double[is]; for (int k = 0; k < is; k++) { dresults[k] = 1.; } for (int j = 0; j < alen; j++) { spos[axis] = j; da.getDoubleArray(dvalues, spos); boolean okay = true; for (int k = 0; k < is; k++) { final double val = dvalues[k]; if (ignoreNaNs && Double.isNaN(val)) { okay = false; break; } if (ignoreInfs && Double.isInfinite(val)) { okay = false; break; } } if (okay) { for (int k = 0; k < is; k++) { dresults[k] *= dvalues[k]; } } } result.set(dresults, qpos); break; } } } result.setShape(ShapeUtils.squeezeShape(oshape, axis)); return result; }
From source file:com.google.android.gms.internal.zzbti.java
public double nextDouble() throws IOException { int i = this.zzcpK; if (i == 0) { i = zzacd();/* w w w. java 2 s.co m*/ } if (i == 15) { this.zzcpK = 0; int[] iArr = this.zzcpR; int i2 = this.zzcpP - 1; iArr[i2] = iArr[i2] + 1; return (double) this.zzcpL; } if (i == 16) { this.zzcpN = new String(this.zzcpH, this.pos, this.zzcpM); this.pos += this.zzcpM; } else if (i == 8 || i == 9) { this.zzcpN = zzd(i == 8 ? '\'' : '\"'); } else if (i == 10) { this.zzcpN = zzacg(); } else if (i != 11) { String valueOf = String.valueOf(zzabQ()); int lineNumber = getLineNumber(); int columnNumber = getColumnNumber(); String path = getPath(); throw new IllegalStateException( new StringBuilder((String.valueOf(valueOf).length() + 71) + String.valueOf(path).length()) .append("Expected a double but was ").append(valueOf).append(" at line ") .append(lineNumber).append(" column ").append(columnNumber).append(" path ") .append(path).toString()); } this.zzcpK = 11; double parseDouble = Double.parseDouble(this.zzcpN); if (this.zzcpG || !(Double.isNaN(parseDouble) || Double.isInfinite(parseDouble))) { this.zzcpN = null; this.zzcpK = 0; int[] iArr2 = this.zzcpR; columnNumber = this.zzcpP - 1; iArr2[columnNumber] = iArr2[columnNumber] + 1; return parseDouble; } columnNumber = getLineNumber(); int columnNumber2 = getColumnNumber(); String path2 = getPath(); throw new zzbtl(new StringBuilder(String.valueOf(path2).length() + Quests.SELECT_ENDING_SOON) .append("JSON forbids NaN and infinities: ").append(parseDouble).append(" at line ") .append(columnNumber).append(" column ").append(columnNumber2).append(" path ").append(path2) .toString()); }
From source file:eu.tango.energymodeller.datasourceclient.SlurmDataSourceAdaptor.java
/** * This ensures that metric values are not added in cases where NaN etc is * given as an output from the data source. * * @param measurement The measurement to add the value to * @param value The value to add./* w w w . jav a2 s.c o m*/ * @return The measurement with the added metric only in cases where the * values correct. */ private boolean validatedAddMetric(Measurement measurement, MetricValue value) { if (Double.isNaN(value.getValue()) || Double.isInfinite(value.getValue())) { return false; } measurement.addMetric(value); return true; }
From source file:org.broadinstitute.gatk.tools.walkers.genotyper.afcalc.AFCalcUnitTest.java
@Test(enabled = true && !DEBUG_ONLY, dataProvider = "Models") public void testBiallelicPriors(final AFCalc model) { for (int REF_PL = 10; REF_PL <= 20; REF_PL += 10) { final Genotype AB = makePL(Arrays.asList(A, C), REF_PL, 0, 10000); for (int log10NonRefPrior = 1; log10NonRefPrior < 10 * REF_PL; log10NonRefPrior += 1) { final double refPrior = 1 - QualityUtils.qualToErrorProb(log10NonRefPrior); final double nonRefPrior = (1 - refPrior) / 2; final double[] priors = MathUtils.normalizeFromLog10( MathUtils.toLog10(new double[] { refPrior, nonRefPrior, nonRefPrior }), true); if (!Double.isInfinite(priors[1])) { GetGLsTest cfg = new GetGLsTest(model, 1, Arrays.asList(AB), priors, "pNonRef" + log10NonRefPrior); final AFCalcResult resultTracker = cfg.execute(); final int actualAC = resultTracker.getAlleleCountsOfMLE()[0]; final double pRefWithPrior = AB.getLikelihoods().getAsVector()[0] + priors[0]; final double pHetWithPrior = AB.getLikelihoods().getAsVector()[1] + priors[1] - Math.log10(0.5); final double nonRefPost = Math.pow(10, pHetWithPrior) / (Math.pow(10, pRefWithPrior) + Math.pow(10, pHetWithPrior)); final double log10NonRefPost = Math.log10(nonRefPost); if (!Double.isInfinite(log10NonRefPost)) Assert.assertEquals(resultTracker.getLog10PosteriorOfAFGT0(), log10NonRefPost, 1e-2); if (nonRefPost >= 0.9) Assert.assertTrue(resultTracker.isPolymorphic(C, -1)); final int expectedMLEAC = 1; // the MLE is independent of the prior Assert.assertEquals(actualAC, expectedMLEAC, "actual AC with priors " + log10NonRefPrior + " not expected " + expectedMLEAC + " priors " + Utils.join(",", priors)); }/*ww w. j a v a 2s.c o m*/ } } }
From source file:org.lockss.test.LockssTestCase.java
public static void assertNotEquals(String message, double expected, double actual, double delta) { // handle infinity specially since subtracting to infinite //values gives NaN and the the following test fails if (Double.isInfinite(expected)) { if (expected == actual) { failEquals(message, new Double(expected), new Double(actual)); }//from www . j a v a2 s. co m } else if ((Math.abs(expected - actual) <= delta)) { // Because comparison with NaN always returns false failEquals(message, new Double(expected), new Double(actual)); } }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("test if value is infinite") @ScalarFunction/*from w ww .ja v a2 s . co m*/ @SqlType(StandardTypes.BOOLEAN) public static boolean isInfinite(@SqlType(StandardTypes.DOUBLE) double num) { return Double.isInfinite(num); }
From source file:com.opengamma.analytics.financial.model.volatility.BlackFormulaRepository.java
/** * Get the log-normal (Black) implied volatility of an European option * @param price The <b>forward</b> price - i.e. the market price divided by the numeraire (i.e. the zero bond p(0,T) for the T-forward measure) * @param forward The forward value of the underlying * @param strike The Strike/*from w w w.j a v a 2 s . co m*/ * @param timeToExpiry The time-to-expiry * @param isCall true for call * @return log-normal (Black) implied volatility */ public static double impliedVolatility(final double price, final double forward, final double strike, final double timeToExpiry, final boolean isCall) { ArgumentChecker.isTrue(price > 0.0, "negative/NaN price; have {}", price); ArgumentChecker.isTrue(forward > 0.0, "negative/NaN forward; have {}", forward); ArgumentChecker.isTrue(strike > 0.0, "negative/NaN strike; have {}", strike); ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isFalse(Double.isInfinite(forward), "forward is Infinity"); ArgumentChecker.isFalse(Double.isInfinite(strike), "strike is Infinity"); ArgumentChecker.isFalse(Double.isInfinite(timeToExpiry), "timeToExpiry is Infinity"); final double intrinsicPrice = Math.max(0., (isCall ? 1 : -1) * (forward - strike)); final double targetPrice = price - intrinsicPrice; //Math.max(0., price - intrinsicPrice) should not used for least chi square final double sigmaGuess = 0.3; return impliedVolatility(targetPrice, forward, strike, timeToExpiry, sigmaGuess); }
From source file:org.lockss.test.LockssTestCase.java
public static void assertNotEquals(String message, float expected, float actual, float delta) { // handle infinity specially since subtracting to infinite //values gives NaN and the the following test fails if (Double.isInfinite(expected)) { if (expected == actual) { failEquals(message, new Float(expected), new Float(actual)); }// w w w. j a va2 s. com } else if ((Math.abs(expected - actual) <= delta)) { // Because comparison with NaN always returns false failEquals(message, new Float(expected), new Float(actual)); } }
From source file:org.broadinstitute.gatk.tools.walkers.genotyper.afcalc.AFCalculationUnitTest.java
@Test(enabled = true, dataProvider = "Models") public void testNoPrior(final AFCalculator model) { for (int REF_PL = 10; REF_PL <= 20; REF_PL += 10) { final Genotype AB = makePL(Arrays.asList(A, C), REF_PL, 0, 10000); final double[] flatPriors = new double[] { 0.0, 0.0, 0.0 }; // test that function computeAlleleFrequency correctly operates when the flat prior option is set // computeAlleleFrequencyPriors takes linear priors final ArrayList<Double> inputPrior = new ArrayList<Double>(); inputPrior.add(1.0 / 3);//from ww w .j a va2 s . c om inputPrior.add(1.0 / 3); final AFPriorProvider log10priorProvider = UnifiedGenotypingEngine .composeAlleleFrequencyPriorProvider(2, 0.0, inputPrior); final double[] noPriors = log10priorProvider.forTotalPloidy(2); GetGLsTest cfgFlatPrior = new GetGLsTest(model, 1, Arrays.asList(AB), flatPriors, "flatPrior"); GetGLsTest cfgNoPrior = new GetGLsTest(model, 1, Arrays.asList(AB), noPriors, "noPrior"); final AFCalculationResult resultTrackerFlat = cfgFlatPrior.execute(); final AFCalculationResult resultTrackerNoPrior = cfgNoPrior.execute(); final double pRefWithNoPrior = AB.getLikelihoods().getAsVector()[0]; final double pHetWithNoPrior = AB.getLikelihoods().getAsVector()[1] - Math.log10(0.5); final double nonRefPost = Math.pow(10, pHetWithNoPrior) / (Math.pow(10, pRefWithNoPrior) + Math.pow(10, pHetWithNoPrior)); final double log10NonRefPost = Math.log10(nonRefPost); if (!Double.isInfinite(log10NonRefPost)) { // check that the no-prior and flat-prior constructions yield same result Assert.assertEquals(resultTrackerFlat.getLog10PosteriorOfAFGT0(), resultTrackerNoPrior.getLog10PosteriorOfAFGT0()); } } }
From source file:org.eclipse.january.dataset.AbstractDatasetTest.java
/** * Test contents functions//from ww w . j a v a 2s. com */ @Test public void testContents() { double[] x = { 0, 2., -12.3 }; double[] y = { 2.3, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY }; double[] z = { 1e14, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY }; Dataset ta = DatasetFactory.createFromObject(x); assertEquals(false, ta.containsNans()); assertEquals(false, ta.containsInfs()); Dataset tb = DatasetFactory.createFromObject(y); assertEquals(true, tb.containsNans()); assertEquals(true, tb.containsInfs()); assertEquals(true, Double.isNaN(tb.min().doubleValue())); assertEquals(false, Double.isInfinite(tb.min().doubleValue())); assertEquals(true, Double.isNaN(tb.max().doubleValue())); assertEquals(false, Double.isInfinite(tb.max().doubleValue())); Dataset f = tb.cast(Dataset.FLOAT32); assertEquals(true, f.containsNans()); assertEquals(true, f.containsInfs()); assertEquals(true, Double.isNaN(f.min().doubleValue())); assertEquals(false, Double.isInfinite(f.min().doubleValue())); assertEquals(true, Double.isNaN(f.max().doubleValue())); assertEquals(false, Double.isInfinite(f.max().doubleValue())); Dataset tc = DatasetFactory.createFromObject(z); assertEquals(true, Double.isInfinite(tc.min().doubleValue())); assertEquals(true, Double.isInfinite(tc.max().doubleValue())); }