# To compute the determinant of a k by k matrix m. k > 0 The matrix is corrupted. # PROC first = (PROC(INT)BOOL p)INT: ( PROC r = (INT j)INT: (p(j) | j | r(j+1)); r(1)); MODE R = REAL; PROC det = (REF [,]R m)R: (INT k = 1 UPB m; IF k /= 2 UPB m THEN print("not square"); GOTO hell FI; IF k = 1 THEN m[1,1] ELIF INT j = first((INT z)BOOL: (z > k ORF m[z, 1] /= 0)); j>k THEN 0 ELSE IF j /= 1 THEN FOR n TO k DO R t = m[j, n]; m[j, n] := m[1, n]; m[1, n] := -t OD FI; R t = 1.0 / m[1,1]; FOR i FROM 2 TO k DO REF []R q = m[i,]; R a = t*q[1]; FOR r FROM 2 TO k DO q[r] -:= a*m[1, r] OD OD; m[1,1] * det(m[2:k, 2:k]) FI); # To test: # PROC t = ([,]R m)VOID: (INT k = 1UPB m; ([k,k]R z; FOR i TO k DO FOR j TO k DO z[i,j] := m[j,i] OD OD; print((det(LOC [k,k]R := m), " ", det(z), newline)))); t(((3.75))); #3.75# t(((1, 0), (0, 1))); #1# t(((1, 0, 0), (0, 1, 0), (0, 0, 1))); #1# t(((2, 3), (5, 4))); #-7# t(((0, 1, 0), (1, 0, 0), (0, 0, 1))); #-1# t(((3.3, 5.6, 4.8, 4.3), (5.6, 4.2, 6.7, 5.2), (7.6, 5.3, 6.6, 4.3), (2.6, 4.8, 4.9, 9.6))); #206.704# t(((0, 2, 3), (0, 3, 6), (0, 5, 2))); #0# hell: SKIP