如何修复此“加号”使用BigInteger的Polynomial类中的方法 [英] How can I fix this "plus" method in Polynomial class using BigInteger

查看:143
本文介绍了如何修复此“加号”使用BigInteger的Polynomial类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I appreciate the help. I was able to finish modifying everything in this class into BigInteger format except for the compose method. Can anyone help me with this last part as to why it is not working correctly? I really appreciate it, thanks.

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


  }

}


推荐答案

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

What I think is the problem is with your toString() itself as it does not align to your defaulting mechanism. Meaning, you assign default value of '0's but do not check for 0 values in the following lines:

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

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

It gets piled up even for 0 coefficient values. Add a condition of checking non-zero coefficient only:

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

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

This should work, I haven't tested it but you can try testing and post the results.

编辑:

嗯,我刚试过你的代码,似乎给了符合上述条件的正确信息:

Well, i just tried your code and seems to give the correct information with the above condition in place:

6x^7 + 2x^3

这篇关于如何修复此“加号”使用BigInteger的Polynomial类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆