source:trunk/yat/statistics/VectorFunction.cc@1486

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 14 years ago

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Id`
File size: 2.0 KB
Line
1// \$Id: VectorFunction.cc 1486 2008-09-09 21:17:19Z 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
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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "VectorFunction.h"
26#include "utility.h"
27
28#include <algorithm>
29#include <cassert>
30#include <numeric>
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace statistics {
36
37  VectorFunction::~VectorFunction(){}
38
39
40  double Max::operator()(const std::vector<double>& vec) const
41  {
42    assert(vec.size());
43    return *(std::max_element(vec.begin(), vec.end()));
44  }
45
46
47  double Mean::operator()(const std::vector<double>& vec) const
48  {
49    assert(vec.size());
50    return std::accumulate(vec.begin(), vec.end(), 0.0)/vec.size();
51  }
52
53
54  double Median::operator()(const std::vector<double>& vec) const
55  {
56    assert(vec.size());
57    return median(vec.begin(), vec.end());
58  }
59
60
61  double Min::operator()(const std::vector<double>& vec) const
62  {
63    assert(vec.size());
64    return *(std::min_element(vec.begin(), vec.end()));
65  }
66
67
68  Nth_Element::Nth_Element(size_t N)
69    : N_(N) {}
70
71
72  double Nth_Element::operator()(const std::vector<double>& vec) const
73  {
74    assert(N_-1<vec.size());
75    std::vector<double> vec_copy(vec);
76    std::vector<double>::iterator nth = vec_copy.begin()+(N_-1);
77    std::nth_element(vec_copy.begin(), nth, vec_copy.end());
78    return *(nth);
79  }
80
81
82
83
84}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.