1
我正在爲矩陣求逆創建一個JavaScript代碼,但函數似乎並未運行。 我要顯示我倒矩陣,輸入矩陣was.I試圖提醒invertedMatrix的值,而不是將它們放入秒,但沒有奏效either.Would是任何幫助JavaScript矩陣求逆
HTML
感激<div id = "table3">
<div class = "header">Macierz odwrotna [2x2]</div>
<form id = "row1">
<input type = "text" class = "det2"/><!--first row-->
<input type = "text" class = "det2"/>
</form>
<form id = "row2">
<input type = "text" class = "det2"/><!--second row-->
<input type = "text" class = "det2"/>
</form>
<div class = "count" onclick="invertedMatrix(2,'det2')"><a href = "#">Wylicz</a> </div>
</div>
的JavaScript
function det(size, className){
var arr = document.getElementsByClassName(className);
var determinant = 0;
if(size == 2){
determinant = (arr[0].value*arr[3].value) - (arr[1].value*arr[2].value);
}
else if(size == 3){
determinant = (arr[0].value*((arr[4].value*arr[8].value) - (arr[5].value * arr[7].value))) -
(arr[1].value*((arr[3].value*arr[8].value) - (arr[5].value * arr[6].value))) +
(arr[2].value*((arr[3].value*arr[7].value) - (arr[4].value * arr[6].value)));
}
return determinant;
}
function invertedMatrix(size,className){
var invertedMatrix = new Array();
var additionalMatrix = new Array();
var matrix = document.getElementsByClassName(className);
if(size == 2){
for(var i = 0; i < matrix.length;i++){
if(i % 2 == 0){
additionalMatrix[i].value = matrix[i].value;
}
else{
additionalMatrix[i].value = -matrix[i].value;
}
}
for(var i = 0;i < matrix.length;i++){
invertedMatrix[i].value = (1/det(2,className)) * additionalMatrix[i].value;
}
}
for(var i = 0;i < matrix.length; i++){
document.getElementsByClassName(className).item(i).value = invertedMatrix[i].value;
}
}
編輯:!如果條件檢查應該有我== 0 ||我== 2,而不是我寫的東西,但仍然無法工作。
我認爲你確實需要將讀取矩陣的代碼從DOM中分離出來,並根據計算行列式和逆矩陣的代碼將其寫入DOM。這太糾結了。 – 2013-05-10 20:19:10