Programming Tutorials

  • View all tutorials
  • Determinant of a matrix in JavaScript using Laplace expansion

    Determinant of 2x2 matrix

    I will be showing you how to write a function to calculate the determinant of a square matrix of any size. Let M represent the following 2x2 matrix. var M = [ [a,b], [c,d] ]; The following is the procedure to calculate the determinant of a 2x2 matrix. det(M) = (a*d)-(b*c); Now, we'll substitute the following values for the variables and write the basic det() function to calculate the determinant of a 2x2 matrix. var M = [ [1,2], [3,4] ]; function det(M) { return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]); } // output = -2

    Determinant of 3x3 matrix

    The determinant of a 3x3 matrix becomes a bit trickier now. Let M be the following. var M = [ [a,b,c], [d,e,f], [g,h,i] ]; The following is the procedure to calculate the determinant of a 3x3 matrix. det(M) = a(ei-fh)-b(di-fg)+c(dh-eg); // notice the alternating signs The algorithm for calculating the determinant of a 3x3 matrix is essentially the following: (1) M[0][0] * determinant of the 2x2 matrix that is excluded from M[0][0]'s row and column. (2) M[0][1] * determinant of the 2x2 matrix that is excluded from M[0][1]'s row and column. (3) M[0][2] * determinant of the 2x2 matrix that is excluded from M[0][2]'s row and column. The algorithm I'm explaining is called Laplace expansion. It technically doesn't need to take the values from the first row of the matrix and multiply them by the remainder matrices-it can actually work if you use any row. This method is also very inefficient for matrices of large size. There are much more efficient ways of calculating the determinant of large square matrices. All of this is explained in the link above. So now to update our det() function to work with 3x3 matrices. We'll rewrite the function to work recursively and our base case will be if M is a 2x2 matrix, which in that case is very simple to calculate the determinant. We'll also need to write a function that deletes a specific row and column from a matrix in order to continue with the aforementioned algorithm, and for this we'll use the JavaScript splice function. var M = [ [1,2,3], [4,5,6], [7,8,9] ]; function det(M) { if (M.length==2) { return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]); } return M[0][0]*det(deleteRowAndColumn(M,0)) - M[0][1]*det(deleteRowAndColumn(M,1)) + M[0][2]*det(deleteRowAndColumn(M,2)); } function deleteRowAndColumn(M,index) { var temp = []; // copy the array first for (var i=0; i Now let's just abstract the code in det(M) into a loop rather than writing out the three separate parts. The Math.pow() function is required in order to determine the sign of the respective part since we aren't explicitly writing out minus or plus. function det(M) { if (M.length==2) { return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]); } var answer = 0; for (var i=0; i

    Determinant of 4x4 and larger matrices

    The pattern simply continues now for larger matrices. Let M be the following matrix. var M = [ [a,b,c,d], [e,f,g,h], [i,j,k,l], [m,n,o,p] ]; The determinant of M will therefore be: det(M) = a*det([[f,g,h],[j,k,l],[n,o,p]])-b*det([[e,g,h],[i,k,l],[m,o,p]])+... So because our code from above runs recursively, it should actually work for a matrix of any size 4x4 or larger. Below is the final code to determine the determinant of any size square matrix. var M = [ [1,2,3,4], [5,6,7,8], [9,1,2,3], [4,5,9,7] ]; function det(M) { if (M.length==2) { return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]); } var answer = 0; for (var i=0; i< M.length; i++) { answer += Math.pow(-1,i)*M[0][i]*det(deleteRowAndColumn(M,i)); } return answer; } function deleteRowAndColumn(M,index) { var temp = []; for (var i=0; i Live Demo
    mrdaniel wrote this tutorial on 12/24/15 | javascript, matrix
    Comments
    Log in to submit a comment.