1 | // $Id: VectorFunction.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "VectorFunction.h" |
---|
24 | #include "utility.h" |
---|
25 | |
---|
26 | #include <algorithm> |
---|
27 | #include <cassert> |
---|
28 | #include <numeric> |
---|
29 | #include <vector> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace statistics { |
---|
34 | |
---|
35 | VectorFunction::~VectorFunction(){} |
---|
36 | |
---|
37 | |
---|
38 | double Max::operator()(const std::vector<double>& vec) const |
---|
39 | { |
---|
40 | assert(vec.size()); |
---|
41 | return *(std::max_element(vec.begin(), vec.end())); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | double Mean::operator()(const std::vector<double>& vec) const |
---|
46 | { |
---|
47 | assert(vec.size()); |
---|
48 | return std::accumulate(vec.begin(), vec.end(), 0.0)/vec.size(); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | double Median::operator()(const std::vector<double>& vec) const |
---|
53 | { |
---|
54 | assert(vec.size()); |
---|
55 | return median(vec.begin(), vec.end()); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | double Min::operator()(const std::vector<double>& vec) const |
---|
60 | { |
---|
61 | assert(vec.size()); |
---|
62 | return *(std::min_element(vec.begin(), vec.end())); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | Nth_Element::Nth_Element(size_t N) |
---|
67 | : N_(N) {} |
---|
68 | |
---|
69 | |
---|
70 | double Nth_Element::operator()(const std::vector<double>& vec) const |
---|
71 | { |
---|
72 | assert(N_-1<vec.size()); |
---|
73 | std::vector<double> vec_copy(vec); |
---|
74 | std::vector<double>::iterator nth = vec_copy.begin()+(N_-1); |
---|
75 | std::nth_element(vec_copy.begin(), nth, vec_copy.end()); |
---|
76 | return *(nth); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | }}} // of namespace statistics, yat, and theplu |
---|