[v3] ipa: libipa: vector: Add matrix-vector multiplication
diff mbox series

Message ID 20240614120211.184588-1-paul.elder@ideasonboard.com
State Superseded
Headers show
Series
  • [v3] ipa: libipa: vector: Add matrix-vector multiplication
Related show

Commit Message

Paul Elder June 14, 2024, 12:02 p.m. UTC
Add an operation for multiplying a matrix with a vector.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

---
Changes in v3:
- make SFINAE more concise

Changes in v2:
- s/D/Rows/ in Vector class

Depends on v9 of "ipa: libipa: Add Matrix class"
---
 src/ipa/libipa/vector.cpp | 11 +++++++++++
 src/ipa/libipa/vector.h   | 19 +++++++++++++++++++
 2 files changed, 30 insertions(+)

Comments

Laurent Pinchart June 16, 2024, 4:47 p.m. UTC | #1
Hi Paul,

Thank you for the patch.

On Fri, Jun 14, 2024 at 09:02:11PM +0900, Paul Elder wrote:
> Add an operation for multiplying a matrix with a vector.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> ---
> Changes in v3:
> - make SFINAE more concise
> 
> Changes in v2:
> - s/D/Rows/ in Vector class
> 
> Depends on v9 of "ipa: libipa: Add Matrix class"
> ---
>  src/ipa/libipa/vector.cpp | 11 +++++++++++
>  src/ipa/libipa/vector.h   | 19 +++++++++++++++++++
>  2 files changed, 30 insertions(+)
> 
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index b071b261b9c4..bd00b01961d5 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -123,6 +123,17 @@ namespace ipa {
>   * \return The length of the vector
>   */
>  
> +/**
> + * \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
> + * \tparam Rows The number of rows in the matrix
> + * \tparam Cols The number of columns in the matrix (= rows in the vector)
> + * \param m The matrix
> + * \param v The vector
> + * \return Product of matrix \a m and vector \a v
> + */
> +
>  /**
>   * \fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
>   * \brief Compare vectors for equality
> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> index 2a2906202ce4..bc7acc9d0a27 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -16,6 +16,8 @@
>  
>  #include "libcamera/internal/yaml_parser.h"
>  
> +#include "matrix.h"
> +
>  namespace libcamera {
>  
>  LOG_DECLARE_CATEGORY(Vector)
> @@ -140,6 +142,23 @@ private:
>  	std::array<T, Rows> data_;
>  };
>  
> +#ifndef __DOXYGEN__
> +template<typename T, unsigned int Rows, unsigned int Cols>
> +#endif /* __DOXYGEN__ */

You can drop the __DOXYGEN__ guard, the template doesn't need to be
hidden.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
> +{
> +	Vector<T, Rows> result;
> +
> +	for (unsigned int i = 0; i < Rows; i++) {
> +		T sum = 0;
> +		for (unsigned int j = 0; j < Cols; j++)
> +			sum += m[i][j] * v[j];
> +		result[i] = sum;
> +	}
> +
> +	return result;
> +}
> +
>  template<typename T, unsigned int Rows>
>  bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
>  {

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index b071b261b9c4..bd00b01961d5 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -123,6 +123,17 @@  namespace ipa {
  * \return The length of the vector
  */
 
+/**
+ * \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
+ * \tparam Rows The number of rows in the matrix
+ * \tparam Cols The number of columns in the matrix (= rows in the vector)
+ * \param m The matrix
+ * \param v The vector
+ * \return Product of matrix \a m and vector \a v
+ */
+
 /**
  * \fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
  * \brief Compare vectors for equality
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index 2a2906202ce4..bc7acc9d0a27 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -16,6 +16,8 @@ 
 
 #include "libcamera/internal/yaml_parser.h"
 
+#include "matrix.h"
+
 namespace libcamera {
 
 LOG_DECLARE_CATEGORY(Vector)
@@ -140,6 +142,23 @@  private:
 	std::array<T, Rows> data_;
 };
 
+#ifndef __DOXYGEN__
+template<typename T, unsigned int Rows, unsigned int Cols>
+#endif /* __DOXYGEN__ */
+Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
+{
+	Vector<T, Rows> result;
+
+	for (unsigned int i = 0; i < Rows; i++) {
+		T sum = 0;
+		for (unsigned int j = 0; j < Cols; j++)
+			sum += m[i][j] * v[j];
+		result[i] = sum;
+	}
+
+	return result;
+}
+
 template<typename T, unsigned int Rows>
 bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
 {