[37] | 1 | function Z = replace(X,Y,W) |
---|
| 2 | %REPLACE Substitutes variables |
---|
| 3 | % |
---|
| 4 | %Z = REPLACE(Y,X,W) Replaces any occurence of the SDPVAR object Y |
---|
| 5 | % in the SDPVAR object X with the double W |
---|
| 6 | % |
---|
| 7 | % Example |
---|
| 8 | % x = sdpvar(1,1); |
---|
| 9 | % t = sdpvar(1,1); |
---|
| 10 | % Y = [1+t;1+x+t]; |
---|
| 11 | % Y = replace(Y,x,2) generates Y=[1+t;3+t] |
---|
| 12 | |
---|
| 13 | % Author Johan Löfberg |
---|
| 14 | % $Id: replace.m,v 1.15 2006/08/24 08:35:24 joloef Exp $ |
---|
| 15 | |
---|
| 16 | if ~isa(X,'sdpvar') |
---|
| 17 | Z = X; |
---|
| 18 | return |
---|
| 19 | end |
---|
| 20 | if ~isa(Y,'sdpvar') |
---|
| 21 | error('Second arguments must be an sdpvar object') |
---|
| 22 | end |
---|
| 23 | |
---|
| 24 | if ~is(Y,'linear') |
---|
| 25 | error('Second arguments must be linear') |
---|
| 26 | end |
---|
| 27 | |
---|
| 28 | if prod(size(W)) == 1 |
---|
| 29 | W = repmat(W,size(Y)); |
---|
| 30 | end |
---|
| 31 | |
---|
| 32 | if ~isequal(size(Y),size(W)) |
---|
| 33 | error('Both arguments must have same size') |
---|
| 34 | end |
---|
| 35 | |
---|
| 36 | if isa(W,'sdpvar') |
---|
| 37 | % This is tricky... |
---|
| 38 | Z = variable_replace(X,Y,W); |
---|
| 39 | return |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | if ~isa(W,'double') |
---|
| 43 | error('Third arguments must be a double') |
---|
| 44 | end |
---|
| 45 | |
---|
| 46 | % Replace with NaN destroys everything, assume it should be cleared |
---|
| 47 | W(isnan(W)) = 0; |
---|
| 48 | |
---|
| 49 | y_lmi_variables = Y.lmi_variables; |
---|
| 50 | b = W(:)-Y.basis(:,1); |
---|
| 51 | A = Y.basis(:,2:end); |
---|
| 52 | feas_var = A\b; |
---|
| 53 | if norm(A*feas_var-b)>sqrt(eps) |
---|
| 54 | error('Inconsistent assignment') |
---|
| 55 | end |
---|
| 56 | |
---|
| 57 | x_lmi_variables = X.lmi_variables; |
---|
| 58 | n = X.dim(1); |
---|
| 59 | m = X.dim(2); |
---|
| 60 | |
---|
| 61 | [monomtable,variabletype] = yalmip('monomtable'); |
---|
| 62 | if all(variabletype(x_lmi_variables)==0) % is(X,'linear') |
---|
| 63 | Z = X.basis(:,1); |
---|
| 64 | for i = 1:length(x_lmi_variables) |
---|
| 65 | j = find(x_lmi_variables(i) == y_lmi_variables); |
---|
| 66 | if isempty(j) |
---|
| 67 | Z = Z + recover(x_lmi_variables(i))*X.basis(:,i+1); |
---|
| 68 | else |
---|
| 69 | Z = Z + feas_var(j)*X.basis(:,i+1); |
---|
| 70 | end |
---|
| 71 | end |
---|
| 72 | else |
---|
| 73 | replaced_vars = depends(Y); |
---|
| 74 | % used_variables = getvariables(X); |
---|
| 75 | used_variables = x_lmi_variables; |
---|
| 76 | % monomtable = yalmip('monomtable'); |
---|
| 77 | local_monom = monomtable(used_variables,replaced_vars); |
---|
| 78 | W = W(:)'; |
---|
| 79 | gain = zeros(length(used_variables),1); |
---|
| 80 | for i = 1:length(used_variables) |
---|
| 81 | % F**N 6.5 0^sparse(0) and 0^0 differ |
---|
| 82 | gain(i) = prod(W.^full(local_monom(i,:))); |
---|
| 83 | end |
---|
| 84 | |
---|
| 85 | local_monoms_left = monomtable(used_variables,:); |
---|
| 86 | local_monoms_left(:,replaced_vars) = 0; |
---|
| 87 | used_left = find(sum(local_monoms_left,1)); |
---|
| 88 | base = recovermonoms(local_monoms_left(:,used_left),recover(used_left)); |
---|
| 89 | base = base.*gain(:); |
---|
| 90 | Z = X.basis(:,1); |
---|
| 91 | Z = Z + X.basis(:,2:end)*base; |
---|
| 92 | end |
---|
| 93 | |
---|
| 94 | Xvariables = getvariables(Z); |
---|
| 95 | extvar = yalmip('extvariables'); |
---|
| 96 | Xext = find(ismember(Xvariables,extvar)); |
---|
| 97 | if ~isempty(Xext) |
---|
| 98 | %We must dig down in extended operators to see if they use the replaced |
---|
| 99 | %set of variables |
---|
| 100 | for i = 1:length(Xext) |
---|
| 101 | extstruct = yalmip('extstruct',Xvariables(Xext(i))); |
---|
| 102 | anychanged = 0; |
---|
| 103 | for j = 1:length(extstruct.arg) |
---|
| 104 | if isa(extstruct.arg{j},'sdpvar') |
---|
| 105 | XinY = find(ismembc(getvariables(extstruct.arg{j}),y_lmi_variables)); |
---|
| 106 | if ~isempty(XinY) |
---|
| 107 | anychanged = 1; |
---|
| 108 | extstruct.arg{j} = replace(extstruct.arg{j},Y,W); |
---|
| 109 | else |
---|
| 110 | end |
---|
| 111 | end |
---|
| 112 | end |
---|
| 113 | if anychanged |
---|
| 114 | Zi = yalmip('addextendedvariable',extstruct.fcn,extstruct.arg{:}); |
---|
| 115 | Xvariables(Xext(i)) = getvariables(Zi); |
---|
| 116 | end |
---|
| 117 | end |
---|
| 118 | % And now recover this sucker |
---|
| 119 | Z = struct(Z); |
---|
| 120 | Z.lmi_variables = Xvariables; |
---|
| 121 | % Fucked up order (lmi_variables should be sorted) |
---|
| 122 | if any(diff(Z.lmi_variables)<0) |
---|
| 123 | [i,j]=sort(Z.lmi_variables); |
---|
| 124 | Z.basis = [Z.basis(:,1) Z.basis(:,j+1)]; |
---|
| 125 | Z.lmi_variables = Z.lmi_variables(j); |
---|
| 126 | end |
---|
| 127 | Z = sdpvar(Z.dim(1),Z.dim(2),[],Z.lmi_variables,Z.basis); |
---|
| 128 | end |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | if isa(Z,'sdpvar') |
---|
| 132 | Z.dim(1) = n; |
---|
| 133 | Z.dim(2) = m; |
---|
| 134 | Z.typeflag = X.typeflag; |
---|
| 135 | % Reset info about conic terms |
---|
| 136 | Z.conicinfo = [0 0]; |
---|
| 137 | else |
---|
| 138 | Z = reshape(full(Z),n,m); |
---|
| 139 | end |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|