Computes an approximation of the gradient of the cost function at x. function approxgrad = getApproxGradient(problem, x) function approxgrad = getApproxGradient(problem, x, storedb) function approxgrad = getApproxGradient(problem, x, storedb, key) Returns an approximation of the gradient at x for the cost function described in the problem structure. storedb is a StoreDB object, key is the StoreDB key to point x. If no approximate gradient was provided, this call is redirected to getGradientFD. See also: getGradientFD canGetApproxGradient
0001 function approxgrad = getApproxGradient(problem, x, storedb, key) 0002 % Computes an approximation of the gradient of the cost function at x. 0003 % 0004 % function approxgrad = getApproxGradient(problem, x) 0005 % function approxgrad = getApproxGradient(problem, x, storedb) 0006 % function approxgrad = getApproxGradient(problem, x, storedb, key) 0007 % 0008 % Returns an approximation of the gradient at x for the cost function 0009 % described in the problem structure. 0010 % 0011 % storedb is a StoreDB object, key is the StoreDB key to point x. 0012 % 0013 % If no approximate gradient was provided, this call is redirected to 0014 % getGradientFD. 0015 % 0016 % See also: getGradientFD canGetApproxGradient 0017 0018 % This file is part of Manopt: www.manopt.org. 0019 % Original author: Nicolas Boumal, Nov. 1, 2016. 0020 % Contributors: 0021 % Change log: 0022 0023 % Allow omission of the key, and even of storedb. 0024 if ~exist('key', 'var') 0025 if ~exist('storedb', 'var') 0026 storedb = StoreDB(); 0027 end 0028 key = storedb.getNewKey(); 0029 end 0030 0031 0032 if isfield(problem, 'approxgrad') 0033 %% Compute the approximate gradient using approxgrad. 0034 0035 % Check whether this function wants to deal with storedb or not. 0036 switch nargin(problem.approxgrad) 0037 case 1 0038 approxgrad = problem.approxgrad(x); 0039 case 2 0040 % Obtain, pass along, and save the store for x. 0041 store = storedb.getWithShared(key); 0042 [approxgrad, store] = problem.approxgrad(x, store); 0043 storedb.setWithShared(store, key); 0044 case 3 0045 % Pass along the whole storedb (by reference), with key. 0046 approxgrad = problem.approxgrad(x, storedb, key); 0047 otherwise 0048 up = MException('manopt:getApproxGradient:badapproxgrad', ... 0049 'approxgrad should accept 1, 2 or 3 inputs.'); 0050 throw(up); 0051 end 0052 0053 else 0054 %% Try to fall back to a standard FD approximation. 0055 0056 approxgrad = getGradientFD(problem, x, storedb, key); 0057 0058 end 0059 0060 end