/** * @param coefs {a_n,a_{n-1},...} of f(x) = a_n x^{n} + a_{n-1} x^{n-1} + .... * @param x the x-value * @param leftknot the knot specifying underlying interpolation function * @return the value of the underlying interpolation function at the value of x */ protected double getValue(DoubleArray coefs, double x, double leftknot) { // needs to delegate as method is protected return getValue(coefs.toArrayUnsafe(), x, leftknot); }
/** * Evaluates the function. * * @param pp the PiecewisePolynomialResult * @param xKey the key * @return the values of piecewise polynomial functions at xKey * When _dim in PiecewisePolynomialResult is greater than 1, i.e., the struct contains * multiple splines, an element in the return values corresponds to each spline */ public DoubleArray evaluate(PiecewisePolynomialResult pp, double xKey) { ArgChecker.notNull(pp, "pp"); ArgChecker.isFalse(Double.isNaN(xKey), "xKey containing NaN"); ArgChecker.isFalse(Double.isInfinite(xKey), "xKey containing Infinity"); DoubleArray knots = pp.getKnots(); int nKnots = knots.size(); DoubleMatrix coefMatrix = pp.getCoefMatrix(); // check for 1 less interval that knots int lowerBound = FunctionUtils.getLowerBoundIndex(knots, xKey); int indicator = lowerBound == nKnots - 1 ? lowerBound - 1 : lowerBound; return DoubleArray.of(pp.getDimensions(), i -> { DoubleArray coefs = coefMatrix.row(pp.getDimensions() * indicator + i); double res = getValue(coefs, xKey, knots.get(indicator)); ArgChecker.isFalse(Double.isInfinite(res), "Too large input"); ArgChecker.isFalse(Double.isNaN(res), "Too large input"); return res; }); }
double sum = getValue(res[indicator], initialKey, knots.get(indicator)); for (int i = indicator; i < nKnots - 2; ++i) { constTerms[i + 1] = constTerms[i] + getValue(res[i], knots.get(i + 1), knots.get(i)) - sum; sum = 0d; constTerms[indicator] = -getValue(res[indicator], initialKey, knots.get(indicator)); for (int i = indicator - 1; i > -1; --i) { constTerms[i] = constTerms[i + 1] - getValue(res[i], knots.get(i + 1), knots.get(i));
double sum = getValue(res[indicator], initialKey, knots.get(indicator)); for (int i = indicator; i < nKnots - 2; ++i) { constTerms[i + 1] = constTerms[i] + getValue(res[i], knots.get(i + 1), knots.get(i)) - sum; sum = 0.; constTerms[indicator] = -getValue(res[indicator], initialKey, knots.get(indicator)); for (int i = indicator - 1; i > -1; --i) { constTerms[i] = constTerms[i + 1] - getValue(res[i], knots.get(i + 1), knots.get(i));
res[k][j] = getValue(coefs, xKeys[j], knots.get(indicator)); ArgChecker.isFalse(Double.isInfinite(res[k][j]), "Too large input"); ArgChecker.isFalse(Double.isNaN(res[k][j]), "Too large input");
res[k][l][j] = getValue(coefs, xKeys[l][j], knots.get(indicator)); ArgChecker.isFalse(Double.isInfinite(res[k][l][j]), "Too large input"); ArgChecker.isFalse(Double.isNaN(res[k][l][j]), "Too large input");