function [H, n, data] = tensor(example)
%tensor: model NEPv from best rank-1 approximation of 3rd order partial symmetric tensors.
%
% 	[H, n, data] = tensor(example) generates NEPv from computation of 
%	the best rank-1 approximation of a 3rd order partial symmetric tensors 
%	T of size n-by-n-by-m, with Ai= T(:,:,i) being symmetric matrices. 
%	The NEPv is in the form of 
%		H(x)x = lam*x,
%	where 
%		H(x) := (x'*A1*x)*A1 + (x'*A2*x)*A2 + ... + (x'*Am*x)*Am 
%	is a symmetric matrix and lam is a largest eigenvalue H(x). 
% 	
%
%	INPUT: example 
%	'new_orleans'	--- for the New Orleans tensor T of size 63891 * 63891 * 20; 
%	'princeton' 	---	for the Princeton tensor T of size 6593 * 6593 * 6; 
%	'reuters'		--- for the Reuters tensor T of size 13332 * 13332 * 66;
%
%	OUTPUT: 
%	H	--- function handle H(x);
%	n	--- size n;
%	data--- data = {A1,A2, ..., Am}.
%
%
% -------
%	Usage:
%	ex = 'new_orleans';
%	[H, n] = rec(ex); 
%	Hx0 = H(randn(n,1)); % size 63891 symmetric matrix
%
%	ex = 'princeton';
%	[H, n] = rec(ex); 
%	Hx0 = H(randn(n,1)); % size 6593 symmetric matrix
%
%	ex = 'reuters';
%	[H, n] = rec(ex); 
%	Hx0 = H(randn(n,1)); % size 13332 symmetric matrix
%
%
% -------
% 	NEPv features: Symmetric H; Single vector X; Unitarily invariant; 
%	Largest eigenvalue; 
%
%	Ding.Lu@uky.edu. Version date July-20-2022.
%

if strcmp(example, 'new_orleans')
	load fb20.mat;

elseif strcmp(example, 'princeton') 
	load princeton.mat;

elseif strcmp(example, 'reuters')
 	load reuters.mat;
else
	disp("Error: Input example = 'new_orleans', 'princeton', or 'reuters'.");
	return
end

n = size(A{1},1);
m = length(A);

% construct H(x) 
H = @(x) Hsub(x, A);
data = A;

return;

end % END OF tensor



% *******************************************
% Local functions
% *******************************************

function H = Hsub(x, A)
H = (x'*A{1}*x)*A{1};
for i = 2:length(A)
    H = H+(x'*A{i}*x)*A{i};
end
end % END OF Hsub



