Mercurial > hg > ltpda
comparison m-toolbox/classes/@smodel/sum.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 % SUM adds all the elements of smodel objects arrays. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: SUM adds all the elements of smodel objects arrays. | |
5 % | |
6 % CALL: obj = sum(mdl_mat) | |
7 % obj = mdl_mat.sum() | |
8 % obj = sum(mdl_mat, dim) % Not yet implemented! | |
9 % obj = mdl_mat.sum(plist('dim', dim)) % Not yet implemented! | |
10 % | |
11 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'sum')">Parameters Description</a> | |
12 % | |
13 % VERSION: $Id: sum.m,v 1.6 2011/04/08 08:56:30 hewitson Exp $ | |
14 % | |
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
16 | |
17 function out = sum(varargin) | |
18 | |
19 % Check if this is a call for parameters | |
20 if utils.helper.isinfocall(varargin{:}) | |
21 out = getInfo(varargin{3}); | |
22 return | |
23 end | |
24 | |
25 if nargout == 0 | |
26 error('### sum cannot be used as a modifier. Please give an output variable.'); | |
27 end | |
28 | |
29 % Collect input variable names | |
30 in_names = cell(size(varargin)); | |
31 for ii = 1:nargin | |
32 in_names{ii} = inputname(ii); | |
33 end | |
34 | |
35 % Collect all smodels and plists | |
36 [as, smodel_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names); | |
37 [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
38 | |
39 % Combine plists | |
40 usepl = parse(pl, getDefaultPlist()); | |
41 | |
42 % Check the model parameters | |
43 for jj = 1:numel(as) | |
44 for kk = 1:jj | |
45 if ~strcmp(as(kk).xvar, as(jj).xvar) | |
46 warning('### Two of the models have different X variables. Taking the first'); | |
47 end | |
48 if ~isequal(as(kk).xvals, as(jj).xvals) | |
49 warning('### Two of the models have different X data. Taking the first'); | |
50 end | |
51 end | |
52 end | |
53 | |
54 % Copy the object | |
55 out = copy(as(1), 1); | |
56 expression = ['(' as(1).expr.s ')']; | |
57 name = ['(' as(1).name ')']; | |
58 | |
59 for jj = 2:numel(as) | |
60 expression = [expression ' + (' as(jj).expr.s ')']; | |
61 name = [name ' + (' as(jj).name ')']; | |
62 out.params = [out.params as(jj).params]; | |
63 out.values = [out.values as(jj).values]; | |
64 end | |
65 | |
66 out.expr = msym(expression); | |
67 out.name = name; | |
68 | |
69 [out.params,ii,jj] = unique(out.params, 'first'); | |
70 out.values = out.values(ii); | |
71 | |
72 if ~isequal(ii,jj) | |
73 warning('Some parameters were not unique. Parameters from the first model were taken.'); | |
74 end | |
75 | |
76 % Add history | |
77 out.addHistory(getInfo('None'), usepl, {inputname(1)}, [as(:).hist]); | |
78 | |
79 end | |
80 | |
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
82 % Local Functions % | |
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
84 | |
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
86 % | |
87 % FUNCTION: getInfo | |
88 % | |
89 % DESCRIPTION: Get Info Object | |
90 % | |
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
92 | |
93 function ii = getInfo(varargin) | |
94 | |
95 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
96 sets = {}; | |
97 pls = []; | |
98 else | |
99 sets = {'Default'}; | |
100 pls = getDefaultPlist; | |
101 end | |
102 % Build info object | |
103 ii = minfo(mfilename, 'smodel', 'ltpda', utils.const.categories.op, '$Id: sum.m,v 1.6 2011/04/08 08:56:30 hewitson Exp $', sets, pls); | |
104 ii.setArgsmin(2); | |
105 ii.setModifier(false); | |
106 end | |
107 | |
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
109 % | |
110 % FUNCTION: getDefaultPlist | |
111 % | |
112 % DESCRIPTION: Get Default Plist | |
113 % | |
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
115 | |
116 function plout = getDefaultPlist() | |
117 persistent pl; | |
118 if exist('pl', 'var')==0 || isempty(pl) | |
119 pl = buildplist(); | |
120 end | |
121 plout = pl; | |
122 end | |
123 | |
124 function pl = buildplist() | |
125 pl = plist('dim', []); | |
126 end |