/** * Checks if this array equals another within the specified tolerance. * <p> * This returns true if the two instances have {@code double} values that are * equal within the specified tolerance. * * @param other the other array * @param tolerance the tolerance * @return true if equal up to the tolerance */ public boolean equalWithTolerance(DoubleArray other, double tolerance) { return DoubleArrayMath.fuzzyEquals(array, other.array, tolerance); }
private double computeKappa(HullWhiteOneFactorPiecewiseConstantParametersProvider hwProvider, double[] discountedCashFlow, double[] alpha, double omega) { double kappa = 0d; if (DoubleArrayMath.fuzzyEqualsZero(alpha, SMALL)) { // threshold coherent to rootfinder in kappa computation double totalPv = DoubleArrayMath.sum(discountedCashFlow); kappa = totalPv * omega > 0d ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; } else { kappa = hwProvider.getModel().kappa(DoubleArray.ofUnsafe(discountedCashFlow), DoubleArray.ofUnsafe(alpha)); } return kappa; }
private double[][] getDataTotal(double[] xData, double[] yData) { int nExtraPoints = _xValuesClamped.length; int nData = xData.length; int nTotal = nExtraPoints + nData; double[] xValuesTotal = new double[nTotal]; double[] yValuesTotal = new double[nTotal]; System.arraycopy(xData, 0, xValuesTotal, 0, nData); System.arraycopy(yData, 0, yValuesTotal, 0, nData); System.arraycopy(_xValuesClamped, 0, xValuesTotal, nData, nExtraPoints); System.arraycopy(_yValuesClamped, 0, yValuesTotal, nData, nExtraPoints); DoubleArrayMath.sortPairs(xValuesTotal, yValuesTotal); return new double[][] {xValuesTotal, yValuesTotal }; }
/** * Checks if this array equals zero within the specified tolerance. * <p> * This returns true if all the {@code double} values equal zero within the specified tolerance. * * @param tolerance the tolerance * @return true if equal up to the tolerance */ public boolean equalZeroWithTolerance(double tolerance) { return DoubleArrayMath.fuzzyEqualsZero(array, tolerance); }
/** * Combines two arrays, returning an array where each element is the sum of the two matching inputs. * <p> * Each element in the result will be the sum of the matching index in the two input arrays. * The two input arrays must have the same length. * <p> * For example: * <pre> * double[] array1 = {1, 5, 9}; * double[] array2 = {2, 3, 2}; * double[] result = DoubleArrayMath.combineByAddition(array1, array2); * // result contains {3, 8, 11} * </pre> * <p> * The result is always a new array. The input arrays are not mutated. * * @param array1 the first array * @param array2 the second array * @return an array combining the two input arrays using the plus operator */ public static double[] combineByAddition(double[] array1, double[] array2) { return combine(array1, array2, (a, b) -> a + b); }
GeneralizedLeastSquareResults<Double> solve( double[] x, double[] y, double[] sigma, List<Function<Double, Double>> basisFunctions, double lambda, int differenceOrder) { return solve(DoubleArrayMath.toObject(x), y, sigma, basisFunctions, lambda, differenceOrder); }
@Override public VectorFunction from(Double[] x) { ArgChecker.notNull(x, "x"); return from(DoubleArrayMath.toPrimitive(x)); }
public void test_sum() { assertThat(DoubleArrayMath.sum(ARRAY_1_2)).isEqualTo(3d); }
public void test_fuzzyEqualsZero() { assertThat(DoubleArrayMath.fuzzyEqualsZero(DoubleArrayMath.EMPTY_DOUBLE_ARRAY, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEqualsZero(ARRAY_0_0, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEqualsZero(ARRAY_1_2, 1e-2)).isFalse(); }
/** * Combines two arrays, returning an array where each element is the multiplication of the two matching inputs. * <p> * Each element in the result will be the multiplication of the matching index in the two input arrays. * The two input arrays must have the same length. * <p> * For example: * <pre> * double[] array1 = {1, 5, 9}; * double[] array2 = {2, 3, 4}; * double[] result = DoubleArrayMath.combineByMultiplication(array1, array2); * // result contains {2, 15, 36} * </pre> * <p> * The result is always a new array. The input arrays are not mutated. * * @param array1 the first array * @param array2 the second array * @return an array combining the two input arrays using the multiply operator */ public static double[] combineByMultiplication(double[] array1, double[] array2) { return combine(array1, array2, (a, b) -> a * b); }
public void toPrimitive() { assertThat(DoubleArrayMath.toPrimitive(new Double[] {})).isEqualTo(new double[] {}); assertThat(DoubleArrayMath.toPrimitive(new Double[] {1d, 2.5d})).isEqualTo(new double[] {1d, 2.5d}); }
public void test_fuzzyEquals() { assertThat(DoubleArrayMath.fuzzyEquals(DoubleArrayMath.EMPTY_DOUBLE_ARRAY, ARRAY_0_0, 1e-2)).isFalse(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_0_0, ARRAY_0_0, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-2)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-3)).isTrue(); assertThat(DoubleArrayMath.fuzzyEquals(ARRAY_1_2, ARRAY_1_2B, 1e-4)).isFalse(); }
private double[][] getDataTotal(double[] xData, double[] yData) { int nExtraPoints = _xValuesClamped.length; int nData = xData.length; int nTotal = nExtraPoints + nData; double[] xValuesTotal = new double[nTotal]; double[] yValuesTotal = new double[nTotal]; System.arraycopy(xData, 0, xValuesTotal, 0, nData); System.arraycopy(yData, 0, yValuesTotal, 0, nData); System.arraycopy(_xValuesClamped, 0, xValuesTotal, nData, nExtraPoints); System.arraycopy(_yValuesClamped, 0, yValuesTotal, nData, nExtraPoints); DoubleArrayMath.sortPairs(xValuesTotal, yValuesTotal); double[] xyTotal = getProduct(xValuesTotal, yValuesTotal); return new double[][] {xValuesTotal, xyTotal }; }
private boolean equalZeroWithTolerance(DoubleMatrix sens, double tolerance) { int colCount = sens.columnCount(); for (int i = 0; i < colCount; ++i) { if (!DoubleArrayMath.fuzzyEqualsZero(sens.column(i).toArray(), tolerance)) { return false; } } return true; }
int len2 = array2.length; if (len1 == len2) { return combine(array1, array2, operator);
public void test_formula() { CoxRossRubinsteinLatticeSpecification test = new CoxRossRubinsteinLatticeSpecification(); DoubleArray computed = test.getParametersTrinomial(VOL, RATE, DT); double u = Math.exp(VOL * Math.sqrt(2.0 * DT)); double d = Math.exp(-VOL * Math.sqrt(2.0 * DT)); double up = Math.pow((Math.exp(0.5 * RATE * DT) - Math.exp(-VOL * Math.sqrt(0.5 * DT))) / (Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(-VOL * Math.sqrt(0.5 * DT))), 2); double dp = Math.pow((Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(0.5 * RATE * DT)) / (Math.exp(VOL * Math.sqrt(0.5 * DT)) - Math.exp(-VOL * Math.sqrt(0.5 * DT))), 2); DoubleArray expected = DoubleArray.of(u, 1d, d, up, 1d - up - dp, dp); assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected.toArray(), 1.0e-14)); }
public void test_sortPairs_doubledouble_sizeDifferent() { double[] keys = {3d, 2d, 5d, 4d}; double[] values = {6d, 4d}; assertThrowsIllegalArg(() -> DoubleArrayMath.sortPairs(keys, values)); }
public void regression_hullWhiteSensitivity() { DoubleArray computed = PRICER.presentValueSensitivityModelParamsHullWhite(SWAPTION_PAY_LONG, RATE_PROVIDER, HW_PROVIDER); double[] expected = new double[] { 2.9365484063149095E7, 3.262667329294093E7, 7.226220286364576E7, 2.4446925038968167E8, 120476.73820821749}; assertTrue(DoubleArrayMath.fuzzyEquals(computed.toArray(), expected, NOTIONAL * TOL)); } }