1 | function [Q,c,f,x,info] = vecquaddecomp(p,z) |
---|
2 | %QUADDECOMP Internal function to decompose quadratic expression |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: vecquaddecomp.m,v 1.2 2006/08/10 14:09:06 joloef Exp $ |
---|
6 | |
---|
7 | [n,m]=size(p); |
---|
8 | info = 0; |
---|
9 | |
---|
10 | Q = cell(n*m,1); |
---|
11 | c = cell(n*m,1); |
---|
12 | f = cell(n*m,1); |
---|
13 | |
---|
14 | % Involved in polynomial expression |
---|
15 | [mt,variabletype] = yalmip('monomtable'); |
---|
16 | x_lin = getvariables(p); |
---|
17 | x_var = find(any(mt(x_lin,:),1)); |
---|
18 | if nargin==2 |
---|
19 | x_var = union(x_var,depends(z)); |
---|
20 | end |
---|
21 | x = recover(x_var); |
---|
22 | |
---|
23 | |
---|
24 | basis = getbase(p); |
---|
25 | vars = getvariables(p); |
---|
26 | mm = mt; |
---|
27 | for index = 1:n*m |
---|
28 | mt = mm; |
---|
29 | base = basis(index,:); |
---|
30 | % all(variabletype(vars(find(base(2:end)))) == 0) |
---|
31 | if all(variabletype(vars(find(base(2:end)))) == 0)% is(p(index),'linear') |
---|
32 | n = length(x); |
---|
33 | Q{index} = spalloc(n,n,0); |
---|
34 | fc = basis(index,:); |
---|
35 | f{index} = fc(1); |
---|
36 | c{index} = zeros(length(x),1); |
---|
37 | for i = 1:length(vars) |
---|
38 | c{index}(find(vars(i)==x_var)) = fc(1+i); |
---|
39 | end |
---|
40 | else |
---|
41 | % degrees = sum(mt(x_lin,:),2); |
---|
42 | if all(variabletype(vars(find(base(2:end)))) <= 2)%all(degrees<=2) |
---|
43 | Qtemp = spalloc(length(x),length(x),2*nnz(base)); |
---|
44 | ctemp = zeros(length(x),1); |
---|
45 | |
---|
46 | if nnz(base(1))==0 |
---|
47 | f{index} = 0; |
---|
48 | base = base(2:end); |
---|
49 | else |
---|
50 | f{index} = base(1); |
---|
51 | base = base(2:end); |
---|
52 | end |
---|
53 | |
---|
54 | mt = mt(x_lin,x_var); |
---|
55 | |
---|
56 | [jj,ii,kk] = find(mt');ii = [ii(:) ;0]; |
---|
57 | top = 1; |
---|
58 | for i = 1:length(x_lin) |
---|
59 | if ii(top) == ii(top+1) |
---|
60 | j = [jj(top) jj(top+1)]; |
---|
61 | top = top + 2; |
---|
62 | else |
---|
63 | j = [jj(top)]; |
---|
64 | top = top + 1; |
---|
65 | end |
---|
66 | if length(j)==1 % One variable |
---|
67 | if kk(top-1)==1 |
---|
68 | ctemp(j)=base(i); |
---|
69 | else |
---|
70 | Qtemp(j,j)=Qtemp(j,j) + base(i)/2; |
---|
71 | end |
---|
72 | else |
---|
73 | Qtemp(j(1),j(2))=Qtemp(j(1),j(2)) + base(i)/2; |
---|
74 | end |
---|
75 | end |
---|
76 | Q{index} = Qtemp+Qtemp'; |
---|
77 | c{index} = ctemp; |
---|
78 | else |
---|
79 | info = 1; |
---|
80 | x = []; |
---|
81 | end |
---|
82 | end |
---|
83 | end |
---|
84 | |
---|