1#ifndef CDA_RAIL_GENERALHELPER_HPP
2#define CDA_RAIL_GENERALHELPER_HPP
32template <std::
floating_po
int T>
33[[nodiscard]]
bool approx_equal(T
const a, T
const b, T
const factor = 10) {
35 return std::abs(a - b) < factor * std::numeric_limits<T>::epsilon();
52 double const tol =
EPS) {
54 if (std::abs(val) < tol) {
70 double const tolerance) {
72 auto factor = 1.0 / tolerance;
73 if (
const auto rounded = std::round(factor);
74 std::abs(factor - rounded) <
GRB_EPS) {
77 return std::round(value * factor) / factor;
92[[nodiscard]] std::vector<cda_rail::index_vector>
102[[nodiscard]] std::vector<std::pair<size_t, size_t>>
115void initialize_plog(
bool debug_input,
bool overwrite_severity =
false);
133[[nodiscard]]
double relu(
double val);
135template <
typename Clock,
typename Duration1,
typename Duration2>
137 std::chrono::time_point<Clock, Duration1>
const& start,
138 std::chrono::time_point<Clock, Duration2>
const& end) {
139 return static_cast<double>(
140 std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
void throw_if_non_positive(double const value, double tolerance, std::string_view const name)
Ensures a value is strictly greater than a tolerance.
Definition CustomExceptions.hpp:360
void throw_if_negative(double const value, std::string_view const name)
Validates that a value is non-negative.
Definition CustomExceptions.hpp:349
Definition CustomExceptions.hpp:15
size_t get_first_time_index_after(double t, double dt, bool t_inclusive)
return the index of the first time step after t
Definition GeneralHelper.cpp:158
bool is_directory_and_create(const std::filesystem::path &p)
Ensures that a filesystem path exists as a directory.
Definition GeneralHelper.cpp:99
std::vector< std::pair< size_t, size_t > > subsets_of_size_2_indices(size_t n)
Generates all unordered index pairs from {0, 1, ..., n - 1}.
Definition GeneralHelper.cpp:73
double get_last_time_step_before(double t, double dt, bool t_inclusive)
return last time step before t
Definition GeneralHelper.cpp:115
constexpr double EPS
Definition Definitions.hpp:26
double get_time_difference_in_seconds(std::chrono::time_point< Clock, Duration1 > const &start, std::chrono::time_point< Clock, Duration2 > const &end)
Definition GeneralHelper.hpp:136
std::vector< cda_rail::index_vector > subsets_of_size_k_indices(size_t n, size_t k)
Generates all subsets of size k of the index set {0, 1, ..., n - 1}.
Definition GeneralHelper.cpp:24
bool approx_equal(T const a, T const b, T const factor=10)
Determines if two floating-point values are approximately equal using a machine-epsilon-scaled tolera...
Definition GeneralHelper.hpp:33
size_t get_last_time_index_before(double t, double dt, bool t_inclusive)
return the index of the last time step before t
Definition GeneralHelper.cpp:143
double relu(double val)
Returns the positive part of a scalar.
Definition GeneralHelper.cpp:114
constexpr double GRB_EPS
Definition Definitions.hpp:27
void initialize_plog(bool debug_input, bool overwrite_severity=false)
Initializes the global plog console logger and adjusts its severity.
Definition GeneralHelper.cpp:89
double round_to_given_tolerance(double const value, double const tolerance)
Rounds a value to the nearest multiple of a given tolerance.
Definition GeneralHelper.hpp:69
void round_small_numbers_to_zero_inplace(double &val, double const tol=EPS)
Sets a value to zero if its absolute magnitude is less than a tolerance threshold.
Definition GeneralHelper.hpp:51
double get_first_time_step_after(double t, double dt, bool t_inclusive)
return first time step after t
Definition GeneralHelper.cpp:122