<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
MatrixMarket.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2025, University of Colorado Denver. All rights reserved.
7//
8// This file is part of <T>LAPACK.
9// <T>LAPACK is free software: you can redistribute it and/or modify it under
10// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
11
12#ifndef TLAPACK_MATRIXMARKET_HH
13#define TLAPACK_MATRIXMARKET_HH
14
15#include <random>
17#include <type_traits>
18
19namespace tlapack {
20
27class PCG32 {
28 private:
29 const uint64_t a = 6364136223846793005ULL;
30 const uint64_t c = 1442695040888963407ULL;
31 uint64_t state;
32
33 public:
36 PCG32(uint64_t s = 1302) noexcept { seed(s); }
37
39 void seed(uint64_t s) noexcept
40 {
41 state = 0;
42 operator()();
43 state += s;
44 operator()();
45 }
46
47 static constexpr uint32_t min() noexcept { return 0; }
48 static constexpr uint32_t max() noexcept { return UINT32_MAX; }
49
52 {
53 // Constants for a 64-bit state and 32-bit output
54 const uint8_t bits = 64;
55 const uint8_t opbits = 5;
56 constexpr uint8_t mask = (1 << opbits) - 1;
57 constexpr uint8_t xshift = (opbits + 32) / 2;
58 constexpr uint8_t bottomspare = (bits - 32) - opbits;
59
60 // Random rotation
61 uint8_t rot = uint8_t(state >> (bits - opbits)) & mask;
62
63 // XOR shift high
65 static_cast<uint32_t>((state ^ (state >> xshift)) >> bottomspare);
66
67 // Rotate right
68 result = (result >> rot) | (result << ((-rot) & mask));
69
70 // Updates the state
71 state = state * a + c;
72
73 return result;
74 }
75};
76
82template <class T,
83 class Generator,
84 class Distribution,
85 enable_if_t<is_real<T>, bool> = true>
87{
88 return T(d(gen));
89}
90
94template <class T,
95 class Generator,
96 class Distribution,
97 enable_if_t<is_complex<T>, bool> = true>
98T rand_helper(Generator& gen, Distribution& d)
99{
100 using real_t = real_type<T>;
101 real_t r1(d(gen));
102 real_t r2(d(gen));
103 return complex_type<real_t>(r1, r2);
104}
105
116template <class T, class Generator>
118{
119 using real_t = real_type<T>;
120 using dist_t =
121 typename std::conditional<(is_same_v<real_t, float> ||
124 std::uniform_real_distribution<real_t>,
125 std::uniform_real_distribution<float>>::type;
126
127 dist_t d;
128 return rand_helper<T>(gen, d);
129}
130
146 template <TLAPACK_MATRIX matrix_t>
147 void colmajor_read(matrix_t& A, std::istream& is) const
148 {
149 using idx_t = size_type<matrix_t>;
150
151 const idx_t m = nrows(A);
152 const idx_t n = ncols(A);
153
154 for (idx_t j = 0; j < n; ++j)
155 for (idx_t i = 0; i < m; ++i)
156 is >> A(i, j);
157 }
158
164 template <TLAPACK_MATRIX matrix_t>
166 {
167 using T = type_t<matrix_t>;
168 using idx_t = size_type<matrix_t>;
169
170 const idx_t m = nrows(A);
171 const idx_t n = ncols(A);
172
173 for (idx_t j = 0; j < n; ++j)
174 for (idx_t i = 0; i < m; ++i)
175 A(i, j) = rand_helper<T>(gen);
176 }
177
186 template <TLAPACK_UPLO uplo_t, TLAPACK_MATRIX matrix_t>
188 {
189 using T = type_t<matrix_t>;
190 using idx_t = size_type<matrix_t>;
191
192 const idx_t m = nrows(A);
193 const idx_t n = ncols(A);
194
195 if (uplo == Uplo::Upper) {
196 for (idx_t j = 0; j < n; ++j)
197 for (idx_t i = 0; i < m; ++i)
198 if (i <= j)
199 A(i, j) = rand_helper<T>(gen);
200 else
201 A(i, j) = T(float(0xCAFEBABE));
202 }
203 else {
204 for (idx_t j = 0; j < n; ++j)
205 for (idx_t i = 0; i < m; ++i)
206 if (i >= j)
207 A(i, j) = rand_helper<T>(gen);
208 else
209 A(i, j) = T(float(0xCAFEBABE));
210 }
211 }
212
218 template <TLAPACK_MATRIX matrix_t,
219 class Distribution = typename std::conditional<
222 is_same_v<type_t<matrix_t>, long double>),
223 std::normal_distribution<type_t<matrix_t>>,
224 std::normal_distribution<double>>::type>
226 {
227 using T = type_t<matrix_t>;
228 using idx_t = size_type<matrix_t>;
229
230 const idx_t m = nrows(A);
231 const idx_t n = ncols(A);
232
233 Distribution d(0, 1);
234 for (idx_t j = 0; j < n; ++j)
235 for (idx_t i = 0; i < m; ++i)
236 A(i, j) = rand_helper<T>(gen, d);
237 }
238
247 template <TLAPACK_UPLO uplo_t,
249 class Distribution = typename std::conditional<
252 is_same_v<type_t<matrix_t>, long double>),
253 std::normal_distribution<type_t<matrix_t>>,
254 std::normal_distribution<double>>::type>
256 {
257 using T = type_t<matrix_t>;
258 using idx_t = size_type<matrix_t>;
259
260 const idx_t m = nrows(A);
261 const idx_t n = ncols(A);
262
263 Distribution d(0, 1);
264 if (uplo == Uplo::Upper) {
265 for (idx_t j = 0; j < n; ++j)
266 for (idx_t i = 0; i < m; ++i)
267 if (i <= j)
268 A(i, j) = rand_helper<T>(gen, d);
269 else
270 A(i, j) = T(float(0xCAFEBABE));
271 }
272 else {
273 for (idx_t j = 0; j < n; ++j)
274 for (idx_t i = 0; i < m; ++i)
275 if (i >= j)
276 A(i, j) = rand_helper<T>(gen, d);
277 else
278 A(i, j) = T(float(0xCAFEBABE));
279 }
280 }
281
289 template <TLAPACK_MATRIX matrix_t>
291 {
292 using T = type_t<matrix_t>;
293 using idx_t = size_type<matrix_t>;
294
295 const idx_t m = nrows(A);
296 const idx_t n = ncols(A);
297
298 for (idx_t j = 0; j < n; ++j)
299 for (idx_t i = 0; i < m; ++i)
300 if (i <= j + 1)
301 A(i, j) = rand_helper<T>(gen);
302 else
303 A(i, j) = T(float(0xFA57C0DE));
304 }
305
317 template <TLAPACK_MATRIX matrix_t>
318 void hilbert(matrix_t& A) const
319 {
320 using T = type_t<matrix_t>;
321 using idx_t = size_type<matrix_t>;
322
323 const idx_t m = nrows(A);
324 const idx_t n = ncols(A);
325
326 for (idx_t j = 0; j < n; ++j)
327 for (idx_t i = 0; i < m; ++i)
328 A(i, j) = T(1) / T(i + j + 1);
329 }
330
337 template <TLAPACK_MATRIX matrix_t>
339 {
340 using idx_t = size_type<matrix_t>;
341
342 const idx_t m = nrows(A);
343 const idx_t n = ncols(A);
344
345 for (idx_t j = 0; j < n; ++j)
346 for (idx_t i = 0; i < m; ++i)
347 A(i, j) = val;
348 }
349
360 template <TLAPACK_UPLO uplo_t, TLAPACK_MATRIX matrix_t>
362 matrix_t& A,
363 const type_t<matrix_t>& val) const
364 {
365 using T = type_t<matrix_t>;
366 using idx_t = size_type<matrix_t>;
367
368 const idx_t m = nrows(A);
369 const idx_t n = ncols(A);
370
371 if (uplo == Uplo::Upper) {
372 for (idx_t j = 0; j < n; ++j)
373 for (idx_t i = 0; i < m; ++i)
374 if (i <= j)
375 A(i, j) = val;
376 else
377 A(i, j) = T(float(0xCAFEBABE));
378 }
379 else {
380 for (idx_t j = 0; j < n; ++j)
381 for (idx_t i = 0; i < m; ++i)
382 if (i >= j)
383 A(i, j) = val;
384 else
385 A(i, j) = T(float(0xCAFEBABE));
386 }
387 }
388
389 PCG32 gen;
390};
391
392} // namespace tlapack
393
394#endif // TLAPACK_MATRIXMARKET_HH
T rand_helper(Generator &gen, Distribution &d)
Helper function to generate random numbers.
Definition MatrixMarket.hpp:86
Permuted Congruential Generator.
Definition MatrixMarket.hpp:27
void seed(uint64_t s) noexcept
Sets the current state of PCG32. Same as PCG32(s).
Definition MatrixMarket.hpp:39
uint32_t operator()() noexcept
Generates a pseudo-random number using PCG's output function (XSH-RR).
Definition MatrixMarket.hpp:51
PCG32(uint64_t s=1302) noexcept
Constructor.
Definition MatrixMarket.hpp:36
#define TLAPACK_UPLO
Macro for tlapack::concepts::Uplo compatible with C++17.
Definition concepts.hpp:942
#define TLAPACK_MATRIX
Macro for tlapack::concepts::Matrix compatible with C++17.
Definition concepts.hpp:896
void rot(vectorX_t &x, vectorY_t &y, const c_type &c, const s_type &s)
Apply plane rotation:
Definition rot.hpp:44
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
MatrixMarket class.
Definition MatrixMarket.hpp:137
void single_value(matrix_t &A, const type_t< matrix_t > &val) const
Generate a matrix with a single value in all entries.
Definition MatrixMarket.hpp:338
void colmajor_read(matrix_t &A, std::istream &is) const
Read a dense matrix from an input stream (file, stdin, etc).
Definition MatrixMarket.hpp:147
void random(uplo_t uplo, matrix_t &A)
Generate an upper- or lower-triangular random matrix.
Definition MatrixMarket.hpp:187
void random(matrix_t &A)
Generate a random dense matrix.
Definition MatrixMarket.hpp:165
void randn(uplo_t uplo, matrix_t &A)
Generate an upper- or lower-triangular random matrix.
Definition MatrixMarket.hpp:255
void hessenberg(matrix_t &A)
Generate a random upper Hessenberg matrix.
Definition MatrixMarket.hpp:290
void hilbert(matrix_t &A) const
Generate a Hilbert matrix.
Definition MatrixMarket.hpp:318
void randn(matrix_t &A)
Generate a random dense matrix based on a normal distribution.
Definition MatrixMarket.hpp:225
void single_value(uplo_t uplo, matrix_t &A, const type_t< matrix_t > &val) const
Generate an upper- or lower-triangular matrix with a single value in all entries.
Definition MatrixMarket.hpp:361