summaryrefslogtreecommitdiff
path: root/src/ipa
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-16 21:02:52 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-26 19:05:19 +0200
commit5ad6b3b1bbd4d1c8d0e7cf38bc6dc1d29a978381 (patch)
treebc8b488d428f747bb912bec4b7d2c0a6bf1e5c9b /src/ipa
parent2a29a2a6a1784223a9caf1fae2a768fe550b5f64 (diff)
ipa: libipa: vector: Add sum() function
Add a function to calculate the sum of a vector. It will be useful for algorithms. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
Diffstat (limited to 'src/ipa')
-rw-r--r--src/ipa/libipa/vector.cpp12
-rw-r--r--src/ipa/libipa/vector.h7
2 files changed, 19 insertions, 0 deletions
diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index d29c1642..8019f8cf 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -290,6 +290,18 @@ namespace ipa {
*/
/**
+ * \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
* \tparam T Numerical type of the contents of the matrix and vector
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index 937a28e9..9bdd54b6 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>
@@ -239,6 +240,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)