[RFC,v2,08/12] ipa: libipa: vector: Add element-wise min() and max() functions
diff mbox series

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

Commit Message

Laurent Pinchart Nov. 18, 2024, 12:07 a.m. UTC
Add functions to calculate the element-wise minimum and maximum of two
vectors or of a vector and a scalar. This will be used in algorithm
implementations.

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

Comments

Milan Zamazal Nov. 18, 2024, 1:41 p.m. UTC | #1
Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> Add functions to calculate the element-wise minimum and maximum of two
> vectors or of a vector and a scalar. This will be used in algorithm
> implementations.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/ipa/libipa/vector.cpp | 28 ++++++++++++++++++++++++++++
>  src/ipa/libipa/vector.h   | 20 ++++++++++++++++++++
>  2 files changed, 48 insertions(+)
>
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index a45f08fde493..14816cdb5d09 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -183,6 +183,34 @@ namespace ipa {
>   * \return This vector
>   */
>  
> +/**
> + * \fn Vector::min(const Vector &other) const
> + * \brief Calculate the minimum of this vector and \a other element-wise
> + * \param[in] other The other vector
> + * \return The element-wise minimum of this vector and \a other
> + */
> +
> +/**
> + * \fn Vector::min(T scalar) const
> + * \brief Calculate the minimum of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise minimum of this vector and \a scalar
> + */
> +
> +/**
> + * \fn Vector::max(const Vector &other) const
> + * \brief Calculate the maximum of this vector and \a other element-wise
> + * \param[in] other The other vector
> + * \return The element-wise maximum of this vector and \a other
> + */
> +
> +/**
> + * \fn Vector::max(T scalar) const
> + * \brief Calculate the maximum of this vector and \a scalar element-wise
> + * \param[in] scalar The scalar
> + * \return The element-wise maximum of this vector and \a scalar
> + */
> +
>  /**
>   * \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 722ffc57ea45..949dc4650aa4 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -154,6 +154,26 @@ public:
>  		return apply(scalar, [](T a, T b) { return a / b; });
>  	}
>  
> +	constexpr Vector min(const Vector &other) const
> +	{
> +		return apply(*this, other, [](T a, T b) { return std::min(a, b); });
> +	}
> +
> +	constexpr Vector min(T scalar) const
> +	{
> +		return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });
> +	}
> +
> +	constexpr Vector max(const Vector &other) const
> +	{
> +		return apply(*this, other, [](T a, T b) { return std::max(a, b); });
> +	}
> +
> +	constexpr Vector max(T scalar) const
> +	{
> +		return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });
> +	}
> +

I wonder whether there could be a way to use std::max and std::min
directly, without wrapping them by lambdas.  I don't know so:

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

>  	constexpr T dot(const Vector<T, Rows> &other) const
>  	{
>  		T ret = 0;
Laurent Pinchart Nov. 18, 2024, 1:53 p.m. UTC | #2
On Mon, Nov 18, 2024 at 02:41:54PM +0100, Milan Zamazal wrote:
> Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:
> 
> > Add functions to calculate the element-wise minimum and maximum of two
> > vectors or of a vector and a scalar. This will be used in algorithm
> > implementations.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/ipa/libipa/vector.cpp | 28 ++++++++++++++++++++++++++++
> >  src/ipa/libipa/vector.h   | 20 ++++++++++++++++++++
> >  2 files changed, 48 insertions(+)
> >
> > diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> > index a45f08fde493..14816cdb5d09 100644
> > --- a/src/ipa/libipa/vector.cpp
> > +++ b/src/ipa/libipa/vector.cpp
> > @@ -183,6 +183,34 @@ namespace ipa {
> >   * \return This vector
> >   */
> >  
> > +/**
> > + * \fn Vector::min(const Vector &other) const
> > + * \brief Calculate the minimum of this vector and \a other element-wise
> > + * \param[in] other The other vector
> > + * \return The element-wise minimum of this vector and \a other
> > + */
> > +
> > +/**
> > + * \fn Vector::min(T scalar) const
> > + * \brief Calculate the minimum of this vector and \a scalar element-wise
> > + * \param[in] scalar The scalar
> > + * \return The element-wise minimum of this vector and \a scalar
> > + */
> > +
> > +/**
> > + * \fn Vector::max(const Vector &other) const
> > + * \brief Calculate the maximum of this vector and \a other element-wise
> > + * \param[in] other The other vector
> > + * \return The element-wise maximum of this vector and \a other
> > + */
> > +
> > +/**
> > + * \fn Vector::max(T scalar) const
> > + * \brief Calculate the maximum of this vector and \a scalar element-wise
> > + * \param[in] scalar The scalar
> > + * \return The element-wise maximum of this vector and \a scalar
> > + */
> > +
> >  /**
> >   * \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 722ffc57ea45..949dc4650aa4 100644
> > --- a/src/ipa/libipa/vector.h
> > +++ b/src/ipa/libipa/vector.h
> > @@ -154,6 +154,26 @@ public:
> >  		return apply(scalar, [](T a, T b) { return a / b; });
> >  	}
> >  
> > +	constexpr Vector min(const Vector &other) const
> > +	{
> > +		return apply(*this, other, [](T a, T b) { return std::min(a, b); });
> > +	}
> > +
> > +	constexpr Vector min(T scalar) const
> > +	{
> > +		return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });
> > +	}
> > +
> > +	constexpr Vector max(const Vector &other) const
> > +	{
> > +		return apply(*this, other, [](T a, T b) { return std::max(a, b); });
> > +	}
> > +
> > +	constexpr Vector max(T scalar) const
> > +	{
> > +		return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });
> > +	}
> > +
> 
> I wonder whether there could be a way to use std::max and std::min
> directly, without wrapping them by lambdas.  I don't know so:

Unfortunately that doesn't seem to be the case.

> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
> 
> >  	constexpr T dot(const Vector<T, Rows> &other) const
> >  	{
> >  		T ret = 0;
>

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index a45f08fde493..14816cdb5d09 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -183,6 +183,34 @@  namespace ipa {
  * \return This vector
  */
 
+/**
+ * \fn Vector::min(const Vector &other) const
+ * \brief Calculate the minimum of this vector and \a other element-wise
+ * \param[in] other The other vector
+ * \return The element-wise minimum of this vector and \a other
+ */
+
+/**
+ * \fn Vector::min(T scalar) const
+ * \brief Calculate the minimum of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise minimum of this vector and \a scalar
+ */
+
+/**
+ * \fn Vector::max(const Vector &other) const
+ * \brief Calculate the maximum of this vector and \a other element-wise
+ * \param[in] other The other vector
+ * \return The element-wise maximum of this vector and \a other
+ */
+
+/**
+ * \fn Vector::max(T scalar) const
+ * \brief Calculate the maximum of this vector and \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return The element-wise maximum of this vector and \a scalar
+ */
+
 /**
  * \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 722ffc57ea45..949dc4650aa4 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -154,6 +154,26 @@  public:
 		return apply(scalar, [](T a, T b) { return a / b; });
 	}
 
+	constexpr Vector min(const Vector &other) const
+	{
+		return apply(*this, other, [](T a, T b) { return std::min(a, b); });
+	}
+
+	constexpr Vector min(T scalar) const
+	{
+		return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });
+	}
+
+	constexpr Vector max(const Vector &other) const
+	{
+		return apply(*this, other, [](T a, T b) { return std::max(a, b); });
+	}
+
+	constexpr Vector max(T scalar) const
+	{
+		return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });
+	}
+
 	constexpr T dot(const Vector<T, Rows> &other) const
 	{
 		T ret = 0;