1 | function Z = mtimes(X,Y) |
---|
2 | %MTIMES (overloaded) |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: mtimes.asv,v 1.2 2006/08/10 23:02:56 joloef Exp $ |
---|
6 | |
---|
7 | % Check classes |
---|
8 | X_is_spdvar = isa(X,'sdpvar'); |
---|
9 | Y_is_spdvar = isa(Y,'sdpvar'); |
---|
10 | |
---|
11 | X_is_ncvar = isa(X,'ncvar'); |
---|
12 | Y_is_ncvar = isa(Y,'ncvar'); |
---|
13 | |
---|
14 | |
---|
15 | nonCommutingTable = yalmip('nonCommutingTable'); |
---|
16 | [monomtable,variabletype] = yalmip('monomtable'); |
---|
17 | if size(monomtable,1)>size(nonCommutingTable,1) |
---|
18 | nonCommutingTable((1+size(nonCommutingTable,1)):(size(monomtable,1)),1) = (1+size(nonCommutingTable,1)):(size(monomtable,1)); |
---|
19 | end |
---|
20 | % Cast commutative variables are nc |
---|
21 | %nonCommutingTable(find(~any(monomtable,2)),1) = find(~any(monomtable,2)); |
---|
22 | |
---|
23 | x_variables = getvariables(X);Xbase = getbase(X); |
---|
24 | y_variables = getvariables(Y);Ybase = getbase(Y); |
---|
25 | temp_monom_table = []; |
---|
26 | temp_nc_table = []; |
---|
27 | temp_c_table = []; |
---|
28 | new_base = []; |
---|
29 | |
---|
30 | for i = 0:length(x_variables) |
---|
31 | if i>0 |
---|
32 | x_monom = nonCommutingTable(x_variables(i),:); |
---|
33 | else |
---|
34 | x_monom = nan; |
---|
35 | end |
---|
36 | x_base = Xbase(:,i+1); |
---|
37 | for j = 0:length(y_variables) |
---|
38 | if j>0 |
---|
39 | y_monom = nonCommutingTable(y_variables(j),:); |
---|
40 | else |
---|
41 | y_monom = nan; |
---|
42 | end |
---|
43 | y_base = Ybase(:,j+1); |
---|
44 | xy_base = reshape(x_base,size(X))*reshape(y_base,size(Y)); |
---|
45 | |
---|
46 | if (i == 0) & (j== 0) |
---|
47 | new_base = xy_base(:); |
---|
48 | else |
---|
49 | xy_monom = [x_monom(2:end) y_monom(2:end)]; |
---|
50 | xy_monom = xy_monom(find(xy_monom)); |
---|
51 | temp_nc_table(end+1,1:length(xy_monom)) = xy_monom; |
---|
52 | temp_c_table(end+1,1:2) = [x_monom(1) y_monom(1)]; |
---|
53 | new_base = [new_base xy_base(:)]; |
---|
54 | end |
---|
55 | end |
---|
56 | end |
---|
57 | |
---|
58 | for i = 1:size(temp_c_table,1) |
---|
59 | aux = spalloc(1,size(monomtable,2),2); |
---|
60 | if ~isnan(temp_c_table(i,1)) |
---|
61 | aux(temp_c_table(i,1)) = aux(temp_c_table(i,1)) + 1; |
---|
62 | end |
---|
63 | if ~isnan(temp_c_table(i,2)) |
---|
64 | aux(temp_c_table(i,2)) = aux(temp_c_table(i,2)) + 1; |
---|
65 | end |
---|
66 | if nnz(aux)>0 |
---|
67 | candidates = findrows(monomtable,aux); |
---|
68 | if ~isempty(aux) |
---|
69 | temp_c_table(i,1) = candidates; |
---|
70 | else |
---|
71 | monomtable = [monomtable;aux]; |
---|
72 | nonCommutingTable(end+1,1) = nan; |
---|
73 | temp_c_table(i,1) = size(monomtable,1); |
---|
74 | switch sum(aux) |
---|
75 | case 1 |
---|
76 | variabletype(end+1) = 0; |
---|
77 | case 2 |
---|
78 | if nnz(aux) == 1 |
---|
79 | variabletype(end+1) = 2; |
---|
80 | else |
---|
81 | variabletype(end+1) = 1; |
---|
82 | end |
---|
83 | otherwise |
---|
84 | variabletype(end+1) = 3; |
---|
85 | end |
---|
86 | end |
---|
87 | end |
---|
88 | end |
---|
89 | |
---|
90 | temp_nc_table = [temp_c_table(:,1) temp_nc_table]; |
---|
91 | % Okay, now we have the monomials. Now we have to match them to |
---|
92 | % possible earlier monomials |
---|
93 | if size(nonCommutingTable,2) < size(temp_nc_table,2) |
---|
94 | nonCommutingTable(1,size(temp_nc_table,2)) = 0; |
---|
95 | elseif size(temp_nc_table,2) < size(nonCommutingTable,2) |
---|
96 | temp_nc_table(1,size(nonCommutingTable,2)) = 0; |
---|
97 | end |
---|
98 | for i = 1:size(temp_nc_table,1) |
---|
99 | candidates = findrows_nan(nonCommutingTable,temp_nc_table(i,:)); |
---|
100 | if isempty(candidates) |
---|
101 | nonCommutingTable = [nonCommutingTable;temp_nc_table(i,:)]; |
---|
102 | monomtable(end+1,end+1) = 0; |
---|
103 | involved = temp_nc_table(i,1+find(temp_nc_table(i,2:end))); |
---|
104 | switch length(involved) |
---|
105 | case 1 |
---|
106 | if isnan(temp_nc_table(i,1)) |
---|
107 | variabletype(end+1) = 0; |
---|
108 | else |
---|
109 | if variabletype(temp_nc_table(i,1)) == 1 |
---|
110 | variabletype(end+1) = 2; |
---|
111 | else |
---|
112 | variabletype(end+1) = 3; |
---|
113 | end |
---|
114 | end |
---|
115 | case 2 |
---|
116 | if involved(1) == involved(2) |
---|
117 | variabletype(end+1) = 2; |
---|
118 | else |
---|
119 | variabletype(end+1) = 1; |
---|
120 | end |
---|
121 | otherwise |
---|
122 | variabletype(end+1) = 3; |
---|
123 | end |
---|
124 | lmivariables(i) = size(nonCommutingTable,1); |
---|
125 | else |
---|
126 | lmivariables(i) = candidates; |
---|
127 | |
---|
128 | end |
---|
129 | end |
---|
130 | if X_is_ncvar |
---|
131 | Z = X; |
---|
132 | else |
---|
133 | Z = Y; |
---|
134 | end |
---|
135 | Z.basis = new_base; |
---|
136 | Z.lmi_variables = lmivariables; |
---|
137 | |
---|
138 | % Fucked up order (lmi_variables should be sorted and unique) |
---|
139 | if any(diff(Z.lmi_variables)<0) |
---|
140 | [i,j]=sort(Z.lmi_variables); |
---|
141 | Z.basis = [Z.basis(:,1) Z.basis(:,j+1)]; |
---|
142 | Z.lmi_variables = Z.lmi_variables(j); |
---|
143 | end |
---|
144 | [un_Z_vars2] = uniquestripped(Z.lmi_variables); |
---|
145 | if length(un_Z_vars2) < length(Z.lmi_variables) |
---|
146 | [un_Z_vars,hh,jj] = unique(Z.lmi_variables); |
---|
147 | if length(Z.lmi_variables) ~=length(un_Z_vars) |
---|
148 | Z.basis = Z.basis*sparse([1 1+jj],[1 1+(1:length(jj))],ones(1,1+length(jj)))'; |
---|
149 | Z.lmi_variables = un_Z_vars; |
---|
150 | end |
---|
151 | end |
---|
152 | |
---|
153 | Z = clean(Z); |
---|
154 | yalmip('nonCommutingTable',nonCommutingTable); |
---|
155 | yalmip('setmonomtable',monomtable,variabletype); |
---|
156 | |
---|
157 | function c = findrows_nan(a,b) |
---|
158 | a(isnan(a)) = 0; |
---|
159 | b(isnan(b)) = 0; |
---|
160 | c=findrows(a,b); |
---|