NaturalSplineInterpolator 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 > 1, "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], "Data should be distinct"); } } double[] xValuesSrt = new double[nDataPts]; double[] yValuesSrt = new double[nDataPts]; xValuesSrt = Arrays.copyOf(xValues, nDataPts); yValuesSrt = Arrays.copyOf(yValues, nDataPts); ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt); final DoubleMatrix2D coefMatrix = this._solver.solve(xValuesSrt, yValuesSrt); final int nCoefs = coefMatrix.getNumberOfColumns(); final int nInts = this._solver.getKnotsMat1D(xValuesSrt).getNumberOfElements() - 1; for (int i = 0; i < nInts; ++i) { for (int j = 0; j < nCoefs; ++j) { ArgumentChecker.isFalse(Double.isNaN(coefMatrix.getData()[i][j]), "Too large input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatrix.getData()[i][j]), "Too large input"); } } return new PiecewisePolynomialResult(this._solver.getKnotsMat1D(xValuesSrt), coefMatrix, nCoefs, 1); } NaturalSplineInterpolator();  NaturalSplineInterpolator(final CubicSplineSolver inherit); @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 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().getData()[i][j], coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().getData()[j], xValues[j]); } }
@Test public void recov4ptsTest() { final double[] xValues = new double[] {1., 2., 3., 4 }; final double[] yValues = new double[] {6., 25. / 6., 10. / 3., 4. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {1. / 6., 0., -2., 6. }, {1. / 6., 1. / 2., -3. / 2., 25. / 6. }, {-1. / 3., 1., 0., 10. / 3. } }; PiecewisePolynomialInterpolator 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().getData()[i][j], coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().getData()[j], xValues[j]); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullXvaluesTest() { double[] xValues = new double[4]; double[] yValues = new double[] {1., 2., 3., 4. }; xValues = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullYvaluesTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[4]; yValues = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongDatalengthTest() { double[] xValues = new double[] {1., 2., 3. }; double[] yValues = new double[] {1., 2., 3., 4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortDataLengthTest() { double[] xValues = new double[] {1. }; double[] yValues = new double[] {4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxValuesTest() { double[] xValues = new double[] {1., 2., Double.NaN, 4. }; double[] yValues = new double[] {1., 2., 3., 4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNyValuesTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 2., Double.NaN, 4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfxValuesTest() { double[] xValues = new double[] {1., 2., 3., INF }; double[] yValues = new double[] {1., 2., 3., 4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfyValuesTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[] yValues = new double[] {1., 2., 3., INF }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void coincideXvaluesTest() { double[] xValues = new double[] {1., 2., 3., 3. }; double[] yValues = new double[] {1., 2., 3., 4. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test 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().getData()[i][j], coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().getData()[j], xValues[j]); } }
@Test public void recov4ptsMultiTest() { final double[] xValues = new double[] {1., 2., 3., 4 }; final double[][] yValues = new double[][] { {6., 25. / 6., 10. / 3., 4. }, {6., 1., 0., 0. } }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 2; final double[][] coefsMatExp = new double[][] { {1. / 6., 0., -2., 6. }, {1., 0., -6., 6. }, {1. / 6., 1. / 2., -3. / 2., 25. / 6. }, {-1., 3., -3., 1. }, {-1. / 3., 1., 0., 10. / 3. }, {0., 0., 0., 0 } }; 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().getData()[i][j], coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().getData()[j], xValues[j]); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullXvaluesMultiTest() { double[] xValues = new double[4]; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {1., 5., 3., 4. } }; xValues = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullYvaluesMultiTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[][] yValues = new double[2][4]; yValues = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongDatalengthMultiTest() { double[] xValues = new double[] {1., 2., 3. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {2., 2., 3., 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortDataLengthMultiTest() { double[] xValues = new double[] {1. }; double[][] yValues = new double[][] { {4. }, {1. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxValuesMultiTest() { double[] xValues = new double[] {1., 2., Double.NaN, 4. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {2., 2., 3., 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNyValuesMultiTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {1., 2., Double.NaN, 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfxValuesMultiTest() { double[] xValues = new double[] {1., 2., 3., INF }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {2., 2., 3., 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfyValuesMultiTest() { double[] xValues = new double[] {1., 2., 3., 4. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {1., 2., 3., INF } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void coincideXvaluesMultiTest() { double[] xValues = new double[] {1., 2., 3., 3. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {2., 2., 3., 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test public void InterpolantsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[][] yValues = new double[][] { {6., 25. / 6., 10. / 3., 4. }, {6., 1., 0., 0. } }; final double[][] xKey = new double[][] { {-1., 0.5, 1.5 }, {2.5, 3.5, 4.5 } }; final double[][][] resultValuesExpected = new double[][][] { { {26. / 3., 57. / 16. }, {10., 1. / 8. } }, { {335. / 48., 85. / 24. }, {71. / 8., 0. } }, { {241. / 48., 107. / 24. }, {25. / 8., 0. } } }; final int yDim = yValues.length; final int keyLength = xKey[0].length; final int keyDim = xKey.length; PiecewisePolynomialInterpolator interp = new NaturalSplineInterpolator(); double value = interp.interpolate(xValues, yValues[0], xKey[0][0]); { final double ref = resultValuesExpected[0][0][0] == 0. ? 1. : Math.abs(resultValuesExpected[0][0][0]); assertEquals(value, resultValuesExpected[0][0][0], ref * EPS); } DoubleMatrix1D valuesVec1 = interp.interpolate(xValues, yValues, xKey[0][0]); for (int i = 0; i < yDim; ++i) { final double ref = resultValuesExpected[0][i][0] == 0. ? 1. : Math.abs(resultValuesExpected[0][i][0]); assertEquals(valuesVec1.getData()[i], resultValuesExpected[0][i][0], ref * EPS); } DoubleMatrix1D valuesVec2 = interp.interpolate(xValues, yValues[0], xKey[0]); for (int k = 0; k < keyLength; ++k) { final double ref = resultValuesExpected[k][0][0] == 0. ? 1. : Math.abs(resultValuesExpected[k][0][0]); assertEquals(valuesVec2.getData()[k], resultValuesExpected[k][0][0], ref * EPS); } DoubleMatrix2D valuesMat1 = interp.interpolate(xValues, yValues[0], xKey); for (int j = 0; j < keyDim; ++j) { for (int k = 0; k < keyLength; ++k) { final double ref = resultValuesExpected[k][0][j] == 0. ? 1. : Math.abs(resultValuesExpected[k][0][j]); assertEquals(valuesMat1.getData()[j][k], resultValuesExpected[k][0][j], ref * EPS); } } DoubleMatrix2D valuesMat2 = interp.interpolate(xValues, yValues, xKey[0]); for (int i = 0; i < yDim; ++i) { for (int k = 0; k < keyLength; ++k) { final double ref = resultValuesExpected[k][i][0] == 0. ? 1. : Math.abs(resultValuesExpected[k][i][0]); assertEquals(valuesMat2.getData()[i][k], resultValuesExpected[k][i][0], ref * EPS); } } DoubleMatrix2D[] valuesMat3 = interp.interpolate(xValues, yValues, xKey); for (int i = 0; i < yDim; ++i) { for (int j = 0; j < keyDim; ++j) { for (int k = 0; k < keyLength; ++k) { final double ref = resultValuesExpected[k][i][j] == 0. ? 1. : Math.abs(resultValuesExpected[k][i][j]); assertEquals(valuesMat3[k].getData()[i][j], resultValuesExpected[k][i][j], ref * EPS); } } } }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfiniteOutputTest() { double[] xValues = new double[] {1.e-308, 2.e-308 }; double[] yValues = new double[] {1., 1.e308 }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfiniteOutputMultiTest() { double[] xValues = new double[] {1.e-308, 2.e-308 }; double[][] yValues = new double[][] { {1., 1.e308 }, {2., 1. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NanOutputTest() { double[] xValues = new double[] {1., 2.e-308, 3.e-308, 4. }; double[] yValues = new double[] {1., 2., 1.e308, 3. }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NanOutputMultiTest() { double[] xValues = new double[] {1., 2.e-308, 3.e-308, 4. }; double[][] yValues = new double[][] { {1., 2., 3., 4. }, {2., 2., 3., 4. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void LargeInterpolantsTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[][] yValues = new double[][] { {2., 10., 2., 5. }, {1., 2., 10., 11. } }; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues[0], 1.e308); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullKeyTest() { double[] xValues = new double[] {1., 2., 3. }; double[] yValues = new double[] {1., 3., 4. }; double[] xKey = new double[3]; xKey = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues, xKey); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullKeyMultiTest() { double[] xValues = new double[] {1., 2., 3. }; double[][] yValues = new double[][] { {1., 3., 4. }, {2., 3., 1. } }; double[] xKey = new double[3]; xKey = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues, xKey); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullKeyMatrixTest() { double[] xValues = new double[] {1., 2., 3. }; double[] yValues = new double[] {1., 3., 4. }; double[][] xKey = new double[3][3]; xKey = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues, xKey); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullKeyMatrixMultiTest() { double[] xValues = new double[] {1., 2., 3. }; double[][] yValues = new double[][] { {1., 3., 4. }, {2., 3., 1. } }; double[][] xKey = new double[3][4]; xKey = null; NaturalSplineInterpolator interp = new NaturalSplineInterpolator(); interp.interpolate(xValues, yValues, xKey); }
@Test(enabled = false) public void printTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {6., 25. / 6., 10. / 3., 4. }; NaturalSplineInterpolator interpMatrix = new NaturalSplineInterpolator(); PiecewisePolynomialResult resultMatrix = interpMatrix.interpolate(xValues, yValues); System.out.println(resultMatrix.getCoefMatrix()); System.out.println(resultMatrix.getKnots()); System.out.println(resultMatrix.getNumberOfIntervals()); System.out.println(resultMatrix.getOrder()); System.out.println(resultMatrix.getDimensions()); }