[37] | 1 | function monom = recovermonoms(newton_m,x) |
---|
| 2 | %RECOVERMONOMS Internal function used in SOS programs |
---|
| 3 | |
---|
| 4 | % Author Johan Löfberg |
---|
| 5 | % $Id: recovermonoms.m,v 1.14 2006/07/26 20:17:58 joloef Exp $ |
---|
| 6 | |
---|
| 7 | if size(newton_m,1)==1 & nnz(newton_m)==0 |
---|
| 8 | monom = 1; |
---|
| 9 | return |
---|
| 10 | end |
---|
| 11 | |
---|
| 12 | [mt,oldvariabletype,mt_hash,hash] = yalmip('monomtable'); |
---|
| 13 | |
---|
| 14 | origSize = size(mt,1); |
---|
| 15 | vars = x.lmi_variables; |
---|
| 16 | |
---|
| 17 | newton_m_extended = spalloc(size(newton_m,1),size(mt,2),nnz(newton_m)); |
---|
| 18 | newton_m_extended(:,vars) = newton_m; |
---|
| 19 | newton_m_extended_hash = newton_m_extended*hash; |
---|
| 20 | |
---|
| 21 | newton_m = newton_m'; |
---|
| 22 | newton_m_extended = newton_m_extended'; |
---|
| 23 | mt = mt'; |
---|
| 24 | |
---|
| 25 | monom = x; |
---|
| 26 | %monom.n = size(newton_m,1); |
---|
| 27 | monom.dim(1) = size(newton_m,2); |
---|
| 28 | monom.dim(2) = 1; |
---|
| 29 | monom.lmi_variables = []; |
---|
| 30 | %variable_here = ones(1,size(newton_m,1)); |
---|
| 31 | %variable_here = ones(1,size(newton_m,2)); |
---|
| 32 | mt_hash = full(mt_hash); |
---|
| 33 | nvar = size(mt,2); |
---|
| 34 | new_mt_hash = []; |
---|
| 35 | new_mt = []; |
---|
| 36 | variable_here = any(newton_m,1); |
---|
| 37 | for i = find(variable_here)%1:size(newton_m,2) |
---|
| 38 | %if nnz(newton_m(:,i))==0 |
---|
| 39 | % variable_here(i) = 0; |
---|
| 40 | %else |
---|
| 41 | if isempty(mt_hash)%MESSY DUE TO BEHAVIOUR IN LINUX 6.1 |
---|
| 42 | previous_variable = []; |
---|
| 43 | else |
---|
| 44 | previous_variable = find(mt_hash == newton_m_extended_hash(i)); |
---|
| 45 | end |
---|
| 46 | if isempty(previous_variable) |
---|
| 47 | if isempty(new_mt_hash) |
---|
| 48 | previous_variable = []; |
---|
| 49 | else |
---|
| 50 | previous_variable = find(new_mt_hash == newton_m_extended_hash(i)); |
---|
| 51 | end |
---|
| 52 | if isempty(previous_variable) |
---|
| 53 | nvar = nvar + 1; |
---|
| 54 | % mt = [mt newton_m_extended(:,i)]; |
---|
| 55 | % mt_hash = [mt_hash ; newton_m_extended(:,i)'*hash]; |
---|
| 56 | new_mt = [new_mt newton_m_extended(:,i)]; |
---|
| 57 | new_mt_hash = [new_mt_hash ; newton_m_extended(:,i)'*hash]; |
---|
| 58 | |
---|
| 59 | monom.lmi_variables = [monom.lmi_variables nvar]; |
---|
| 60 | else |
---|
| 61 | monom.lmi_variables = [monom.lmi_variables size(mt,2)+previous_variable]; |
---|
| 62 | end |
---|
| 63 | else |
---|
| 64 | % try |
---|
| 65 | monom.lmi_variables = [monom.lmi_variables previous_variable]; |
---|
| 66 | % catch |
---|
| 67 | % error |
---|
| 68 | % end |
---|
| 69 | end |
---|
| 70 | % end |
---|
| 71 | end |
---|
| 72 | mt = [mt new_mt]'; |
---|
| 73 | mt_hash = [mt_hash;new_mt_hash]; |
---|
| 74 | %mt = mt'; |
---|
| 75 | |
---|
| 76 | % if append_one |
---|
| 77 | % monom.basis = [[1;spalloc(monom.dim(1),1,0)] [spalloc(1,monom.dim(1),0);speye(monom.dim(1))]]; |
---|
| 78 | % monom.dim(1) = monom.dim(1) + 1; |
---|
| 79 | % else |
---|
| 80 | % monom.basis = [spalloc(monom.dim(1),1,0) spalloc(monom.dim(1),length(find(variable_here)),0)]; |
---|
| 81 | % monom.basis = [spalloc(monom.dim(1),1+length(find(variable_here)),1+length(find(variable_here)))]; |
---|
| 82 | % monom.basis(find(variable_here),2:end) = speye(length(find(variable_here))); |
---|
| 83 | % monom.basis(find(variable_here==0),1)=1; |
---|
| 84 | |
---|
| 85 | nz = find(variable_here); |
---|
| 86 | zv = find(variable_here==0); |
---|
| 87 | i = [zv(:);nz(:)]; |
---|
| 88 | j = [ones(length(zv),1);(2:(length(nz)+1))']; |
---|
| 89 | k = ones(length(variable_here),1); |
---|
| 90 | monom.basis = sparse(i,j,k,monom.dim(1),1+length(find(variable_here))); |
---|
| 91 | |
---|
| 92 | %end |
---|
| 93 | |
---|
| 94 | % Fucked up order |
---|
| 95 | if any(diff(monom.lmi_variables)<0) |
---|
| 96 | [i,j]=sort(monom.lmi_variables); |
---|
| 97 | monom.lmi_variables = monom.lmi_variables(j); |
---|
| 98 | monom.basis(:,2:end) = monom.basis(:,j+1); |
---|
| 99 | end |
---|
| 100 | |
---|
| 101 | un_monom_vars = uniquestripped(monom.lmi_variables); |
---|
| 102 | if length(un_monom_vars)<length(monom.lmi_variables) |
---|
| 103 | [un_monom_vars,hh,jj] = unique(monom.lmi_variables); |
---|
| 104 | if length(monom.lmi_variables) ~=length(un_monom_vars) |
---|
| 105 | newmonombase = monom.basis*sparse([1 1+jj],[1 1+(1:length(jj))],ones(1,1+length(jj)))'; |
---|
| 106 | monom.basis = newmonombase; |
---|
| 107 | monom.lmi_variables = un_monom_vars; |
---|
| 108 | end |
---|
| 109 | end |
---|
| 110 | |
---|
| 111 | if size(mt,1) > origSize |
---|
| 112 | newmt = mt(origSize+1:end,:); |
---|
| 113 | newvariabletype = spalloc(size(newmt,1),1,0)'; |
---|
| 114 | nonlinear = ~(sum(newmt,2)==1 & sum(newmt~=0,2)==1); |
---|
| 115 | if ~isempty(nonlinear) |
---|
| 116 | %mt = internal_sdpvarstate.monomtable; |
---|
| 117 | newvariabletype(nonlinear) = 3; |
---|
| 118 | quadratic = sum(newmt,2)==2; |
---|
| 119 | newvariabletype(quadratic) = 2; |
---|
| 120 | bilinear = max(newmt,[],2)<=1; |
---|
| 121 | newvariabletype(bilinear & quadratic) = 1; |
---|
| 122 | sigmonial = any(0>newmt,2) | any(newmt-fix(newmt),2); |
---|
| 123 | newvariabletype(sigmonial) = 4; |
---|
| 124 | end |
---|
| 125 | yalmip('setmonomtable',mt,[oldvariabletype newvariabletype]); |
---|
| 126 | end |
---|
| 127 | |
---|
| 128 | |
---|