コード例 #1
0
  /**
   * 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;
  }