Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1#ifndef HYPERISO_UTILS_H
2#define HYPERISO_UTILS_H
3
4#include <algorithm>
5#include <complex>
6#include <iomanip>
7#include <iostream>
8#include <ranges>
9#include <string>
10#include <vector>
11#include <unordered_set>
12#include <unordered_map>
13#include <map>
14#include <memory>
15#include <limits>
16
35typedef std::complex<double> complex_t;
36
43const std::numeric_limits<double> nld = *new std::numeric_limits<double>;
44
55bool ends_with(const std::string& str, const std::string& suffix);
56
71std::vector<std::string> split(const std::string& s, char delimiter);
72
83std::string doubleToString(double value, int precision);
84
94std::string to_lowercase(const std::string& str);
95
107template<typename T, typename U>
108inline std::unordered_set<T> get_keys(const std::map<T, U>& map) {
109 std::unordered_set<T> keys;
110 for (const std::pair<const T, U>& item : map) {
111 keys.emplace(item.first);
112 }
113 return keys;
114}
115
124template<typename T, typename U>
125inline std::unordered_set<T> get_keys(const std::unordered_map<T, U>& map) {
126 std::unordered_set<T> keys;
127 for (const std::pair<const T, U>& item : map) {
128 keys.emplace(item.first);
129 }
130 return keys;
131}
132
144template<typename T, typename U>
145inline bool haveSameKeys(const std::map<T, U>& map1, const std::map<T, U>& map2) {
146 return get_keys(map1) == get_keys(map2);
147}
148
160template <typename T>
161void print_value(std::ostream& os, const T& value) {
162 os << value;
163}
164
175template <typename T>
176void print_value(std::ostream& os, const std::shared_ptr<T> &ptr) {
177 if (ptr)
178 os << *ptr;
179 else
180 os << "nullptr";
181}
182
200template <typename Map>
201std::enable_if_t<
202 std::is_same_v<typename Map::value_type,
203 std::pair<const typename Map::key_type, typename Map::mapped_type>>,
204 std::ostream&
205> operator<<(std::ostream& os, const Map& m) {
206 os << "{ ";
207 for (const auto& [key, value] : m) {
208 os << key << ": ";
209 print_value(os, value);
210 os << ", ";
211 }
212 if (!m.empty())
213 os.seekp(-2, std::ios_base::end); // remove ", "
214 os << " }";
215 return os;
216}
217
240template <typename T, std::size_t cache_size, typename Func, typename... Args>
241void fill_cache(Func&& f, double a, double b, std::array<T, cache_size>& cache, Args&&... args) {
242 double x_0 = (b - a) * nld.epsilon() + a;
243 cache[0] = f(x_0, std::forward<Args>(args)...);
244 for (std::size_t i = 1; i < cache_size - 1; ++i) {
245 double s = static_cast<double>(i) / static_cast<double>(cache_size - 1);
246 double x = (b - a) * s + a;
247 cache[i] = f(x, std::forward<Args>(args)...);
248 }
249 double x_1 = (b - a) * (1. - nld.epsilon()) + a;
250 cache[cache_size - 1] = f(x_1, std::forward<Args>(args)...);
251}
252
275template <typename T, typename U, std::size_t cache_size>
276T lerp(U x, const std::array<T, cache_size>& lookup, double a=0.0, double b=1.0)
277{
278 // Clamp s ∈ [0, 1]
279 x = std::clamp(static_cast<double>(x), a, b);
280 double u = (x - a) / (b - a);
281 double scaled = u * (cache_size - 1);
282 std::size_t i = static_cast<std::size_t>(std::floor(scaled));
283
284 if (i >= cache_size - 1) {
285 return lookup[cache_size - 1];
286 }
287
288 double t = scaled - static_cast<double>(i);
289 return T((1.0 - t)) * lookup[i] + T(t) * lookup[i + 1];
290}
291
292
293#endif // HYPERISO_UTILS_H
void fill_cache(Func &&f, double a, double b, std::array< T, cache_size > &cache, Args &&... args)
Fills a lookup cache for a function on a finite interval [a, b].
Definition Utils.h:241
bool ends_with(const std::string &str, const std::string &suffix)
Tests whether a string ends with a given suffix.
Definition Utils.cpp:3
std::enable_if_t< std::is_same_v< typename Map::value_type, std::pair< const typename Map::key_type, typename Map::mapped_type > >, std::ostream & > operator<<(std::ostream &os, const Map &m)
Generic stream output operator for map-like containers.
Definition Utils.h:205
const std::numeric_limits< double > nld
Numeric limits for double-precision floating point.
Definition Utils.h:43
std::vector< std::string > split(const std::string &s, char delimiter)
Splits a string into parts using a single-character delimiter.
Definition Utils.cpp:8
std::string doubleToString(double value, int precision)
Converts a double to a fixed-format string with a given precision.
Definition Utils.cpp:25
void print_value(std::ostream &os, const T &value)
Generic helper to print a value to an output stream.
Definition Utils.h:161
std::complex< double > complex_t
Convenience alias for std::complex<double>.
Definition Utils.h:35
bool haveSameKeys(const std::map< T, U > &map1, const std::map< T, U > &map2)
Checks whether two std::map instances have the same set of keys.
Definition Utils.h:145
std::unordered_set< T > get_keys(const std::map< T, U > &map)
Extracts the key set from a std::map into an unordered_set.
Definition Utils.h:108
std::string to_lowercase(const std::string &str)
Returns a lowercase copy of the input string.
Definition Utils.cpp:31
T lerp(U x, const std::array< T, cache_size > &lookup, double a=0.0, double b=1.0)
Linearly interpolates a cached function on [a, b].
Definition Utils.h:276
double T(double x)
Wilson coefficient T(x).
double f(double x)
Wilson special function f depending on x.