

      PROGRAM CGJDEMO
      INCLUDE 'STDHDR.FOR'

      COMPLEX  x(0:maxc, 0:maxc),y(0:maxc)
      COMPLEX solution(0:maxc),  cinv(0:maxc, 0:maxc)
      INTEGER numvar,i
      REAL matdet


      CALL  Getdata(x,y,numvar)

      CALL ComplexGaussJordan(x, y, numvar, solution, cinv, matdet)

      WRITE (*,*) 'The solution is: '
      WRITE (*,*)
      DO i = 0, numvar - 1
        WRITE (*,20) 'x', i, ' = ', REAL(solution(i)),
     +        IMAG(solution(i)), 'j '
20    FORMAT (A2, I2, A3 F7.2, F7.2, A2)
      END DO
      WRITE (*,*)
      WRITE (*,25) 'Matrix determinant = ', matdet
25    FORMAT (1X, A21, F10.2)
      CALL CheckAnswer(x, solution, numvar)

      END




      SUBROUTINE CheckAnswer( x, solution, numvar)
      INCLUDE 'STDHDR.FOR'
      COMPLEX  x(0:maxc, 0:maxc)
      COMPLEX solution(0:maxc)

      COMPLEX sum(0:maxr),temp, temp2
      INTEGER i, j
       WRITE (*,*) 'Checking the answer by calculating'
       WRITE (*,*) 'y values using calculated'
       WRITE (*,*) 'x values and original coefficients.'
       DO i = 0, numvar - 1
        sum(i) = CMPLX(0.0,0.0)
        DO j = 0, numvar - 1
          CALL ComplexMath(x(i, j), 2, solution(j), temp)
          CALL ComplexMath(sum(i), 0, temp, temp2)
          sum(i) = temp2
        END DO
        WRITE (*,15) 'y', i,
     +      ' = ( ', REAL(sum(i)), IMAG(sum(i)), 'J  )'
15      FORMAT (1X, A2, I2, A6, F7.2, 1X, F7.2, A5)
        WRITE (*,*)
      END DO
      END !SUBROUTINE CheckAnswer





      SUBROUTINE Getdata(x,y,numvar)
      INCLUDE 'STDHDR.FOR'
      COMPLEX  x(0:maxc, 0:maxc),y(0:maxc)
      INTEGER numvar, ans
      REAL tmpr,tmpi

      ans = 0
      DO WHILE (ans .eq. 0)
        WRITE (*,*) 'Enter number of equations '
        WRITE (*,11) 'must equal number of variables:  '
11      FORMAT (1X, A33 \)
        READ *, numvar
        WRITE (*,*)
        WRITE (*,*)
     +     'You will be asked to enter the complex coefficients.'
        WRITE (*,*) 'Enter the real part first, then the'
        WRITE (*,*) 'Coefficient of the imaginary part.'
        DO i = 0, numvar - 1
          WRITE (*,*) 'Equation # ', i
          DO j = 0, numvar - 1
            WRITE (*,14) 'Complex coefficient # ',j
14          FORMAT (1X, A22 I2 )
            WRITE (*,12) ' Real=     '
            READ  *, tmpr
            WRITE (*,12) ' Imaginary= '
12          FORMAT (A18 \)
            READ *, tmpi
            x(i, j) = CMPLX(tmpr,tmpi)
          END DO
          WRITE (*,13) ' Constant  real =  '
          READ *, tmpr
          WRITE (*,13) '      Imaginary =  '
13        FORMAT (A25 \)
          READ *, tmpi
          y(i) = CMPLX(tmpr,tmpi)
          WRITE (*,*)
        END DO
        WRITE (*,*) 'The following system of equations was entered:'
        WRITE (*,*)
        DO i = 0, numvar - 1
          DO j = 0, numvar - 1
            WRITE (*,19) '(', REAL(x(i, j)), IMAG(x(i, j)),
     +         'i X',j,')'
            IF (j .NE. numvar)  WRITE (*,20) '+'
19          FORMAT( 1X, A2, 2(F5.2) A3,I2, A1\)
20          FORMAT( A2\ )
          END DO
          WRITE (*,21) ' = (', REAL(y(i)), IMAG(y(i)), 'i)'
21        FORMAT (A5, 2(F5.2), A3)
          WRITE (*,*)
        END DO

        WRITE (*,*) 'Is this right (1-y OR 0-n) '
        READ *, ans
      END DO
      END !SUBROUTINE GetData


