BicubicSplineInterpolator extends PiecewisePolynomialInterpolator2D { @Override public PiecewisePolynomialResult2D interpolate(final double[] x0Values, final double[] x1Values, final double[][] yValues) { ArgumentChecker.notNull(x0Values, "x0Values"); ArgumentChecker.notNull(x1Values, "x1Values"); ArgumentChecker.notNull(yValues, "yValues"); final int nData0 = x0Values.length; final int nData1 = x1Values.length; DoubleMatrix2D yValuesMatrix = new DoubleMatrix2D(yValues); final PiecewisePolynomialFunction1D func = new PiecewisePolynomialFunction1D(); double[][] diff0 = new double[nData1][nData0]; double[][] diff1 = new double[nData0][nData1]; double[][] cross = new double[nData0][nData1]; final PiecewisePolynomialResult result0 = _method[0].interpolate(x0Values, OG_ALGEBRA.getTranspose(yValuesMatrix).getData()); diff0 = func.differentiate(result0, x0Values).getData(); final PiecewisePolynomialResult result1 = _method[1].interpolate(x1Values, yValuesMatrix.getData()); diff1 = func.differentiate(result1, x1Values).getData(); final int order = 4; for (int i = 0; i < nData0; ++i) { for (int j = 0; j < nData1; ++j) { if (yValues[i][j] == 0.) { if (diff0[j][i] == 0.) { cross[i][j] = diff1[i][j]; } else { if (diff1[i][j] == 0.) { cross[i][j] = diff0[j][i]; } else { cross[i][j] = Math.signum(diff0[j][i] * diff1[i][j]) * Math.sqrt(Math.abs(diff0[j][i] * diff1[i][j])); } } } else { cross[i][j] = diff0[j][i] * diff1[i][j] / yValues[i][j]; } } } DoubleMatrix2D[][] coefMat = new DoubleMatrix2D[nData0 - 1][nData1 - 1]; for (int i = 0; i < nData0 - 1; ++i) { for (int j = 0; j < nData1 - 1; ++j) { double[] diffsVec = new double[16]; for (int l = 0; l < 2; ++l) { for (int m = 0; m < 2; ++m) { diffsVec[l + 2 * m] = yValues[i + l][j + m]; } } for (int l = 0; l < 2; ++l) { for (int m = 0; m < 2; ++m) { diffsVec[4 + l + 2 * m] = diff0[j + m][i + l]; } } for (int l = 0; l < 2; ++l) { for (int m = 0; m < 2; ++m) { diffsVec[8 + l + 2 * m] = diff1[i + l][j + m]; } } for (int l = 0; l < 2; ++l) { for (int m = 0; m < 2; ++m) { diffsVec[12 + l + 2 * m] = cross[i + l][j + m]; } } final DoubleMatrix1D diffs = new DoubleMatrix1D(diffsVec); final double[] ansVec = ((DoubleMatrix1D) OG_ALGEBRA.multiply(new DoubleMatrix2D(s_invMat), diffs)).getData(); double ref = 0.; double[][] coefMatTmp = new double[order][order]; for (int l = 0; l < order; ++l) { for (int m = 0; m < order; ++m) { coefMatTmp[order - l - 1][order - m - 1] = ansVec[l + m * (order)] / Math.pow((x0Values[i + 1] - x0Values[i]), l) / Math.pow((x1Values[j + 1] - x1Values[j]), m); ArgumentChecker.isFalse(Double.isNaN(coefMatTmp[order - l - 1][order - m - 1]), "Too large/small input"); ArgumentChecker.isFalse(Double.isInfinite(coefMatTmp[order - l - 1][order - m - 1]), "Too large/small input"); ref += coefMatTmp[order - l - 1][order - m - 1] * Math.pow((x0Values[i + 1] - x0Values[i]), l) * Math.pow((x1Values[j + 1] - x1Values[j]), m); } } final double bound = Math.max(Math.abs(ref) + Math.abs(yValues[i + 1][j + 1]), 0.1); ArgumentChecker.isTrue(Math.abs(ref - yValues[i + 1][j + 1]) < ERROR * bound, "Input is too large/small or data points are too close"); coefMat[i][j] = new DoubleMatrix2D(coefMatTmp); } } return new PiecewisePolynomialResult2D(new DoubleMatrix1D(x0Values), new DoubleMatrix1D(x1Values), coefMat, new int[] {order, order }); } BicubicSplineInterpolator(final PiecewisePolynomialInterpolator[] method);  BicubicSplineInterpolator(final PiecewisePolynomialInterpolator method); @Override PiecewisePolynomialResult2D interpolate(final double[] x0Values, final double[] x1Values, final double[][] yValues);  }
@Test public void linearTest() { double[] x0Values = new double[] {1., 2., 3., 4. }; double[] x1Values = new double[] {-1., 0., 1., 2., 3. }; final int n0Data = x0Values.length; final int n1Data = x1Values.length; double[][] yValues = new double[n0Data][n1Data]; for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { yValues[i][j] = (x0Values[i] + 2.) * (x1Values[j] + 5.); } } CubicSplineInterpolator method = new CubicSplineInterpolator(); PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(new CubicSplineInterpolator[] {method, method }); PiecewisePolynomialResult2D result = interp.interpolate(x0Values, x1Values, yValues); final int n0IntExp = n0Data - 1; final int n1IntExp = n1Data - 1; final int orderExp = 4; DoubleMatrix2D[][] coefsExp = new DoubleMatrix2D[n0Data - 1][n1Data - 1]; for (int i = 0; i < n0Data - 1; ++i) { for (int j = 0; j < n1Data - 1; ++j) { coefsExp[i][j] = new DoubleMatrix2D(new double[][] { {0., 0., 0., 0., }, {0., 0., 0., 0., }, {0., 0., 1., (5. + x1Values[j]) }, {0., 0., (2. + x0Values[i]), (2. + x0Values[i]) * (5. + x1Values[j]) } }); } } assertEquals(result.getNumberOfIntervals()[0], n0IntExp); assertEquals(result.getNumberOfIntervals()[1], n1IntExp); assertEquals(result.getOrder()[0], orderExp); assertEquals(result.getOrder()[1], orderExp); final int n0Keys = 51; final int n1Keys = 61; double[] x0Keys = new double[n0Keys]; double[] x1Keys = new double[n1Keys]; for (int i = 0; i < n0Keys; ++i) { x0Keys[i] = 0. + 5. * i / (n0Keys - 1); } for (int i = 0; i < n1Keys; ++i) { x1Keys[i] = -2. + 6. * i / (n1Keys - 1); } for (int i = 0; i < n0Data; ++i) { final double ref = Math.abs(x0Values[i]) == 0. ? 1. : Math.abs(x0Values[i]); assertEquals(result.getKnots0().getData()[i], x0Values[i], ref * EPS); assertEquals(result.getKnots2D().get(0).getData()[i], x0Values[i], ref * EPS); } for (int i = 0; i < n1Data; ++i) { final double ref = Math.abs(x1Values[i]) == 0. ? 1. : Math.abs(x1Values[i]); assertEquals(result.getKnots1().getData()[i], x1Values[i], ref * EPS); assertEquals(result.getKnots2D().get(1).getData()[i], x1Values[i], ref * EPS); } for (int i = 0; i < n0Data - 1; ++i) { for (int j = 0; j < n1Data - 1; ++j) { for (int k = 0; k < orderExp; ++k) { for (int l = 0; l < orderExp; ++l) { final double ref = Math.abs(coefsExp[i][j].getData()[k][l]) == 0. ? 1. : Math.abs(coefsExp[i][j].getData()[k][l]); assertEquals(result.getCoefs()[i][j].getData()[k][l], coefsExp[i][j].getData()[k][l], ref * EPS); } } } } double[][] resValues = interp.interpolate(x0Values, x1Values, yValues, x0Keys, x1Keys).getData(); for (int i = 0; i < n0Keys; ++i) { for (int j = 0; j < n1Keys; ++j) { final double expVal = (x0Keys[i] + 2.) * (x1Keys[j] + 5.); final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resValues[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Keys; ++i) { for (int j = 0; j < n1Keys; ++j) { final double expVal = (x0Keys[i] + 2.) * (x1Keys[j] + 5.); final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resValues[i][j], expVal, ref * EPS); } } { final double expVal = (x0Keys[1] + 2.) * (x1Keys[2] + 5.); final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(interp.interpolate(x0Values, x1Values, yValues, x0Keys[1], x1Keys[2]), expVal, ref * EPS); } { final double expVal = (x0Keys[23] + 2.) * (x1Keys[20] + 5.); final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(interp.interpolate(x0Values, x1Values, yValues, x0Keys[23], x1Keys[20]), expVal, ref * EPS); } }
@Test public void quadraticTest() { double[] x0Values = new double[] {1., 2., 3., 4. }; double[] x1Values = new double[] {-1., 0., 1., 2., 3. }; final int n0Data = x0Values.length; final int n1Data = x1Values.length; double[][] yValues = new double[n0Data][n1Data]; for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { yValues[i][j] = (x0Values[i] - 1.5) * (x0Values[i] - 1.5) * (x1Values[j] - 2.) * (x1Values[j] - 2.); } } CubicSplineInterpolator method = new CubicSplineInterpolator(); PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method); PiecewisePolynomialResult2D result = interp.interpolate(x0Values, x1Values, yValues); final int n0IntExp = n0Data - 1; final int n1IntExp = n1Data - 1; final int orderExp = 4; final int n0Keys = 51; final int n1Keys = 61; double[] x0Keys = new double[n0Keys]; double[] x1Keys = new double[n1Keys]; for (int i = 0; i < n0Keys; ++i) { x0Keys[i] = 0. + 5. * i / (n0Keys - 1); } for (int i = 0; i < n1Keys; ++i) { x1Keys[i] = -2. + 6. * i / (n1Keys - 1); } assertEquals(result.getNumberOfIntervals()[0], n0IntExp); assertEquals(result.getNumberOfIntervals()[1], n1IntExp); assertEquals(result.getOrder()[0], orderExp); assertEquals(result.getOrder()[1], orderExp); for (int i = 0; i < n0Data; ++i) { final double ref = Math.abs(x0Values[i]) == 0. ? 1. : Math.abs(x0Values[i]); assertEquals(result.getKnots0().getData()[i], x0Values[i], ref * EPS); assertEquals(result.getKnots2D().get(0).getData()[i], x0Values[i], ref * EPS); } for (int i = 0; i < n1Data; ++i) { final double ref = Math.abs(x1Values[i]) == 0. ? 1. : Math.abs(x1Values[i]); assertEquals(result.getKnots1().getData()[i], x1Values[i], ref * EPS); assertEquals(result.getKnots2D().get(1).getData()[i], x1Values[i], ref * EPS); } for (int i = 0; i < n0Data - 1; ++i) { for (int j = 0; j < n1Data - 1; ++j) { final double ref = Math.abs(yValues[i][j]) == 0. ? 1. : Math.abs(yValues[i][j]); assertEquals(result.getCoefs()[i][j].getData()[orderExp - 1][orderExp - 1], yValues[i][j], ref * EPS); } } double[][] resValues = interp.interpolate(x0Values, x1Values, yValues, x0Values, x1Values).getData(); final PiecewisePolynomialFunction2D func2D = new PiecewisePolynomialFunction2D(); double[][] resDiffX0 = func2D.differentiateX0(result, x0Values, x1Values).getData(); double[][] resDiffX1 = func2D.differentiateX1(result, x0Values, x1Values).getData(); final PiecewisePolynomialFunction1D func1D = new PiecewisePolynomialFunction1D(); double[][] expDiffX0 = func1D.differentiate(method.interpolate(x0Values, OG_ALGEBRA.getTranspose(new DoubleMatrix2D(yValues)).getData()), x0Values).getData(); double[][] expDiffX1 = func1D.differentiate(method.interpolate(x1Values, yValues), x1Values).getData(); for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX1[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX1[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX0[j][i]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX0[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = yValues[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resValues[i][j], expVal, ref * EPS); } } }
@Test public void cubicTest() { double[] x0Values = new double[] {1., 2., 3., 4. }; double[] x1Values = new double[] {-1., 0., 1., 2., 3. }; final int n0Data = x0Values.length; final int n1Data = x1Values.length; double[][] yValues = new double[n0Data][n1Data]; for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { yValues[i][j] = (x0Values[i] - 1.) * (x0Values[i] - 1.) * (x0Values[i] - 1.) * (x1Values[j] + 14. / 13.) * (x1Values[j] + 14. / 13.) * (x1Values[j] + 14. / 13.); } } CubicSplineInterpolator method = new CubicSplineInterpolator(); PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method); PiecewisePolynomialResult2D result = interp.interpolate(x0Values, x1Values, yValues); final int n0IntExp = n0Data - 1; final int n1IntExp = n1Data - 1; final int orderExp = 4; final int n0Keys = 51; final int n1Keys = 61; double[] x0Keys = new double[n0Keys]; double[] x1Keys = new double[n1Keys]; for (int i = 0; i < n0Keys; ++i) { x0Keys[i] = 0. + 5. * i / (n0Keys - 1); } for (int i = 0; i < n1Keys; ++i) { x1Keys[i] = -2. + 6. * i / (n1Keys - 1); } assertEquals(result.getNumberOfIntervals()[0], n0IntExp); assertEquals(result.getNumberOfIntervals()[1], n1IntExp); assertEquals(result.getOrder()[0], orderExp); assertEquals(result.getOrder()[1], orderExp); for (int i = 0; i < n0Data; ++i) { final double ref = Math.abs(x0Values[i]) == 0. ? 1. : Math.abs(x0Values[i]); assertEquals(result.getKnots0().getData()[i], x0Values[i], ref * EPS); assertEquals(result.getKnots2D().get(0).getData()[i], x0Values[i], ref * EPS); } for (int i = 0; i < n1Data; ++i) { final double ref = Math.abs(x1Values[i]) == 0. ? 1. : Math.abs(x1Values[i]); assertEquals(result.getKnots1().getData()[i], x1Values[i], ref * EPS); assertEquals(result.getKnots2D().get(1).getData()[i], x1Values[i], ref * EPS); } for (int i = 0; i < n0Data - 1; ++i) { for (int j = 0; j < n1Data - 1; ++j) { final double ref = Math.abs(yValues[i][j]) == 0. ? 1. : Math.abs(yValues[i][j]); assertEquals(result.getCoefs()[i][j].getData()[orderExp - 1][orderExp - 1], yValues[i][j], ref * EPS); } } double[][] resValues = interp.interpolate(x0Values, x1Values, yValues, x0Values, x1Values).getData(); final PiecewisePolynomialFunction2D func2D = new PiecewisePolynomialFunction2D(); double[][] resDiffX0 = func2D.differentiateX0(result, x0Values, x1Values).getData(); double[][] resDiffX1 = func2D.differentiateX1(result, x0Values, x1Values).getData(); final PiecewisePolynomialFunction1D func1D = new PiecewisePolynomialFunction1D(); double[][] expDiffX0 = func1D.differentiate(method.interpolate(x0Values, OG_ALGEBRA.getTranspose(new DoubleMatrix2D(yValues)).getData()), x0Values).getData(); double[][] expDiffX1 = func1D.differentiate(method.interpolate(x1Values, yValues), x1Values).getData(); for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX1[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX1[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX0[j][i]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX0[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = yValues[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resValues[i][j], expVal, ref * EPS); } } }
@Test public void crossDerivativeTest() { double[] x0Values = new double[] {1., 2., 3., 4. }; double[] x1Values = new double[] {-1., 0., 1., 2., 3. }; final int n0Data = x0Values.length; final int n1Data = x1Values.length; double[][] yValues = new double[][] { {1.0, -1.0, 0.0, 1.0, 0.0, }, {1.0, -1.0, 0.0, 1.0, -2.0 }, {1.0, -2.0, 0.0, -2.0, -2.0 }, {-1.0, -1.0, -2.0, -2.0, -1.0 } }; NaturalSplineInterpolator method = new NaturalSplineInterpolator(); PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method); PiecewisePolynomialResult2D result = interp.interpolate(x0Values, x1Values, yValues); final int n0IntExp = n0Data - 1; final int n1IntExp = n1Data - 1; final int orderExp = 4; final int n0Keys = 51; final int n1Keys = 61; double[] x0Keys = new double[n0Keys]; double[] x1Keys = new double[n1Keys]; for (int i = 0; i < n0Keys; ++i) { x0Keys[i] = 0. + 5. * i / (n0Keys - 1); } for (int i = 0; i < n1Keys; ++i) { x1Keys[i] = -2. + 6. * i / (n1Keys - 1); } assertEquals(result.getNumberOfIntervals()[0], n0IntExp); assertEquals(result.getNumberOfIntervals()[1], n1IntExp); assertEquals(result.getOrder()[0], orderExp); assertEquals(result.getOrder()[1], orderExp); for (int i = 0; i < n0Data; ++i) { final double ref = Math.abs(x0Values[i]) == 0. ? 1. : Math.abs(x0Values[i]); assertEquals(result.getKnots0().getData()[i], x0Values[i], ref * EPS); assertEquals(result.getKnots2D().get(0).getData()[i], x0Values[i], ref * EPS); } for (int i = 0; i < n1Data; ++i) { final double ref = Math.abs(x1Values[i]) == 0. ? 1. : Math.abs(x1Values[i]); assertEquals(result.getKnots1().getData()[i], x1Values[i], ref * EPS); assertEquals(result.getKnots2D().get(1).getData()[i], x1Values[i], ref * EPS); } for (int i = 0; i < n0Data - 1; ++i) { for (int j = 0; j < n1Data - 1; ++j) { final double ref = Math.abs(yValues[i][j]) == 0. ? 1. : Math.abs(yValues[i][j]); assertEquals(result.getCoefs()[i][j].getData()[orderExp - 1][orderExp - 1], yValues[i][j], ref * EPS); } } double[][] resValues = interp.interpolate(x0Values, x1Values, yValues, x0Values, x1Values).getData(); final PiecewisePolynomialFunction2D func2D = new PiecewisePolynomialFunction2D(); double[][] resDiffX0 = func2D.differentiateX0(result, x0Values, x1Values).getData(); double[][] resDiffX1 = func2D.differentiateX1(result, x0Values, x1Values).getData(); final PiecewisePolynomialFunction1D func1D = new PiecewisePolynomialFunction1D(); double[][] expDiffX0 = func1D.differentiate(method.interpolate(x0Values, OG_ALGEBRA.getTranspose(new DoubleMatrix2D(yValues)).getData()), x0Values).getData(); double[][] expDiffX1 = func1D.differentiate(method.interpolate(x1Values, yValues), x1Values).getData(); for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX1[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX1[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = expDiffX0[j][i]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resDiffX0[i][j], expVal, ref * EPS); } } for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { final double expVal = yValues[i][j]; final double ref = Math.abs(expVal) == 0. ? 1. : Math.abs(expVal); assertEquals(resValues[i][j], expVal, ref * EPS); } } }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullx0Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; x0Values = null; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullx1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; x1Values = null; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullyTest() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; yValues = null; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongLengthx0Test() { double[] x0Values = new double[] {0., 1., 2. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void wrongLengthx1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2., 3. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortx0Test() { double[] x0Values = new double[] {1. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] {{1., 2., 4. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shortx1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0. }; double[][] yValues = new double[][] { {1. }, {-1. }, {2. }, {5. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infX0Test() { double[] x0Values = new double[] {0., 1., 2., INF }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanX0Test() { double[] x0Values = new double[] {0., 1., 2., Double.NaN }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infX1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., INF }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanX1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., Double.NaN }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infYTest() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., INF }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nanYTest() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., Double.NaN } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void coincideX0Test() { double[] x0Values = new double[] {0., 1., 1., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void coincideX1Test() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 1. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void notTwoMethodsTest() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1., 2., 4. }, {-1., 2., -4. }, {2., 3., 4. }, {5., 2., 1. } }; BicubicSplineInterpolator interp = new BicubicSplineInterpolator(new PiecewisePolynomialInterpolator[] {new CubicSplineInterpolator() }); interp.interpolate(x0Values, x1Values, yValues); }
@Test(expectedExceptions = IllegalArgumentException.class) public void notKnotRevoveredTests() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1.e-20, 3.e-120, 5.e120 }, {2.e-20, 3.e-120, 4.e-120 }, {1.e-20, 1.e-120, 1.e-20 }, {4.e-120, 3.e-20, 2.e-20 } }; BicubicSplineInterpolator intp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); intp.interpolate(x0Values, x1Values, yValues); }
@Test (enabled = false) public void printTest() { double[] x0Values = new double[] {0., 1., 2., 3. }; double[] x1Values = new double[] {0., 1., 2. }; double[][] yValues = new double[][] { {1.e-20, 3.e-120, 5.e-20 }, {2.e-20, 3.e-120, 4.e-120 }, {1.e-20, 1.e-120, 1.e-20 }, {4.e-120, 3.e-20, 2.e-20 } }; BicubicSplineInterpolator intp = new BicubicSplineInterpolator(new CubicSplineInterpolator()); PiecewisePolynomialResult2D result2D = intp.interpolate(x0Values, x1Values, yValues); System.out.println(result2D.getCoefs()[0][0]); System.out.println(result2D.getCoefs()[2][1]); final int n0Keys = 31; final int n1Keys = 21; double[] x0Keys = new double[n0Keys]; double[] x1Keys = new double[n1Keys]; for (int i = 0; i < n0Keys; ++i) { x0Keys[i] = 0. + 3. * i / (n0Keys - 1); } for (int i = 0; i < n1Keys; ++i) { x1Keys[i] = 0. + 2. * i / (n1Keys - 1); } PiecewisePolynomialFunction2D func = new PiecewisePolynomialFunction2D(); final double[][] values = func.evaluate(result2D, x0Keys, x1Keys).getData(); for (int i = 0; i < n0Keys; ++i) { System.out.print("\t" + x0Keys[i]); } System.out.print("\n"); for (int j = 0; j < n1Keys; ++j) { System.out.print(x1Keys[j]); for (int i = 0; i < n0Keys; ++i) { System.out.print("\t" + values[i][j]); } System.out.print("\n"); } System.out.print("\n"); for (int i = 0; i < x0Values.length; ++i) { System.out.print("\t" + x0Values[i]); } System.out.print("\n"); for (int j = 0; j < x1Values.length; ++j) { System.out.print(x1Values[j]); for (int i = 0; i < x0Values.length; ++i) { System.out.print("\t" + yValues[i][j]); } System.out.print("\n"); } System.out.print("\n"); }
@Test (enabled = false) public void randomTest() { double[] x0Values = new double[] {1., 2., 3., 4. }; double[] x1Values = new double[] {-1., 0., 1., 2., 3. }; final int n0Data = x0Values.length; final int n1Data = x1Values.length; double[][] yValues = new double[n0Data][n1Data]; final Random randObj = new Random(); int k = 0; while (k < 100000) { for (int i = 0; i < n0Data; ++i) { for (int j = 0; j < n1Data; ++j) { yValues[i][j] = randObj.nextInt(4) - 2.; } } System.out.println(new DoubleMatrix2D(yValues)); NaturalSplineInterpolator method = new NaturalSplineInterpolator(); PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method); interp.interpolate(x0Values, x1Values, yValues); ++k; } }