1 | function Z = variable_replace(X,Y,W) |
---|
2 | |
---|
3 | % Check so that Y is a simple unit variable |
---|
4 | Ybase = getbase(Y); |
---|
5 | Yvariables = getvariables(Y); |
---|
6 | Xbase = getbase(X); |
---|
7 | Xvariables = getvariables(X); |
---|
8 | [i,j,k] = find(Ybase); |
---|
9 | if ~isequal(sort(i),1:length(i)) |
---|
10 | end |
---|
11 | if ~isequal(sort(j),2:(length(i)+1)) |
---|
12 | end |
---|
13 | if ~all(k == 1) |
---|
14 | end |
---|
15 | |
---|
16 | [mt,variabletype] = yalmip('monomtable'); |
---|
17 | if all(variabletype(Xvariables) == 0) |
---|
18 | % Simple linear replacement |
---|
19 | v = 1; |
---|
20 | for i = 1:length(Xvariables) |
---|
21 | XisinY = find(Xvariables(i) == Yvariables); |
---|
22 | if ~isempty(XisinY) |
---|
23 | v = [v;W(XisinY)]; |
---|
24 | else |
---|
25 | v = [v;recover(Xvariables(i))]; |
---|
26 | end |
---|
27 | end |
---|
28 | Z = Xbase*v; |
---|
29 | Z = reshape(Z,size(X,1),size(X,2)); |
---|
30 | else |
---|
31 | error('Nonlinear replacement not supported yet') |
---|
32 | end |
---|
33 | |
---|
34 | % This has not been tested (copied from variable_replace) so it is placed |
---|
35 | % in a catch t obe safe. |
---|
36 | try |
---|
37 | Xvariables = getvariables(Z); |
---|
38 | extvar = yalmip('extvariables'); |
---|
39 | Xext = find(ismember(Xvariables,extvar)); |
---|
40 | if ~isempty(Xext) |
---|
41 | %We must dig down in extended operators to see if they use the replaced |
---|
42 | %set of variables |
---|
43 | for i = 1:length(Xext) |
---|
44 | extstruct = yalmip('extstruct',Xvariables(Xext(i))); |
---|
45 | anychanged = 0; |
---|
46 | for j = 1:length(extstruct.arg) |
---|
47 | if isa(extstruct.arg{j},'sdpvar') |
---|
48 | XinY = find(ismembc(getvariables(extstruct.arg{j}),Yvariables)); |
---|
49 | if ~isempty(XinY) |
---|
50 | anychanged = 1; |
---|
51 | extstruct.arg{j} = replace(extstruct.arg{j},Y,W); |
---|
52 | else |
---|
53 | end |
---|
54 | end |
---|
55 | end |
---|
56 | if anychanged |
---|
57 | Zi = yalmip('addextendedvariable',extstruct.fcn,extstruct.arg{:}); |
---|
58 | Xvariables(Xext(i)) = getvariables(Zi); |
---|
59 | end |
---|
60 | end |
---|
61 | % And now recover this sucker |
---|
62 | Z = struct(Z); |
---|
63 | Z.lmi_variables = Xvariables; |
---|
64 | % Fucked up order (lmi_variables should be sorted) |
---|
65 | if any(diff(Z.lmi_variables)<0) |
---|
66 | [i,j]=sort(Z.lmi_variables); |
---|
67 | Z.basis = [Z.basis(:,1) Z.basis(:,j+1)]; |
---|
68 | Z.lmi_variables = Z.lmi_variables(j); |
---|
69 | end |
---|
70 | Z = sdpvar(Z.dim(1),Z.dim(2),[],Z.lmi_variables,Z.basis); |
---|
71 | end |
---|
72 | catch |
---|
73 | end |
---|