I am creating a javascript code for matrix inversion.But the function doesn't seem to run. I want my inverted matrix to be displayed where the input matrix was.I have tried alerting values of the invertedMatrix instead of putting them into s but that didn't work either.Would be grateful for any help
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;
}
}
EDIT!:if condition check should have i == 0 || i == 2 instead of what i've written.But still won't work anyway.