<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) 2021-2023, 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
16
17namespace tlapack {
18
35 private:
36 const uint64_t a = 6364136223846793005;
37 const uint64_t c = 1442695040888963407;
38 uint64_t state = 1302;
39
40 public:
41 static constexpr uint32_t min() noexcept { return 0; }
42 static constexpr uint32_t max() noexcept { return UINT32_MAX; }
43 constexpr void seed(uint64_t s) noexcept { state = s; }
44
46 constexpr uint32_t operator()()
47 {
48 state = state * a + c;
49 return state >> 32;
50 }
51};
52
57template <typename T, enable_if_t<is_real<T>, bool> = true>
59{
60 return T(static_cast<float>(gen()) / static_cast<float>(gen.max()));
61}
62
66template <typename T, enable_if_t<is_complex<T>, bool> = true>
67T rand_helper(rand_generator& gen)
68{
69 using real_t = real_type<T>;
70 real_t r1(static_cast<float>(gen()) / static_cast<float>(gen.max()));
71 real_t r2(static_cast<float>(gen()) / static_cast<float>(gen.max()));
72 return complex_type<real_t>(r1, r2);
73}
74
78template <typename T, enable_if_t<is_real<T>, bool> = true>
80{
81 return T(static_cast<float>(rand()) / static_cast<float>(RAND_MAX));
82}
83
87template <typename T, enable_if_t<is_complex<T>, bool> = true>
88T rand_helper()
89{
90 using real_t = real_type<T>;
91 real_t r1(static_cast<float>(rand()) / static_cast<float>(RAND_MAX));
92 real_t r2(static_cast<float>(rand()) / static_cast<float>(RAND_MAX));
93 return complex_type<real_t>(r1, r2);
94}
95
111 template <TLAPACK_MATRIX matrix_t>
112 void colmajor_read(matrix_t& A, std::istream& is) const
113 {
114 using idx_t = size_type<matrix_t>;
115
116 const idx_t m = nrows(A);
117 const idx_t n = ncols(A);
118
119 for (idx_t j = 0; j < n; ++j)
120 for (idx_t i = 0; i < m; ++i)
121 is >> A(i, j);
122 }
123
129 template <TLAPACK_MATRIX matrix_t>
131 {
132 using T = type_t<matrix_t>;
133 using idx_t = size_type<matrix_t>;
134
135 const idx_t m = nrows(A);
136 const idx_t n = ncols(A);
137
138 for (idx_t j = 0; j < n; ++j)
139 for (idx_t i = 0; i < m; ++i)
140 A(i, j) = rand_helper<T>(gen);
141 }
142
151 template <TLAPACK_UPLO uplo_t, TLAPACK_MATRIX matrix_t>
153 {
154 using T = type_t<matrix_t>;
155 using idx_t = size_type<matrix_t>;
156
157 const idx_t m = nrows(A);
158 const idx_t n = ncols(A);
159
160 if (uplo == Uplo::Upper) {
161 for (idx_t j = 0; j < n; ++j)
162 for (idx_t i = 0; i < m; ++i)
163 if (i <= j)
164 A(i, j) = rand_helper<T>(gen);
165 else
166 A(i, j) = T(float(0xCAFEBABE));
167 }
168 else {
169 for (idx_t j = 0; j < n; ++j)
170 for (idx_t i = 0; i < m; ++i)
171 if (i >= j)
172 A(i, j) = rand_helper<T>(gen);
173 else
174 A(i, j) = T(float(0xCAFEBABE));
175 }
176 }
177
185 template <TLAPACK_MATRIX matrix_t>
187 {
188 using T = type_t<matrix_t>;
189 using idx_t = size_type<matrix_t>;
190
191 const idx_t m = nrows(A);
192 const idx_t n = ncols(A);
193
194 for (idx_t j = 0; j < n; ++j)
195 for (idx_t i = 0; i < m; ++i)
196 if (i <= j + 1)
197 A(i, j) = rand_helper<T>(gen);
198 else
199 A(i, j) = T(float(0xFA57C0DE));
200 }
201
213 template <TLAPACK_MATRIX matrix_t>
214 void hilbert(matrix_t& A) const
215 {
216 using T = type_t<matrix_t>;
217 using idx_t = size_type<matrix_t>;
218
219 const idx_t m = nrows(A);
220 const idx_t n = ncols(A);
221
222 for (idx_t j = 0; j < n; ++j)
223 for (idx_t i = 0; i < m; ++i)
224 A(i, j) = T(1) / T(i + j + 1);
225 }
226
233 template <TLAPACK_MATRIX matrix_t>
235 {
236 using idx_t = size_type<matrix_t>;
237
238 const idx_t m = nrows(A);
239 const idx_t n = ncols(A);
240
241 for (idx_t j = 0; j < n; ++j)
242 for (idx_t i = 0; i < m; ++i)
243 A(i, j) = val;
244 }
245
256 template <TLAPACK_UPLO uplo_t, TLAPACK_MATRIX matrix_t>
258 matrix_t& A,
259 const type_t<matrix_t>& val) const
260 {
261 using T = type_t<matrix_t>;
262 using idx_t = size_type<matrix_t>;
263
264 const idx_t m = nrows(A);
265 const idx_t n = ncols(A);
266
267 if (uplo == Uplo::Upper) {
268 for (idx_t j = 0; j < n; ++j)
269 for (idx_t i = 0; i < m; ++i)
270 if (i <= j)
271 A(i, j) = val;
272 else
273 A(i, j) = T(float(0xCAFEBABE));
274 }
275 else {
276 for (idx_t j = 0; j < n; ++j)
277 for (idx_t i = 0; i < m; ++i)
278 if (i >= j)
279 A(i, j) = val;
280 else
281 A(i, j) = T(float(0xCAFEBABE));
282 }
283 }
284
285 rand_generator gen;
286};
287
288} // namespace tlapack
289
290#endif // TLAPACK_MATRIXMARKET_HH
T rand_helper(rand_generator &gen)
Helper function to generate random numbers.
Definition MatrixMarket.hpp:58
Random number generator.
Definition MatrixMarket.hpp:34
constexpr uint32_t operator()()
Generates a pseudo-random number.
Definition MatrixMarket.hpp:46
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:102
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:234
void colmajor_read(matrix_t &A, std::istream &is) const
Read a dense matrix from an input stream (file, stdin, etc).
Definition MatrixMarket.hpp:112
void random(uplo_t uplo, matrix_t &A)
Generate an upper- or lower-triangular random matrix.
Definition MatrixMarket.hpp:152
void random(matrix_t &A)
Generate a random dense matrix.
Definition MatrixMarket.hpp:130
void hessenberg(matrix_t &A)
Generate a random upper Hessenberg matrix.
Definition MatrixMarket.hpp:186
void hilbert(matrix_t &A) const
Generate a Hilbert matrix.
Definition MatrixMarket.hpp:214
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:257