A toolkit for working with phylogenetic data.
v0.22.1
operations.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 #include <cassert>
34 #include <cmath>
35 #include <vector>
36 
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Histogram Operations
45 // =================================================================================================
46 
47 void offset (Histogram& h, double value)
48 {
49  for (size_t i = 0; i < h.bins(); ++i) {
50  h[i] += value;
51  }
52 }
53 
54 void scale (Histogram& h, double factor)
55 {
56  for (size_t i = 0; i < h.bins(); ++i) {
57  h[i] *= factor;
58  }
59 }
60 
61 void normalize (Histogram& h, double total)
62 {
63  // Don't do anything for empty histograms.
64  auto const s = sum( h );
65  if( std::isfinite(s) && s > 0.0 ) {
66  scale( h, total / s );
67  }
68 }
69 
70 } // namespace utils
71 } // namespace genesis
void offset(Histogram &h, double value)
Definition: operations.cpp:47
Header of Histogram class.
Histogram class for accumulating and summarizing data.
Definition: histogram.hpp:68
double sum(const Histogram &h)
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Header of Histogram operations functions.
Header of Histogram statistics functions.
void scale(Histogram &h, double factor)
Definition: operations.cpp:54
void normalize(Histogram &h, double total)
Definition: operations.cpp:61