|
ampsci
High-precision calculations for one- and two-valence atomic systems
|
General-purpose utility library.
Provides lightweight, header-only utilities used throughout ampsci, including:
<omp.h> to allow compilation with+without OpenMPNamespaces | |
| namespace | overloads |
| Operator overloads for std::vector. | |
Classes | |
| class | Arithmetic |
| Helper template that provides +, -, *, / given +=, -=, *=, /=. More... | |
| class | Arithmetic2 |
| Like Arithmetic, but for two different types (T op U). More... | |
| class | Array |
| N-dimensional array with arithmetic operators. More... | |
| class | ArrayView |
| Non-owning view onto a 1D contiguous or strided array segment. More... | |
| class | Comparison |
| Helper template that provides !=, >, <=, >= given == and <. More... | |
| class | ConstStrideIterator |
| Const version of StrideIterator. More... | |
| struct | less_abs |
| Function object for comparisons by absolute value. More... | |
| class | ProgressBar |
| Thread-safe progress bar for OpenMP parallel loops. More... | |
| class | StrideIterator |
| Iterator with a configurable stride. More... | |
| struct | StrongType |
| A light-weight easy-to-use single-file header-only template class for strong typing. More... | |
Functions | |
| template<typename T , typename... Args> | |
| T | 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 > ¤t, const std::array< std::size_t, N > &maxValues, std::size_t index) |
| template<typename... Args> | |
| auto | NDrange (std::size_t first, Args... rest) |
| Returns all index tuples for an N-dimensional range. | |
| template<typename T , typename... Args> | |
| T | max (T first, Args... rest) |
| Returns the maximum of any number of parameters (variadic). | |
| template<typename T , typename... Args> | |
| T | min (T first, Args... rest) |
| Returns the minimum of any number of parameters (variadic). | |
| template<typename T , typename... Args> | |
| T | max_abs (T first, Args... rest) |
| Returns the value with maximum absolute value of any number of parameters (variadic). | |
| template<typename T , typename... Args> | |
| T | min_abs (T first, Args... rest) |
| Returns the value with minimum absolute value of any number of parameters (variadic). | |
| template<typename T , typename... Args> | |
| T | max_difference (T first, Args... rest) |
| Returns max - min over any number of arguments (variadic). | |
| template<int n, typename T > | |
| constexpr auto | pow (T x) |
| x^n for compile-time integer n, x any arithmetic type. | |
| template<typename T > | |
| constexpr T | pow (T x, int n) |
| x^n for runtime integer n; T must be floating point. | |
| template<typename T > | |
| constexpr double | factorial (T x) |
| Factorial x! - takes integer, returns double. | |
| template<typename T > | |
| constexpr double | double_factorial (T x) |
| Double factorial x!! - takes integer, returns double. | |
| template<typename T > | |
| constexpr int | sign (T value) |
| Returns the sign of value. Note: sign(0) == 0. | |
| template<typename T > | |
| constexpr T | clamp_abs (T value, T max_abs) |
| Clamps value to [-max_abs, max_abs] - i.e., using abs. | |
| template<typename T > | |
| constexpr T | chop (T value, T min_abs) |
| Sets value to zero if |value| < min_abs, otherwise returns value unchanged. | |
| 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) |
| Numerical derivative of y(x) at point x; returns {dy/dx, error}. | |
| 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) |
| Solves f(x) = 0 using Newton's method; returns {root, error}. | |
| 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) |
| Solves f(x) = 0 using Newton's method with bounds; returns {root, error}. | |
| std::string | omp_details () |
| Returns a short string describing the threading status, e.g. "Using OpenMP with 8 threads." or "Single-threaded.". | |
| std::string | random_string (std::size_t length) |
| Returns a random alphanumeric string of the given length. | |
| std::string | fstring (const std::string format,...) |
| Returns a formatted string using printf-style format specifiers. | |
| std::string | fstring (const std::size_t size, const std::string format,...) |
| Overload of fstring with explicit buffer size (maximum string length). | |
| bool | wildcard_compare (std::string_view s1, std::string_view s2) |
| Compares s1 against pattern s2, where s2 may contain one wildcard '*' that matches any substring. | |
| char | tolower (char ch) |
| Conversion of a single character to lowercase. | |
| std::string | tolower (std::string t_string) |
| Returns a lowercase copy of the string. | |
| bool | contains (std::string_view the_string, std::string_view sub_string) |
| Returns true if the_string contains sub_string. | |
| bool | ci_contains (const std::string &the_string, const std::string &sub_string) |
| Returns true if the_string contains sub_string (case insensitive). | |
| bool | contains (const std::string &the_string, const std::vector< std::string > &sub_strings) |
| Returns true if the_string contains any of the sub_strings. | |
| bool | ci_contains (const std::string &the_string, const std::vector< std::string > &sub_strings) |
| Returns true if the_string contains any of the sub_strings (case insensitive). | |
| bool | ci_compare (std::string_view s1, std::string_view s2) |
| Case-insensitive string comparison; equivalent to tolower(s1) == tolower(s2). | |
| bool | ci_wc_compare (std::string_view s1, std::string_view s2) |
| Case-insensitive version of wildcard_compare. | |
| auto | Levenstein (std::string_view a, std::string_view b) |
| Returns the Levenshtein edit distance between strings a and b. | |
| auto | ci_Levenstein (std::string_view a, std::string_view b) |
| Case-insensitive version of Levenstein. | |
| auto | closest_match (std::string_view test_string, const std::vector< std::string > &list) |
| Returns an iterator to the closest match to test_string in list, using Levenstein distance. | |
| std::string | ci_closest_match (const std::string_view test_string, const std::vector< std::string > &list) |
| Returns the closest match (case insensitive) to test_string in list, using ci_Levenstein distance. | |
| bool | string_is_integer (std::string_view s) |
| Returns true if the string represents an integer. | |
| std::vector< std::string > | split (const std::string &s, char delim=' ') |
| Splits a string by delimiter into a vector of substrings. | |
| std::string | concat (const std::vector< std::string > &v, const std::string &delim="") |
| Concatenates a vector of strings into one, with an optional delimiter. | |
| std::string | wrap (const std::string &input, std::size_t at=80, const std::string &prefix="") |
| Word-wraps input at column at, optionally prefixing each line. | |
| std::string | int_to_roman (int a) |
| Converts an integer to a Roman numeral string. Assumes |a| <= 3999. | |
| template<typename T , typename... Args> | |
| std::vector< T > | merge (std::vector< T > first, const std::vector< T > &second, const Args &...rest) |
| Merges any 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) |
| Compares two arithmetic vectors element-wise; returns {max_delta, iterator}. | |
| template<typename T , typename U , typename Func > | |
| auto | compare (const std::vector< T > &first, const std::vector< U > &second, Func &func) |
| Compares two vectors using a user-supplied function; returns {max, iterator}. | |
| template<typename T > | |
| auto | compare_eps (const std::vector< T > &first, const std::vector< T > &second) |
| Compares two floating-point vectors element-wise relative to the second; returns {max_eps, iterator}. | |
| 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). | |
| 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; returns result sized to the largest input. | |
| 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). | |
| 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; returns result sized to the largest input. | |
| template<typename F , typename T , typename... Args> | |
| void | compose (const F &func, std::vector< T > *first, const std::vector< T > &second, const Args &...rest) |
| Applies func element-wise across any number of vectors in place (modifies *first). | |
| 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) |
| Applies func element-wise across any number of vectors; returns result sized to the largest input. | |
| template<typename T > | |
| void | scale (std::vector< T > *vec, T x) |
| In-place scalar multiplication of a vector; types must match. | |
| template<typename T > | |
| std::vector< T > | scale (std::vector< T > vec, T x) |
| Scalar multiplication of a vector; types must match. | |
| template<typename T = double, typename N = std::size_t> | |
| std::vector< T > | uniform_range (T first, T last, N number) |
| Returns a uniformly distributed range [first, last] with number points. | |
| template<typename T = double, typename N = std::size_t> | |
| std::vector< T > | logarithmic_range (T first, T last, N number) |
| Returns a logarithmically distributed range [first, last] with number points. | |
| template<typename T = double, typename N = std::size_t> | |
| std::vector< T > | loglinear_range (T first, T last, T b, N number) |
| Returns a log-linear distributed range [first, last] with number points. | |
| template<typename T , typename... Args> | |
| constexpr auto | multiply_at (std::size_t i, const T &first, const Args &...rest) |
| Helper: returns first[i] * rest[i] * ... at index i. | |
| template<typename T , typename... Args> | |
| constexpr auto | inner_product (const T &first, const Args &...rest) |
| Variadic inner product: 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) |
| Inner product over the subrange [p0, pinf). | |
| template<typename F , typename T > | |
| T | apply_to (const F &func, T list) |
| Applies func to each element of list in place; returns the modified list. | |
| template<typename T , typename Func > | |
| std::vector< T > | select_if (const std::vector< T > &in, Func condition) |
| Returns a copy of in containing only elements satisfying condition. | |
| 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) |
| Returns a reversed copy of the vector. | |
| template<typename T > | |
| T | mean (std::vector< T > vec) |
| Mean of a vector. | |
| template<typename T > | |
| T | variance (std::vector< T > vec, std::size_t dof=0) |
| Variance using the two-pass method. | |
| template<typename T > | |
| T | sdev (std::vector< T > vec, std::size_t dof=0) |
| Standard deviation: sqrt(variance). | |
| template<typename T > | |
| T | sem (std::vector< T > vec, std::size_t dof=0) |
| Standard error of the mean: sdev / sqrt(N). | |
| void | progbar (int i, int max, int length=50) |
| Basic progress bar. Prints new line if (and only if) i==(max-1) | |
Variables | |
| constexpr bool | use_omp = false |
| True if compiled with OpenMP support, false otherwise. | |
| T qip::product | ( | T | first, |
| Args... | rest | ||
| ) |
Variadic product - helper function.
| auto qip::NDrange | ( | std::size_t | first, |
| Args... | rest | ||
| ) |
Returns all index tuples for an N-dimensional range.
Enables iteration over every index combination, e.g.:
| T qip::max | ( | T | first, |
| Args... | rest | ||
| ) |
Returns the maximum of any number of parameters (variadic).
| T qip::min | ( | T | first, |
| Args... | rest | ||
| ) |
Returns the minimum of any number of parameters (variadic).
| T qip::max_abs | ( | T | first, |
| Args... | rest | ||
| ) |
Returns the value with maximum absolute value of any number of parameters (variadic).
| T qip::min_abs | ( | T | first, |
| Args... | rest | ||
| ) |
Returns the value with minimum absolute value of any number of parameters (variadic).
| T qip::max_difference | ( | T | first, |
| Args... | rest | ||
| ) |
Returns max - min over any number of arguments (variadic).
|
constexpr |
x^n for compile-time integer n, x any arithmetic type.
Returns double for negative n, T otherwise.
|
constexpr |
x^n for runtime integer n; T must be floating point.
|
constexpr |
Factorial x! - takes integer, returns double.
|
constexpr |
Double factorial x!! - takes integer, returns double.
|
constexpr |
Returns the sign of value. Note: sign(0) == 0.
|
constexpr |
Clamps value to [-max_abs, max_abs] - i.e., using abs.
|
constexpr |
Sets value to zero if |value| < min_abs, otherwise returns value unchanged.
| 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 |
||
| ) |
Numerical derivative of y(x) at point x; returns {dy/dx, error}.
Iteratively halves the step size until |dy/dx_n - dy/dx_{n-1}| < delta_target.
| Function | Callable as y(x), returning a value convertible to Real. |
| Real | Floating-point type. |
| y | Function to differentiate. |
| x | Point at which to evaluate the derivative. |
| delta_target | Convergence target (default 1e-6). |
| dx | Initial step size (default 0.01). |
| it_limit | Maximum number of iterations (default 250). |
| 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 |
||
| ) |
Solves f(x) = 0 using Newton's method; returns {root, error}.
| Function | Callable as f(x), returning a value convertible to Real. |
| Real | Floating-point type. |
| f | Function to solve. |
| x | Initial guess. |
| delta_target | Convergence target for |x_n - x_{n+1}| (default 1e-6). |
| dx | Initial step size for derivative (default 0.01). |
| it_limit | Maximum number of iterations (default 250). |
| std::pair< Real, Real > qip::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 |
||
| ) |
Solves f(x) = 0 using Newton's method with bounds; returns {root, error}.
Solution is clamped to [bounds.first, bounds.second].
| f | Function to solve. |
| x | Initial guess. |
| bounds | Allowed range [lower, upper]. |
| delta_target | Convergence target for |x_n - x_{n+1}| (default 1e-6). |
| dx | Initial step size for derivative (default 0.01). |
| it_limit | Maximum number of iterations (default 250). |
|
inline |
Returns a short string describing the threading status, e.g. "Using OpenMP with 8 threads." or "Single-threaded.".
|
inline |
Returns a random alphanumeric string of the given length.
|
inline |
Returns a formatted string using printf-style format specifiers.
Maximum string length is 256 characters. Use the size overload for longer strings.
|
inline |
Overload of fstring with explicit buffer size (maximum string length).
|
inline |
Compares s1 against pattern s2, where s2 may contain one wildcard '*' that matches any substring.
|
inline |
Conversion of a single character to lowercase.
|
inline |
Returns a lowercase copy of the string.
|
inline |
Returns true if the_string contains sub_string.
|
inline |
Returns true if the_string contains sub_string (case insensitive).
|
inline |
Returns true if the_string contains any of the sub_strings.
|
inline |
Returns true if the_string contains any of the sub_strings (case insensitive).
|
inline |
Case-insensitive string comparison; equivalent to tolower(s1) == tolower(s2).
|
inline |
Case-insensitive version of wildcard_compare.
Compares s1 against pattern s2, where s2 may contain one wildcard '*' that matches any substring.
|
inline |
Returns the Levenshtein edit distance between strings a and b.
|
inline |
Case-insensitive version of Levenstein.
|
inline |
Returns an iterator to the closest match to test_string in list, using Levenstein distance.
|
inline |
Returns the closest match (case insensitive) to test_string in list, using ci_Levenstein distance.
|
inline |
Returns true if the string represents an integer.
Accepts an optional leading '+' or '-'. e.g., "16" and "-12" return true; "12x" and "12.5" return false.
|
inline |
Splits a string by delimiter into a vector of substrings.
|
inline |
Concatenates a vector of strings into one, with an optional delimiter.
|
inline |
Word-wraps input at column at, optionally prefixing each line.
Does not split words unless unavoidable.
|
inline |
Converts an integer to a Roman numeral string. Assumes |a| <= 3999.
| std::vector< T > qip::merge | ( | std::vector< T > | first, |
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Merges any number of vectors: {a,b,c},{d,e},{f} -> {a,b,c,d,e,f}.
| auto qip::compare | ( | const std::vector< T > & | first, |
| const std::vector< T > & | second | ||
| ) |
Compares two arithmetic vectors element-wise; returns {max_delta, iterator}.
Returns {delta, itr} where delta = max|first - second| (signed as first-second), and itr points to the position in first where the maximum occurred.
If all compare exactly, max_delta should be zero, and iterator points to begining of list.
| auto qip::compare | ( | const std::vector< T > & | first, |
| const std::vector< U > & | second, | ||
| Func & | func | ||
| ) |
Compares two vectors using a user-supplied function; returns {max, iterator}.
Returns {delta, itr} where delta = max|func(first[i], second[i])|, and itr points to the position in first where the maximum occurred.
| auto qip::compare_eps | ( | const std::vector< T > & | first, |
| const std::vector< T > & | second | ||
| ) |
Compares two floating-point vectors element-wise relative to the second; returns {max_eps, iterator}.
Returns {eps, itr} where eps = max|(first-second)/second| (signed), and itr points to the position in first where the maximum occurred.
| void qip::add | ( | std::vector< T > * | first, |
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Adds any number of vectors in place (modifies *first).
Resizes *first to the size of the largest vector if needed.
| std::vector< T > qip::add | ( | std::vector< T > | first, |
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Adds any number of vectors; returns result sized to the largest input.
| void qip::multiply | ( | std::vector< T > * | first, |
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Multiplies any number of arithmetic vectors in place (modifies *first).
Resizes *first if needed; elements beyond the shorter vector are set to zero.
| std::vector< T > qip::multiply | ( | std::vector< T > | first, |
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Multiplies any number of vectors; returns result sized to the largest input.
| void qip::compose | ( | const F & | func, |
| std::vector< T > * | first, | ||
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Applies func element-wise across any number of vectors in place (modifies *first).
e.g., qip::compose(std::plus{}, &vo, v2, v3) is equivalent to qip::add(&vo, v2, v3). Resizes *first if needed.
| std::vector< T > qip::compose | ( | const F & | func, |
| std::vector< T > | first, | ||
| const std::vector< T > & | second, | ||
| const Args &... | rest | ||
| ) |
Applies func element-wise across any number of vectors; returns result sized to the largest input.
| void qip::scale | ( | std::vector< T > * | vec, |
| T | x | ||
| ) |
In-place scalar multiplication of a vector; types must match.
| std::vector< T > qip::scale | ( | std::vector< T > | vec, |
| T | x | ||
| ) |
Scalar multiplication of a vector; types must match.
| std::vector< T > qip::uniform_range | ( | T | first, |
| T | last, | ||
| N | number | ||
| ) |
Returns a uniformly distributed range [first, last] with number points.
first and last are guaranteed endpoints. number must be at least 2. For integral T the spacing may not be perfectly uniform due to rounding; duplicate values may appear if too many steps are requested.
| std::vector< T > qip::logarithmic_range | ( | T | first, |
| T | last, | ||
| N | number | ||
| ) |
Returns a logarithmically distributed range [first, last] with number points.
first and last are guaranteed endpoints. number must be at least 2. For integral T the spacing may not be perfectly logarithmic due to rounding; duplicate values may appear if too many steps are requested.
| std::vector< T > qip::loglinear_range | ( | T | first, |
| T | last, | ||
| T | b, | ||
| N | number | ||
| ) |
Returns a log-linear distributed range [first, last] with number points.
Roughly logarithmic for values below b and linear above b. number must be at least 3. T must be floating point. Not tested for negative values.
|
constexpr |
Helper: returns first[i] * rest[i] * ... at index i.
|
constexpr |
Variadic inner product: sum_i v1[i]*v2[i]*...*vn[i].
| auto qip::inner_product_sub | ( | std::size_t | p0, |
| std::size_t | pinf, | ||
| const T & | first, | ||
| const Args &... | rest | ||
| ) |
Inner product over the subrange [p0, pinf).
| T qip::apply_to | ( | const F & | func, |
| T | list | ||
| ) |
Applies func to each element of list in place; returns the modified list.
| std::vector< T > qip::select_if | ( | const std::vector< T > & | in, |
| Func | condition | ||
| ) |
Returns a copy of in containing only elements satisfying condition.
condition must have signature bool condition(T).
| void qip::insert_into_if | ( | const std::vector< T > & | in, |
| std::vector< T > * | inout, | ||
| Func | condition | ||
| ) |
Inserts elements from in into *inout if condition is met.
| std::vector< T > qip::reverse | ( | std::vector< T > | in | ) |
Returns a reversed copy of the vector.
| T qip::mean | ( | std::vector< T > | vec | ) |
Mean of a vector.
\[ \bar x = \frac{1}{N}\sum_i x_i \]
| T qip::variance | ( | std::vector< T > | vec, |
| std::size_t | dof = 0 |
||
| ) |
Variance using the two-pass method.
\[ \sigma^2 = \sum_i (x_i - \bar x)^2 / (N - \text{dof}) \]
dof is the degrees of freedom; use dof=1 for sample variance.
| T qip::sdev | ( | std::vector< T > | vec, |
| std::size_t | dof = 0 |
||
| ) |
Standard deviation: sqrt(variance).
| T qip::sem | ( | std::vector< T > | vec, |
| std::size_t | dof = 0 |
||
| ) |
Standard error of the mean: sdev / sqrt(N).
|
inline |
Basic progress bar. Prints new line if (and only if) i==(max-1)
|
constexpr |
True if compiled with OpenMP support, false otherwise.