Computes the Hessian of the cost function at x along d. function hess = getHessian(problem, x, d) function hess = getHessian(problem, x, d, storedb) function hess = getHessian(problem, x, d, storedb, key) Returns the Hessian at x along d of the cost function described in the problem structure. storedb is a StoreDB object, key is the StoreDB key to point x. If an exact Hessian is not provided, an approximate Hessian is returned if possible, without warning. If not possible, an exception will be thrown. To check whether an exact Hessian is available or not (typically to issue a warning if not), use canGetHessian. See also: getPrecon getApproxHessian canGetHessian
0001 function hess = getHessian(problem, x, d, storedb, key) 0002 % Computes the Hessian of the cost function at x along d. 0003 % 0004 % function hess = getHessian(problem, x, d) 0005 % function hess = getHessian(problem, x, d, storedb) 0006 % function hess = getHessian(problem, x, d, storedb, key) 0007 % 0008 % Returns the Hessian at x along d of the cost function described in the 0009 % problem structure. 0010 % 0011 % storedb is a StoreDB object, key is the StoreDB key to point x. 0012 % 0013 % If an exact Hessian is not provided, an approximate Hessian is returned 0014 % if possible, without warning. If not possible, an exception will be 0015 % thrown. To check whether an exact Hessian is available or not (typically 0016 % to issue a warning if not), use canGetHessian. 0017 % 0018 % See also: getPrecon getApproxHessian canGetHessian 0019 0020 % This file is part of Manopt: www.manopt.org. 0021 % Original author: Nicolas Boumal, Dec. 30, 2012. 0022 % Contributors: 0023 % Change log: 0024 % 0025 % April 3, 2015 (NB): 0026 % Works with the new StoreDB class system. 0027 % 0028 % Feb. 10, 2020 (NB): 0029 % Allowing M.ehess2rhess to take (storedb, key) as extra inputs. 0030 0031 % Allow omission of the key, and even of storedb. 0032 if ~exist('key', 'var') 0033 if ~exist('storedb', 'var') 0034 storedb = StoreDB(); 0035 end 0036 key = storedb.getNewKey(); 0037 end 0038 0039 0040 if isfield(problem, 'hess') 0041 %% Compute the Hessian using hess. 0042 0043 % Check whether this function wants to deal with storedb or not. 0044 switch nargin(problem.hess) 0045 case 2 0046 hess = problem.hess(x, d); 0047 case 3 0048 % Obtain, pass along, and save the store for x. 0049 store = storedb.getWithShared(key); 0050 [hess, store] = problem.hess(x, d, store); 0051 storedb.setWithShared(store, key); 0052 case 4 0053 % Pass along the whole storedb (by reference), with key. 0054 hess = problem.hess(x, d, storedb, key); 0055 otherwise 0056 up = MException('manopt:getHessian:badhess', ... 0057 'hess should accept 2, 3 or 4 inputs.'); 0058 throw(up); 0059 end 0060 0061 elseif isfield(problem, 'ehess') && canGetEuclideanGradient(problem) 0062 %% Compute the Hessian using ehess. 0063 0064 % We will need the Euclidean gradient for the conversion from the 0065 % Euclidean Hessian to the Riemannian Hessian. 0066 egrad = getEuclideanGradient(problem, x, storedb, key); 0067 0068 % Check whether this function wants to deal with storedb or not. 0069 switch nargin(problem.ehess) 0070 case 2 0071 ehess = problem.ehess(x, d); 0072 case 3 0073 % Obtain, pass along, and save the store for x. 0074 store = storedb.getWithShared(key); 0075 [ehess, store] = problem.ehess(x, d, store); 0076 storedb.setWithShared(store, key); 0077 case 4 0078 % Pass along the whole storedb (by reference), with key. 0079 ehess = problem.ehess(x, d, storedb, key); 0080 otherwise 0081 up = MException('manopt:getHessian:badehess', ... 0082 'ehess should accept 2, 3 or 4 inputs.'); 0083 throw(up); 0084 end 0085 0086 % Convert to the Riemannian Hessian 0087 switch nargin(problem.M.ehess2rhess) 0088 case 4 0089 hess = problem.M.ehess2rhess(x, egrad, ehess, d); 0090 case 6 0091 hess = problem.M.ehess2rhess(x, egrad, ehess, d, storedb, key); 0092 otherwise 0093 up = MException('manopt:getHessian:ehess2rhess', ... 0094 'ehess2rhess should accept 4 or 6 inputs.'); 0095 throw(up); 0096 end 0097 0098 else 0099 %% Attempt the computation of an approximation of the Hessian. 0100 0101 hess = getApproxHessian(problem, x, d, storedb, key); 0102 0103 end 0104 0105 end