comparison m-toolbox/classes/@smodel/hessian.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % hessian compute the hessian matrix for a symbolic model.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: hessian compute the hessian matrix for a symbolic model.
5 %
6 % CALL: H = hessian(obj);
7 %
8 % INPUTS: obj - a smodel
9 %
10 % <a href="matlab:web(smodel.getInfo('hessian').tohtml, '-helpbrowser')">Parameters Description</a>
11 %
12 % VERSION: $Id: hessian.m,v 1.6 2011/04/08 08:56:30 hewitson Exp $
13 %
14 % HISTORY: 28-10-2010 G. Congedo
15 %
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18 function varargout = hessian(varargin)
19
20 %%% Check if this is a call for parameters
21 if utils.helper.isinfocall(varargin{:})
22 varargout{1} = getInfo(varargin{3});
23 return
24 end
25
26 import utils.const.*
27 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
28
29 % Collect input variable names
30 in_names = cell(size(varargin));
31 for ii = 1:nargin,in_names{ii} = inputname(ii);end
32
33 % Collect all AOs and plists
34 [mdl, mdl_invars] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
35 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
36
37
38 if nargout == 0
39 error('### hessian cannot be used as a modifier. Please give an output variable.');
40 end
41
42 if ~all(isa(mdl, 'smodel'))
43 error('### hessian must be only applied to smodel objects.');
44 end
45
46 % Extract necessary parameters
47 p = pl.find('params');
48
49 if isempty(p) || strcmp(p,'all')
50 p = mdl.params;
51 end
52 Np = numel(p);
53 % grad = zeros(Np,1);
54 % H = zeros(Np);
55
56 % compute symbolic 1st-order differentiation
57 for ll=1:Np
58 grad(ll,1) = diff(mdl,p{ll});
59 end
60
61 % compute symbolic 2nd-order differentiation
62 for mm=1:Np
63 for ll=1:mm
64 H(ll,mm) = diff(grad(ll),p{mm});
65 end
66 end
67
68 % symmetrize matrix
69 for ll=1:Np
70 for mm=1:ll
71 H(ll,mm) = H(mm,ll);
72 end
73 end
74
75 % set name
76 for ll=1:Np
77 for m=1:Np
78 H(ll,mm).name = sprintf('hessian(%s)', mdl.name);
79 end
80 end
81
82 H.addHistory(getInfo('None'), pl, mdl_invars(:), [mdl(:).hist]);
83
84 % Set outputs
85 if nargout > 0
86 varargout{1} = H;
87 end
88
89 end
90
91
92 %--------------------------------------------------------------------------
93 % Get Info Object
94 %--------------------------------------------------------------------------
95 function ii = getInfo(varargin)
96 if nargin == 1 && strcmpi(varargin{1}, 'None')
97 sets = {};
98 pl = [];
99 else
100 sets = {'Default'};
101 pl = getDefaultPlist;
102 end
103 % Build info object
104 ii = minfo(mfilename, 'smodel', 'ltpda', utils.const.categories.helper, '$Id: hessian.m,v 1.6 2011/04/08 08:56:30 hewitson Exp $', sets, pl);
105 end
106
107 %--------------------------------------------------------------------------
108 % Get Default Plist
109 %--------------------------------------------------------------------------
110 function plout = getDefaultPlist()
111 persistent pl;
112 if exist('pl', 'var')==0 || isempty(pl)
113 pl = buildplist();
114 end
115 plout = pl;
116 end
117
118 function pl = buildplist()
119
120 pl = plist();
121
122 % params to diff
123 p = param({'params', 'A cell-array of parameters to differentiate with respect to.'}, 'all');
124 pl.append(p);
125
126 end
127