comparison m-toolbox/classes/@matrix/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 matrix objects.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MTIMES implements mtimes operator for matrix objects.
5 %
6 % CALL: obj = obj1 * obj2
7 % obj = mtimes(obj1,obj2);
8 % obj = mtimes(obj1,obj2,obj3) == mtimes(add(obj1,obj2),obj3)
9 %
10 % <a href="matlab:utils.helper.displayMethodInfo('matrix', 'mtimes')">Parameters Description</a>
11 %
12 % VERSION: $Id: mtimes.m,v 1.7 2011/10/13 12:43:49 hewitson Exp $
13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15
16 function varargout = mtimes(varargin)
17
18 callerIsMethod = utils.helper.callerIsMethod;
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 if nargout == 0
27 error('### Matrix multiplication operator can not be used as a modifier.');
28 end
29
30 % Collect input variable names
31 in_names = cell(size(varargin));
32 for ii = 1:nargin,in_names{ii} = inputname(ii);end
33
34 mat1 = varargin{1};
35 mat2 = varargin{2};
36
37 if isnumeric(mat1) || ischar(mat1)
38 expr = mat1;
39 varNames{1} = num2str(expr);
40 % build a numeric/char object of the same size of mat2
41 mat1 = copy(mat2,1);
42 % check we do not have filters
43 if any(isa(mat1.objs,'ltpda_filter'))
44 error('### Undefined function or method %s for input arguments of type ltpda_filter',op);
45 end
46 for ii=1:numel(mat1.objs)
47 mat1.objs(ii)=expr;
48 mat1.objs(ii).setName(varNames{1});
49 mat1.objs(ii).setXunits(mat2.objs(ii).xunits);
50 end
51 end
52 if isnumeric(mat2) || ischar(mat2)
53 expr = mat2;
54 varNames{2} = num2str(expr);
55 % build a numeric/char object of the same size of mat1
56 mat2 = copy(mat1,1);
57 % check we do not have filters
58 if any(isa(mat2.objs,'ltpda_filter'))
59 error('### Undefined function or method %s for input arguments of type ltpda_filter',op);
60 end
61 for ii=1:numel(mat2.objs)
62 mat2.objs(ii)=expr;
63 mat2.objs(ii).setName(varNames{2});
64 mat2.objs(ii).setXunits(mat1.objs(ii).xunits);
65 end
66 end
67
68 % init output
69 mat = copy(mat1,1);
70
71 % check we do not have filters
72 if any(isa(mat1.objs,'ltpda_filter')) || any(isa(mat2.objs,'ltpda_filter'))
73 error('### Undefined function or method %s for input arguments of type ltpda_filter',op);
74 end
75
76 [rw1,cl1] = size(mat1.objs);
77 [rw2,cl2] = size(mat2.objs);
78
79 % Check input model dimensions
80 if ((rw1 == 1) && (rw2 == 1) && (cl1 == 1) && (cl2 == 1))
81 ids = '1D';
82 else
83 if (cl1 ~= rw2)
84 error('!!! Matrices inner dimensions must agree')
85 else
86 ids = 'ND';
87 end
88 end
89
90 switch ids
91 case '1D'
92
93 mat.objs = mat1.objs * mat2.objs;
94 if ~callerIsMethod
95 mat.addHistory(getInfo('None'), [], {inputname(1), inputname(2)}, [mat1.hist mat2.hist]);
96 end
97 case 'ND'
98 % init output
99 estr = sprintf('nobjs(%s,%s) = %s;',num2str(rw1),num2str(cl2),class(mat1.objs(1)));
100 eval(estr)
101 mat = matrix(nobjs,plist('shape',[rw1,cl2]));
102
103 % do row by colum product
104 for kk = 1:rw1
105 for jj = 1:cl2
106 % fix the first element of the sum
107 tobj = mat1.objs(kk,1).*mat2.objs(1,jj);
108 for zz = 2:cl1
109 tobj = tobj + mat1.objs(kk,zz).*mat2.objs(zz,jj);
110 end
111 % simplify y units
112 tobj_yu = tobj.yunits;
113 tobj_yu.simplify;
114 tobj.setYunits(tobj_yu);
115 if isa(tobj, 'data2D')
116 % simplify x units
117 if ~isempty(tobj.xunits)
118 tobj_xu = tobj.xunits;
119 tobj_xu.simplify;
120 tobj.setXunits(tobj_xu);
121 end
122 end
123 mat.objs(kk,jj) = tobj;
124 end
125 end
126 if ~callerIsMethod
127 mat(:).addHistory(getInfo('None'), [], {inputname(1), inputname(2)}, [mat1(:).hist mat2(:).hist]);
128 end
129 end
130
131 varargout{1} = mat;
132
133 end
134
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 % Local Functions %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %
141 % FUNCTION: getInfo
142 %
143 % DESCRIPTION: Get Info Object
144 %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 function ii = getInfo(varargin)
148
149 if nargin == 1 && strcmpi(varargin{1}, 'None')
150 sets = {};
151 pls = [];
152 else
153 sets = {'Default'};
154 pls = getDefaultPlist;
155 end
156 % Build info object
157 ii = minfo(mfilename, 'matrix', 'ltpda', utils.const.categories.aop, '$Id: mtimes.m,v 1.7 2011/10/13 12:43:49 hewitson Exp $', sets, pls);
158 ii.setArgsmin(2);
159 ii.setModifier(false);
160 end
161
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 %
164 % FUNCTION: getDefaultPlist
165 %
166 % DESCRIPTION: Get Default Plist
167 %
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 function plout = getDefaultPlist()
171 persistent pl;
172 if exist('pl', 'var')==0 || isempty(pl)
173 pl = buildplist();
174 end
175 plout = pl;
176 end
177
178 function pl = buildplist()
179 pl = plist.EMPTY_PLIST;
180 end