LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#ifndef LSST_GAUSS2D_FIT_UTIL_H
2#define LSST_GAUSS2D_FIT_UTIL_H
3
4#include <algorithm>
5#include <memory>
6#include <set>
7#include <sstream>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12namespace lsst::gauss2d::fit {
13
19template <typename T>
21 const bool isclose;
22 const double diff_abs;
23 const T margin;
24
25 std::string str() const {
27 ss.precision(5);
28 ss << "isclose=" << isclose << " from diff=" << diff_abs << " <= margin=" << margin;
29 return ss.str();
30 }
31};
32
47template <typename T>
48IsCloseResult<T> isclose(T a, T b, T rtol = 1e-5, T atol = 1e-8) {
49 const T diff_abs = std::abs(a - b);
50 const T margin = atol + rtol * std::abs(b);
51 return IsCloseResult<T>{diff_abs <= margin, diff_abs, margin};
52}
53
54// The rest of these functions are mainly intended to make printing container members easier
55template <template <typename...> class Map, class Key, class Value>
56std::set<Key> map_keys(const Map<Key, Value>& map) {
58 for (const auto& it : map) {
59 result.insert(it.first);
60 }
61 return result;
62}
63
64template <template <typename...> class Map, class Key, class Value>
71
72template <template <typename...> class Map, class Key, class Value>
79
80template <template <typename...> class Map, class Key, class Value>
87
88// Builds a new vector with only unique elements, preserving order
89// Note: std::unique only removes consecutive identical elements
90// Also, this returns a new vector, potentially copying elements
91template <typename T>
93 std::set<T> set{};
94 std::vector<T> rval{};
95 rval.reserve(vec.size());
96 for (const auto& elem : vec) {
97 auto result = set.insert(elem);
98 if (result.second) rval.push_back(elem);
99 }
100 return rval;
101}
102
103/*
104template <typename T>
105std::string str_ptr(const T* obj) {
106 return (obj == nullptr) ? "None" : obj->str();
107}
108
109template <typename T>
110std::string str_iter_ptr(const T& container) {
111 std::string str = "[";
112 for (const auto& obj : container) str += str_ptr(obj) + ",";
113 return str.substr(0, str.size() - 1) + "]";
114}
115
116*/
117template <template <typename...> class Container, class Value>
118Container<Value> head_iter(const Container<Value>& container, size_t n) {
119 return Container<Value>(container.begin(), container.begin() + n);
120}
121
122template <template <typename...> class Container, class Value>
123Container<Value> tail_iter(const Container<Value>& container, size_t n) {
124 return Container<Value>(container.end() - n, container.end());
125}
126
127} // namespace lsst::gauss2d::fit
128
129#endif
py::object result
Definition _schema.cc:429
table::Key< int > b
T insert(T... args)
std::set< std::reference_wrapper< const Key > > map_keys_ref_const(const Map< std::reference_wrapper< const Key >, Value, std::less< const Key > > &map)
Definition util.h:65
std::set< std::shared_ptr< const Value > > map_values_shared_ptr_const(const Map< Key, std::shared_ptr< const Value > > &map)
Definition util.h:73
IsCloseResult< T > isclose(T a, T b, T rtol=1e-5, T atol=1e-8)
Check if two values are close, within some tolerances.
Definition util.h:48
Container< Value > head_iter(const Container< Value > &container, size_t n)
Definition util.h:118
std::set< Key > map_keys(const Map< Key, Value > &map)
Definition util.h:56
Container< Value > tail_iter(const Container< Value > &container, size_t n)
Definition util.h:123
std::vector< T > nonconsecutive_unique(const std::vector< T > &vec)
Definition util.h:92
std::set< std::reference_wrapper< const Value > > map_values_ref_const(const Map< Key, std::reference_wrapper< const Value > > &map)
Definition util.h:81
T precision(T... args)
T reserve(T... args)
T size(T... args)
T str(T... args)
The result of an isclose() check.
Definition util.h:20
std::string str() const
Definition util.h:25