comparison m-toolbox/classes/@matrix/linearize.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 % LINEARIZE output the derivatives of the model relative to the parameters.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: LINEARIZE output the derivatives of the model relative to
5 % the parameters. Output is a collection of models corresponding to the
6 % derivative of input model for each parameter
7 %
8 % CALL: dmod = linearize(imod)
9 %
10 % <a href="matlab:utils.helper.displayMethodInfo('matrix', 'linearize')">Parameters Description</a>
11 %
12 % VERSION: $Id: linearize.m,v 1.14 2011/04/08 08:56:31 hewitson Exp $
13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 function varargout = linearize(varargin)
16
17 % utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
18
19 % Check if this is a call for parameters
20 if utils.helper.isinfocall(varargin{:})
21 varargout{1} = getInfo(varargin{3});
22 return
23 end
24
25 % Collect input variable names
26 in_names = cell(size(varargin));
27 for ii = 1:nargin
28 in_names{ii} = inputname(ii);
29 end
30
31 % Collect all smodels and plists
32 [as, matrix_invars, rest] = utils.helper.collect_objects(varargin(:), 'matrix', in_names);
33 [pl, pl_invars, rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);
34
35 % Merge with default plist
36 pl = parse(pl, getDefaultPlist);
37
38 % get parameters and make sure we are working with a cell array
39 pnames = find(pl,'Params');
40 if isa(pnames, 'char')
41 pnames = {pnames};
42 end
43
44 % decide if sorting
45 sorting = find(pl,'Sorting');
46 if sorting
47 pnames = sort(pnames);
48 end
49
50 % loop over input matrices
51 dmod(numel(as),1)=collection;
52 for ww=1:numel(as)
53 imod=as(ww);
54 if strcmpi(class(imod.objs(1)),'smodel')
55 % a matrix of smodels is assumed
56 % join params
57
58 if isempty(pnames) % linearize with respect to all model parameters
59 % store a common set of parameters for mod, it is needed for
60 % the derivative
61 [rw,cl] = size(imod.objs);
62 % loop over dimensions
63 mpars = {};
64 mvals = {};
65 for aa = 1:rw
66 for bb = 1:cl
67 obj = imod.objs(aa,bb);
68 [mpars,id1,id2] = union(mpars,obj.params);
69 nom1 = mvals(id1);
70 nom2 = obj.values(id2);
71 mvals = [nom1 nom2];
72 end
73 end
74 for ff = 1:numel(imod.objs)
75 imod.objs(ff).setParams(mpars,mvals);
76 end
77
78 % it is assumed a common set of parameters for the matrix object
79 pnames = imod.objs(1).params;
80 end %isempty(pnames)
81
82 % get matrix dimension
83 [rw,cl] = size(imod.objs);
84
85 % start linearization
86 for ii = 1:numel(pnames)
87 tmod = copy(imod,1);
88 for jj = 1:rw
89 for kk = 1:cl
90 tmod.objs(jj,kk) = diff(imod.objs(jj,kk),pnames{ii}); % do symbolic derivative for smodel
91 tmod.objs(jj,kk).setName(sprintf('d{%s}/d{%s}',imod.objs(jj,kk).name,pnames{ii}));
92 end
93 end
94 % set the name of the parameter to the matrix, this is important to
95 % identify automatically to what derivatives we are referring
96 tmod.setName(pnames{ii});
97 % dmod.addObjects(tmod);
98 dmod(ww).addObjects(tmod);
99 end
100 dmod(ww).setName(sprintf('linearize(%s)',imod.name));
101 else
102 error('Only matrix of smodels supported at the moment')
103 end %strcmpi(class(imod.objs(1)),'smodel')
104
105 end
106
107 if nargout == 1
108 varargout{1} = dmod;
109 elseif nargout == numel(as)
110 % List of outputs
111 for ii = 1:numel(as)
112 varargout{ii} = dmod.index(ii);
113 end
114 else
115 error('Set at least one output value')
116 end
117
118 end
119
120 %--------------------------------------------------------------------------
121 % Get Info Object
122 %--------------------------------------------------------------------------
123 function ii = getInfo(varargin)
124
125 if nargin == 1 && strcmpi(varargin{1}, 'None')
126 sets = {};
127 pls = [];
128 else
129 sets = {'Default'};
130 pls = getDefaultPlist;
131 end
132 % Build info object
133 ii = minfo(mfilename, 'matrix', 'ltpda', utils.const.categories.op, '$Id: linearize.m,v 1.14 2011/04/08 08:56:31 hewitson Exp $', sets, pls);
134 ii.setModifier(false);
135 end
136
137 %--------------------------------------------------------------------------
138 % Get Default Plist
139 %--------------------------------------------------------------------------
140 function plout = getDefaultPlist()
141 persistent pl;
142 if exist('pl', 'var')==0 || isempty(pl)
143 pl = buildplist();
144 end
145 plout = pl;
146 end
147
148 function pl = buildplist()
149 pl = plist();
150
151 p = param({'Params', ['Cell array with parameters names with respect to linearize. <br>' ...
152 'Leave it empty to linearize with respect to all model parameters']}, paramValue.EMPTY_DOUBLE);
153 pl.append(p);
154
155 p = param({'Sorting', ' Decide to sort the lits of input parameters'}, paramValue.TRUE_FALSE);
156 pl.append(p);
157
158 end