How to calculate the determinant of a javascript matrix?

Asked

Viewed 2,189 times

11

Ex.: det([-1, -2, 3], [3, 3, 1], [-1, 2, -3]) // retorna 22

Ex.: det([1, 2], [2, 5]) // retorna 1

Ex.: det([8]) // retorna 8

  • 3

    The question is about the mathematics of calculus, or about the implementation in Javascript?

  • 1

    The implementation in javascript

  • 2

    These links can be of some help: http://paczynski.net/testarea/matrix.html and http://numericjs.com/

2 answers

12


I created the following function in 2010, but made some improvements recently:

function determinante(a) {
    var ordem = a.length;

    if (ordem === 1) {
        return a[0][0];
    }

    if (ordem === 2) {
        return a[0][0] * a[1][1] - a[0][1] * a[1][0];
    }

    var det = 0;

    for (var j = 0; j < ordem; j++) {
        det += a[0][j] * cofator(a, 0, j);
    }

    return det;
}

function cofator(a, linha, coluna) {
    var sub_matriz = [],
        ordem = a.length,
        m = 0;

    for (var i = 1; i < ordem; i++) {
        sub_matriz[m] = [];

        for (var j = 0; j < ordem; j++) {
            if (j !== coluna) {
                sub_matriz[m].push(a[i][j]);
            }
        }
        m++;
    }

    //return Math.pow(-1, linha + coluna) * determinante(sub_matriz);
    return (coluna % 2 ? -1 : 1) * determinante(sub_matriz);
}



The function determinante is indirect recursive (A calls B, B calls A) and uses the Laplace’s theorem to calculate the determinant of a square matrix. The theorem is applied always in the first row of the matrix and not in the row or column with the largest number of zeros, as we do on paper.

Examples of use:

determinante([[1,-2,8,4,-3,1], [-2,-9,3,1,4,-4], [3,0,8,7,0,2], [5,7,1,2,5,-1], [7,-8,-6,4,0,-5], [0,-2,-8,0,0,1]])
// Retorna 1560

Check in: http://www.wolframalpha.com/input/? i=det+%28%5B1%2C-2%2C8%2C4%2C-3%2C1%5D%2C+%5B-2%2C-9%2C3%2C1%2C-4%5D%2C+%5B3%2C0%2C0%2C8%2C0%2C0%2C2%5D%2C+%5B5%2C7%2C1%2C2%2C5%2C-1%5D%2C+%5B7%2C-8%2C-6%2C%2C0%2C-5%5D%2C+%5D%2B0%2C-2%2C-2%2C-8%2C0%2C%5D%


determinante([[1,2,3], [4,5,6], [7,8,1]]);
// Retorna 24

Check in: http://www.wolframalpha.com/input/? i=+det+%28%5B1%2C2%2C3%5D%2C+%5B4%2C5%2C6%5D%2C+%5B7%2C8%2C1%5D%29


determinante([[-1,-2,3], [3,3,1], [-1,2,-3]]);
// Retorna 22

Check in: http://www.wolframalpha.com/input/? i=det%28%5B-1%2C-2%2C3%5D%2C+%5B3%2C3%2C1%5D%2C+%5B-1%2C2%2C-3%5D%29


determinante([[8]]);
// Retorna 8

determinante([
    [2, 1, 2, 3, 4, 5, 6, 7, 2, 3],
    [0, 2, 0, 0, 0, 0, 0, 2, 3, 2],
    [0, 0, 4, 5, 5, 5, 2, 3, 2, 1],
    [0, 0, 0, 2, 0, 2, 3, 2, 5, 2],
    [0, 0, 0, 0, 4, 3, 2, 0, 5, 3],
    [0, 0, 0, 0, 0, 1, 0, 0, 5, 4],
    [0, 0, 0, 0, 0, 0, 4, 0, 5, 5],
    [0, 0, 0, 0, 0, 0, 0, 2, 5, 6],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 7],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 2]
]);
// Retorna 2048

FIDDLE: http://jsfiddle.net/E9F4E/

Note: in fact, the implementation using Laplace’s theorem is not the most efficient, but is easy to visualize and works well for relatively small matrices (order < 11).

The most efficient way to calculate the determinant would be the scheduling method. See this article. The only one however in this method is a small loss of numerical accuracy. For example, instead of 1560, the function returns 1559.9999999723.

4

Direct determinant calculation by cofactors is didactic but rather inefficient if you want to use large matrices seriously.

A better option would be to calculate LU decomposition. Since the matrices L and U are triangular, the determinant of each is simply the multiplication of the main diagonal elements. If A=LU then |A|=|L||U|.

The best thing would be to use the Sylvester.js library http://sylvester.jcoglan.com/api/matrix.html#determinant

Browser other questions tagged

You are not signed in. Login or sign up in order to post.