@Override protected double doInterpolate(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); return Math.exp(resValue); }
@Override protected double doInterpolate(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); return Math.exp(resValue); }
@Override protected double doInterpolate(double xValue) { return evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); }
@Override protected double doInterpolate(double xValue) { return evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); }
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval that knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); double[] resSense = nodeSensitivity( xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), interval, coefficientSensitivity).toArray(); double resValue = Math.exp(evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions())); double[] knotValues = getValues(logYValues); final int knotValuesLength = knotValues.length; double[] res = new double[knotValuesLength]; for (int i = 0; i < knotValuesLength; ++i) { res[i] = resSense[i] * resValue / knotValues[i]; } return DoubleArray.ofUnsafe(res); }
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval than knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); int nCoefs = coefficientSensitivity.rowCount(); double s = xValue - poly.getKnots().get(interval); DoubleArray res = coefficientSensitivity.row(0); for (int i = 1; i < nCoefs; i++) { res = (DoubleArray) MA.scale(res, s); res = (DoubleArray) MA.add(res, coefficientSensitivity.row(i)); } return res; }
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval than knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); int nCoefs = coefficientSensitivity.rowCount(); double s = xValue - poly.getKnots().get(interval); DoubleArray res = coefficientSensitivity.row(0); for (int i = 1; i < nCoefs; i++) { res = (DoubleArray) MA.scale(res, s); res = (DoubleArray) MA.add(res, coefficientSensitivity.row(i)); } return res; }
@Override protected DoubleArray doParameterSensitivity(double xValue) { int interval = FunctionUtils.getLowerBoundIndex(poly.getKnots(), xValue); if (interval == poly.getKnots().size() - 1) { interval--; // there is 1 less interval that knots } DoubleMatrix coefficientSensitivity = polySens.get().getCoefficientSensitivity(interval); double[] resSense = nodeSensitivity( xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), interval, coefficientSensitivity).toArray(); double resValue = Math.exp(evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions())); double[] knotValues = getValues(logYValues); final int knotValuesLength = knotValues.length; double[] res = new double[knotValuesLength]; for (int i = 0; i < knotValuesLength; ++i) { res[i] = resSense[i + 1] * resValue / knotValues[i]; } return DoubleArray.ofUnsafe(res); }
@Override protected double doFirstDerivative(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); double resDerivative = differentiate( xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); return Math.exp(resValue) * resDerivative; }
@Override protected double doFirstDerivative(double xValue) { double resValue = evaluate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions()); int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); double resDerivative = differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); return Math.exp(resValue) * resDerivative; }
@Override protected double doFirstDerivative(double xValue) { int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); return differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); }
@Override protected double doFirstDerivative(double xValue) { int nCoefs = poly.getOrder(); int numberOfIntervals = poly.getNumberOfIntervals(); return differentiate(xValue, poly.getKnots(), poly.getCoefMatrix(), poly.getDimensions(), nCoefs, numberOfIntervals); }
Bound(DoubleArray xValues, DoubleArray yValues) { super(xValues, yValues); this.xValues = xValues.toArrayUnsafe(); this.yValues = yValues.toArrayUnsafe(); PiecewisePolynomialInterpolator underlying = new PiecewiseCubicHermiteSplineInterpolatorWithSensitivity(); PiecewisePolynomialResult poly = underlying.interpolate(xValues.toArray(), yValues.toArray()); this.knots = poly.getKnots(); this.coefMatrix = poly.getCoefMatrix(); this.coefMatrixSensi = Suppliers.memoize( () -> underlying.interpolateWithSensitivity(xValues.toArray(), yValues.toArray()).getCoefficientSensitivityAll()); }
/** * d_i =0 if delta_i = 0 or delta_{i-1} = 0 */ public void CoincideYvaluesTest() { final double[] xValues = new double[] {1., 2., 3., 4. }; final double[] yValues = new double[] {1., 2., 2., 3. }; final int nIntervalsExp = 3; final int orderExp = 4; final int dimExp = 1; final double[][] coefsMatExp = new double[][] { {-1. / 2., 0., 1.5, 1. }, {0., 0., 0., 2. }, {-0.5, 1.5, 0., 2. } }; PiecewiseCubicHermiteSplineInterpolator interp = new PiecewiseCubicHermiteSplineInterpolator(); PiecewisePolynomialResult result = interp.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().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ 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().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ 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 = 2; final int dimExp = 2; final double[][] coefsMatExp = new double[][] { {-5., 6. }, {3., 2. } }; LinearInterpolator interpMatrix = new LinearInterpolator(); 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().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * */ public void recov4ptsMultiTest() { final double[] xValues = new double[] {1., 2., 3., 4 }; final double[][] yValues = new double[][] { {6., 1., 8., -2. }, {1., 1. / 3., 2. / 11., 1. / 7. } }; final int nIntervalsExp = 3; final int orderExp = 2; final int dimExp = 2; final double[][] coefsMatExp = new double[][] { {-5., 6. }, {-2. / 3., 1. }, {7., 1. }, {-5. / 33., 1. / 3. }, {-10., 8. }, {-3. / 77., 2. / 11. } }; LinearInterpolator interpMatrix = new LinearInterpolator(); 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().get(i, j), coefsMatExp[i][j], ref * EPS); } } for (int j = 0; j < nIntervalsExp + 1; ++j) { assertEquals(result.getKnots().get(j), xValues[j]); } }
/** * Finds the first derivatives. * * @param pp the PiecewisePolynomialResult * @param xKeys the key * @return the first derivatives of piecewise polynomial functions at xKeys * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, a row vector of return value corresponds to each piecewise polynomial */ public DoubleMatrix differentiate(PiecewisePolynomialResult pp, double[] xKeys) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 2, "polynomial degree < 1"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 1; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, colCount, pp.getDimensions()); return evaluate(ppDiff, xKeys); }
/** * Finds the first derivatives. * * @param pp the PiecewisePolynomialResult * @param xKey the key * @return the first derivatives of piecewise polynomial functions at xKey * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, an element in the return values corresponds to each piecewise polynomial */ public DoubleArray differentiate(PiecewisePolynomialResult pp, double xKey) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 2, "polynomial degree < 1"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 1; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, colCount, pp.getDimensions()); return evaluate(ppDiff, xKey); }
/** * Finds the second derivatives. * * @param pp the PiecewisePolynomialResult * @param xKeys the key * @return the second derivatives of piecewise polynomial functions at xKeys * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple piecewise polynomials, a row vector of return value corresponds to each piecewise polynomial */ public DoubleMatrix differentiateTwice(PiecewisePolynomialResult pp, double[] xKeys) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(pp.getOrder() < 3, "polynomial degree < 2"); DoubleArray knots = pp.getKnots(); int nCoefs = pp.getOrder(); int rowCount = pp.getDimensions() * pp.getNumberOfIntervals(); int colCount = nCoefs - 2; DoubleMatrix coef = DoubleMatrix.of( rowCount, colCount, (i, j) -> pp.getCoefMatrix().get(i, j) * (nCoefs - j - 1) * (nCoefs - j - 2)); PiecewisePolynomialResult ppDiff = new PiecewisePolynomialResult(knots, coef, nCoefs - 1, pp.getDimensions()); return evaluate(ppDiff, xKeys); }