public static void derivative(Polynomial poly, Polynomial deriv) { deriv.size = poly.size - 1; for (int i = 1; i < poly.size; i++) { deriv.c[i - 1] = poly.c[i] * i; } }
/** * Polynomial division. Computes both the quotient and the remainder.<br> * <br> * quotient = numerator/denominator<br> * remainder = numerator % denominator * * @param numerator Numerator in the division. Not modified. * @param denominator Denominator in the division. Not modified. * @param quotient Output quotient, Modified. * @param remainder Output remainder. Modified. */ public static void divide( Polynomial numerator, Polynomial denominator, Polynomial quotient, Polynomial remainder) { int nn = numerator.size - 1; int nd = denominator.size - 1; while (nd >= 0 && denominator.c[nd] == 0) nd -= 1; quotient.size = nn - nd + 1; remainder.setTo(numerator); for (int k = nn - nd; k >= 0; k--) { double c = quotient.c[k] = remainder.c[nd + k] / denominator.c[nd]; for (int j = k + nd; j >= k; j--) { remainder.c[j] -= c * denominator.c[j - k]; } } // The remainder can't be larger than the denominator remainder.size = nd; }
/** * Multiplies the two polynomials together. * * @param a Polynomial * @param b Polynomial * @param result Optional storage parameter for the results. Must be have enough coefficients to * store the results. If null a new instance is declared. * @return Results of the multiplication */ public static Polynomial multiply(Polynomial a, Polynomial b, Polynomial result) { int N = Math.max(0, a.size() + b.size() - 1); if (result == null) { result = new Polynomial(N); } else { if (result.size < N) throw new IllegalArgumentException("Unexpected length of 'result'"); result.zero(); } for (int i = 0; i < a.size; i++) { double coef = a.c[i]; int index = i; for (int j = 0; j < b.size; j++) { result.c[index++] += coef * b.c[j]; } } return result; }
// TODO try using a linear search alg here public static double refineRoot(Polynomial poly, double root, int maxIterations) { // for( int i = 0; i < maxIterations; i++ ) { // // double v = poly.c[poly.size-1]; // double d = v*(poly.size-1); // // for( int j = poly.size-1; j > 0; j-- ) { // v = poly.c[j] + v*root; // d = poly.c[j]*j + d*root; // } // v = poly.c[0] + v*root; // // if( d == 0 ) // return root; // // root -= v/d; // } // // return root; Polynomial deriv = new Polynomial(poly.size()); derivative(poly, deriv); for (int i = 0; i < maxIterations; i++) { double v = poly.evaluate(root); double d = deriv.evaluate(root); if (d == 0) return root; root -= v / d; } return root; }