[37] | 1 | function y = mpower(x,d) |
---|
| 2 | %MPOWER (overloaded) |
---|
| 3 | |
---|
| 4 | % Author Johan Löfberg |
---|
| 5 | % $Id: mpower.m,v 1.18 2006/09/21 14:31:06 joloef Exp $ |
---|
| 6 | |
---|
| 7 | %Sanity check |
---|
| 8 | if prod(size(d))>1 |
---|
| 9 | error('The power must be scalar.'); |
---|
| 10 | end |
---|
| 11 | if x.dim(1)~=x.dim(2) |
---|
| 12 | error('Matrix must be square.') |
---|
| 13 | end |
---|
| 14 | |
---|
| 15 | % Trivial cases |
---|
| 16 | if d==0 |
---|
| 17 | y = eye(x.dim(1),x.dim(2))^0; |
---|
| 18 | return |
---|
| 19 | end |
---|
| 20 | if d==1 |
---|
| 21 | y = x; |
---|
| 22 | return |
---|
| 23 | end |
---|
| 24 | |
---|
| 25 | % Fractional and negative powers |
---|
| 26 | if (ceil(d)-d>0) | (d<0) |
---|
| 27 | if x.dim(1)>1 | x.dim(2)>1 |
---|
| 28 | error('Only scalars can have negative or non-integer powers'); |
---|
| 29 | else |
---|
| 30 | base = getbase(x); |
---|
| 31 | if isequal(base,sparse([0 1])) % Simple unit scalar |
---|
| 32 | [mt,variabletype] = yalmip('monomtable'); |
---|
| 33 | var = getvariables(x); |
---|
| 34 | hash = randn(size(mt,2),1); |
---|
| 35 | hashM = mt*hash; |
---|
| 36 | hashV = (mt(var,:)*d)*hash; |
---|
| 37 | previous_var = find(abs(hashM - hashV) < 1e-20); |
---|
| 38 | if isempty(previous_var) |
---|
| 39 | newmt = mt(getvariables(x),:)*d; |
---|
| 40 | mt(end+1,:) = newmt; |
---|
| 41 | yalmip('setmonomtable',mt,[variabletype newvariabletypegen(newmt)]); |
---|
| 42 | y = recover(size(mt,1)); |
---|
| 43 | else |
---|
| 44 | y = recover(previous_var); |
---|
| 45 | end |
---|
| 46 | elseif (size(base,2) == 2) & base(1)==0 |
---|
| 47 | % Something like a*t^-d |
---|
| 48 | y = base(2)^d*recover(getvariables(x))^d; |
---|
| 49 | else |
---|
| 50 | % Bummer, something more complex, add an internal equality constraint |
---|
| 51 | y = (yalmip('addextendedvariable','mpower',x))^d; |
---|
| 52 | end |
---|
| 53 | end |
---|
| 54 | return |
---|
| 55 | end |
---|
| 56 | |
---|
| 57 | % Integer power of matrix |
---|
| 58 | if x.dim(1)>1 | x.dim(2)>1 |
---|
| 59 | switch d |
---|
| 60 | case 0 |
---|
| 61 | y = 1; |
---|
| 62 | case 1 |
---|
| 63 | y = x; |
---|
| 64 | otherwise |
---|
| 65 | y = x*mpower(x,d-1); |
---|
| 66 | end |
---|
| 67 | else %Integer power of scalar |
---|
| 68 | |
---|
| 69 | base = x.basis; |
---|
| 70 | if isequal(base,[0 1]) % Unit scalar can be done fast |
---|
| 71 | [mt,variabletype] = yalmip('monomtable'); |
---|
| 72 | %var = getvariables(x); |
---|
| 73 | var = x.lmi_variables; |
---|
| 74 | possible = find(mt(:,var)); |
---|
| 75 | if length(possible)==1 |
---|
| 76 | % Even faster, we don't need to search, cannot have been |
---|
| 77 | % definded earlier, since only the linear terms is in monom |
---|
| 78 | % table |
---|
| 79 | newmt = mt(var,:)*d; |
---|
| 80 | mt = [mt;newmt]; |
---|
| 81 | yalmip('setmonomtable',mt,[variabletype newvariabletypegen(newmt)]); |
---|
| 82 | y = x; |
---|
| 83 | y.lmi_variables = size(mt,1); |
---|
| 84 | else |
---|
| 85 | hash = randn(size(mt,2),1); |
---|
| 86 | mt_hash = mt*hash; |
---|
| 87 | mt_hash = mt_hash(possible); |
---|
| 88 | previous_var = findhash(mt_hash , (d*mt(var,:))*hash); |
---|
| 89 | if isempty(previous_var) |
---|
| 90 | newmt = mt(var,:)*d; |
---|
| 91 | % mt(end+1,:) = newmt; |
---|
| 92 | mt = [mt;newmt]; |
---|
| 93 | yalmip('setmonomtable',mt,[variabletype newvariabletypegen(newmt)]); |
---|
| 94 | y = x; |
---|
| 95 | y.lmi_variables = size(mt,1); |
---|
| 96 | else |
---|
| 97 | previous_var = possible(previous_var); |
---|
| 98 | y = x; |
---|
| 99 | y.lmi_variables = previous_var; |
---|
| 100 | end |
---|
| 101 | end |
---|
| 102 | else % General scalar |
---|
| 103 | switch d |
---|
| 104 | case 0 |
---|
| 105 | y = 1; |
---|
| 106 | case 1 |
---|
| 107 | y = x; |
---|
| 108 | otherwise |
---|
| 109 | y = x*mpower(x,d-1); |
---|
| 110 | end |
---|
| 111 | end |
---|
| 112 | end |
---|
| 113 | |
---|
| 114 | function newvariabletype = newvariabletypegen(newmt) |
---|
| 115 | newvariabletype = spalloc(size(newmt,1),1,0)'; |
---|
| 116 | nonlinear = ~(sum(newmt,2)==1 & sum(newmt~=0,2)==1); |
---|
| 117 | if ~isempty(nonlinear) |
---|
| 118 | newvariabletype(nonlinear) = 3; |
---|
| 119 | quadratic = sum(newmt,2)==2; |
---|
| 120 | newvariabletype(quadratic) = 2; |
---|
| 121 | bilinear = max(newmt,[],2)<=1; |
---|
| 122 | newvariabletype(bilinear & quadratic) = 1; |
---|
| 123 | sigmonial = any(0>newmt,2) | any(newmt-fix(newmt),2); |
---|
| 124 | newvariabletype(sigmonial) = 4; |
---|
| 125 | end |
---|
| 126 | |
---|
| 127 | |
---|