PiecewisePolynomialFunction1D { public DoubleMatrix1D evaluate(final PiecewisePolynomialResult pp, final double xKey) { ArgumentChecker.notNull(pp, "pp"); ArgumentChecker.isFalse(Double.isNaN(xKey), "xKey containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xKey), "xKey containing Infinity"); final double[] knots = pp.getKnots().getData(); final int nKnots = knots.length; final DoubleMatrix2D coefMatrix = pp.getCoefMatrix(); final int dim = pp.getDimensions(); double[] res = new double[dim]; int indicator = FunctionUtils.getLowerBoundIndex(knots, xKey); if (indicator == nKnots - 1) { indicator--; } for (int j = 0; j < dim; ++j) { final double[] coefs = coefMatrix.getRowVector(dim * indicator + j).getData(); res[j] = getValue(coefs, xKey, knots[indicator]); ArgumentChecker.isFalse(Double.isInfinite(res[j]), "Too large input"); ArgumentChecker.isFalse(Double.isNaN(res[j]), "Too large input"); } return new DoubleMatrix1D(res); } PiecewisePolynomialFunction1D(); DoubleMatrix1D evaluate(final PiecewisePolynomialResult pp, final double xKey); DoubleMatrix2D evaluate(final PiecewisePolynomialResult pp, final double[] xKeys); DoubleMatrix2D[] evaluate(final PiecewisePolynomialResult pp, final double[][] xKeys); DoubleMatrix1D differentiate(final PiecewisePolynomialResult pp, final double xKey); DoubleMatrix2D differentiate(final PiecewisePolynomialResult pp, final double[] xKeys); DoubleMatrix1D differentiateTwice(final PiecewisePolynomialResult pp, final double xKey); DoubleMatrix2D differentiateTwice(final PiecewisePolynomialResult pp, final double[] xKeys); double integrate(final PiecewisePolynomialResult pp, final double initialKey, final double xKey); DoubleMatrix1D integrate(final PiecewisePolynomialResult pp, final double initialKey, final double[] xKeys);  }
@Test public void evaluateAllTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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 double[][][] valuesExp = new double[][][] { { {-64., -1., 0., 1. / 8. }, {-1. / 8., 1. / 27., 3375. / 7. / 7. / 7., 27. } }, { {125., 20., 5., 5. / 4. }, {45. / 4., 20. / 9., 2240. / 7. / 7. / 7., 20. } } }; final int dim = 2; final int nCoefs = 4; final int keyLength = xKeys[0].length; final int keyDim = xKeys.length; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); final DoubleMatrix2D[] valuesResMat = function.evaluate(pp, xKeys); for (int i = 0; i < dim; ++i) { for (int k = 0; k < keyDim; ++k) { for (int j = 0; j < keyLength; ++j) { final double ref = valuesExp[i][k][j] == 0. ? 1. : Math.abs(valuesExp[i][k][j]); assertEquals(valuesResMat[i].getData()[k][j], valuesExp[i][k][j], ref * EPS); } } } final double[][] valuesRes = function.evaluate(pp, xKeys[0]).getData(); for (int i = 0; i < dim; ++i) { for (int j = 0; j < keyLength; ++j) { final double ref = valuesExp[i][0][j] == 0. ? 1. : Math.abs(valuesExp[i][0][j]); assertEquals(valuesRes[i][j], valuesExp[i][0][j], ref * EPS); } } double[] valuesResVec = function.evaluate(pp, xKeys[0][0]).getData(); for (int i = 0; i < dim; ++i) { final double ref = valuesExp[i][0][0] == 0. ? 1. : Math.abs(valuesExp[i][0][0]); assertEquals(valuesResVec[i], valuesExp[i][0][0], ref * EPS); } valuesResVec = function.evaluate(pp, xKeys[0][3]).getData(); for (int i = 0; i < dim; ++i) { final double ref = valuesExp[i][0][3] == 0. ? 1. : Math.abs(valuesExp[i][0][3]); assertEquals(valuesResVec[i], valuesExp[i][0][3], ref * EPS); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateMultiTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullpEvaluateMatrixTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullxEvaluateTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullxEvaluateMatrixTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateMultiTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxEvaluateMatrixTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateMultiTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxEvaluateMatrixTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( 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(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.evaluate(pp, xKeys); }