'************************************************************ '* This program calculates the determinant of a real square * '* matrix using Function FindDet (see ref. below). * '* -------------------------------------------------------- * '* SAMPLE RUN: * '* * '* Calculate the deteminant of square matrix: * '* * '* ! 0 -1 -1 -1 ! * '* ! 1 0 -1 -1 ! * '* ! 1 1 0 -1 ! * '* ! 1 1 1 0 ! * '* * '* Determinant = 1.000000 * '* * '* Basic Version By J-P Moreau, Paris. * '************************************************************ DEFINT I-N DEFDBL A-H, O-Z N = 4 DIM A(N, N) A(1, 1) = 0: A(1, 2) = 1: A(1, 3) = 1: A(1, 4) = 1 A(2, 1) = -1: A(2, 2) = 0: A(2, 3) = 1: A(2, 4) = 1 A(3, 1) = -1: A(3, 2) = -1: A(3, 3) = 0: A(3, 4) = 1 A(4, 1) = -1: A(4, 2) = -1: A(4, 3) = -1: A(4, 4) = 0 GOSUB 1000 PRINT PRINT " Determinant ="; D PRINT END '---------------------------------------------------------------------------------------------------- 'Function to find the determinant of a square matrix 'Author : Louisda16th a.k.a Ashwith J. Rego 'Description: The subroutine is based on two key points: '1) A determinant is unaltered when row operations are performed: Hence, using this principle, 'row operations (column operations would work as well) are used 'to convert the matrix into upper traingular form '2)The determinant of a triangular matrix is obtained by finding the product of the diagonal elements '---------------------------------------------------------------------------------------------------- 1000 'FUNCTION FindDet(A, N) DIM m AS DOUBLE DIM DetExists AS INTEGER l = 1 'Convert to upper triangular form FOR k = 1 TO N - 1 IF (A(k, k) = 0) THEN DetExists = 0 FOR i = k + 1 TO N IF (A(i, k) <> 0) THEN FOR j = 1 TO N temp = A(i, j) A(i, j) = A(k, j) A(k, j) = temp NEXT DetExists = 1 l = -l GOTO 10 END IF NEXT 10 IF (DetExists = 0) THEN D = 0 RETURN END IF END IF FOR j = k + 1 TO N m = A(j, k) / A(k, k) FOR i = k + 1 TO N A(j, i) = A(j, i) - m * A(k, i) NEXT NEXT NEXT 'Calculate determinant by finding product of diagonal elements D = l FOR i = 1 TO N D = D * A(i, i) NEXT RETURN 'end of file tfinddet.bas