ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
Namespaces | Classes | Functions | Variables
qip Namespace Reference

qip library: A collection of useful functions More...

Namespaces

 overloads
 namespace qip::overloads provides operator overloads for std::vector
 

Classes

class  StrideIterator
 An iterator accounting for a stride. More...
 
class  ConstStrideIterator
 A constant iterator accounting for a stride. More...
 
class  ArrayView
 A view onto a 1D array; used for rows/collumns of ND array. Can have a stride. More...
 
struct  less_abs
 Function object for performing comparisons of absolute values (uses std::abs). Works similarly to std::less. More...
 
struct  StrongType
 A light-weight easy-to-use single-file header-only template class for strong typing. More...
 
class  Comparison
 Helper template for comparisons. Derive from this to provide !=,>,<=,>=, given == and <. More...
 
class  Arithmetic
 Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=, -=, *=, /=. More...
 
class  Arithmetic2
 Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=, -=, *=, /=. Works for two different types. More...
 

Functions

template<typename T , typename... Args>
product (T first, Args... rest)
 Variadic product - helper function.
 
template<std::size_t N>
void NDrange_impl (std::vector< std::array< std::size_t, N >> &result, std::array< std::size_t, N > &current, const std::array< std::size_t, N > &maxValues, std::size_t index)
 
template<typename... Args>
auto NDrange (std::size_t first, Args... rest)
 Variadic array of all possible indexes. More...
 
template<typename T , typename... Args>
max (T first, Args... rest)
 Returns maximum of any number of parameters (variadic function)
 
template<typename T , typename... Args>
min (T first, Args... rest)
 Returns minimum of any number of parameters (variadic function)
 
template<typename T , typename... Args>
max_abs (T first, Args... rest)
 Returns value with maximum absolute value of any number of parameters (variadic function)
 
template<typename T , typename... Args>
min_abs (T first, Args... rest)
 Returns value with minimum absolute value of any number of parameters (variadic function)
 
template<typename T , typename... Args>
max_difference (T first, Args... rest)
 Returns max{args..} - min{args...}, for any number of args (variadic)
 
template<int n, typename T >
constexpr auto pow (T x)
 x^n for integer n (n compile-time template parameter), x any arithmetic type (T). Returns double for inverse powers, T otherwise
 
template<typename T >
constexpr T pow (T x, int n)
 x^n for integer n (runtime n), x any floating point type (T).
 
template<typename T >
constexpr int sign (T value)
 Returns sign of value. Note: sign(0)==0.
 
template<typename T >
constexpr T clip (T value, T max_abs)
 Clips value to between -max <= value <= max clip(x,max) : |x| > max, ret max; if |x|<-max, -max; else x.
 
template<typename T >
constexpr T chop (T value, T min_abs)
 Sets values |v|<min to zero; if |v|>=min, returns v.
 
template<typename Function , typename Real >
std::pair< Real, Real > derivative (Function y, Real x, Real delta_target=Real{1.0e-6}, Real dx=Real{0.01}, unsigned it_limit=250)
 Slow, but accurate, method of finding derivative of function (y) at a point (x). Returns derivative + error estimate. More...
 
template<typename Function , typename Real >
std::pair< Real, Real > Newtons (Function f, Real x, Real delta_target=Real{1.0e-6}, Real dx=Real{0.01}, unsigned it_limit=250)
 Solve f(x) = 0 for x using Newtons method. Returns root + error estimate/. More...
 
template<typename Function , typename Real >
std::pair< Real, Real > Newtons (Function f, Real x, std::pair< Real, Real > bounds, Real delta_target=Real{1.0e-6}, Real dx=Real{0.01}, unsigned it_limit=250)
 Solve f(x) = 0 for x using Newtons method. Returns root + error estimate. Enforced to be between bounds.
 
std::string omp_details ()
 
std::string random_string (std::size_t length)
 
std::string fstring (const std::string format,...)
 Returns a formatted std::string, with formatting printf-like commands. Note: maximum string lenth is 256 characters. If longer string required, use provided overload.
 
std::string fstring (const std::size_t size, const std::string format,...)
 Overload: size is maximum string length (buffer size).
 
bool wildcard_compare (std::string_view s1, std::string_view s2)
 Compares two strings, s1 and s2. s2 may contain ONE wildcard ('*') which will match anything.
 
char tolower (char ch)
 return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
 
bool ci_compare (std::string_view s1, std::string_view s2)
 Case insensitive string compare. Essentially: LowerCase(s1)==LowerCase(s2)
 
bool ci_wc_compare (std::string_view s1, std::string_view s2)
 Compares two strings, s1 and s2. s2 may contain ONE wildcard ('*') which will match anything. Case Insensitive version.
 
auto Levenstein (std::string_view a, std::string_view b)
 A simple non-optimised implementation of the Levenshtein distance.
 
auto ci_Levenstein (std::string_view a, std::string_view b)
 A simple non-optimised implementation of the Levenshtein distance (case insensitive)
 
auto closest_match (std::string_view test_string, const std::vector< std::string > &list)
 Finds the closest match in list to test_string (return iterator)
 
auto ci_closest_match (std::string_view test_string, const std::vector< std::string > &list)
 Finds the closest match (case insensitive) in list to test_string (return iterator)
 
bool string_is_integer (std::string_view s)
 Checks if a string-like s is integer-like (including -) More...
 
std::vector< std::string > split (const std::string &s, char delim=' ')
 Splits a string by delimeter into a vector.
 
std::string concat (const std::vector< std::string > &v, const std::string &delim="")
 Takes vector of strings, concats into single string, with optional delimeter.
 
std::string wrap (const std::string &input, std::size_t at=80, const std::string &prefix="")
 Wraps the string, 'input', at line 'at'. Optionally appends a prefix 'prefix' to each line. Does not split words (if can be avoided)
 
std::string int_to_roman (int a)
 Converts integer, a, to Roman Numerals. Assumed that |a|<=4000.
 
template<typename T , typename... Args>
std::vector< T > merge (std::vector< T > first, const std::vector< T > &second, const Args &...rest)
 Merges a number of vectors: {a,b,c},{d,e},{f} -> {a,b,c,d,e,f}.
 
template<typename T >
auto compare (const std::vector< T > &first, const std::vector< T > &second)
 Directly compare two arithmetic vectors of the same type and length. Returns pair {delta, itr} where delta = |max|{first - second}, itr is iterator to position in first vector where the maximum delta occured. Note: Maximum is by magnitude, but delta is signed as (first-second)
 
template<typename T , typename U , typename Func >
auto compare (const std::vector< T > &first, const std::vector< U > &second, Func &func)
 Compares two vectors of the same length, according to the rule given by func. Returns pair {delta, itr} where delta = max{|func(first,second)|}, itr is iterator to position in first vector where the maximum delta occured. Note: Maximum is by magnitude.
 
template<typename T >
auto compare_eps (const std::vector< T > &first, const std::vector< T > &second)
 Compares values of two arithmetic vectors of the same type and length, relative to second value. Returns pair {eps, itr} where eps = |max|{(first - second)/second}, itr is iterator to position in first vector where the maximum eps occured. Note: Maximum is by magnitude, but eps is signed as (first-second)/second.
 
template<typename T , typename... Args>
void add (std::vector< T > *first, const std::vector< T > &second, const Args &...rest)
 Adds any number of vectors, in place (modifies first vector). Must be of same type. May allocate; will resize first to be size of largest vector.
 
template<typename T , typename... Args>
std::vector< T > add (std::vector< T > first, const std::vector< T > &second, const Args &...rest)
 Adds any number of vectors. Must be of same type. Size of returned vector will be size of largest vector; may allocate more than once if vectors are not all of same size.
 
template<typename T , typename... Args>
void multiply (std::vector< T > *first, const std::vector< T > &second, const Args &...rest)
 Multiplies any number of (arithmetic) vectors, in place (modifies first vector). Must be of same type. May allocate; will resize first to be size of largest vector.
 
template<typename T , typename... Args>
std::vector< T > multiply (std::vector< T > first, const std::vector< T > &second, const Args &...rest)
 Multiplies any number of vectors. Must be of same type. Size of returned vector will be size of largest vector; may allocate more than once if vectors are not all of same size.
 
template<typename F , typename T , typename... Args>
void compose (const F &func, std::vector< T > *first, const std::vector< T > &second, const Args &...rest)
 Composes any number of vectors, in place (modifies first vector), using the provided function. Must be of same type. May allocate; will resize first to be size of largest vector. e.g., qip::compose(std::plus{}, &vo, v2, v3); same as qip::add(&vo, v2, v3)
 
template<typename F , typename T , typename... Args>
std::vector< T > compose (const F &func, std::vector< T > first, const std::vector< T > &second, const Args &...rest)
 Composes any number of vectors. Must be of same type. Size of returned vector will be size of largest vector; may allocate more than once if vectors are not all of same size.
 
template<typename T >
void scale (std::vector< T > *vec, T x)
 In-place scalar multiplication of std::vector - types must match.
 
template<typename T >
std::vector< T > scale (std::vector< T > vec, T x)
 Scalar multiplication of std::vector - types must match.
 
template<typename T = double, typename N = std::size_t>
std::vector< T > uniform_range (T first, T last, N number)
 Produces a uniformly*(see below) distributed range of values between [first,last] with number steps. number must be at least 2. first+last are guarenteed to be the first and last points in the range. For integral T, range will not be perfectly uniform, due to [first, ..., last] guarentee and rounding; also in this case same value may appear more than once if too-many steps are requested.
 
template<typename T = double, typename N = std::size_t>
std::vector< T > logarithmic_range (T first, T last, N number)
 Produces a logarithmicly*(see below) distributed range of values between [first,last] with number steps. number must be at least 2. first+last are guarenteed to be the first and last points in the range. For integral T, range will not be perfectly logarithmic, due to [first, ..., last] guarentee and rounding; also in this case same value may appear more than once if too-many steps are requested.
 
template<typename T = double, typename N = std::size_t>
std::vector< T > loglinear_range (T first, T last, T b, N number)
 Produces a Log-Linear distributed range of values between [first,last] with number steps. number must be at least 3. first+last are guarenteed to be the first and last points in the range. T must be floating point. Range is roughly logarithmic for values below 'b', and linear for values above b. Not tested for negative values.
 
template<typename T , typename... Args>
constexpr auto multiply_at (std::size_t i, const T &first, const Args &...rest)
 first[i]*...rest[i] – used to allow inner_product
 
template<typename T , typename... Args>
constexpr auto inner_product (const T &first, const Args &...rest)
 Variadic inner product (v1,v2,...vn) : sum_i v1[i]*v2[i]*...vn[i].
 
template<typename T , typename... Args>
auto inner_product_sub (std::size_t p0, std::size_t pinf, const T &first, const Args &...rest)
 
template<typename F , typename T >
apply_to (const F &func, T list)
 
template<typename T , typename Func >
std::vector< T > select_if (const std::vector< T > &in, Func condition)
 Creates a subset of a vector, whose element match condition. By copy. condition must have function signature: bool condition(T)
 
template<typename T , typename Func >
void insert_into_if (const std::vector< T > &in, std::vector< T > *inout, Func condition)
 Inserts elements from in into inout, if condition is met.
 
template<typename T >
std::vector< T > reverse (std::vector< T > in)
 Reverses a list.
 
template<typename T >
mean (std::vector< T > vec)
 Mean: xbar = \sum_i x_i / N.
 
template<typename T >
variance (std::vector< T > vec, std::size_t dof=0)
 Variance: \sum_i (x_i-xbar)^2 / (N-dof)
 
template<typename T >
sdev (std::vector< T > vec, std::size_t dof=0)
 Standard deviation: sqrt(variance)
 
template<typename T >
sem (std::vector< T > vec, std::size_t dof=0)
 Standard deviation: sqrt(variance)
 
void progbar (int i, int max, int length=50)
 
void progbar50 (int i, int max)
 

Variables

constexpr bool use_omp = false
 

Detailed Description

qip library: A collection of useful functions

Collection of handy tools.

Function Documentation

◆ derivative()

template<typename Function , typename Real >
std::pair<Real, Real> qip::derivative ( Function  y,
Real  x,
Real  delta_target = Real{1.0e-6},
Real  dx = Real{0.01},
unsigned  it_limit = 250 
)

Slow, but accurate, method of finding derivative of function (y) at a point (x). Returns derivative + error estimate.

delta_target is target for |dy/dx_n - dy/dx_{n+1}| < delta_target (1.0e-6); dx is initial step-size used to find derivative of f (0.01); it_limit is maximum number of iterations.

◆ NDrange()

template<typename... Args>
auto qip::NDrange ( std::size_t  first,
Args...  rest 
)

Variadic array of all possible indexes.

Allows ranged for loop like: for ([i,j,k,m,...,z] : array){ array.at(i,j,k,m,...,z); } Note: not memory efficient at all! Don't use for large arrays

◆ Newtons()

template<typename Function , typename Real >
std::pair<Real, Real> qip::Newtons ( Function  f,
Real  x,
Real  delta_target = Real{1.0e-6},
Real  dx = Real{0.01},
unsigned  it_limit = 250 
)

Solve f(x) = 0 for x using Newtons method. Returns root + error estimate/.

x is be initial guess for the root; delta_target is target for |x_n - x_{n+1}| < delta_target (1.0e-6); dx is initial step-size used to find derivative of f (0.01); it_limit is maximum number of iterations.

◆ string_is_integer()

bool qip::string_is_integer ( std::string_view  s)
inline

Checks if a string-like s is integer-like (including -)

e.g., The input strings "16" and "-12" would both return 'true', while "12x" or "12.5" would not. Does this by checking if all characters are integer digits exept first character, which is allowed to be an integer, or '+' or -'-