title 'IMLDET2: Evaluation of determinants'; proc iml; reset print log; *--define a function module to compute cofactor; start cofactor(A,i,j); reset noprint; size = nrow(A); *size of matrix; rows = loc( (1:size) ^= i ); *delete row i; cols = loc( (1:size) ^= j ); *delete col j; minor = det( A[rows,cols] ); reset print; return ( ((-1)##(i+j)) # minor ); finish; *-- 1. det by cofactor expansion; A ={4 2 1, 5 6 7, 1 0 3}; r = det(A); *-- cofactors of row 1 elements; print (cofactor(A,1,1)) (A[{2 3},{2 3}]); print (cofactor(A,1,2)) (A[{2 3},{1 3}]); print (cofactor(A,1,3)) (A[{2 3},{1 2}]); *-- det = product of row with cofactors; r = A[1,] * {18, -8, -6}; *-- 2. det by Gaussian elimination (pivoting); * [cf. Green and Carroll, table 2.2]; M = {2 3 1 2, 4 2 3 4, 1 4 2 2, 3 1 0 1}; d = det(M); *-- pivot on m[1,1] -> det = product of pivots; D=M[1,1]; *-- Reduce row, col 1 to 0; M[1,] = M[1,] / M[1,1]; M = M - M[,1] * M[1,]; *-- Drop first row and column; M = M[{2 3 4},{2 3 4}]; *-- Accumulate product of pivots; D = D # M[1,1]; *--Repeat, reducing new row, col 1 to 0; M[1,] = M[1,] / M[1,1]; M = M - M[,1] * M[1,]; M = M[{2 3},{2 3}]; D = D # M[1,1]; *--Repeat once more. d = det(m); M[1,] = M[1,] / M[1,1]; M = M - M[,1] * M[1,]; M = M[2,2]; D = D # M[1,1]; quit;