Home > manopt > core > getPartialGradient.m

## PURPOSE

Computes the gradient of a subset of terms in the cost function at x.

## DESCRIPTION

``` Computes the gradient of a subset of terms in the cost function at x.

Assume the cost function described in the problem structure is a sum of
many terms, as

f(x) = sum_i f_i(x) for i = 1:d,```

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:

## SOURCE CODE

```0001 function grad = getPartialGradient(problem, x, I, storedb, key)
0002 % Computes the gradient of a subset of terms in the cost function at x.
0003 %
0007 %
0008 % Assume the cost function described in the problem structure is a sum of
0009 % many terms, as
0010 %
0011 %    f(x) = sum_i f_i(x) for i = 1:d,
0012
0013 % where d is specified as d = problem.ncostterms.
0014 %
0015 % For a subset I of 1:d, getPartialGradient obtains the gradient of the
0016 % partial cost function
0017 %
0018 %    f_I(x) = sum_i f_i(x) for i = I.
0019 %
0020 % storedb is a StoreDB object, key is the StoreDB key to point x.
0021 %
0023
0024 % This file is part of Manopt: www.manopt.org.
0025 % Original author: Nicolas Boumal, June 28, 2016
0026 % Contributors:
0027 % Change log:
0028 %
0029 %   Feb. 10, 2020 (NB):
0031
0032
0033     % Allow omission of the key, and even of storedb.
0034     if ~exist('key', 'var')
0035         if ~exist('storedb', 'var')
0036             storedb = StoreDB();
0037         end
0038         key = storedb.getNewKey();
0039     end
0040
0041
0042     % Make sure I is a row vector, so that it is natural to loop over it
0043     % with " for i = I ".
0044     I = (I(:)).';
0045
0046
0049
0050         % Check whether this function wants to deal with storedb or not.
0052             case 2
0054             case 3
0055                 % Obtain, pass along, and save the store for x.
0056                 store = storedb.getWithShared(key);
0058                 storedb.setWithShared(store, key);
0059             case 4
0060                 % Pass along the whole storedb (by reference), with key.
0062             otherwise
0064                     'partialgrad should accept 2, 3 or 4 inputs.');
0065                 throw(up);
0066         end
0067
0070
0072         % Convert to the Riemannian gradient
0074             case 2
0076             case 4
0078             otherwise
0081                 throw(up);
0082         end
0083
0084     else
0085     %% Abandon computing the partial gradient.
0086