[v3,07/17] ipa: libipa: vector: Add missing binary arithemtic operators
diff mbox series

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

Commit Message

Laurent Pinchart Nov. 18, 2024, 10:16 p.m. UTC
The Vector class defines multiple element-wise arithmetic operators
between vectors or between a vector and a scalar. A few variants are
missing. Add them.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
Changes since v2:

- Use std::plus, std::minus, std::multiplies and std::divides
---
 src/ipa/libipa/vector.cpp | 54 +++++++++++++++++++++++++++++----------
 src/ipa/libipa/vector.h   | 38 ++++++++++++++++++++-------
 2 files changed, 70 insertions(+), 22 deletions(-)

Comments

Milan Zamazal Nov. 19, 2024, 10:24 a.m. UTC | #1
Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> The Vector class defines multiple element-wise arithmetic operators
> between vectors or between a vector and a scalar. A few variants are
> missing. Add them.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Milan Zamazal <mzamazal@redhat.com>

> ---
> Changes since v2:
>
> - Use std::plus, std::minus, std::multiplies and std::divides
> ---
>  src/ipa/libipa/vector.cpp | 54 +++++++++++++++++++++++++++++----------
>  src/ipa/libipa/vector.h   | 38 ++++++++++++++++++++-------
>  2 files changed, 70 insertions(+), 22 deletions(-)
>
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index 143ee9262f05..d8f7ce951d60 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -77,32 +77,60 @@ namespace ipa {
>   * \return The negated vector
>   */
>  
> +/**
> + * \fn Vector::operator+(Vector const &other) const
> + * \brief Calculate the sum of this vector and \a other element-wise
> + * \param[in] other The other vector
> + * \return The element-wise sum of this vector and \a other
> + */
> +
> +/**
> + * \fn Vector::operator+(T scalar) const
> + * \brief Calculate the sum of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise sum of this vector and \a other
> + */
> +
>  /**
>   * \fn Vector::operator-(Vector const &other) const
> - * \brief Subtract one vector from another
> + * \brief Calculate the difference of this vector and \a other element-wise
>   * \param[in] other The other vector
> - * \return The difference of \a other from this vector
> + * \return The element-wise subtraction of \a other from this vector
>   */
>  
>  /**
> - * \fn Vector::operator+()
> - * \brief Add two vectors together
> + * \fn Vector::operator-(T scalar) const
> + * \brief Calculate the difference of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise subtraction of \a scalar from this vector
> + */
> +
> +/**
> + * \fn Vector::operator*(const Vector &other) const
> + * \brief Calculate the product of this vector and \a other element-wise
>   * \param[in] other The other vector
> - * \return The sum of the two vectors
> + * \return The element-wise product of this vector and \a other
>   */
>  
>  /**
> - * \fn Vector::operator*(T factor) const
> - * \brief Multiply the vector by a scalar
> - * \param[in] factor The factor
> - * \return The vector multiplied by \a factor
> + * \fn Vector::operator*(T scalar) const
> + * \brief Calculate the product of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise product of this vector and \a scalar
>   */
>  
>  /**
> - * \fn Vector::operator/()
> - * \brief Divide the vector by a scalar
> - * \param[in] factor The factor
> - * \return The vector divided by \a factor
> + * \fn Vector::operator/(const Vector &other) const
> + * \brief Calculate the quotient of this vector and \a other element-wise
> + * \param[in] other The other vector
> + * \return The element-wise division of this vector by \a other
> + */
> +
> +/**
> + * \fn Vector::operator/(T scalar) const
> + * \brief Calculate the quotient of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise division of this vector by \a scalar
>   */
>  
>  /**
> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> index 76fd2934b870..88f2a03fe579 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -80,24 +80,44 @@ public:
>  		return ret;
>  	}
>  
> -	constexpr Vector operator-(const Vector &other) const
> -	{
> -		return apply(*this, other, std::minus<>{});
> -	}
> -
>  	constexpr Vector operator+(const Vector &other) const
>  	{
>  		return apply(*this, other, std::plus<>{});
>  	}
>  
> -	constexpr Vector operator*(T factor) const
> +	constexpr Vector operator+(T scalar) const
>  	{
> -		return apply(*this, factor, std::multiplies<>{});
> +		return apply(*this, scalar, std::plus<>{});
>  	}
>  
> -	constexpr Vector operator/(T factor) const
> +	constexpr Vector operator-(const Vector &other) const
>  	{
> -		return apply(*this, factor, std::divides<>{});
> +		return apply(*this, other, std::minus<>{});
> +	}
> +
> +	constexpr Vector operator-(T scalar) const
> +	{
> +		return apply(*this, scalar, std::minus<>{});
> +	}
> +
> +	constexpr Vector operator*(const Vector &other) const
> +	{
> +		return apply(*this, other, std::multiplies<>{});
> +	}
> +
> +	constexpr Vector operator*(T scalar) const
> +	{
> +		return apply(*this, scalar, std::multiplies<>{});
> +	}
> +
> +	constexpr Vector operator/(const Vector &other) const
> +	{
> +		return apply(*this, other, std::divides<>{});
> +	}
> +
> +	constexpr Vector operator/(T scalar) const
> +	{
> +		return apply(*this, scalar, std::divides<>{});
>  	}
>  
>  	constexpr T dot(const Vector<T, Rows> &other) const

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index 143ee9262f05..d8f7ce951d60 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -77,32 +77,60 @@  namespace ipa {
  * \return The negated vector
  */
 
+/**
+ * \fn Vector::operator+(Vector const &other) const
+ * \brief Calculate the sum of this vector and \a other element-wise
+ * \param[in] other The other vector
+ * \return The element-wise sum of this vector and \a other
+ */
+
+/**
+ * \fn Vector::operator+(T scalar) const
+ * \brief Calculate the sum of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise sum of this vector and \a other
+ */
+
 /**
  * \fn Vector::operator-(Vector const &other) const
- * \brief Subtract one vector from another
+ * \brief Calculate the difference of this vector and \a other element-wise
  * \param[in] other The other vector
- * \return The difference of \a other from this vector
+ * \return The element-wise subtraction of \a other from this vector
  */
 
 /**
- * \fn Vector::operator+()
- * \brief Add two vectors together
+ * \fn Vector::operator-(T scalar) const
+ * \brief Calculate the difference of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise subtraction of \a scalar from this vector
+ */
+
+/**
+ * \fn Vector::operator*(const Vector &other) const
+ * \brief Calculate the product of this vector and \a other element-wise
  * \param[in] other The other vector
- * \return The sum of the two vectors
+ * \return The element-wise product of this vector and \a other
  */
 
 /**
- * \fn Vector::operator*(T factor) const
- * \brief Multiply the vector by a scalar
- * \param[in] factor The factor
- * \return The vector multiplied by \a factor
+ * \fn Vector::operator*(T scalar) const
+ * \brief Calculate the product of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise product of this vector and \a scalar
  */
 
 /**
- * \fn Vector::operator/()
- * \brief Divide the vector by a scalar
- * \param[in] factor The factor
- * \return The vector divided by \a factor
+ * \fn Vector::operator/(const Vector &other) const
+ * \brief Calculate the quotient of this vector and \a other element-wise
+ * \param[in] other The other vector
+ * \return The element-wise division of this vector by \a other
+ */
+
+/**
+ * \fn Vector::operator/(T scalar) const
+ * \brief Calculate the quotient of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise division of this vector by \a scalar
  */
 
 /**
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index 76fd2934b870..88f2a03fe579 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -80,24 +80,44 @@  public:
 		return ret;
 	}
 
-	constexpr Vector operator-(const Vector &other) const
-	{
-		return apply(*this, other, std::minus<>{});
-	}
-
 	constexpr Vector operator+(const Vector &other) const
 	{
 		return apply(*this, other, std::plus<>{});
 	}
 
-	constexpr Vector operator*(T factor) const
+	constexpr Vector operator+(T scalar) const
 	{
-		return apply(*this, factor, std::multiplies<>{});
+		return apply(*this, scalar, std::plus<>{});
 	}
 
-	constexpr Vector operator/(T factor) const
+	constexpr Vector operator-(const Vector &other) const
 	{
-		return apply(*this, factor, std::divides<>{});
+		return apply(*this, other, std::minus<>{});
+	}
+
+	constexpr Vector operator-(T scalar) const
+	{
+		return apply(*this, scalar, std::minus<>{});
+	}
+
+	constexpr Vector operator*(const Vector &other) const
+	{
+		return apply(*this, other, std::multiplies<>{});
+	}
+
+	constexpr Vector operator*(T scalar) const
+	{
+		return apply(*this, scalar, std::multiplies<>{});
+	}
+
+	constexpr Vector operator/(const Vector &other) const
+	{
+		return apply(*this, other, std::divides<>{});
+	}
+
+	constexpr Vector operator/(T scalar) const
+	{
+		return apply(*this, scalar, std::divides<>{});
 	}
 
 	constexpr T dot(const Vector<T, Rows> &other) const