@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 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 doFirstDerivative(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); double resDerivative = differentiate( xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); return Math.exp(resValue) * resDerivative; }
@Override protected double doFirstDerivative(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); double resDerivative = differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); return Math.exp(resValue) * resDerivative; }
/** * d_i =0 if delta_i = 0 or delta_{i-1} = 0 */ public void CoincideYvaluesTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 2., 2., 3. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {-1. / 2., 0., 1.5, 1. }, {0., 0., 0., 2. }, {-0.5, 1.5, 0., 2. } }; PiecewiseCubicHermiteSplineInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * Intervals have different length */ public void diffIntervalsTest() { final double[] xValues = new double[] {1., 2., 5., 8. }; final double[] yValues = new double[] {2., 3., 2., 1. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {-2. / 3., 1. / 3., 4. / 3., 2. }, {1. / 27., -2. / 9., 0., 3. }, {0., 0., -1. / 3., 2. } }; PiecewiseCubicHermiteSplineInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ public void recov2ptsMultiTest() { final double[] xValues = new double[] {1., 2. }; final double[][] yValues = new double[][] { {6., 1. }, {2., 5. } }; final int nIntervalsExp = 1; final int orderExp = 4; final int dimExp = 2; final double[][] coefsMatExp = new double[][] { {0., 0., -5., 6. }, {0., 0., 3., 2. } }; NaturalSplineInterpolator interpMatrix = new NaturalSplineInterpolator(); PiecewisePolynomialResult result = interpMatrix.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp * dimExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * Test for the case with boundary value d_0 = ((2 * h_0 + h_1) * delta_0 - h_0 * delta_1)/(h_0 + h_1) */ public void BvCase3Test() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {2., 3., 2., 3. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {0., -1., 2., 2. }, {2., -3., 0., 3. }, {0., 1., 0., 2. } }; PiecewiseCubicHermiteSplineInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ public void recov2ptsTest() { final double[] xValues = new double[] {1., 2. }; final double[] yValues = new double[] {6., 1. }; final int nIntervalsExp = 1; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] {{0., 0., -5., 6. } }; NaturalSplineInterpolator interpMatrix = new NaturalSplineInterpolator(); PiecewisePolynomialResult result = interpMatrix.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * Test for the case with boundary value d_0 = 0 */ public void BvCase1Test() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 2., 10., 11. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {-2. / 9., 11. / 9., 0., 1. }, {-112. / 9., 56. / 3., 16. / 9., 2. }, {-2. / 9., -80. / 144., 16. / 9., 10. } }; PiecewisePolynomialInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefsMatExp[i][j] == 0. ? 1. : Math.abs(coefsMatExp[i][j]); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ 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; } }
/** * */ 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; } }
/** * */ 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; } }
/** * PiecewiseCubicHermiteSplineInterpolator is not modified for positive data */ public void noModificationTest() { final double[] xValues = new double[] {1., 2., 3., 4., 5. }; final double[][] yValues = new double[][] { {0.1, 1., 1., 20., 5. }, {1., 2., 3., 0., 0. } }; PiecewisePolynomialInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); PiecewisePolynomialInterpolator interpPos = new NonnegativityPreservingCubicSplineInterpolator(interp); PiecewisePolynomialResult resultPos = interpPos.interpolate(xValues, yValues); assertEquals(resultPos.getDimensions(), result.getDimensions()); assertEquals(resultPos.getNumberOfIntervals(), result.getNumberOfIntervals()); assertEquals(resultPos.getOrder(), result.getOrder()); for (int i = 1; i < xValues.length - 1; ++i) { for (int j = 0; j < 4; ++j) { final double ref = result.getCoefMatrix().get(i, j) == 0. ? 1. : Math.abs(result.getCoefMatrix().get(i, j)); assertEquals(resultPos.getCoefMatrix().get(i, j), result.getCoefMatrix().get(i, j), ref * EPS); } } }
/** * Linear interpolation for 2 data points */ public void LinearTest() { final double[] xValues = new double[] {1., 2. }; final double[] yValues = new double[] {1., 4. }; final int nIntervalsExp = 1; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] {{0., 0., 3., 1. } }; PiecewiseCubicHermiteSplineInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), dimExp); assertEquals(result.getNumberOfIntervals(), nIntervalsExp); assertEquals(result.getDimensions(), dimExp); for (int i = 0; i < nIntervalsExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = result.getCoefMatrix().get(i, j) == 0. ? 1. : Math.abs(result.getCoefMatrix().get(i, j)); assertEquals(result.getCoefMatrix().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ public void extremumTest() { final double[] xValues = new double[] {1., 2., 3., 4., 5., 6., 7. }; final double[] yValues = new double[] {1., 1., 4., 5., 4., 1., 1. }; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); PiecewisePolynomialInterpolator interp = new ConstrainedCubicSplineInterpolator(); PiecewisePolynomialResult result = interp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getNumberOfIntervals(), 6); assertEquals(result.getOrder(), 4); final int nKeys = 31; double key0 = 1.; for (int i = 1; i < nKeys; ++i) { final double key = 1. + 3. / (nKeys - 1) * i; assertTrue(function.evaluate(result, key).get(0) - function.evaluate(result, key0).get(0) >= 0.); key0 = 1. + 3. / (nKeys - 1) * i; } key0 = 4.; for (int i = 1; i < nKeys; ++i) { final double key = 4. + 3. / (nKeys - 1) * i; assertTrue(function.evaluate(result, key).get(0) - function.evaluate(result, key0).get(0) <= 0.); key0 = 4. + 3. / (nKeys - 1) * i; } }
/** * Finds the first derivatives. * * @param pp the PiecewisePolynomialResult * @param xKeys the key * @return the first derivatives of piecewise polynomial functions at xKeys * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, a row vector of return value corresponds to each piecewise polynomial */ public DoubleMatrix differentiate(PiecewisePolynomialResult pp, double[] xKeys) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 2, "polynomial degree < 1"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 1; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, colCount, pp.getDimensions()); return evaluate(ppDiff, xKeys); }
/** * Finds the second derivatives. * * @param pp the PiecewisePolynomialResult * @param xKeys the key * @return the second derivatives of piecewise polynomial functions at xKeys * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, a row vector of return value corresponds to each piecewise polynomial */ public DoubleMatrix differentiateTwice(PiecewisePolynomialResult pp, double[] xKeys) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 3, "polynomial degree < 2"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 2; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1) * (nCoefs - j - 2)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, nCoefs - 1, pp.getDimensions()); return evaluate(ppDiff, xKeys); }
/** * Finds the second derivatives. * * @param pp the PiecewisePolynomialResult * @param xKey the key * @return the second derivatives of piecewise polynomial functions at xKey * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, an element in the return values corresponds to each piecewise polynomial */ public DoubleArray differentiateTwice(PiecewisePolynomialResult pp, double xKey) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 3, "polynomial degree < 2"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 2; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1) * (nCoefs - j - 2)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, nCoefs - 1, pp.getDimensions()); return evaluate(ppDiff, xKey); }
/** * Finds the first derivatives. * * @param pp the PiecewisePolynomialResult * @param xKey the key * @return the first derivatives of piecewise polynomial functions at xKey * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, an element in the return values corresponds to each piecewise polynomial */ public DoubleArray differentiate(PiecewisePolynomialResult pp, double xKey) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 2, "polynomial degree < 1"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 1; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, colCount, pp.getDimensions()); return evaluate(ppDiff, xKey); }