<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
larnv.hpp
Go to the documentation of this file.
1
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_LARNV_HH
13#define TLAPACK_LARNV_HH
14
15#include <random>
16
18
19namespace tlapack {
20
43template <int idist, TLAPACK_VECTOR vector_t, class iseed_t>
45{
46 using idx_t = size_type<vector_t>;
47 using T = type_t<vector_t>;
48 using real_t = real_type<T>;
49
50 // Constants
51 const idx_t n = size(x);
52 const double twopi(8 * std::atan(1.0));
53
54 // Initialize the Mersenne Twister generator
55 std::random_device device;
56 std::mt19937 generator(device());
57 generator.seed(iseed);
58
59 if constexpr (idist == 1) {
60 std::uniform_real_distribution<> d1(0, 1);
61 for (idx_t i = 0; i < n; ++i) {
62 if constexpr (is_complex<T>)
63 x[i] = T(real_t(d1(generator)), real_t(d1(generator)));
64 else
65 x[i] = T(d1(generator));
66 }
67 }
68 else if constexpr (idist == 2) {
69 std::uniform_real_distribution<> d2(-1, 1);
70 for (idx_t i = 0; i < n; ++i) {
71 if constexpr (is_complex<T>)
72 x[i] = T(real_t(d2(generator)), real_t(d2(generator)));
73 else
74 x[i] = T(d2(generator));
75 }
76 }
77 else if constexpr (idist == 3) {
78 std::normal_distribution<> d3(0, 1);
79 for (idx_t i = 0; i < n; ++i) {
80 if constexpr (is_complex<T>)
81 x[i] = T(real_t(d3(generator)), real_t(d3(generator)));
82 else
83 x[i] = T(d3(generator));
84 }
85 }
86 else if constexpr (is_complex<T>) {
87 if constexpr (idist == 4) {
88 std::uniform_real_distribution<> d4(0, 1);
89 for (idx_t i = 0; i < n; ++i) {
90 double r = sqrt(d4(generator));
91 double theta = twopi * d4(generator);
92 x[i] = T(r * cos(theta), r * sin(theta));
93 }
94 }
95 else if constexpr (idist == 5) {
96 std::uniform_real_distribution<> d5(0, 1);
97 for (idx_t i = 0; i < n; ++i) {
98 double theta = twopi * d5(generator);
99 x[i] = T(real_t(cos(theta)), real_t(sin(theta)));
100 }
101 }
102 }
103
104 // Update the seed
105 iseed = iseed + 1;
106}
107
108} // namespace tlapack
109
110#endif // TLAPACK_LARNV_HH
void larnv(iseed_t &iseed, vector_t &x)
Returns a vector of n random numbers from a uniform or normal distribution.
Definition larnv.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