3

I'm trying to compute determinant of a matrix in JS. I used algorithm from http://www.sanfoundry.com/java-program-compute-determinant-matrix/ but I lost my mind on the last condition. I just do not understand. Can you help me?

This is how looks like my code right now. In another function I create an empty 2d array and then copy it to det function. Next I retrieve values from html and then trying to compute determinant of a matrix. The first 2 cases are simple but I have a problem with the last one. I couldn't find working example in JS.

function det() {
  var det = 0;
  var array1 = array.slice();

  for (i = 0; i < array1.length; i++) {

    for (j = 0; j < array1[i].length; j++) {
      array1[i][j] = parseInt(document.getElementById("element" + (i + 1) + (j + 1)).value, 10);
    }

  }

  if (array1.length == 1) {
    det = array1[0][0];
  } else if (array1.length == 2) {
    det = (array1[0][0] * array1[1][1]) - (array1[1][0] * array1[0][1]);
  } else {

  }

}
sa77
  • 3,563
  • 3
  • 24
  • 37
kureva
  • 65
  • 1
  • 7
  • in the last condition , you create a sub matrix which is `array1` deleting the row and column at `a0j` where `j` is from `0` to `N`, you multiply `a0j` with `det(subarray)` and the sum of the products is the final `determinant` , that is the definition of `determinant`, the code written before the recursive call is just filling the subarray that's all – niceman Jun 10 '17 at 15:33
  • Recursion is nice, but be warned with js https://stackoverflow.com/questions/37224520/are-functions-in-javascript-tail-call-optimized – Richard Tyler Miles Nov 23 '21 at 05:24

3 Answers3

11

I may suggest my solution, based on recursive algorithm, which takes only few lines of code and, I guess, will suit most of practical applications:

const determinant = m => 
  m.length == 1 ?
  m[0][0] :
  m.length == 2 ? 
  m[0][0]*m[1][1]-m[0][1]*m[1][0] :
  m[0].reduce((r,e,i) => 
    r+(-1)**(i+2)*e*determinant(m.slice(1).map(c => 
      c.filter((_,j) => i != j))),0)

const test1 = [[3]]                      // 3
const test2 = [[3,-2],[7,4]]             // 26
const test3 = [[1,3,7],[2,-1,4],[5,0,2]] // 81

console.log(determinant(test1))
console.log(determinant(test2))
console.log(determinant(test3))
.as-console-wrapper {min-height: 100%}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
1

I created a Matrix class with some function for the basic operations, one of them the determinant calculation

Here we have the constructor

constructor(rows,cols) {
    this.rows = rows;
    this.cols = cols;
    this.vals = new Array(rows*cols);
    for(let i = 0; i < this.vals.length; i++) this.vals[i] = 0;
}

and here the determinant function

determinant() {
    if (this.rows != this.cols ) {
        console.log("Matrix must be square");
        return;
    }
    let size = this.rows;
    if (size == 1) return this.vals[0];
    if (size == 2) return this.vals[0]*this.vals[3]-this.vals[1]*this.vals[2];
    let sign = 1;
    let result = 0;
    for (let k = 0 ; k < size ; k++){
        let cofactors = new Matrix(size-1,size-1);
        cofactors.vals = this.vals.slice(size,size*size).filter((_,index)=>index%size!=k);
        result += sign * this.vals[k] * cofactors.determinant();
        sign*=(-1);
    }
    return result;
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '22 at 04:17
0

You can see definition of the determinant for square matrix here https://en.wikipedia.org/wiki/Determinant#n_.C3.97_n_matrices. Algorithm used in http://www.sanfoundry.com/java-program-compute-determinant-matrix/ use some properties of determinat to calculate it in recursive way as sum over all permutations. In this way you get N * N! operations! It is very big even for small N.

For solving this problem you can first transform matrix to triangular with the same determinant and after that calculate determinant as product of all diagonal elements.

ivan kuklin
  • 140
  • 2
  • 8