      subroutine linfitac_for(fit,vaf,coord,addcon,prox,inperm,n)
      
      integer n,i,j,index,jone,jtwo,jthree
      integer inperm(n)
      real*8 vaf,addcon,acondiff,cr,p1,p2,p3,addconpv,del
      real*8 denom,diff,aveprox
      real*8 prox(n,n),fit(n,n),coord(n),work(n*(n-1)*(n-2))
      real*8 fmadd(n,n),proxave(n,n)
       


      acondiff = 1.0
      addcon = 0.0

      do while (acondiff.gt.(.00001))

      do i = 1,n
         do j = 1,n

            if(i.ne.j) then

               fit(i,j) = prox(inperm(i),inperm(j)) + addcon

            end if
            
            if(i.eq.j) then

               fit(i,j) = 0.0

            end if

         end do
      end do
   
    
      
      addconpv = addcon

      do i = 1,(n*(n-1)*(n-2))

         work(i) = 0.0

      end do
 
      cr = 1.0

      do while (cr.gt.(.000001))
   
      cr = 0.0
      index = 0
   
      do jone = 1,(n-2)
         do jtwo = (jone+1),(n-1)
            do jthree = (jtwo+1),n
            
            p1 = fit(jone,jtwo)
            p2 = fit(jone,jthree)
            p3 = fit(jtwo,jthree)
            
            fit(jone,jtwo) = fit(jone,jtwo) - work(index+1)
            fit(jone,jthree) = fit(jone,jthree) - work(index+2)
            fit(jtwo,jthree) = fit(jtwo,jthree) - work(index+3)
            
            del = (fit(jone,jthree) - fit(jone,jtwo) -
     &      fit(jtwo,jthree))/3.0
            
            fit(jone,jthree) = fit(jone,jthree) - del
            fit(jone,jtwo) = fit(jone,jtwo) + del
            fit(jtwo,jthree) = fit(jtwo,jthree) + del
            
            work(index+1) = del
            work(index+2) = -del
            work(index+3) = del
            
            index = index + 3
            
            cr = cr + abs(p1-fit(jone,jtwo)) +
     &      abs(p2-fit(jone,jthree))
     &      + abs(p3 - fit(jtwo,jthree))
            
            end do
         end do
      end do
   
      do jone = 1,(n-1)
         do jtwo = (jone+1),n
         
         p1 = fit(jone,jtwo)
         
         fit(jone,jtwo) = fit(jone,jtwo) - work(index+1)
         
         if(fit(jone,jtwo).lt.(0.0)) then
            
            work(index+1) = -fit(jone,jtwo)
            fit(jone,jtwo) = 0.0

          end if
            
          if(fit(jone,jtwo).ge.(0.0)) then
            
            work(index+1) = 0.0
            
          end if
         
         index = index + 1
         
         cr= cr + abs(p1-fit(jone,jtwo))

          end do
        end do

      end do



      do jone = 1,(n-1)
         do jtwo = (jone+1),n
                  
         fit(jtwo,jone) = fit(jone,jtwo)
         
         end do
      end do

      addcon = 0.0

      do i = 1,n
         do j = 1,n

            addcon = addcon - 
     &      (prox(inperm(i),inperm(j)) - fit(i,j))/(n*(n-1))

         end do
      end do

      acondiff = abs(addcon - addconpv)

      end do


      aveprox = 0.0

      do i = 1,n
        do j = 1,n

         aveprox = aveprox + (prox(i,j)/(n*(n-1)))

         end do
      end do

      do i = 1,n
         do j = 1,n

         if(i.ne.j) then

         proxave(i,j) = aveprox
         fmadd(i,j) = fit(i,j) - addcon

         end if

         if(i.eq.j) then

         proxave(i,j) = 0.0
         fmadd(i,j) = 0.0

         end if

         end do
      end do

      diff = 0.0
      denom = 0.0

      do i = 1,n
         do j =1,n

         diff = diff + (prox(inperm(i),inperm(j)) - 
     &          fmadd(i,j))**2

         denom = denom + (prox(inperm(i),inperm(j)) -
     &           proxave(i,j))**2


      vaf = 1 - (diff/denom)

         end do
      end do

      do i = 1,n
         coord(i) = 0.0
      end do


      do i = 1,n
         do j = 1,n

        if(i.gt.j) then
            coord(i) = coord(i) + fit(i,j)
        end if

      
        if(i.lt.j) then
            coord(i) = coord(i) - fit(i,j)
        end if
      end do
    
         coord(i) = coord(i)/n

      end do

      return
      end     
