MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::generators Namespace Reference

Namespaces

namespace  ill_conditioned_detail
namespace  detail

Classes

class  hilbert
 Implicit Hilbert matrix: H(i,j) = 1/(i+j+1). More...
struct  ill_conditioned_dot
 An ill-conditioned dot product: x . More...
struct  ill_conditioned_sum
 An ill-conditioned sum: sum p rounded to nearest is s, and its achieved condition number sum |p_i| / |sum p_i| is cond. More...
class  lehmer
 Implicit Lehmer matrix: L(i,j) = (min(i,j)+1)/(max(i,j)+1). More...
class  lotkin
 Implicit Lotkin matrix: Hilbert with first row all 1s. More...
class  minij
 Implicit min(i,j) matrix: M(i,j) = min(i+1,j+1). More...
class  ones
 Implicit ones matrix: all entries equal to 1. Rank-1. More...

Functions

template<typename T = double>
auto clement (std::size_t n)
 Clement (tridiagonal) matrix, symmetric form: zero diagonal and C(i,i+1) = C(i+1,i) = sqrt((i+1)*(n-1-i)) for i = 0..n-2.
template<typename T = double>
auto companion (const std::vector< T > &coeffs)
 Companion matrix for polynomial p(x) = x^n + c[n-1]*x^{n-1} + ... + c[0].
template<typename T = double>
auto forsythe (std::size_t n, T alpha=T(1e-10), T lambda=T(0))
 Forsythe matrix: n x n Jordan block with eigenvalue lambda, plus alpha in position (n-1, 0).
template<typename T = double>
auto frank (std::size_t n)
 Frank matrix: upper Hessenberg with F(i,j) = n+1-max(i+1,j+1) for j >= i-1, zero below the sub-diagonal.
template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_dot< T > gen_dot (std::size_t n, double c, std::mt19937_64 &gen)
 GenDot (Algorithm 6.1): vectors x, y of length n >= 6 whose dot product has an anticipated condition number c >= 1.
template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_dot< T > gen_dot (std::size_t n, double c, std::uint64_t seed)
 GenDot from a seed.
template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_sum< T > gen_sum (std::size_t n, double c, std::mt19937_64 &gen)
 An ill-conditioned sum of even length n >= 12, the paper's way: GenDot of length n / 2 at condition number c, each product x_i y_i split exactly by two_product into hi + lo, the n summands permuted.
template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_sum< T > gen_sum (std::size_t n, double c, std::uint64_t seed)
 GenSum from a seed.
template<typename T = double>
auto kahan (std::size_t n, T theta=T(1.2), T pert=T(25))
 Kahan matrix, as MATLAB's gallery('kahan', n, theta, pert):
template<typename T = double>
auto laplacian_1d (std::size_t n)
 1D Laplacian: n x n tridiagonal [-1, 2, -1].
template<typename T = double>
auto laplacian_2d (std::size_t nx, std::size_t ny)
 2D Laplacian: (nx*ny) x (nx*ny) 5-point stencil on an nx x ny grid.
template<typename T = double>
mat::dense2D< T > magic (std::size_t N)
 Magic square of order N.
template<typename T = double>
auto moler (std::size_t n, T alpha=T(-1))
 Moler matrix: M = L*L^T where L(i,i) = 1, L(i,j) = alpha for i > j.
template<typename T = double>
auto pascal (std::size_t n)
 Pascal matrix: P(i,j) = C(i+j, i) = (i+j)!
template<typename T = double>
auto poisson2d_dirichlet (std::size_t nx, std::size_t ny)
 2D Poisson with Dirichlet BCs: (nx*ny) x (nx*ny) 5-point stencil.
template<typename T = double>
auto randorth (std::size_t n, std::mt19937_64 &gen)
 Random orthogonal matrix of size n x n, from the random matrix drawn from gen (operation/random.hpp, uniform on [-1, 1)).
template<typename T = double>
auto randorth (std::size_t n, std::uint64_t seed)
 Random orthogonal matrix, reproducibly from seed: the random matrix is the same with every compiler and standard library, so Q is too, up to the rounding of the QR factorization.
template<typename T = double>
auto randorth (std::size_t n)
 Random orthogonal matrix, non-deterministically.
template<typename T = double>
auto randspd (std::size_t n, const std::vector< T > &eigenvalues, std::mt19937_64 &gen)
 A symmetric positive definite matrix with prescribed eigenvalues, Q drawn from gen.
template<typename T = double>
auto randspd (std::size_t n, const std::vector< T > &eigenvalues, std::uint64_t seed)
 Prescribed eigenvalues, reproducibly from seed.
template<typename T = double>
auto randspd (std::size_t n, const std::vector< T > &eigenvalues)
 Prescribed eigenvalues, non-deterministically.
template<typename T = double>
auto randspd (std::size_t n, T kappa, int mode, std::mt19937_64 &gen)
 An SPD matrix with eigenvalues in [1/kappa, 1] from mode (the randsvd modes 1-5, see make_sigma), drawing from gen.
template<typename T = double>
auto randspd (std::size_t n, T kappa, int mode, std::uint64_t seed)
 Eigenvalues from kappa and mode, reproducibly from seed.
template<typename T = double>
auto randspd (std::size_t n, T kappa, int mode=3)
 Eigenvalues from kappa and mode, non-deterministically.
template<typename T = double>
auto randsvd (std::size_t m, std::size_t n, const std::vector< T > &sigma, std::mt19937_64 &gen)
 Construct a matrix with prescribed singular values, drawing U and V from gen.
template<typename T = double>
auto randsvd (std::size_t m, std::size_t n, const std::vector< T > &sigma, std::uint64_t seed)
 Prescribed singular values, reproducibly from seed.
template<typename T = double>
auto randsvd (std::size_t m, std::size_t n, const std::vector< T > &sigma)
 Prescribed singular values, non-deterministically.
template<typename T = double, std::integral Mi, std::integral Ni>
auto randsvd (Mi mi, Ni ni, T kappa, int mode, std::mt19937_64 &gen)
 A rectangular matrix with condition number kappa, singular values from mode (see make_sigma), drawing from gen.
template<typename T = double, std::integral Mi, std::integral Ni>
auto randsvd (Mi mi, Ni ni, T kappa, int mode, std::uint64_t seed)
 A rectangular matrix with condition number kappa, reproducibly from seed.
template<typename T = double, std::integral Mi, std::integral Ni>
requires (!std::integral<T>)
auto randsvd (Mi mi, Ni ni, T kappa, int mode=3)
 A rectangular matrix with condition number kappa, non-deterministically.
template<typename T = double, std::integral Ni, std::integral Mode, std::integral Seed>
requires (!std::integral<T>)
auto randsvd (Ni ni, T kappa, Mode mode, Seed seed)
 A square matrix with condition number kappa, reproducibly from seed.
template<typename T = double>
auto randsvd (std::size_t n, T kappa, int mode, std::mt19937_64 &gen)
 A square matrix with condition number kappa, drawing from gen.
template<typename T = double>
auto randsvd (std::size_t n, T kappa, int mode=3)
 A square matrix with condition number kappa, non-deterministically.
template<typename T = double>
auto randsym (std::size_t n, const std::vector< T > &eigenvalues, std::mt19937_64 &gen)
 A symmetric matrix with prescribed eigenvalues, Q drawn from gen.
template<typename T = double>
auto randsym (std::size_t n, const std::vector< T > &eigenvalues, std::uint64_t seed)
 Prescribed eigenvalues, reproducibly from seed.
template<typename T = double>
auto randsym (std::size_t n, const std::vector< T > &eigenvalues)
 Prescribed eigenvalues, non-deterministically.
template<typename T = double>
auto randsym (std::size_t n, T kappa, int mode, std::mt19937_64 &gen)
 A symmetric POSITIVE DEFINITE matrix with eigenvalues in [1/kappa, 1] from mode (the randsvd modes 1-5, see make_sigma), drawing from gen: the same kind of matrix as randspd.
template<typename T = double>
auto randsym (std::size_t n, T kappa, int mode, std::uint64_t seed)
 Eigenvalues from kappa and mode, reproducibly from seed.
template<typename T = double>
auto randsym (std::size_t n, T kappa, int mode=3)
 Eigenvalues from kappa and mode, non-deterministically.
template<typename T = double>
auto randsym_indefinite (std::size_t n, T kappa, int mode, std::size_t num_negative, std::mt19937_64 &gen)
 A symmetric INDEFINITE matrix: the eigenvalues of randsym(n, kappa, mode), with num_negative of them negated (issue 961).
template<typename T = double>
auto randsym_indefinite (std::size_t n, T kappa, int mode, std::size_t num_negative, std::uint64_t seed)
 randsym_indefinite reproducibly from seed.
template<typename T = double>
auto randsym_indefinite (std::size_t n, T kappa, int mode, std::size_t num_negative)
 randsym_indefinite non-deterministically.
template<typename T = double>
vec::dense_vector< T > arange (std::int64_t start, std::int64_t stop, std::int64_t step=1)
 arange: integer-strided sequence projected into T over the half-open interval [start, stop) (NumPy semantics – the stop value is excluded).
template<typename T = double>
vec::dense_vector< T > linspace (const T &start, const T &stop, std::size_t steps, bool endpoint=true)
 linspace: steps evenly spaced samples over the interval [start, stop].
template<typename T = double>
vec::dense_vector< T > logspace (const T &start, const T &stop, std::size_t steps, bool endpoint=true, const T &base=T(10))
 logspace: steps samples whose exponents are evenly spaced over [start, stop]; element i equals base^exponent_i.
template<typename T = double>
vec::dense_vector< T > geomspace (const T &start, const T &stop, std::size_t steps, bool endpoint=true)
 geomspace: steps samples in a geometric progression from start to stop (NumPy semantics – start and stop are the actual endpoints).
template<typename T = double>
auto rosser ()
 Rosser matrix: 8x8 symmetric integer matrix with known eigenvalues, in ascending order: -10*sqrt(10405), 0, 510-100*sqrt(26), 1000, 1000, 510+100*sqrt(26), 1020, 10*sqrt(10405) ~= -1020.0490, 0, 0.0980, 1000, 1000, 1019.9020, 1020, 1020.0490.
template<typename T = double>
auto vandermonde (const std::vector< T > &nodes)
 Vandermonde matrix: V(i,j) = nodes[i]^j.
template<typename T = double>
auto wilkinson (std::size_t n)
 Wilkinson W+ tridiagonal matrix of size n x n, as MATLAB's wilkinson(n): diagonal abs(-(n-1)/2 : (n-1)/2), that is |i - (n-1)/2| for i = 0..n-1, and all 1s on the sub- and super-diagonal.

Function Documentation

◆ arange()

template<typename T = double>
vec::dense_vector< T > mtl::generators::arange ( std::int64_t start,
std::int64_t stop,
std::int64_t step = 1 )

arange: integer-strided sequence projected into T over the half-open interval [start, stop) (NumPy semantics – the stop value is excluded).

Returns an empty vector if step == 0 or the interval is empty.

◆ clement()

template<typename T = double>
auto mtl::generators::clement ( std::size_t n)

Clement (tridiagonal) matrix, symmetric form: zero diagonal and C(i,i+1) = C(i+1,i) = sqrt((i+1)*(n-1-i)) for i = 0..n-2.

Its eigenvalues are n-1, n-3, ..., -(n-1): +/-(n-1), ..., +/-1 for even n, and +/-(n-1), ..., +/-2, 0 for odd n.

◆ companion()

template<typename T = double>
auto mtl::generators::companion ( const std::vector< T > & coeffs)

Companion matrix for polynomial p(x) = x^n + c[n-1]*x^{n-1} + ... + c[0].

The coefficients vector contains [c_0, c_1, ..., c_{n-1}] (low to high). Eigenvalues of the companion matrix are the roots of p(x).

◆ forsythe()

template<typename T = double>
auto mtl::generators::forsythe ( std::size_t n,
T alpha = T(1e-10),
T lambda = T(0) )

Forsythe matrix: n x n Jordan block with eigenvalue lambda, plus alpha in position (n-1, 0).

F(i,i) = lambda, F(i,i+1) = 1, F(n-1,0) = alpha. When alpha = 0, it's a standard Jordan block. For n >= 2 its eigenvalues are lambda + alpha^(1/n) * (the n-th roots of unity). For n = 1 the corner is the diagonal, so the matrix is the 1 x 1 Jordan block [lambda] and alpha is not used (MATLAB's gallery('forsythe', 1) gives [alpha] instead).

◆ frank()

template<typename T = double>
auto mtl::generators::frank ( std::size_t n)

Frank matrix: upper Hessenberg with F(i,j) = n+1-max(i+1,j+1) for j >= i-1, zero below the sub-diagonal.

Has known, real, positive eigenvalues.

◆ gen_dot() [1/2]

template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_dot< T > mtl::generators::gen_dot ( std::size_t n,
double c,
std::mt19937_64 & gen )

GenDot (Algorithm 6.1): vectors x, y of length n >= 6 whose dot product has an anticipated condition number c >= 1.

The first half is random over the exponent range [0, b/2], b = log2 c; each entry of the second half has its y_i chosen to cancel the exact running dot product down to 2^e_i, with e_i falling linearly from b/2 to 0; then both are permuted alike.

◆ gen_dot() [2/2]

template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_dot< T > mtl::generators::gen_dot ( std::size_t n,
double c,
std::uint64_t seed )

GenDot from a seed.

◆ gen_sum() [1/2]

template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_sum< T > mtl::generators::gen_sum ( std::size_t n,
double c,
std::mt19937_64 & gen )

An ill-conditioned sum of even length n >= 12, the paper's way: GenDot of length n / 2 at condition number c, each product x_i y_i split exactly by two_product into hi + lo, the n summands permuted.

The sum is x . y, so its condition number sum |p_i| / |sum p_i| is about half the dot product's.

◆ gen_sum() [2/2]

template<typename T = double>
requires math::exact_accumulable<T>
ill_conditioned_sum< T > mtl::generators::gen_sum ( std::size_t n,
double c,
std::uint64_t seed )

GenSum from a seed.

◆ geomspace()

template<typename T = double>
vec::dense_vector< T > mtl::generators::geomspace ( const T & start,
const T & stop,
std::size_t steps,
bool endpoint = true )

geomspace: steps samples in a geometric progression from start to stop (NumPy semantics – start and stop are the actual endpoints).

Requires start and stop to be nonzero, not NaN, and of the same sign.

Note: unlike the historical Universal implementation (which aliased logspace and therefore treated the endpoints as exponents), this computes a true geometric progression: geomspace(1, 1000, 4) == {1, 10, 100, 1000}.

◆ kahan()

template<typename T = double>
auto mtl::generators::kahan ( std::size_t n,
T theta = T(1.2),
T pert = T(25) )

Kahan matrix, as MATLAB's gallery('kahan', n, theta, pert):

K = diag(1, s, s^2, ..., s^(n-1)) * (I - c * triu(ones(n), 1))

  • pert * eps * diag(n, n-1, ..., 1), s = sin(theta), c = cos(theta)

so K(i,i) = s^i + pert*eps*(n-i), K(i,j) = -c*s^i for i < j, and 0 below the diagonal (0-based i, j; eps is std::numeric_limits<T>::epsilon()). Its condition number grows exponentially in n: the inverse of the unscaled factor has entries c*(1+c)^(j-i-1) above the diagonal. The perturbation (MATLAB's default pert = 25) keeps column-pivoted QR from swapping columns that tie only through rounding; pert = 0 gives the unperturbed matrix.

◆ laplacian_1d()

template<typename T = double>
auto mtl::generators::laplacian_1d ( std::size_t n)

1D Laplacian: n x n tridiagonal [-1, 2, -1].

SPD with eigenvalues 2 - 2*cos(k*pi/(n+1)), k = 1..n.

◆ laplacian_2d()

template<typename T = double>
auto mtl::generators::laplacian_2d ( std::size_t nx,
std::size_t ny )

2D Laplacian: (nx*ny) x (nx*ny) 5-point stencil on an nx x ny grid.

SPD sparse, tests iterative solvers. Uses natural (row-major) ordering.

◆ linspace()

template<typename T = double>
vec::dense_vector< T > mtl::generators::linspace ( const T & start,
const T & stop,
std::size_t steps,
bool endpoint = true )

linspace: steps evenly spaced samples over the interval [start, stop].

With endpoint == true (default, NumPy semantics) stop is the last sample; with endpoint == false the interval is treated as half-open.

◆ logspace()

template<typename T = double>
vec::dense_vector< T > mtl::generators::logspace ( const T & start,
const T & stop,
std::size_t steps,
bool endpoint = true,
const T & base = T(10) )

logspace: steps samples whose exponents are evenly spaced over [start, stop]; element i equals base^exponent_i.

As in NumPy, start and stop are exponents, not endpoints (see geomspace for endpoint semantics).

◆ magic()

template<typename T = double>
mat::dense2D< T > mtl::generators::magic ( std::size_t N)

Magic square of order N.

Supported orders:

  • odd N -> Siamese (De la Loubere) method;
  • doubly-even N -> (N % 4 == 0) complement method. Singly-even orders (N even, N % 4 != 0, e.g. 6, 10) are not yet supported and throw std::invalid_argument, as does N == 0.

◆ moler()

template<typename T = double>
auto mtl::generators::moler ( std::size_t n,
T alpha = T(-1) )

Moler matrix: M = L*L^T where L(i,i) = 1, L(i,j) = alpha for i > j.

Default alpha = -1. SPD with eigenvalues that cluster near zero. M(i,i) = i*alpha^2 + 1, M(i,j) = min(i,j)*alpha^2 + alpha for i != j.

◆ pascal()

template<typename T = double>
auto mtl::generators::pascal ( std::size_t n)

Pascal matrix: P(i,j) = C(i+j, i) = (i+j)!

/ (i! * j!). Symmetric positive definite with determinant 1. The entries grow like 4^n / sqrt(pi n): they are exact in T only up to the largest integer T represents exactly (2^24 in float: n <= 14; 2^53 in double: n <= 29), and the largest, P(n-1,n-1), overflows to inf in float for n >= 67 and in double for n >= 516.

◆ poisson2d_dirichlet()

template<typename T = double>
auto mtl::generators::poisson2d_dirichlet ( std::size_t nx,
std::size_t ny )

2D Poisson with Dirichlet BCs: (nx*ny) x (nx*ny) 5-point stencil.

h_x = 1/(nx+1), h_y = 1/(ny+1) diagonal = 2/h_x^2 + 2/h_y^2, off-diag x = -1/h_x^2, off-diag y = -1/h_y^2

◆ randorth() [1/3]

template<typename T = double>
auto mtl::generators::randorth ( std::size_t n)

Random orthogonal matrix, non-deterministically.

◆ randorth() [2/3]

template<typename T = double>
auto mtl::generators::randorth ( std::size_t n,
std::mt19937_64 & gen )

Random orthogonal matrix of size n x n, from the random matrix drawn from gen (operation/random.hpp, uniform on [-1, 1)).

Constructed via QR factorization of a random matrix: A = Q*R, return Q. Q satisfies Q^T * Q = I to machine precision.

Q is taken from the QR with R's diagonal non-negative, which is unique. qr_factor's own R has LAPACK's sign (#672), so without the normalization each column of Q would carry a sign that depends on A's pivot entries, and Q's distribution would differ from the one randorth gave before 5.22.0.

◆ randorth() [3/3]

template<typename T = double>
auto mtl::generators::randorth ( std::size_t n,
std::uint64_t seed )

Random orthogonal matrix, reproducibly from seed: the random matrix is the same with every compiler and standard library, so Q is too, up to the rounding of the QR factorization.

◆ randspd() [1/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
const std::vector< T > & eigenvalues )

Prescribed eigenvalues, non-deterministically.

◆ randspd() [2/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
const std::vector< T > & eigenvalues,
std::mt19937_64 & gen )

A symmetric positive definite matrix with prescribed eigenvalues, Q drawn from gen.

Throws std::invalid_argument unless eigenvalues.size() == n and every eigenvalue is > 0.

◆ randspd() [3/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
const std::vector< T > & eigenvalues,
std::uint64_t seed )

Prescribed eigenvalues, reproducibly from seed.

◆ randspd() [4/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
T kappa,
int mode,
std::mt19937_64 & gen )

An SPD matrix with eigenvalues in [1/kappa, 1] from mode (the randsvd modes 1-5, see make_sigma), drawing from gen.

Throws for kappa < 1.

◆ randspd() [5/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
T kappa,
int mode,
std::uint64_t seed )

Eigenvalues from kappa and mode, reproducibly from seed.

◆ randspd() [6/6]

template<typename T = double>
auto mtl::generators::randspd ( std::size_t n,
T kappa,
int mode = 3 )

Eigenvalues from kappa and mode, non-deterministically.

◆ randsvd() [1/9]

template<typename T = double, std::integral Mi, std::integral Ni>
auto mtl::generators::randsvd ( Mi mi,
Ni ni,
T kappa,
int mode,
std::mt19937_64 & gen )

A rectangular matrix with condition number kappa, singular values from mode (see make_sigma), drawing from gen.

◆ randsvd() [2/9]

template<typename T = double, std::integral Mi, std::integral Ni>
auto mtl::generators::randsvd ( Mi mi,
Ni ni,
T kappa,
int mode,
std::uint64_t seed )

A rectangular matrix with condition number kappa, reproducibly from seed.

Also the seeded form of the square matrix: randsvd(n, n, kappa, mode, seed).

◆ randsvd() [3/9]

template<typename T = double, std::integral Mi, std::integral Ni>
requires (!std::integral<T>)
auto mtl::generators::randsvd ( Mi mi,
Ni ni,
T kappa,
int mode = 3 )

A rectangular matrix with condition number kappa, non-deterministically.

cond(A) = kappa up to rounding. See make_sigma for the modes (1-5).

◆ randsvd() [4/9]

template<typename T = double, std::integral Ni, std::integral Mode, std::integral Seed>
requires (!std::integral<T>)
auto mtl::generators::randsvd ( Ni ni,
T kappa,
Mode mode,
Seed seed )

A square matrix with condition number kappa, reproducibly from seed.

kappa must not be integral, nor n, mode and seed anything else (see the header comment): the call has the rectangular form's shape.

◆ randsvd() [5/9]

template<typename T = double>
auto mtl::generators::randsvd ( std::size_t m,
std::size_t n,
const std::vector< T > & sigma )

Prescribed singular values, non-deterministically.

◆ randsvd() [6/9]

template<typename T = double>
auto mtl::generators::randsvd ( std::size_t m,
std::size_t n,
const std::vector< T > & sigma,
std::mt19937_64 & gen )

Construct a matrix with prescribed singular values, drawing U and V from gen.

A = U * Sigma * V^T where U (m x m) and V (n x n) are random orthogonal. Throws std::invalid_argument unless sigma.size() == min(m, n).

◆ randsvd() [7/9]

template<typename T = double>
auto mtl::generators::randsvd ( std::size_t m,
std::size_t n,
const std::vector< T > & sigma,
std::uint64_t seed )

Prescribed singular values, reproducibly from seed.

◆ randsvd() [8/9]

template<typename T = double>
auto mtl::generators::randsvd ( std::size_t n,
T kappa,
int mode,
std::mt19937_64 & gen )

A square matrix with condition number kappa, drawing from gen.

◆ randsvd() [9/9]

template<typename T = double>
auto mtl::generators::randsvd ( std::size_t n,
T kappa,
int mode = 3 )

A square matrix with condition number kappa, non-deterministically.

cond(A) = kappa up to rounding. See make_sigma for the modes (1-5).

◆ randsym() [1/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
const std::vector< T > & eigenvalues )

Prescribed eigenvalues, non-deterministically.

◆ randsym() [2/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
const std::vector< T > & eigenvalues,
std::mt19937_64 & gen )

A symmetric matrix with prescribed eigenvalues, Q drawn from gen.

Throws std::invalid_argument unless eigenvalues.size() == n.

◆ randsym() [3/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
const std::vector< T > & eigenvalues,
std::uint64_t seed )

Prescribed eigenvalues, reproducibly from seed.

◆ randsym() [4/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
T kappa,
int mode,
std::mt19937_64 & gen )

A symmetric POSITIVE DEFINITE matrix with eigenvalues in [1/kappa, 1] from mode (the randsvd modes 1-5, see make_sigma), drawing from gen: the same kind of matrix as randspd.

For an indefinite one with this conditioning, see randsym_indefinite below.

◆ randsym() [5/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
T kappa,
int mode,
std::uint64_t seed )

Eigenvalues from kappa and mode, reproducibly from seed.

◆ randsym() [6/6]

template<typename T = double>
auto mtl::generators::randsym ( std::size_t n,
T kappa,
int mode = 3 )

Eigenvalues from kappa and mode, non-deterministically.

◆ randsym_indefinite() [1/3]

template<typename T = double>
auto mtl::generators::randsym_indefinite ( std::size_t n,
T kappa,
int mode,
std::size_t num_negative )

randsym_indefinite non-deterministically.

◆ randsym_indefinite() [2/3]

template<typename T = double>
auto mtl::generators::randsym_indefinite ( std::size_t n,
T kappa,
int mode,
std::size_t num_negative,
std::mt19937_64 & gen )

A symmetric INDEFINITE matrix: the eigenvalues of randsym(n, kappa, mode), with num_negative of them negated (issue 961).

The negated positions are a random subset of the spectrum, drawn from gen after it, as LAPACK's DLATMS does with RSIGN = 'T'. So

  • for n >= 2, cond_2(A) = max |lambda| / min |lambda| = kappa, as for randsym: the signs do not change the magnitudes;
  • the inertia is (n - num_negative, num_negative, 0). n = 1 gives the 1x1 matrix [1], or [-1] with one negative, whatever kappa is (make_sigma's single value is 1), so its condition number is 1; n = 0 gives an empty matrix, with num_negative = 0. The subset is a partial Fisher-Yates shuffle over the raw engine stream (detail::random_unit), so a seed gives the same matrix with every compiler and standard library. With num_negative == 0 nothing is drawn for it, and the matrix is randsym(n, kappa, mode)'s for the same engine state. Throws std::invalid_argument for num_negative > n, and as randsym does for kappa and mode.

(A distinct name, not a randsym overload: randsym(n, kappa, mode, num_negative) would have the parameter types of the seeded randsym(n, kappa, mode, std::uint64_t seed) where std::size_t is std::uint64_t.)

◆ randsym_indefinite() [3/3]

template<typename T = double>
auto mtl::generators::randsym_indefinite ( std::size_t n,
T kappa,
int mode,
std::size_t num_negative,
std::uint64_t seed )

randsym_indefinite reproducibly from seed.

◆ rosser()

template<typename T = double>
auto mtl::generators::rosser ( )

Rosser matrix: 8x8 symmetric integer matrix with known eigenvalues, in ascending order: -10*sqrt(10405), 0, 510-100*sqrt(26), 1000, 1000, 510+100*sqrt(26), 1020, 10*sqrt(10405) ~= -1020.0490, 0, 0.0980, 1000, 1000, 1019.9020, 1020, 1020.0490.

They sum to the trace, 4040.

◆ vandermonde()

template<typename T = double>
auto mtl::generators::vandermonde ( const std::vector< T > & nodes)

Vandermonde matrix: V(i,j) = nodes[i]^j.

The resulting matrix is n x n where n = nodes.size(). Ill-conditioned for uniformly-spaced nodes.

◆ wilkinson()

template<typename T = double>
auto mtl::generators::wilkinson ( std::size_t n)

Wilkinson W+ tridiagonal matrix of size n x n, as MATLAB's wilkinson(n): diagonal abs(-(n-1)/2 : (n-1)/2), that is |i - (n-1)/2| for i = 0..n-1, and all 1s on the sub- and super-diagonal.

For odd n = 2m+1 the diagonal is m, m-1, ..., 1, 0, 1, ..., m; for even n it is half-integers, e.g. 1.5, 0.5, 0.5, 1.5 for n = 4. Throws std::invalid_argument for n = 0, and for an even n with a value type that cannot hold 1/2 (an integer type).