矩阵行列式计算器的递归函数

时间:2012-05-14 17:21:16

标签: javascript

我正在玩数组,我尝试制作一个递归函数来计算矩阵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 个答案:

没有答案