JS coding - Episode 49: Calculate the Matrix Inverse of integer numbers (N*N)

Print the values of your Matrix Inverse our return the Matrix Inversa as you want. Your code must be able to work with any Matrix dimensions. But for this challenge you can test a Matrix 3 * 3 and 4 * 4

Category Coding

Hello, the problem of the day.

 

Calculate the Matrix Inverse of integer numbers (N*N).

Explanation of the exercice:
http://www.purplemath.com/modules/mtrxinvr.htm4

Function Name: matInverse(mat) {}

console.log(matInverse([ [1, 3, 3], [1, 4, 3], [1, 3, 4] ]));

Finally: Print the values of your Matrix Inverse our return the Matrix Inversa as you want. Your code must be able to work with any Matrix dimensions. But for this challenge you can test a Matrix 3 * 3 and 4 * 4.

Instructions:

Inside your code you should be able to create the identity Matrix that you’ll need to use to calculate the inverse.

You should use a Matrix identity or if you know another way to calculate the inverse you can use it’s up to you.

I recommend you don’t use mutations of row or columns because this will increase the difficult level. But If you want use mutations you can use.

You must do zero above and below the main diagonal. For do this you can use method of Gauss-Jordan.

Please Note: Try to understand the method don’t try to write code without understand the method before, because you will waste your time.

 

 

My solution:

function getDeterminant(matrix) {
    if (matrix.length === 1) return matrix[0];
    if (matrix.length === 2) return getSmallDeterminant(matrix);
    let i = getLineWithMaxZero(matrix);
    let sum = 0;
    for (let j = 0; j < matrix.length; j++) {
        let factor = matrix[i][j];
        if (factor === 0) continue;
        if ((i+j) % 2 === 1) factor *= -1;
        var det_ = getDeterminant(getNewMatrix(matrix,i,j));
        sum += factor*det_;
    }
    return sum;
}

function getNewMatrix(matrix, i_, j_) {
    var result = [];
    for (let i = 0; i < matrix.length; i++){
        if (i === i_) continue;
        var lineArray = [];
        for (let j = 0; j < matrix[i].length; j++) {
            if (j === j_) continue;
            lineArray.push(matrix[i][j]);
        }
        result.push(lineArray);
    }
    return result;
}

function getSmallDeterminant(matrix) {
    var result = 0;
    result += matrix[0][0] * matrix[1][1];
    result -= matrix[1][0] * matrix[0][1];
    return result;
}
function getLineWithMaxZero(matrix) {
    let anz = 0;
    let line = 0;
    for (let i = 0; i < matrix.length; i++) {
        let tempAnz = 0;
        for (let j = 0; j < matrix[i].length; j++) {
            if (matrix[i][j] === 0) tempAnz++;
        }
        if (tempAnz > anz) {
            anz = tempAnz;
            line = i;
        }
    }
    return line;
}
function getCof(matrix, i, j) {
    let factor = 1;
    if ((i + j) % 2 === 1) factor *= -1;
    if (matrix.length === 2) {
        if (i === 0 && j === 0) return factor * matrix[1][1];
        if (i === 0 && j === 1) return factor * matrix[1][0];
        if (i === 1 && j === 0) return factor * matrix[0][1];
        if (i === 1 && j === 1) return factor * matrix[0][0];
    }
    return factor * getDeterminant(getNewMatrix(matrix,i,j));
}
function getCofMatrix(matrix) {
    var result = [];
    for (let i = 0; i < matrix.length; i++) {
        var lineArray = [];
        for (let j = 0; j < matrix[i].length; j++) {
            lineArray.push(getCof(matrix,i,j));
        }
        result.push(lineArray);
    }
    return result;
}

function transportMatrix(matrix) {
    var result = [];
    for (let i = 0; i < matrix.length; i++) {
        var lineArray = [];
        for (let j = 0; j < matrix[i].length; j++) {
            lineArray.push(matrix[j][i]);
        }
        result.push(lineArray);
    }
    return result;
}
function multiplyMatrix(matrix, factor) {
    var result = [];
    for (let i = 0; i < matrix.length; i++) {
        var lineArray = [];
        for (let j = 0; j < matrix[i].length; j++) {
            lineArray.push(factor*matrix[i][j]);
        }
        result.push(lineArray);
    }
    return result;
}
function clearZeros(matrix) {
    var result = [];
    for (let i = 0; i < matrix.length; i++) {
        var lineArray = [];
        for (let j = 0; j < matrix[i].length; j++) {
            let n=matrix[i][j];
            if(n===-0) n=0;
            lineArray.push(n);
        }
        result.push(lineArray);
    }
    return result;
}
function matInverse(matrix) {
    return clearZeros(multiplyMatrix(transportMatrix(getCofMatrix(matrix)),1/getDeterminant(matrix)));
}
console.log(matInverse([[1, 3, 3], [1, 4, 3], [1, 3, 4]]));