-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSCgetPinv.m
122 lines (109 loc) · 3.15 KB
/
SCgetPinv.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function Minv = SCgetPinv(M,varargin)
% SCgetPinv
% =========
%
% NAME
% ----
% SCgetPinv - Calculates the pseudo inverse of a matrix
%
% SYNOPSIS
% --------
% `Minv = SCgetPinv(M [, options])`
%
%
% DESCRIPTION
% -----------
% Calculats the pseudo-inverse `Minv` of the input matrix `M` based on a singular value
% decomposition. Optional parameters define the number of singular values to be set to zero, a
% global scaling factor applied to all singular values or the Tikhonov regularization parameter.
%
% INPUTS
% ------
% `M`:: Input matrix
%
%
% OPTIONS
% -------
% The following options can be given as name/value-pairs:
%
% `'N'` (0)::
% Number of singular values to be set to zero (starting from smallest value)
% `'damping'` ([])::
% If not empty, all singular values are scaled by this factor
% `'alpha'` ([])::
% If not empty, Tikhonov regularization is applied with regularization parameter `alpha`
% `'plot'` (0)::
% If true, initial and inverse singular value spectrum is plotted
%
% RETURN VALUE
% ------------
% `Minv`::
% Pseudo-inverse of the input matrix.
%
%
% EXAMPLES
% --------
% Calculates the svd-based pseudo-inverse
% ------------------------------------------------------------------
% Minv = SCgetPinv(M);
% ------------------------------------------------------------------
%
% Calculates the svd-based pseudo-inverse while cutting off the last 10 singular values.
% ------------------------------------------------------------------
% Minv = SCgetPinv(M,'N',10);
% ------------------------------------------------------------------
%
% Calculates the svd-based pseudo-inverse while cutting off the last 10 singular values and using a
% Tikhonov regularization with regularization parameter of 10.
% ------------------------------------------------------------------
% Minv = SCgetPinv(M,'N',10,'alpha',1);
% ------------------------------------------------------------------
%
% SEE ALSO
% --------
% *SCgetModelRM*
% Parse input
p = inputParser;
addOptional(p,'N',0);
addOptional(p,'alpha',[]);
addOptional(p,'damping',[]);
addOptional(p,'plot',0);
parse(p,varargin{:});
par = p.Results;
% Singular value decomposition
[U,S,V] = svd(M);
% Get singular values
SVs = diag(S);
D = zeros(size(S));
% Perform Tikhonov regularization
if ~isempty(par.alpha)
D(1:length(SVs),1:length(SVs)) = diag(SVs ./ (SVs .* SVs + par.alpha^2));
else
D(1:length(SVs),1:length(SVs)) = diag(1./SVs);
end
% Apply damping factor
if ~isempty(par.damping)
D = par.damping * D;
end
% Cut singular values
if par.N~=0
keep = length(SVs)-par.N;
D(keep+1:end,keep+1:end) = 0;
end
% Calculate final pseudo inverse.
Minv = V * D' * U';
if par.plot
figure(66);
subplot(1,2,1);
semilogy(diag(S)/max(diag(S)),'o--');
xlabel('Number of SV');ylabel('$\sigma/\sigma_0$');
subplot(1,2,2);
plot(diag(S).*diag(D),'o--')
xlabel('Number of SV');ylabel('$\sigma * \sigma^+$');set(gca,'yaxislocation','right')
set(findall(gcf,'-property','FontSize'),'FontSize',18);
set(findall(gcf,'-property','Interpreter'),'Interpreter','latex');
set(findall(gcf,'-property','TickLabelInterpreter'),'TickLabelInterpreter','latex');
set(gcf,'color','w');
drawnow
end
end