comparison m-toolbox/classes/@ao/lincom.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 % LINCOM make a linear combination of analysis objects
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: LINCOM makes a linear combination of the input analysis
5 % objects
6 %
7 % CALL: b = lincom(a1,a2,a3,...,aN,c)
8 % b = lincom([a1,a2,a3,...,aN],c)
9 % b = lincom(a1,a2,a3,...,aN,[c1,c2,c3,...,cN])
10 % b = lincom([a1,a2,a3,...,aN],[c1,c2,c3,...,cN])
11 % b = lincom(a1,a2,a3,...,aN,pl)
12 % b = lincom([a1,a2,a3,...,aN],pl)
13 %
14 %
15 % If no plist is specified, the last object should be:
16 % + an AO of type cdata with the coefficients inside OR
17 % + a vector of AOs of type cdata with individual coefficients OR
18 % + a pest object with the coefficients
19 %
20 % INPUTS: ai - a list of analysis objects of the same type
21 % c - analysis object OR pest object with coefficient(s)
22 % pl - input parameter list
23 %
24 % OUTPUTS: b - output analysis object
25 %
26 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'lincom')">Parameters Description</a>
27 %
28 % VERSION: $Id: lincom.m,v 1.38 2011/04/08 08:56:12 hewitson Exp $
29 %
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31
32 function varargout = lincom(varargin)
33
34 %%% Check if this is a call for parameters
35 if utils.helper.isinfocall(varargin{:})
36 varargout{1} = getInfo(varargin{3});
37 return
38 end
39
40 import utils.const.*
41 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
42
43 if nargout == 0
44 error('### lincom cannot be used as a modifier. Please give an output variable.');
45 end
46
47 %%% Collect input variable names
48 in_names = cell(size(varargin));
49 for ii = 1:nargin
50 in_names{ii} = inputname(ii);
51 end
52
53 % Collect all AOs and plists
54 [as, ao_invars, rest] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
55 [ps, ps_invars, rest] = utils.helper.collect_objects(rest(:), 'pest', in_names);
56 pl = utils.helper.collect_objects(rest(:), 'plist', in_names);
57
58 % Combine input PLIST with default PLIST
59 usepl = parse(pl, getDefaultPlist());
60
61 % Check inputs
62 if numel(ps) > 1
63 error('### This method supports only one PEST object.')
64 end
65
66 % Get the length of the arguments
67 aosIdx = cellfun('isclass', varargin, 'ao');
68 num_as = cellfun('length', varargin(aosIdx));
69
70 coeffHist = [];
71 if ~isempty(usepl.find('COEFFS'))
72 coeffObj = usepl.find('COEFFS');
73 elseif numel(ps) == 1
74 coeffObj = ps;
75 coeffHist = ps.hist;
76 elseif isa(as(end).data, 'cdata') && as(end).len == numel(as(1:end-1))
77 % The last AOs have the coefficients
78 coeffObj = as(end);
79 coeffHist = coeffObj.hist;
80 % Remove the coefficients AOs from the data AOs
81 as(end) = [];
82 elseif all(num_as == 1) || num_as(end) == sum(num_as(1:end-1))
83 % list of single input AOs
84 if mod(numel(as), 2) ~= 0
85 error('### If you insert a list of AOs must be the number of AOs even.')
86 end
87 coeffObj = as(numel(as)/2+1:end);
88 coeffHist = [coeffObj.hist];
89 % Remove the coefficients AOs from the data AOs
90 as(numel(as)/2+1:end) = [];
91 else
92 error('### Incorrect shape of input!');
93 end
94
95 % Convert coefficient Object to an AO
96 if isa(coeffObj, 'ao')
97 elseif isa(coeffObj, 'pest')
98 coeffObj = coeffObj.find(coeffObj.names{:},'internal');
99 elseif isnumeric(coeffObj)
100 coeffObj = ao(coeffObj);
101 else
102 error('### Unsupported data type ''%s'' of the coefficient.', class(coeffObj));
103 end
104
105 % Checks that coefficients are cdata AO(s)
106 for kk = 1:numel(coeffObj)
107 if ~isa(coeffObj(kk).data, 'cdata')
108 error('### lincom can not be used on this data type.');
109 end
110 end
111 coeffs = coeffObj.y;
112 cunits = unit.initObjectWithSize(1, numel(coeffs));
113 if numel(coeffObj) == 1
114 % Single coefficients AO, get the unit and make copies
115 for kk = 1:numel(coeffs)
116 cunits(kk) = unit(coeffObj.yunits);
117 end
118 else
119 % Array of coefficients AOs, get each unit separately
120 cunits = unit(coeffObj.yunits);
121 end
122
123 na = length(as);
124 nc = length(coeffs);
125 if na ~= nc
126 disp(sprintf('Num AOs input: %d', na));
127 disp(sprintf('Num Coeffs input: %d', nc));
128 error('### specify one coefficient per analysis object.');
129 end
130
131 % Make linear combination, collect name string, compute the new units
132 nstr = '';
133 y = zeros(size(as(1).data.getY));
134
135 for kk = 1:nc
136 nstr = [nstr num2str(coeffs(kk)) '*' ao_invars{kk} ' + '];
137 y = y + coeffs(kk).*as(kk).y;
138 new_units(kk) = simplify(as(kk).yunits .* cunits(kk), plist('Prefixes',false));
139 end
140 nstr = deblank(nstr(1:end-2));
141 for kk = 1:nc-1
142 if ~eq(new_units(kk), new_units(kk+1))
143 error('### inconsistent units in data/coefficients');
144 end
145 end
146
147 % create new ao
148 b = ao;
149 b.data = copy(as(1).data, 1);
150 b.data.setY(y);
151
152 %%% Set Name
153 b.name = nstr;
154
155 %%% Set new Units
156 b.data.setYunits(new_units(1));
157
158 %%% Propagate 'plotinfo'
159 plotinfo = [as(:).plotinfo];
160 if ~isempty(plotinfo)
161 b.plotinfo = combine(plotinfo);
162 end
163
164 %%% Add History
165 b.addHistory(getInfo('None'), usepl, [ao_invars ps_invars], [as.hist coeffHist]);
166
167 % Set output
168 varargout{1} = b;
169 end
170
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % Local Functions %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 %
177 % FUNCTION: getInfo
178 %
179 % DESCRIPTION: Get Info Object
180 %
181 % HISTORY: 11-07-07 M Hewitson
182 % Creation.
183 %
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 function ii = getInfo(varargin)
187 if nargin == 1 && strcmpi(varargin{1}, 'None')
188 sets = {};
189 pl = [];
190 else
191 sets = {'Default'};
192 pl = getDefaultPlist;
193 end
194 % Build info object
195 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: lincom.m,v 1.38 2011/04/08 08:56:12 hewitson Exp $', sets, pl);
196 ii.setModifier(false);
197 end
198
199 %--------------------------------------------------------------------------
200 % Get Default Plist
201 %--------------------------------------------------------------------------
202 function plout = getDefaultPlist()
203 persistent pl;
204 if exist('pl', 'var')==0 || isempty(pl)
205 pl = buildplist();
206 end
207 plout = pl;
208 end
209
210 function pl = buildplist()
211 pl = plist(...
212 {'COEFFS',['A vector of AOs of type cdata with individual coefficients<br>' ...
213 'or a PEST containing a vector of coefficients<br>' ...
214 'or a vector of numeric coefficients.']}, paramValue.EMPTY_DOUBLE);
215 end
216 % END
217
218