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: sym.m,v 1.1 2005/02/22 16:50:11 johanl Exp $ |
---|
30 | allnames = {}; |
---|
31 | for pi = 1:size(pvec,1) |
---|
32 | for pj = 1:size(pvec,2) |
---|
33 | Y.type = '()'; |
---|
34 | Y.subs = [{pi} {pj}]; |
---|
35 | p = subsref(pvec,Y); |
---|
36 | % p = pvec(pi,pj); |
---|
37 | |
---|
38 | if isa(p,'double') |
---|
39 | symb_p = num2str(p); |
---|
40 | else |
---|
41 | LinearVariables = depends(p); |
---|
42 | x = recover(LinearVariables); |
---|
43 | exponent_p = full(exponents(p,x)); |
---|
44 | names = cell(length(x),1); |
---|
45 | for i = 1:length(names) |
---|
46 | names{i} = ['x' num2str(LinearVariables(i))]; |
---|
47 | allnames{end+1} = names{i}; |
---|
48 | end |
---|
49 | |
---|
50 | symb_p = ''; |
---|
51 | if all(exponent_p(1,:)==0) |
---|
52 | symb_p = num2str(full(getbasematrix(p,0))); |
---|
53 | exponent_p = exponent_p(2:end,:); |
---|
54 | end |
---|
55 | |
---|
56 | for i = 1:size(exponent_p,1) |
---|
57 | coeff = full(getbasematrixwithoutcheck(p,i)); |
---|
58 | switch coeff |
---|
59 | case 1 |
---|
60 | coeff='+'; |
---|
61 | case -1 |
---|
62 | coeff = '-'; |
---|
63 | otherwise |
---|
64 | if isreal(coeff) |
---|
65 | if coeff >0 |
---|
66 | coeff = ['+' num2str2(coeff)]; |
---|
67 | else |
---|
68 | coeff=[num2str2(coeff)]; |
---|
69 | end |
---|
70 | else |
---|
71 | coeff = ['+' '(' num2str2(coeff) ')' ]; |
---|
72 | end |
---|
73 | end |
---|
74 | symb_p = [symb_p coeff symbmonom(names,exponent_p(i,:))]; |
---|
75 | end |
---|
76 | if symb_p(1)=='+' |
---|
77 | symb_p = symb_p(2:end); |
---|
78 | end |
---|
79 | end |
---|
80 | symb_pvec{pi,pj} = symb_p; |
---|
81 | end |
---|
82 | end |
---|
83 | allnames = unique(allnames); |
---|
84 | for i = 1:length(allnames) |
---|
85 | evalin('caller',['syms ' allnames{i}]); |
---|
86 | end |
---|
87 | |
---|
88 | |
---|
89 | S = ''; |
---|
90 | for pi = 1:size(pvec,1) |
---|
91 | ss = ''; |
---|
92 | for pj = 1:size(pvec,2) |
---|
93 | ss = [ss ' ' symb_pvec{pi,pj} ',']; |
---|
94 | end |
---|
95 | S = [S ss ';']; |
---|
96 | end |
---|
97 | S = ['[' S ']'] ; |
---|
98 | symb_pvec = evalin('caller',S); |
---|
99 | |
---|
100 | |
---|
101 | function s = symbmonom(names,monom) |
---|
102 | s = ''; |
---|
103 | for j = 1:length(monom) |
---|
104 | if abs( monom(j))>0 |
---|
105 | s = [s names{j}]; |
---|
106 | if monom(j)~=1 |
---|
107 | s = [s '^' num2str(monom(j))]; |
---|
108 | end |
---|
109 | s =[s '*']; |
---|
110 | end |
---|
111 | |
---|
112 | end |
---|
113 | if isequal(s(end),'*') |
---|
114 | s = s(1:end-1); |
---|
115 | end |
---|
116 | |
---|
117 | function s = num2str2(x) |
---|
118 | s = num2str(full(x)); |
---|
119 | if isequal(s,'1') |
---|
120 | s = ''; |
---|
121 | end |
---|
122 | if isequal(s,'-1') |
---|
123 | s = '-'; |
---|
124 | end |
---|
125 | |
---|
126 | |
---|