std.numeric

This module is a port of a growing fragment of the numeric header in Alexander Stepanov's Standard Template Library, with a few additions.

Members

Classes

Fft
class Fft

A class for performing fast Fourier transforms of power of two sizes. This class encapsulates a large amount of state that is reusable when performing multiple FFTs of sizes smaller than or equal to that specified in the constructor. This results in substantial speedups when performing multiple FFTs with a known maximum size. However, a free function API is provided for convenience if you need to perform a one-off FFT.

Enums

CustomFloatFlags
enum CustomFloatFlags

Format flags for CustomFloat.

Functions

cosineSimilarity
CommonType!(ElementType!(Range1), ElementType!(Range2)) cosineSimilarity(Range1 a, Range2 b)

Computes the cosine similarity of input ranges a and b. The two ranges must have the same length. If both ranges define length, the check is done once; otherwise, it is done at each iteration. If either range has all-zero elements, return 0.

decimalToFactorial
size_t decimalToFactorial(ulong decimal, ubyte[21] fac)

This function transforms decimal value into a value in the factorial number system stored in fac.

dotProduct
CommonType!(ElementType!(Range1), ElementType!(Range2)) dotProduct(Range1 a, Range2 b)
CommonType!(F1, F2) dotProduct(F1[] avector, F2[] bvector)
F dotProduct(F[N] a, F[N] b)

Computes the dot product of input ranges a and b. The two ranges must have the same length. If both ranges define length, the check is done once; otherwise, it is done at each iteration.

entropy
ElementType!Range entropy(Range r)
ElementType!Range entropy(Range r, F max)

Computes _entropy of input range r in bits. This function assumes (without checking) that the values in r are all in [0, 1]. For the entropy to be meaningful, often r should be normalized too (i.e., its values should sum to 1). The two-parameter version stops evaluating as soon as the intermediate result is greater than or equal to max.

euclideanDistance
CommonType!(ElementType!(Range1), ElementType!(Range2)) euclideanDistance(Range1 a, Range2 b)
CommonType!(ElementType!(Range1), ElementType!(Range2)) euclideanDistance(Range1 a, Range2 b, F limit)

Computes Euclidean distance between input ranges a and b. The two ranges must have the same length. The three-parameter version stops computation as soon as the distance is greater than or equal to limit (this is useful to save computation if a small distance is sought).

fft
Complex!F[] fft(R range)
void fft(R range, Ret buf)

Convenience functions that create an Fft object, run the FFT or inverse FFT and return the result. Useful for one-off FFTs.

findLocalMin
Tuple!(T, "x", Unqual!(ReturnType!DF), "y", T, "error") findLocalMin(DF f, T ax, T bx, T relTolerance, T absTolerance)

Find a real minimum of a real function f(x) via bracketing. Given a function f and a range (ax .. bx), returns the value of x in the range which is closest to a minimum of f(x). f is never evaluted at the endpoints of ax and bx. If f(x) has more than one minimum in the range, one will be chosen arbitrarily. If f(x) returns NaN or -Infinity, (x, f(x), NaN) will be returned; otherwise, this algorithm is guaranteed to succeed.

findRoot
T findRoot(DF f, T a, T b, DT tolerance)
T findRoot(DF f, T a, T b)

Find a real root of a real function f(x) via bracketing.

findRoot
Tuple!(T, T, R, R) findRoot(DF f, T ax, T bx, R fax, R fbx, DT tolerance)
Tuple!(T, T, R, R) findRoot(DF f, T ax, T bx, R fax, R fbx)
T findRoot(R delegate(T) f, T a, T b, bool delegate(T lo, T hi) tolerance)

Find root of a real function f(x) by bracketing, allowing the termination condition to be specified.

gapWeightedSimilarity
F gapWeightedSimilarity(R1 s, R2 t, F lambda)

The so-called "all-lengths gap-weighted string kernel" computes a similarity measure between s and t based on all of their common subsequences of all lengths. Gapped subsequences are also included.

gapWeightedSimilarityIncremental
GapWeightedSimilarityIncremental!(R, F) gapWeightedSimilarityIncremental(R r1, R r2, F penalty)

Similar to gapWeightedSimilarity, just works in an incremental manner by first revealing the matches of length 1, then gapped matches of length 2, and so on. The memory requirement is O(s.length * t.length). The time complexity is O(s.length * t.length) time for computing each step. Continuing on the previous example:

gapWeightedSimilarityNormalized
Select!(isFloatingPoint!(F), F, double) gapWeightedSimilarityNormalized(R1 s, R2 t, F lambda, F sSelfSim, F tSelfSim)

The similarity per gapWeightedSimilarity has an issue in that it grows with the lengths of the two strings, even though the strings are not actually very similar. For example, the range ["Hello", "world"] is increasingly similar with the range ["Hello", "world", "world", "world",...] as more instances of "world" are appended. To prevent that, gapWeightedSimilarityNormalized computes a normalized version of the similarity that is computed as gapWeightedSimilarity(s, t, lambda) / sqrt(gapWeightedSimilarity(s, t, lambda) * gapWeightedSimilarity(s, t, lambda)). The function gapWeightedSimilarityNormalized (a so-called normalized kernel) is bounded in [0, 1], reaches 0 only for ranges that don't match in any position, and 1 only for identical ranges.

gcd
typeof(Unqual!(T).init % Unqual!(U).init) gcd(T a, U b)
auto gcd(T a, T b)

Computes the greatest common divisor of a and b by using an efficient algorithm such as Euclid's or Stein's algorithm.

inverseFft
Complex!F[] inverseFft(R range)
void inverseFft(R range, Ret buf)

Convenience functions that create an Fft object, run the FFT or inverse FFT and return the result. Useful for one-off FFTs.

jensenShannonDivergence
CommonType!(ElementType!Range1, ElementType!Range2) jensenShannonDivergence(Range1 a, Range2 b)
CommonType!(ElementType!Range1, ElementType!Range2) jensenShannonDivergence(Range1 a, Range2 b, F limit)

Computes the Jensen-Shannon divergence between a and b, which is the sum (ai * log(2 * ai / (ai + bi)) + bi * log(2 * bi / (ai + bi))) / 2. The base of logarithm is 2. The ranges are assumed to contain elements in [0, 1]. Usually the ranges are normalized probability distributions, but this is not required or checked by jensenShannonDivergence. If the inputs are normalized, the result is bounded within [0, 1]. The three-parameter version stops evaluations as soon as the intermediate result is greater than or equal to limit.

kullbackLeiblerDivergence
CommonType!(ElementType!Range1, ElementType!Range2) kullbackLeiblerDivergence(Range1 a, Range2 b)

Computes the Kullback-Leibler divergence between input ranges a and b, which is the sum ai * log(ai / bi). The base of logarithm is 2. The ranges are assumed to contain elements in [0, 1]. Usually the ranges are normalized probability distributions, but this is not required or checked by kullbackLeiblerDivergence. If any element bi is zero and the corresponding element ai nonzero, returns infinity. (Otherwise, if ai == 0 && bi == 0, the term ai * log(ai / bi) is considered zero.) If the inputs are normalized, the result is positive.

lcm
typeof(Unqual!(T).init % Unqual!(U).init) lcm(T a, U b)
auto lcm(T a, T b)

Computes the least common multiple of a and b. Arguments are the same as gcd.

normalize
bool normalize(R range, ElementType!(R) sum)

Normalizes values in range by multiplying each element with a number chosen such that values sum up to sum. If elements in range sum to zero, assigns sum / range.length to all. Normalization makes sense only if all elements in range are positive. normalize assumes that is the case without checking it.

sumOfLog2s
ElementType!Range sumOfLog2s(Range r)

Compute the sum of binary logarithms of the input range r. The error of this method is much smaller than with a naive sum of log2.

Structs

CustomFloat
struct CustomFloat(uint precision, uint exponentWidth, CustomFloatFlags flags, uint bias)

Allows user code to define custom floating-point formats. These formats are for storage only; all operations on them are performed by first implicitly extracting them to real first. After the operation is completed the result can be stored in a custom floating-point value via assignment.

GapWeightedSimilarityIncremental
struct GapWeightedSimilarityIncremental(Range, F = double)

Similar to gapWeightedSimilarity, just works in an incremental manner by first revealing the matches of length 1, then gapped matches of length 2, and so on. The memory requirement is O(s.length * t.length). The time complexity is O(s.length * t.length) time for computing each step. Continuing on the previous example:

Templates

CustomFloat
template CustomFloat(uint bits)
template CustomFloat(uint precision, uint exponentWidth, CustomFloatFlags flags = CustomFloatFlags.ieee)

Allows user code to define custom floating-point formats. These formats are for storage only; all operations on them are performed by first implicitly extracting them to real first. After the operation is completed the result can be stored in a custom floating-point value via assignment.

FPTemporary
template FPTemporary(F)

Defines the fastest type to use when storing temporaries of a calculation intended to ultimately yield a result of type F (where F must be one of float, double, or real). When doing a multi-step computation, you may want to store intermediate results as FPTemporary!F.

secantMethod
template secantMethod(alias fun)

Implements the secant method for finding a root of the function fun starting from points [xn_1, x_n] (ideally close to the root). Num may be float, double, or real.

Meta

Authors

Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko