comparison m-toolbox/classes/@matrix/elementOp.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 % ELEMENTOP applies the given operator to the input matrices.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: ELEMENTOP applies the given operator to the input matrices.
5 %
6 % CALL: a = elementOp(op, opname, opsym, infoObj, pl, fcnArgIn, varNames)
7 %
8 % PARAMETERS: op - MATLAB operation name
9 % opname - Name for displaying
10 % opsym - Operation symbol
11 % infoObj - minfo object
12 % pl - default plist
13 % fcnArgIn - Input argument list of the calling fcn.
14 % varNames - Variable names of the input
15 %
16 % VERSION: $Id: elementOp.m,v 1.10 2011/02/11 12:30:29 hewitson Exp $
17 %
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19
20 function varargout = elementOp(varargin)
21
22 import utils.const.*
23
24 % Settings
25 callerIsMethod = varargin{1};
26 op = varargin{2};
27 opname = varargin{3};
28 opsym = varargin{4};
29
30 infoObj = varargin{5};
31 pl = varargin{6};
32
33 fcnArgIn = varargin{7};
34 varNames = varargin{8};
35
36 if numel(fcnArgIn) ~=2
37 error('### The operator %s can only operate on two inputs', op);
38 end
39
40 mat1 = fcnArgIn{1};
41 mat2 = fcnArgIn{2};
42
43 if isnumeric(mat1) || ischar(mat1)
44 if ischar(mat1)
45 mat1 = eval(mat1);
46 end
47 nums = mat1;
48 varNames{1} = utils.helper.mat2str(nums);
49 % build a numeric/char object of the same size of mat2
50 mat1 = copy(mat2,1);
51 for ii=1:numel(mat1.objs)
52 if numel(nums) == numel(mat1.objs)
53 expr = nums(ii);
54 else
55 expr = nums;
56 end
57 mat1.objs(ii) = feval(class(mat2.objs), expr);
58 mat1.objs(ii).setName(num2str(expr));
59 if isprop(mat2.objs(ii), 'xunits') || (isa(mat2.objs(ii), 'ao') && ...
60 isprop(mat2.objs(ii).data, 'xunits'))
61 mat1.objs(ii).setXunits(mat2.objs(ii).xunits);
62 end
63 end
64 end
65 if isnumeric(mat2) || ischar(mat2)
66 if ischar(mat2)
67 mat2 = eval(mat2);
68 end
69 nums = mat2;
70 varNames{2} = utils.helper.mat2str(nums);
71 % build a numeric/char object of the same size of mat1
72 mat2 = copy(mat1,1);
73 for ii=1:numel(mat2.objs)
74 if numel(nums) == numel(mat2.objs)
75 expr = nums(ii);
76 else
77 expr = nums;
78 end
79 mat2.objs(ii) = feval(class(mat1.objs), expr);
80 mat2.objs(ii).setName(utils.helper.num2str(expr));
81 if isprop(mat2.objs(ii), 'xunits') || (isa(mat2.objs(ii), 'ao') && ...
82 isprop(mat2.objs(ii).data, 'xunits'))
83 mat2.objs(ii).setXunits(mat1.objs(ii).xunits);
84 end
85 end
86 end
87
88 % matrix objects must all contain the same class
89 if ~strcmp(class(mat1.objs), class(mat2.objs))
90 error('### The %s operator can only apply to matrix objects containing the same class of objects. [%s .* %s]', op, class(mat1.objs), class(mat2.objs));
91 end
92
93
94 % init output
95 mat = copy(mat1,1);
96
97 %%%%%%%%%% If the first or second input is only one object then %%%%%%%%%%
98 %%%%%%%%%% resize the input to the size of the other object. %%%%%%%%%%
99 if numel(mat1.objs) == 1 && numel(mat1.objs) ~= numel(mat2.objs)
100 h1 = mat1.hist;
101 obj1 = mat1.objs;
102 obj1(1:numel(mat2.objs)) = obj1;
103 obj1 = reshape(obj1, size(mat2.objs));
104 mat1 = matrix(obj1);
105 mat1.name = varNames{1};
106 mat1.hist = h1;
107 end
108 if numel(mat2.objs) == 1 && numel(mat2.objs) ~= numel(mat1.objs)
109 h2 = mat2.hist;
110 obj2 = mat2.objs;
111 obj2(1:numel(mat1.objs)) = obj2;
112 obj2 = reshape(obj2, size(mat1.objs));
113 mat2 = matrix(obj2);
114 mat2.name = varNames{2};
115 mat2.hist = h2;
116 end
117
118 if ~all(size(mat1.objs) == size(mat2.objs))
119 error('### The Input objects must have the same size. Size of mat1 [%d %d], mat2 [%d %d]', size(mat1.objs,1), size(mat1.objs,2), size(mat2.objs,1), size(mat2.objs,2));
120 end
121
122 % switch between operations
123 switch lower(op)
124 case 'plus'
125 % plus operation
126 for kk = 1:numel(mat1.objs)
127 mat.objs(kk) = mat1.objs(kk) + mat2.objs(kk);
128 end
129 case 'minus'
130 % minus operation
131 for kk = 1:numel(mat1.objs)
132 mat.objs(kk) = mat1.objs(kk) - mat2.objs(kk);
133 end
134 case 'times'
135 % times operation
136 for kk = 1:numel(mat1.objs)
137 mat.objs(kk) = mat1.objs(kk) .* mat2.objs(kk);
138 end
139 case 'rdivide'
140 % rdivide operation
141 for kk = 1:numel(mat1.objs)
142 mat.objs(kk) = mat1.objs(kk) ./ mat2.objs(kk);
143 end
144 end
145
146 if ~callerIsMethod
147 mat.name = [varNames{1} opsym varNames{2}];
148 mat.addHistory(infoObj, pl, varNames, [mat1.hist mat2.hist]);
149 end
150
151 varargout{1} = mat;
152
153 end
154