我有一个问题要完成使用Java多项式。我已经设置了所有代码,它看起来没问题,但是使用我的复合方法,我收到的输出不同于预期。
class Polyonmial
{
final static private int mantissa=52;
final static private double epsilon=Math.pow(2.0,-mantissa);
private double coefficient=0.0;
private int power=0;
private Polynomial successor=null;
public Polynomial(double coefficient, int power)
{
if (Double.isNaN(coefficient)) return;
if (Math.abs(coefficient)<epsilon) return;
if (power<0) return;
this.coefficient=coefficient;
this.power=power;
}
final public double coefficient(int power)
{
if (power<0) return 0.0;
Polynomial traverser=this;
do
{
if (traverser.power<power) return 0.0;
if (traverser.power==power) return traverser.coefficient;
traverser=traverser.successor;
}
while (traverser!=null);
return 0.0;
}
final public Polynomial composite(Polynomial that)
{
if (that==null) return null;
Polynomial thisClone = this.clone();
Polynomial thatClone = that.clone();
Polynomial temp = new Polynomial(0.0,0);
Polynomial result = new Polynomial(0.0,0);
while(thisClone != null)
{
if (thisClone.power == 0)
{
temp.coefficient = 1.0;
temp.power = 0;
}
else
{
if (thisClone.power == 1)
temp = thatClone;
}
//System.out.println("temp:"+temp);
while(temp != null)
{
temp.coefficient = thisClone.coefficient*temp.coefficient;
result = result.plus(temp);
temp = temp.successor;
}
temp = new Polynomial(0.0,0);
thisClone=thisClone.successor;
}
return result;
}
final public Polynomial[] dividedBy(Polynomial that)
{
if (that==null) return null;
if (that.coefficient==0.0) return null;
Polynomial quotient=new Polynomial(0.0,0);
Polynomial remainder=this.clone();
Polynomial traverser = this.clone();
Polynomial resultoftemp = new Polynomial(0.0, 0);
Polynomial temp = new Polynomial(0.0, 0);
double thiscoffe = this.coefficient(this.degree());
double thatcoffe = that.coefficient(that.degree());
if(that.coefficient !=0 && that.power !=0)
{
quotient.coefficient = thatcoffe / thiscoffe;
quotient.power = that.power - this.power;
}
while(traverser !=null)
{
temp.power = quotient.power + traverser.power;
temp.coefficient = quotient.coefficient * traverser.coefficient;
traverser=traverser.successor;
resultoftemp = resultoftemp.plus(temp);
remainder = that.minus(resultoftemp);
}
Polynomial[] result=new Polynomial[2];
result[0]=quotient;
result[1]=remainder;
return result;
}
final public Polynomial integrate()
{
if (this.coefficient==0.0) return new Polynomial(0.0,0);
Polynomial result=this.clone();
Polynomial temp = new Polynomial(0.0, 0);
Polynomial rstemp = new Polynomial(0.0, 0);
while(result!=null)
{
rstemp.power = result.power + 1;
rstemp.coefficient = result.coefficient / (result.power +1);
result = result.successor;
temp = temp.plus(rstemp);
}
return result;
}
final public Polynomial minus(Polynomial that)
{
if (that==null) return null;
if (this.equals(that)) return new Polynomial(0.0,0);
Polynomial result=this.clone();
if (that.coefficient==0.0) return result;
Polynomial traverser=that;
do
{
add(result,-traverser.coefficient,traverser.power);
traverser=traverser.successor;
}
while (traverser!=null);
return result;
}
final public int powerMax()
{
int max=Integer.MIN_VALUE;
Polynomial traverser=this;
do
{
if (max<traverser.power) max=traverser.power;
traverser=traverser.successor;
}
while (traverser!=null);
return max;
}
final public int powerMin()
{
int min=Integer.MAX_VALUE;
Polynomial traverser=this;
do
{
if (min>traverser.power) min=traverser.power;
traverser=traverser.successor;
}
while (traverser!=null);
return min;
}
}
在我预期的输出文件中,我应该收到这个:
(-1.0*X^1+1.0).composite(-1.0*X^1+1.0)=
1.0*X^1
但我收到了:
(-1.0*X^1+1.0).composite(-1.0*X^1+1.0)=
1.0*X^1+1.0
非常感谢任何提示或帮助。
答案 0 :(得分:2)
我稍微改变了你的复合方法(逻辑等效,支撑移动和println添加):
final public Polynomial composite(Polynomial that) {
if (that==null) return null;
Polynomial thisClone = this.clone();
Polynomial thatClone = that.clone();
Polynomial temp = new Polynomial(0.0,0);
Polynomial result = new Polynomial(0.0,0);
while(thisClone != null) {
System.out.println("composite with term degree: " + thisClone.power);
if (thisClone.power == 0) {
temp.coefficient = 1.0;
temp.power = 0;
} else if (thisClone.power == 1) {
temp = thatClone;
} else {
for(int i=2; i<=thisClone.power; i++) {
temp = temp.plus(thatClone.times(thatClone));
}
}
System.out.println("temp heading in: " + temp);
while(temp != null) {
temp.coefficient = thisClone.coefficient*temp.coefficient;
result = result.plus(temp);
temp = temp.successor;
}
System.out.println("result so far: " + result);
temp = new Polynomial(0.0,0);
thisClone=thisClone.successor;
}
return result;
}
,对于示例(-1.0*X^1+1.0).composite(-1.0*X^1+1.0)
,这是输出:
复合词,学位:1 临时标题:-1.0 * X ^ 1 + 1.0
到目前为止的结果:1.0 * X ^ 1
期限度为0的复合词 温度标题:1.0
到目前为止的结果:1.0 * X ^ 1 + 1.0
我是否相信第一个“迄今为止的结果”应为“-1.0 * X ^ 1-1.0”?
如果是这样,让我们检查循环:
while(temp != null) {
temp.coefficient = thisClone.coefficient*temp.coefficient;
result = result.plus(temp);
temp = temp.successor;
}
我认为这是你的问题:result = result.plus(temp)
不只是将“当前术语”添加到temp,还添加了后续术语。但是你通过设置它等于它的继承者来循环使用temp并再次执行它!
我认为解决方案是这样的(首先计算所有临时术语,然后更新结果):
Polynomial looptemp = temp;
while(looptemp != null) {
looptemp.coefficient = thisClone.coefficient*looptemp.coefficient;
looptemp = looptemp.successor;
}
result = result.plus(temp);
至少,它适用于我尝试过的单个例子。