19#ifndef SRC_UTILS_INCLUDE_UTILS_MATRIX_HPP
20#define SRC_UTILS_INCLUDE_UTILS_MATRIX_HPP
42#include <boost/qvm/mat_operations.hpp>
43#include <boost/qvm/vec_mat_operations.hpp>
44#include <boost/qvm/vec_operations.hpp>
47#include <boost/qvm/deduce_mat.hpp>
48#include <boost/qvm/deduce_scalar.hpp>
49#include <boost/qvm/deduce_vec.hpp>
50#include <boost/qvm/map_mat_mat.hpp>
51#include <boost/qvm/map_mat_vec.hpp>
52#include <boost/qvm/map_vec_mat.hpp>
53#include <boost/qvm/mat.hpp>
54#include <boost/qvm/mat_access.hpp>
55#include <boost/qvm/mat_traits.hpp>
65template <
typename T, std::
size_t Rows, std::
size_t Cols>
struct Matrix {
79 template <
class Archive>
void serialize(Archive &ar,
const unsigned int) {
85 Matrix(std::initializer_list<T> init_list) {
86 assert(init_list.size() == Rows * Cols);
87 std::copy(init_list.begin(), init_list.end(),
begin());
89 Matrix(std::initializer_list<std::initializer_list<T>> init_list) {
90 assert(init_list.size() == Rows);
155 static_assert(R < Rows,
"Invalid row index.");
156 return boost::qvm::row<R>(*
this);
164 static_assert(C < Cols,
"Invalid column index.");
165 return boost::qvm::col<C>(*
this);
172 static_assert(Rows == Cols,
173 "Diagonal can only be retrieved from square matrices.");
174 return boost::qvm::diag(*
this);
182 return std::accumulate(d.begin(), d.end(), T{}, std::plus<T>{});
190 return boost::qvm::transposed(*
this);
198 static_assert(Rows == Cols,
199 "Inversion of a non-square matrix not implemented.");
200 return boost::qvm::inverse(*
this);
206 constexpr std::pair<std::size_t, std::size_t>
shape() const noexcept {
211using boost::qvm::operator+;
212using boost::qvm::operator+=;
213using boost::qvm::operator-;
214using boost::qvm::operator-=;
215using boost::qvm::operator*;
216using boost::qvm::operator*=;
217using boost::qvm::operator==;
219template <
typename T, std::
size_t M, std::
size_t N>
224template <
typename T, std::
size_t Rows, std::
size_t Cols>
226 static_assert(Rows == Cols,
"Diagonal matrix has to be a square matrix.");
227 return boost::qvm::diag_mat(v);
230template <
typename T, std::
size_t Rows, std::
size_t Cols>
232 static_assert(Rows == Cols,
233 "Identity matrix only defined for square matrices.");
234 return boost::qvm::identity_mat<T, Rows>();
242template <
typename T, std::
size_t Rows, std::
size_t Cols>
243struct mat_traits<
Utils::Matrix<T, Rows, Cols>> {
245 static int const rows = Rows;
246 static int const cols = Cols;
249 template <std::
size_t R, std::
size_t C>
251 static_assert(R < Rows,
"Invalid row index.");
252 static_assert(C < Cols,
"Invalid column index.");
256 template <std::
size_t R, std::
size_t C>
258 static_assert(R < Rows,
"Invalid row index.");
259 static_assert(C < Cols,
"Invalid column index.");
277template <
typename T,
typename U>
282template <
typename T,
typename U>
287template <
typename T,
typename U>
292template <
typename T,
typename U>
297template <
typename T,
typename U>
Array implementation with CUDA support.
Vector implementation and trait types for boost qvm interoperability.
void flatten(Range const &v, OutputIterator out)
Flatten a range of ranges.
Matrix< T, Rows, Cols > identity_mat()
Matrix< T, Rows, Cols > diagonal_mat(Utils::Vector< T, Rows > const &v)
DEVICE_QUALIFIER constexpr pointer data() noexcept
DEVICE_QUALIFIER constexpr iterator begin() noexcept
const value_type & const_reference
const value_type * const_pointer
DEVICE_QUALIFIER constexpr iterator end() noexcept
const value_type * const_iterator
Matrix representation with static size.
constexpr iterator begin() noexcept
Iterator access (non const).
Matrix< T, Cols, Rows > transposed() const
Retrieve a transposed copy of the matrix.
constexpr const_pointer data() const noexcept
Access to the underlying data pointer (non const).
typename container::pointer pointer
typename container::const_pointer const_pointer
Vector< T, Cols > diagonal() const
Retrieve the diagonal.
Matrix(std::initializer_list< std::initializer_list< T > > init_list)
typename container::const_reference const_reference
constexpr const_iterator end() const noexcept
Iterator access (non const).
T trace() const
Retrieve the trace.
Vector< T, Cols > row() const
Retrieve an entire matrix row.
typename container::iterator iterator
Matrix< T, Rows, Cols > inversed() const
Retrieve an inverted copy of the matrix.
Vector< T, Rows > col() const
Retrieve an entire matrix column.
constexpr std::pair< std::size_t, std::size_t > shape() const noexcept
Retrieve the shape of the matrix.
typename container::value_type value_type
typename container::reference reference
typename container::const_iterator const_iterator
friend class boost::serialization::access
constexpr iterator end() noexcept
Iterator access (non const).
constexpr pointer data()
Access to the underlying data pointer (non const).
constexpr value_type operator()(std::size_t row, std::size_t col) const
Element access (const).
constexpr const_iterator begin() const noexcept
Iterator access (const).
constexpr reference operator()(std::size_t row, std::size_t col)
Element access (non const).
Matrix(std::initializer_list< T > init_list)
static scalar_type read_element_idx(std::size_t r, std::size_t c, mat_type const &m)
static scalar_type & write_element_idx(std::size_t r, std::size_t c, mat_type &m)
static scalar_type read_element(mat_type const &m)
static scalar_type & write_element(mat_type &m)
typename Utils::Matrix< T, Rows, Cols > mat_type