[37] | 1 | function symb_pvec = sdisplay(pvec,symbolicname) |
---|
| 2 | %SDISPLAY Symbolic display of SDPVAR expression |
---|
| 3 | % |
---|
| 4 | % Note that the symbolic display only work if all |
---|
| 5 | % involved variables are explicitely defined as |
---|
| 6 | % scalar variables. |
---|
| 7 | % |
---|
| 8 | % Variables that not are defined as scalars |
---|
| 9 | % will be given the name ryv(i). ryv means |
---|
| 10 | % recovered YALMIP variables, i indicates the |
---|
| 11 | % index in YALMIP (i.e. the result from getvariables) |
---|
| 12 | % |
---|
| 13 | % If you want to change the generic name ryv, just |
---|
| 14 | % pass a second string argument |
---|
| 15 | % |
---|
| 16 | % EXAMPLES |
---|
| 17 | % sdpvar x y |
---|
| 18 | % sdisplay(x^2+y^2) |
---|
| 19 | % ans = |
---|
| 20 | % 'x^2+y^2' |
---|
| 21 | % |
---|
| 22 | % t = sdpvar(2,1); |
---|
| 23 | % sdisplay(x^2+y^2+t'*t) |
---|
| 24 | % ans = |
---|
| 25 | % 'x^2+y^2+ryv(5)^2+ryv(6)^2' |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | % Author Johan Löfberg |
---|
| 29 | % $Id: sdisplay.m,v 1.10 2006/08/11 11:48:15 joloef Exp $ |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | r1=1:size(pvec,1); |
---|
| 33 | r2=1:size(pvec,2); |
---|
| 34 | |
---|
| 35 | for pi = 1:size(pvec,1) |
---|
| 36 | for pj = 1:size(pvec,2) |
---|
| 37 | |
---|
| 38 | p = pvec(pi,pj); |
---|
| 39 | |
---|
| 40 | if isa(p,'double') |
---|
| 41 | symb_p = num2str(p); |
---|
| 42 | else |
---|
| 43 | LinearVariables = depends(p); |
---|
| 44 | x = recover(LinearVariables); |
---|
| 45 | [exponent_p,ordered_list] = exponents(p,x); |
---|
| 46 | exponent_p = full(exponent_p); |
---|
| 47 | names = cell(length(x),1); |
---|
| 48 | |
---|
| 49 | % First, some boooring stuff. we need to |
---|
| 50 | % figure out the symbolic names and connect |
---|
| 51 | % these names to YALMIPs variable indicies |
---|
| 52 | W = evalin('caller','whos'); |
---|
| 53 | for i = 1:size(W,1) |
---|
| 54 | if strcmp(W(i).class,'sdpvar') | strcmp(W(i).class,'ncvar') |
---|
| 55 | % Get the SDPVAR variable |
---|
| 56 | thevars = evalin('caller',W(i).name); |
---|
| 57 | |
---|
| 58 | % Distinguish 4 cases |
---|
| 59 | % 1: Sclalar varible x |
---|
| 60 | % 2: Vector variable x(i) |
---|
| 61 | % 3: Matrix variable x(i,j) |
---|
| 62 | % 4: Variable not really defined |
---|
| 63 | if is(thevars,'scalar') & is(thevars,'linear') & length(getvariables(thevars))==1 & isequal(getbase(thevars),[0 1]) |
---|
| 64 | index_in_p = find(ismember(LinearVariables,getvariables(thevars))); |
---|
| 65 | |
---|
| 66 | if ~isempty(index_in_p) |
---|
| 67 | already = ~isempty(names{index_in_p}); |
---|
| 68 | if already |
---|
| 69 | already = ~strfind(names{index_in_p},'internal'); |
---|
| 70 | if isempty(already) |
---|
| 71 | already = 0; |
---|
| 72 | end |
---|
| 73 | end |
---|
| 74 | else |
---|
| 75 | already = 0; |
---|
| 76 | end |
---|
| 77 | |
---|
| 78 | if ~isempty(index_in_p) & ~already |
---|
| 79 | % Case 1 |
---|
| 80 | names{index_in_p}=W(i).name; |
---|
| 81 | end |
---|
| 82 | elseif is(thevars,'lpcone') |
---|
| 83 | |
---|
| 84 | if size(thevars,1)==size(thevars,2) |
---|
| 85 | % Case 2 |
---|
| 86 | vars = getvariables(thevars); |
---|
| 87 | indicies = find(ismember(vars,LinearVariables)); |
---|
| 88 | for ii = indicies |
---|
| 89 | index_in_p = find(ismember(LinearVariables,vars(ii))); |
---|
| 90 | |
---|
| 91 | if ~isempty(index_in_p) |
---|
| 92 | already = ~isempty(names{index_in_p}); |
---|
| 93 | if already |
---|
| 94 | already = ~strfind(names{index_in_p},'internal'); |
---|
| 95 | if isempty(already) |
---|
| 96 | already = 0; |
---|
| 97 | end |
---|
| 98 | end |
---|
| 99 | else |
---|
| 100 | already = 0; |
---|
| 101 | end |
---|
| 102 | |
---|
| 103 | if ~isempty(index_in_p) & ~already |
---|
| 104 | B = reshape(getbasematrix(thevars,vars(ii)),size(thevars,1),size(thevars,2)); |
---|
| 105 | [ix,jx,kx] = find(B); |
---|
| 106 | ix=ix(1); |
---|
| 107 | jx=jx(1); |
---|
| 108 | names{index_in_p}=[W(i).name '(' num2str(ix) ',' num2str(jx) ')']; |
---|
| 109 | end |
---|
| 110 | end |
---|
| 111 | |
---|
| 112 | else |
---|
| 113 | % Case 3 |
---|
| 114 | vars = getvariables(thevars); |
---|
| 115 | indicies = find(ismember(vars,LinearVariables)); |
---|
| 116 | for ii = indicies |
---|
| 117 | index_in_p = find(ismember(LinearVariables,vars(ii))); |
---|
| 118 | |
---|
| 119 | if ~isempty(index_in_p) |
---|
| 120 | already = ~isempty(names{index_in_p}); |
---|
| 121 | if already |
---|
| 122 | already = ~strfind(names{index_in_p},'internal'); |
---|
| 123 | if isempty(already) |
---|
| 124 | already = 0; |
---|
| 125 | end |
---|
| 126 | end |
---|
| 127 | else |
---|
| 128 | already = 0; |
---|
| 129 | end |
---|
| 130 | |
---|
| 131 | if ~isempty(index_in_p) & ~already |
---|
| 132 | names{index_in_p}=[W(i).name '(' num2str(ii) ')']; |
---|
| 133 | end |
---|
| 134 | end |
---|
| 135 | end |
---|
| 136 | |
---|
| 137 | elseif is(thevars,'sdpcone') |
---|
| 138 | % Case 3 |
---|
| 139 | vars = getvariables(thevars); |
---|
| 140 | indicies = find(ismember(vars,LinearVariables)); |
---|
| 141 | for ii = indicies |
---|
| 142 | index_in_p = find(ismember(LinearVariables,vars(ii))); |
---|
| 143 | if ~isempty(index_in_p) |
---|
| 144 | already = ~isempty(names{index_in_p}); |
---|
| 145 | if already |
---|
| 146 | already = ~strfind(names{index_in_p},'internal'); |
---|
| 147 | end |
---|
| 148 | else |
---|
| 149 | already = 0; |
---|
| 150 | end |
---|
| 151 | |
---|
| 152 | if ~isempty(index_in_p) & ~already |
---|
| 153 | B = reshape(getbasematrix(thevars,vars(ii)),size(thevars,1),size(thevars,2)); |
---|
| 154 | [ix,jx,kx] = find(B); |
---|
| 155 | ix=ix(1); |
---|
| 156 | jx=jx(1); |
---|
| 157 | names{index_in_p}=[W(i).name '(' num2str(ix) ',' num2str(jx) ')']; |
---|
| 158 | end |
---|
| 159 | end |
---|
| 160 | |
---|
| 161 | else |
---|
| 162 | % Case 4 |
---|
| 163 | vars = getvariables(thevars); |
---|
| 164 | indicies = find(ismember(vars,LinearVariables)); |
---|
| 165 | |
---|
| 166 | for i = indicies |
---|
| 167 | index_in_p = find(ismember(LinearVariables,vars(i))); |
---|
| 168 | if ~isempty(index_in_p) & isempty(names{index_in_p}) |
---|
| 169 | names{index_in_p}=['internal(' num2str(vars(i)) ')']; |
---|
| 170 | end |
---|
| 171 | end |
---|
| 172 | |
---|
| 173 | end |
---|
| 174 | end |
---|
| 175 | end |
---|
| 176 | |
---|
| 177 | % Okay, now got all the symbolic names compiled. |
---|
| 178 | % Time to construct the expression |
---|
| 179 | |
---|
| 180 | % The code below is also a bit fucked up at the moment, due to |
---|
| 181 | % the experimental code with noncommuting stuff |
---|
| 182 | |
---|
| 183 | % Remove 0 constant |
---|
| 184 | symb_p = ''; |
---|
| 185 | if size(ordered_list,1)>0 |
---|
| 186 | nummonoms = size(ordered_list,1); |
---|
| 187 | if full(getbasematrix(p,0)) ~= 0 |
---|
| 188 | symb_p = num2str(full(getbasematrix(p,0))); |
---|
| 189 | end |
---|
| 190 | elseif all(exponent_p(1,:)==0) |
---|
| 191 | symb_p = num2str(full(getbasematrix(p,0))); |
---|
| 192 | exponent_p = exponent_p(2:end,:); |
---|
| 193 | nummonoms = size(exponent_p,1); |
---|
| 194 | else |
---|
| 195 | nummonoms = size(exponent_p,1); |
---|
| 196 | end |
---|
| 197 | |
---|
| 198 | % Loop through all monomial terms |
---|
| 199 | for i = 1:nummonoms |
---|
| 200 | coeff = full(getbasematrixwithoutcheck(p,i)); |
---|
| 201 | switch coeff |
---|
| 202 | case 1 |
---|
| 203 | coeff='+'; |
---|
| 204 | case -1 |
---|
| 205 | coeff = '-'; |
---|
| 206 | otherwise |
---|
| 207 | if isreal(coeff) |
---|
| 208 | if coeff >0 |
---|
| 209 | coeff = ['+' num2str2(coeff)]; |
---|
| 210 | else |
---|
| 211 | coeff=[num2str2(coeff)]; |
---|
| 212 | end |
---|
| 213 | else |
---|
| 214 | coeff = ['+' '(' num2str2(coeff) ')' ]; |
---|
| 215 | end |
---|
| 216 | end |
---|
| 217 | if isempty(ordered_list) |
---|
| 218 | symb_p = [symb_p coeff symbmonom(names,exponent_p(i,:))]; |
---|
| 219 | else |
---|
| 220 | symb_p = [symb_p coeff symbmonom_noncommuting(names,ordered_list(i,:))]; |
---|
| 221 | end |
---|
| 222 | end |
---|
| 223 | % Clean up some left overs, lazy coding... |
---|
| 224 | symb_p = strrep(symb_p,'+*','+'); |
---|
| 225 | symb_p = strrep(symb_p,'-*','-'); |
---|
| 226 | if symb_p(1)=='+' |
---|
| 227 | symb_p = symb_p(2:end); |
---|
| 228 | end |
---|
| 229 | if symb_p(1)=='*' |
---|
| 230 | symb_p = symb_p(2:end); |
---|
| 231 | end |
---|
| 232 | end |
---|
| 233 | symb_pvec{pi,pj} = symb_p; |
---|
| 234 | end |
---|
| 235 | end |
---|
| 236 | |
---|
| 237 | if prod(size(symb_pvec))==1 & nargout==0 |
---|
| 238 | display(symb_pvec{1,1}); |
---|
| 239 | clear symb_pvec |
---|
| 240 | end |
---|
| 241 | |
---|
| 242 | function s = symbmonom(names,monom) |
---|
| 243 | s = ''; |
---|
| 244 | for j = 1:length(monom) |
---|
| 245 | if abs( monom(j))>0 |
---|
| 246 | if isempty(names{j}) |
---|
| 247 | names{j} = ['internal(' num2str(j) ')']; |
---|
| 248 | end |
---|
| 249 | s = [s '*' names{j}]; |
---|
| 250 | if monom(j)~=1 |
---|
| 251 | s = [s '^' num2str(monom(j))]; |
---|
| 252 | end |
---|
| 253 | end |
---|
| 254 | end |
---|
| 255 | |
---|
| 256 | function s = symbmonom_noncommuting(names,monom) |
---|
| 257 | s = ''; |
---|
| 258 | j = 1; |
---|
| 259 | while j <= length(monom) |
---|
| 260 | if abs( monom(j))>0 |
---|
| 261 | if isempty(names{monom(j)}) |
---|
| 262 | names{monom(j)} = ['internal(' num2str(j) ')']; |
---|
| 263 | end |
---|
| 264 | s = [s '*' names{monom(j)}]; |
---|
| 265 | power = 1; |
---|
| 266 | k = j; |
---|
| 267 | while j<length(monom) & monom(j) == monom(j+1) |
---|
| 268 | power = power + 1; |
---|
| 269 | j = j + 1; |
---|
| 270 | %if j == (length(monom)-1) |
---|
| 271 | % j = 5; |
---|
| 272 | %end |
---|
| 273 | end |
---|
| 274 | if power~=1 |
---|
| 275 | s = [s '^' num2str(power)]; |
---|
| 276 | end |
---|
| 277 | end |
---|
| 278 | j = j + 1; |
---|
| 279 | end |
---|
| 280 | |
---|
| 281 | function s = num2str2(x) |
---|
| 282 | s = num2str(full(x)); |
---|
| 283 | if isequal(s,'1') |
---|
| 284 | s = ''; |
---|
| 285 | end |
---|
| 286 | if isequal(s,'-1') |
---|
| 287 | s = '-'; |
---|
| 288 | end |
---|
| 289 | |
---|