CYCLUS
cyc_arithmetic.cc
Go to the documentation of this file.
1 /*! \file cyc_arithmetic.cc
2  \brief Implements the CycArithmetic class for various arithmetic algorithms
3  \author Kathryn D. Huff
4  */
5 #include <iostream>
6 
7 #include "error.h"
8 #include "cyc_arithmetic.h"
9 
10 namespace cyclus {
11 
12 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 double CycArithmetic::KahanSum(std::vector<double> input) {
15  // http://en.wikipedia.org/wiki/Kahan_summation_algorithm
16  double y, t;
17  double sum = 0.0;
18  // A running compensation for lost low-order bits.
19  double c = 0.0;
20  for (int i = 0; i < input.size(); i++) {
21  y = input[i] - c;
22  // So far, so good: c is zero.
23  t = sum + y;
24  // Alas, sum is big, y small, so low-order digits of y are lost.
25  c = (t - sum) - y;
26  // (t - sum) recovers the high-order part of y; subtracting y recovers -(low part of y)
27  sum = t;
28  // Algebraically, c should always be zero. Beware eagerly optimizing compilers!
29  // Next time around, the lost low part will be added to y in a fresh attempt.
30  }
31  return sum;
32 }
33 
34 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35 void CycArithmetic::sort_inplace_ascending(std::vector<double>& to_sort) {
36  sort(to_sort.begin(), to_sort.end());
37 }
38 
39 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40 std::vector<double> CycArithmetic::sort_ascending(std::vector<double> to_sort) {
41  sort(to_sort.begin(), to_sort.end());
42  return to_sort;
43 }
44 
45 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 std::vector<double> CycArithmetic::sort_ascending(std::map<int, double>
47  to_sort) {
48  std::vector<double> vec_to_sort;
49  std::map<int, double>::const_iterator it;
50  for (it = to_sort.begin(); it != to_sort.end(); ++it) {
51  vec_to_sort.push_back((*it).second);
52  }
53  return sort_ascending(vec_to_sort);
54 }
55 
56 } // namespace cyclus
static std::vector< double > sort_ascending(std::vector< double > to_sort)
orders the vector from smallest value to largest value.
static double KahanSum(std::vector< double > input)
sums the materials in the vector in an intelligent way, to avoid floating point issues.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
Declares the CycArithmetic class, which holds arithmetic algorithms.
static void sort_inplace_ascending(std::vector< double > &to_sort)
Identical to sort_ascending(std::vector<double>) except it modifies the sorts the vector in-place mod...