Home > manopt > core > getCost.m

getCost

PURPOSE ^

Computes the cost function at x.

SYNOPSIS ^

function cost = getCost(problem, x, storedb, key)

DESCRIPTION ^

 Computes the cost function at x.

 function cost = getCost(problem, x)
 function cost = getCost(problem, x, storedb)
 function cost = getCost(problem, x, storedb, key)

 Returns the value at x of the cost function described in the problem
 structure.

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

 See also: canGetCost

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cost = getCost(problem, x, storedb, key)
0002 % Computes the cost function at x.
0003 %
0004 % function cost = getCost(problem, x)
0005 % function cost = getCost(problem, x, storedb)
0006 % function cost = getCost(problem, x, storedb, key)
0007 %
0008 % Returns the value at x of the cost function described in the problem
0009 % structure.
0010 %
0011 % storedb is a StoreDB object, key is the StoreDB key to point x.
0012 %
0013 % See also: canGetCost
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, 'cost')
0033     %% Compute the cost function using cost.
0034     
0035         % Check whether this function wants to deal with storedb or not.
0036         switch nargin(problem.cost)
0037             case 1
0038                 cost = problem.cost(x);
0039             case 2
0040                 % Obtain, pass along, and save the store for x.
0041                 store = storedb.getWithShared(key);
0042                 [cost, store] = problem.cost(x, store);
0043                 storedb.setWithShared(store, key);
0044             case 3
0045                 % Pass along the whole storedb (by reference), with key.
0046                 cost = problem.cost(x, storedb, key);
0047             otherwise
0048                 up = MException('manopt:getCost:badcost', ...
0049                     'cost should accept 1, 2 or 3 inputs.');
0050                 throw(up);
0051         end
0052         
0053     elseif isfield(problem, 'costgrad')
0054     %% Compute the cost function using costgrad.
0055     
0056         % Check whether this function wants to deal with storedb or not.
0057         switch nargin(problem.costgrad)
0058             case 1
0059                 cost = problem.costgrad(x);
0060             case 2
0061                 % Obtain, pass along, and save the store for x.
0062                 store = storedb.getWithShared(key);
0063                 [cost, grad, store] = problem.costgrad(x, store); %#ok
0064                 storedb.setWithShared(store, key);
0065             case 3
0066                 % Pass along the whole storedb (by reference), with key.
0067                 cost = problem.costgrad(x, storedb, key);
0068             otherwise
0069                 up = MException('manopt:getCost:badcostgrad', ...
0070                     'costgrad should accept 1, 2 or 3 inputs.');
0071                 throw(up);
0072         end
0073 
0074     else
0075     %% Abandon computing the cost function.
0076 
0077         up = MException('manopt:getCost:fail', ...
0078             ['The problem description is not explicit enough to ' ...
0079              'compute the cost.']);
0080         throw(up);
0081         
0082     end
0083     
0084 end

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