
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% private_380_ROUTINE-- this routine is called frequently by STEPIT; it performs many calculations
%								Keeping it in the STEPIT method proper would have just taken up too much
%								space.
%
%
%	Written by Jason Lott, University of Alabama at Birmingham, 2001
%  CONTACT: hoffa@uab.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function a = private_380_ROUTINE(b)						

global X
global XMAX
global XMIN
global DELTX
global DELMIN
global ERR
global FOBJ
global VEC
global DLX
global XS
global FSTORE
global DX
global SALVO
global XOSC
global FOSC
global ARG
global STCUT
global ACK
global FACUP
global RZERO
global XPLUS
global FSAVE
global FBEST
global XSAVE
global ABSDX
global FPREV
global DENOM
global DEL
global DXZ
global DXU
global DFZ
global DFU
global ABSVEC
global SUMV
global CINDER
global COXCOM
global COSIN
global STEPS
global J
global JFLAT
global JFLMIN
global JOCK
global JUMP
global JVARY
global JX
global K
global KERFEL
global KL
global KW
global MASK
global MATRX
global MINSOC
global MAXOSC
global MAXSTP
global NACK
global NACTIV
global NAH
global NCIRC
global NEQUAL
global NF
global NFLAT
global NFMAX
global NFSAV
global NGATE
global NGIANT
global NONZER
global NOREP
global NOSC
global NOUT
global NRETRY
global NSSW
global NSTEPS
global NT
global NTRACE
global NV
global NEXTRA
global NZIP
global tag_380
global tag_390
global wtag
global tag
global continueflag
global T
global Y
global YSIG
global grandtag
global KFLAG
global DX
global XS
global DLX
global fortag
global KFLAGENCOUNTER

tag_380 = 1;


while tag_380 == 1
   
   tag_390 = 1;
   FPREV = FSTORE(JX);
   tag_400 = 1;
                              
   while tag_390 == 1
      
      NRETRY = -1;
      NGIANT = 0;
      NFSAV = NF;
      tag_400 = 1;
      
      while tag_400 == 1
         
         for J=1:NV
            
            XS(J) = X(J);
            
            if MASK(J)~=0
               
                                       
            else
               
               X(J) = X(J) + DLX(J);
               
               if X(J)>XMAX(J)
                  
                  X(J) = XMAX(J);
                  
               end
               
               if X(J)<XMIN(J)
                  
                  X(J) = XMIN(J);
                  
               end
               
            end
            
         end
         
         JOCK = 0;
         JVARY =0;
         private_FUNK(1);
         NF = NF + 1;
                                  
         if FOBJ>=FBEST
            
            if NGIANT<=0
               
               for J=1:NV
                  
                  if NRETRY>=0
                     
                     DLX(J) = SALVO(J);
                     
                  end
                  
                  X(J) = XS(J);
                  
               end
               
               if NTRACE>=1
                  
                  disp('FOBJ= '),disp(FOBJ),disp(' AFTER '),disp(NGIANT),disp('GIANT STEPS')
                  
               end
               
               if NGIANT>0
                  
                  tag_400 = 0;								%break out of 400 LOOP
                  tag_380 =0;
                  tag_390 = 0;								
               
                  NCIRC = 0;	%%%%%%%% Call to 640 ROUTINE
                  NZIP = 0;
                  NACK = 0;
                  grandtag = 1;
                  fortag = 0;
                  %disp('call to 640 from 380')
                  %pause
                 
                  
               elseif NRETRY>0
                  
                  if KL>NAH
                     
                     tag_400 = 0;							%break out of 400 LOOP
                     tag_380 = 1;
                     tag_390 = 0;							%break out of 390 loop, renter 380 loop
                                                    
                  else
                     
                     tag = 1;
                     
                     for K=KL:NAH
                        
                        if tag == 1
                           
                           NRETRY = NAH - K;
                           COSIN = RZERO;
                                                    
                           for J=1:NV
                              
                              COSIN = COSIN + ERR(J,NOSC)*ERR(J,K);
                              
                           end
                           
                           if (K>=(NOSC-1))|((COSIN>RZERO)&(COSIN>COXOM))
                              
                              KL = KL+1;
                              NT = NOSC-K;
                                                       
                              if NTRACE>=1
                                 
                                 disp('*****GIGANTIC STEP WITH PERIOD '),disp(NT),disp(' BEING ATTEMPTED.')
                                 disp('COXCOM= '),disp(COXCOM)
                                 disp('COSIN= '), disp(COSIN)
                                 
                              end
                              
                              for J=1:NV
                                 
                                 SALVO(J) = DLX(J);
                                 DLX(J) = X(J)-XOSC(J,K);
                                                         
                              end
                              
                              FPREV = FOSC(K);
                              tag_390 = 1;				%stay in 390 loop, accomplished with  tag=0 and very next IF statement below
                              tag = 0;
                              
                           end
                           
                        end
                        
                     end
                     
                     if tag == 0
                        
                        tag_400 = 0;						%Break out of 400 LOOP
                        tag_390 = 1;						%ARRR! Stay in tha 390 loop, matey
                        
                     else
                        
                        tag_400 = 0;						%Break out of 400 LOOP
                        tag_380 = 1;
                        tag_390 = 0;						%break out of 390 loop, renter 380 loop
                                                
                     end
                     
                  end
                  
               elseif NRETRY == 0
                  
                  tag_400 = 0;								%Break out of 400 LOOP
                  tag_380 = 1;
                  tag_380 = 0;								%%Kick out of 390 loop, reenter 380 loop
                                                 
               else
                  
                  NOSC = NOSC -1;
                  
                  if NOSC<0
                     
                     NOSC = 0;
                     
                  end
                  
                  FSTORE(JX) = FBEST;
                  
                  if NF>NFMAX
                     
                     KFLAG = -2;
                     %disp('ENTERING KFLAG EXIT from 380 1')
                     %pause
                     private_KFLAG_EXIT(KFLAG);
                     tag_400 = 0;
                     tag_380 = 0;
                     tag_390 = 0;							%To ensure that I break out of all loops to exit properly.
                                                                                                                                                        
                  else
                     
                     tag_400 = 0;							%Break out of 400 LOOP
                     tag_380 = 0;
                     tag_390 = 0;							%%Kicks me out of while loop	%Call to 540--truly keeps me in FOR loop
                     %disp('truly stay in for loop, matey, from 380')
                     %pause
                                                      
                  end
                  
               end
               
            else												%%Perform quadratic interpolation
               
               DENOM = ACK*(FPREV-FBEST) - (FBEST-FOBJ);
               
               if DENOM<=RZERO
                  
                  for J=1:NV
                     
                     if NRETRY>=0
                        
                        DLX(J) = SALVO(J);
                        
                     end
                     
                     X(J) = XS(J);
                     
                  end
                  
                  if NTRACE>=1
                     
                     disp('FOBJ= '),disp(FOBJ),disp(' AFTER '),disp(NGIANT),disp('GIANT STEPS')
                     
                  end
                  
                  if NGIANT>0
                     
                     tag_400 = 0;	             			%Break out of 400 LOOP        
                     tag_380 =0;
                     tag_390 = 0;								
                     NCIRC =0;											%Call to 640!!!!!!!!!!!!!!!!!!!!!!!!
                     NZIP = 0;
                     NACK = 0;
                     grandtag = 1;
                     fortag = 0;
                     %disp('call to 640, matey from 380')
                     %pause
                     
                                              
                  elseif NRETRY>0
                     
                     if KL>NAH
                        
                        tag_400 = 0;							%Break out of 400 LOOP
                        tag_380 = 1;
                        tag_390 = 0;							%break out of 390 loop, renter 380 loop
                                                    
                     else
                        
                        tag = 1;
                        
                        for K=KL:NAH
                           
                           if tag == 1
                              
                              NRETRY = NAH - K;
                              COSIN = RZERO;
                                                    
                              for J=1:NV
                                 
                                 COSIN = COSIN + ERR(J,NOSC)*ERR(J,K);
                                 
                              end
                              
                              if (K>=(NOSC-1))|((COSIN>RZERO)&(COSIN>COXOM))
                                 
                                 KL = KL+1;
                                 NT = NOSC-K;
                                                       
                                 if NTRACE>=1
                                    
                                    disp('*****GIGANTIC STEP WITH PERIOD '),disp(NT),disp(' BEING ATTEMPTED.')
                                    disp('COXCOM= '),disp(COXCOM)
                                    disp('COSIN= '), disp(COSIN)
                                    
                                 end
                                 
                                 for J=1:NV
                                    
                                    SALVO(J) = DLX(J);
                                    DLX(J) = X(J)-XOSC(J,K);
                                                         
                                 end
                                 
                                 FPREV = FOSC(K);
                                 tag_390 = 1;				%stay in 390 loop, accomplished with  tag=0 and very next IF statement below
                                 tag = 0
                                 
                              end
                              
                           end
                           
                        end
                        
                        if tag == 0
                           
                           tag_400 = 0;						%Break out of 400 LOOP
                           tag_390 = 1;						%ARRR! Stay in tha 390 loop, matey
                           
                        else
                           
                           tag_400 = 0;						%Break out of 400 LOOP
                           tag_380 = 1;
                           tag_390 = 0;							%break out of 390 loop, renter 380 loop
                                                
                        end
                        
                     end
                     
                  elseif NRETRY == 0
                     
                     tag_400 = 0;								%Break out of 400 LOOP
                     tag_380 = 1;
                     tag_390 = 0;								%%Kick out of 390 loop, reenter 380 loop
                                                 
                  else
                     
                     NOSC = NOSC -1;
                     
                     if NOSC<0
                        
                        NOSC = 0;
                        
                     end
                     
                     FSTORE(JX) = FBEST;
                     
                     if NF>NFMAX
                        
                        KFLAG = -2;
                        %disp('ENTERING KFLAG EXIT FROM 380 2')
                        %pause
                        private_KFLAG_EXIT(KFLAG);
                        tag_400 = 0;
                        tag_380 = 0;
                        tag_390 = 0;							%To ensure that I break out of all loops when to exit properly.
                                                                                                                                                        
                     else
                        
                        tag_400 = 0;							%Break out of 400 LOOP
                        tag_380 = 0;
                        tag_390 = 0;							%%Kicks me out of while loop, truly stay in big FOR loop
                        %disp('truly stay in for loop from 380')
                        %pause
                                                      
                     end
                     
                  end
                  
               else
                  
                  CINDER = ((FPREV-FBEST)*ACK + (FBEST-FOBJ)/ACK)/(DENOM+DENOM);
                  
                  for J=1:NV
                     
                     if MASK(J)~=0
                        
                     else
                        
                        X(J) = XS(J) + CINDER*DLX(J);
                        
                        if X(J)>XMAX(J)
                           
                           X(J) = XMAX(J);
                           
                        end
                        
                        if X(J)<XMIN(J)
                           
                           X(J) = XMIN(J);
                           
                        end
                        
                     end
                     
                  end
                  
                  JOCK = 0;
                  JVARY = 0;
                  private_FUNK(1);
                  NF = NF+1;
                                       
                  if FOBJ>=FBEST										%quadratic section statement
                     
                     for J=1:NV
                        
                        if NRETRY>=0
                           
                           DLX(J) = SALVO(J);
                           
                        end
                        
                        X(J) = XS(J);
                        
                     end
                     
                     if NTRACE>=1
                        
                        disp('FOBJ= '),disp(FOBJ),disp(' AFTER '),disp(NGIANT),disp('GIANT STEPS')
                        
                     end
                     
                     if NGIANT>0
                        
                        tag_400 = 0;								%Break out of 400 LOOP
                        tag_380 =0;
                        tag_390 = 0;	
                        NCIRC = 0;												%Call to 640 ROUTINE!!!!
                        NZIP = 0;
                        NACK = 0;
                        grandtag = 1;
                        fortag = 0;
                        %disp('call to 640 routine from 380')
                        %pause
                        
                     elseif NRETRY>0
                        
                        if KL>NAH
                           
                           tag_400 = 0;							%Break out of 400 LOOP
                           tag_380 = 1;
                           tag_390 = 0;							%break out of 390 loop, renter 380 loop
                           
                        else
                           
                           tag = 1;
                           
                           for K=KL:NAH
                              
                              if tag == 1
                                 
                                 NRETRY = NAH - K;
                                 COSIN = RZERO;
                                                    
                                 for J=1:NV
                                    
                                    COSIN = COSIN + ERR(J,NOSC)*ERR(J,K);
                                    
                                 end
                                 
                                 if (K>=(NOSC-1))|((COSIN>RZERO)&(COSIN>COXOM))
                                    
                                    KL = KL+1;
                                    NT = NOSC-K;
                                                       
                                    if NTRACE>=1
                                       
                                       disp('*****GIGANTIC STEP WITH PERIOD '),disp(NT),disp(' BEING ATTEMPTED.')
                                       disp('COXCOM= '),disp(COXCOM)
                                       disp('COSIN= '), disp(COSIN)
                                       
                                    end
                                    
                                    for J=1:NV
                                       
                                       SALVO(J) = DLX(J);
                                       DLX(J) = X(J)-XOSC(J,K);
                                                         
                                    end
                                    
                                    FPREV = FOSC(K);
                                    tag_390 = 1;				%stay in 390 loop, accomplished with  tag=0 and very next IF statement below
                                    tag = 0;
                                    
                                 end
                                 
                              end
                              
                           end
                           
                           if tag == 0
                              
                              tag_400 = 0;						%Break out of 400 LOOP
                              tag_390 = 1;						%ARRR! Stay in tha 390 loop, matey
                           
                           else
                              
                              tag_400 = 0;						%Break out of 400 LOOP
                              tag_380 = 1;
                              tag_390 = 0;							%break out of 390 loop, renter 380 loop
                              
                           end
                           
                        end
                        
                     elseif NRETRY == 0
                        
                        tag_400 = 0;								%Break out of 400 LOOP
                        tag_380 = 1;
                        tag_390 = 0;								%%Kick out of 390 loop, reenter 380 loop
                        
                     else
                        
                        NOSC = NOSC -1;
                        
                        if NOSC<0
                           
                           NOSC = 0;
                           
                        end
                        
                        FSTORE(JX) = FBEST;
                        
                        if NF>NFMAX
                           
                           KFLAG = -2;
                           %disp('ENTERING KFLAG EXIT from 380 3')
                           %pause
                           private_KFLAG_EXIT(KFLAG);
                           tag_400 = 0;
                           tag_380 = 0;
                           tag_390 = 0;							%To ensure I leave all loops and exit properly
                           
                        else
                           
                           tag_400 = 0;							%Break out of 400 LOOP
                           tag_380 = 0;
                           tag_390 = 0;							%%Kicks me out of while loop, TRULY CONTINUE IN BIG FOR LOOP
                           %disp('truly continuing in for loop from 380')
                           %pause
                           
                        end
                        
                     end
                     
                  else
                     
                     FBEST = FOBJ;
                     JOCK = 1;
                     STEPS = NGIANT;
                     STEPS = STEPS + CINDER;
                  
                     if NTRACE>=1
                        
                        disp('FOBJ= '),disp(FOBJ),disp(' AFTER '),disp(STEPS),disp(' GIANT STEPS.')
                        disp('X(J)= ')
                                          
                        for J=1:NV
                           
                           disp(X(J))
                           
                        end
                        
                     end
                     
                     tag_400 = 0;			%Break out of 400 LOOP
                     tag_390 = 0;
                     tag_380 = 0;			%Breaks me out of while loop, keeps me in big FOR loop
                     NCIRC = 0;							%CALL TO 640 ROUTINE!!!!!!
                     NZIP = 0;
                     NACK = 0;
                     %disp('call to 640 from 380')
                     %pause
                     
                  end
                  
               end								%Ends quadrature section
               
            end
               
         else
            
            FPREV = FBEST;
            FBEST = FOBJ;
         
            for J=1:NV
               
               DLX(J) = DLX(J)*ACK;
               
            end
            
            NGIANT = NGIANT+1;
            
            if NTRACE<1
               
               tag_400 = 1;						%Stay in inner 400 LOOP
               
            elseif NGIANT>1
               
               disp('FOBJ= '),disp(FOBJ),disp(' NF= '),disp(NF)
               disp('X(J)= ')
                                       
               for J=1:NV
                  
                  disp(X(J))
                  
               end
               
               tag_400 = 1;						%Stay in inner 400 LOOP
               
            else
               
               disp('No of steps = ')
               
               for J=1:JX
                  
                  disp(VEC(J))
                  
               end
               
               disp('FOBJ= '),disp(FPREV),disp(' NF= '), disp(NSAVE),
               disp('X(J) = ')
                                       
               for J=1:NV
                  
                  disp(X(J))
                  
               end
               
               tag_400 = 1;						%Stay in inner 400 LOOP
               
            end
            
         end
         
      end
      
   end
   
end											%End of the huge while loops

%disp('EXITING 380')
%pause
