scran_test_utils
Test utilities for libscran projects
Loading...
Searching...
No Matches
simulate_vector.hpp
Go to the documentation of this file.
1#ifndef SCRAN_TESTS_SIMULATE_VECTOR_HPP
2#define SCRAN_TESTS_SIMULATE_VECTOR_HPP
3
4#include <random>
5#include <vector>
6#include <cstdint>
7#include <type_traits>
8#include <cstddef>
9
15namespace scran_tests {
16
20typedef std::mt19937_64 RngEngine;
21
26template<typename Type_>
27constexpr Type_ default_simulation_min() {
28 if constexpr(std::is_unsigned<Type_>::value) {
29 return 0;
30 } else {
31 return -10;
32 }
33}
34
40template<typename Type_ = double>
45 double density = 1;
46
52
57 Type_ upper = 10;
58
62 typename RngEngine::result_type seed = 1234567890;
63};
64
68template<typename Type_>
69auto create_simulating_distribution(const Type_ lower, const Type_ upper) {
70 if constexpr(std::is_floating_point<Type_>::value) {
71 return std::uniform_real_distribution<Type_>(lower, upper);
72 } else {
73 return std::uniform_int_distribution<Type_>(lower, upper - 1);
74 }
75}
76
77// Back-compatibility.
78template<typename Type_ = double>
79using SimulationParameters = SimulateVectorParameters<Type_>;
95template<typename Type_ = double>
96std::vector<Type_> simulate_vector(const typename std::vector<Type_>::size_type length, const SimulateVectorParameters<Type_>& params) {
97 RngEngine rng(params.seed);
98 auto unif = create_simulating_distribution(params.lower, params.upper);
99 std::vector<Type_> values(length);
100
101 if (params.density == 1) {
102 for (auto& v : values) {
103 v = unif(rng);
104 }
105 } else {
106 std::uniform_real_distribution<double> nonzero(0.0, 1.0);
107 for (auto& v : values) {
108 if (nonzero(rng) <= params.density) {
109 v = unif(rng);
110 }
111 }
112 }
113
114 return values;
115}
116
117}
118
119#endif
Test utilites for libscran.
Definition compare_almost_equal.hpp:12
std::vector< Type_ > simulate_vector(const typename std::vector< Type_ >::size_type length, const SimulateVectorParameters< Type_ > &params)
Definition simulate_vector.hpp:96
constexpr Type_ default_simulation_min()
Definition simulate_vector.hpp:27
std::mt19937_64 RngEngine
Definition simulate_vector.hpp:20
Parameters for simulate_vector().
Definition simulate_vector.hpp:41
RngEngine::result_type seed
Definition simulate_vector.hpp:62
Type_ upper
Definition simulate_vector.hpp:57
double density
Definition simulate_vector.hpp:45
Type_ lower
Definition simulate_vector.hpp:51