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.0 KB
|
Line | |
---|
1 | function sump=sum(p,dim) |
---|
2 | % sympoly/sum: sum a sympoly array along a given dimension |
---|
3 | % usage: sump=sum(p); |
---|
4 | % usage: sump=sum(p,dim); |
---|
5 | % |
---|
6 | % arguments: |
---|
7 | % p - sympoly array object |
---|
8 | % dim - (OPTIONAL) dimension to sum over |
---|
9 | % DEFAULT ==1, unless sp is a row vector |
---|
10 | % |
---|
11 | % sump - sympoly object containing the sum reduced array |
---|
12 | |
---|
13 | s = size(p); |
---|
14 | np = length(s); |
---|
15 | |
---|
16 | % default for dim is 1, UNLESS p is a row vector. |
---|
17 | if (nargin<2) || isempty(dim) |
---|
18 | if (s(1) == 1) && (np==2) |
---|
19 | % a row vector |
---|
20 | dim = 2; |
---|
21 | else |
---|
22 | % any other shape array |
---|
23 | dim = 1; |
---|
24 | end |
---|
25 | end |
---|
26 | |
---|
27 | % for dim == 1 or dim == 2, do the sum using a dot product |
---|
28 | if (dim == 1) && (np == 2) |
---|
29 | % sum down rows |
---|
30 | sump = ones(1,s(1))*p; |
---|
31 | |
---|
32 | elseif (dim == 2) && (np == 2) |
---|
33 | % sum across columns |
---|
34 | sump = p*ones(s(2),1); |
---|
35 | |
---|
36 | else |
---|
37 | % its an n-d array |
---|
38 | ss = s; |
---|
39 | ss(dim) = 1; |
---|
40 | |
---|
41 | si = cell(1,np); |
---|
42 | for i = 1:np |
---|
43 | si{i} = 1:s(i); |
---|
44 | end |
---|
45 | |
---|
46 | if any(ss~=1) |
---|
47 | sump = repmat(sympoly(0),ss); |
---|
48 | else |
---|
49 | sump = sympoly(0); |
---|
50 | end |
---|
51 | for i = 1:s(dim) |
---|
52 | si{dim} = i; |
---|
53 | sump = sump + p(si{:}); |
---|
54 | end |
---|
55 | |
---|
56 | end |
---|
57 | |
---|
58 | |
---|
59 | |
---|
Note: See
TracBrowser
for help on using the repository browser.