function dd = diag_red(K)

%function dd = diag_red(K)
%
%Finds the largest positive diagonal values dd that can be subtracted from
%the diagonal of K while leaving it positive semi definite.
%
%INPUTS
% K = the kernel of which the diagonal should be reduced
%
%OUTPUTS
% dd = the (positive) values that can be subtracted from the diagonal while
%      leaving the kernel positive semi definite
%
%
%Makes use of SeDuMi.
%(Ref: Kandola et al.)

%Author: Tijl De Bie, februari 2004.

working_path=pwd;

%y = [dd], dimension n
%
% max b'y
% s.t. c-A'y >= 0

n=size(K,1);

% object function: sum(dd)
b = [ones(n,1)];

c = [zeros(n,1) ; vec(K)];

%linear constraints

mAt=sparse([ speye(n) ; sparse(n^2,n,n)]);

%sdp constraints

for i=1:n
    mAt(n+i+n*(i-1),i)=-1;
end

K.l=[n];
K.s = [n];
%cd 'C:\matlab\toolbox\SDPT3-3.02'; A=-mAt'; save sedumiformat.mat A c b K; startup; [blk,AA,CC,b] = read_sedumi('sedumiformat.mat'); [obj,primalsol,dualsol,Z] = sqlp(blk,AA,CC,b);
cd C:\matlab\work; [primalsol, dualsol, info] = sedumi(-mAt',b,c,K);
cd(working_path)

dd=dualsol;
cd 'C:\Documents and Settings\Tijl\My Documents\werk\mfiles-matfiles-datafiles\qcqp_kernel-learning'
