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

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

Added original make3d

File size: 6.0 KB
Line 
1function y = times(X,Y)
2%TIMES (overloaded)
3
4% Author Johan Löfberg
5% $Id: times.m,v 1.1 2006/08/10 18:00:22 joloef Exp $
6
7% Check dimensions
8[n,m]=size(X);
9if ~((prod(size(X))==1) | (prod(size(Y))==1))
10    if ~((n==size(Y,1) & (m ==size(Y,2))))
11        error('Matrix dimensions must agree.')
12    end
13end;
14
15% Convert block objects
16if isa(X,'blkvar')
17    X = sdpvar(X);
18end
19
20if isa(Y,'blkvar')
21    Y = sdpvar(Y);
22end
23
24
25if isempty(X)
26    YY = full(reshape(Y.basis(:,1),Y.dim(1),Y.dim(2)));
27    y = X.*YY;
28    return
29elseif isempty(Y)
30    XX = full(reshape(X.basis(:,1),X.dim(1),X.dim(2)));
31    y = XX.*Y;
32    return
33end
34
35if (isa(X,'sdpvar') & isa(Y,'sdpvar'))
36
37    if (X.typeflag==5) & (Y.typeflag==5)
38        error('Product of norms not allowed');
39    end
40
41    try
42
43
44        x_isscalar =  (X.dim(1)*X.dim(2)==1);
45        y_isscalar =  (Y.dim(1)*Y.dim(2)==1);
46
47        all_lmi_variables = uniquestripped([X.lmi_variables Y.lmi_variables]);
48        Z = X;Z.dim(1) = 1;Z.dim(2) = 1;Z.lmi_variables = all_lmi_variables;Z.basis = [];
49
50        % Awkward code due to bug in ML6.5
51        Xbase = reshape(X.basis(:,1),X.dim(1),X.dim(2));
52        Ybase = reshape(Y.basis(:,1),Y.dim(1),Y.dim(2));
53        if x_isscalar
54            Xbase = sparse(full(Xbase));
55        end
56        if y_isscalar
57            Ybase = sparse(full(Ybase));
58        end
59
60
61        index_Y = zeros(length(all_lmi_variables),1);
62        index_X = zeros(length(all_lmi_variables),1);
63        for j = 1:length(all_lmi_variables)
64            indexy = find(all_lmi_variables(j)==Y.lmi_variables);
65            indexx = find(all_lmi_variables(j)==X.lmi_variables);
66            if ~isempty(indexy)
67                index_Y(j) = indexy;
68            end
69            if ~isempty(indexx)
70                index_X(j) = indexx;
71            end
72        end
73
74        ny = Y.dim(1);
75        my = Y.dim(2);
76        nx = X.dim(1);
77        mx = X.dim(2);
78
79        % Linear terms
80        base = Xbase.*Ybase;
81        Z.basis = base(:);
82        x_base_not_zero = nnz(Xbase)>0;
83        y_base_not_zero = nnz(Ybase)>0;
84        for i = 1:length(all_lmi_variables)
85            base = 0;
86            if index_Y(i) & x_base_not_zero
87                base = Xbase.*getbasematrixwithoutcheck(Y,index_Y(i));
88            end
89            if index_X(i) & y_base_not_zero
90                base = base + getbasematrixwithoutcheck(X,index_X(i)).*Ybase;
91            end
92            Z.basis(:,i+1) = base(:);
93        end
94
95        % Nonlinear terms
96        i = i+1;
97        ix=1;
98
99        new_mt = [];
100        mt = yalmip('monomtable');
101        nvar = length(all_lmi_variables);
102        local_mt = mt(all_lmi_variables,:);
103        theyvars = find(index_Y);
104        thexvars = find(index_X);
105
106        hash = randn(size(mt,2),1);
107        mt_hash = mt*hash;
108
109        for ix = thexvars(:)'
110
111            if mx==1
112                Xibase = X.basis(:,1+index_X(ix));
113            else
114                Xibase = reshape(X.basis(:,1+index_X(ix)),nx,mx);
115            end
116            mt_x = local_mt(ix,:);
117         
118            for iy = theyvars(:)'
119                    ff=Y.basis(:,1+index_Y(iy));
120                    Yibase = reshape(ff,ny,my);
121                    prodbase = Xibase.*Yibase;             
122                if (norm(prodbase,inf)>1e-12)
123                    mt_y = local_mt(iy,:);
124                    % Idiot-hash the lists
125                    new_hash = (mt_x+mt_y)*hash;
126                    if abs(new_hash)<eps%if nnz(mt_x+mt_y)==0
127                        Z.basis(:,1) = Z.basis(:,1) + prodbase(:);
128                    else
129                        before = find(abs(mt_hash-(mt_x+mt_y)*hash)<eps);
130                        if isempty(before)
131                            mt = [mt;mt_x+mt_y];
132                            mt_hash = [mt_hash;(mt_x+mt_y)*hash];
133                            Z.lmi_variables = [Z.lmi_variables size(mt,1)];
134                        else
135                            Z.lmi_variables = [Z.lmi_variables before];
136                        end
137                        Z.basis(:,i+1)  = prodbase(:);i = i+1;
138                    end
139                end
140            end
141        end
142
143        % Fucked up order
144        if any(diff(Z.lmi_variables)<0)
145            [i,j]=sort(Z.lmi_variables);
146            Z.lmi_variables = Z.lmi_variables(j);
147            Z.basis(:,2:end) = Z.basis(:,j+1);
148        end
149
150        % FIX : Speed up
151        if length(Z.lmi_variables) ~=length(unique(Z.lmi_variables))
152            un_Z_vars = unique(Z.lmi_variables);
153            newZbase = Z.basis(:,1);
154            for i = 1:length(un_Z_vars)
155                newZbase = [newZbase sum(Z.basis(:,find(un_Z_vars(i)==Z.lmi_variables)+1),2)];
156            end
157            Z.basis = newZbase;
158            Z.lmi_variables = un_Z_vars;
159        end
160
161        yalmip('setmonomtable',mt);
162
163        if ~(x_isscalar | y_isscalar)
164            Z.dim(1) = X.dim(1);
165            Z.dim(2) = Y.dim(2);
166        else
167            Z.dim(1) = max(X.dim(1),Y.dim(1));
168            Z.dim(2) = max(X.dim(2),Y.dim(2));
169        end
170    catch
171        error(lasterr)
172    end
173    % Reset info about conic terms
174    Z.conicinfo = [0 0];
175    y = clean(Z);
176    return
177end
178
179if isa(X,'sdpvar')
180    lmi_variables = getvariables(X);
181    nv = length(lmi_variables);
182    y  = X;
183    n = X.dim(1);
184    m = X.dim(2);
185    temp = (reshape(X.basis(:,1),n,m)).*Y;
186    y.basis = temp(:);
187    for i = 1:nv
188        temp = (reshape(X.basis(:,i+1),n,m)).*Y;
189        %  temp = temp(:);
190        % [i1,j1,s1] = find(temp);
191        y.basis(:,i+1) = temp(:);
192        % y.basis(i1,i+1) = temp(i1);
193    end;
194    y.dim(1) = size(temp,1);
195    y.dim(2) = size(temp,2);
196end
197
198if isa(Y,'sdpvar')
199    lmi_variables = getvariables(Y);
200    nv = length(lmi_variables);
201    y = Y;
202    n = Y.dim(1);
203    m = Y.dim(2);
204    temp = X.*(reshape(Y.basis(:,1),n,m));
205    y.basis = temp(:);
206    if m==1
207        for i = 1:nv
208            y.basis(:,i+1) = X.*Y.basis(:,i+1);
209        end
210    else
211        for i = 1:nv
212            temp = X.*(reshape(Y.basis(:,i+1),n,m));
213            y.basis(:,i+1) = temp(:);
214        end
215    end
216    y.dim(1) = size(temp,1);
217    y.dim(2) = size(temp,2);
218end
219% Reset info about conic terms
220y.conicinfo = [0 0];
221y = clean(y);
222
223
Note: See TracBrowser for help on using the repository browser.