source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/@sdpvar/replace.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 3.7 KB
Line 
1function 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
16if ~isa(X,'sdpvar')
17    Z = X;
18    return
19end
20if ~isa(Y,'sdpvar')
21  error('Second arguments must be an sdpvar object')
22end
23
24if ~is(Y,'linear')
25  error('Second arguments must be linear')
26end
27
28if prod(size(W)) == 1
29    W = repmat(W,size(Y));
30end
31
32if ~isequal(size(Y),size(W))
33  error('Both arguments must have same size')
34end
35
36if isa(W,'sdpvar')
37    % This is tricky...
38    Z = variable_replace(X,Y,W);
39    return
40end
41
42if ~isa(W,'double')
43  error('Third arguments must be a double')
44end
45
46% Replace with NaN   destroys everything, assume it should be cleared
47W(isnan(W)) = 0;
48
49y_lmi_variables = Y.lmi_variables;
50b = W(:)-Y.basis(:,1);
51A = Y.basis(:,2:end);
52feas_var = A\b;
53if norm(A*feas_var-b)>sqrt(eps)
54  error('Inconsistent assignment')
55end
56
57x_lmi_variables = X.lmi_variables;
58n = X.dim(1);
59m = X.dim(2);
60
61[monomtable,variabletype] = yalmip('monomtable');
62if 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
72else
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;
92end
93
94Xvariables = getvariables(Z);
95extvar = yalmip('extvariables');
96Xext = find(ismember(Xvariables,extvar));
97if ~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);
128end
129
130
131if 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];
137else
138    Z = reshape(full(Z),n,m);
139end
140
141
142
143
144
145
146
147
148
Note: See TracBrowser for help on using the repository browser.