List of usage examples for com.google.common.math DoubleMath fuzzyEquals
public static boolean fuzzyEquals(double a, double b, double tolerance)
From source file:com.opengamma.collect.ArgChecker.java
/** * Checks that the argument is greater than zero to within a given accuracy. * <p>//from www . j ava 2s . c o m * Given the input parameter, 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 parameter the value to check * @param tolerance the tolerance to use for zero * @param name the name of the parameter to use in the error message, not null * @return the input {@code parameter} * @throws IllegalArgumentException if the absolute value of the argument is less than eps */ public static double notNegativeOrZero(double parameter, double tolerance, String name) { if (DoubleMath.fuzzyEquals(parameter, 0, tolerance)) { throw new IllegalArgumentException("Argument '" + name + "' must not be zero"); } if (parameter < 0) { throw new IllegalArgumentException("Argument '" + name + "' must be greater than zero"); } return parameter; }
From source file:com.opengamma.strata.loader.csv.RatesCalibrationCsvLoader.java
private static CurveNode curveFxSwapCurveNode(String conventionStr, String timeStr, String label, QuoteId quoteId, double spread, CurveNodeDate date, CurveNodeDateOrder order) { if (!DoubleMath.fuzzyEquals(spread, 0d, 1e-10d)) { throw new IllegalArgumentException("Additional spread must be zero for FX swaps"); }/* w w w. ja v a 2 s .c o m*/ Matcher matcher = SIMPLE_YMD_TIME_REGEX.matcher(timeStr.toUpperCase(Locale.ENGLISH)); if (!matcher.matches()) { throw new IllegalArgumentException(Messages.format("Invalid time format for FX swap: {}", timeStr)); } Period periodToEnd = Period.parse("P" + matcher.group(1)); FxSwapConvention convention = FxSwapConvention.of(conventionStr); FxSwapTemplate template = FxSwapTemplate.of(periodToEnd, convention); return FxSwapCurveNode.builder().template(template).farForwardPointsId(quoteId).label(label).date(date) .dateOrder(order).build(); }
From source file:com.opengamma.collect.ArgChecker.java
/** * Checks that the argument is not equal to zero to within a given accuracy. * <p>/*from w ww .j a va2 s.c om*/ * Given the input parameter, this returns only if it is not zero comparing * using the {@code eps} accuracy. * For example, in a constructor: * <pre> * this.amount = ArgChecker.notZero(amount, 0.0001d, "amount"); * </pre> * * @param parameter the value to check * @param tolerance the tolerance to use for zero * @param name the name of the parameter to use in the error message, not null * @return the input {@code parameter} * @throws IllegalArgumentException if the absolute value of the argument is less than eps */ public static double notZero(double parameter, double tolerance, String name) { if (DoubleMath.fuzzyEquals(parameter, 0d, tolerance)) { throw new IllegalArgumentException("Argument '" + name + "' must not be zero"); } return parameter; }
From source file:com.analog.lyric.dimple.factorfunctions.core.SparseFactorTable.java
@Override int normalizeDirected(boolean justCheck, boolean ignoreZeroWeightInputs) { final JointDomainIndexer domains = getDomainIndexer(); boolean computeNormalizedTotal = justCheck; double normalizedTotal = 1.0; int nNotNormalized = 0; // We represent the joint index such that the outputs for the same // input are stored consecutively, so we only need to walk through // the values in order. ///*w ww. j av a2 s . c om*/ // When just checking, we allow total to equal something other than one // as long as they are all the same. final int size = _indexArray.length; for (int si = 0, nextsi = 1, start = 0; si < size; si = nextsi++) { if (nextsi == size || !domains.hasSameInputs(_indexArray[si]._indices, _indexArray[nextsi]._indices)) { double totalForInput = 0.0; if (hasSparseWeights()) { for (int i = start; i < nextsi; ++i) { totalForInput += _sparseWeights[i]; } } else { for (int i = start; i < nextsi; ++i) { totalForInput += energyToWeight(_sparseEnergies[i]); } } if (totalForInput == 0.0) { if (ignoreZeroWeightInputs) ++nNotNormalized; else return normalizeDirectedHandleZeroForInput(justCheck); } if (computeNormalizedTotal) { normalizedTotal = totalForInput; computeNormalizedTotal = false; } else if (!DoubleMath.fuzzyEquals(totalForInput, normalizedTotal, 1e-12)) { if (justCheck) { return 1; } } if (!justCheck) { if (totalForInput != 0.0) { if (hasSparseWeights()) { for (int i = start; i < nextsi; ++i) { _sparseWeights[i] /= totalForInput; } } if (hasSparseEnergies()) { double logTotalForInput = Math.log(totalForInput); for (int i = start; i < nextsi; ++i) { _sparseEnergies[i] += logTotalForInput; } } } } start = nextsi; } } if (nNotNormalized == 0) { _computedMask |= CONDITIONAL | CONDITIONAL_COMPUTED; } else { _computedMask |= CONDITIONAL_COMPUTED; } return nNotNormalized; }
From source file:com.analog.lyric.dimple.factorfunctions.core.SparseFactorTable.java
@Override boolean normalizeUndirected(boolean justCheck) { if ((_computedMask & NORMALIZED) != 0) { return true; }/*from ww w . jav a 2 s .c o m*/ double total = 0.0; if (hasSparseWeights()) { for (double w : _sparseWeights) { total += w; } } else { for (double e : _sparseEnergies) { total += energyToWeight(e); } } if (!DoubleMath.fuzzyEquals(total, 1.0, 1e-12)) { if (justCheck) { return false; } if (total == 0.0) { throw normalizeUndirectedHandleZero(); } for (int i = _sparseWeights.length; --i >= 0;) { _sparseWeights[i] /= total; } if (_sparseEnergies.length > 0) { final double logTotal = Math.log(total); for (int i = _sparseEnergies.length; --i >= 0;) { _sparseEnergies[i] += logTotal; } } } _computedMask |= NORMALIZED | NORMALIZED_COMPUTED; return true; }
From source file:com.opengamma.strata.collect.ArgChecker.java
/** * Checks that the argument is greater than zero to within a given accuracy. * <p>/* w ww.j ava 2 s. com*/ * 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; }
From source file:com.opengamma.strata.collect.ArgChecker.java
/** * Checks that the argument is not equal to zero to within a given accuracy. * <p>/*from w ww.j a v a 2 s . co m*/ * Given the input argument, this returns only if it is not zero comparing * using the {@code eps} accuracy. * For example, in a constructor: * <pre> * this.amount = ArgChecker.notZero(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 the tolerance */ public static double notZero(double argument, double tolerance, String name) { if (DoubleMath.fuzzyEquals(argument, 0d, tolerance)) { throw new IllegalArgumentException("Argument '" + name + "' must not be zero"); } return argument; }
From source file:com.analog.lyric.dimple.factorfunctions.core.FactorTable.java
@Override int normalizeDirected(boolean justCheck, boolean ignoreZeroWeightInputs) { final JointDomainIndexer domains = getDomainIndexer(); final int inputSize = domains.getInputCardinality(); final int outputSize = domains.getOutputCardinality(); final boolean hasSparseToJoint = _sparseIndexToJointIndex.length > 0; boolean computeNormalizedTotal = justCheck; double normalizedTotal = 1.0; double totalForInput = 0.0; int nNotNormalized = 0; final double[] normalizers = justCheck ? null : new double[inputSize]; // We represent the joint index such that the outputs for the same // input are stored consecutively, so we only need to walk through // the values in order. //// w w w . java2 s . com // When just checking, we allow total to equal something other than one // as long as they are all the same. switch (_representation & ALL_VALUES) { case DETERMINISTIC: break; case ALL_VALUES: case ALL_WEIGHT: case ALL_SPARSE: case NOT_DENSE_WEIGHT: case NOT_SPARSE_ENERGY: case DENSE_ENERGY_SPARSE_WEIGHT: case NOT_DENSE_ENERGY: case SPARSE_WEIGHT: for (int ii = 0, si = 0, size = _sparseWeights.length; ii < inputSize; ++ii) { final int maxji = domains.jointIndexFromInputOutputIndices(ii, outputSize - 1); totalForInput = 0.0; for (; si < size && maxji >= (hasSparseToJoint ? _sparseIndexToJointIndex[si] : si); ++si) { totalForInput += _sparseWeights[si]; } if (totalForInput == 0.0) { if (ignoreZeroWeightInputs) ++nNotNormalized; else return normalizeDirectedHandleZeroForInput(justCheck); } if (computeNormalizedTotal) { normalizedTotal = totalForInput; computeNormalizedTotal = false; } else if (!DoubleMath.fuzzyEquals(totalForInput, normalizedTotal, 1e-12)) { if (justCheck) { return 1; } } if (normalizers != null) { normalizers[ii] = totalForInput; } } if (normalizers != null) { for (int si = 0, size = _sparseWeights.length; si < size; ++si) { final int ji = hasSparseToJoint ? _sparseIndexToJointIndex[si] : si; final int ii = domains.inputIndexFromJointIndex(ji); final double normalizer = normalizers[ii]; if (normalizer != 0.0) { setWeightForSparseIndex(_sparseWeights[si] / normalizers[ii], si); } } } break; case ALL_ENERGY: case SPARSE_ENERGY: case NOT_SPARSE_WEIGHT: case SPARSE_ENERGY_DENSE_WEIGHT: // TODO: if sparse size is large enough, it would be faster to iterate over the dense weights for (int ii = 0, si = 0, size = _sparseEnergies.length; ii < inputSize; ++ii) { final int maxji = domains.jointIndexFromInputOutputIndices(ii, outputSize - 1); totalForInput = 0.0; for (; si < size && maxji >= (hasSparseToJoint ? _sparseIndexToJointIndex[si] : si); ++si) { totalForInput += energyToWeight(_sparseEnergies[si]); } if (totalForInput == 0.0) { if (ignoreZeroWeightInputs) ++nNotNormalized; else return normalizeDirectedHandleZeroForInput(justCheck); } if (computeNormalizedTotal) { normalizedTotal = totalForInput; computeNormalizedTotal = false; } else if (!DoubleMath.fuzzyEquals(totalForInput, normalizedTotal, 1e-12)) { if (justCheck) { return 1; } } if (normalizers != null) { normalizers[ii] = Math.log(totalForInput); } } if (normalizers != null) { for (int si = 0, size = _sparseEnergies.length; si < size; ++si) { final int ji = hasSparseToJoint ? _sparseIndexToJointIndex[si] : si; final int ii = domains.inputIndexFromJointIndex(ji); final double normalizer = normalizers[ii]; if (normalizer != Double.NEGATIVE_INFINITY) { setEnergyForSparseIndex(_sparseEnergies[si] + normalizers[ii], si); } } } break; case ALL_DENSE: case DENSE_WEIGHT: for (int jointIndex = 0, inputIndex = 0; inputIndex < inputSize; ++inputIndex, jointIndex += outputSize) { totalForInput = 0.0; for (int outputIndex = 0; outputIndex < outputSize; ++outputIndex) { totalForInput += _denseWeights[jointIndex + outputIndex]; } if (totalForInput == 0.0) { if (ignoreZeroWeightInputs) ++nNotNormalized; else return normalizeDirectedHandleZeroForInput(justCheck); } if (computeNormalizedTotal) { normalizedTotal = totalForInput; computeNormalizedTotal = false; } else if (!DoubleMath.fuzzyEquals(totalForInput, normalizedTotal, 1e-12)) { if (justCheck) { return 1; } } if (normalizers != null) { normalizers[inputIndex] = totalForInput; } } if (normalizers != null) { for (int jointIndex = 0, inputIndex = 0; inputIndex < inputSize; ++inputIndex, jointIndex += outputSize) { totalForInput = normalizers[inputIndex]; if (totalForInput != 0.0) { for (int outputIndex = 0; outputIndex < outputSize; ++outputIndex) { int ji = jointIndex + outputIndex; setWeightForJointIndex(_denseWeights[ji] / totalForInput, ji); } } } } break; case DENSE_ENERGY: for (int jointIndex = 0, inputIndex = 0; inputIndex < inputSize; ++inputIndex, jointIndex += outputSize) { totalForInput = 0.0; for (int outputIndex = 0; outputIndex < outputSize; ++outputIndex) { totalForInput += energyToWeight(_denseEnergies[jointIndex + outputIndex]); } if (totalForInput == 0.0) { if (ignoreZeroWeightInputs) ++nNotNormalized; else return normalizeDirectedHandleZeroForInput(justCheck); } if (computeNormalizedTotal) { normalizedTotal = totalForInput; computeNormalizedTotal = false; } else if (!DoubleMath.fuzzyEquals(totalForInput, normalizedTotal, 1e-12)) { if (justCheck) { return 1; } } if (normalizers != null) { normalizers[inputIndex] = Math.log(totalForInput); } } if (normalizers != null) { for (int jointIndex = 0, inputIndex = 0; inputIndex < inputSize; ++inputIndex, jointIndex += outputSize) { final double normalizer = normalizers[inputIndex]; if (normalizer != Double.NEGATIVE_INFINITY) { for (int outputIndex = 0; outputIndex < outputSize; ++outputIndex) { int ji = jointIndex + outputIndex; setEnergyForJointIndex(_denseEnergies[ji] + normalizer, ji); } } } } break; } if (nNotNormalized == 0) { _computedMask |= CONDITIONAL | CONDITIONAL_COMPUTED; } else { _computedMask |= CONDITIONAL_COMPUTED; } return nNotNormalized; }
From source file:com.analog.lyric.dimple.factorfunctions.core.FactorTable.java
@Override boolean normalizeUndirected(boolean justCheck) { if ((_computedMask & NORMALIZED) != 0) { return true; }/*from ww w .j a va2 s .co m*/ double total = 0.0; switch (_representation & ALL_VALUES) { case ALL_VALUES: case ALL_WEIGHT: case ALL_SPARSE: case NOT_DENSE_WEIGHT: case NOT_SPARSE_ENERGY: case DENSE_ENERGY_SPARSE_WEIGHT: case NOT_DENSE_ENERGY: case SPARSE_WEIGHT: for (double w : _sparseWeights) { total += w; } break; case ALL_ENERGY: case SPARSE_ENERGY: case NOT_SPARSE_WEIGHT: case SPARSE_ENERGY_DENSE_WEIGHT: // TODO: if sparse size is large enough, it would be faster to iterate over the dense weights for (double e : _sparseEnergies) { total += energyToWeight(e); } break; case ALL_DENSE: case DENSE_WEIGHT: for (double w : _denseWeights) { total += w; } break; case DENSE_ENERGY: for (double e : _denseEnergies) { total += energyToWeight(e); } break; } if (!DoubleMath.fuzzyEquals(total, 1.0, 1e-12)) { if (justCheck) { return false; } if (total == 0.0) { throw normalizeUndirectedHandleZero(); } for (int i = _sparseWeights.length; --i >= 0;) { _sparseWeights[i] /= total; } if (_sparseWeights != _denseWeights) { for (int i = _denseWeights.length; --i >= 0;) { _denseWeights[i] /= total; } } final double logTotal = Math.log(total); for (int i = _sparseEnergies.length; --i >= 0;) { _sparseEnergies[i] += logTotal; } if (_sparseEnergies != _denseEnergies) { for (int i = _denseEnergies.length; --i >= 0;) { _denseEnergies[i] += logTotal; } } } _computedMask |= NORMALIZED | NORMALIZED_COMPUTED; return true; }