function Info = estab_OP07_IEEETAC_T3_yal(Ai,deg,d)
%function Info = estab_OP07_IEEETAC_T3_yal(Ai,deg,d)
% test the Schur stability of the polytope Ai via a homogeneous
% polynomially parameter-dependent Lyapunov function of arbitray degree
% using Polya's relaxations. Theorem 3 of (Oliveira & Peres, IEEE-TAC 2007)
%input:  Ai      -> vertices of the polytope (Ai = [A1 A2 ... A3])
%        deg     -> degree of the polynomially parameter dependent Lyapunov function
%        d       -> number of P�lya's relaxations
%output: Info.feas     -> feasible (1) or not (0)
%        Info.cpusec   -> cpu time to solve the LMIs (seconds)
%        Info.cpusec_m -> cpu time to mount the LMIs (seconds)
%        Info.Pi       -> Solution variables P
%        Info.K        -> number of scalar variables
%        Info.L        -> number of LMI rows
%
%example: 2 states and 2 vertices
% A1 = [-0.1  1.1;   
%       -0.8  -0.3];  
% A2 = [-0.6  0.6;
%       0.4   -0.3];
% Ai = [A1 A2];
%Info = estab_OP07_IEEETAC_T3_yal(Ai,1,0); %affine parameter-dependent Lyapunov matrix
%
% Date: 13/03/2007
% Author: ricfow@dt.fee.unicamp.br   
       
%determine the number of vertices of polytope
[order,vertices] = size(Ai);
vertices = vertices / order;

Info.cpusec_m = clock;

%new LMI system
LMIs = set([]);
Info.L = 0;

%generate the exponents of the monomials to create the variables P_k
K_g = gen_coefs(vertices,deg);
number_VAR = size(K_g,1);%number of variables

%generate the variables
for i=1:number_VAR
    mon_string = string_monomial(K_g(i,:));
    eval(['P',mon_string,' = sdpvar(order,order,''symmetric'');']);
end

Kfr_d = gen_coefs(vertices,d);
size_Kfr_d = size(Kfr_d,1);
Kfr_g_d_1 = gen_coefs(vertices,deg + d + 1);
number_LMIs = size(Kfr_g_d_1,1);

% [P(alplha) P(alpha)A(alpha); A(alpha)'P(alpha) P(alpha)] > 0
for i=1:number_LMIs

    k = Kfr_g_d_1(i,:);

    Term = zeros(2*order);
    for j=1:size_Kfr_d
        kprime = Kfr_d(j,:);
        if size(find(k-kprime>=0),2) == vertices
            Cfr  = factorial(d)/Pi(kprime);
            for p=1:vertices
                if k(p) > kprime(p)
                    e = zeros(1,vertices);
                    e(p) = 1;

                    A = Ai(:,(p-1)*order+1:p*order);
                    mon_string = string_monomial(k-kprime-e);
                    P = eval(['P',mon_string]);

                    Term = Term + Cfr*([P P*A;A'*P P]);
                end
            end
        end
    end
    LMIs = LMIs + set(Term > 0);
    Info.L = Info.L + 2*order;
end

Info.cpusec_m = etime(clock,Info.cpusec_m);

Info.K = size(getvariables(LMIs),2);

sol = solvesdp(LMIs,[],sdpsettings('verbose',0,'solver','sedumi'));
Info.cpusec = sol.solvertime;
[p,d]=checkset(LMIs);

Info.feas = 0;
%capturing the solutions (if ones exist)
if sum(p < 0) == 0
    Info.Pi   = [];
    for i=1:number_VAR
        mon_string = string_monomial(K_g(i,:));
        Info.Pi = [Info.Pi double(eval(['P',mon_string]))];
    end
    Info.feas = 1;
end

%___________________________________________________________________________________________________
function R = Pi(monomial)

R=prod(factorial(monomial));

%___________________________________________________________________________________________________
function [comb] = gen_coefs(vertices,degree)
%generates all the solutions of: a_1 + a_2 + ... + a_vertices = degree

name = strcat('comb_N',int2str(vertices),'d',int2str(degree),'.mat');
if exist(name,'file') == 2
    load(name);
else
    comb = [];
    if vertices == 1
        comb = degree;
    else
        for i = 0:degree
            temp = gen_coefs(vertices-1,degree-i);
            comb   = [ comb
                i*ones(size(temp,1),1) temp ];
        end
    end
end

%___________________________________________________________________________________________________
function [string] = string_monomial(monomial)

string = '';
for i=1:size(monomial,2)
    string = strcat(string,'_',int2str(monomial(1,i)));
end