[PATCH v4 10/17] ipa: libipa: vector: Add sum() function
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Tue Nov 19 13:19:21 CET 2024
Add a function to calculate the sum of a vector. It will be useful for
algorithms.
Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal at redhat.com>
---
Changes since v1:
- Drop Vector::normalize()
---
src/ipa/libipa/vector.cpp | 12 ++++++++++++
src/ipa/libipa/vector.h | 7 +++++++
2 files changed, 19 insertions(+)
diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index a506487c7b43..d6e25e3e722d 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -302,6 +302,18 @@ namespace ipa {
* \return The length of the vector
*/
+/**
+ * \fn Vector::sum() const
+ * \brief Calculate the sum of all the vector elements
+ * \tparam R The type of the sum
+ *
+ * The type R of the sum defaults to the type T of the elements, but can be set
+ * explicitly to use a different type in case the type T would risk
+ * overflowing.
+ *
+ * \return The sum of all the vector elements
+ */
+
/**
* \fn Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
* \brief Multiply a matrix by a vector
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index 85b7f6ee68a7..5a2a29b53441 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -10,6 +10,7 @@
#include <array>
#include <cmath>
#include <functional>
+#include <numeric>
#include <optional>
#include <ostream>
@@ -243,6 +244,12 @@ public:
return std::sqrt(length2());
}
+ template<typename R = T>
+ constexpr R sum() const
+ {
+ return std::accumulate(data_.begin(), data_.end(), R{});
+ }
+
private:
template<class BinaryOp>
static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list