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

Last change on this file since 827 was 827, checked in by Peter, 16 years ago

added Functors to be used in CIR. Refs #151

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Id`
File size: 2.0 KB
Line
1// \$Id: VectorFunction.cc 827 2007-03-19 18:52:36Z peter \$
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "VectorFunction.h"
25#include "utility.h"
26
27#include <algorithm>
28#include <cassert>
29#include <numeric>
30#include <vector>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  VectorFunction::~VectorFunction(){}
37
38
39  double Max::operator()(const std::vector<double>& vec) const
40  {
41    assert(vec.size());
42    return *(std::max_element(vec.begin(), vec.end()));
43  }
44
45
46  double Mean::operator()(const std::vector<double>& vec) const
47  {
48    assert(vec.size());
49    return std::accumulate(vec.begin(), vec.end(), 0.0)/vec.size();
50  }
51
52
53  double Median::operator()(const std::vector<double>& vec) const
54  {
55    assert(vec.size());
56    return median(vec);
57  }
58
59
60  double Min::operator()(const std::vector<double>& vec) const
61  {
62    assert(vec.size());
63    return *(std::min_element(vec.begin(), vec.end()));
64  }
65
66
67  Nth_Element::Nth_Element(u_int N)
68    : N_(N) {}
69
70
71  double Nth_Element::operator()(const std::vector<double>& vec) const
72  {
73    assert(N_-1<vec.size());
74    std::vector<double> vec_copy(vec);
75    std::vector<double>::iterator nth = vec_copy.begin()+(N_-1);
76    std::nth_element(vec_copy.begin(), nth, vec_copy.end());
77    return *(nth);
78  }
79
80
81
82
83}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.