74#define POSITION_TRACE (std::string(__FILE__) + ":" + std::to_string(__LINE__))
84template <
class T,
class Compare = Aleph::less<T>>
85inline const T *
median(
const T &a,
const T &b,
const T &c,
const Compare &
cmp = Compare())
128 assert(i >= 0
and i <= 15);
130 const char ret = i < 10 ? i +
'0' : i - 10 +
'A';
132 assert((ret >=
'0' and ret <=
'9') or (ret >=
'A' and ret <=
'F'));
143 assert((c >=
'0' and c <=
'9') or (c >=
'A' and c <=
'F'));
145 const int ret = c <
'A' ? c -
'0' : c -
'A' + 10;
147 assert(ret >= 0
and ret <= 15);
163#define DERIVATE_ITERATOR(container_name, base_it_name, it_name) \
164 struct it_name : public base_it_name \
170 it_name(container_name &c) : base_it_name(c) \
175 it_name(const it_name &it) : base_it_name(it) \
180 it_name &operator = (const it_name &it) \
182 return base_it_name::operator = (it); \
204inline constexpr size_t u_index(
const size_t &i)
218inline constexpr size_t l_index(
const size_t i)
231 return x && !(x & (x - 1));
249 <<
"next_power_of_2: no power of 2 that a size_t can hold is >= " << x;
272 std::unique_ptr<char, void (*)(
void *)> res{abi::__cxa_demangle(name,
nullptr,
nullptr, &status),
274 return (status == 0) ? res.get() : name;
282#define CLASSNAME_TO_STRING(class_ptr) ::Aleph::demangle(typeid(*class_ptr).name())
290 std::cerr << msg <<
'\n';
301 return (stat(name.c_str(), &buffer) == 0);
310inline std::string
Rvector(
const std::string &name,
const C &c)
312 std::ostringstream s;
313 s << name <<
" <- c(";
314 auto last_ptr = &c.get_last();
315 for (
auto it = c.get_it(); it.has_curr(); it.next())
317 auto &v = it.get_curr();
338 std::ostringstream s;
339 s << c.get_first() <<
" <- c(";
340 auto last_ptr = &c.get_last();
341 for (
auto it = c.get_it(1); it.has_curr(); it.next())
343 auto &v = it.get_curr();
373 assert(x2 > x1
and x <= x2 and x >= x1);
374 return y1 + (y2 -
y1) * (x - x1) / (x2 - x1);
397 assert(x2 > x1
and x < x1);
398 return y1 - (y2 -
y1) * (x1 - x) / (x2 - x1);
404inline double pow2(
const double x)
412inline double pow3(
const double x)
437 assert(x2 > x1
and x > x2);
438 return y2 + (y2 -
y1) * (x - x2) / (x2 - x1);
449 constexpr double lim = std::numeric_limits<double>::max();
450 return std::nextafter(val, lim);
460 constexpr double lim = std::numeric_limits<double>::min();
461 return std::nextafter(val, lim);
468[[nodiscard]]
constexpr bool are_near(
const double v1,
const double v2,
const double e)
noexcept
473 const double d = v1 - v2;
474 return (d < 0.0 ? -d : d) <= e;
485inline void execute_R_script(
const std::string &scr,
const std::string &file_name =
"tmp.R")
487 std::ofstream tmp(file_name);
488 tmp <<
"#!/usr/bin/Rscript" <<
'\n'
492 <<
"message(\"Press Return To Continue\")" <<
'\n'
493 <<
"invisible(readLines(\"stdin\", n=1))" <<
'\n';
494 const std::string
cmd =
"Rscript " + file_name;
511 if constexpr (std::numeric_limits<double>::is_iec559 &&
sizeof(double) ==
sizeof(std::uint64_t))
514 std::uint64_t
bits = 0;
515 std::memcpy(&
bits, &n,
sizeof(
bits));
517 constexpr std::uint64_t exp_mask = 0x7ff0000000000000ULL;
518 constexpr std::uint64_t frac_mask = 0x000fffffffffffffULL;
520 const std::uint64_t
exp =
bits & exp_mask;
521 const std::uint64_t
frac =
bits & frac_mask;
532 if (not std::isfinite(n))
535 const int status = std::fpclassify(n);
536 return (status == FP_NORMAL or status == FP_ZERO);
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Standard functor implementations and comparison objects.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_frac_function > > frac(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
double next_value(const double val)
Return the next representable floating-point value to val
size_t next_power_of_2(size_t x)
Return the smallest power of 2 that is not less than x.
void error_msg(const std::string &msg)
Display message and abort program execution.
bool is_power_of_2(size_t x)
Taken from http://stackoverflow.com/questions/3638431/determine-if-an-int-is-a-power-of-2-or-not-in-a...
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
constexpr bool are_near(const double v1, const double v2, const double e) noexcept
Return true if v1 is within absolute distance e of v2.
double extrapolate_right(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
std::decay_t< typename HeadC::Item_Type > T
double interpolate(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear interpolation.
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
and
Check uniqueness with explicit hash + equality functors.
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
double prev_value(const double val)
Return the next representable floating-point value of val towards the smallest positive normal number...
bool is_even(const long n)
Return true if n is even.
double pow2(const double x)
Return x^2.
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
bool exists_file(const std::string &name)
Return true if it exists a file of name
bool resize_process_stack(size_t new_size)
Resize the process stack to new_size.
int char_to_nibble(const char c)
Convert a hex character in 0..9A..F to its 4-bit nibble value.
std::string Rvector(const std::string &name, const C &c)
Return a string with R specification of a vector with name and data stored in container c
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
std::string demangle(const char *name)
Given a linker symbol name generated by a c++ compiler, this functions decodes it into a user level n...
double extrapolate_left(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
double pow3(const double x)
Return x^3.
void execute_R_script(const std::string &scr, const std::string &file_name="tmp.R")
Generate and execute a R script from a string (containing the script)