[RFC,07/11] ipa: libipa: vector: Add compound assignment operators
diff mbox series

Message ID 20241117221712.29616-8-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • Improve linear algebra helpers in libipa
Related show

Commit Message

Laurent Pinchart Nov. 17, 2024, 10:17 p.m. UTC
Extend the Vector class with compound assignment operators that match
the binary arithmetic operators.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/ipa/libipa/vector.cpp | 56 ++++++++++++++++++++++++++++++++++++++
 src/ipa/libipa/vector.h   | 57 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index d0e38f402220..a45f08fde493 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -127,6 +127,62 @@  namespace ipa {
  * \return The element-wise division of this vector by \a scalar
  */
 
+/**
+ * \fn Vector::operator+=(Vector const &other)
+ * \brief Add \a other element-wise to this vector
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator+=(T scalar)
+ * \brief Add \a scalar element-wise to this vector
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator-=(Vector const &other)
+ * \brief Subtract \a other element-wise from this vector
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator-=(T scalar)
+ * \brief Subtract \a scalar element-wise from this vector
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator*=(const Vector &other)
+ * \brief Multiply this vector by \a other element-wise
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator*=(T scalar)
+ * \brief Multiply this vector by \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator/=(const Vector &other)
+ * \brief Divide this vector by \a other element-wise
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator/=(T scalar)
+ * \brief Divide this vector by \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
 /**
  * \fn Vector::dot(const Vector<T, Rows> &other) const
  * \brief Compute the dot product
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index dda49b1f468b..722ffc57ea45 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -114,6 +114,46 @@  public:
 		return apply(*this, scalar, [](T a, T b) { return a / b; });
 	}
 
+	Vector &operator+=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a + b; });
+	}
+
+	Vector &operator+=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a + b; });
+	}
+
+	Vector &operator-=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a - b; });
+	}
+
+	Vector &operator-=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a - b; });
+	}
+
+	Vector &operator*=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a * b; });
+	}
+
+	Vector &operator*=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a * b; });
+	}
+
+	Vector &operator/=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a / b; });
+	}
+
+	Vector &operator/=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a / b; });
+	}
+
 	constexpr T dot(const Vector<T, Rows> &other) const
 	{
 		T ret = 0;
@@ -206,6 +246,23 @@  private:
 		return result;
 	}
 
+	Vector &apply(const Vector &other, std::function<T(T, T)> func)
+	{
+		auto itOther = other.data_.begin();
+		std::for_each(data_.begin(), data_.end(),
+			      [&func, &itOther](T &v) { v = func(v, *itOther++); });
+
+		return *this;
+	}
+
+	Vector &apply(T scalar, std::function<T(T, T)> func)
+	{
+		std::for_each(data_.begin(), data_.end(),
+			      [&func, scalar](T &v) { v = func(v, scalar); });
+
+		return *this;
+	}
+
 	std::array<T, Rows> data_;
 };