%%%%% MakeProblemToe 
%
%Create random Teoplitz cycle completion problem as in:
%Corollary 6 of Barrett, Johnson, Tarazaga (1992)
%reference [2] in our manuscript
%
%Sremac, Woerdeman, and Wolkowicz
%2020

%n := order of matrices
%feas := 1-pd completable, 2-psd completable, else-not completable
%svind := to speed up ssvec
%theta, phi := angles for specified values of matrix

function [A,b,X0,Exp,theta,phi] = MakeProblemToe(n,feas,svind,theta,phi)

temp = factor(n);       %factor n to determine if it is even or odd
party = temp(1);

if ~exist('phi','var')
    phi = pi*rand(1);       %random choice for phi
end

if ~exist('theta','var')
    if party==2       %if n is even
        if feas==1      %if a pd completable partial matrix is desired
            theta = phi + ((n-2)*pi)*rand(1);
            theta = theta/(n-1);
        elseif feas==2      %if a psd completable partial matrix desired
            theta = phi/(n-1);
        else                %if a non-completable partial matrix is desired
            theta = (phi/(n-2))*rand(1);
        end
    else                    %if n is odd
        if feas==1
            theta = phi + ((n-1)*pi - 2*phi)*rand(1);
            theta = theta/(n-1);
        elseif feas==2      %if a psd completable partial matrix desired
            theta = phi/(n-1);
        else                %if a non-completable partial matrix is desired
            theta = (phi/(n-2))*rand(1);
        end
    end
end

if party==2
    if phi>(n-1)*theta || (n-1)*theta>(n-2)*pi+phi
        fprintf('No completion for given theta and phi. \n')
    end
else
    if phi>(n-1)*theta || (n-1)*theta>(n-1)*pi-phi
        fprintf('No completion for given theta and phi. \n')
    end
end
    
tn = n*(n+1)/2;
m = 2*n;   % number of constraints
A = zeros(m,tn);
b = zeros(m,1);
tt=0;
In=eye(n);
for ii=1:n-1       %all the constraints except the (1,n) element
    temp=In(:,ii)*In(:,ii)';        %elements on the diagonal
    A(ii,:)=ssvec(temp,svind)';
    b(ii) = 1;
    temp=In(:,ii)*In(:,ii+1)';      %elements on the off-diagonal
	temp=temp+temp';
    A(ii+n,:)=ssvec(temp,svind)';
    b(ii+n) = 2*cos(theta);
end

temp=In(:,n)*In(:,n)';        %the (n,n) element
A(n,:)=ssvec(temp,svind)';
b(n) = 1;
temp=In(:,1)*In(:,n)';      %the (n,1) element
temp=temp+temp';
A(2*n,:) = ssvec(temp,svind)';
b(2*n) = 2*cos(phi);

%%%%%%%%%%%%%% Feasible point, 0's in the unknown elements
%X0 = In + diag(cos(theta)*ones(n-1,1),1);
X0 =  diag(cos(theta)*ones(n-1,1),1);
X0(1,n) = cos(phi);
X0 = (X0 + X0')+In;

if abs(phi-pi)<1e-10 && abs(theta-pi/(n-1))<1e-10 %special case
    for jj=2:n-2
        %keyboard
        X0 = X0 + diag(cos(pi*(-jj)/(n-1))*ones(n-jj,1),jj) + ...
            diag(cos(pi*(-jj)/(n-1))*ones(n-jj,1),-jj);
    end
    X0 = (X0 + X0')/2;
end
%%%%%%%%%%%%%% Exposing vector
Exp=zeros(n);               %the exposing vector is just the zero matrix

end     %end function










