Example #1
0
  /**
   * 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;
  }