2011-05-23 43 views
0

我有一個問題需要完成使用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

您是否試過通過調試器運行第一個示例((-1.0 * X^1 + 1.0).composite(-1.0 * X^1 + 1.0))?這個例子看起來很簡單,值得一試。這似乎相當奇特的-1.0 + 1.0沒有發生。 – ccoakley 2011-05-23 03:32:45

+0

@ccoakley我認爲它的複合方法 – Cheese 2011-05-23 04:09:33

+0

@Bill:好,這是我第一次看,我發現了一個明顯的錯誤。如果我誤解了某些內容,請告訴我。 – ccoakley 2011-05-23 04:41:26

回答

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),這是輸出:

複合材料term degree:1
temp heading in:-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!

我認爲解決的辦法是這樣的(先計算所有的臨時條款,那麼更新結果):

Polynomial looptemp = temp; 
    while(looptemp != null) { 
     looptemp.coefficient = thisClone.coefficient*looptemp.coefficient; 
     looptemp = looptemp.successor; 
    } 
    result = result.plus(temp); 

最起碼,它適用於單例如我試過了。

+0

@ Bill:它是否對任何度數小於2的例子失敗?我也認爲你的'temp = temp.plus(thatClone.times(thatClone));'看起來有點可疑,但我沒有嘗試一個例子來行使這個代碼。不幸的是,這是我的就寢時間。祝你好運! – ccoakley 2011-05-23 05:18:24

+0

@Bill:只是一個調試建議:進一步簡化測試用例。嘗試只有兩個條件(行使繼任者)和一個任期(如果沒有後繼問題)。沒有令人信服的理由(尚未)用三個術語進行測試。我期望如果存在錯誤,可以用兩個術語來行使,並且希望不是隻有一個。負的係數是好的,但使它們不同(忽略符號,你的測試用例中有3個1.0係數)。再次評論,我會檢查下班回家的時間。再次祝你好運! – ccoakley 2011-05-23 17:52:56