LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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