/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateMultiTest() { 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. } }); double[][] xKeys = new double[][] { {-2, 1, INF, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateTest() { 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. } }); double[][] xKeys = new double[][] { {INF, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateTest() { 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. } }); double[][] xKeys = new double[][] { {Double.NaN, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateMatrixTest() { 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. } }); double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., Double.NaN } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateMultiTest() { final 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. } }); final double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
/** * Error tests below */ @Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateTest() { final 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. } }); final double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateMatrixTest() { final 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. } }); final double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateMatrixTest() { 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. } }); double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., INF } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateMultiTest() { 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. } }); double[][] xKeys = new double[][] { {-2, 1, Double.NaN, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void nullxEvaluateTest() { 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. } }); double[] xKeys = new double[] {-2, 1, 2, 2.5 }; final int dim = 2; final int nCoefs = 4; xKeys = null; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
/** * */ @Test(expectedExceptions = IllegalArgumentException.class) public void nullxEvaluateMatrixTest() { 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. } }); double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 2; final int nCoefs = 4; xKeys = null; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(X_VALUES, coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
public void baseInterpolationTest() { final int nExamples = Y.length; final int n = XX.length; for (int example = 0; example < nExamples; example++) { PiecewisePolynomialResult pp = PCHIP.interpolate(X, Y[example]); for (int i = 0; i < n; i++) { final double y = PPVAL.evaluate(pp, XX[i]).get(0); assertEquals(YY[example][i], y, 1e-14); } } }
/** * 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; } }
private DoubleArray[] fdSenseCal(double[] xValues, double[] yValues, double[] xx) { int nData = yValues.length; double eps = 1e-6; double scale = 0.5 / eps; DoubleArray[] res = new DoubleArray[nData]; double[] temp = new double[nData]; PiecewisePolynomialResult pp; for (int i = 0; i < nData; i++) { System.arraycopy(yValues, 0, temp, 0, nData); temp[i] += eps; pp = PCHIP.interpolate(xValues, temp); DoubleArray yUp = PPVAL.evaluate(pp, xx).row(0); temp[i] -= 2 * eps; pp = PCHIP.interpolate(xValues, temp); DoubleArray yDown = PPVAL.evaluate(pp, xx).row(0); res[i] = (DoubleArray) MA.scale(MA.subtract(yUp, yDown), scale); } return res; }
/** * */ 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; } }
/** * 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 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 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); }