@Test(expectedExceptions = IllegalArgumentException.class) public void testAddNull() { F1.add(null); }
/** * Adds a function to the polynomial. * If the function is not a {@link RealPolynomialFunction1D} then the addition takes * place as in {@link DoubleFunction1D}, otherwise the result will also be a polynomial. * * @param f the function to add * @return $P+f$ */ @Override public DoubleFunction1D add(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f instanceof RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D) f; double[] c1 = p1.getCoefficients(); double[] c = _coefficients; int n = c1.length; boolean longestIsNew = n > _n; double[] c3 = longestIsNew ? Arrays.copyOf(c1, n) : Arrays.copyOf(c, _n); for (int i = 0; i < (longestIsNew ? _n : n); i++) { c3[i] += longestIsNew ? c[i] : c1[i]; } return new RealPolynomialFunction1D(c3); } return DoubleFunction1D.super.add(f); }
/** * Calculates polynomials. * @param n the n value * @param alpha the alpha value * @param beta the beta value * @return the result */ public DoubleFunction1D[] getPolynomials(int n, double alpha, double beta) { ArgChecker.isTrue(n >= 0); DoubleFunction1D[] polynomials = new DoubleFunction1D[n + 1]; for (int i = 0; i <= n; i++) { if (i == 0) { polynomials[i] = getOne(); } else if (i == 1) { polynomials[i] = new RealPolynomialFunction1D(new double[] {(alpha - beta) / 2, (alpha + beta + 2) / 2}); } else { int j = i - 1; polynomials[i] = (polynomials[j].multiply(getB(alpha, beta, j)).add(polynomials[j].multiply(getX()).multiply(getC(alpha, beta, j)) .add(polynomials[j - 1].multiply(getD(alpha, beta, j))))) .divide(getA(alpha, beta, j)); } } return polynomials; }