%% Localization in the matrix exponential.
% In order for local solution techniques to be feasiblely efficient, we
% need to make sure that the solution has a strong localization.  Here, we
% show this effect for the matrix exponential 

%% Setup
addpath('../mcode');

%% Load data
A = readSMAT('../data/pgp-cc.smat');
n = size(A,1);
L = nlaplacian(A);

%% Compute the matrix exponential
M = expm(L);

%%
ipar = sum(M.^4)./sum(M.^2).^2;

neffstates = round(1./ipar);
plot(sort(neffstates));


%% Participation ratios
% The inverse participation ratios show
tol = 0.99;
locs = zeros(n,1);
for i=1:n
    % find the localization at tol
    cdf = cumsum(sort(abs(M(:,i)),'descend'));
    cdf = cdf/sum(abs(M(:,i)));
    locs(i) = find(cdf > tol, 1,'first');
end

%%
plot(sort(locs,'descend'))
%%
figure(1); clf;
[x,h] = dhist(locs);
semilogx(x,h,'.');
xlabel({'Localization', 'at 99% of mass'});
grid on;
set(gca,'GridLineStyle','-')
set(gca,'XMinorGrid','off');
set(gca,'YMinorGrid','off');
set(gca,'XTick',[1,10,100,1000,10000]);
set_figure_size([1.5 2]);
box off;
greygrid(gca,[0.75,0.75,0.75])

%%
% pd = fitdist(locs,'inversegaussian');
% hold on;
% plot(15000*pdf(pd,x),'k-');
% hold off;

print('pgp-cc-localization.eps','-depsc2');
