Skip to content
Snippets Groups Projects
Commit 3cd6a765 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add min/max/sum functions to SmallArray

parent 0f646a89
No related branches found
No related tags found
1 merge request!124Add files for high order integration with quadratures
......@@ -6,6 +6,7 @@
#include <utils/PugsAssert.hpp>
#include <utils/PugsMacros.hpp>
#include <utils/PugsUtils.hpp>
#include <utils/Types.hpp>
#include <algorithm>
......@@ -148,4 +149,60 @@ convert_to_small_array(const Container& given_container)
return array;
}
template <typename DataType>
[[nodiscard]] std::remove_const_t<DataType>
min(const SmallArray<DataType>& array)
{
using data_type = std::remove_const_t<DataType>;
using index_type = typename SmallArray<DataType>::index_type;
data_type min_value = std::numeric_limits<data_type>::max();
for (index_type i = 0; i < array.size(); ++i) {
if (array[i] < min_value) {
min_value = array[i];
}
}
return min_value;
}
template <typename DataType>
[[nodiscard]] std::remove_const_t<DataType>
max(const SmallArray<DataType>& array)
{
using data_type = std::remove_const_t<DataType>;
using index_type = typename SmallArray<DataType>::index_type;
data_type max_value = -std::numeric_limits<data_type>::max();
for (index_type i = 0; i < array.size(); ++i) {
if (array[i] > max_value) {
max_value = array[i];
}
}
return max_value;
}
template <typename DataType>
[[nodiscard]] std::remove_const_t<DataType>
sum(const SmallArray<DataType>& array)
{
using data_type = std::remove_const_t<DataType>;
using index_type = typename SmallArray<DataType>::index_type;
data_type sum_value = [] {
if constexpr (std::is_arithmetic_v<DataType>) {
return 0;
} else if constexpr (is_tiny_vector_v<DataType> or is_tiny_matrix_v<DataType>) {
return zero;
} else {
static_assert(std::is_arithmetic_v<DataType>, "invalid data type");
}
}();
for (index_type i = 0; i < array.size(); ++i) {
sum_value += array[i];
}
return sum_value;
}
#endif // SMALL_ARRAY_HPP
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <algebra/TinyMatrix.hpp>
#include <algebra/TinyVector.hpp>
#include <utils/PugsAssert.hpp>
#include <utils/SmallArray.hpp>
#include <utils/Types.hpp>
......@@ -281,5 +283,71 @@ TEST_CASE("SmallArray", "[utils]")
REQUIRE(array[i] == std::numeric_limits<int>::max() / 2);
}
}
SECTION("checking for SmallArray reductions")
{
SmallArray<int> a(10);
a[0] = 13;
a[1] = 1;
a[2] = 8;
a[3] = -3;
a[4] = 23;
a[5] = -1;
a[6] = 13;
a[7] = 0;
a[8] = 12;
a[9] = 9;
SECTION("Min")
{
REQUIRE(min(a) == -3);
}
SECTION("Max")
{
REQUIRE(max(a) == 23);
}
SECTION("Sum")
{
REQUIRE((sum(a) == 75));
}
SECTION("TinyVector Sum")
{
using N2 = TinyVector<2, int>;
SmallArray<N2> b(10);
b[0] = {13, 2};
b[1] = {1, 3};
b[2] = {8, -2};
b[3] = {-3, 2};
b[4] = {23, 4};
b[5] = {-1, -3};
b[6] = {13, 17};
b[7] = {0, 9};
b[8] = {12, 13};
b[9] = {9, -17};
REQUIRE((sum(b) == N2{75, 28}));
}
SECTION("TinyMatrix Sum")
{
using N22 = TinyMatrix<2, 2, int>;
SmallArray<N22> b(10);
b[0] = {13, 2, 0, 1};
b[1] = {1, 3, 6, 3};
b[2] = {8, -2, -1, 21};
b[3] = {-3, 2, 5, 12};
b[4] = {23, 4, 7, 1};
b[5] = {-1, -3, 33, 11};
b[6] = {13, 17, 12, 13};
b[7] = {0, 9, 1, 14};
b[8] = {12, 13, -3, -71};
b[9] = {9, -17, 0, 16};
REQUIRE((sum(b) == N22{75, 28, 60, 21}));
}
}
#endif // NDEBUG
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment