Home > manopt > autodiff > basic_examples_AD > complex_example_AD.m

complex_example_AD

PURPOSE ^

A basic example that shows how to define the cost funtion for

SYNOPSIS ^

function complex_example_AD()

DESCRIPTION ^

 A basic example that shows how to define the cost funtion for 
 optimization problems on complex manifolds.

 Note that automatic differentiation for complex numbers is not supported
 for Matlab R2021a or earlier. To fully exploit the convenience of AD,
 please update to the latest version if possible. If the user cannot have 
 access to Matlab R2021b or later, manopt provides an alternative way to 
 deal with complex problems which requires the user to define the cost 
 funtion using the basic functions listed in the folder /functions_AD or 
 to define their own functions following the rules described in that file.
 See the following as an example.

 See also: manoptADhelp

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function complex_example_AD()
0002 % A basic example that shows how to define the cost funtion for
0003 % optimization problems on complex manifolds.
0004 %
0005 % Note that automatic differentiation for complex numbers is not supported
0006 % for Matlab R2021a or earlier. To fully exploit the convenience of AD,
0007 % please update to the latest version if possible. If the user cannot have
0008 % access to Matlab R2021b or later, manopt provides an alternative way to
0009 % deal with complex problems which requires the user to define the cost
0010 % funtion using the basic functions listed in the folder /functions_AD or
0011 % to define their own functions following the rules described in that file.
0012 % See the following as an example.
0013 %
0014 % See also: manoptADhelp
0015 
0016 % This file is part of Manopt and is copyrighted. See the license file.
0017 %
0018 % Main author: Xiaowen Jiang, August, 31, 2021
0019 % Contributors: Nicolas Boumal
0020 % Change log:
0021 %
0022 
0023     % Verify that the deep learning tool box was installed
0024     assert(exist('dlarray', 'file') == 2, ['Deep learning tool box is '... 
0025     'needed for automatic differentiation.\n Please install the'...
0026     'latest version of the deep learning tool box and \nupgrade to Matlab'...
0027     ' R2021b if possible.'])
0028     
0029     % Generate the problem data.
0030     n = 100;
0031     A = randn(n, n) + 1i*randn(n, n);
0032     A = .5*(A+A');
0033 
0034     % Create the problem structure.
0035     S = spherecomplexfactory(n);
0036     problem.M = S;
0037     
0038     % Define the problem cost function
0039     % For Matlab R2021b or later, define the problem cost function as usual
0040     % problem.cost  = @(X) -.5*real(X'*A*X);
0041     
0042     % For Matlab R2021a or earlier, translate the cost function into a
0043     % particular format with the basic functions in /functions_AD
0044     problem.cost  = @(X) -creal(cprod(cprod(ctransp(X), A), X));
0045     
0046     % Define the gradient and the hessian via automatic differentiation
0047     problem = manoptAD(problem);
0048 
0049     % Numerically check gradient and Hessian consistency.
0050     figure;
0051     checkgradient(problem);
0052     figure;
0053     checkhessian(problem);
0054     
0055     % Solve.
0056     [x, xcost, info] = trustregions(problem);          %#ok<ASGLU>
0057     
0058     % Display some statistics.
0059     figure;
0060     semilogy([info.iter], [info.gradnorm], '.-');
0061     xlabel('Iteration #');
0062     ylabel('Gradient norm');
0063     title(['Convergence of the trust-regions algorithm on the'...
0064         'complex sphere power manifold']);
0065 
0066 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005