如何使用BigInteger在Polynomial类中修复此“plus”方法

时间:2014-02-06 07:57:25

标签: java biginteger polynomial-math

我很感激帮助。除了compose方法之外,我能够将此类中的所有内容修改为BigInteger格式。任何人都可以帮助我最后一部分,为什么它不能正常工作?我真的很感激,谢谢。

import java.math.BigInteger;

public class Polynomial {
private BigInteger[] coef;  // coefficients
private int deg;     // degree of polynomial (0 for the zero polynomial)



/** Creates the constant polynomial P(x) = 1.
  */
public Polynomial(){
    coef = new BigInteger[1];
    coef[0] = new BigInteger("1");
    deg = 0;
}



/** Creates the linear polynomial of the form P(x) =  x + a.
  */
public Polynomial(int a){
    coef = new BigInteger[2];
    coef[1] = new BigInteger("1");
    coef[0] = new BigInteger(Integer.toString(a));
    deg = 1;
}

/** Creates the polynomial P(x) = a * x^b.
  */
public Polynomial(int a, int b) {
    coef = new BigInteger[b+1];
    for(int i = 0; i < b; i++){
        if(coef[i] == null)
            coef[i] = new BigInteger("0");

    }
    coef[b] = new BigInteger(Integer.toString(a));
    deg = degree();
}


/** Return the degree of this polynomial (0 for the constant polynomial).
  */
public int degree() {
    int d = 0;
    for (int i = 0; i < coef.length; i++)
        if (coef[i] != null) d = i; // check to make sure this works
    return d;
}

/** Return the composite of this polynomial and b, i.e., return this(b(x))  - compute using Horner's method.
  */
public Polynomial compose(Polynomial b) {
    Polynomial a = this;
    Polynomial c = new Polynomial(0, 0);
    for (int i = a.deg; i >= 0; i--) {
        Polynomial term = new Polynomial(a.coef[i].intValue(), 0);
        c = term.plus(b.times(c));
    }
    return c;
}

  public Polynomial times(Polynomial b) {
    Polynomial a = this;
    Polynomial c = new Polynomial(0, a.deg + b.deg);
    for (int i = 0; i <= a.deg; i++)
        for (int j = 0; j <= b.deg; j++)
            c.coef[i+j] = c.coef[i+j].add((a.coef[i].multiply(b.coef[j])));
    c.deg = c.degree();
    return c;
}

/** Return a textual representation of this polynomial.
  */
public String toString() {
    if (deg ==  0) return "" + coef[0];
    if (deg ==  1) return coef[1] + "x + " + coef[0];
    String s = coef[deg] + "x^" + deg;
    for (int i = deg-1; i >= 0; i--) {
        if      (coef[i] == null) continue;
        else if (coef[i].intValue()  > 0) s = s + " + " + ( coef[i]);
        else if (coef[i].intValue()  < 0) s = s + " - " + (coef[i].negate());
        if(coef[i].intValue() != 0)
            if      (i == 1) s = s + "x";
        else if (i >  1) s = s + "x^" + i;
    }
    return s;
}

public static void main(String[] args) {
   Polynomial p = new Polynomial(1,2);
   Polynomial q = new Polynomial(2,3); 
   Polynomial t    = p.compose(q); // incorrect
   System.out.println("p(q(x))     = " + t);  // incorrect


  }

}

1 个答案:

答案 0 :(得分:2)

我认为问题在于您的toString()本身,因为它与您的默认机制不一致。意思是,您指定默认值'0'但不检查以下行中的0值:

if      (i == 1) s = s + "x";
else if (i >  1) s = s + "x^" + i;

即使是0系数值也会堆积起来。添加仅检查非零系数的条件:

if (coef[i].intValue() != 0)
 if      (i == 1) s = s + "x";
 else if (i >  1) s = s + "x^" + i;

这应该可行,我没有测试过,但您可以尝试测试并发布结果。

修改

好吧,我刚试过你的代码,似乎在上述条件下提供了正确的信息:

6x^7 + 2x^3