我正在为矩阵反转创建一个 javascript 代码。但该函数似乎没有运行。我希望我的倒置矩阵显示在输入矩阵所在的位置。我尝试过提醒倒置矩阵的值而不是将它们放入 s 中,但这也不起作用。如果有任何帮助,我将不胜感激
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;
}
}
编辑!:如果条件检查应该有 i == 0 || i == 2 而不是我所写的。但仍然无法正常工作。