2 #include <doctest/doctest.h> 25 template <
typename Tx =
double,
typename Ty = Tx,
bool centerbin = false>
class Equidistant2DTable 32 inline int to_bin(Tx x)
const 34 if constexpr (centerbin) {
35 return (x < 0) ? int(x * _dxinv - 0.5) : int(x * _dxinv + 0.5);
38 return std::floor((x - _xmin) * _dxinv);
42 Tx from_bin(
int i)
const 45 return i / _dxinv + _xmin;
66 bool operator!=(
const iterator& other)
const {
return i != other.i; }
68 auto operator*() {
return tbl[i]; }
92 bool operator!=(
const const_iterator& other)
const {
return i != other.i; }
94 auto operator*()
const {
return tbl[i]; }
119 setResolution(dx, xmin,
xmax);
122 void setResolution(Tx dx, Tx xmin, Tx
xmax = std::numeric_limits<Tx>::infinity())
127 if (
xmax < std::numeric_limits<Tx>::infinity()) {
135 for (
auto& i : vec) {
141 const std::vector<Ty>&
yvec()
const {
return vec; }
143 std::vector<Tx> xvec()
const 146 v.reserve(vec.size());
147 for (
size_t i = 0; i < vec.size(); i++) {
148 v.push_back(from_bin(i));
153 void clear() { vec.clear(); }
155 size_t size()
const {
return vec.size(); }
157 bool empty()
const {
return vec.empty(); }
159 Tx dx()
const {
return 1 / _dxinv; }
161 Tx
xmin()
const {
return _xmin; }
165 return (vec.empty()) ? _xmin : from_bin(vec.size() - 1);
171 int i =
to_bin(x) + offset;
172 if (i >= vec.size()) {
173 vec.resize(i + 1, Ty());
178 std::pair<Tx, Ty&> operator[](
size_t index)
181 assert(index < size());
182 return {from_bin(index), vec[index]};
185 std::pair<Tx, const Ty&> operator[](
size_t index)
const 187 assert(size() > index);
188 return {from_bin(index), vec[index]};
191 const Ty& operator()(Tx x)
const 194 int i =
to_bin(x) + offset;
195 assert(i < vec.size());
200 std::function<void(std::ostream&, Tx, Ty)> stream_decorator =
nullptr;
202 friend std::ostream& operator<<(std::ostream& o,
207 if (tbl.stream_decorator ==
nullptr) {
208 o << d.first <<
" " << d.second <<
"\n";
211 tbl.stream_decorator(o, d.first, d.second);
216 auto& operator<<(std::istream& in)
218 assert(stream_decorator ==
nullptr &&
"you probably don't want to load a decorated file");
223 while (std::getline(in, line)) {
224 std::stringstream o(line);
231 throw std::runtime_error(
"table load error: x smaller than xmin");
239 TEST_CASE(
"[Faunus] Equidistant2DTable")
241 using doctest::Approx;
246 CHECK_EQ(y.
xmin(), Approx(-3.0));
247 CHECK_EQ(y.dx(), Approx(0.5));
249 CHECK_EQ(y(-2.5), Approx(0.15));
251 CHECK_EQ(y(-3), Approx(0.11));
253 CHECK_EQ(y(0.5), Approx(0.3));
255 CHECK_EQ(y(1.5), Approx(0.5));
256 CHECK_EQ(y.
xmax(), Approx(1.5));
259 SUBCASE(
"lower bound")
262 CHECK_EQ(y.
xmin(), Approx(-3.0));
263 CHECK_EQ(y.dx(), Approx(0.5));
265 CHECK_EQ(y(-3.0), Approx(0.15));
267 CHECK_EQ(y(-3), Approx(0.11));
269 CHECK_EQ(y(0.0), Approx(0.3));
271 CHECK_EQ(y(1.0), Approx(0.5));
272 CHECK_EQ(y.
xmax(), Approx(1.0));
Definition: equidistant_table.h:49
Tint to_bin(T x, T dx=1)
Round a floating point to integer for use with histogram binning.
Definition: auxiliary.h:118
double sumy() const
sum all y-values
Definition: equidistant_table.h:132
Definition: equidistant_table.h:25
Cell list class templates.
Definition: actions.cpp:11
Tx xmax() const
maximum stored x-value
Definition: equidistant_table.h:163
Definition: equidistant_table.h:75
Equidistant2DTable(Tx dx, Tx xmin, Tx xmax=std::numeric_limits< Tx >::infinity())
Constructor.
Definition: equidistant_table.h:117
Tx xmin() const
minimum x-value
Definition: equidistant_table.h:161
const std::vector< Ty > & yvec() const
vector with y-values
Definition: equidistant_table.h:141