function [invmat] = safeinv(mat,thresh)

% mat = (mat + mat')/2;
% 
% if(nargin==1)
%     thresh=1e-20;
% end
% 
% [U,S,V] = svd(mat);
% 
% S = diag(S);
% 
% index = find(S<=thresh);
% 
% S(index) = thresh * ones(length(index),1);
% 
% invmat = V * diag(1./S) * U';
% 
% invmat = (invmat + invmat')/2;

mat = symmat(mat);

% invmat = inv(mat);

%mat = symmat(mat);

%mat = symmat(mat);

%m = mat - mat'


invmat = cholinv(mat);

% invmat - invmat'
