function [fit, vaf, coord, addcon] = linfitac_fortran(prox,inperm)

%LINFITAC does a confirmatory fitting of a given unidimensional order
%  using the Dykstra-Kaczmarz iterative projection least-squares method, 
%  but differing from LINFIT.M in including the estimation of an additive
%  constant.
%  INPERM  is the given order;
%  FIT is an $n \times n$ matrix that is fitted to PROX(INPERM,INPERM) with
%  variance-accounted-for VAF;
%  COORD gives the ordered coordinates whose absolute differences
%  could be used to reconstruct FIT; ADDCON is the estimated additive constant
%  that can be interpreted as being added to PROX.

tic
n=size(prox,1);
acondiff = 1.0;
addcon = 0.0;
proxpermute = prox(inperm,inperm);

[fit,addcon] = linfitac_for(proxpermute,n);

 
  aveprox = sum(sum(prox))/(n*(n-1));

for i = 1:n
   for j = 1:n
      if( i ~= j)
         proxave(i,j) = aveprox;
         fmadd(i,j) = fit(i,j) - addcon;
      else
         proxave(i,j) = 0;
         fmadd(i,j) = 0.0;
      end
   end
end

diff = sum(sum((prox - fmadd).^2));

denom = sum(sum((prox - proxave).^2));

vaf = 1 - (diff/denom);

coord = zeros(n,1);

for i = 1:n
    for j = 1:n
        if(i>j)
            coord(i) = coord(i) + fit(i,j);
        end
        if(i<j)
            coord(i) = coord(i) - fit(i,j);
        end
    end
    
    coord(i) = coord(i)/n;
end

toc
