DECLARE SUB CDIV (Z1#(), Z2#(), Z#()) DECLARE SUB CMUL (Z1#(), Z2#(), Z#()) '************************************************************ '* This program calculates the determinant of a complex * '* square matrix using Function CFindDet (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, 0.000000) * '* * '* -------------------------------------------------------- * '* * '* Basic Version By J-P Moreau, Paris. * '************************************************************ DEFINT I-N DEFDBL A-H, O-Z n = 4 DIM A(n, n, 2) 'Complex matrix of size N*N DIM D(2) 'Complex determinant A(1, 1, 1) = 0!: A(1, 1, 2) = 0! A(2, 1, 1) = 1!: A(2, 1, 2) = 0! A(3, 1, 1) = 1!: A(3, 1, 2) = 0! A(4, 1, 1) = 1!: A(4, 1, 2) = 0! A(1, 2, 1) = -1!: A(1, 2, 2) = 0! A(2, 2, 1) = 0!: A(2, 2, 2) = 0! A(3, 2, 1) = 1!: A(3, 2, 2) = 0! A(4, 2, 1) = 1!: A(4, 2, 2) = 0! A(1, 3, 1) = -1!: A(1, 3, 2) = 0! A(2, 3, 1) = -1!: A(2, 3, 2) = 0! A(3, 3, 1) = 0!: A(3, 3, 2) = 0! A(4, 3, 1) = 1!: A(4, 3, 2) = 0! A(1, 4, 1) = -1!: A(1, 4, 2) = 0! A(2, 4, 1) = -1!: A(2, 4, 2) = 0! A(3, 4, 1) = -1!: A(3, 4, 2) = 0! A(4, 4, 1) = 0!: A(4, 4, 2) = 0! GOSUB 1000 'D = CFindDet(A, N) PRINT PRINT " Determinant = ("; D(1); ","; D(2); ")" 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 CFindDet(A, n) DIM cm(2), temp(2), temp1(2), CONE(2), CZERO(2) 'Complex numbers DIM DetExists AS INTEGER 'Boolean DIM FALSE, TRUE AS INTEGER l = 1 FALSE = 0: TRUE = 1 DetExists = TRUE CZERO(1) = 0: CZERO(2) = 0 CONE(1) = 1!: CONE(2) = 0! 'Convert to upper triangular form FOR k = 1 TO n - 1 IF A(k, k, 1) = CZERO(1) AND A(k, k, 2) = CZERO(2) THEN DetExists = FALSE FOR i = k + 1 TO n IF A(i, k, 1) <> CZERO(1) OR A(i, k, 2) <> CZERO(2) THEN FOR j = 1 TO n temp(1) = A(i, j, 1): temp(2) = A(i, j, 2) A(i, j, 1) = A(k, j, 1): A(i, j, 2) = A(k, j, 2) A(k, j, 1) = temp(1): A(k, j, 2) = temp(2) NEXT j DetExists = TRUE l = -l GOTO 10 END IF NEXT i 10 IF DetExists = FALSE THEN D(1) = CZERO(1): D(2) = CZERO(2) RETURN END IF END IF FOR j = k + 1 TO n 'm = A(j,k)/A(k,k) temp(1) = A(j, k, 1): temp(2) = A(j, k, 2) temp1(1) = A(k, k, 1): temp1(2) = A(k, k, 2) CALL CDIV(temp(), temp1(), cm()) FOR i = k + 1 TO n 'A(j,i) = A(j,i) - m*A(k,i) temp1(1) = A(k, i, 1): temp1(2) = A(k, i, 2) CALL CMUL(cm(), temp1(), temp()) A(j, i, 1) = A(j, i, 1) - temp(1) A(j, i, 2) = A(j, i, 2) - temp(2) NEXT i NEXT j NEXT k 'Calculate determinant by finding product of diagonal elements D(1) = 1#: D(2) = 0# FOR i = 1 TO n D(1) = D(1) * A(i, i, 1) D(2) = D(2) * A(i, i, 2) NEXT i RETURN 'end of file cfinddet.bas SUB CDIV (Z1(), Z2(), Z()) DIM C1(2) D1 = Z2(1) ^ 2 + Z2(2) ^ 2 IF D1 < 2E-12 THEN PRINT #2, " Complex Divide by zero!" ELSE C1(1) = Z2(1): C1(2) = -Z2(2) CMUL Z1(), C1(), Z() Z(1) = Z(1) / D1: Z(2) = Z(2) / D1 END IF END SUB SUB CMUL (Z1(), Z2(), Z()) Z(1) = Z1(1) * Z2(1) - Z1(2) * Z2(2) Z(2) = Z1(1) * Z2(2) + Z1(2) * Z2(1) END SUB