-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy patheig_geap.m
394 lines (333 loc) · 9.96 KB
/
eig_geap.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
function [varargout] = eig_geap(A,B,varargin)
%EIG_GEAP Shifted power method for generalized tensor eigenproblem.
%
% [LAMBDA,X] = EIG_GEAP(A,B) finds an eigenvalue (LAMBDA) and eigenvector
% (X) for the real tensor A and the positive definite tensor B such that
% Ax^{m-1} = lambda * Bx^{m-1}.
%
% [LAMBDA,X] = EIG_GEAP(A,B,parameter,value,...) can specify additional
% parameters as follows:
%
% 'Shift' : Shift for eigenvalue calculation (Default: 'Adaptive')
% 'Margin' : Margin for positive/negative definiteness in adaptive
% shift caluclation. (Default: 1e-6)
% 'MaxIts' : Maximum power method iterations (Default: 1000)
% 'Start' : Initial guess (Default: normal random vector)
% 'Tol' : Tolerance on norm of change in |lambda| (Default: 1e-15)
% 'Concave' : Treat the problem as concave rather than convex.
% (Default: true for negative shift; false otherwise.)
% 'Display' : Display every n iterations (Default: -1 for no display)
%
% [LAMBDA,X,FLAG] = EIG_GEAP(...) also returns a flag indicating
% convergence.
%
% FLAG = 0 => Succesfully terminated
% FLAG = -1 => Norm(X) = 0
% FLAG = -2 => Maximum iterations exceeded
%
% INFO = EIG_GEAP(...) returns a structure with the above plus other
% information, including the starting guess, the number of iterations,
% the final shift, the number of monotinicity violations, and a trace of
% the lambdas.
%
% REFERENCE: T. G. Kolda and J. R. Mayo, An Adaptive Shifted Power Method
% for Computing Generalized Tensor Eigenpairs, SIAM Journal on Matrix
% Analysis and Applications 35(4):1563-1581, December 2014,
% http://dx.doi.org/10.1137/140951758
%
% See also EIG_SSHOPM, TENSOR, SYMMETRIZE, ISSYMMETRIC.
%
%MATLAB Tensor Toolbox.
%Copyright 2015, Sandia Corporation.
% This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others.
% http://www.sandia.gov/~tgkolda/TensorToolbox.
% Copyright (2015) Sandia Corporation. Under the terms of Contract
% DE-AC04-94AL85000, there is a non-exclusive license for use of this
% work by or on behalf of the U.S. Government. Export of this data may
% require a license from the United States Government.
% The full license terms can be found in the file LICENSE.txt
%% Error checking on A
N = size(A,1);
%% Check inputs
p = inputParser;
p.addParamValue('Shift', 'adaptive', @(x) strcmpi(x,'adaptive') || isscalar(x));
p.addParamValue('MaxIts', 1000, @(x) isscalar(x) && (x > 0));
p.addParamValue('Start', [], @(x) isequal(size(x),[N 1]));
p.addParamValue('Tol', 1.0e-15, @(x) isscalar(x) && (x > 0));
p.addParamValue('Display', -1, @isscalar);
p.addParamValue('Concave', false, @islogical);
p.addParamValue('Margin', 1e-6, @(x) isscalar(x) && (x > 0));
p.addParamValue('SkipChecks', false);
p.parse(varargin{:});
%% Copy inputs
maxits = p.Results.MaxIts;
x0 = p.Results.Start;
shift = p.Results.Shift;
tol = p.Results.Tol;
display = p.Results.Display;
concave = p.Results.Concave;
margin = p.Results.Margin;
skipchecks = p.Results.SkipChecks;
%% Check inputs
if ~skipchecks
if ~issymmetric(A)
error('Tensor A must be symmetric')
end
if ~isempty(B)
if ~issymmetric(B)
error('Tensor B must be symmetric');
end
if ~isequal(size(A),size(B))
error('A and B must be the same size');
end
end
end
%% Check shift
if ~isnumeric(shift)
adaptive = true;
shift = 0;
else
adaptive = false;
end
%% Check starting vector
if isempty(x0)
x0 = 2*rand(N,1)-1;
end
if norm(x0) < eps
error('Zero starting vector');
end
%% Check concavity
if concave
beta = -1;
else
beta = 1;
end
if ~adaptive
if (shift < 0) && (beta == 1)
error('Set ''concave'' to true for a negative shift');
elseif (shift > 0) && (beta == -1)
error('Set ''concave'' to false for a positive shift');
end
end
%% Execute power method
if (display >= 0)
fprintf('Generalized Adaptive Tensor Eigenpair Power Method: ');
if (beta == -1)
fprintf('Concave ');
else
fprintf('Convex ');
end
fprintf('\n');
fprintf('---- --------- ----- ------------ -----\n');
fprintf('Iter Lambda Diff |newx-x| Shift\n');
fprintf('---- --------- ----- ------------ -----\n');
end
flag = -2;
x = x0 / norm(x0);
data = geap_data(x,A,B);
lambda = data.Axm / data.Bxm;
nviols = 0;
lambdatrace = zeros(maxits+1,1);
lambdatrace(1) = lambda;
if adaptive
shifttrace = zeros(maxits,1);
else
shifttrace = shift * ones(maxits,1);
end
for its = 1:maxits
if adaptive
tmp = min( eig( beta * geap_hessian(data) ) );
shift = beta * max(0, ( margin / data.m ) - tmp);
shifttrace(its) = shift;
end
if data.Bex
newx = beta * (data.Axm1 - lambda * data.Bxm1 + (shift + lambda) * data.Bxm * x);
else
newx = beta * (data.Axm1 + shift * x);
end
nx = norm(newx);
if nx < eps,
flag = -1;
break;
end
newx = newx / nx;
newdata = geap_data(newx,A,B);
newlambda = newdata.Axm / newdata.Bxm;
if norm(abs(newlambda-lambda)) < tol
flag = 0;
elseif (beta == 1) && (newlambda < lambda)
if (display > 0)
warning('Lambda is decreasing by %e when it should be increasing', abs(lambda-newlambda));
end
nviols = nviols + 1;
elseif (beta == -1) && (newlambda > lambda)
if (display > 0)
warning('Lambda is increasing by %e when it should be decreasing', abs(lambda-newlambda));
end
nviols = nviols + 1;
end
if (display > 0) && ((flag == 0) || (mod(its,display) == 0))
% Iteration Number
fprintf('%4d ', its);
% Lambda
fprintf('%9.6f ', newlambda);
d = newlambda-lambda;
if (d ~= 0)
if (d < 0), c = '-'; else c = '+'; end
fprintf('%ce%+03d ', c, round(log10(abs(d))));
else
fprintf(' ');
end
% Change in X
fprintf('%8.6e ', norm(newx-x));
% Shift
fprintf('%5.2f', shift);
% Line end
fprintf('\n');
end
x = newx;
data = newdata;
lambda = newlambda;
lambdatrace(its+1) = lambda;
if flag == 0
break
end
end
%% Check results
if (display >=0)
switch(flag)
case 0
fprintf('Successful Convergence');
case -1
fprintf('Converged to Zero Vector');
case -2
fprintf('Exceeded Maximum Iterations');
otherwise
fprintf('Unrecognized Exit Flag');
end
fprintf('\n');
end
%% Process output
nout = max(nargout,1);
if nout == 1
% Save everything in info
info.lambda = lambda;
info.x = x;
info.flag = flag;
info.x0 = x0;
info.its = its;
info.nviols = nviols;
info.shift = shift;
info.lambdatrace = lambdatrace(1:its+1);
info.shifttrace = shifttrace(1:its);
varargout{1} = info;
elseif nout >= 2
varargout{1} = lambda;
varargout{2} = x;
if nout == 3
varargout{3} = flag;
end
end
%% ----------------------------------------------------
function data = geap_data(x,A,B)
%GEAP_DATA Computes values needed for Generalized Tensor Eigenproblem
%
% DATA = GEAP_DATA(X,A,B) assumes X is a vector and A and B are symmetric
% tensors of appropriate sizes. No checking for sizes or symmetry are
% enforced. The following quanties are computed...
%
% - DATA.x - original X vector
% - DATA.m - ndims(A)
% - DATA.normx - norm(X)
% - DATA.normxeq1 - True if |norm(X)-1|<10*eps
% - DATA.nxm - norm(X)^ndims(A)
% - DATA.Axm - ttsv(A,X)
% - DATA.Axm1 - ttsv(A,X,-1)
% - DATA.Axm2 - ttsv(A,X,-2)
% - DATA.Bex - true, incidating B tensor is specified.
% - DATA.Bxm - ttsv(B,X)
% - DATA.Bxm1 - ttsv(B,X,-1)
% - DATA.Bxm2 - ttsv(B,X,-2)
%
% Alternatively, if B is empty, then
% - DATA.Bex - false
% - DATA.Bxm - 1
% - DATA.Bxm1 - X
% - DATA.Bxm2 - []
%
% See also GEAP_FUNCTION, GEAP_GRADIENT, GEAP_HESSIAN, GEAP.
data.x = x;
data.m = ndims(A);
data.normx = norm(x);
data.normxeq1 = abs(data.normx-1) < 10*eps;
data.nxm = (data.normx)^(data.m);
data.Axm2 = ttsv(A,x,-2);
data.Axm1 = data.Axm2*x;
data.Axm = data.Axm1'*x;
if isempty(B)
data.Bex = false;
data.Bxm = 1;
data.Bxm1 = x;
data.Bxm2 = [];
else
data.Bex = true;
data.Bxm2 = ttsv(B,x,-2);
data.Bxm1 = data.Bxm2*x;
data.Bxm = data.Bxm1'*x;
if data.Bxm < 0
disp(data.x)
disp(data.Bxm)
error('B is not positive definite')
end
end
function H = geap_hessian(data,alpha,dividebym)
%GEAP_HESSIAN Computes Generalized Tensor Eigenproblem gradient.
%
% G = GEAP_HESSIAN(DATA) returns the GEAP function Hessian divided by
% DATA.m, where DATA is the result of calling the GEAP_DATA function.
%
% G = GEAP_FUNTION(DATA,ALPHA) returns the Hessian of the shifted GEAP
% function, where the shift if ALPHA.
%
% G = GEAP_FUNCTION(DATA,ALPHA,FALSE) does not divide the result by
% data.m.
%
% See also GEAP_DATA, GEAP_FUNCTION, GEAP_GRADIENT, GEAP.
if ~exist('alpha','var')
alpha = 0;
end
if ~exist('dividebym','var')
dividebym = true;
end
if (~data.normxeq1)
warning('Norm(x) = %e, but should be 1.\n', data.normx);
end
m = data.m;
x = data.x;
n = size(x,1);
Axm = data.Axm;
Axm1 = data.Axm1;
Axm2 = data.Axm2;
xxt = x*x';
mat4 = eye(n) + (m-2) * xxt;
if data.Bex
Bxm = data.Bxm;
Bxm1 = data.Bxm1;
Bxm2 = data.Bxm2;
mat1 = symprod(Bxm1,Bxm1);
mat2 = symprod(Axm1,x);
mat3 = symprod(Axm1,Bxm1);
mat5 = symprod(Bxm1,x);
H1dm = ((m*Axm)/(Bxm^3)) * mat1 ...
+ (1/Bxm) * ( (m-1) * Axm2 + m * mat2 + Axm * mat4 ) ...
- (1/Bxm^2) * ( m * mat3 + (m-1) * Axm * Bxm2 + m * Axm * mat5);
else
H1dm = (m-1)*Axm2;
end
H2dm = alpha * mat4;
H = H1dm + H2dm;
if ~dividebym
H = m * H;
end
function M = symprod(a,b)
M = a*b' + b*a';