function p=mc_fixpt(Pt)
% mc_fixpt - fixed point of Markov chain (stationary state distribution)
%
% mc_fixpt :: [[K,K]]~'transition matrix'-> [[K]].


% for ergodic HMM we want to get stationary distribution 
% use eigenvalues of transition matrix.
[V,D]=eig(Pt);

% one of the eigenvalues should be 1
[dummy,k]=max(-abs(diag(D)-1));

% check that it is really close
if abs(D(k,k)-1)>0.001
	disp('mc_fixpt: failed to find eigenvalue of 1 in ');
	disp(Pt)
	error('mc_fixpt:noeig','failed to find eigenvalue of 1');
end

% get eigenvector and flip it over if all negative
v=V(:,k); if sum(v)<0, v=-v; end

if ~isreal(v)
	disp('mc_fixpt: discarding complex parts of eigenvector'); 
	v=real(v);
end
if any(v<0)
	%fprintf('mc_fixpt: discarding negative parts of eigenvector: %s\n',mat2str(v(v<0))); 
	v=max(0,v);
end

% final normalisation
p=v/sum(v);
