コード例 #1
0
  public Polynomial pow(int exp) {
    if (exp == 0) return valueOf(new Number[] {baseRing.getONE()});
    if (exp == 1) return valueOf(this.getCoeffs());
    if (exp < 0)
      throw new IllegalArgumentException("Tried to raise a Polynomial to a negative power");

    Polynomial res = valueOf(new Number[] {baseRing.getONE()});
    Polynomial currentPower = this;
    int ex = exp;
    while (ex != 0) {
      if ((ex & 1) == 1) res = res.mul(currentPower);
      ex >>= 1;
      if (ex == 0) break;
      currentPower = currentPower.mul(currentPower);
    }
    return res;
  }
コード例 #2
0
  public String toString() {
    if (degree == 0) return coeffs[0].toString();
    StringBuffer sb = new StringBuffer("");
    for (int i = degree; i >= 0; --i) {
      String s = coeffs[i].toString();

      // don't bother if a zero coeff
      if (s.equals("0") || this.baseRing.equals(coeffs[i], baseRing.getZERO())) continue;

      // apart from first add a + sign if positive
      if (i != degree && !s.startsWith("-")) sb.append("+");

      // always print the final coeff (if non zero)
      if (i == 0) {
        String s1 = coeffs[i].toString();
        sb.append(s1);
        // if(s1.startsWith("(") && s1.endsWith(")"))
        // {
        //		sb.append(s1.substring(1,s1.length()-1));
        // }
        // else 	sb.append(s1);
        break;
      }
      // if its -1 t^i just print -
      if (s.equals("-1")) sb.append("-");
      else if (s.equals("1") || this.baseRing.equals(coeffs[i], baseRing.getONE())) {
      } // don't print 1
      else {
        if (needsBrackets(coeffs[i].toString())) {
          sb.append("(");
          sb.append(coeffs[i].toString());
          sb.append(")");
        } else sb.append(coeffs[i].toString());
        // sb.append(stripBrackets(coeffs[i]));
        sb.append(" ");
      }
      if (i >= 2) sb.append(symbol + "^" + i);
      else if (i == 1) sb.append(symbol);
    }
    sb.append("");
    return sb.toString();
  }