@Override protected double doFirstDerivative(double xValue) { int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); return differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (!(obj instanceof PiecewisePolynomialResultsWithSensitivity)) { return false; } PiecewisePolynomialResultsWithSensitivity other = (PiecewisePolynomialResultsWithSensitivity) obj; if (!Arrays.equals(_coeffSense, other._coeffSense)) { return false; } return true; }
@Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + Arrays.hashCode(_coeffSense); return result; }
@Override protected double doInterpolate(double xValue) { return evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); }
/** * */ public void localMonotonicityIncTest() { final double[] xValues = new double[] {2., 3., 5., 8., 9., 13. }; final double[] yValues = new double[] {1., 1.01, 2., 2.1, 2.2, 2.201 }; PiecewisePolynomialInterpolator interp = new NaturalSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); PiecewisePolynomialInterpolator interpPos = new MonotonicityPreservingCubicSplineInterpolator(interp); PiecewisePolynomialResult resultPos = interpPos.interpolate(xValues, yValues); assertEquals(resultPos.getDimensions(), result.getDimensions()); assertEquals(resultPos.getNumberOfIntervals(), result.getNumberOfIntervals()); assertEquals(resultPos.getOrder(), result.getOrder()); final int nKeys = 111; double key0 = 2.; for (int i = 1; i < nKeys; ++i) { final double key = 2. + 11. / (nKeys - 1) * i; assertTrue(function.evaluate(resultPos, key).get(0) - function.evaluate(resultPos, key0).get(0) >= 0.); key0 = 2. + 11. / (nKeys - 1) * i; } }
return new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1);
final PiecewisePolynomialResult result = _method.interpolate(xValues, yValues); ArgChecker.isTrue(result.getOrder() == 4, "Primary interpolant is not cubic"); return new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1);
Bound(DoubleArray xValues, DoubleArray yValues) { super(xValues, yValues); this.xValues = xValues.toArrayUnsafe(); this.yValues = yValues.toArrayUnsafe(); PiecewisePolynomialInterpolator underlying = new PiecewiseCubicHermiteSplineInterpolatorWithSensitivity(); PiecewisePolynomialResult poly = underlying.interpolate(xValues.toArray(), yValues.toArray()); this.knots = poly.getKnots(); this.coefMatrix = poly.getCoefMatrix(); this.coefMatrixSensi = Suppliers.memoize( () -> underlying.interpolateWithSensitivity(xValues.toArray(), yValues.toArray()).getCoefficientSensitivityAll()); }
new PiecewisePolynomialResult(DoubleArray.copyOf(knots1), DoubleMatrix.copyOf(matrix1), order, dim1); final PiecewisePolynomialResult res2 = new PiecewisePolynomialResult(DoubleArray.copyOf(knots1), DoubleMatrix.copyOf(matrix1), order, dim1); final PiecewisePolynomialResult res3 = new PiecewisePolynomialResult(DoubleArray.copyOf(knots2), DoubleMatrix.copyOf(matrix2), order, dim2); final PiecewisePolynomialResult res4 = new PiecewisePolynomialResult(DoubleArray.copyOf(knots1), DoubleMatrix.copyOf(matrix1), 2, dim1); final PiecewisePolynomialResult res5 = new PiecewisePolynomialResult(DoubleArray.copyOf(knots1), DoubleMatrix.copyOf(matrix1), order, dim1 - 1); final PiecewisePolynomialResult res6 = new PiecewisePolynomialResult(DoubleArray.of(1., 2., 3., 5.), DoubleMatrix.copyOf(matrix1), order, dim1); assertTrue(res1.equals(res1)); assertTrue(res1.equals(res2)); assertTrue(res2.equals(res1)); assertTrue(res2.hashCode() == res1.hashCode()); assertTrue(!(res3.hashCode() == res1.hashCode())); assertTrue(!(res1.equals(res3))); assertTrue(!(res3.equals(res1))); assertTrue(!(res4.hashCode() == res1.hashCode())); assertTrue(!(res1.equals(res4))); assertTrue(!(res4.equals(res1))); assertTrue(!(res5.hashCode() == res1.hashCode())); assertTrue(!(res1.equals(res5))); assertTrue(!(res5.equals(res1)));
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval than knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); int nCoefs = coefficientSensitivity.rowCount(); double s = xValue - poly.getKnots().get(interval); DoubleArray res = coefficientSensitivity.row(0); for (int i = 1; i < nCoefs; i++) { res = (DoubleArray) MA.scale(res, s); res = (DoubleArray) MA.add(res, coefficientSensitivity.row(i)); } return res; }
/** * Sample data */ public void sampleDataTest() { final double[] xValues = new double[] {0., 10., 30., 50., 70., 90., 100. }; final double[] yValues = new double[] {30., 130., 150., 150., 170., 220., 320. }; PiecewisePolynomialInterpolator interp = new ConstrainedCubicSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); final double[][] coefsMatPartExp = new double[][] { {-9. / 220., 0., 155. / 11., 30. }, {-1. / 2200., -7. / 220., 20. / 11., 130. }, {0., 0., 0., 150. } }; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { final double ref = Math.abs(coefsMatPartExp[i][j]) == 0. ? 1. : Math.abs(coefsMatPartExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatPartExp[i][j], ref * EPS); } } int nKeys = 101; double key0 = 0.; for (int i = 1; i < nKeys; ++i) { final double key = 0. + 100. / (nKeys - 1) * i; assertTrue(function.evaluate(result, key).get(0) - function.evaluate(result, key0).get(0) >= -EPS); key0 = 0. + 100. / (nKeys - 1) * i; } }
@Override protected double doInterpolate(double xValue) { return evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); }
/** * */ public void localMonotonicityClampedTest() { final double[] xValues = new double[] {-2., 3., 4., 8., 9.1, 10. }; final double[] yValues = new double[] {0., 10., 9.5, 2., 1.1, -2.2, -2.6, 0. }; PiecewisePolynomialInterpolator interp = new CubicSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); PiecewisePolynomialInterpolator interpPos = new MonotonicityPreservingCubicSplineInterpolator(interp); PiecewisePolynomialResult resultPos = interpPos.interpolate(xValues, yValues); assertEquals(resultPos.getDimensions(), result.getDimensions()); assertEquals(resultPos.getNumberOfIntervals(), result.getNumberOfIntervals()); assertEquals(resultPos.getOrder(), result.getOrder()); final int nKeys = 121; double key0 = -2.; for (int i = 1; i < nKeys; ++i) { final double key = -2. + 12. / (nKeys - 1) * i; assertTrue(function.evaluate(resultPos, key).get(0) - function.evaluate(resultPos, key0).get(0) <= 0.); key0 = -2. + 11. / (nKeys - 1) * i; } }
return new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1);
final PiecewisePolynomialResult result = _method.interpolate(xValues, yValues); ArgChecker.isTrue(result.getOrder() == 4, "Primary interpolant is not cubic"); return new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1);
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval than knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); int nCoefs = coefficientSensitivity.rowCount(); double s = xValue - poly.getKnots().get(interval); DoubleArray res = coefficientSensitivity.row(0); for (int i = 1; i < nCoefs; i++) { res = (DoubleArray) MA.scale(res, s); res = (DoubleArray) MA.add(res, coefficientSensitivity.row(i)); } return res; }
@Override protected double doFirstDerivative(double xValue) { int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); return differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); }
@Override protected double doInterpolate(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); return Math.exp(resValue); }
/** * */ public void localMonotonicityDecTest() { final double[] xValues = new double[] {-2., 3., 4., 8., 9.1, 10. }; final double[] yValues = new double[] {10., 9.5, 2., 1.1, -2.2, -2.6 }; PiecewisePolynomialInterpolator interp = new CubicSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); PiecewisePolynomialInterpolator interpPos = new MonotonicityPreservingCubicSplineInterpolator(interp); PiecewisePolynomialResult resultPos = interpPos.interpolate(xValues, yValues); assertEquals(resultPos.getDimensions(), result.getDimensions()); assertEquals(resultPos.getNumberOfIntervals(), result.getNumberOfIntervals()); assertEquals(resultPos.getOrder(), result.getOrder()); final int nKeys = 121; double key0 = -2.; for (int i = 1; i < nKeys; ++i) { final double key = -2. + 12. / (nKeys - 1) * i; assertTrue(function.evaluate(resultPos, key).get(0) - function.evaluate(resultPos, key0).get(0) <= 0.); key0 = -2. + 11. / (nKeys - 1) * i; } }
/** * dim must be 1 for evaluateAndDifferentiate. */ @Test(expectedExceptions = UnsupportedOperationException.class) public void dimFailTest() { DoubleMatrix coefsMatrix = DoubleMatrix.copyOf(new double[][] { {1., -3., 3., -1 }, {0., 5., -20., 20 }, {1., 0., 0., 0. }, {0., 5., -10., 5 }, {1., 3., 3., 1. }, {0., 5., 0., 0. } }); int dim = 2; int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluateAndDifferentiate(pp, 1.5); }