ShapePreservingCubicSplineInterpolator extends PiecewisePolynomialInterpolator { @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues) { ArgumentChecker.notNull(xValues, "xValues"); ArgumentChecker.notNull(yValues, "yValues"); ArgumentChecker.isTrue(xValues.length == yValues.length, "xValues length = yValues length"); ArgumentChecker.isTrue(xValues.length > 2, "Data points should be more than 1"); final int nDataPts = xValues.length; for (int i = 0; i < nDataPts; ++i) { ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity"); ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity"); } for (int i = 0; i < nDataPts; ++i) { for (int j = i + 1; j < nDataPts; ++j) { ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct"); } } final double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts); final double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts); ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt); final double[] intervals = intervalsCalculator(xValuesSrt); final double[] slopes = slopesCalculator(yValuesSrt, intervals); final double[] beta = betaCalculator(slopes); double[] first = firstDiffFinder(intervals, slopes); double[] rValues = rValuesCalculator(slopes, first); boolean correctSign = false; int it = 0; while (correctSign == false) { correctSign = signChecker(beta, rValues); if (correctSign == false) { first = firstDiffSweep(intervals, slopes, beta, first); rValues = rValuesCalculator(slopes, first); } ++it; if (it > 10) { throw new IllegalArgumentException("Spline is not found!"); } } final double[] second = secondDiffFinder(intervals, beta, rValues); final double[] tau = tauFinder(intervals, slopes, beta, first, second); final double[] knots = knotsProvider(xValuesSrt, intervals, tau); final double[][] coefMatrix = solve(yValuesSrt, intervals, slopes, first, second, tau); for (int i = 0; i < coefMatrix.length; ++i) { double ref = 0.; final double interval = knots[i + 1] - knots[i]; for (int j = 0; j < 4; ++j) { ref += coefMatrix[i][j] * Math.pow(interval, 3 - j); ArgumentChecker.isFalse(Double.isNaN(coefMatrix[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatrix[i][j]), "Too large input"); } final double yVal = i == coefMatrix.length - 1 ? yValues[nDataPts - 1] : coefMatrix[i + 1][3]; final double bound = Math.max(Math.abs(ref) + Math.abs(yVal), 1.e-1); ArgumentChecker.isTrue(Math.abs(ref - yVal) < ERROR * bound, "Input is too large/small or data points are too close"); } return new PiecewisePolynomialResult(new DoubleMatrix1D(knots), new DoubleMatrix2D(coefMatrix), 4, 1); }  @Override PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues); @Override PiecewisePolynomialResult interpolate(final double[] xValues, final double[][] yValuesMatrix); @Override PiecewisePolynomialResultsWithSensitivity interpolateWithSensitivity(final double[] xValues, final double[] yValues);  }
@Test public void linearTest() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; final double[] yValues = new double[] {2., 4., 6., 8., 10., 12. }; final double[][] coefExp = new double[][] { {0., 0., 2., 2., }, {0., 0., 2., 8. / 3. }, {0., 0., 2., 10. / 3. }, {0., 0., 2., 4. }, {0., 0., 2., 14. / 3. }, {0., 0., 2., 16. / 3. }, {0., 0., 2., 6. }, {0., 0., 2., 20. / 3. }, {0., 0., 2., 22. / 3. }, {0., 0., 2., 8. }, {0., 0., 2., 26. / 3. }, {0., 0., 2., 28. / 3. }, {0., 0., 2., 10. }, {0., 0., 2., 32. / 3. }, {0., 0., 2., 34. / 3. } }; final double[] knotsExp = new double[] {0., 1. / 3., 2. / 3., 1., 4. / 3., 5. / 3., 2., 7. / 3., 8. / 3., 3., 10. / 3., 11. / 3., 4., 13. / 3., 14. / 3., 5. }; final int orderExp = 4; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), orderExp); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 0; i < intLengthExp; ++i) { for (int j = 0; j < orderExp; ++j) { final double ref = coefExp[i][j] == 0. ? 1. : Math.abs(coefExp[i][j]); assertEquals(result.getCoefMatrix().getData()[i][j], coefExp[i][j], ref * EPS); } } for (int j = 0; j < intLengthExp + 1; ++j) { final double ref = knotsExp[j] == 0. ? 1. : Math.abs(knotsExp[j]); assertEquals(result.getKnots().getData()[j], knotsExp[j], ref * EPS); } double[] xKeys = new double[nData - 1]; double[] valuesExp = new double[nData - 1]; for (int i = 0; i < nData - 1; ++i) { xKeys[i] = 0.5 * (xValues[i + 1] + xValues[i]); valuesExp[i] = 0.5 * (yValues[i + 1] + yValues[i]); } double[] values = intp.interpolate(xValues, yValues, xKeys).getData(); for (int i = 0; i < nData - 1; ++i) { final double ref = valuesExp[i] == 0. ? 1. : Math.abs(valuesExp[i]); assertEquals(values[i], valuesExp[i], ref * EPS); } }
@Test public void correctedExtraKnotsTest() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; final double[] yValues = new double[] {5.117767385717404, 6.448193771622548, 2.2821942943281783, 6.26865829460428, 8.66539745601466, 0.4684081305693999 }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); { final double ref = Math.abs(1. + 1. / 3. * Math.pow(0.8, 117)); assertEquals(result.getKnots().getData()[4], 1. + 1. / 3. * Math.pow(0.8, 117), EPS * ref); } }
@Test public void zeroBeta1Test() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5., 6., 7., 7.5, 8., 8.3, 8.4 }; final double[] yValues = new double[] {1., 2., 3., 4., 5., 1.41, 1.43324, 1.43, 1.4333, 1.42, 1.420006, 1.42001 }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 3; i < intLengthExp - 1; i += 3) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } int[] i = new int[] {1, 2 }; for (int ii : i) { assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); } }
@Test public void zeroBeta2Test() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5., 6., 7., 7.5, 8., 8.3, 8.4 }; final double[] yValues = new double[] {1., 2., 3., 4., 5., 6.41, 6.43324, 6.44, 6.4333, 6.42, 6.420006, 6.42001 }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 3; i < intLengthExp - 1; i += 3) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } int[] i = new int[] {1, 2, 3, 4 }; for (int ii : i) { assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); } }
@Test public void zeroBeta3Test() { final double[] xValues = new double[] {3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20. }; final double[] yValues = new double[] {1.42, 1.42, 1.43, 1.42, 1., 0., -1., -2., -3., 2.42, 2.42, 2.43, 2.42, 2., 0., -1., -2., -3. }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); final double[] beta = new double[] {1.0, 1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0 }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 3; i < intLengthExp - 1; i += 3) { if (beta[i / 3] != 0) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } } int[] i = new int[] {5, 6, 13, 14 }; for (int ii : i) { assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); assertEquals(Math.signum(result.getCoefMatrix().getData()[3 * ii + 1][2]), Math.signum(result.getCoefMatrix().getData()[3 * ii + 2][2]), 1); } }
@Test public void zeroBeta4Test() { final double[] xValues = new double[] {0., 1., 1.1, 1.3, 3., 4., 4.1, 4.6, 4.8, 5., 6.2, 7.9 }; final double[] yValues = new double[] {0.9008311501090895, 0.1719634598183083, 0.1010309520578011, 0.10504721080659263, 0.17540666458174503, 0.2093234479937922, 0.8881947659143393, 0.34762162959831977, 0.3329817591903894, 0.5182377743435055, 0.183164167705688, 0.047878939120233 }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 1; i < 6; i += 3) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } }
@Test public void zeroBeta5Test() { final double[] xValues = new double[] {3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 12.1 }; final double[] yValues = new double[] {1.42, 1.421, 1.422, 1.421, 1., 0., 6.41, 6.43324, 6.44, 6.4333, 6.42 }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 3; i < intLengthExp - 1; i += 3) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } }
@Test public void zeroBeta6Test() { final double[] xValues = new double[] {3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 12.1 }; final double[] yValues = new double[] {-1.42, -1.421, -1.422, -1.421, -1., 0., -6.41, -6.43324, -6.44, -6.4333, -6.42 }; final int nData = xValues.length; final int intLengthExp = 3 * (nData - 1); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); PiecewisePolynomialResult result = intp.interpolate(xValues, yValues); assertEquals(result.getDimensions(), 1); assertEquals(result.getOrder(), 4); assertEquals(result.getNumberOfIntervals(), intLengthExp); for (int i = 3; i < intLengthExp - 1; i += 3) { assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i - 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i - 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), 1); assertEquals(Math.signum(Math.abs(result.getCoefMatrix().getData()[i + 1][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i + 1][1]), Math.signum(Math.abs(result.getCoefMatrix().getData()[i][1]) < 1.e-15 ? 0. : result.getCoefMatrix().getData()[i][1]), 1); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void noSplineFoundTest() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; final double[] yValues = new double[] {6.826093986047667, 2.0898823357582286, 0.9283831909337348, 0.7977927420474962, 3.5944356762557206, 6.620380982226143 }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void noSplineFoundTauTest() { final double[] xValues = new double[] {0., 1., 1.1, 1.3, 3., 4., 4.1, 4.6, 4.8, 5., 6.2, 7.9 }; final double[] yValues = new double[] {0.026006379073901575, 0.10550492102081444, 0.6794576424972392, 0.921863379969563, 0.28648620217835274, 0.7199803557365164, 0.22626060774706713, 0.22624762260968234, 0.579582487626177, 0.22299042180552542, 0.09527581704572874, 0.021179942182509737 }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void noSplineFoundEtaTest() { final double[] xValues = new double[] {0., 1., 1.1, 1.3, 3., 4., 4.1, 4.6, 4.8, 5., 6.2, 7.9 }; final double[] yValues = new double[] {0.1876319060272671, 0.9614212044716063, 0.6816857638943187, 0.37675837990239536, 0.5844983876486328, 0.6434299586348089, 0.41471703104897784, 0.4148555495946201, 0.40730984287499683, 0.1149629409860089, 0.2922931033679792, 0.18170530592245404 }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void multiDimTest() { final double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; final double[][] yValues = new double[][] { {0., 1., 2., 3., 4., 5. }, {1., 2., 3., 4., 5., 6. } }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullXvaluesTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; xValues = null; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullYvaluesTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; yValues = null; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanXvaluesTest() { double[] xValues = new double[] {0., 1., 2., Double.NaN, 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanYvaluesTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., Double.NaN }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infXvaluesTest() { double[] xValues = new double[] {0., 1., 2., INF, 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infYvaluesTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., INF }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void lengthMismatchTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void lengthShortTest() { double[] xValues = new double[] {5., 6. }; double[] yValues = new double[] {4., 2. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void sameXvaluesTest() { double[] xValues = new double[] {0., 1., 1. }; double[] yValues = new double[] {0., 1., 2. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullKeyTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] xKeys = new double[] {0., 1., 2., 3., 4., 5. }; xKeys = null; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanKeyTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] xKeys = new double[] {0., 1., 2., 3., 4., Double.NaN }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infKeyTest() { double[] xValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] yValues = new double[] {0., 1., 2., 3., 4., 5. }; double[] xKeys = new double[] {0., 1., 2., 3., INF, 5. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void notReconnectedTest() { double[] xValues = new double[] {1., 2., 2.0000000000001, 4. }; double[] yValues = new double[] {2., 3.e10, 4.e-5, 5.e11 }; PiecewisePolynomialInterpolator interpPos = new ShapePreservingCubicSplineInterpolator(); interpPos.interpolate(xValues, yValues); }
@Test (enabled = false) public void printTest() { final double[] xValues = new double[] {0., 1., 1.1, 1.3, 3., 4., 4.1, 4.6, 4.8, 5., 6.2, 7.9 }; final int nData = xValues.length; double[] yValues = new double[nData]; Random randObj = new Random(); int ctr = 0; int ctr2 = 0; int n = 0; int k = 0; while (n < 1000) { try { k = 0; while (k < 10000) { for (int i = 0; i < nData; ++i) { yValues[i] = randObj.nextDouble(); } System.out.println(new DoubleMatrix1D(yValues)); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); System.out.println("\n"); ++k; } } catch (IllegalArgumentException e) { if (e.getMessage() == "Spline is not found" | e.getMessage() == "Local monotonicity can not be preserved") { ctr2 += (k + 1); ++ctr; } } ++n; } System.out.println(ctr + " / " + ctr2); }
@Test (enabled = false) public void print2Test() { final double[] xValues = new double[] {0., 1., 1.1, 1.3, 3., 4., 4.1, 4.6, 4.8, 5., 6.2, 7.9 }; final double[] yValues = new double[] {0.46608273840991754, 0.8312159840478093, 0.9194772023433536, 0.6757561802041987, 0.6796484240935459, 0.30926871248752386, 0.10127356457226167, 0.37084482298919885, 0.4707389784307331, 0.45361468489333356, 0.9307438159899785, 0.3902599731656107 }; System.out.println(new DoubleMatrix1D(yValues)); ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); System.out.println("\n"); }
@Test (enabled = false) public void print3Test() { final double[] xValues = new double[] {0., 0.1, 2., 3., 4., 9., 20., 30. }; final double[] yValues = new double[] {0., 6., 5., 5., 5., 6.5, 6., 6. }; ShapePreservingCubicSplineInterpolator intp = new ShapePreservingCubicSplineInterpolator(); intp.interpolate(xValues, yValues); final int nPts = 301; double[] keys = new double[nPts]; for (int i = 0; i < nPts; ++i) { keys[i] = 0.01 + 30. / (nPts - 1) * i; } double[] values = intp.interpolate(xValues, yValues, keys).getData(); System.out.println(intp.interpolate(xValues, yValues).getCoefMatrix()); System.out.println(intp.interpolate(xValues, yValues).getKnots()); for (int i = 0; i < nPts; ++i) { System.out.println(keys[i] + "\t" + values[i]); } }