1 #ifndef STAN_MATH_PRIM_FUN_LINSPACED_INT_ARRAY_HPP
2 #define STAN_MATH_PRIM_FUN_LINSPACED_INT_ARRAY_HPP
3
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <vector>
7
8 namespace stan {
9 namespace math {
10
11 /**
12 * Return an array of linearly spaced integers.
13 *
14 * This produces an array from `low` to `high` (inclusive). If `high - low` is
15 * greater or equal to `K - 1`, then the integers are evenly spaced. If it is
16 * not possible to get from `low` to `high` with a multiple of an integer,
17 * `high` is lowered until this is possible.
18 *
19 * If `K - 1` is greater than `high - low`, then integers are repeated. For
20 * instance, `low, low, low + 1, low + 1, ...`. `high` is lowered until `K - 1`
21 * is a multiple of `high - low`
22 *
23 * For `K = 1`, the array will contain the `high` value. For `K = 0` it returns
24 * an empty array.
25 *
26 * @param K size of the array
27 * @param low smallest value
28 * @param high largest value
29 * @return An array of size K with elements linearly spaced between
30 * low and high.
31 * @throw std::domain_error if K is negative or high is less than low.
32 */
linspaced_int_array(int K,int low,int high)33 inline std::vector<int> linspaced_int_array(int K, int low, int high) {
34 static const char* function = "linspaced_int_array";
35 check_nonnegative(function, "size", K);
36 check_greater_or_equal(function, "high", high, low);
37 if (K == 0) {
38 return {};
39 }
40
41 Eigen::VectorXi v = Eigen::VectorXi::LinSpaced(K, low, high);
42 return {v.data(), v.data() + K};
43 }
44
45 } // namespace math
46 } // namespace stan
47
48 #endif
49