[PATCH v3 10/17] ipa: libipa: vector: Add sum() function
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Mon Nov 18 23:16:11 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 56c494a7e4da..a1ad47cd046b 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 3fed9570bf62..564292743829 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>
@@ -251,6 +252,12 @@ public:
return std::sqrt(length2());
}
+ template<typename R = T>
+ constexpr R sum() const
+ {
+ return std::accumulate(data_.begin(), data_.end(), R{});
+ }
+
private:
static constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function<T(T, T)> func)
{
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list