Skip to content

Commit

Permalink
Moved all almost_the_same functions to utility.cpp/.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Borschensky Christoph committed Dec 11, 2024
1 parent 9ce1972 commit d10e35a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 92 deletions.
20 changes: 1 addition & 19 deletions include/BSMPT/minimum_tracer/minimum_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,24 +580,6 @@ Create1DimGrid(const std::vector<double> &min_start,
const std::vector<double> &min_end,
const int npoints = 100);

/**
* Returns true if two values are the same given some relative precision
*/
bool almost_the_same(const double &a,
const double &b,
const double &rel_precision = 0.01,
const double &num_zero = 1e-10);

/**
* Returns true if two vectors are the element-wise the same given some relative
* precision
*/
bool almost_the_same(const std::vector<double> &a,
const std::vector<double> &b,
const bool &allow_for_sign_flip = false,
const double &rel_precision = 0.01,
const double &num_zero = 1e-10);

/**
* @brief Phase object
*
Expand Down Expand Up @@ -950,4 +932,4 @@ struct Vacuum
void PrintPhasesDiagram(int size = 100);
};

} // namespace BSMPT
} // namespace BSMPT
7 changes: 0 additions & 7 deletions include/BSMPT/models/ClassPotentialOrigin.h
Original file line number Diff line number Diff line change
Expand Up @@ -978,13 +978,6 @@ class Class_Potential_Origin
* Ensures the correct rotation matrix convention
*/
virtual void AdjustRotationMatrix() = 0;
/**
* Returns true if two values are the same given some relative precision
*/
bool almost_the_same(double a, double b, double rel_precision = 0.01);
bool almost_the_same(std::complex<double> a,
std::complex<double> b,
double rel_precision = 0.01);
/**
* Checks whether rotation matrix is properly set after implying conventions
*/
Expand Down
23 changes: 23 additions & 0 deletions include/BSMPT/utility/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <random>
#include <string>
#include <vector>
#include <complex>


#ifdef Boost_FOUND
#include <boost/version.hpp>
Expand Down Expand Up @@ -284,6 +286,27 @@ double Li2(const double &x);
*/
double EllipIntSecond(const double &x);

/**
* @brief Checks if two double numbers are (almost) the same with a given
* relative precision; if both numbers are smaller than num_zero, then they
* are considered to be zero and the function always returns true; with
* additional versions of the function for std::complex<double> and
* std::vector<double> input.
*/
bool almost_the_same(const double &a,
const double &b,
const double &rel_precision = 0.01,
const double &num_zero = 1e-10);
bool almost_the_same(const std::complex<double> &a,
const std::complex<double> &b,
const double &rel_precision = 0.01,
const double &num_zero = 1e-10);
bool almost_the_same(const std::vector<double> &a,
const std::vector<double> &b,
const bool &allow_for_sign_flip = false,
const double &rel_precision = 0.01,
const double &num_zero = 1e-10);

/**
* @brief operator << overload for the model parameter
*/
Expand Down
46 changes: 0 additions & 46 deletions src/minimum_tracer/minimum_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1425,52 +1425,6 @@ Create1DimGrid(const std::vector<double> &min_start,
return res_vec;
}

bool almost_the_same(const double &a,
const double &b,
const double &rel_precision,
const double &num_zero)
{
if (std::abs(a) < num_zero and std::abs(b) < num_zero)
{
return true;
}
return std::abs(a - b) < std::abs(a + b) / 2 * rel_precision;
}

bool almost_the_same(const std::vector<double> &a,
const std::vector<double> &b,
const bool &allow_for_sign_flip,
const double &rel_precision,
const double &num_zero)
{
if (a.size() != b.size())
{
throw std::runtime_error("Error. Vectors must have the same size.");
}
int count_true = 0;
for (std::size_t i = 0; i < a.size(); i++)
{
if (allow_for_sign_flip)
{
count_true +=
int(almost_the_same(a.at(i), b.at(i), rel_precision, num_zero));
}
else
{
count_true += int(almost_the_same(
std::abs(a.at(i)), std::abs(b.at(i)), rel_precision, num_zero));
}
}
if (std::size_t(count_true) == a.size())
{
return true;
}
else
{
return false;
}
}

Phase::Phase()
{
}
Expand Down
20 changes: 0 additions & 20 deletions src/models/ClassPotentialOrigin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,26 +959,6 @@ Class_Potential_Origin::SecondDerivativeOfEigenvaluesNonRepeated(
return res;
}

bool Class_Potential_Origin::almost_the_same(double a,
double b,
double rel_precision)
{
if (std::abs(a) < 1e-10 and std::abs(b) < 1e-10)
{
return true;
}
return std::abs(a - b) < std::abs(a + b) / 2 * rel_precision;
}

bool Class_Potential_Origin::almost_the_same(std::complex<double> a,
std::complex<double> b,
double rel_precision)
{
bool real_part = almost_the_same(a.real(), b.real(), rel_precision);
bool imag_part = almost_the_same(a.imag(), b.imag(), rel_precision);
return (real_part and imag_part);
}

// Sanity check to make sure HiggsRotationMatrix is a proper rotation
// matrix, i.e. its inverse should correspond to its transpose, and its
// determinant should be +1 or -1
Expand Down
59 changes: 59 additions & 0 deletions src/utility/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ostream>
#include <sstream>
#include <string>
#include <complex>

/**
* @file
Expand Down Expand Up @@ -129,4 +130,62 @@ double EllipIntSecond(const double &x)
return result;
}

bool almost_the_same(const double &a,
const double &b,
const double &rel_precision,
const double &num_zero)
{
if (std::abs(a) < num_zero and std::abs(b) < num_zero)
{
return true;
}
return std::abs(a - b) < std::abs(a + b) / 2 * rel_precision;
}

bool almost_the_same(const std::complex<double> &a,
const std::complex<double> &b,
const double &rel_precision,
const double &num_zero)
{
bool real_part = almost_the_same(a.real(), b.real(), rel_precision,
num_zero);
bool imag_part = almost_the_same(a.imag(), b.imag(), rel_precision,
num_zero);
return (real_part and imag_part);
}

bool almost_the_same(const std::vector<double> &a,
const std::vector<double> &b,
const bool &allow_for_sign_flip,
const double &rel_precision,
const double &num_zero)
{
if (a.size() != b.size())
{
throw std::runtime_error("Error. Vectors must have the same size.");
}
int count_true = 0;
for (std::size_t i = 0; i < a.size(); i++)
{
if (allow_for_sign_flip)
{
count_true +=
int(almost_the_same(a.at(i), b.at(i), rel_precision, num_zero));
}
else
{
count_true += int(almost_the_same(
std::abs(a.at(i)), std::abs(b.at(i)), rel_precision, num_zero));
}
}
if (std::size_t(count_true) == a.size())
{
return true;
}
else
{
return false;
}
}

} // namespace BSMPT

0 comments on commit d10e35a

Please sign in to comment.