function [c,s,y]=GeneralizedGivensRotation(x,sig);
% GENERALIZEDGIVENSROTATION computes a generalized Givens rotation
%   [c,s,y]=GeneralizedGivensRotation(x,sig); computes for the given
%   number x(1), x(2) and sig a generalized Givens rotation to obtain
%   G'x = y = [r, sig]'

scale=max(abs(x));
if scale==0,
   c=1; s=0;
else
  x=x/scale; sig=sig/scale;
  norm2=norm(x);
  r=(norm2-sig)*(norm2+sig);
  if r<0, error('rotation does not exists'); end
  r=sqrt(r); norm2=norm2^2;
  s=([sig -r]*x)/norm2; 
  c=([r sig]*x)/norm2;
  y=[scale*r; sig];
end