-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathopKron.m
103 lines (85 loc) · 3.13 KB
/
opKron.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
%opKron Kronecker tensor product
%
% opKron(OP1,OP2,...OPn) creates an operator that is the Kronecker
% tensor product of OP1, OP2, ..., OPn.
% Copyright 2009, Rayan Saab, Ewout van den Berg and Michael P. Friedlander
% http://www.cs.ubc.ca/labs/scl/sparco
% $Id$
classdef opKron < opSpot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function op = opKron(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
% Determine operator size and complexity (this code is
% general for any number of operators)
opList = {A,B};
opA = opList{1};
[m,n] = size(opA);
cflag = opA.cflag;
linear = opA.linear;
for i=2:length(opList)
opA = opList{i};
cflag = cflag | opA.cflag;
linear = linear & opA.linear;
[mi,ni]= size(opA);
m = m * mi; n = n * ni;
end
% Construct operator
op = op@opSpot('Kron', m, n);
op.cflag = cflag;
op.linear = linear;
op.children = opList;
end % Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function str = char(op)
% Get operators
op1 = op.children{1};
op2 = op.children{2};
str = ['Kron(',char(op1),', ',char(op2),')'];
end % Char
end % Methods
methods ( Access = protected )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply(op,x,mode)
opList = op.children;
[m,n] = size(op);
if mode == 1
for i=length(opList):-1:1
k = size(opList{i},2);
x = reshape(x,k,n/k);
z = opList{i} * x;
x = z.';
n = size(opList{i},1) * n / k;
end
y = x(:);
else
for i=length(opList):-1:1
k = size(opList{i},1);
x = reshape(x,k,m/k);
z = opList{i}' * x;
x = z.';
m = size(opList{i},2) * m / k;
end
y = x(:);
end
end % Multiply
end % Methods
end % Classdef