1 | function y = power(x,d) |
---|
2 | %POWER (overloaded) |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: power.m,v 1.10 2006/07/26 20:17:58 joloef Exp $ |
---|
6 | |
---|
7 | |
---|
8 | % Sanity check |
---|
9 | if prod(size(x))==1 & (prod(size(d))>1) |
---|
10 | x = x.*ones(size(d)); |
---|
11 | end |
---|
12 | if prod(size(d))>1 |
---|
13 | if any(size(d)~=size(x)) |
---|
14 | error('Matrix dimensions must agree.'); |
---|
15 | end |
---|
16 | else |
---|
17 | d = ones(x.dim(1),x.dim(2))*d; |
---|
18 | end |
---|
19 | |
---|
20 | % Trivial cases |
---|
21 | if all(all(d==0)) |
---|
22 | if x.dim(1)~=x.dim(2) |
---|
23 | error('Matrix must be square.') |
---|
24 | end |
---|
25 | y = eye(x.dim(1),x.dim(2)).^0; |
---|
26 | return |
---|
27 | end |
---|
28 | if all(all(d==1)) |
---|
29 | y = x; |
---|
30 | return |
---|
31 | end |
---|
32 | |
---|
33 | % Fractional, negative or different powers are |
---|
34 | % treated less efficiently using simple code. |
---|
35 | fractional = any(any((ceil(d)-d>0))); |
---|
36 | negative = any(any(d<0)); |
---|
37 | different = ~all(all(d==d(1))); |
---|
38 | if fractional | negative | different |
---|
39 | if x.dim(1)>1 | x.dim(2)>1 |
---|
40 | [n,m] = size(x); |
---|
41 | y = []; |
---|
42 | for i = 1:n % FIX : Vectorize! |
---|
43 | temp = []; |
---|
44 | for j = 1:m |
---|
45 | temp = [temp extsubsref(x,i,j).^d(i,j)]; |
---|
46 | end |
---|
47 | y = [y;temp]; |
---|
48 | end |
---|
49 | return |
---|
50 | else |
---|
51 | base = getbase(x); |
---|
52 | if isequal(base,[0 1]) |
---|
53 | mt = yalmip('monomtable'); |
---|
54 | var = getvariables(x); |
---|
55 | previous_var = find((mt(:,var)==d) & (sum(mt~=0,2)==1)); |
---|
56 | if isempty(previous_var) |
---|
57 | mt(end+1,:) = mt(getvariables(x),:)*d; |
---|
58 | yalmip('setmonomtable',mt); |
---|
59 | y = recover(size(mt,1)); |
---|
60 | else |
---|
61 | y = recover(previous_var); |
---|
62 | end |
---|
63 | elseif (size(base,2) == 2) & base(1)==0 |
---|
64 | % Something like a*t^-d |
---|
65 | y = base(2)^d*recover(getvariables(x))^d; |
---|
66 | else |
---|
67 | error('Only unit scalars can have negative or non-integer powers.'); |
---|
68 | end |
---|
69 | end |
---|
70 | return |
---|
71 | end |
---|
72 | |
---|
73 | % Back to scalar power... |
---|
74 | d = d(1,1); |
---|
75 | if x.dim(1)>1 | x.dim(2)>1 |
---|
76 | switch d |
---|
77 | case 0 |
---|
78 | y = 1; |
---|
79 | case 1 |
---|
80 | y = x; |
---|
81 | otherwise |
---|
82 | y = x.*power(x,d-1); |
---|
83 | end |
---|
84 | else |
---|
85 | base = getbase(x); |
---|
86 | if isequal(base,[0 1]) |
---|
87 | mt = yalmip('monomtable'); |
---|
88 | var = getvariables(x); |
---|
89 | previous_var = find((mt(:,var)==d) & (sum(mt~=0,2)==1)); |
---|
90 | if isempty(previous_var) |
---|
91 | mt(end+1,:) = mt(getvariables(x),:)*d; |
---|
92 | yalmip('setmonomtable',mt); |
---|
93 | y = x; |
---|
94 | y.lmi_variables = size(mt,1); |
---|
95 | else |
---|
96 | y = x; |
---|
97 | y.lmi_variables = previous_var; |
---|
98 | end |
---|
99 | else |
---|
100 | switch d |
---|
101 | case 0 |
---|
102 | y = 1; |
---|
103 | case 1 |
---|
104 | y = x; |
---|
105 | otherwise |
---|
106 | y = x.*power(x,d-1); |
---|
107 | end |
---|
108 | end |
---|
109 | end |
---|