14#include <libcamera/base/span.h>
23template<
typename T,
unsigned int Rows,
unsigned int Cols,
24 std::enable_if_t<std::is_arithmetic_v<T>,
bool> =
true>
26template<
typename T,
unsigned int Rows,
unsigned int Cols>
33 data_.fill(
static_cast<T
>(0));
36 Matrix(
const std::array<T, Rows * Cols> &data)
38 std::copy(data.begin(), data.end(), data_.begin());
44 for (
size_t i = 0; i < std::min(Rows, Cols); i++)
45 ret[i][i] =
static_cast<T
>(1);
53 std::stringstream out;
56 for (
unsigned int i = 0; i < Rows; i++) {
58 for (
unsigned int j = 0; j < Cols; j++) {
60 out << ((j + 1 < Cols) ?
", " :
" ");
62 out << ((i + 1 < Rows) ?
"], " :
"]");
71 return Span<const T, Cols>{ &data_.data()[i * Cols], Cols };
76 return Span<T, Cols>{ &data_.data()[i * Cols], Cols };
80 template<
typename U, std::enable_if_t<std::is_arithmetic_v<U>>>
86 for (
unsigned int i = 0; i < Rows * Cols; i++)
92 std::array<T, Rows * Cols> data_;
96template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
97 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
99template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
105 for (
unsigned int i = 0; i < Rows; i++) {
106 for (
unsigned int j = 0; j < Cols; j++)
107 result[i][j] = d * m[i][j];
114template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
115 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
117template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
126 unsigned int R1,
unsigned int C1,
127 unsigned int R2,
unsigned int C2,
128 std::enable_if_t<C1 == R2> * =
nullptr>
130template<
typename T,
unsigned int R1,
unsigned int C1,
unsigned int R2,
unsigned in C2>
136 for (
unsigned int i = 0; i < R1; i++) {
137 for (
unsigned int j = 0; j < C2; j++) {
140 for (
unsigned int k = 0; k < C1; k++)
141 sum += m1[i][k] * m2[k][j];
150template<
typename T,
unsigned int Rows,
unsigned int Cols>
155 for (
unsigned int i = 0; i < Rows; i++) {
156 for (
unsigned int j = 0; j < Cols; j++)
157 result[i][j] = m1[i][j] + m2[i][j];
164bool matrixValidateYaml(
const YamlObject &obj,
unsigned int size);
168template<
typename T,
unsigned int Rows,
unsigned int Cols>
169std::ostream &
operator<<(std::ostream &out,
const Matrix<T, Rows, Cols> &m)
175template<
typename T,
unsigned int Rows,
unsigned int Cols>
176struct YamlObject::Getter<
Matrix<T, Rows, Cols>> {
177 std::optional<Matrix<T, Rows, Cols>>
get(
const YamlObject &obj)
const
179 if (!matrixValidateYaml(obj, Rows * Cols))
182 Matrix<T, Rows, Cols> matrix;
183 T *data = &matrix[0][0];
186 for (
const YamlObject &entry : obj.asList()) {
187 const auto value = entry.get<T>();
Matrix class.
Definition matrix.h:29
Span< T, Cols > operator[](size_t i)
Index to a row in the matrix.
Definition matrix.h:74
Matrix(const std::array< T, Rows *Cols > &data)
Construct a matrix from supplied data.
Definition matrix.h:36
Matrix< T, Rows, Cols > & operator*=(U d)
Multiply the matrix by a scalar in-place.
Definition matrix.h:84
Matrix()
Construct a zero matrix.
Definition matrix.h:31
const std::string toString() const
Assemble and return a string describing the matrix.
Definition matrix.h:51
static Matrix identity()
Construct an identity matrix.
Definition matrix.h:41
Span< const T, Cols > operator[](size_t i) const
Index to a row in the matrix.
Definition matrix.h:69
std::optional< T > get() const
Parse the YamlObject as a T value.
Definition yaml_parser.h:175
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:46
Top-level libcamera namespace.
Definition backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:101
Matrix< T, Rows, Cols > operator+(const Matrix< T, Rows, Cols > &m1, const Matrix< T, Rows, Cols > &m2)
Matrix addition.
Definition matrix.h:151