numbers / numbers.js

Advanced Mathematics Library for Node.js and JavaScript
Apache License 2.0
1.77k stars 167 forks source link

Determinant is wrong for matrix n > 3 #160

Open plouffed opened 3 years ago

plouffed commented 3 years ago

From the source code, we can see that implemented method to calculate the determinant for dimension n>2 is

  for (col = 0; col < numCol; col++) {
      diagLeft = m[0][col];
      diagRight = m[0][col];

      for (row = 1; row < numRow; row++) {
        diagRight *= m[row][(((col + row) % numCol) + numCol) % numCol];
        diagLeft *= m[row][(((col - row) % numCol) + numCol) % numCol];
      }

      det += diagRight - diagLeft;
    }

It is pretty clear that this code represents Leibniz formula for a 3x3 matrix (according to wikipedia).

So it means that this piece of code cannot compute determinant of matrix n > 3.

For exemple:

    let m4x4 = [[2,3,4,6],[1,3,-2,4],[5,2,1,3],[1,5,2,-3]];
    numbers.matrix.determinant(m4x4) // expected 825, gives 534

    let m5x5 = [[2,2,3,4,6],[1,3,3,-2,4],[5,4,2,1,3],[1,4,2,0,3],[9,4,8,9,10]];
    numbers.matrix.determinant(m5x5); // expected 793, gives 2187