comparison m-toolbox/classes/@smodel/mtimes.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 % MTIMES implements mtimes operator for smodel objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MTIMES implements mtimes operator for smodel objects.
5 %
6 % CALL: obj = obj1 * obj2
7 % obj = mtimes(obj1,obj2);
8 %
9 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'mtimes')">Parameters Description</a>
10 %
11 % VERSION: $Id: mtimes.m,v 1.7 2011/05/10 21:00:15 mauro Exp $
12 %
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15 function mdl = mtimes(varargin)
16
17 callerIsMethod = utils.helper.callerIsMethod;
18
19 % Check if this is a call for parameters
20 if utils.helper.isinfocall(varargin{:})
21 mdl = getInfo(varargin{3});
22 return
23 end
24
25 if nargout == 0
26 error('### Matrix multiplication operator can not be used as a modifier.');
27 end
28
29 mdl1 = varargin{1};
30 mdl2 = varargin{2};
31
32 % Convert numbers into a smodel object
33 if isnumeric(mdl1)
34 mdl1 = smodel(mdl1);
35 end
36 if isnumeric(mdl2)
37 mdl2 = smodel(mdl2);
38 end
39
40 %----------------- Gather the input objects names and history
41 in_names = cell(size(varargin));
42 for ii = 1:nargin,in_names{ii} = inputname(ii);end
43
44 % Convert cdata aos into a smodel object
45 if isa(mdl2, 'ao')
46 if isa(mdl2.data, 'cdata') && numel(mdl2.data.y) == 1
47 mdl2 = smodel(mdl2.y);
48 else
49 error('### It is not possible to multiply the two objects!');
50 end
51 end
52
53 [rw1,cl1] = size(mdl1);
54 [rw2,cl2] = size(mdl2);
55
56 % Check input model dimensions
57 if ((rw1 == 1) && (rw2 == 1) && (cl1 == 1) && (cl2 == 1))
58 ids = '1D';
59 else
60 if (cl1 ~= rw2)
61 error('!!! Matrices inner dimensions must agree')
62 else
63 ids = 'ND';
64 end
65 end
66
67 switch ids
68 case '1D'
69
70 mdl = copy(mdl1, true);
71 mdl.expr = msym(['(' mdl.expr.s ')*(' mdl2.expr.s ')']);
72 mdl.name = ['(' mdl.name ')*(' mdl2.name ')'];
73
74 smodel.mergeFields(mdl1, mdl2, mdl, 'params', 'values');
75 smodel.mergeFields(mdl1, mdl2, mdl, 'aliasNames', 'aliasValues');
76 smodel.mergeFields(mdl1, mdl2, mdl, 'xvar', 'xvals');
77 smodel.mergeFields(mdl1, mdl2, mdl, 'xvar', 'xunits');
78 smodel.mergeFields(mdl1, mdl2, mdl, 'xvar', 'trans');
79
80 if ~callerIsMethod
81 mdl.addHistory(getInfo('None'), [], in_names, [mdl1.hist mdl2.hist]);
82 end
83
84 case 'ND'
85 % some consistence checks
86 if ~all(strcmp({mdl2(:).xvar}, mdl1(1).xvar))
87 warning('### The models have different X variables. Taking the first');
88 end
89 if ~all(cellfun(@(x)isequal(x, mdl1(1).xvals),{mdl2(:).xvals}))
90 warning('### The models have different X data. Taking the first');
91 end
92
93 % init output
94 mdl = smodel.newarray([rw1 cl2]);
95 % init expression strins array
96 tmst = cell(rw1,cl2);
97 % do raw by colum product
98 for kk = 1:rw1
99 for jj = 1:cl2
100 tmst{kk,jj} = ['(' mdl1(kk,1).expr.s ').*(' mdl2(1,jj).expr.s ')'];
101 mdl(kk,jj).params = [mdl1(kk,1).params mdl2(1,jj).params];
102 mdl(kk,jj).values = [mdl1(kk,1).values mdl2(1,jj).values];
103 unt = simplify(unit(mdl1(kk,1).yunits).*unit(mdl2(1,jj).yunits));
104 for rr = 2:cl1
105 tmst{kk,jj} = ['(' tmst{kk,jj} ')+(' '(' mdl1(kk,rr).expr.s ').*(' mdl2(rr,jj).expr.s ')' ')'];
106 mdl(kk,jj).params = [mdl(kk,jj).params mdl1(kk,rr).params mdl2(rr,jj).params];
107 mdl(kk,jj).values = [mdl(kk,jj).values mdl1(kk,rr).values mdl2(rr,jj).values];
108 % check on yunits
109 unt2 = simplify(unit(mdl1(kk,rr).yunits).*unit(mdl2(rr,jj).yunits));
110 if ~eq(unt, unt2)
111 error('!!! Units for the elements of %s*%s[%d,%d] must be the same',in_names{1},in_names{2},kk,jj);
112 end
113 end
114 mdl(kk,jj).expr = msym(tmst{kk,jj});
115 mdl(kk,jj).name = ['(' in_names{1} '*' in_names{2} ')[' num2str(kk) ',' num2str(jj) ']'];
116 [mdl(kk,jj).params,i,j] = unique(mdl(kk,jj).params, 'first');
117 mdl(kk,jj).values = mdl(kk,jj).values(i);
118 mdl(kk,jj).xvar = mdl1(kk,1).xvar;
119 mdl(kk,jj).xvals = mdl1(kk,1).xvals;
120 mdl(kk,jj).xunits = mdl1(kk,1).xunits;
121 mdl(kk,jj).yunits = unt;
122 end
123 end
124
125 if ~callerIsMethod
126 mdl(:).addHistory(getInfo('None'), [], in_names, [mdl1.hist, mdl2.hist]);
127 end
128
129 end
130
131 end
132
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 % Local Functions %
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 %--------------------------------------------------------------------------
137 % Get Info Object
138 %--------------------------------------------------------------------------
139 function ii = getInfo(varargin)
140
141 if nargin == 1 && strcmpi(varargin{1}, 'None')
142 sets = {};
143 pl = [];
144 else
145 sets = {'Default'};
146 pl = getDefaultPlist();
147 end
148 % Build info object
149 ii = minfo(mfilename, 'smodel', 'ltpda', utils.const.categories.aop, '$Id: mtimes.m,v 1.7 2011/05/10 21:00:15 mauro Exp $', sets, pl);
150 ii.setArgsmin(2);
151 ii.setModifier(false);
152 end
153
154 %--------------------------------------------------------------------------
155 % Get Default Plist
156 %--------------------------------------------------------------------------
157 function plout = getDefaultPlist()
158 persistent pl;
159 if ~exist('pl', 'var') || isempty(pl)
160 pl = buildplist();
161 end
162 plout = pl;
163 end
164
165 function pl = buildplist()
166 pl = plist.EMPTY_PLIST;
167 end