1 | function symb_pvec = amplexpr(pvec) |
---|
2 | %AMPLEXPR Converts SDPVAR variable to AMPL string |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: amplexpr.m,v 1.3 2005/02/10 16:43:32 johanl Exp $ |
---|
6 | |
---|
7 | for pi = 1:size(pvec,1) |
---|
8 | for pj = 1:size(pvec,2) |
---|
9 | p = pvec(pi,pj); |
---|
10 | |
---|
11 | if isa(p,'double') |
---|
12 | symb_p = num2str(p); |
---|
13 | else |
---|
14 | LinearVariables = depends(p); |
---|
15 | x = recover(LinearVariables); |
---|
16 | exponent_p = full(exponents(p,x)); |
---|
17 | names = cell(length(LinearVariables),1); |
---|
18 | for i = 1:length(LinearVariables) |
---|
19 | names{i}=['x[' num2str(LinearVariables(i)) ']']; |
---|
20 | end |
---|
21 | |
---|
22 | symb_p = ''; |
---|
23 | if all(exponent_p(1,:)==0) |
---|
24 | symb_p = num2str(getbasematrix(p,0)); |
---|
25 | exponent_p = exponent_p(2:end,:); |
---|
26 | end |
---|
27 | |
---|
28 | for i = 1:size(exponent_p,1) |
---|
29 | coeff = getbasematrixwithoutcheck(p,i); |
---|
30 | switch full(coeff) |
---|
31 | case 1 |
---|
32 | coeff='+'; |
---|
33 | case -1 |
---|
34 | coeff = '-'; |
---|
35 | otherwise |
---|
36 | if coeff >0 |
---|
37 | coeff = ['+' num2str2(coeff)]; |
---|
38 | else |
---|
39 | coeff=num2str2(coeff); |
---|
40 | end |
---|
41 | end |
---|
42 | if strcmp(symb_p,'') & (strcmp(coeff,'+') | strcmp(coeff,'-')) |
---|
43 | symb_p = [symb_p coeff symbmonom(names,exponent_p(i,:))]; |
---|
44 | else |
---|
45 | symb_p = [symb_p coeff '*' symbmonom(names,exponent_p(i,:))]; |
---|
46 | end |
---|
47 | end |
---|
48 | if symb_p(1)=='+' |
---|
49 | symb_p = symb_p(2:end); |
---|
50 | end |
---|
51 | end |
---|
52 | |
---|
53 | symb_p = strrep(symb_p,'+*','+'); |
---|
54 | symb_p = strrep(symb_p,'-*','-'); |
---|
55 | symb_pvec{pi,pj} = symb_p; |
---|
56 | end |
---|
57 | end |
---|
58 | |
---|
59 | function s = symbmonom(names,monom) |
---|
60 | s = ''; |
---|
61 | for j = 1:length(monom) |
---|
62 | if monom(j)>0 |
---|
63 | if strcmp(s,'') |
---|
64 | s = [s names{j}]; |
---|
65 | else |
---|
66 | s = [s '*' names{j}]; |
---|
67 | end |
---|
68 | end |
---|
69 | if monom(j)>1 |
---|
70 | s = [s '^' num2str(monom(j))]; |
---|
71 | end |
---|
72 | end |
---|
73 | |
---|
74 | function s = num2str2(x) |
---|
75 | s = num2str(x); |
---|
76 | if isequal(s,'1') |
---|
77 | s = ''; |
---|
78 | end |
---|
79 | if isequal(s,'-1') |
---|
80 | s = '-'; |
---|
81 | end |
---|