almost complete. Need to doublecheck names for functions in *_serial.h
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint> //uint64_t
|
||||
//#include <stdexcept> // std::runtime_error
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
#include "../utils/vector.h"
|
||||
#include "../utils/matrix.h"
|
||||
|
||||
namespace numerics::detail{
|
||||
|
||||
// Shared engine
|
||||
inline std::mt19937& rng() {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
return gen;
|
||||
}
|
||||
|
||||
// Integral overload
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_integral<T>::value, int>::type = 0
|
||||
>
|
||||
inline T random(const T low, const T high) {
|
||||
std::uniform_int_distribution<T> dist(low, high);
|
||||
return dist(rng());
|
||||
}
|
||||
|
||||
// Floating-point overload
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0
|
||||
>
|
||||
inline T random(const T low, const T high) {
|
||||
std::uniform_real_distribution<T> dist(low, high);
|
||||
return dist(rng());
|
||||
}
|
||||
|
||||
// ---------------- Matrix ----------------
|
||||
template <typename T>
|
||||
inline void inplace_random_serial(utils::Matrix<T>& A, const T low, const T high) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
A(i,j) = random(low, high);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Vector ----------------
|
||||
template <typename T>
|
||||
inline void inplace_random_serial(utils::Vector<T>& v, const T low, const T high) {
|
||||
const uint64_t N = v.size();
|
||||
for (uint64_t i = 0; i < N; ++i){
|
||||
v[i] = random(low, high);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
Reference in New Issue
Block a user