Skip to content

Commit 1695e50

Browse files
committed
Added simulate_vector overload that accepts number of rows/columns.
This takes some effort out of having to safely compute the product. Also templated the size parameter for some greater flexibility.
1 parent ef36348 commit 1695e50

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

include/tatami_test/simulate_vector.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ struct SimulateVectorOptions {
4545
* Simulate a vector of values from a uniform distribution.
4646
*
4747
* @tparam Type_ Type of value to be simulated.
48+
* @tparam Length_ Integer type of the length of the output vector.
49+
*
4850
* @param length Length of the array of values to simulate.
4951
* @param options Simulation options.
5052
*
5153
* @return Vector of simulated values.
5254
*/
53-
template<typename Type_>
54-
std::vector<Type_> simulate_vector(std::size_t length, const SimulateVectorOptions& options) {
55+
template<typename Type_, typename Length_>
56+
std::vector<Type_> simulate_vector(const Length_ length, const SimulateVectorOptions& options) {
5557
auto output = sanisizer::create<std::vector<Type_> >(length);
5658
RngEngine rng(options.seed);
5759
std::uniform_real_distribution<> unif(options.lower, options.upper);
@@ -72,6 +74,23 @@ std::vector<Type_> simulate_vector(std::size_t length, const SimulateVectorOptio
7274
return output;
7375
}
7476

77+
/**
78+
* Overload of `simulate_vector()` for simulating the contents of a dense random matrix.
79+
*
80+
* @tparam Type_ Type of value to be simulated.
81+
* @tparam Index_ Integer type of the dimension extents.
82+
*
83+
* @param nrow Number of rows in the matrix.
84+
* @param ncol Number of columns in the matrix.
85+
* @param options Simulation options.
86+
*
87+
* @return Vector of simulated values of length equal to the product of `nrow` and `ncol`.
88+
*/
89+
template<typename Type_, typename Index_>
90+
std::vector<Type_> simulate_vector(const Index_ nrow, const Index_ ncol, const SimulateVectorOptions& options) {
91+
return simulate_vector<Type_>(sanisizer::product<typename std::vector<Type_>::size_type>(nrow, ncol), options);
92+
}
93+
7594
/**
7695
* @cond
7796
*/

tests/src/simulate_vector.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ TEST(SimulateVector, Dense) {
4242
}());
4343
EXPECT_NE(res, res2);
4444
}
45+
46+
{
47+
auto res = tatami_test::simulate_vector<double>(1000, []{
48+
tatami_test::SimulateVectorOptions opt;
49+
opt.seed = 12345;
50+
return opt;
51+
}());
52+
auto res2 = tatami_test::simulate_vector<double>(50, 20, []{
53+
tatami_test::SimulateVectorOptions opt;
54+
opt.seed = 12345;
55+
return opt;
56+
}());
57+
EXPECT_EQ(res, res2);
58+
}
4559
}
4660

4761
TEST(SimulateVector, Sparse) {

0 commit comments

Comments
 (0)