-->

Javascript matrix inversion

2019-06-07 06:52发布

问题:

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.

回答1:

You can also take a look at my (work-in-progress) library matrix.js that supports matrices of any dimension (if you don't need that, you can stop reading here as arbitrary sizes add extreme overhead).

The relevant code for inversion is

Matrix.prototype.inverse = function () {
    if( !this.isSquare() ) {
        throw new MatrixError( MatrixError.ErrorCodes.DIMENSION_MISMATCH, 'Matrix must be square' );
    }

    var M = this.augment( Matrix.eye( this.rows() ) ),
        row, row_before, new_row, i, j, k, factor, rows, columns;

    try {
        M = M.decomposeLU();
        rows = M.rows();
        columns = M.columns();

        for( i = rows; i > 1; i-- ) {
            row_before = M.__getRow( i - 1 );
            row = M.__getRow( i );
            factor = row_before[i - 1] / row[i - 1];

            new_row = [];
            for( k = 0; k < columns; k++ ) {
                new_row[k] = row_before[k] - row[k] * factor;
            }
            M.__setRow( i - 1, new_row );
        }

        for( j = 1; j <= rows; j++ ) {
            row = M.__getRow( j );
            new_row = [];

            for( k = 0; k < columns; k++ ) {
                new_row[k] = row[k] / row[j - 1];
            }

            M.__setRow( j, new_row );
        }
    } catch( e ) {
        throw new MatrixError( MatrixError.ErrorCodes.MATRIX_IS_SINGULAR );
    }

    return M.submatrix( 1, rows, this.columns() + 1, columns );
};

However, you can see it has some dependencies to a LU decomposition, for example. If you're interested, take a look at it. The inverse is not exactly an optimal solution so far, but rather basic.