1 | function y = plus(X,Y) |
---|
2 | %PLUS (overloaded) |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: plus.m,v 1.2 2006/08/11 11:48:15 joloef Exp $ |
---|
6 | |
---|
7 | if isa(X,'sdpvar') |
---|
8 | X = ncvar(struct(X)); |
---|
9 | elseif isa(Y,'sdpvar') |
---|
10 | Y = ncvar(struct(Y)); |
---|
11 | end |
---|
12 | |
---|
13 | X_is_ncvar = isa(X,'ncvar'); |
---|
14 | Y_is_ncvar = isa(Y,'ncvar'); |
---|
15 | |
---|
16 | switch 2*X_is_ncvar+Y_is_ncvar |
---|
17 | case 1 |
---|
18 | if isempty(X) |
---|
19 | try |
---|
20 | y = full(X - reshape(Y.basis(:,1),Y.dim(1),Y.dim(2))); |
---|
21 | catch |
---|
22 | error(lasterr); |
---|
23 | end |
---|
24 | return |
---|
25 | end |
---|
26 | |
---|
27 | y = Y; |
---|
28 | n_Y = Y.dim(1); |
---|
29 | m_Y = Y.dim(2); |
---|
30 | [n_X,m_X] = size(X); |
---|
31 | x_isscalar = (n_X*m_X==1); |
---|
32 | y_isscalar = (n_Y*m_Y==1); |
---|
33 | any_scalar = x_isscalar | y_isscalar; |
---|
34 | |
---|
35 | if x_isscalar & y_isscalar |
---|
36 | y.basis(1) = y.basis(1)+X; |
---|
37 | % Reset info about conic terms |
---|
38 | y.conicinfo = [0 0]; |
---|
39 | return |
---|
40 | end |
---|
41 | |
---|
42 | if any_scalar | ([n_Y m_Y]==[n_X m_X]) |
---|
43 | if y_isscalar |
---|
44 | y.basis = repmat(y.basis,n_X*m_X,1); |
---|
45 | y.dim(1) = n_X; |
---|
46 | y.dim(2) = m_X; |
---|
47 | end |
---|
48 | y.basis(:,1) = y.basis(:,1)+X(:); |
---|
49 | else |
---|
50 | error('Matrix dimensions must agree.'); |
---|
51 | end |
---|
52 | % Reset info about conic terms |
---|
53 | y.conicinfo = [0 0]; |
---|
54 | |
---|
55 | case 2 |
---|
56 | |
---|
57 | if isempty(Y) |
---|
58 | try |
---|
59 | y = full(reshape(X.basis(:,1),X.dim(1),X.dim(2))-Y); |
---|
60 | catch |
---|
61 | error(lasterr); |
---|
62 | end |
---|
63 | return |
---|
64 | end |
---|
65 | |
---|
66 | y = X; |
---|
67 | n_X = X.dim(1); |
---|
68 | m_X = X.dim(2); |
---|
69 | [n_Y,m_Y] = size(Y); |
---|
70 | x_isscalar = (n_X*m_X==1); |
---|
71 | y_isscalar = (n_Y*m_Y==1); |
---|
72 | any_scalar = x_isscalar | y_isscalar; |
---|
73 | |
---|
74 | % Special special case... |
---|
75 | if x_isscalar & y_isscalar |
---|
76 | y.basis(1) = y.basis(1)+Y; |
---|
77 | % Reset info about conic terms |
---|
78 | y.conicinfo = [0 0]; |
---|
79 | return |
---|
80 | end |
---|
81 | |
---|
82 | if any_scalar | ([n_Y m_Y]==[n_X m_X]) |
---|
83 | if x_isscalar |
---|
84 | y.basis = repmat(y.basis,n_Y*m_Y,1); |
---|
85 | y.dim(1) = n_Y; |
---|
86 | y.dim(2) = m_Y; |
---|
87 | end |
---|
88 | y.basis(:,1) = y.basis(:,1)+Y(:); |
---|
89 | else |
---|
90 | error('Matrix dimensions must agree.'); |
---|
91 | end |
---|
92 | % Reset info about conic terms |
---|
93 | y.conicinfo = [0 0]; |
---|
94 | |
---|
95 | case 3 |
---|
96 | |
---|
97 | n_X = X.dim(1); |
---|
98 | m_X = X.dim(2); |
---|
99 | n_Y = Y.dim(1); |
---|
100 | m_Y = Y.dim(2); |
---|
101 | x_isscalar = (n_X*m_X==1); |
---|
102 | y_isscalar = (n_Y*m_Y==1); |
---|
103 | any_scalar = x_isscalar | y_isscalar; |
---|
104 | |
---|
105 | if (~((n_X==n_Y) & (m_X==m_Y))) & ~any_scalar |
---|
106 | error('Matrix dimensions must agree.') |
---|
107 | end |
---|
108 | |
---|
109 | all_lmi_variables = uniquestripped([X.lmi_variables Y.lmi_variables]); |
---|
110 | y = X; |
---|
111 | X.basis = []; |
---|
112 | y.lmi_variables = all_lmi_variables; |
---|
113 | |
---|
114 | % ismembc faster (buggy?) |
---|
115 | in_X = find(ismembc(all_lmi_variables,X.lmi_variables)); |
---|
116 | in_Y = find(ismembc(all_lmi_variables,Y.lmi_variables)); |
---|
117 | % in_X = find(ismember(all_lmi_variables,X.lmi_variables)); |
---|
118 | % in_Y = find(ismember(all_lmi_variables,Y.lmi_variables)); |
---|
119 | |
---|
120 | if isequal(X.lmi_variables,Y.lmi_variables) & n_Y==n_X & m_Y==m_X |
---|
121 | y.basis = y.basis + Y.basis; |
---|
122 | if length(X.lmi_variables)==1 |
---|
123 | if all(y.basis(:,2)==0) |
---|
124 | y = full(y.basis(1)); |
---|
125 | else |
---|
126 | % Reset info about conic terms |
---|
127 | y.conicinfo = [0 0]; |
---|
128 | end |
---|
129 | return |
---|
130 | end |
---|
131 | else |
---|
132 | if 1 |
---|
133 | [ix,jx,sx] = find(y.basis);y.basis = []; |
---|
134 | [iy,jy,sy] = find(Y.basis);Y.basis = []; |
---|
135 | mapX = [1 1+in_X]; |
---|
136 | mapY = [1 1+in_Y]; |
---|
137 | basis_X = sparse(ix,mapX(jx),sx,n_X*m_X,1+length(all_lmi_variables));ix=[];jx=[];sx=[]; |
---|
138 | basis_Y = sparse(iy,mapY(jy),sy,n_Y*m_Y,1+length(all_lmi_variables));iy=[];jy=[];sy=[]; |
---|
139 | else |
---|
140 | % MATLAB sparse fails on this for huge problems at a certain size |
---|
141 | basis_X = spalloc(n_X*m_X,1+length(all_lmi_variables),nnz(X.basis)); |
---|
142 | basis_Y = spalloc(n_Y*m_Y,1+length(all_lmi_variables),nnz(Y.basis)); |
---|
143 | basis_X(:,[1 1+in_X])=y.basis;y.basis = []; |
---|
144 | basis_Y(:,[1 1+in_Y])=Y.basis;Y.basis = []; |
---|
145 | end |
---|
146 | |
---|
147 | % Fix addition of matrix+scalar |
---|
148 | if n_X*m_X<n_Y*m_Y |
---|
149 | y.dim(1) = n_Y; |
---|
150 | y.dim(2) = m_Y; |
---|
151 | basis_X = repmat(basis_X,n_Y*m_Y,1); |
---|
152 | end |
---|
153 | if n_Y*m_Y<n_X*m_X |
---|
154 | y.dim(1) = n_X; |
---|
155 | y.dim(2) = m_X; |
---|
156 | basis_Y = repmat(basis_Y,n_X*m_X,1); |
---|
157 | end |
---|
158 | % OK, solution is... |
---|
159 | y.basis = basis_X;basis_X = []; |
---|
160 | y.basis = y.basis+basis_Y;basis_Y = []; |
---|
161 | end |
---|
162 | % Reset info about conic terms |
---|
163 | y.conicinfo = [0 0]; |
---|
164 | y = clean(y); |
---|
165 | |
---|
166 | otherwise |
---|
167 | end |
---|
168 | |
---|
169 | |
---|