blitz Version 1.0.2
Loading...
Searching...
No Matches
reduce.h
Go to the documentation of this file.
1// -*- C++ -*-
2/***************************************************************************
3 * blitz/reduce.h Reduction operators: sum, mean, min, max,
4 * minIndex, maxIndex, product, count, any, all
5 *
6 * $Id$
7 *
8 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
9 *
10 * This file is a part of Blitz.
11 *
12 * Blitz is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation, either version 3
15 * of the License, or (at your option) any later version.
16 *
17 * Blitz is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with Blitz. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Suggestions: blitz-devel@lists.sourceforge.net
26 * Bugs: blitz-support@lists.sourceforge.net
27 *
28 * For more information, please see the Blitz++ Home Page:
29 * https://sourceforge.net/projects/blitz/
30 *
31 ***************************************************************************/
32
33#ifndef BZ_REDUCE_H
34#define BZ_REDUCE_H
35
36#include <blitz/blitz.h>
37#include <blitz/numtrait.h>
38#include <blitz/numinquire.h>
39#include <blitz/tinyvec2.h>
40
41
42// The various reduce classes.
43// The prototype of the reset method is mandated by the class _bz_ReduceReset
44// in file array/reduce.h
45
46namespace blitz {
47
48template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
49class ReduceSum {
50public:
51
52 typedef P_sourcetype T_sourcetype;
53 typedef P_resulttype T_resulttype;
55
56 static const bool needIndex = false, needInit = false;
57
59
60 bool operator()(const T_sourcetype& x,const int=0) const {
61 sum_ += x;
62 return true;
63 }
64
65 T_resulttype result(const int) const { return sum_; }
66
67 void reset() const { sum_ = zero(T_resulttype()); }
68
69 static const char* name() { return "sum"; }
70
71protected:
72
74};
75
76template<typename P_sourcetype, typename P_resulttype = BZ_FLOATTYPE(P_sourcetype)>
78public:
79
80 typedef P_sourcetype T_sourcetype;
81 typedef P_resulttype T_resulttype;
83
84 static const bool needIndex = false, needInit = false;
85
87
88 bool operator()(const T_sourcetype& x,const int=0) const {
89 sum_ += x;
90 return true;
91 }
92
93 T_resulttype result(const int count) const { return sum_ / count; }
94
95 void reset() const { sum_ = zero(T_resulttype()); }
96
97 static const char* name() { return "mean"; }
98
99protected:
100
102};
103
104template<typename P_sourcetype>
106public:
107
108 typedef P_sourcetype T_sourcetype;
109 typedef P_sourcetype T_resulttype;
111
112 static const bool needIndex = false, needInit = false;
113
115
116 bool operator()(const T_sourcetype& x,const int=0) const {
117 if (x < min_)
118 min_ = x;
119 return true;
120 }
121
122 T_resulttype result(const int) const { return min_; }
123
124 void reset() const { min_ = huge(P_sourcetype()); }
125
126 static const char* name() { return "min"; }
127
128protected:
129
131};
132
133template<typename P_sourcetype>
135public:
136
137 typedef P_sourcetype T_sourcetype;
138 typedef P_sourcetype T_resulttype;
140
141 static const bool needIndex = false, needInit = false;
142
144
145 bool operator()(const T_sourcetype& x,const int=0) const {
146 if (x > max_)
147 max_ = x;
148 return true;
149 }
150
151 T_resulttype result(const int) const { return max_; }
152
153 void reset() const { max_ = neghuge(P_sourcetype()); }
154
155 static const char* name() { return "max"; }
156
157protected:
158
160};
161
162template <typename T>
164 void operator=(const T& val) { min = max = val; }
167};
168
169template<typename P_sourcetype>
171public:
172
173 typedef P_sourcetype T_sourcetype;
176
177 static const bool needIndex = false, needInit = true;
178
180
181 bool operator()(T_sourcetype x,const int=0) const {
182 if (x > minmax_.max)
183 minmax_.max = x;
184 else if (x < minmax_.min)
185 minmax_.min = x;
186 return true;
187 }
188
189 T_resulttype result(int) { return minmax_; }
190
191 void reset(P_sourcetype initialValue) const { minmax_ = initialValue; }
192
193 static const char* name() { return "minmax"; }
194
195protected:
196
198};
199
200template<typename P_sourcetype>
202public:
203
204 typedef P_sourcetype T_sourcetype;
205 typedef int T_resulttype;
207
208 static const bool needIndex = true, needInit = false;
209
211
212 bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
213 if (x < min_) {
214 min_ = x;
215 index_ = index;
216 }
217 return true;
218 }
219
220 T_resulttype result(const int) const { return index_; }
221
222 void reset(const T_resulttype& index) const {
223 min_ = huge(T_sourcetype());
224 index_ = index;
225 }
226
227 static const char* name() { return "minIndex"; }
228
229protected:
230
233};
234
235template<typename P_sourcetype, int N>
237public:
238
239 typedef P_sourcetype T_sourcetype;
242
243 static const bool needIndex = true, needInit = false;
244
246
247 bool operator()(const T_sourcetype& x, const T_resulttype& index) const {
248 if (x < min_) {
249 min_ = x;
250 index_ = index;
251 }
252 return true;
253 }
254
255 T_resulttype result(const int) const { return index_; }
256
257 void reset(const T_resulttype& index) const {
258 min_ = huge(T_sourcetype());
259 index_ = index;
260 }
261
262 static const char* name() { return "minIndexVector"; }
263
264protected:
265
268};
269
270template<typename P_sourcetype>
272public:
273
274 typedef P_sourcetype T_sourcetype;
275 typedef int T_resulttype;
277
278 static const bool needIndex = true, needInit = false;
279
281
282 bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
283 if (x > max_) {
284 max_ = x;
285 index_ = index;
286 }
287 return true;
288 }
289
290 T_resulttype result(int) const { return index_; }
291
292 void reset(const T_resulttype& index) const {
293 max_ = neghuge(T_sourcetype());
294 index_ = index;
295 }
296
297 static const char* name() { return "maxIndex"; }
298
299protected:
300
303};
304
305template<typename P_sourcetype, int N_rank>
307public:
308
309 typedef P_sourcetype T_sourcetype;
312
313 static const bool needIndex = true, needInit = false;
314
316
317 bool operator()(const T_sourcetype& x, const T_resulttype& index) const {
318 if (x > max_) {
319 max_ = x;
320 index_ = index;
321 }
322 return true;
323 }
324
325 T_resulttype result(const int) const { return index_; }
326
327 void reset(const T_resulttype& index) const {
328 max_ = neghuge(T_sourcetype());
329 index_ = index;
330 }
331
332 static const char* name() { return "maxIndexVector"; }
333
334protected:
335
338};
339
340template<typename P_sourcetype>
342public:
343
344 typedef P_sourcetype T_sourcetype;
345 typedef int T_resulttype;
347
348 static const bool needIndex = false, needInit = false;
349
351
352 bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
353 if (x) {
354 index_ = index;
355 return false;
356 } else
357 return true;
358 }
359
360 T_resulttype result(const int) const { return index_; }
361
362 void reset() const { index_ = tiny(int()); }
363
364 static const char* name() { return "first"; }
365
366protected:
367
369};
370
371template<typename P_sourcetype>
373public:
374
375 typedef P_sourcetype T_sourcetype;
376 typedef int T_resulttype;
378
379 static const bool needIndex = false, needInit = false;
380
382
383 bool operator()(const T_sourcetype& x,const T_resulttype& index) const {
384 if (x) {
385 index_ = index;
386 return true;
387 } else
388 return true;
389 }
390
391 T_resulttype result(const int) const { return index_; }
392
393 void reset() const { index_ = huge(int()); }
394
395 static const char* name() { return "last"; }
396
397protected:
398
400};
401
402template<typename P_sourcetype, typename P_resulttype = BZ_SUMTYPE(P_sourcetype)>
404public:
405
406 typedef P_sourcetype T_sourcetype;
407 typedef P_resulttype T_resulttype;
409
410 static const bool needIndex = false, needInit = false;
411
413
414 bool operator()(const T_sourcetype& x,const int=0) const {
415 product_ *= x;
416 return true;
417 }
418
419 T_resulttype result(const int) const { return product_; }
420
421 void reset() const { product_ = one(T_resulttype()); }
422
423 static const char* name() { return "product"; }
424
425protected:
426
428};
429
430template<typename P_sourcetype>
432public:
433
434 typedef P_sourcetype T_sourcetype;
435 typedef int T_resulttype;
437
438 static const bool needIndex = false, needInit = false;
439
441
442 bool operator()(const T_sourcetype& x,const int=0) const {
443 if (bool(x))
444 ++count_;
445 return true;
446 }
447
448 T_resulttype result(const int) const { return count_; }
449
450 void reset() const { count_ = zero(T_resulttype()); }
451
452 static const char* name() { return "count"; }
453
454protected:
455
457};
458
459template<typename P_sourcetype>
461public:
462
463 typedef P_sourcetype T_sourcetype;
464 typedef bool T_resulttype;
466
467 static const bool needIndex = false, needInit = false;
468
470
471 bool operator()(const T_sourcetype& x,const int=0) const {
472 if (bool(x)) {
473 any_ = true;
474 return false;
475 }
476
477 return true;
478 }
479
480 T_resulttype result(const int) const { return any_; }
481
482 void reset() const { any_ = false; }
483
484 static const char* name() { return "any"; }
485
486protected:
487
489};
490
491template<typename P_sourcetype>
493public:
494
495 typedef P_sourcetype T_sourcetype;
496 typedef bool T_resulttype;
498
499 static const bool needIndex = false, needInit = false;
500
502
503 bool operator()(const T_sourcetype& x,const int=0) const {
504 if (!bool(x)) {
505 all_ = false;
506 return false;
507 } else
508 return true;
509 }
510
511 T_resulttype result(const int) const { return all_; }
512
513 void reset() const { all_ = true; }
514
515 static const char* name() { return "all"; }
516
517protected:
518
520};
521
522}
523
524#endif // BZ_REDUCE_H
Definition: reduce.h:492
static const bool needInit
Definition: reduce.h:499
T_resulttype T_numtype
Definition: reduce.h:497
bool T_resulttype
Definition: reduce.h:496
void reset() const
Definition: reduce.h:513
ReduceAll()
Definition: reduce.h:501
P_sourcetype T_sourcetype
Definition: reduce.h:495
T_resulttype result(const int) const
Definition: reduce.h:511
static const bool needIndex
Definition: reduce.h:499
static const char * name()
Definition: reduce.h:515
T_resulttype all_
Definition: reduce.h:519
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:503
Definition: reduce.h:460
static const char * name()
Definition: reduce.h:484
static const bool needInit
Definition: reduce.h:467
P_sourcetype T_sourcetype
Definition: reduce.h:463
void reset() const
Definition: reduce.h:482
ReduceAny()
Definition: reduce.h:469
T_resulttype result(const int) const
Definition: reduce.h:480
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:471
static const bool needIndex
Definition: reduce.h:467
T_resulttype any_
Definition: reduce.h:488
bool T_resulttype
Definition: reduce.h:464
T_resulttype T_numtype
Definition: reduce.h:465
Definition: reduce.h:431
ReduceCount()
Definition: reduce.h:440
static const bool needIndex
Definition: reduce.h:438
void reset() const
Definition: reduce.h:450
T_resulttype count_
Definition: reduce.h:456
P_sourcetype T_sourcetype
Definition: reduce.h:434
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:442
static const bool needInit
Definition: reduce.h:438
T_resulttype result(const int) const
Definition: reduce.h:448
int T_resulttype
Definition: reduce.h:435
static const char * name()
Definition: reduce.h:452
T_resulttype T_numtype
Definition: reduce.h:436
Definition: reduce.h:341
static const bool needInit
Definition: reduce.h:348
static const bool needIndex
Definition: reduce.h:348
void reset() const
Definition: reduce.h:362
static const char * name()
Definition: reduce.h:364
T_resulttype T_numtype
Definition: reduce.h:346
int T_resulttype
Definition: reduce.h:345
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:352
T_resulttype result(const int) const
Definition: reduce.h:360
T_resulttype index_
Definition: reduce.h:368
ReduceFirst()
Definition: reduce.h:350
P_sourcetype T_sourcetype
Definition: reduce.h:344
Definition: reduce.h:372
P_sourcetype T_sourcetype
Definition: reduce.h:375
int T_resulttype
Definition: reduce.h:376
T_resulttype T_numtype
Definition: reduce.h:377
void reset() const
Definition: reduce.h:393
static const bool needIndex
Definition: reduce.h:379
static const bool needInit
Definition: reduce.h:379
static const char * name()
Definition: reduce.h:395
ReduceLast()
Definition: reduce.h:381
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:383
T_resulttype index_
Definition: reduce.h:399
T_resulttype result(const int) const
Definition: reduce.h:391
Definition: reduce.h:306
static const bool needIndex
Definition: reduce.h:313
P_sourcetype T_sourcetype
Definition: reduce.h:309
T_resulttype result(const int) const
Definition: reduce.h:325
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:317
ReduceMaxIndexVector()
Definition: reduce.h:315
T_resulttype T_numtype
Definition: reduce.h:311
static const bool needInit
Definition: reduce.h:313
T_resulttype index_
Definition: reduce.h:337
T_sourcetype max_
Definition: reduce.h:336
void reset(const T_resulttype &index) const
Definition: reduce.h:327
static const char * name()
Definition: reduce.h:332
TinyVector< int, N_rank > T_resulttype
Definition: reduce.h:310
Definition: reduce.h:271
static const bool needInit
Definition: reduce.h:278
static const char * name()
Definition: reduce.h:297
P_sourcetype T_sourcetype
Definition: reduce.h:274
T_resulttype result(int) const
Definition: reduce.h:290
void reset(const T_resulttype &index) const
Definition: reduce.h:292
T_sourcetype max_
Definition: reduce.h:301
T_resulttype T_numtype
Definition: reduce.h:276
int T_resulttype
Definition: reduce.h:275
ReduceMaxIndex()
Definition: reduce.h:280
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:282
static const bool needIndex
Definition: reduce.h:278
T_resulttype index_
Definition: reduce.h:302
Definition: reduce.h:134
T_resulttype T_numtype
Definition: reduce.h:139
static const bool needInit
Definition: reduce.h:141
void reset() const
Definition: reduce.h:153
ReduceMax()
Definition: reduce.h:143
P_sourcetype T_resulttype
Definition: reduce.h:138
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:145
T_resulttype max_
Definition: reduce.h:159
P_sourcetype T_sourcetype
Definition: reduce.h:137
static const bool needIndex
Definition: reduce.h:141
T_resulttype result(const int) const
Definition: reduce.h:151
static const char * name()
Definition: reduce.h:155
Definition: reduce.h:77
T_resulttype sum_
Definition: reduce.h:101
void reset() const
Definition: reduce.h:95
static const bool needIndex
Definition: reduce.h:84
static const char * name()
Definition: reduce.h:97
T_resulttype T_numtype
Definition: reduce.h:82
T_resulttype result(const int count) const
Definition: reduce.h:93
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:88
P_sourcetype T_sourcetype
Definition: reduce.h:80
static const bool needInit
Definition: reduce.h:84
P_resulttype T_resulttype
Definition: reduce.h:81
ReduceMean()
Definition: reduce.h:86
Definition: reduce.h:236
static const char * name()
Definition: reduce.h:262
static const bool needIndex
Definition: reduce.h:243
T_resulttype result(const int) const
Definition: reduce.h:255
T_resulttype index_
Definition: reduce.h:267
void reset(const T_resulttype &index) const
Definition: reduce.h:257
P_sourcetype T_sourcetype
Definition: reduce.h:239
T_resulttype T_numtype
Definition: reduce.h:241
T_sourcetype min_
Definition: reduce.h:266
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:247
TinyVector< int, N > T_resulttype
Definition: reduce.h:240
ReduceMinIndexVector()
Definition: reduce.h:245
static const bool needInit
Definition: reduce.h:243
Definition: reduce.h:201
ReduceMinIndex()
Definition: reduce.h:210
bool operator()(const T_sourcetype &x, const T_resulttype &index) const
Definition: reduce.h:212
T_resulttype T_numtype
Definition: reduce.h:206
static const char * name()
Definition: reduce.h:227
static const bool needIndex
Definition: reduce.h:208
int T_resulttype
Definition: reduce.h:205
T_resulttype result(const int) const
Definition: reduce.h:220
P_sourcetype T_sourcetype
Definition: reduce.h:204
static const bool needInit
Definition: reduce.h:208
T_resulttype index_
Definition: reduce.h:232
void reset(const T_resulttype &index) const
Definition: reduce.h:222
T_sourcetype min_
Definition: reduce.h:231
Definition: reduce.h:170
T_resulttype result(int)
Definition: reduce.h:189
T_resulttype minmax_
Definition: reduce.h:197
bool operator()(T_sourcetype x, const int=0) const
Definition: reduce.h:181
T_resulttype T_numtype
Definition: reduce.h:175
void reset(P_sourcetype initialValue) const
Definition: reduce.h:191
static const bool needInit
Definition: reduce.h:177
P_sourcetype T_sourcetype
Definition: reduce.h:173
MinMaxValue< P_sourcetype > T_resulttype
Definition: reduce.h:174
ReduceMinMax()
Definition: reduce.h:179
static const char * name()
Definition: reduce.h:193
static const bool needIndex
Definition: reduce.h:177
Definition: reduce.h:105
T_resulttype T_numtype
Definition: reduce.h:110
T_resulttype min_
Definition: reduce.h:130
static const bool needInit
Definition: reduce.h:112
static const bool needIndex
Definition: reduce.h:112
void reset() const
Definition: reduce.h:124
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:116
P_sourcetype T_sourcetype
Definition: reduce.h:108
T_resulttype result(const int) const
Definition: reduce.h:122
ReduceMin()
Definition: reduce.h:114
static const char * name()
Definition: reduce.h:126
P_sourcetype T_resulttype
Definition: reduce.h:109
Definition: reduce.h:403
static const bool needInit
Definition: reduce.h:410
T_resulttype T_numtype
Definition: reduce.h:408
ReduceProduct()
Definition: reduce.h:412
T_resulttype result(const int) const
Definition: reduce.h:419
void reset() const
Definition: reduce.h:421
P_resulttype T_resulttype
Definition: reduce.h:407
static const bool needIndex
Definition: reduce.h:410
P_sourcetype T_sourcetype
Definition: reduce.h:406
static const char * name()
Definition: reduce.h:423
T_resulttype product_
Definition: reduce.h:427
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:414
Definition: reduce.h:49
ReduceSum()
Definition: reduce.h:58
P_sourcetype T_sourcetype
Definition: reduce.h:52
P_resulttype T_resulttype
Definition: reduce.h:53
T_resulttype sum_
Definition: reduce.h:73
T_resulttype T_numtype
Definition: reduce.h:54
bool operator()(const T_sourcetype &x, const int=0) const
Definition: reduce.h:60
static const char * name()
Definition: reduce.h:69
void reset() const
Definition: reduce.h:67
static const bool needIndex
Definition: reduce.h:56
T_resulttype result(const int) const
Definition: reduce.h:65
static const bool needInit
Definition: reduce.h:56
The TinyVector class is a one-dimensional, fixed length vector that implements the blitz expression t...
Definition: tinyvec2.h:73
Definition: array-impl.h:66
Definition: reduce.h:163
T max
Definition: reduce.h:166
void operator=(const T &val)
Definition: reduce.h:164
T min
Definition: reduce.h:165