[37] | 1 | function deg=degree(p,y,e) |
---|
| 2 | %DEGREE Polynomial degree |
---|
| 3 | % |
---|
| 4 | % DEG = DEGREE(p,x,e) |
---|
| 5 | % |
---|
| 6 | % p : SDPVAR object. |
---|
| 7 | % x : Degree w.r.t linear SDPVAR objects, can be []. |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | % e : If e=1, returns degree of each element in p |
---|
| 11 | % |
---|
| 12 | % Examples |
---|
| 13 | % x1 = sdpvar(1,1);x2 = sdpvar(1,1); |
---|
| 14 | % p = [x1;x1*x2+x2^2]; |
---|
| 15 | % |
---|
| 16 | % degree(p) returns 2 |
---|
| 17 | % |
---|
| 18 | % degree(p,x1) returns 1 |
---|
| 19 | % |
---|
| 20 | % degree(p,[x1 x2]) returns [1 2] |
---|
| 21 | % |
---|
| 22 | % degree(p,[x1 x2],1) returns [1 0;1 2] |
---|
| 23 | % |
---|
| 24 | % degree(p,[],1) returns [1;3] |
---|
| 25 | |
---|
| 26 | % Author Johan Löfberg |
---|
| 27 | % $Id: degree.m,v 1.2 2004/07/01 11:17:09 johanl Exp $ |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | if isa(p,'double') |
---|
| 31 | if nargin==1 |
---|
| 32 | deg = 0; |
---|
| 33 | else |
---|
| 34 | deg = zeros(1,length(y)); |
---|
| 35 | end |
---|
| 36 | return |
---|
| 37 | end |
---|
| 38 | |
---|
| 39 | if nargin<2 |
---|
| 40 | y = recover(depends(p)); |
---|
| 41 | end |
---|
| 42 | |
---|
| 43 | if nargin<3 | (nargin==3 & e==0) |
---|
| 44 | exponent_p = exponents(p,y); |
---|
| 45 | switch nargin |
---|
| 46 | case 1 |
---|
| 47 | deg = full(max(sum(exponent_p,2))); |
---|
| 48 | case {2,3} |
---|
| 49 | deg = full(max(exponent_p,[],1)); |
---|
| 50 | otherwise |
---|
| 51 | error('Too many arguments. Wadda ya mean?') |
---|
| 52 | end |
---|
| 53 | else |
---|
| 54 | p = p(:); |
---|
| 55 | if isempty(y) |
---|
| 56 | yy = recover(depends(p)); |
---|
| 57 | else |
---|
| 58 | yy = y; |
---|
| 59 | end |
---|
| 60 | |
---|
| 61 | for i = 1:length(p) |
---|
| 62 | z.type = '()'; |
---|
| 63 | z.subs{1} = i; |
---|
| 64 | exponent_p = exponents(subsref(p,z),yy); |
---|
| 65 | switch nargin |
---|
| 66 | case 1 |
---|
| 67 | deg(i,:) = full(max(sum(exponent_p,2))); |
---|
| 68 | case {2,3} |
---|
| 69 | deg(i,:) = full(max(exponent_p,[],1)); |
---|
| 70 | otherwise |
---|
| 71 | error('Too many arguments. Wadda ya mean?') |
---|
| 72 | end |
---|
| 73 | end |
---|
| 74 | if isempty(y) |
---|
| 75 | deg = sum(deg,2); |
---|
| 76 | end |
---|
| 77 | end |
---|