function [A,noupdt]=secfac(n,x,g,A,xp,gp,epsm,itncnt,rnf,iagflg,noupdt)
if any(imag(gp)) | any(imag(xp)) | any(imag(g)) | any(imag(x));
  disp('g,gp,xp,x=')
  [g,gp,xp,x]
end;
if itncnt==1; noupdt=1; end;
s=xp-x;
y=gp-g;
den1=sum(s.*y);
snorm2=norm(s);
ynrm2=norm(y);
if den1>=sqrt(epsm)*snorm2*ynrm2;
  u=triu(A')*s;
  den2=sum(u.*u);
  alp=sqrt(den1/den2);
  if noupdt;
    u=alp*u;
    A=A+(alp-1)*tril(A);
    noupdt=0;
    den2=den1;
    alp=1;
  end;
  skpupd=1;
  w=tril(A)*u;
  if iagflg==0;
    reltol=sqrt(rnf);
  else;
  %  reltol=rnf;
    reltol=sqrt(rnf);
  end;
  if any(abs(y-w)>=reltol*max(g,gp)); 
    skpupd=0;
  else;
    A=triu(A)+triu(A,1)';
    return; 
  end;
  w=y-alp*w;
  alp=alp/den1;
  u=alp*u;
  A=tril(A)';
  k=1;
  tem=find(u~=0);
  if ~isempty(tem); k=tem(length(tem)); end;
  if k>1;
    for i=1:k-1;
      j=k-i;
      if u(j)==0;
        tem=A(j+1,j:n);
        A(j+1,j:n)=A(j,j:n);
        A(j,j:n)=tem;
        u(j)=u(j+1);
      else;
        tem=sqrt(u(j)*u(j)+u(j+1)*u(j+1));
        t1=u(j)/tem;
        t2=-u(j+1)/tem;
        tem1=A(j,j:n);
        A(j,j:n)=t1*tem1-t2*A(j+1,j:n);
        A(j+1,j:n)=t2*tem1+t1*A(j+1,j:n);
        u(j)=tem;
      end;
    end;
  end;
  A(1,:)=A(1,:)+u(1)*w(:)';
  if k>1;
    for i=1:k-1;
      if A(i,i)==0;
        tem=A(i+1,i:n);
        A(i+1,i:n)=A(i,i:n);
        A(i,i:n)=tem;
      else;
        tem=sqrt(A(i,i)*A(i,i)+A(i+1,i)*A(i+1,i));
        t1=A(i,i)/tem;
        t2=-A(i+1,i)/tem;
        tem=A(i,i:n);
        A(i,i:n)=t1*tem-t2*A(i+1,i:n);
        A(i+1,i:n)=t2*tem+t1*A(i+1,i:n);
      end;
    end;
  end;
  A=triu(A)+triu(A,1)';
end;   

if ~isempty(A);
  aa=imag(A);
  if sum(any(aa));
    disp('imaginary part in A on exit in secfac--second')
  end;
end;
