Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0d33ba9806+3d1aa5cd78,g0fba68d861+6c07529581,g1e78f5e6d3+aa4d1c21f1,g1ec0fe41b4+f536777771,g1fd858c14a+c6963eae98,g35bb328faa+fcb1d3bbc8,g4af146b050+58cb980876,g4d2262a081+bfae794ebc,g53246c7159+fcb1d3bbc8,g5a012ec0e7+b20b785ecb,g60b5630c4e+3d1aa5cd78,g67b6fd64d1+4086c0989b,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g87b7deb4dc+a8b896a16a,g8852436030+75f93ca278,g89139ef638+4086c0989b,g9125e01d80+fcb1d3bbc8,g94187f82dc+3d1aa5cd78,g989de1cb63+4086c0989b,g9f33ca652e+94dd9a85be,g9f7030ddb1+3f50642ad9,ga2b97cdc51+3d1aa5cd78,ga44b1db4f6+7d2d5e68ea,gabe3b4be73+1e0a283bba,gabf8522325+fa80ff7197,gb1101e3267+96cb2ddcf2,gb58c049af0+f03b321e39,gb89ab40317+4086c0989b,gcf25f946ba+75f93ca278,gd6cbbdb0b4+af3c3595f5,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+ed90e8109d,ge278dab8ac+d65b3c2b70,ge410e46f29+4086c0989b,gf67bdafdda+4086c0989b,gfe06eef73a+e2ab3e8e4f,v29.0.0.rc5
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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) {
57 std::set<Key> result;
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>
68 for (const auto& it : map) result.insert(it.first.get());
69 return result;
70}
71
72template <template <typename...> class Map, class Key, class Value>
74 const Map<Key, std::shared_ptr<const Value>>& map) {
76 for (const auto& it : map) result.insert(it.second);
77 return result;
78}
79
80template <template <typename...> class Map, class Key, class Value>
82 const Map<Key, std::reference_wrapper<const Value>>& map) {
84 for (const auto& it : map) result.insert(it.second);
85 return result;
86}
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
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