BSMPT 3.1.3
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 <functional>
12#include <gsl/gsl_integration.h>
13#include <iostream>
14#include <numeric>
15#include <random>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20#ifdef Boost_FOUND
21#include <boost/version.hpp>
22#if BOOST_VERSION >= 107200
23#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
24#else
25#include <boost/math/interpolators/cubic_b_spline.hpp>
26#endif
27#endif
28
32namespace BSMPT
33{
34
41template <typename key, typename value>
42std::unordered_map<value, key>
43InvertMap(const std::unordered_map<key, value> &originalMap,
44 const std::string &errorOnDuplicateValue)
45{
46 std::unordered_map<value, key> result;
47 for (const auto &[orig_key, orig_value] : originalMap)
48 {
49 auto success = result.emplace(orig_value, orig_key);
50 if (not success.second)
51 {
52 throw std::runtime_error(errorOnDuplicateValue);
53 }
54 }
55
56 return result;
57}
58
62bool StringStartsWith(const std::string &str, const std::string &prefix);
63
70bool StringEndsWith(const std::string &str, const std::string &suffix);
71
75const std::string sep = "\t";
76
80int factorial(const int &a);
81
85template <typename T>
86std::vector<T> push_back(std::vector<T> &a, const std::vector<T> &b)
87{
88 return a.insert(a.end(), b.begin(), b.end());
89}
90
94template <typename T> std::string vec_to_string(const std::vector<T> &vec)
95{
96 std::string res;
97 bool first = true;
98 for (const auto &el : vec)
99 {
100 if (not first)
101 {
102 res += sep + std::to_string(el);
103 }
104 else
105 {
106 res = std::to_string(el);
107 first = false;
108 }
109 }
110 return res;
111}
112
116std::vector<std::string> split(const std::string &str, char delimiter);
117
121template <typename T>
122std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec)
123{
124 bool first = true;
125 for (const auto &el : vec)
126 {
127 if (not first)
128 {
129 os << sep;
130 }
131 else
132 {
133 first = false;
134 }
135 os << el;
136 }
137 return os;
138}
139
143template <typename T>
144std::vector<T> operator+(const std::vector<T> &a, const std::vector<T> &b)
145{
146 if (a.size() != b.size())
147 throw std::runtime_error(
148 "Vector cannot be added. Must have the same size.");
149 std::vector<T> result;
150 result.reserve(a.size());
151
152 std::transform(a.begin(),
153 a.end(),
154 b.begin(),
155 std::back_inserter(result),
156 std::plus<T>());
157 return result;
158}
159
163template <typename T>
164std::vector<T> operator-(const std::vector<T> &a, const std::vector<T> &b)
165{
166 if (a.size() != b.size())
167 throw("Vector cannot be subtracted. Must have the same size.");
168
169 std::vector<T> result;
170 result.reserve(a.size());
171
172 std::transform(a.begin(),
173 a.end(),
174 b.begin(),
175 std::back_inserter(result),
176 std::minus<T>());
177 return result;
178}
179
183template <typename T, typename T2>
184std::vector<T> operator*(const T2 &a, const std::vector<T> &b)
185{
186 std::vector<T> result;
187 result.reserve(b.size());
188
189 std::transform(b.begin(),
190 b.end(),
191 std::back_inserter(result),
192 [&a](T i) { return a * i; });
193 return result;
194}
195
199template <typename T, typename T2>
200std::vector<T> operator/(const std::vector<T> &a, const T2 &b)
201{
202 std::vector<T> result;
203 result.reserve(a.size());
204
205 std::transform(a.begin(),
206 a.end(),
207 std::back_inserter(result),
208 [&b](T i) { return i / b; });
209 return result;
210}
211
215template <typename T>
216T operator*(const std::vector<T> &a, const std::vector<T> &b)
217{
218 if (a.size() != b.size())
219 throw(
220 "Dot product between vectors cannot be done. Must have the same size.");
221
222 std::vector<T> result;
223 result.reserve(a.size());
224
225 std::transform(a.begin(),
226 a.end(),
227 b.begin(),
228 std::back_inserter(result),
229 [](T i, T j) { return (i * j); });
230
231 T result1 = std::accumulate(result.begin(), result.end(), 0.0);
232
233 return result1;
234}
235
239template <typename T>
240std::vector<T> operator*(const std::vector<std::vector<T>> &a,
241 const std::vector<T> &b)
242{
243 if (a.size() != b.size())
244 throw("Multiplication of matrix with vector cannot be done. Must have the "
245 "same size.");
246
247 std::vector<T> result;
248 result.reserve(a.size());
249
250 std::transform(a.begin(),
251 a.end(),
252 std::back_inserter(result),
253 [&](std::vector<T> i) { return (i * b); });
254
255 return result;
256}
257
261template <typename T>
262std::vector<T> flatten(std::vector<std::vector<T>> const &vec)
263{
264 std::vector<T> flattened;
265 for (auto const &v : vec)
266 {
267 flattened.insert(flattened.end(), v.begin(), v.end());
268 }
269 return flattened;
270}
271
277double L2NormVector(const std::vector<double> &vec);
278
284std::vector<std::vector<double>>
285Transpose(const std::vector<std::vector<double>> &A);
286
295double Li2(const double &x);
296
310double EllipIntSecond(const double &x);
311
312#ifdef Boost_FOUND
313#if BOOST_VERSION >= 107200
314template <typename T>
315using boost_cubic_b_spline =
316 boost::math::interpolators::cardinal_cubic_b_spline<T>;
317#else
318template <typename T>
319using boost_cubic_b_spline = boost::math::cubic_b_spline<T>;
320#endif
321#endif
322
323} // 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:184
std::vector< T > operator+(const std::vector< T > &a, const std::vector< T > &b)
vector addition
Definition utility.h:144
std::vector< T > operator-(const std::vector< T > &a, const std::vector< T > &b)
vector subtraction
Definition utility.h:164
std::unordered_map< value, key > InvertMap(const std::unordered_map< key, value > &originalMap, const std::string &errorOnDuplicateValue)
Inverts a map.
Definition utility.h:43
std::vector< T > operator/(const std::vector< T > &a, const T2 &b)
division of vector by scalar
Definition utility.h:200
std::string vec_to_string(const std::vector< T > &vec)
vector to_string
Definition utility.h:94
bool StringStartsWith(const std::string &str, const std::string &prefix)
StringStartsWith checks if str starts with prefix.
Definition utility.cpp:32
int factorial(const int &a)
factorial function
Definition utility.cpp:37
std::vector< T > flatten(std::vector< std::vector< T > > const &vec)
flatten matrix
Definition utility.h:262
double L2NormVector(const std::vector< double > &vec)
L2NormVector.
Definition utility.cpp:42
std::vector< T > push_back(std::vector< T > &a, const std::vector< T > &b)
push back vector into vector
Definition utility.h:86
double Li2(const double &x)
Dilogarithm of x.
Definition utility.cpp:70
const std::string sep
seperator used to write into output files
Definition utility.h:75
std::vector< std::vector< double > > Transpose(const std::vector< std::vector< double > > &A)
Calculates the tranpose of a matrix.
Definition utility.cpp:54
bool StringEndsWith(const std::string &str, const std::string &suffix)
StringEndsWith tests if str ends with suffix.
Definition utility.cpp:90
std::ostream & operator<<(std::ostream &os, const StatusNLOStability &status)
Override << operator to handle StatusNLOStability.
Definition minimum_tracer.cpp:18
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:96
std::vector< std::string > split(const std::string &str, char delimiter)
split string separated by delimiter into substrings
Definition utility.cpp:19