TabulatedFunctionErrors.cc
Go to the documentation of this file.
1 
7 #include <sstream>
8 #if !defined(__clang_major__) || __clang_major__ < 5
9 # include <ext/algorithm>
10 #endif
11 #include <utl/AugerException.h>
12 #include <utl/TabulatedFunctionIterators.h>
13 #include <utl/TabulatedFunctionErrors.h>
14 #include <utl/ErrorLogger.h>
15 
16 using namespace std;
17 using namespace utl;
18 
19 
20 void TabulatedFunctionErrors::FillTable(const std::vector<double>& xValues,
21  const std::vector<double>& xErrValues,
22  const std::vector<double>& yValues,
23  const std::vector<double>& yErrValues)
24 
25 {
26  const unsigned int xSize = xValues.size();
27 
28  if (xSize != yValues.size() || xSize != xErrValues.size() ||
29  xSize != yErrValues.size()) {
30  ostringstream err;
31  err << "size of x, xErr, y, yErr arrays are different";
32  ERROR(err);
33  throw OutOfBoundException(err.str());
34  }
35 
36  fX = xValues;
37  fY = yValues;
38  fXErr = xErrValues;
39  fYErr = yErrValues;
40 
41  if (is_sorted(fX.begin(), fX.end()))
42  return;
43 
44  // insertion sort
45  for (unsigned int i = 0; i < xSize; ++i) {
46  unsigned int mini = i;
47  double minx = fX[i];
48  for (unsigned int j = i+1; j < xSize; ++j) {
49  const double x = fX[j];
50  if (x < minx) {
51  minx = x;
52  mini = j;
53  }
54  }
55  if (mini != i) {
56  using std::swap;
57  swap(fX[i], fX[mini]);
58  swap(fY[i], fY[mini]);
59  swap(fXErr[i], fXErr[mini]);
60  swap(fYErr[i], fYErr[mini]);
61  }
62  }
63 }
64 
65 
66 void
67 TabulatedFunctionErrors::PushBack(const double x, const double xErr,
68  const double y, const double yErr)
69 {
70  fX.push_back(x);
71  fY.push_back(y);
72  fXErr.push_back(xErr);
73  fYErr.push_back(yErr);
74 }
75 
76 
77 /*ConstTabulatedFunctionErrIterator
78 TabulatedFunctionErrors::Find(const double x,const double y)const{
79 
80  Array::const_iterator itx = find(fX.begin(),fX.end(),x);
81  Array::const_iterator ity = find(fY.begin(),fY.end(),y);
82 
83  if (itx == fX.end() || ity ==fY.end() )
84  return ConstTabulatedFunctionErrIterator(fX.end(),fXErr.end(),
85  fY.end(),fYErr.end());
86  else {
87  unsigned int pos = ity - fY.begin();
88  Array::const_iterator itxerr= fXErr.begin() + pos;
89  Array::const_iterator ityerr= fYErr.begin() + pos;
90  return ConstTabulatedFunctionErrIterator(itx,itxerr,ity,ityerr);
91  }
92 }*/
93 
94 
96 TabulatedFunctionErrors::FindX(const double x)
97  const
98 {
99  const Array::const_iterator xBegin = fX.begin();
100  const Array::const_iterator xEnd = fX.end();
101  const Array::const_iterator xIt = lower_bound(xBegin, xEnd, x);
102  if (xIt == xEnd || x != *xIt)
103  return End();
104  else {
105  const int off = xIt - xBegin;
106  const Array::const_iterator yIt = fY.begin() + off;
107  const Array::const_iterator xErrIt = fXErr.begin() + off;
108  const Array::const_iterator yErrIt = fYErr.begin() + off;
109  return ConstTabulatedFunctionErrIterator(xIt, xErrIt, yIt, yErrIt);
110  }
111 }
112 
113 
115 TabulatedFunctionErrors::FindY(const double y)
116  const
117 {
118  const Array::const_iterator yIt = find(fY.begin(), fY.end(), y);
119  if (yIt == fY.end())
120  return End();
121  else {
122  const int off = yIt - fY.begin();
123  const Array::const_iterator xIt = fX.begin() + off;
124  const Array::const_iterator xErrIt = fXErr.begin() + off;
125  const Array::const_iterator yErrIt = fYErr.begin() + off;
126  return ConstTabulatedFunctionErrIterator(xIt, xErrIt, yIt, yErrIt);
127  }
128 }
129 
130 
132 TabulatedFunctionErrors::Insert(const TabulatedFunctionErrIterator& pos,
133  const double x, const double xErr, const double y, const double yErr)
134 {
135  TabulatedFunctionErrIterator it = Begin();
136  while (it != pos && it != End())
137  ++it;
138 
139  Array::iterator xIt = it.fXIterator;
140  Array::iterator xErrIt = it.fXErrIterator;
141  Array::iterator yIt = it.fYIterator;
142  Array::iterator yErrIt = it.fYErrIterator;
143 
144  xIt = fX.insert(xIt, x);
145  xErrIt = fXErr.insert(xErrIt, xErr);
146  yIt = fY.insert(yIt, y);
147  yErrIt = fYErr.insert(yErrIt, yErr);
148 
149  return TabulatedFunctionErrIterator(xIt, xErrIt, yIt, yErrIt);
150 }
151 
152 
153 void
154 TabulatedFunctionErrors::Clear()
155 {
156  TabulatedFunction::Clear();
157  fXErr.clear();
158  fYErr.clear();
159 }
160 
void swap(utl::Trace< T > &t1, utl::Trace< T > &t2)
Definition: Trace.h:363
Exception for reporting variable out of valid range.
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165

, generated on Tue Sep 26 2023.