PiecewisePolynomialFunction1D { public double integrate(final PiecewisePolynomialResult pp, final double initialKey, final double xKey) { ArgumentChecker.notNull(pp, "pp"); ArgumentChecker.isFalse(Double.isNaN(initialKey), "initialKey containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(initialKey), "initialKey containing Infinity"); ArgumentChecker.isTrue(pp.getDimensions() == 1, "Dimension should be 1"); final double[] knots = pp.getKnots().getData(); final int nCoefs = pp.getOrder(); final int nKnots = pp.getNumberOfIntervals() + 1; final double[][] coefMatrix = pp.getCoefMatrix().getData(); double[][] res = new double[nKnots - 1][nCoefs + 1]; for (int i = 0; i < nKnots - 1; ++i) { Arrays.fill(res[i], 0.); } for (int i = 0; i < nKnots - 1; ++i) { for (int j = 0; j < nCoefs; ++j) { res[i][j] = coefMatrix[i][j] / (nCoefs - j); } } double[] constTerms = new double[nKnots - 1]; Arrays.fill(constTerms, 0.); int indicator = 0; if (initialKey <= knots[1]) { indicator = 0; } else { for (int i = 1; i < nKnots - 1; ++i) { if (knots[i] < initialKey) { indicator = i; } } } double sum = getValue(res[indicator], initialKey, knots[indicator]); for (int i = indicator; i < nKnots - 2; ++i) { constTerms[i + 1] = constTerms[i] + getValue(res[i], knots[i + 1], knots[i]) - sum; sum = 0.; } constTerms[indicator] = -getValue(res[indicator], initialKey, knots[indicator]); for (int i = indicator - 1; i > -1; --i) { constTerms[i] = constTerms[i + 1] - getValue(res[i], knots[i + 1], knots[i]); } for (int i = 0; i < nKnots - 1; ++i) { res[i][nCoefs] = constTerms[i]; } final PiecewisePolynomialResult ppInt = new PiecewisePolynomialResult(new DoubleMatrix1D(knots), new DoubleMatrix2D(res), nCoefs + 1, 1); return evaluate(ppInt, xKey).getData()[0]; } 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(expectedExceptions = IllegalArgumentException.class) public void nullpIntegrateTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); final double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullpIntegrateMultiTest() { final double[] xValues = new double[] {1, 2, 3, 4 }; final DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); final double[][] xKeys = new double[][] { {-2, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); pp = null; PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullxIntTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); double[] xKeys = new double[] {-2, 1, 2, 2.5 }; final int dim = 1; final int nCoefs = 4; xKeys = null; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxIntTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); double[][] xKeys = new double[][] { {INF, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void infxIntMultiTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); double[] xKeys = new double[] {1.5, 7. / 3., 29. / 7., INF }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxIntTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); double[][] xKeys = new double[][] { {Double.NaN, 1, 2, 2.5 }, {1.5, 7. / 3., 29. / 7., 5. } }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys[0][0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNxIntMultiTest() { double[] xValues = new double[] {1, 2, 3, 4 }; DoubleMatrix2D coefsMatrix = new DoubleMatrix2D( new double[][] { {1., -3., 3., -1 }, {1., 0., 0., 0. }, {1., 3., 3., 1. } }); double[] xKeys = new double[] {1.5, 7. / 3., 29. / 7., Double.NaN }; final int dim = 1; final int nCoefs = 4; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullDimIntTest() { 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; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys[0]); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullDimIntMultiTest() { 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; PiecewisePolynomialResult pp = new PiecewisePolynomialResult(new DoubleMatrix1D(xValues), coefsMatrix, nCoefs, dim); PiecewisePolynomialFunction1D function = new PiecewisePolynomialFunction1D(); function.integrate(pp, 1., xKeys); }