Mercurial > hg > octave-nkf
annotate scripts/general/accumarray.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | b0084095098e |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2007-2011 David Bateman |
2 ## Copyright (C) 2009-2010 VZLU Prague | |
6770 | 3 ## |
7016 | 4 ## This file is part of Octave. |
6770 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
6770 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6770 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
21 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{func}, @var{fillval}, @var{issparse}) |
6770 | 22 ## @deftypefnx {Function File} {} accumarray (@var{csubs}, @var{vals}, @dots{}) |
23 ## | |
7186 | 24 ## Create an array by accumulating the elements of a vector into the |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
25 ## positions defined by their subscripts. The subscripts are defined by |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
26 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each row |
6770 | 27 ## of @var{subs} corresponds to one of the values in @var{vals}. |
28 ## | |
29 ## The size of the matrix will be determined by the subscripts themselves. | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
30 ## However, if @var{sz} is defined it determines the matrix size. The length |
6770 | 31 ## of @var{sz} must correspond to the number of columns in @var{subs}. |
32 ## | |
33 ## The default action of @code{accumarray} is to sum the elements with the | |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
34 ## same subscripts. This behavior can be modified by defining the @var{func} |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
35 ## function. This should be a function or function handle that accepts a |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
36 ## column vector and returns a scalar. The result of the function should not |
6770 | 37 ## depend on the order of the subscripts. |
38 ## | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7186
diff
changeset
|
39 ## The elements of the returned array that have no subscripts associated with |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
40 ## them are set to zero. Defining @var{fillval} to some other value allows |
6770 | 41 ## these values to be defined. |
42 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8934
diff
changeset
|
43 ## By default @code{accumarray} returns a full matrix. If @var{issparse} is |
6770 | 44 ## logically true, then a sparse matrix is returned instead. |
45 ## | |
46 ## An example of the use of @code{accumarray} is: | |
47 ## | |
48 ## @example | |
49 ## @group | |
7186 | 50 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2], 101:105) |
6770 | 51 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] |
52 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] | |
53 ## @end group | |
54 ## @end example | |
10275
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
55 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
56 ## The complexity in the non-sparse case is generally O(M+N), where N is the |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
57 ## number of |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
58 ## subscripts and M is the maximum subscript (linearized in multi-dimensional |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
59 ## case). |
10275
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
60 ## If @var{func} is one of @code{@@sum} (default), @code{@@max}, @code{@@min} |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
61 ## or @code{@@(x) @{x@}}, an optimized code path is used. |
10275
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
62 ## Note that for general reduction function the interpreter overhead can play a |
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
63 ## major part and it may be more efficient to do multiple accumarray calls and |
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
64 ## compute the results in a vectorized manner. |
6770 | 65 ## @end deftypefn |
66 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 function A = accumarray (subs, vals, sz = [], func = [], fillval = [], issparse = []) |
6770 | 68 |
69 if (nargin < 2 || nargin > 6) | |
70 print_usage (); | |
71 endif | |
72 | |
7186 | 73 if (iscell (subs)) |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
10962
diff
changeset
|
74 subs = cellfun (@vec, subs, "uniformoutput", false); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
75 ndims = numel (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
76 if (ndims == 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
77 subs = subs{1}; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
78 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
79 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
80 ndims = columns (subs); |
6770 | 81 endif |
82 | |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
83 if (isempty (fillval)) |
6770 | 84 fillval = 0; |
85 endif | |
86 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
87 if (isempty (issparse)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
88 issparse = false; |
6770 | 89 endif |
7186 | 90 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
91 if (issparse) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
92 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
93 ## Sparse case. Avoid linearizing the subscripts, because it could overflow. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
94 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
95 if (fillval != 0) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
96 error ("accumarray: FILLVAL must be zero in the sparse case"); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
97 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
98 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
99 ## Ensure subscripts are a two-column matrix. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
100 if (iscell (subs)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
101 subs = [subs{:}]; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
102 endif |
6770 | 103 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
104 ## Validate dimensions. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
105 if (ndims == 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
106 subs(:,2) = 1; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
107 elseif (ndims != 2) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
108 error ("accumarray: in the sparse case, needs 1 or 2 subscripts"); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
109 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
110 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
111 if (isnumeric (vals) || islogical (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
112 vals = double (vals); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
113 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
114 error ("accumarray: in the sparse case, values must be numeric or logical"); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
115 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
116 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
117 if (! (isempty (func) || func == @sum)) |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
118 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
119 ## Reduce values. This is not needed if we're about to sum them, because |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
120 ## "sparse" can do that. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
121 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
122 ## Sort indices. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
123 [subs, idx] = sortrows (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
124 n = rows (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
125 ## Identify runs. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
126 jdx = find (any (diff (subs, 1, 1), 2)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
127 jdx = [jdx; n]; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
128 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
129 vals = cellfun (func, mat2cell (vals(:)(idx), diff ([0; jdx]))); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
130 subs = subs(jdx, :); |
10283
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
131 mode = "unique"; |
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
132 else |
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
133 mode = "sum"; |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
134 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
135 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
136 ## Form the sparse matrix. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
137 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
138 A = sparse (subs(:,1), subs(:,2), vals, mode); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
139 elseif (length (sz) == 2) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
140 A = sparse (subs(:,1), subs(:,2), vals, sz(1), sz(2), mode); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
141 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
142 error ("accumarray: dimensions mismatch") |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
143 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
144 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
145 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
146 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
147 ## Linearize subscripts. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
148 if (ndims > 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
149 if (isempty (sz)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
150 if (iscell (subs)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
151 sz = cellfun (@max, subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
152 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
153 sz = max (subs, [], 1); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
154 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
155 elseif (ndims != length (sz)) |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
156 error ("accumarray: dimensions mismatch") |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
157 endif |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
158 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
159 ## Convert multidimensional subscripts. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
160 if (ismatrix (subs)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
161 subs = num2cell (subs, 1); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
162 endif |
10274
db613bccd992
take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10271
diff
changeset
|
163 subs = sub2ind (sz, subs{:}); # creates index cache |
10271
297996005012
1 more small fix in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10269
diff
changeset
|
164 elseif (! isempty (sz) && length (sz) < 2) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
165 error ("accumarray: needs at least 2 dimensions"); |
10274
db613bccd992
take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10271
diff
changeset
|
166 elseif (! isindex (subs)) # creates index cache |
db613bccd992
take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10271
diff
changeset
|
167 error ("accumarray: indices must be positive integers"); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
168 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
169 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
170 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
171 ## Some built-in reductions handled efficiently. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
172 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
173 if (isempty (func) || func == @sum) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
174 ## Fast summation. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
175 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
176 A = __accumarray_sum__ (subs, vals); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
177 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
178 A = __accumarray_sum__ (subs, vals, prod (sz)); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
179 ## set proper shape. |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
180 A = reshape (A, sz); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
181 endif |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
182 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
183 ## we fill in nonzero fill value. |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
184 if (fillval != 0) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
185 mask = true (size (A)); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
186 mask(subs) = false; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
187 A(mask) = fillval; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
188 endif |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
189 elseif (func == @max) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
190 ## Fast maximization. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
191 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
192 if (isinteger (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
193 zero = intmin (class (vals)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
194 elseif (islogical (vals)) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
195 zero = false; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
196 elseif (fillval == 0 && all (vals(:) >= 0)) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
197 ## This is a common case - fillval is zero, all numbers nonegative. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
198 zero = 0; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
199 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
200 zero = NaN; # Neutral value. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
201 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
202 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
203 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
204 A = __accumarray_max__ (subs, vals, zero); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
205 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
206 A = __accumarray_max__ (subs, vals, zero, prod (sz)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
207 A = reshape (A, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
208 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
209 |
10775 | 210 if (fillval != zero && ! (isnan (fillval) || isnan (zero))) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
211 mask = true (size (A)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
212 mask(subs) = false; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
213 A(mask) = fillval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
214 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
215 elseif (func == @min) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
216 ## Fast minimization. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
217 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
218 if (isinteger (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
219 zero = intmax (class (vals)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
220 elseif (islogical (vals)) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
221 zero = true; |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
222 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
223 zero = NaN; # Neutral value. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
224 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
225 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
226 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
227 A = __accumarray_min__ (subs, vals, zero); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
228 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
229 A = __accumarray_min__ (subs, vals, zero, prod (sz)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
230 A = reshape (A, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
231 endif |
8820
89b95972e178
fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents:
8507
diff
changeset
|
232 |
10775 | 233 if (fillval != zero && ! (isnan (fillval) || isnan (zero))) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
234 mask = true (size (A)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
235 mask(subs) = false; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
236 A(mask) = fillval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
237 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
238 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
239 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
240 ## The general case. Reduce values. |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
241 n = rows (subs); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
242 if (numel (vals) == 1) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
243 vals = vals(ones (1, n), 1); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
244 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
245 vals = vals(:); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
246 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
247 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
248 ## Sort indices. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
249 [subs, idx] = sort (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
250 ## Identify runs. |
10283
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
251 jdx = find (subs(1:n-1) != subs(2:n)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
252 jdx = [jdx; n]; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
253 vals = mat2cell (vals(idx), diff ([0; jdx])); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
254 ## Optimize the case when function is @(x) {x}, i.e. we just want to |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
255 ## collect the values to cells. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
256 persistent simple_cell_str = func2str (@(x) {x}); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
257 if (! strcmp (func2str (func), simple_cell_str)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
258 vals = cellfun (func, vals); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
259 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
260 subs = subs(jdx); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
261 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
262 ## Construct matrix of fillvals. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
263 if (iscell (vals)) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
264 A = cell (sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
265 elseif (fillval == 0) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
266 A = zeros (sz, class (vals)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
267 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
268 A = repmat (fillval, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
269 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
270 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
271 ## Set the reduced values. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
272 A(subs) = vals; |
6770 | 273 endif |
274 endif | |
275 endfunction | |
276 | |
277 %!error (accumarray (1:5)) | |
278 %!error (accumarray ([1,2,3],1:2)) | |
279 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208]) | |
280 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105),cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208])) | |
281 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105,[],@(x)sin(sum(x))),sin(cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208]))) | |
282 %!assert (accumarray ({[1 3 3 2 3 1 2 2 3 3 1 2],[3 4 2 1 4 3 4 2 2 4 3 4],[1 1 2 2 1 1 2 1 1 1 2 2]},101:112),cat(3,[0,0,207,0;0,108,0,0;0,109,0,317],[0,0,111,0;104,0,0,219;0,103,0,0])) | |
283 %!assert (accumarray ([1,1;2,1;2,3;2,1;2,3],101:105,[2,4],@max,NaN),[101,NaN,NaN,NaN;104,NaN,105,NaN]) | |
284 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2 4],@prod,0,true),sparse([1,2,2],[1,1,3],[101,10608,10815],2,4)) | |
285 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0]) | |
286 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x)length(x)>1),[false,false,false,false;true,false,true,false]) | |
287 %!test | |
288 %! A = accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x){x}); | |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
289 %! assert (A{2},[102;104]) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
290 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
291 %! subs = ceil (rand (2000, 3)*10); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
292 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
293 %! assert (accumarray (subs, vals, [], @max), accumarray (subs, vals, [], @(x) max (x))); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
294 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
295 %! subs = ceil (rand (2000, 1)*100); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
296 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
297 %! assert (accumarray (subs, vals, [100, 1], @min, NaN), accumarray (subs, vals, [100, 1], @(x) min (x), NaN)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
298 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
299 %! subs = ceil (rand (2000, 2)*30); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
300 %! subsc = num2cell (subs, 1); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
301 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
302 %! assert (accumarray (subsc, vals, [], [], 0, true), accumarray (subs, vals, [], [], 0, true)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
303 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
304 %! subs = ceil (rand (2000, 3)*10); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
305 %! subsc = num2cell (subs, 1); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
306 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
307 %! assert (accumarray (subsc, vals, [], @max), accumarray (subs, vals, [], @max)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
308 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
309 |