      SUBROUTINE BALGEN (N,MA,A,MB,B,LOW,IGH,CSCALE,CPERM,WK)
C
C     *****PARAMETERS:
      INTEGER IGH,LOW,MA,MB,N
      DOUBLE PRECISION A(MA,N),B(MB,N),CPERM(N),CSCALE(N),WK(N,6)
C
C     *****LOCAL VARIABLES:
C     NONE
C
C     *****FUNCTIONS:
C     NONE
C
C     *****SUBROUTINES CALLED:
C     REDUCE, SCALEG, GRADEQ
C
C     ---------------------------------------------------------------
C
C     *****PURPOSE:
C     THIS SUBROUTINE BALANCES THE MATRICES A AND B TO IMPROVE THE
C     ACCURACY OF COMPUTING THE EIGENSYSTEM OF THE GENERALIZED
C     EIGENPROBLEM A*X = (LAMBDA)*B*X.  THE ALGORITHM IS SPECIFICALLY
C     DESIGNED TO PRECEDE QZ TYPE ALGORITHMS, BUT IMPROVED PERFORMANCE
C     IS EXPECTED FROM MOST EIGENSYSTEM SOLVERS.
C     REF.:  WARD, R. C., BALANCING THE GENERALIZED EIGENVALUE
C     PROBLEM, SIAM J. SCI. STAT. COMPUT., VOL. 2, NO. 2, JUNE 1981,
C     141-152.
C
C     *****PARAMETER DESCRIPTION:
C
C     ON INPUT:
C
C       MA,MB   INTEGER
C               ROW DIMENSIONS OF THE ARRAYS CONTAINING MATRICES
C               A AND B RESPECTIVELY, AS DECLARED IN THE MAIN CALLING
C               PROGRAM DIMENSION STATEMENT;
C
C       N       INTEGER
C               ORDER OF THE MATRICES A AND B;
C
C       A       REAL(MA,N)
C               CONTAINS THE A MATRIX OF THE GENERALIZED EIGENPROBLEM
C               DEFINED ABOVE;
C
C       B       REAL(MB,N)
C               CONTAINS THE B MATRIX OF THE GENERALIZED EIGENPROBLEM
C               DEFINED ABOVE;
C
C       WK      REAL(N,6)
C               WORK ARRAY THAT MUST CONTAIN AT LEAST 6*N STORAGE
C               LOCATIONS.  WK IS ALTERED BY THIS SUBROUTINE.
C
C     ON OUTPUT:
C
C       A,B     CONTAIN THE BALANCED A AND B MATRICES;
C
C       LOW     INTEGER
C               BEGINNING INDEX OF THE SUBMATRICES OF A AND B
C               CONTAINING THE NON-ISOLATED EIGENVALUES;
C
C       IGH     INTEGER
C               ENDING INDEX OF THE SUBMATRICES OF A AND B
C               CONTAINING THE NON-ISOLATED EIGENVALUES.  IF
C               IGH = 1 (LOW = 1 ALSO), THE A AND B MATRICES HAVE
C               BEEN PERMUTED INTO UPPER TRIANGULAR FORM AND HAVE
C               NOT BEEN BALANCED;
C
C       CSCALE  REAL(N)
C               CONTAINS THE EXPONENTS OF THE COLUMN SCALING FACTORS
C               IN ITS LOW THROUGH IGH LOCATIONS AND THE REDUCING
C               COLUMN PERMUTATIONS IN ITS FIRST LOW-1 AND ITS
C               IGH+1 THROUGH N LOCATIONS;
C
C       CPERM   REAL(N)
C               CONTAINS THE COLUMN PERMUTATIONS APPLIED IN GRADING
C               THE A AND B SUBMATRICES IN ITS LOW THROUGH IGH
C               LOCATIONS;
C
C       WK      CONTAINS THE EXPONENTS OF THE ROW SCALING FACTORS
C               IN ITS LOW THROUGH IGH LOCATIONS, THE REDUCING ROW
C               PERMUTATIONS IN ITS FIRST LOW-1 AND ITS IGH+1
C               THROUGH N LOCATIONS, AND THE ROW PERMUTATIONS
C               APPLIED IN GRADING THE A AND B SUBMATRICES IN ITS
C               N+LOW THROUGH N+IGH LOCATIONS.
C
C     *****ALGORITHM NOTES:
C     NONE
C
C     *****HISTORY:
C     WRITTEN BY R. C. WARD.......
C
C     ---------------------------------------------------------------
C
      CALL REDUCE (N,MA,A,MB,B,LOW,IGH,CSCALE,WK)
      IF (LOW .EQ. IGH) GO TO 10
      CALL SCALEG (N,MA,A,MB,B,LOW,IGH,CSCALE,CPERM,WK)
      CALL GRADEQ (N,MA,A,MB,B,LOW,IGH,CPERM,WK(1,2))
   10 CONTINUE
      RETURN
C
C     LAST LINE OF BALGEN
C
      END
