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

Last change on this file since 1261 was 1261, checked in by Peter, 13 years ago

changing some u_int to size_t

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1// $Id: VectorFunction.cc 1261 2008-04-08 14:01:33Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/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.begin(), vec.end());
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(size_t 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.