BSMPT 3.1.14
BSMPT - Beyond the Standard Model Phase Transitions : A C++ package for the computation of the EWPT in BSM models
Loading...
Searching...
No Matches
utility.h
Go to the documentation of this file.
1// Copyright (C) 2020 Philipp Basler, Margarete Mühlleitner and Jonas Müller
2// SPDX-FileCopyrightText: 2021 Philipp Basler, Margarete Mühlleitner and Jonas
3// Müller
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6
7#pragma once
8
9#include <BSMPT/config.h>
10#include <algorithm>
11#include <complex>
12#include <functional>
13#include <gsl/gsl_integration.h>
14#include <iostream>
15#include <numeric>
16#include <random>
17#include <string>
18#include <unordered_map>
19#include <vector>
20
21#ifdef Boost_FOUND
22#include <boost/version.hpp>
23#if BOOST_VERSION >= 107200
24#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
25#else
26#include <boost/math/interpolators/cubic_b_spline.hpp>
27#endif
28#endif
29
33namespace BSMPT
34{
35
42template <typename key, typename value>
43std::unordered_map<value, key>
44InvertMap(const std::unordered_map<key, value> &originalMap,
45 const std::string &errorOnDuplicateValue)
46{
47 std::unordered_map<value, key> result;
48 for (const auto &[orig_key, orig_value] : originalMap)
49 {
50 auto success = result.emplace(orig_value, orig_key);
51 if (not success.second)
52 {
53 throw std::runtime_error(errorOnDuplicateValue);
54 }
55 }
56
57 return result;
58}
59
63bool StringStartsWith(const std::string &str, const std::string &prefix);
64
71bool StringEndsWith(const std::string &str, const std::string &suffix);
72
76const std::string sep = "\t";
77
81int factorial(const int &a);
82
86template <typename T>
87std::vector<T> push_back(std::vector<T> &a, const std::vector<T> &b)
88{
89 return a.insert(a.end(), b.begin(), b.end());
90}
91
95template <typename T> std::string vec_to_string(const std::vector<T> &vec)
96{
97 std::string res;
98 bool first = true;
99 for (const auto &el : vec)
100 {
101 if (not first)
102 {
103 res += sep + std::to_string(el);
104 }
105 else
106 {
107 res = std::to_string(el);
108 first = false;
109 }
110 }
111 return res;
112}
113
117std::vector<std::string> split(const std::string &str, char delimiter);
118
122template <typename T>
123std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec)
124{
125 bool first = true;
126 for (const auto &el : vec)
127 {
128 if (not first)
129 {
130 os << sep;
131 }
132 else
133 {
134 first = false;
135 }
136 os << el;
137 }
138 return os;
139}
140
144template <typename T>
145std::vector<T> operator+(const std::vector<T> &a, const std::vector<T> &b)
146{
147 if (a.size() != b.size())
148 throw std::runtime_error(
149 "Vector cannot be added. Must have the same size.");
150 std::vector<T> result;
151 result.reserve(a.size());
152
153 std::transform(a.begin(),
154 a.end(),
155 b.begin(),
156 std::back_inserter(result),
157 std::plus<T>());
158 return result;
159}
160
164template <typename T>
165std::vector<T> operator-(const std::vector<T> &a, const std::vector<T> &b)
166{
167 if (a.size() != b.size())
168 throw("Vector cannot be subtracted. Must have the same size.");
169
170 std::vector<T> result;
171 result.reserve(a.size());
172
173 std::transform(a.begin(),
174 a.end(),
175 b.begin(),
176 std::back_inserter(result),
177 std::minus<T>());
178 return result;
179}
180
184template <typename T, typename T2>
185std::vector<T> operator*(const T2 &a, const std::vector<T> &b)
186{
187 std::vector<T> result;
188 result.reserve(b.size());
189
190 std::transform(b.begin(),
191 b.end(),
192 std::back_inserter(result),
193 [&a](T i) { return a * i; });
194 return result;
195}
196
200template <typename T, typename T2>
201std::vector<T> operator/(const std::vector<T> &a, const T2 &b)
202{
203 std::vector<T> result;
204 result.reserve(a.size());
205
206 std::transform(a.begin(),
207 a.end(),
208 std::back_inserter(result),
209 [&b](T i) { return i / b; });
210 return result;
211}
212
216template <typename T>
217T operator*(const std::vector<T> &a, const std::vector<T> &b)
218{
219 if (a.size() != b.size())
220 throw(
221 "Dot product between vectors cannot be done. Must have the same size.");
222
223 std::vector<T> result;
224 result.reserve(a.size());
225
226 std::transform(a.begin(),
227 a.end(),
228 b.begin(),
229 std::back_inserter(result),
230 [](T i, T j) { return (i * j); });
231
232 T result1 = std::accumulate(result.begin(), result.end(), 0.0);
233
234 return result1;
235}
236
240template <typename T>
241std::vector<T> operator*(const std::vector<std::vector<T>> &a,
242 const std::vector<T> &b)
243{
244 if (a.size() != b.size())
245 throw("Multiplication of matrix with vector cannot be done. Must have the "
246 "same size.");
247
248 std::vector<T> result;
249 result.reserve(a.size());
250
251 std::transform(a.begin(),
252 a.end(),
253 std::back_inserter(result),
254 [&](std::vector<T> i) { return (i * b); });
255
256 return result;
257}
258
262template <typename T>
263std::vector<T> flatten(std::vector<std::vector<T>> const &vec)
264{
265 std::vector<T> flattened;
266 for (auto const &v : vec)
267 {
268 flattened.insert(flattened.end(), v.begin(), v.end());
269 }
270 return flattened;
271}
272
278double L2NormVector(const std::vector<double> &vec);
279
285std::vector<std::vector<double>>
286Transpose(const std::vector<std::vector<double>> &A);
287
296double Li2(const double &x);
297
311double EllipIntSecond(const double &x);
312
313#ifdef Boost_FOUND
314#if BOOST_VERSION >= 107200
315template <typename T>
316using boost_cubic_b_spline =
317 boost::math::interpolators::cardinal_cubic_b_spline<T>;
318#else
319template <typename T>
320using boost_cubic_b_spline = boost::math::cubic_b_spline<T>;
321#endif
322#endif
323
334bool almost_the_same(const double &a,
335 const double &b,
336 const double &rel_precision,
337 const double &num_zero = 1e-10);
338
349bool almost_the_same(const std::complex<double> &a,
350 const std::complex<double> &b,
351 const double &rel_precision,
352 const double &num_zero = 1e-10);
353
366bool almost_the_same(const std::vector<double> &a,
367 const std::vector<double> &b,
368 const bool &allow_for_sign_flip,
369 const double &rel_precision,
370 const double &num_zero = 1e-10);
371
372} // namespace BSMPT
This classes calculates the Bounce action of the potential with a set temperature.
Definition CalculateEtaInterface.h:24
std::vector< T > operator*(const T2 &a, const std::vector< T > &b)
multiplication of vector with scalar
Definition utility.h:185
std::vector< T > operator+(const std::vector< T > &a, const std::vector< T > &b)
vector addition
Definition utility.h:145
std::vector< T > operator-(const std::vector< T > &a, const std::vector< T > &b)
vector subtraction
Definition utility.h:165
std::unordered_map< value, key > InvertMap(const std::unordered_map< key, value > &originalMap, const std::string &errorOnDuplicateValue)
Inverts a map.
Definition utility.h:44
bool almost_the_same(const double &a, const double &b, const double &rel_precision, const double &num_zero=1e-10)
almost_the_same check if two doubles
Definition utility.cpp:119
std::vector< T > operator/(const std::vector< T > &a, const T2 &b)
division of vector by scalar
Definition utility.h:201
std::string vec_to_string(const std::vector< T > &vec)
vector to_string
Definition utility.h:95
bool StringStartsWith(const std::string &str, const std::string &prefix)
StringStartsWith checks if str starts with prefix.
Definition utility.cpp:33
int factorial(const int &a)
factorial function
Definition utility.cpp:38
std::vector< T > flatten(std::vector< std::vector< T > > const &vec)
flatten matrix
Definition utility.h:263
double L2NormVector(const std::vector< double > &vec)
L2NormVector.
Definition utility.cpp:43
std::vector< T > push_back(std::vector< T > &a, const std::vector< T > &b)
push back vector into vector
Definition utility.h:87
double Li2(const double &x)
Dilogarithm of x.
Definition utility.cpp:71
const std::string sep
seperator used to write into output files
Definition utility.h:76
std::vector< std::vector< double > > Transpose(const std::vector< std::vector< double > > &A)
Calculates the tranpose of a matrix.
Definition utility.cpp:55
bool StringEndsWith(const std::string &str, const std::string &suffix)
StringEndsWith tests if str ends with suffix.
Definition utility.cpp:91
std::ostream & operator<<(std::ostream &os, const StatusNLOStability &status)
Override << operator to handle StatusNLOStability.
Definition minimum_tracer.cpp:30
double EllipIntSecond(const double &x)
Incomplete elliptic integral of the second kind of x with a different parameterization and k^2 = -2.
Definition utility.cpp:97
std::vector< std::string > split(const std::string &str, char delimiter)
split string separated by delimiter into substrings
Definition utility.cpp:20