Home > manopt > core > getCostGrad.m

getCostGrad

PURPOSE ^

Computes the cost function and the gradient at x in one call if possible.

SYNOPSIS ^

function [cost, grad] = getCostGrad(problem, x, storedb, key)

DESCRIPTION ^

 Computes the cost function and the gradient at x in one call if possible.

 function [cost, grad] = getCostGrad(problem, x)
 function [cost, grad] = getCostGrad(problem, x, storedb)
 function [cost, grad] = getCostGrad(problem, x, storedb, key)

 Returns the value at x of the cost function described in the problem
 structure, as well as the gradient at x.

 storedb is a StoreDB object, key is the StoreDB key to point x.

 See also: canGetCost canGetGradient getCost getGradient

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [cost, grad] = getCostGrad(problem, x, storedb, key)
0002 % Computes the cost function and the gradient at x in one call if possible.
0003 %
0004 % function [cost, grad] = getCostGrad(problem, x)
0005 % function [cost, grad] = getCostGrad(problem, x, storedb)
0006 % function [cost, grad] = getCostGrad(problem, x, storedb, key)
0007 %
0008 % Returns the value at x of the cost function described in the problem
0009 % structure, as well as the gradient at x.
0010 %
0011 % storedb is a StoreDB object, key is the StoreDB key to point x.
0012 %
0013 % See also: canGetCost canGetGradient getCost getGradient
0014 
0015 % This file is part of Manopt: www.manopt.org.
0016 % Original author: Nicolas Boumal, Dec. 30, 2012.
0017 % Contributors:
0018 % Change log:
0019 %
0020 %   April 3, 2015 (NB):
0021 %       Works with the new StoreDB class system.
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, 'costgrad')
0033     %% Compute the cost/grad pair using costgrad.
0034     
0035         % Check whether this function wants to deal with storedb or not.
0036         switch nargin(problem.costgrad)
0037             case 1
0038                 [cost, grad] = problem.costgrad(x);
0039             case 2
0040                 % Obtain, pass along, and save the store for x.
0041                 store = storedb.getWithShared(key);
0042                 [cost, grad, store] = problem.costgrad(x, store);
0043                 storedb.setWithShared(store, key);
0044             case 3
0045                 % Pass along the whole storedb (by reference), with key.
0046                 [cost, grad] = problem.costgrad(x, storedb, key);
0047             otherwise
0048                 up = MException('manopt:getCostGrad:badcostgrad', ...
0049                     'costgrad should accept 1, 2 or 3 inputs.');
0050                 throw(up);
0051         end
0052 
0053     else
0054     %% Revert to calling getCost and getGradient separately
0055     
0056         cost = getCost(problem, x, storedb, key);
0057         grad = getGradient(problem, x, storedb, key);
0058         
0059     end
0060     
0061 end

Generated on Fri 08-Sep-2017 12:43:19 by m2html © 2005