This question already has an answer here:
这个问题在这里已有答案:
- How can I fix this “plus” method in Polynomial class using BigInteger 1 answer
如何使用BigInteger 1答案在Polynomial类中修复此“plus”方法
I have to modify a Polynomial implemented with integers, so that it can hold BigIntegers as well. I'm getting null pointer exception error. Please help!
我必须修改用整数实现的多项式,这样它也可以容纳BigIntegers。我得到空指针异常错误。请帮忙!
Exception at Graph.Polynomial.plus(Polynomial.java:59) and at Graph.Polynomial.main(Polynomial.java:189)
Graph.Polynomial.plus(Polynomial.java:59)和Graph.Polynomial.main(Polynomial.java:189)的异常
package Graph;
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(BigInteger a){
coef = new BigInteger[2];
coef[1] = new BigInteger("1");
coef[0] = a;
deg = 1;
}
/** Creates the polynomial P(x) = a * x^b.
*/
public Polynomial(BigInteger a, BigInteger b) {
coef = new BigInteger[b.intValue()+1];
coef[b.intValue()] = 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]!= new BigInteger("0")) d = i;
return d;
}
/** Return the sum of this polynomial and b, i.e., return c = this + b.
*/
public Polynomial plus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(new BigInteger(("0"), Math.max(a.deg, b.deg)));
for (int i = 0; i <= a.deg; i++) c.coef[i] = c.coef[i].add(a.coef[i]);
for (int i = 0; i <= b.deg; i++) c.coef[i] = c.coef[i].add(b.coef[i]);
c.deg = c.degree();
return c;
}
/** Return the difference of this polynomial and b, i.e., return (this - b).
*/
public Polynomial minus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(new BigInteger("0", Math.max(a.deg, b.deg)));
for (int i = 0; i <= a.deg; i++) c.coef[i] = c.coef[i].add(a.coef[i]);
for (int i = 0; i <= b.deg; i++) c.coef[i] = c.coef[i].subtract(b.coef[i]);
c.deg = c.degree();
return c;
}
/** Return the product of this polynomial and b, i.e., return (this * b).
*/
public Polynomial times(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(new BigInteger("0"), new BigInteger("a.deg").add(new BigInteger("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 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(new BigInteger("0"), new BigInteger("0"));
for (int i = a.deg; i >= 0; i--) {
Polynomial term = new Polynomial(a.coef[i], new BigInteger("0"));
c = term.plus(b.times(c));
}
return c;
}
/** Return true whenever this polynomial and b are identical to one another.
*/
public boolean equals(Polynomial b) {
Polynomial a = this;
if (a.deg != b.deg) return false;
for (int i = a.deg; i >= 0; i--)
if (a.coef[i] != b.coef[i]) return false;
return true;
}
/** Evaluate this polynomial at x, i.e., return this(x).
*/
public BigInteger evaluate(BigInteger x) {
BigInteger p = new BigInteger("0");
for (int i = deg; i >= 0; i--)
p = coef[i].add((new BigInteger("x").multiply(new BigInteger("p"))));
return p;
}
/** Return the derivative of this polynomial.
*/
public Polynomial differentiate() {
if (deg == 0) return new Polynomial(new BigInteger("0"), new BigInteger("0"));
Polynomial deriv = new Polynomial(new BigInteger("0"), new BigInteger("deg").subtract(new BigInteger("1")));
deriv.deg = deg - 1;
for (int i = 0; i < deg; i++)
deriv.coef[i] = new BigInteger("i + 1").multiply(coef[i + 1]);
return deriv;
}
/** 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] == new BigInteger("0")) continue;
else if (coef[i].signum()==1) s = s + " + " + ( coef[i]);
else if (coef[i].signum()== -1) s = s + " - " + (coef[i].multiply(new BigInteger("-1")));
if (i == 1) s = s + "x";
else if (i > 1) s = s + "x^" + i;
}
return s;
}
public static void main(String[] args) {
Polynomial zero = new Polynomial(new BigInteger("0"), new BigInteger("0"));
Polynomial p1 = new Polynomial(new BigInteger("476867"), new BigInteger("8"));
Polynomial p2 = new Polynomial(new BigInteger("3"), new BigInteger("2"));
Polynomial p3 = new Polynomial(new BigInteger("-1"), new BigInteger("0"));
Polynomial p4 = new Polynomial(new BigInteger("-2"), new BigInteger("1"));
Polynomial p = p1.plus(p2).plus(p3).plus(p4);
Polynomial q1 = new Polynomial(new BigInteger("3"), new BigInteger("2"));
Polynomial q2 = new Polynomial(new BigInteger("5"), new BigInteger("0"));
Polynomial q = q1.minus(q2);
Polynomial r = p.plus(q);
Polynomial s = p.times(q);
Polynomial t = p.compose(q);
System.out.println("zero(x) = " + zero);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + r);
System.out.println("p(x) * q(x) = " + s);
System.out.println("p(q(x)) = " + t);
System.out.println("0 - p(x) = " + zero.minus(p));
System.out.println("p(3) = " + p.evaluate(new BigInteger("3")));
System.out.println("p'(x) = " + p.differentiate());
System.out.println("p''(x) = " + p.differentiate().differentiate());
Polynomial poly = new Polynomial();
for(int k=0; k<=3; k++){
poly = poly.times(new Polynomial(new BigInteger("-k")));
}
System.out.println(poly);
}
}
2 个解决方案
#1
1
When you create a polynomial with this constructor:
使用此构造函数创建多项式时:
public Polynomial(BigInteger a, BigInteger b) {
coef = new BigInteger[b.intValue()+1];
coef[b.intValue()] = a;
deg = degree();
}
most of the entries in the coef
array will be null
. This leads to a NPE when you later add two such polynomials with plus
coef数组中的大多数条目都将为null。当你稍后添加两个带加号的多项式时,这会导致NPE
#2
1
On line 58 you have
在第58行你有
Polynomial c = new Polynomial(new BigInteger(("0"), Math.max(a.deg, b.deg)));
多项式c = new多项式(new BigInteger((“0”),Math.max(a.deg,b.deg)));
I think you have a bug here.
This seems to create polynomial c of degree 0.
我觉得你这里有个bug。这似乎创建了0度的多项式c。
Basically you're calling this constructor.
基本上你是在调用这个构造函数。
BigInteger(String val, int radix)
BigInteger(String val,int radix)
I don't think this was your intention here,
maybe you put some brackets at some wrong places.
我不认为这是你的意图,也许你把一些括号放在错误的地方。
#1
1
When you create a polynomial with this constructor:
使用此构造函数创建多项式时:
public Polynomial(BigInteger a, BigInteger b) {
coef = new BigInteger[b.intValue()+1];
coef[b.intValue()] = a;
deg = degree();
}
most of the entries in the coef
array will be null
. This leads to a NPE when you later add two such polynomials with plus
coef数组中的大多数条目都将为null。当你稍后添加两个带加号的多项式时,这会导致NPE
#2
1
On line 58 you have
在第58行你有
Polynomial c = new Polynomial(new BigInteger(("0"), Math.max(a.deg, b.deg)));
多项式c = new多项式(new BigInteger((“0”),Math.max(a.deg,b.deg)));
I think you have a bug here.
This seems to create polynomial c of degree 0.
我觉得你这里有个bug。这似乎创建了0度的多项式c。
Basically you're calling this constructor.
基本上你是在调用这个构造函数。
BigInteger(String val, int radix)
BigInteger(String val,int radix)
I don't think this was your intention here,
maybe you put some brackets at some wrong places.
我不认为这是你的意图,也许你把一些括号放在错误的地方。