C******************************************************************************
C                     S U B R O U T I N E     O P T
C******************************************************************************
C PURPOSE:  To compute the next iterate
C
C INPUTS - requiring initialization:
C   Integer     ITS        iteration number
C   Integer     IO         unit number for primary output file
C   Integer     NOUT       unit number for screen output
C   Integer     NSP        number of gridpoints, must be a power of 2
C   Integer     NDIM       number of dimensions
C   Integer     MXSP       maximum number of gridpoints allowed
C   Integer     MXDIM      maximum number of dimensions 
C   Integer     MXFC       maximum number of fourier coefficients available
C   Integer     MXSIZ      maximum order of Hankel matrices
C   Integer     INFOEXIT   exit status parameter
C   Integer     INFO       level of diagnostics for output
C   Integer     ISUM       dimension of array that holds packed form of Hankel
C                          ISUM = MXSIZ*(MXSIZ+1)/2
C   Dble Cmplx  F          array, holds current iterate, dimension MXSP
C   Dble Prec   FCF        array, holds initial guess for eigenvector to be
C                          used in the power method, dimension MXSIZ
C   Dble Prec   EPS        tolerance for iteration of |TAU| to 1; we accept
C                          TAU IF ||TAU|-1| < EPS
C
C INPUTS - not requiring initialization:
C   Dble Cmplx  KSTORE     array, workspace, will hold function to be 
C                          approximated in HINFS, dimension NSP by NDIM
C   Dble Cmplx  H          array, will hold solution to linear subproblem,
C                          dimension NSP by NDIM
C   Dble Cmplx  AK         array, workspace, will hold grid values of partial
C                          derivative of objective function, dim. mxsp by mxdim
C   Dble Cmplx  ALPHA      array, workspace, will hold grid values of spectral
C                          factors of second partial deriv., dim. NSP by NDIm
C   Dble Cmplx  FCDAT      array, workspace for HINFV, dim. MXSP
C   Dble Cmplx  FWORK      array, workspace for HINFS, dimension MXSP
C   Dble Cmplx  ZPT        array, workspace for HINFS, dimension MXSP
C   Dble Cmplx  ALPHARHS   array, workspace, will hold grid values of spectral
C                          factor of the RHS of linear subproblem, dim. NSP
C   Dble Prec   GK         array, workspace, will hold grid values of
C                          objective function at F
C   Dble Prec   HK         array, workspace, will hold grid values of second
C                          partial derivative of objective function at F,
C                          dimension MXSP
C   Dble Prec   SDEL       array, workspace, will hold grid values of the
C                          quantity:  GK - (|AK|**2/HK), dimension NSP
C   Dble Prec   SUMAKS     array, holds: sum, over all dimensions of
C                          |Ak|**2/Hk, dim. MXSP
C   Dble Prec   X          matrix, MXSIZ*MxDim by MXSIZ, will hold 
C                          composite hankel of  K
C   Dble Prec   KFC        matrix, MXFC by MxDim, fourier coeff of jth dim
C                          of K are stored in jth coumn of KFC
C   Dble Prec   GFC        matrix, MXFC by MxDim, holds results of X applied
C                          to the vector FCF
C   Dble Prec   RFCR       array, workspace, dimension NSP/4+1
C   Dble Cmplx  CFCR       array, workspace
C   Dble Prec   ZARR       array, workspace, dimension NSP/4+1
C   Dble Prec   WORK       array, workspace for LINPACK routine DSVDC
C   Dble Prec   ALPHAFC    array, workspace, dimension NSP/4+1
C   Dble Prec   EIGVAL     array, worksp, will hold singular values. dim MXSIZ
C   Dble Prec   EIGVEC     matrix, workspace, will hold singular vectors, dim.
C                          MXSIZ by MXSIZ
C   Dble Prec   AP         array, workspace, will hold packed form of Toeplitz
C                          matrix, dimension ISUM
C   Dble Cmplx  CSTORE     workspace.  no values retained.  Dimension mxsp
C
C OUTPUTS:
C   Dble Cmplx  F          on return, F is replaced by the next iterate
C   Dble Prec   PROBLIN    supremum of linear subproblem at this iteration
C   Dble Prec   PERCENT    PROBLIN - PROBINF; measures flatness of linear subproblem
C   Integer     INFOEXIT   0 if iteration was successful; see DSAPRX for other values
C   Integer     INFODIM    HOlds dimension at which a problem occurs
C   Dble Prec   SIGMAH     holds measure of analyticity of H, the solution
C                          to the linear subproblem
C   Dble Prec   HNORM      1-norm of the solution to the linear subproblem
C   Dble Prec   SPECSIG    holds indication of quality of spectral factorization of HK
C   Dble Prec   HANKSIG    holds sum of entries of last column of Hankel matrix of K
C   Dble Prec   RHSSIG     indicates quality of spectral factorization     
C                          right hand side of linear subproblem
C   Dble Prec   VALUE      supremum of objective function evaluted at the new iterate
C   Dble Prec   GINF       infimum of objective function evaluted at the new iterate
C   Dble Prec   STEP       optimum t-value in line search
C   Integer     UPDATES    Interation within opt  
C
C EXTERNALS:
C               MAKEGK     user supplied subroutine to compute gridpoint values
C                          of objective function
C               MAKEAK     user supplied subroutine to compute gridpoint values
C                          of 1st partial derivative of objective function
C               MAKEHK     user supplied subroutine to compute gridpoint values
C                          of 2nd partial derivative of objective 
C
C@@@@@@@@@@@this algorithm will have to change@ to be consistent with scalar and vecto together@@@@@@@@@@@@@@@@@@@@
C ALGORITHM:
C   PART I - ALGEBRAIC MANIPULATION OF LINEAR SUBPROBLEM 
C            INTO A FORM THAT HINFS CAN SOLVE
C     (1)  Compute gridpoint values of objective (gk), 1st partial (ak), and
C          2nd partial (hk)
C     (2)  Compute the sum over all dimensions of |Ak|**2/Hk  and
C          spectral factor of each component of HK
C     (3)  Compute the supremum of Gk, plus the supremum and infimum of the
C          quantity FK - SumAks
C     (4)  Decide if right hand side of linear subproblem is constant.
C          If so, then Simple is TRUE   (This begins the top of loop )
C     (5)  Spectral factorize right hand side of linear subproblem; divide left
C          side by this factor and multiply each component of Kstore by 
C          spectral factor of corresponding component of HK to get the final
C          gridpoint of KSTORE
C
C   PART II - SOLVE THE TRANSFORMED PROBLEM
C     (6)  Feed KStore from (5) to HINFS to get TAU
C     (7)  If Simple is TRUE, go to PART IV
C
C   PART III - DETERMINE NEW VALUE OF DELTA THAT (hopefully)
C              PRODUCES |TAU| CLOSE TO 1
C     (8)  The initial guess for DELTA is delmax.  If this fails to produce |TAU|
C          close to 1, then we set DELTA to slightly larger than DELMIN and go to
C          the top of loop.  If the TAU produced by this delta is > 1, then
C          we start a bisection search for the optimal delta in [DELMIN,DELMAX].
C          Otherwise, we go to PART IV
C
C   PART IV - TRANSFORM THE LINEAR SUBPROBLEM BACK TO ITS ORIGINAL FORM
C     (9)  Multiply H by spectral factor of the right hand side; divide by 
C          spectral factor of 2nd partial derivative
C
C   PART V - LINE SEARCH
C    (10)  Eliminate negative fourier coefficients of H
C    (11)  Perform line search
C    (12)  Compute gridpoint values of left hand side of linear subproblem
C    (13)  Compute supremum and infimum of left hand side of linear subproblem
C          evaluated at H produced by HINFS; set PERCENT = PROBLIN - PROBINF
C  ??????????????????????????????????????????????????????????????????????????

      SUBROUTINE Opt  (Its, NSp, NDim, MakeGk, MakeAk, MakeHk, MxSp,
     &                 MxDim, MxFc, MxSiz, iSum, Info, Io, nOut, F, Ak,
     &                 Zpt, Alpha, AlphaRhs, Kstore, H, FcDat, FWork,      
     &                 Gk, Hk, Eps, KFc, GFc, X, EigVec, Work, EigVal, 
     &                 RFcr, FcF, Updates, CFcR, Wno, AlphaFc, Zarr, 
     &                 Ap, SDel, SumAks, Problin, HankSig, SpecSig,
     &                 RhsSig, SigmaH, Hnorm, Percent, Value, Ginf,
     &                 Step, InfoExit, InfoDim, WnoV, cStore)


C  Subroutine Arguments:
      INTEGER          NSp, NDim, MxSp, MxDim, MxFc, MxSiz, InfoExit,
     &                 iSum, Io, nOut, Info, InfoDim, Its, Updates 
      DOUBLE COMPLEX   Alpha(mxsp,mxdim), Ak(mxsp,mxdim), F(mxsp,mxdim),
     &                 AlphaRhs(mxsp), FcDat(mxsp), FWork(mxsp),
     &                 Zpt(mxsp), H(mxsp,mxdim), Kstore(mxsp,mxdim),
     &                 CFcR(mxsp), cStore(mxsp)
      DOUBLE PRECISION SumAks(mxsp), Hk(mxsp,mxdim), Gk(mxsp),
     &                 Eps, KFc(mxfc,mxdim), GFc(mxfc,mxdim),
     &                 AlphaFc(mxfc), Zarr(iSum), Ap(iSum), Hnorm, 
     &                 SDel(mxsp), X(mxsiz*mxdim,mxsiz), Wno, 
     &                 EigVec(mxsiz,mxsiz), EigVal(mxsiz),
     &                 SpecSig(mxdim), HankSig(mxdim), RhsSig, Step,
     &                 SigmaH(mxdim), Work(mxsiz*mxdim),
     &                 FcF(mxsiz), Problin, Value, Percent, GInf,
     &                 RFcR(mxfc),WnoV
      EXTERNAL         MakeGk, MakeAk, MakeHk

C  Local and Lower-level arguments:
      Integer          ISup(128)
      DOUBLE COMPLEX   KijStore
      DOUBLE PRECISION Norm, Huge, DelMax, DelInf, DelMin, Tau,
     &                 RootDelta, ProbInf, Delta, 
     &                 DelMin2, X1, X2, FA, FB, A, B, C, TolMin, M,
     &                 rStep, BStep, B2, FC, small,xx,sum 
      LOGICAL          Simple, Theorem, Cond

C  Functions:
      DOUBLE PRECISION Wind, Norm1

C@@@@@@@@@@would like to make these more consistent.  Tol probably 
C@@@@@@@@@@@@@should be set in dsaprx and passed consistently
C  Initializations:
      DATA             Huge    / 1.0d25  /,
     &                 Small   / 1.0d-7   /,
     &                 Tol     / 1.0d-14 /,
     &                 TolMin  / 1.0d-14 /

      Simple        =  .FALSE.





C 1.1 Compute gridpoint values of objective function and partials
C     ===========================================================
      CALL MakeGk (NSp, NDim, MxSp, F, Gk, MxDim)
      CALL MakeAk (NSp, NDim, MxSp, F, Ak, MxDim)
      CALL MakeHk (NSp, NDim, MxSp, F, Hk, MxDim)


C ... Display gridpoint values, if requested
      IF (Info .GT. 3)  THEN
         WRITE(io,7010)
         DO 15 i = 1,NSp
            WRITE(io,7020) i, Gk(i)
15       CONTINUE
         WRITE(io,7030)
         DO 16 j = 1,NDim
            WRITE(io,7040) j
            DO 17 i = 1,NSp
               WRITE(io,7050) i, Ak(i,j)
17          CONTINUE
16       CONTINUE
         WRITE(io,7060)
         DO 18 j = 1,NDim
            WRITE(io,7070) j
            DO 19 i = 1,NSp
               WRITE(io,7020) i, Hk(i,j)
19          CONTINUE
18       CONTINUE
      ENDIF


C +++++Calculate Sup-Inf
      Value = -1d25
      Ginf  =  1d25
      DO 28 i = 1, NSp
         IF (Gk(i) .LT. Ginf) Ginf = Gk(i)
         IF (Gk(i) .GT. Value+1d-15) THEN
            Value = Gk(i)
            ISup(1) = i
            kk = 1
         ENDIF
         IF (Gk(i) .GT. Value-1d-15) THEN
            kk = kk + 1 
            ISup(kk) = i
         ENDIF
28    CONTINUE
      GDiff = Value - GInf

C +++++New check on Ak of Sup
      DO 27 k = 1, kk
         sum = 0.0d0
         DO 29 j = 1, NDim
            sum = sum + CDABS(Ak(Isup(k),j))**2
29       CONTINUE
         sum = DSQRT(sum)
         IF (sum .LT. 1d-15) THEN
            WRITE(Nout,8100)
            STOP 
         ENDIF
27    CONTINUE
  


C 1.3 Compute "Gk - SumAks", where SumAks=|Ak|*2/Hk over all dimension.
C  ...Compute sup and inf of this quantity, plus sup of Gk.
C  ...Min value of Delta = sup"Gk - SumAks"; Max value of Delta = sup "Gk"
C     =====================================================================
      DelInf  =  Huge
      DelMin  = -Huge
      DelMax  = -Huge
      DelMin2 =  Huge
      DO 20 i = 1,NSp
C     ...Compute the Sum of  |Ak|**2/Hk  over all dimensions 
         SumAks(i) = 0.0d0
         DO 30 j = 1,NDim
C        ...First, test for nonpositive 2nd partial derivatives. 
            IF (Hk(i,j) .LE. Small) THEN
               InfoExit = -i
               InfoDim  =  j
               RETURN
            ENDIF
            SumAks(i)  = SumAks(i) + CDABS(Ak(i,j))**2  /Hk(i,j)
30       CONTINUE
 
C     ...Compute the difference of Gk and SumAks 
         SDel(i) = Gk(i) - SumAks(i)

C     ...Determine sup and inf
         IF (SDel(i) .GT. DelMin )  DelMin  = SDel(i)
         IF (SDel(i) .LT. DelInf )  DelInf  = SDel(i)
         IF (Gk(i)   .LT. DelMin2)then
               DelMin2 = Gk(i)
         endif
         IF (Gk(i)   .GT. DelMax)then 
                DelMax  = Gk(i)
                imaxgk = i
         endif
20    CONTINUE


C 1.4 If "Gk - SumAks" is small enough, all we need is a simple approximation
C     =======================================================================
      IF ((DABS(DelMin) .LT. Tol) .AND. (DABS(DelInf) .LT. Tol)) THEN
         Simple = .TRUE.
      ENDIF


C  ...Write headers for the sup and inf, and the rhs of the linear subproblem
      IF (Info .GT. 3) THEN
         WRITE(io,7979) DelMin, DelMax
         IF (Simple) THEN
             WRITE(io,*) 'RHS of linear subproblem is constant'
         ELSE
             WRITE(io,*) 'RHS of subproblem is NOT constant'
         ENDIF
      ENDIF



C  ...Fill the grid values of spectral factor of 1st partial derivative
      DO 21 j = 1,NDim
         DO 21 i = 1,NSp
            Alpha(i,j) = DCMPLX(Hk(i,j),0.0d0)
21    CONTINUE



C  ...Compute gridpoint values of spectral factor of 2nd partial derivative
C  ...for each component of Hk
      DO 35 j = 1,NDim
         IF (Info .GT. 1)  WRITE(io,7090) j
         CALL MakeSpec(NSp, Alpha(1,j), RFcr, AlphaFc, SpecSig(j),
     &                 iSum, Zarr, Ap, InfoExit, Info, Io, MxSp, MxFc)
C     ...Check for bad Cholesky decomposition for spectral factor of 2nd der.
         IF (InfoExit .NE. 0) THEN
            InfoExit = 4
            InfoDim  = j
            RETURN
         ENDIF
35    CONTINUE



C  ...Initial guess for Delta:  Test to see if a Delta exists that will give
C  ...a Tau of one. If not, Theorem=.False. and Hinfs will act accordingly
      IF (.NOT. Simple)  THEN
         IF ((NDim .GT. 1)  .AND.  (DelMin2 .LT. DelMin)) THEN
             Delta   = DelMin + Small
             Theorem = .FALSE.

         ELSE IF((NDim .EQ. 1) .AND. ((DelMin2 .LT. DelMin) .OR. 
     &           (Wind(NSp,Ak,MxSp,MxDim,nOut) .LT. 0.5d0))) THEN
            Delta    = DelMin + Small
            Theorem  = .FALSE.

         ELSE
            Delta   = DelMin2
            A       = Delta
            C       = A
            Theorem = .TRUE.
         ENDIF

      ELSE
         DELTA = 1.0d0
      ENDIF




C*********************
C     TOP OF MAIN LOOP
C*********************
      Updates = 0
50    CONTINUE
      Updates = Updates + 1


C 1.5 Spectral factorize rhs of linear subproblem
C     ==========================================
C  ...If Simple is true, then the spec fact is merely the square root Delta
C  ...(We assume in this computation that Delta is positive, as assigned above.)
      IF (Info .GT. 1)   WRITE(io,7110)
      IF (Simple) THEN
         RootDelta = DSQRT(Delta)
         DO 65  i = 1,NSp
            AlphaRhs(i) = DCMPLX(RootDelta,0.0d0)
65       CONTINUE
         IF (Info .GT. 1)   WRITE(io,7120) RootDelta

      ELSE
         DO 60 i = 1,NSp
            AlphaRhs(i) = DCMPLX((Delta-SDel(i)),0.0d0)
60       CONTINUE
         CALL MakeSpec (NSp, AlphaRhs, RFcr, AlphaFc, RhsSig, iSum,
     &                  Zarr, Ap, InfoExit, Info, Io, MxSp, MxFc)
C     ...Check for bad Cholskey Decomposition for spectral fact. of RHS
         IF (InfoExit .NE. 0) THEN
            InfoExit = 5
            RETURN
         ENDIF
      ENDIF



C ... Construct function to be approximated
C  ...Devide the lhs of subproblem by spec fact of the right to obtain
C  ...the function to approximate from H-infinity
      DO 80 j = 1,NDim
         DO 80 i = 1,NSp
            IF ((Alpha(i,j)  .EQ. (0.0d0,0.0d0))  .OR.
     &          (AlphaRhs(i) .EQ. (0.0d0,0.0d0)))   THEN
               STOP 'Div by 0 in Opt.  Spec fact or spec fact of rhs=0'
            ENDIF
            KijStore    = DCONJG(Ak(i,j)) /DCONJG(Alpha(i,j))
            Kstore(i,j) = KijStore /AlphaRhs(i)
80    CONTINUE



C 2.6 Compute the best approximation from H-infinity
C     ==============================================
      IF (NDim .EQ. 1)  THEN
         CALL HinfS (Simple, Eps, NSp, KStore, H, iSum, Ap, EigVec,
     &               EigVal, FcF, MxSiz, Info, InfoExit, CFcR, RFcr,
     &               Tau, Io, HankSig, Theorem, Tol, MxSp, MxDim,
     &               MxFc, NDim)
      ELSE
         CALL HinfV (NSp, NDim, MxSp, MxSiz, MxFc, MxDim, Io, 
     &               Info, Simple, Eps, Kstore, FcDat, FWork, 
     &               Zpt, KFc, GFc, X, EigVec, Work, EigVal, FcF, 
     &               Tau, HankSig, H, InfoExit, Theorem, Cond, Tol)
      ENDIF

      if(info.gt.1)then
         write(io,*)'nehari  = ',simple
         write(io,*)'updates = ',updates
         write(io,*)'epsilon = ',eps
         write(io,*)'tau2-1  = ',tau*tau-1.0d0
         write(io,*)'delta   = ',delta
         write(io,*)'tau     = ',tau
      endif
C  ...Check if the eigenvalue problem can be solved
      IF (InfoExit .NE. 0)   RETURN



C 3.8 Determine how good Tau is and decide if should continue looping
C     ===============================================================
      IF ((.NOT.Simple).AND.(DABS(DABS(Tau)-1.0d0).GT.Eps))THEN
         IF (Theorem) THEN
C        ...For the first loop through
            IF (Updates .EQ. 1) THEN
                FA  = DABS(Tau) - 1.0d0
                IF (DABS(Tau) .LT. (1.0d0 - Eps)) THEN
                   Delta = DelMin + Small
                   Cond  = .TRUE. 
                ELSE
                   Delta = DelMax
                   Cond  = .FALSE.        
                ENDIF
                GO TO 50
            ELSE
                FB = DABS(Tau) - 1.0d0
                IF ((Cond) .AND. (DABS(Tau) .LT. (1.0d0 + Eps))
     &             .AND. (Updates .EQ. 2) )
     &             GO TO 121
                IF (DABS(FB-FA) .GT. TolMin)  THEN
                   IF ((FB-FA) .EQ. 0.0d0)
     &                STOP 'Div by 0 in Opt.  Fa-Fb = 0'
                   IF (FB*FA .LT. 0.0d0) THEN
                      C  = A
                      FC = FA
                   ENDIF
                   B     = Delta
                   rStep  = (A-B) * FB /(FB-FA)
                   BStep = B + rStep
                   M     = (C-B) /2.0d0
                   IF ((B .LE. BStep) .AND. (B+M .GE. BStep)) THEN
                      B2 = BStep
                   ELSE
                      B2 = B + M
                   ENDIF
                   A  = Delta
                   FA = FB
                   IF (B2 .LE. DelMin+Small) THEN
                       Delta = 0.5d0 * (Delta + Delmin+Small)
                   ELSE
                       DELTA = B2
                   ENDIF
                   GO TO 50
               ENDIF
            ENDIF

         ELSE
            IF (Updates .EQ. 1) THEN
               IF (DABS(Tau) .GT. (1.0d0+Eps)) THEN
                  X1    = Delta
                  X2    = DelMax
                  Delta = DelMax
                  GO TO 50
               ENDIF
            ELSE
               IF (DABS(Tau) .GT. 1.0d0) THEN
                  X1    = Delta
                  Delta = 0.5d0 * (X1 + X2)
               ELSE
                  X2    = Delta
                  Delta = 0.5d0 * (X1 + X2)
               ENDIF
               GO TO 50
            ENDIF
         ENDIF
      ENDIF

C*********************
C     END OF MAIN LOOP (solution to linear subproblem computed)
C*********************
121   CONTINUE



C 4.9 Adjust H for the spectral factorizations we mulitiplied through by
C     earlier in computing Kstore.  (Alpha already checked for 0)
C     ================================================================
      DO 130 j = 1,NDim
         DO 130 i = 1,NSp
            H(i,j) = -AlphaRhs(i) * H(i,j) /Alpha(i,j)
130   CONTINUE


C 5.10 Compute sigma for components of H
C     ==================================
      DO 190 j = 1,NDim
         CALL Fft (NSp, H(1,j))
         DO 200 i = 1,NSp
            H(i,j) = H(i,j) /DBLE(NSp)
200      CONTINUE
         SigmaH(j) = 0.0d0
         DO 210 i = 2,NSp/8
            SigmaH(j) = SigmaH(j) + CDABS(H(i,j))
210      CONTINUE
190   CONTINUE

C  ...Display fourier coefficients of the update
      IF (Info .GT. 3)  THEN
         WRITE(io,7130)
         DO 211 j = 1,NDim
            WRITE(io,7140) j
            DO 215 i = 1,NSp/4
               IF (i .EQ. 1) THEN
                  WRITE(io,7150) i-1, H(i,j), H(i,j)
               ELSE
                  WRITE(io,7150) i-1, H(nsp-i+2,j), H(i,j)
               ENDIF
215         CONTINUE
            WRITE(io,*)
            WRITE(io,*) 'Sigma for this component of H = ',SigmaH(j)
211      CONTINUE
      ENDIF


C  ...Zero-out the negative fourier coeff. to insure analytic update direction
      DO 212 j = 1,NDim
         H(1,j) = DCMPLX (DREAL(H(1,j)),0.0d0)
         DO 220 i = 2,NSp/2
            H(i,j) = DCMPLX (DREAL(H(nsp-i+2,j)),0.0d0)
220      CONTINUE
         DO 230 i = (NSp/2)+1,NSp
            H(i,j) = (0.0d0,0.0d0)
230      CONTINUE
         CALL Fft (NSp, H(1,j))
212   CONTINUE
c


C 5.12/5.13 Compute value of linear subproblem at solution
C  ... First, store grid values of subproblem in Gk
C     ====================================================
      DO 140 i = 1,NSp
         Gk(i) = 0.0d0
         DO 150 j = 1,NDim
            Norm  = (CDABS(DCONJG(Ak(i,j))+Hk(i,j)*H(i,j)))**2
            Gk(i) = Gk(i) + (Norm /Hk(i,j))
150      CONTINUE
140   CONTINUE
      IF (.NOT.Simple) THEN
         DO 160 i = 1,NSp
            Gk(i) = Gk(i) + SDel(i)
160      CONTINUE
      ENDIF


C  ...Now sup and inf over those stored values
      ProbLin = 0.0d0
      ProbInf = Huge
      DO 170 i = 1,NSp
         IF (ProbLin .LT. Gk(i))  ProbLin = Gk(i)
         IF (ProbInf .GT. Gk(i))  ProbInf = Gk(i)
170   CONTINUE

C  ...Linear subproblem diagnostic to measure flatness
      Percent = ProbLin - ProbInf



C  ...Compute HNorm, the norm of the update function H
C  ...For the scalar case, we can avoid calling the subroutine Norm1
      IF (NDim .EQ. 1)  THEN
         HNorm = 0.0d0
         DO 300 i = 1,NSp
            IF (HNorm .LT. (CDABS(H(i,1))))   HNorm = CDABS(H(i,1))
300      CONTINUE

C  ...In the vector case, we need to call the subroutine Norm1
C  ...(Use Ak to define a 0-function in order to save space)
      ELSE    
         DO 305 i = 1,NSp
            DO 305 j = 1,NDim
               Ak(i,j) = DCMPLX(0.0d0,0.0d0)
305      CONTINUE
         HNorm = Norm1 (NSp, NDim, MxSp, MxDim, H, Ak)
      ENDIF


C 5.11 Line Search
C     ============
      CALL Gold (Io, Info, Nsp, F, H, KStore, Gk, MakeGk,
     &            Value, GInf, Step, NDim, MxDim, MxSp, Huge,hnorm)


C  ...Compute the winding number
C@@@@@@@@@@@@@@make sure this is where he wants the winding number
      CALL MakeAk (NSp, NDim, MxSp, F, Ak, MxDim)
      IF (NDim .EQ. 1) THEN
         Wno = Wind(NSp, Ak, MxSp, MxDim, NDim)
      ELSE
         CALL WindV (NDim, NSp, MxDim, MxSp, Ak, WnoV, nOut, cStore)
      ENDIF


      RETURN



8100  FORMAT(1h ,'The function Gamma at f attains a maximum ',
     &       'at a gridpoint at which the partial ',
     &       'derivative in z of Gamma at f is 0.  DSAPRX can not ',
     &       'produce a good update in this case',/
     &       'Suggestion: try a different initial guess')
7010  FORMAT(1h0///1h0,'Gridpoint values of objective function ',
     &                 'evaluated at the current iterate:',/
     &       1h ,72('-')//
     &       1h0,'Gridpoint',7x,'Function Value'/)
7020  FORMAT(1h ,i5,12x,g14.6)
7030  FORMAT(1h1,'Gridpoint values of partial derivative of objective',
     &           ' function evaluated at the current iterate:'/
     &           95('-')/)
7040  FORMAT(1h0,'Dimension = ',i2///
     &       1h0,'Gridpoint',16x,'Function Value'/)
7050  FORMAT(1h ,4x,i5,8x,g14.6,2x,g14.6)
7060  FORMAT(1h1,'Gridpoint values of second partial derivative of ',
     &           'objective function',
     &           ' evaluated at the current iterate:'/1h ,100('-')/)
7070  FORMAT(1h0,'Dimension = ',i2///
     &       1h0,'Gridpoint',9x,'Function Value'/)
7090  FORMAT(1h1,'Spectral Factorization Data for 2nd partial ',
     &           'derivative'/1h ,60('-')//1h0,'Dimension = ',i2,/)
7110  FORMAT(1h1,'Spectral Factorization Data for Right Hand Side of',
     &           ' linear subproblem:'/1h ,70('-')/)
7120  FORMAT(1h0,'Spectral factor is square root of Delta:',g14.6)
7130  FORMAT(1h1,'Fourier coefficients of the update H',/,
     &       1h ,39('-'))
7140  FORMAT(1h0//1h0,'Dimension =',i2//,
     &       1h0,16x,'Positive Fourier Coeffs',14x,'Negative ',
     &           'Fourier Coeffs'/)
7150  FORMAT(1h ,i4,2(10x,g12.6,3x,g12.6))
7979  FORMAT(1h0////1h0,'Optimum Delta lies within ',g14.6,
     &                  'and ',g14.6//) 

      END
