-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathregfun.m
40 lines (37 loc) · 911 Bytes
/
regfun.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
% OLD VERSION:
function out = regfun(x,abstol,reltol)
[V,d] = eig(x,'vector');
lb = max(abstol,reltol*max(d));
good = (d >= lb);
if all(good)
out = x;
else
d(~good) = lb;
out = V * diag(d) * V.';
if ~ issymmetric(out)
out = (out+out.')/2;
end
end
% NEW VERSION: add a multiple of the identity matrix to improve
% conditioning
% Input:
% x: symmetric square matrix
% abstol: smallest eigenvalue of regularized matrix
% inverse of maximum condition number of regularized matrix
% function out = regfun(x,abstol,reltol)
% [v,d] = eig(x,'vector');
% out = x;
% if any(d < abstol)
% d(d < abstol) = abstol;
% out = v * diag(d) * v.';
% if ~issymmetric(out)
% out = 0.5 * (out+out.');
% end
% end
% dmax = max(d);
% dmin = min(d);
% lam = max(abstol-dmin,(reltol*dmax-dmin)/(1-reltol));
% if lam > 0
% out = out + diag(repelem(lam,numel(d)));
% end
% end