2012-05-14 35 views
0

我在玩數組,我試着做一個遞歸函數來計算矩陣A [i] [j]的行列式,其中i = j = k。我寫的功能如下。看來我做錯了什麼,但我不知道是什麼。 A數組由用戶定義並由另一個函數讀取。我試圖做到這一點,而不使用和庫或預定義的對象。 我已經添加了整個代碼爲你做一個總體思路:矩陣行列式計算器的遞歸函數

<script> 

    var k; 

    function readGrad() {  //reads the matrix dimesnions  
     k = parseInt(document.getElementById("grad").value);    
     if (isNaN(k)) { 
      alert('Gradul introdus nu este intreg, reintroduceti gradul matricii'); 
     } 
     if (k == 0) { 
      alert ('Determinantul trebuie sa fie mai mare ca 1'); 
     } 
     if (k == 1) { 
      alert ('Determinantul trebuie sa fie mai mare ca 1'); 
     } 
     return k; 
    }  

    function genTable(i,j) {   //generates the table for the user to insert the values 
     //var i,j = parseInt(document.getElementById("grad").value); 
     var myTable = '<TABLE BORDER="1" BORDERCOLOR="BLACK">\n <TBODY>\n'; 
     for (i = 0; i < 1; i++) { 
      myTable += ' <TR>\n'; 
      for (j = 0; j < k+1; j++) { 
       myTable += ' <TD>'+j+'</TD>\n'; 
      } 
      myTable += ' </TR>\n'; 
     } 
     for (i = 1; i < k+1; i++) { 
      myTable += ' <TR>\n'; 
      for (j = 0; j < 1; j++) { 
       myTable += ' <TD>'+i+'</TD>\n'; 
      } 
      for (j = 1; j < k+1; j++) { 
      myTable += ' <TD><input class="element" id="A' + i + j + '"value="0"></TD>\n'; 
      } 
      myTable += ' </TR>\n'; 
     } 
     myTable += ' </TBODY>\n</TABLE>\n'; 
     document.getElementById('container').innerHTML = myTable; 
    } 

    function calcDet() { 

     var A = [];  //generates the array 
      for (var i = 0; i < k; i++) { 
       A[i] = []; 
       for (var j = 0; j < k; j++) {      
        A[i][j] = i + ':' + j;    
       } 
      }   

     for (var i = 0; i < k; i++) { //reads the array 
      for (var j=0; j<k; j++) { 
       var id = "A" + (i + 1) + (j + 1); 
       A[i][j] = parseFloat(document.getElementById(id).value);           
      } 
     } 

     var s; 
     var det = 0; 

     function calcRec (A) { 
      if (A.length == 1) {  //bottom case of the recursive function 
       alert (A) 
      } 
      if (i == 2) {  
       det = A[0][0] * A[1][1] - A[1][0] * A [0][1]; 
       alert (det); 
      } 

      for (var i=0; i<k; i++) { //creates smaller matrix- values not in same row, column 
       var smaller=new Array(A.length-1); 
       for(h=0;h<smaller.length;h++){ 
       smaller[h]=new Array(smaller.length); 
       } 
      } 

      for(a=1;a<A.length;a++){  
       for(b=0;b<A.length;b++){ 
        if(b<i){ 
        smaller[a-1][b]=A[a][b]; 
        } 
        else if(b>i){ 
         smaller[a-1][b-1]=A[a][b]; 
        } 
       } 
      } 

      if (i%2==0) { 
       s=1; 
      } 
      else { 
       s=-1; 
      } 
      det+=s*matrix[0][i]*(calcRec(smaller));  
      return (det); 
     } 
     } 
</script> 
<body style="background-color: #777; color: ddd;"> 
    <div style="margin: 20px;"> 
     <h1>Program de calculare determinant matrice de orice grad.</h1> 
    </div> 
    <div> 
     Introduceti gradul matricei 
     <input id="grad" type="text" value="" style="width: 50px;" onChange="readGrad()">   
     <input style="margin-top: 20px;" type="button" name="Calculate determinant" value="Generati tabel" onClick="genTable()"> 
    </div> 
    <form name="Det Matrice"> 
     <div style="margin-top: 100px; float: left; width: 100%;"> 
      Introduceti valorile: 
      <table style="text-align: center;"> 
       <div id="container"></div> 
      </table> 
      <br> 
     </div> 
     <div style="float: left; margin-top: 20px;">    
      <input type="button" name="Calculate determinant" value="Calculati determinant" onClick="calcDet()"> 
     </div> 
    </form> 

</body> 
+0

你能否提供一些有關錯誤的更多細節?是否引發錯誤?一些示例輸入,預期輸出以及您獲得的「錯誤」輸出也會有所幫助。 – DGH

+0

ideea是當我按下一個按鈕calcDet()被調用,它應該警告det的最終值,但現在沒有任何反應。代碼相當長。如果你願意的話,我可以添加它,但如果我註釋掉這個函數並用簡單的警報(A)替換它,那麼一切都會好起來。 – viktor

+0

我已經添加了整個代碼 – viktor

回答

-1

撒母耳指出的那樣,你需要調用calcRec calcDet(這是由onclick事件調用)內。你現在正在做的是在函數內部定義一個函數。

它看起來像你試圖在Javascript中實現this(你遺留了一些工件,比如在之前使用「A」的底部使用變量「矩陣」)。如果是這樣的目標,這應該是非常接近你所追求的:

function calcDet() { 
    var A = []; //generates the array 
    for (var i = 0; i < k; i++) { 
     A[i] = []; 
     for (var j = 0; j < k; j++) { 
      A[i][j] = i + ':' + j; 
     } 
    } 
    for (var i = 0; i < k; i++) { //reads the array 
     for (var j = 0; j < k; j++) { 
      var id = "A" + (i + 1) + (j + 1); 
      A[i][j] = parseFloat(document.getElementById(id).value); 
     } 
    } 

    function calcRec(A) { 
     var s; 
     var det = 0; 
     if (A.length == 1) { //bottom case of the recursive function 
      return A[0][0]; 
     } 
     if (A.length == 2) {  
      det = A[0][0] * A[1][1] - A[1][0] * A [0][1]; 
      return det; 
     } 
     for (var i = 0; i < k; i++) { 
      //creates smaller matrix- values not in same row, column 
      var smaller = new Array(A.length - 1); 
      for (h = 0; h < smaller.length; h++) { 
       smaller[h] = new Array(A.length - 1); 
      } 
      for (a = 1; a < A.length; a++) { 
       for (b = 0; b < A.length; b++) { 
        if (b < i) { 
         smaller[a - 1][b] = A[a][b]; 
        } else if (b > i) { 
         smaller[a - 1][b - 1] = A[a][b]; 
        } 
       } 
      } 
      if (i % 2 == 0) { 
       s = 1; 
      } else { 
       s = -1; 
      } 
      det += s * A[0][i] * (calcRec(smaller)); 
     } 
     return (det); 
    } 
    alert(calcRec(A)); 
} 

你的「我」循環的範圍爲打造「小」數組是錯誤的,「DET」的初始化是「calcRec」之外可能導致一些未定義的行爲。

+0

是的。我知道這個鏈接,那是我試圖重現的algorythm。但是我現在太累了,明天我會看看這個。謝謝 – viktor