source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/extras/variable_replace.m @ 37

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

Added original make3d

File size: 2.2 KB
Line 
1function Z = variable_replace(X,Y,W)
2
3% Check so that Y is a simple unit variable
4Ybase = getbase(Y);
5Yvariables = getvariables(Y);
6Xbase = getbase(X);
7Xvariables = getvariables(X);
8[i,j,k] = find(Ybase);
9if ~isequal(sort(i),1:length(i))
10end
11if ~isequal(sort(j),2:(length(i)+1))
12end
13if ~all(k == 1)
14end
15
16[mt,variabletype] = yalmip('monomtable');
17if 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));
30else
31    error('Nonlinear replacement not supported yet')
32end
33
34% This has not been tested (copied from variable_replace) so it is placed
35% in a catch t obe safe.
36try
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
72catch
73end
Note: See TracBrowser for help on using the repository browser.