 Programming 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*M)-(M*M); } // 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 * determinant of the 2x2 matrix that is excluded from M's row and column. (2) M * determinant of the 2x2 matrix that is excluded from M's row and column. (3) M * determinant of the 2x2 matrix that is excluded from M'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*M)-(M*M); } return M*det(deleteRowAndColumn(M,0)) - M*det(deleteRowAndColumn(M,1)) + M*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*M)-(M*M); } 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*M)-(M*M); } var answer = 0; for (var i=0; i< M.length; i++) { answer += Math.pow(-1,i)*M[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. Challenges About Sign Up/Login Blog Social Privacy Terms Contact Membership ```
``` var quill_comment = new Quill('#answer_text_area', { modules: { toolbar: [ ['code-block'] ] }, theme: 'snow' }); // remove formatting when copied from somewhere else // https://github.com/quilljs/quill/issues/1184 quill_comment.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => { let ops = [] delta.ops.forEach(op => { if (op.insert && typeof op.insert === 'string') { ops.push({ insert: op.insert }) } }); delta.ops = ops; return delta; }); \$(document).ready(function() { // save quill div content to textarea so POST can get it \$('#submit_ques_ans').hover(function() { var customContent = \$('#answer_text_area').find('.ql-editor').html(); \$('#answer_text_area_actual').val(customContent); }); // no answers yet \$('#all_ans_title').html('<p style="padding-top:7px;color:#888888;font-size:14px;">No comments yet</p>'); // modify votes \$(".pos_vote_sign").click(function() { }); \$(".neg_vote_sign").click(function() { }); }); ```