]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/private/__s2mat__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / private / __s2mat__.m
1 ## Copyright (C) 2010 Olaf Till <olaf.till@uni-jena.de>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## __s2mat__ (s, ord)
17 ##
18 ## Returns a matrix of second derivatives with respect to some
19 ## parameters from the structure-based representation of such a matrix
20 ## in s, using the order of parameter names ord. s has to contain all
21 ## fields named in ord. Each field has some subfields named in ord so
22 ## that each second derivative is represented at least in one of its two
23 ## possible orders. If it is represented differently in both orders, no
24 ## error is returned, but both entries might get into the final matrix
25 ## at symmetric positions.
26 ##
27 ## Should be included as a subfunction of a wrapper for optimization
28 ## functions possibly needing a Hessian.
29
30 function ret = __s2mat__ (s, ord)
31
32   if (any (size (s) != [1, 1]))
33     error ("structure must be scalar");
34   endif
35
36   if (! (iscell (ord) && isvector (ord)))
37     error ("ord must be a one-dimensional cell-array");
38   endif
39
40   c = fields2cell (structcat (1, fields2cell (s, ord){:}), ord);
41
42   neidx = ! (eidx = cellfun ("isempty", c));
43
44   ret = zeros (length (ord));
45
46   ret(neidx) = [c{neidx}]; # faster than [c{:}] ?
47
48   ret(eidx) = ret.'(eidx);
49
50 endfunction