function MSAi = affine2MS_LTI(Atheta,bound_theta)
% function MSAi = affine2MS_LTI(Atheta,m)
% converts a time-invariat polynomial matrix given in the affine form, i.e.
% A(theta)= A_0 + theta_1A_1 + ... theta_mA_m,  tmin_i < theta_i < tmax_i
% to a polynomial matrix given in the multi-simplex representation. The following change
% of variables is performed: theta_i = (tmax_i-tmin_i)alpha_{i1}+tmin_i,  alpha_{i2}=1-alpha_{i1}, i=1,..,m
% and subsequently, the resulting matrix is homogenized accordingly.
%
% inputs: Atheta -> Matrix coefficients of the affine model [A0 A1 ... Am]
%         bound_theta -> bounds of the parameters: [theta_min_1 theta_max_1;
%                                                   theta_min_2 theta_max_12
%                                                      ..           ..
%                                                   theta_min_m theta_max_m];
%
% outputs: MSAi  -> Homogeneous polynomial matrix in the multi-simplex representation.
%
%
% Example: affine model A(theta)=A0 + A_1theta_1, |theta_1|< 1,   with
% A0 = [-4 2 -2;5 -6 1; -2 2 -7];
% A1 = [-5 -3 -13;-5 0 0;10 13 16];
% Atheta = [A0 A1];
% bounds = [-1 1];
% MSAi = affine2MS_LTI(Atheta,bounds)
%
% Date: 18/02/2008
% Author: ricfow@dt.fee.unicamp.br
%

N=size(bound_theta,1);
lin = size(Atheta,1);
col = size(Atheta,2)/(N+1);
if rem(col,1) ~= 0
    fprintf('Dimensions of the system vertices do not fit !\n');
    MSAi = [];
    return;
end

Ai_ini = Atheta;
Atheta = Ai_ini(:,1:col);
for i=1:N
    Atheta = Atheta +bound_theta(i,1)*Ai_ini(:,(i+1-1)*col+1:(i+1)*col);
end
for i=1:N
    Atheta = [Atheta (bound_theta(i,2)-bound_theta(i,1))*Ai_ini(:,(i+1-1)*col+1:(i+1)*col)];
end

for i=1:N
    MS{i} = create_MSpolynomial([lin lin],1,i,1,2,'I');    
    j= i+1;    
    coeff.mon{1} = [1; 0];
    coeff.value{1} = Atheta(:,(j-1)*col+1:j*col);
    coeff.mon{2} = [0; 1];
    coeff.value{2} = zeros([lin col]);
    MSs{i} = create_MSpolynomial([lin col],1,i,1,2,'I',coeff);    
end

coeff2.to_all = 1;
coeff2.val_all = Atheta(:,1:col);
MSf{1} = create_MSpolynomial([lin col],1,1,1,2,'A0',coeff2);
for i=2:N
    MSf{1} = multiply_MSpoly(MS{i},MSf{1});
end

for i=1:N
    empty = 1;
    for j=1:N
        if j == i
            if empty == 1
                MSf{i+1} = MSs{j};
                empty = 0;
            else
                MSf{i+1}  = multiply_MSpoly(MSf{i+1},MSs{j});
            end
        else
            if empty == 1
                MSf{i+1} = MS{j};
                empty = 0;
            else
                MSf{i+1}  = multiply_MSpoly(MS{j},MSf{i+1});
            end
        end
    end
end

coeff2.val_all = zeros([lin col]);
MSAi = create_MSpolynomial([lin col],N,[1:N],ones(1,N),2*ones(1,N),'A',coeff2);

for i=1:MSAi.nmon
    coef_val = zeros([lin col]);
    mon_atual = MSAi.mon(:,i);
    for j=1:N+1
        for k=1:MSf{j}.nmon
            mon_comp = MSf{j}.mon(:,k);
            if sum(mon_atual == mon_comp) == size(mon_atual,1)
                coef_val = coef_val + MSf{j}.coef{k}.coef_val;
                break;
            end
        end
    end
    MSAi.coef{i}.coef_val = coef_val;
end
