Java Code Examples for com.google.common.math.DoubleMath#fuzzyEquals()
The following examples show how to use
com.google.common.math.DoubleMath#fuzzyEquals() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CubicRealRootFinder.java From Strata with Apache License 2.0 | 6 votes |
@Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coefficients = function.getCoefficients(); if (coefficients.length != 4) { throw new IllegalArgumentException("Function is not a cubic"); } ComplexNumber[] result = ROOT_FINDER.getRoots(function); List<Double> reals = new ArrayList<>(); for (ComplexNumber c : result) { if (DoubleMath.fuzzyEquals(c.getImaginary(), 0d, 1e-16)) { reals.add(c.getReal()); } } ArgChecker.isTrue(reals.size() > 0, "Could not find any real roots"); return reals.toArray(EMPTY_ARRAY); }
Example 2
Source File: CommonIndexedPersistenceEncoding.java From geowave with Apache License 2.0 | 6 votes |
/** * Tool can be used custom index strategies to check if the tiles actual intersect with the * provided bounding box. * * @param boxRangeData * @param innerTile * @return */ private boolean checkCoverage( final MultiDimensionalNumericData boxRangeData, final MultiDimensionalNumericData innerTile) { for (int i = 0; i < boxRangeData.getDimensionCount(); i++) { final double i1 = innerTile.getDataPerDimension()[i].getMin(); final double i2 = innerTile.getDataPerDimension()[i].getMax(); final double j1 = boxRangeData.getDataPerDimension()[i].getMin(); final double j2 = boxRangeData.getDataPerDimension()[i].getMax(); final boolean overlaps = ((i1 < j2) || DoubleMath.fuzzyEquals(i1, j2, DOUBLE_TOLERANCE)) && ((i2 > j1) || DoubleMath.fuzzyEquals(i2, j1, DOUBLE_TOLERANCE)); if (!overlaps) { return false; } } return true; }
Example 3
Source File: LaguerrePolynomialRealRootFinder.java From Strata with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * @throws MathException If there are no real roots; if the Commons method could not evaluate the function; if the Commons method could not converge. */ @Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); try { Complex[] roots = ROOT_FINDER.solveAllComplex(function.getCoefficients(), 0); List<Double> realRoots = new ArrayList<>(); for (Complex c : roots) { if (DoubleMath.fuzzyEquals(c.getImaginary(), 0d, EPS)) { realRoots.add(c.getReal()); } } if (realRoots.isEmpty()) { throw new MathException("Could not find any real roots"); } return realRoots.toArray(new Double[realRoots.size()]); } catch (TooManyEvaluationsException e) { throw new MathException(e); } }
Example 4
Source File: AllocationIdUtil.java From proctor with Apache License 2.0 | 6 votes |
private static boolean isUnbalancedRatioChange(final Allocation previous, final Allocation current) { Map<Integer, Double> previousRatios = getBucketRatios(previous.getRanges()); Map<Integer, Double> currentRatios = getBucketRatios(current.getRanges()); final Map<Integer, Double> before = filterEmptyRatios(previousRatios); final Map<Integer, Double> after = filterEmptyRatios(currentRatios); if (!before.keySet().equals(after.keySet())) { return true; } if (before.isEmpty()) { return false; } final int firstBucket = before.keySet().iterator().next(); final double firstRatio = after.get(firstBucket) / before.get(firstBucket); for (final int bucket : before.keySet()) { final double ratio = after.get(bucket) / before.get(bucket); if (!DoubleMath.fuzzyEquals(ratio, firstRatio, 1e-6)) { return true; } } return false; }
Example 5
Source File: PercentageEnricher.java From brooklyn-server with Apache License 2.0 | 5 votes |
@Override public void onEvent(SensorEvent<Number> event) { Number current = producer.sensors().get(sourceCurrentSensor); if (current == null) { LOG.trace("Can't calculate percentage value for entity {} as current from producer {} is null", entity, producer); return; } Number total = producer.sensors().get(sourceTotalSensor); if (total == null) { LOG.trace("Can't calculate percentage value for entity {} as total from producer {} is null", entity, producer); return; } Double currentDouble = current.doubleValue(); Double totalDouble = total.doubleValue(); if (DoubleMath.fuzzyEquals(totalDouble, 0d, EPSILON)) { LOG.trace("Can't calculate percentage value for entity {} as total from producer {} is zero", entity, producer); return; } if (currentDouble < 0d || totalDouble < 0d) { LOG.trace("Can't calculate percentage value for entity {} as current ({}) or total ({}) from producer {} is negative", new Object[] { entity, currentDouble, totalDouble, producer }); return; } Double result = currentDouble / totalDouble; emit(targetSensor, result); }
Example 6
Source File: AllocationIdUtil.java From proctor with Apache License 2.0 | 5 votes |
private static Map<Integer, Double> getBucketRatios(final List<Range> ranges) { final Map<Integer, Double> result = new HashMap<>(); for (final Range range : ranges) { final int bucket = range.getBucketValue(); final double length = range.getLength(); if ((bucket != -1) && !DoubleMath.fuzzyEquals(length, 0.0, 1e-6)) { final double total = result.getOrDefault(bucket, 0.0) + length; result.put(bucket, total); } } return ImmutableMap.copyOf(result); }
Example 7
Source File: GallicSemiring.java From jopenfst with MIT License | 5 votes |
@Override public boolean isApproxEqual(GallicWeight a, GallicWeight b) { if (isZero(a) && isZero(b)) return true; if (isZero(a) || isZero(b)) return false; if (!DoubleMath.fuzzyEquals(a.getWeight(), b.getWeight(), Semiring.DEFAULT_APPROX_EQUAL)) { return false; } return a.getLabels().equals(b.getLabels()); }
Example 8
Source File: FstUtils.java From jopenfst with MIT License | 5 votes |
public static boolean arcEquals(Object thisArcObj, Object thatArcObj, double epsilon) { if (thisArcObj == thatArcObj) { return true; } if (thisArcObj == null || thatArcObj == null) { return false; } if (!Arc.class.isAssignableFrom(thisArcObj.getClass()) || !Arc.class.isAssignableFrom(thatArcObj.getClass())) { return false; } Arc thisArc = (Arc) thisArcObj; Arc thatArc = (Arc) thatArcObj; if (thisArc.getIlabel() != thatArc.getIlabel()) { return false; } if (thisArc.getNextState().getId() != thatArc.getNextState().getId()) { return false; } if (thisArc.getOlabel() != thatArc.getOlabel()) { return false; } if (!(thisArc.getWeight() == thatArc.getWeight())) { if (!DoubleMath.fuzzyEquals(thisArc.getWeight(), thatArc.getWeight(), epsilon)) { return false; } } return true; }
Example 9
Source File: GeneralizedExtremeValueDistribution.java From Strata with Apache License 2.0 | 5 votes |
/** * Creates an instance. * * @param mu The location parameter * @param sigma The scale parameter, not negative or zero * @param ksi The shape parameter */ public GeneralizedExtremeValueDistribution(double mu, double sigma, double ksi) { ArgChecker.isTrue(sigma >= 0, "sigma must be >= 0"); _mu = mu; _sigma = sigma; _ksi = ksi; _ksiIsZero = DoubleMath.fuzzyEquals(ksi, 0d, 1e-13); }
Example 10
Source File: PacketStream.java From tracecompass with Eclipse Public License 2.0 | 5 votes |
/** * Get the the average byte per second from B to A. * * @return the average byte per second from B to A. */ public synchronized double getBPSBtoA() { if (DoubleMath.fuzzyEquals(getDuration(), 0, DELTA)) { return 0; } return fNbBytesBtoA / getDuration(); }
Example 11
Source File: PacketStream.java From tracecompass with Eclipse Public License 2.0 | 5 votes |
/** * Get the the average byte per second from A to B. * * @return the average byte per second from A to B. */ public synchronized double getBPSAtoB() { if (DoubleMath.fuzzyEquals(getDuration(), 0, DELTA)) { return 0; } return fNbBytesAtoB / getDuration(); }
Example 12
Source File: IntWritable.java From DataVec with Apache License 2.0 | 5 votes |
public boolean fuzzyEquals(Writable o, double tolerance) { double other; if (o instanceof IntWritable){ other = ((IntWritable) o).toDouble(); } else if (o instanceof LongWritable) { other = ((LongWritable) o).toDouble(); } else if (o instanceof ByteWritable) { other = ((ByteWritable) o).toDouble(); } else if (o instanceof DoubleWritable) { other = ((DoubleWritable) o).toDouble(); } else if (o instanceof FloatWritable) { other = ((FloatWritable) o).toDouble(); } else { return false; } return DoubleMath.fuzzyEquals(this.value, other, tolerance); }
Example 13
Source File: ByteWritable.java From DataVec with Apache License 2.0 | 5 votes |
public boolean fuzzyEquals(Writable o, double tolerance) { double other; if (o instanceof IntWritable){ other = ((IntWritable) o).toDouble(); } else if (o instanceof LongWritable) { other = ((LongWritable) o).toDouble(); } else if (o instanceof ByteWritable) { other = ((ByteWritable) o).toDouble(); } else if (o instanceof DoubleWritable) { other = ((DoubleWritable) o).toDouble(); } else if (o instanceof FloatWritable) { other = ((FloatWritable) o).toDouble(); } else { return false; } return DoubleMath.fuzzyEquals(this.value, other, tolerance); }
Example 14
Source File: LongWritable.java From DataVec with Apache License 2.0 | 5 votes |
public boolean fuzzyEquals(Writable o, double tolerance) { double other; if (o instanceof IntWritable){ other = ((IntWritable) o).toDouble(); } else if (o instanceof LongWritable) { other = ((LongWritable) o).toDouble(); } else if (o instanceof ByteWritable) { other = ((ByteWritable) o).toDouble(); } else if (o instanceof DoubleWritable) { other = ((DoubleWritable) o).toDouble(); } else if (o instanceof FloatWritable) { other = ((FloatWritable) o).toDouble(); } else { return false; } return DoubleMath.fuzzyEquals(this.value, other, tolerance); }
Example 15
Source File: FloatWritable.java From DataVec with Apache License 2.0 | 5 votes |
public boolean fuzzyEquals(Writable o, double tolerance) { double other; if (o instanceof IntWritable){ other = ((IntWritable) o).toDouble(); } else if (o instanceof LongWritable) { other = ((LongWritable) o).toDouble(); } else if (o instanceof ByteWritable) { other = ((ByteWritable) o).toDouble(); } else if (o instanceof DoubleWritable) { other = ((DoubleWritable) o).toDouble(); } else if (o instanceof FloatWritable) { other = ((FloatWritable) o).toDouble(); } else { return false; } return DoubleMath.fuzzyEquals(this.value, other, tolerance); }
Example 16
Source File: InverseIncompleteBetaFunction.java From Strata with Apache License 2.0 | 4 votes |
@Override public Double apply(Double x) { ArgChecker.inRangeInclusive(x, 0d, 1d, "x"); double pp, p, t, h, w, lnA, lnB, u, a1 = _a - 1; double b1 = _b - 1; if (_a >= 1 && _b >= 1) { pp = x < 0.5 ? x : 1 - x; t = Math.sqrt(-2 * Math.log(pp)); p = (2.30753 + t * 0.27061) / (1 + t * (0.99229 + t * 0.04481)) - t; if (p < 0.5) { p *= -1; } a1 = (Math.sqrt(p) - 3.) / 6.; double tempA = 1. / (2 * _a - 1); double tempB = 1. / (2 * _b - 1); h = 2. / (tempA + tempB); w = p * Math.sqrt(a1 + h) / h - (tempB - tempA) * (a1 + 5. / 6 - 2. / (3 * h)); p = _a / (_a + _b + Math.exp(2 * w)); } else { lnA = Math.log(_a / (_a + _b)); lnB = Math.log(_b / (_a + _b)); t = Math.exp(_a * lnA) / _a; u = Math.exp(_b * lnB) / _b; w = t + u; if (x < t / w) { p = Math.pow(_a * w * x, 1. / _a); } else { p = 1 - Math.pow(_b * w * (1 - x), 1. / _b); } } double afac = -_lnGamma.apply(_a) - _lnGamma.apply(_b) + _lnGamma.apply(_a + _b); double error; for (int j = 0; j < 10; j++) { if (DoubleMath.fuzzyEquals(p, 0d, 1e-16) || DoubleMath.fuzzyEquals(p, (double) 1, 1e-16)) { throw new MathException("a or b too small for accurate evaluation"); } error = _beta.apply(p) - x; t = Math.exp(a1 * Math.log(p) + b1 * Math.log(1 - p) + afac); u = error / t; t = u / (1 - 0.5 * Math.min(1, u * (a1 / p - b1 / (1 - p)))); p -= t; if (p <= 0) { p = 0.5 * (p + t); } if (p >= 1) { p = 0.5 * (p + t + 1); } if (Math.abs(t) < EPS * p && j > 0) { break; } } return p; }
Example 17
Source File: GuavaDoubleMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenFuzzyEqualDouble_shouldReturnZeroIfInRange() { boolean result = DoubleMath.fuzzyEquals(4, 4.05, 0.6); assertTrue(result); }
Example 18
Source File: DoubleArrayMath.java From Strata with Apache License 2.0 | 3 votes |
/** * Compares each element in the array to zero within a tolerance. * <p> * An empty array returns true; * <p> * The input array is not mutated. * * @param array the array to check * @param tolerance the tolerance to use * @return true if the array is effectively equal to zero */ public static boolean fuzzyEqualsZero(double[] array, double tolerance) { for (int i = 0; i < array.length; i++) { if (!DoubleMath.fuzzyEquals(array[i], 0, tolerance)) { return false; } } return true; }
Example 19
Source File: CurrencyAmount.java From Strata with Apache License 2.0 | 3 votes |
/** * Converts this amount to an equivalent amount the specified currency. * <p> * The result will be expressed in terms of the given currency, converting * using the specified FX rate. * <p> * For example, if this represents 'GBP 100' and this method is called with * arguments {@code (USD, 1.6)} then the result will be 'USD 160'. * * @param resultCurrency the currency of the result * @param fxRate the FX rate from this currency to the result currency * @return the converted instance, which should be expressed in the specified currency * @throws IllegalArgumentException if the FX is not 1 when no conversion is required */ public CurrencyAmount convertedTo(Currency resultCurrency, double fxRate) { if (currency.equals(resultCurrency)) { if (DoubleMath.fuzzyEquals(fxRate, 1d, 1e-8)) { return this; } throw new IllegalArgumentException("FX rate must be 1 when no conversion required"); } return CurrencyAmount.of(resultCurrency, amount * fxRate); }
Example 20
Source File: ArgChecker.java From Strata with Apache License 2.0 | 3 votes |
/** * Checks that the argument is greater than zero to within a given accuracy. * <p> * Given the input argument, this returns only if it is greater than zero * using the {@code eps} accuracy for zero. * For example, in a constructor: * <pre> * this.amount = ArgChecker.notNegativeOrZero(amount, 0.0001d, "amount"); * </pre> * * @param argument the value to check * @param tolerance the tolerance to use for zero * @param name the name of the argument to use in the error message, not null * @return the input {@code argument} * @throws IllegalArgumentException if the absolute value of the argument is less than eps */ public static double notNegativeOrZero(double argument, double tolerance, String name) { if (DoubleMath.fuzzyEquals(argument, 0, tolerance)) { throw new IllegalArgumentException("Argument '" + name + "' must not be zero"); } if (argument < 0) { throw new IllegalArgumentException("Argument '" + name + "' must be greater than zero but has value " + argument); } return argument; }