Skip to content

Commit 2ff171a

Browse files
committed
Add utitlity for creating evenly spaced numbers in a given range
1 parent 90bc9a9 commit 2ff171a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/rlenvs/utils/maths/math_utils.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
///
66

77
#include <algorithm>
8+
#include <cmath>
9+
#include <vector>
10+
#include <cassert>
811

912
namespace rlenvscpp{
1013
namespace utils{
@@ -104,6 +107,41 @@ min(const VectorType& vec) {
104107
return *std::min_element(vec.begin(), vec.end());
105108
}
106109

110+
namespace{
111+
112+
///
113+
/// \brief Return a value in the linear sequence in the range
114+
/// [start, stop]. The index parameter is the current number
115+
/// in the sequence of the n total numbers we are about to
116+
/// generate
117+
///
118+
template<typename T>
119+
auto lin_value(T start, T stop, uint_t index, uint_t n){
120+
121+
assert(n > 1 && index < n);
122+
const auto amount = static_cast<T>(index) / (n -1);
123+
const auto v = str::lerp(start, stop, amount);
124+
return v;
125+
}
126+
127+
}
128+
129+
///
130+
/// \brief Generate n evenly linearly spaced numbers between
131+
/// [start, stop]. Analogous to NumPy linspace.
132+
///
133+
template<typename T>
134+
std::vector<T>
135+
lin_space(T start, T stop, uint_t n){
136+
137+
auto v = std::vector<T>{};
138+
v.reserve(n);
139+
for(auto i=0u; i<n; ++i){
140+
v.push_back(lin_value(start, stop, i, n));
141+
}
142+
return v;
143+
}
144+
107145

108146
}
109147
}

0 commit comments

Comments
 (0)