added helper function to make view of vector

This commit is contained in:
froejdh_e
2025-04-22 09:24:13 +02:00
parent fa6f33fd85
commit d75a870e37
3 changed files with 18 additions and 1 deletions

View File

@@ -184,4 +184,9 @@ std::ostream& operator <<(std::ostream& os, const NDView<T, Ndim>& arr){
}
template <typename T>
NDView<T,1> make_view(std::vector<T>& vec){
return NDView<T,1>(vec.data(), {static_cast<int64_t>(vec.size())});
}
} // namespace aare

View File

@@ -190,4 +190,16 @@ TEST_CASE("compare two views") {
NDView<int, 2> view2(vec2.data(), Shape<2>{3, 4});
REQUIRE((view1 == view2));
}
TEST_CASE("Create a view over a vector"){
std::vector<int> vec;
for (int i = 0; i != 12; ++i) {
vec.push_back(i);
}
auto v = aare::make_view(vec);
REQUIRE(v.shape()[0] == 12);
REQUIRE(v[0] == 0);
REQUIRE(v[11] == 11);
}

View File

@@ -90,7 +90,7 @@ void apply_custom_weights(NDView<uint16_t, 1> input, NDView<double, 1> output, c
// Apply custom weights to each element in the input array
for (ssize_t i = 0; i < input.shape(0); i++) {
double result = 0.0;
for (ssize_t bit_index = 0; bit_index < weights_powers.size(); ++bit_index) {
for (size_t bit_index = 0; bit_index < weights_powers.size(); ++bit_index) {
result += ((input(i) >> bit_index) & 1) * weights_powers[bit_index];
}
output(i) = result;