Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
util.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/util.cc
11 /// @brief small bundle of utilities for dealing with numbers.
12 /// @author James Thompson
13 
14 #include <numeric/types.hh>
15 #include <utility/vector1.hh>
16 
17 #include <algorithm>
18 
19 namespace numeric {
20 
22  assert( values.size() ); // An empty list doesn't have a median
24  std::sort( vals.begin(), vals.end() );
25 
26  numeric::Size const n_vals( vals.size() );
27  numeric::Real retval( 0.0 );
28  if ( n_vals % 2 == 0 ) { // Even number of items
29  retval += 0.5 * vals[ n_vals / 2 ];
30  retval += 0.5 * vals[ n_vals / 2 + 1];
31  } else { // Odd number of items
32  retval = vals[ (n_vals - 1) / 2 + 1 ];
33  }
34  return retval;
35 }
36 
39 
40  numeric::Size const n_vals( values.size() );
41  numeric::Real total( 0.0 );
42  for ( iter it = values.begin(), end = values.end(); it != end; ++it ) {
43  total += *it;
44  }
45 
46  return static_cast< numeric::Real > ( total / n_vals );
47 }
48 
49 } // numeric
platform::Size Size
Definition: types.hh:42
utility::keys::lookup::end< KeyType > const end
numeric::Real median(utility::vector1< numeric::Real > const &values)
Returns the median from a vector1 of Real values.
Definition: util.cc:21
numeric::Real mean(utility::vector1< numeric::Real > const &values)
Definition: util.cc:37
std::vector with 1-based indexing
Definition: vector1.fwd.hh:44
super::const_iterator const_iterator
Definition: vector1.hh:62
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
double Real
Definition: types.hh:39
vector1: std::vector with 1-based indexing