function sys = ss2ss(sys,T)
%SS2SS  Change of state coordinates for state-space models.
%
%   SYS = SS2SS(SYS,T) performs the similarity transformation z = Tx on the
%   state vector x of the state-space model SYS. The resulting state-space 
%   model is described by:
%
%               .       -1        
%               z = [TAT  ] z + [TB] u
%                       -1
%               y = [CT   ] z + D u
%
%   or, in the descriptor case,
%
%           -1  .       -1        
%       [TET  ] z = [TAT  ] z + [TB] u
%                       -1
%               y = [CT   ] z + D u  .
%
%   SS2SS is applicable to both continuous- and discrete-time models. For 
%   arrays of state-space models, the transformation T is applied to each 
%   individual model in the array. For GENSS or USS models, the transformation 
%   T is applied to the state vector of the interconnection model (first 
%   output argument of GETLFTMODEL). For MECHSS models, the transformation
%   T is applied to the vector q of displacements.
%
%   See also CANON, BALREAL, SS, SPARSS, MECHSS, GENSS, GETLFTMODEL.

%	 Clay M. Thompson, P. Gahinet
%   Copyright 1986-2010 The MathWorks, Inc.
narginchk(2,2)
if nmodels(sys)==0
   return
end

% Check dimensions
tsizes = size(T);
if length(tsizes)>2 || tsizes(1)~=tsizes(2)
   error(message('Control:transformation:ss2ss1'))
end

try
   if issparse(sys)
      sys = ss2ss_(sys,T);
   else
      % LU decomposition of T
      [l,u,p] = lu(T,'vector');
      % Perform coordinate transformation
      sys = ss2ss_(sys,T,l,u,p);
      % Issue warning if T ill conditioned
      if rcond(u)<eps && ~isequal(T,diag(diag(T)))
         warning(message('Control:transformation:Ss2ssAccuracy'))
      end
   end
catch E
   ltipack.throw(E,'command','ss2ss',class(sys))
end