# Evaluation of determinants

knitr::opts_chunk$set( warning = FALSE, message = FALSE ) options(digits=4) par(mar=c(5,4,1,1)+.1) This example shows two classical ways to find the determinant,$\det(A)$of a square matrix. They each work by reducing the problem to a series of smaller ones which can be more easily calculated.  library(matlib) ## 1. Calculate det() by cofactor expansion Set up a$3 \times 3$matrix, and find its determinant (so we know what the answer should be).  A <- matrix(c(4, 2, 1, 5, 6, 7, 1, 0, 3), nrow=3, byrow=TRUE) det(A) ### Find cofactors of row 1 elements The cofactor$A{i,j}$of element$a{i,j}$is the signed determinant of what is left when row i, column j of the matrix$A$are deleted. NB: In R, negative subscripts delete rows or columns.  cat(cofactor(A, 1, 1), " == ", 1 * det( (A[-1, -1]), "\n" )) cat(cofactor(A, 1, 2), " == ", -1 * det( (A[-1, -2]), "\n" )) cat(cofactor(A, 1, 3), " == ", 1 * det( (A[-1, -3]), "\n" )) ### det() = product of row with cofactors In symbols:$\det(A) = a{1,1} * A{1,1} + a{1,2} * A{1,2} + a{1,3} * A{1,3}$rowCofactors() is a convenience function, that calculates these all together  rowCofactors(A, 1) Voila: Multiply row 1 times the cofactors of its elements. NB: In R, this multiplication gives a$1 \times 1$matrix.  A[1,] %*% rowCofactors(A, 1) all.equal( det(A), c(A[1,] %*% rowCofactors(A, 1)) ) ## 2. Finding det() by Gaussian elimination (pivoting) This example follows Green and Carroll, Table 2.2. Start with a 4 x 4 matrix,$M\$, and save det(M).

 M <- matrix(c(2, 3, 1, 2, 4, 2, 3, 4, 1, 4, 2, 2, 3, 1, 0, 1), nrow=4, ncol=4, byrow=TRUE) (dsave <- det(M)) # ### 'pivot' on the leading diagonal element, M[1,1]:

det() will be the product of the 'pivots', the leading diagonal elements. This step reduces row 1 and column 1 to 0, so it may be discarded. NB: In R, dropping a row/column can change a matrix to a vector, so we use drop = FALSE inside the subscript.

 (d <- M[1,1]) #-- Reduce row 1, col 1 to 0 (M[1,] <- M[1,, drop=FALSE] / M[1, 1]) (M <- M - M[,1] %*% M[1,, drop=FALSE]) #-- Drop first row and column M <- M[-1, -1] #-- Accumulate the product of pivots d <- d * M[1, 1]

### Repeat, reducing new row, col 1 to 0

 (M[1,] <- M[1,, drop=FALSE] / M[1,1]) (M <- M - M[,1] %*% M[1,, drop=FALSE]) M <- M[-1, -1] d = d * M[1, 1]

### Repeat once more. d = det(M)

 (M[1,] <- M[1,, drop=FALSE] / M[1,1]) (M <- M - M[,1] %*% M[1,, drop=FALSE]) M <- M[-1, -1, drop=FALSE] d <- d * M[1, 1] # did we get it right? all.equal(d, dsave)
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.rdocumentation.org/packages/matlib/versions/0.9.2/vignettes/det-ex2.Rmd", "fetch_time": 1582930142000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2020-10/segments/1581875147647.2/warc/CC-MAIN-20200228200903-20200228230903-00536.warc.gz", "warc_record_offset": 856652744, "warc_record_length": 5419, "token_count": 953, "char_count": 2512, "score": 4.15625, "int_score": 4, "crawl": "CC-MAIN-2020-10", "snapshot_type": "latest", "language": "en", "language_score": 0.6498708724975586, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00022-of-00064.parquet"}