1#ifndef PBAT_MATH_LINALG_MINI_EIGEN_H
2#define PBAT_MATH_LINALG_MINI_EIGEN_H
16template <
class TMatrix>
21 } -> std::convertible_to<typename TMatrix::ScalarType*>;
24template <
class TMatrix>
25Eigen::Map<Eigen::Matrix<
26 typename std::remove_cvref_t<TMatrix>::ScalarType,
27 std::remove_cvref_t<TMatrix>::kRows,
28 std::remove_cvref_t<TMatrix>::kCols,
29 (std::remove_cvref_t<TMatrix>::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>>
32 using MatrixType = std::remove_cvref_t<TMatrix>;
34 using EigenMatrixType = Eigen::Matrix<
35 typename MatrixType::ScalarType,
38 (MatrixType::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>;
39 return Eigen::Map<EigenMatrixType>(A.Data());
42template <
class TMatrix>
43Eigen::Map<Eigen::Matrix<
44 typename std::remove_cvref_t<TMatrix>::ScalarType,
45 std::remove_cvref_t<TMatrix>::kRows,
46 std::remove_cvref_t<TMatrix>::kCols,
47 (std::remove_cvref_t<TMatrix>::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor> const>
48ToEigen(TMatrix const& A)
50 using MatrixType = std::remove_cvref_t<TMatrix>;
51 static_assert(CEigenConvertible<MatrixType>,
"A must satisfy CEigenConvertible");
52 using EigenMatrixType = Eigen::Matrix<
53 typename MatrixType::ScalarType,
56 (MatrixType::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>;
57 return Eigen::Map<EigenMatrixType const>(A.Data());
60template <
class TDerived>
61class ConstEigenMatrixWrapper
64 using ScalarType =
typename TDerived::Scalar;
65 using SelfType = ConstEigenMatrixWrapper<TDerived>;
67 static int constexpr kRows = TDerived::RowsAtCompileTime;
68 static int constexpr kCols = TDerived::ColsAtCompileTime;
69 static bool constexpr bRowMajor = TDerived::IsRowMajor;
71 PBAT_HOST_DEVICE ConstEigenMatrixWrapper(TDerived
const& A) : mA(A)
73 static_assert(TDerived::RowsAtCompileTime > 0,
"A must have compile-time row dimensions");
75 TDerived::ColsAtCompileTime > 0,
76 "A must have compile-time column dimensions");
79 PBAT_HOST_DEVICE ScalarType operator()(
auto i,
auto j)
const {
return mA(i, j); }
82 PBAT_HOST_DEVICE ScalarType operator()(
auto i)
const {
return mA.reshaped()(i); }
83 PBAT_HOST_DEVICE ScalarType operator[](
auto i)
const {
return (*
this)(i); }
85 PBAT_MINI_READ_API(SelfType)
91template <
class TDerived>
92class EigenMatrixWrapper
95 using ScalarType =
typename TDerived::Scalar;
96 using SelfType = EigenMatrixWrapper<TDerived>;
98 static int constexpr kRows = TDerived::RowsAtCompileTime;
99 static int constexpr kCols = TDerived::ColsAtCompileTime;
100 static bool constexpr bRowMajor = TDerived::IsRowMajor;
102 PBAT_HOST_DEVICE EigenMatrixWrapper(TDerived& A) : mA(A)
104 static_assert(TDerived::RowsAtCompileTime > 0,
"A must have compile-time row dimensions");
106 TDerived::ColsAtCompileTime > 0,
107 "A must have compile-time column dimensions");
110 PBAT_HOST_DEVICE
void SetConstant(ScalarType k) { Assign(*
this, k); }
112 PBAT_HOST_DEVICE ScalarType operator()(
auto i,
auto j)
const {
return mA(i, j); }
113 PBAT_HOST_DEVICE ScalarType& operator()(
auto i,
auto j) {
return mA(i, j); }
116 PBAT_HOST_DEVICE ScalarType operator()(
auto i)
const {
return mA.reshaped()(i); }
117 PBAT_HOST_DEVICE ScalarType operator[](
auto i)
const {
return (*
this)(i); }
118 PBAT_HOST_DEVICE ScalarType& operator()(
auto i) {
return mA.reshaped()(i); }
119 PBAT_HOST_DEVICE ScalarType& operator[](
auto i) {
return (*
this)(i); }
121 PBAT_MINI_READ_WRITE_API(SelfType)
127template <
class TDerived>
128auto FromEigen(TDerived& A)
133template <
class TDerived>
134auto FromEigen(TDerived
const& A)
136 return ConstEigenMatrixWrapper<TDerived>(A);
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
The main namespace of the library.
Definition Aliases.h:15