0001 function realtest_AD3()
0002
0003
0004
0005
0006
0007 if isempty(which('spherefactory'))
0008 error(['You should first add Manopt to the Matlab path.\n' ...
0009 'Please run importmanopt.']);
0010 end
0011
0012
0013 assert(exist('dlarray', 'file') == 2, ['Deep learning tool box is '...
0014 'needed for automatic differentiation.\n Please install the'...
0015 'latest version of the deep learning tool box and \nupgrade to Matlab'...
0016 ' R2021b if possible.'])
0017
0018
0019 n = 100;
0020 A = randn(n);
0021 A = .5*(A+A');
0022
0023
0024 S = spherefactory(n);
0025 P = powermanifold(S,1);
0026 X.x = S;
0027 X.y = P;
0028 problem.M = productmanifold(X);
0029
0030
0031 problem.cost = @(X) -X.x'*(A*X.y{1});
0032
0033
0034 problem = manoptAD(problem);
0035
0036
0037 figure;
0038 checkgradient(problem);
0039 figure;
0040 checkhessian(problem);
0041
0042
0043 [x, xcost, info] = trustregions(problem);
0044
0045
0046 ground_truth = svd(A);
0047 distance = abs(ground_truth(1) - (-problem.cost(x)));
0048 fprintf('The distance between the ground truth and the solution is %e \n',distance);
0049
0050
0051 end