forked from mpf/spot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opSum.m
109 lines (88 loc) · 3.51 KB
/
opSum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
classdef opSum < opSpot
%OPSUM Addition of two operators.
%
% opSum(A,B) creates a compound operator representing (A + B).
%
% See also opMinus.
% Copyright 2009, Ewout van den Berg and Michael P. Friedlander
% See the file COPYING.txt for full copyright information.
% Use the command 'spot.gpl' to locate this file.
% http://www.cs.ubc.ca/labs/scl/spot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties (SetAccess = private)
operators = {}; % List of preprocessed operators
end % Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function op = opSum(A,B)
if nargin ~= 2
error('Exactly two operators must be specified.')
end
% Input matrices are immediately cast as opMatrix's.
if isa(A,'numeric'), A = opMatrix(A); end
if isa(B,'numeric'), B = opMatrix(B); end
% Check that the input operators are valid.
if ~( isa(A,'opSpot') && isa(B,'opSpot') )
error('One of the operators is not a valid input.')
end
% Check operator consistency and complexity
[mA, nA] = size(A);
[mB, nB] = size(B);
compatible = ((mA == mB) && (nA == nB));
if ~compatible
error('Operators are not compatible in size.');
end
% Determine size
m = mA; n = nA;
% Construct operator
op = op@opSpot('Sum', m, n);
op.cflag = A.cflag | B.cflag;
op.linear = A.linear | B.linear;
op.sweepflag = A.sweepflag & B.sweepflag;
op.children = {A, B};
op.precedence = 4;
end % Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function str = char(op)
% Get operators
op1 = op.children{1};
op2 = op.children{2};
% Format first operator
str1 = char(op1);
if op1.precedence > op.precedence
str1 = ['(',str1,')'];
end
% Format second operator
str2 = char(op2);
if op2.precedence > op.precedence
str2 = ['(',str2,')'];
end
% Combine
str = [str1, ' + ', str2];
end % Char
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Double
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function A = double(op)
A = double(op.children{1}) + double(op.children{2});
end
end % Methods
methods ( Access = protected )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply(op,x,mode)
y = applyMultiply(op.children{1},x,mode);
y = y + applyMultiply(op.children{2},x,mode);
end % Multiply
end % Methods
end % Classdef