Last change
on this file since 4 was
4,
checked in by zhangj, 10 years ago
|
Initial import--MML version from SOLEIL@2013
|
File size:
1.4 KB
|
Line | |
---|
1 | function sp=power(sp,n) |
---|
2 | % sympoly/power: raise a sympoly (elementwise) to a scalar power |
---|
3 | % usage: sp=sp.^n; |
---|
4 | % |
---|
5 | % arguments: |
---|
6 | % sp - sympoly object |
---|
7 | % n - scalar - exponent of the sympoly, need not be an integer |
---|
8 | |
---|
9 | if ~isa(sp,'sympoly') |
---|
10 | error 'sp must be a sympoly' |
---|
11 | end |
---|
12 | |
---|
13 | if ~isnumeric(n) || (length(n) > 1) |
---|
14 | error 'n must be a scalar numeric variable' |
---|
15 | end |
---|
16 | |
---|
17 | % for an array, raise the individual elements |
---|
18 | % to the n'th power |
---|
19 | k=numel(sp); |
---|
20 | if k>1 |
---|
21 | % its an array. Raise each element to the nth power |
---|
22 | for i=1:k |
---|
23 | sp(i)=sp(i).^n; |
---|
24 | end |
---|
25 | else |
---|
26 | % a scalar sympoly. |
---|
27 | |
---|
28 | % If n is a scalar, non-negative integer, then |
---|
29 | % we will compute the power by repeated multiplies. |
---|
30 | % If it is fractional, then we can only do this |
---|
31 | % for single term sympolys. |
---|
32 | |
---|
33 | if n == 0 |
---|
34 | % The zero'th power is 1 |
---|
35 | sp = sympoly(1); |
---|
36 | |
---|
37 | elseif n==1 |
---|
38 | % n == 1 is a no-op. |
---|
39 | |
---|
40 | elseif (rem(n,1)==0) && (n>0) |
---|
41 | % A positive integer exponent. Use multiplication |
---|
42 | |
---|
43 | % loop on k |
---|
44 | sp1=sp; |
---|
45 | k=1; |
---|
46 | while (2*k)<=n |
---|
47 | % square until near k |
---|
48 | sp=sp.*sp; |
---|
49 | k=2*k; |
---|
50 | end |
---|
51 | |
---|
52 | if k<n |
---|
53 | for i=(k+1):n |
---|
54 | sp=sp.*sp1; |
---|
55 | end |
---|
56 | end |
---|
57 | |
---|
58 | else |
---|
59 | % it must be a fractional or negative exponent |
---|
60 | if length(sp.Coefficient)>1 |
---|
61 | error 'Cannot raise a multinomial sympoly to a fractional or negative power.' |
---|
62 | end |
---|
63 | |
---|
64 | % raise a single term to the nth power |
---|
65 | sp.Coefficient=sp.Coefficient.^n; |
---|
66 | sp.Exponent = sp.Exponent*n; |
---|
67 | |
---|
68 | end |
---|
69 | end |
---|
70 | |
---|
71 | |
---|
72 | |
---|
Note: See
TracBrowser
for help on using the repository browser.