8#include <initializer_list>
25template <
typename Scalar,
int Rows,
int Cols>
36 std::initializer_list<std::initializer_list<Scalar>> list)
39 template <
typename Derived>
40 requires std::derived_from<Derived, Eigen::MatrixBase<Derived>>
42 constexpr ct_matrix(
const Derived& mat) : m_storage{mat} {}
49 constexpr int rows()
const {
return m_storage.rows(); }
56 constexpr int cols()
const {
return m_storage.cols(); }
64 constexpr const Scalar&
operator()(
int row,
int col)
const {
65 return m_storage(row, col);
74 constexpr Scalar&
operator()(
int row,
int col) {
return m_storage(row, col); }
82 requires(Rows == 1 || Cols == 1)
84 return m_storage(
index);
93 requires(Rows == 1 || Cols == 1)
95 return m_storage(
index);
107 if (std::is_constant_evaluated()) {
110 for (
int i = 0; i < rhs.
rows(); ++i) {
111 for (
int j = 0; j < rhs.
cols(); ++j) {
112 result(i, j) = lhs * rhs(i, j);
118 return lhs * rhs.m_storage;
134 if (std::is_constant_evaluated()) {
137 for (
int i = 0; i < lhs.
rows(); ++i) {
138 for (
int j = 0; j < rhs.
cols(); ++j) {
140 for (
int k = 0; k < lhs.
cols(); ++k) {
141 sum += lhs(i, k) * rhs(k, j);
149 return lhs.m_storage * rhs.
storage();
163 if (std::is_constant_evaluated()) {
166 for (
int row = 0; row < 3; ++row) {
167 for (
int col = 0; col < 3; ++col) {
168 result(row, col) = lhs(row, col) + rhs(row, col);
174 return lhs.m_storage + rhs.m_storage;
188 if (std::is_constant_evaluated()) {
191 for (
int row = 0; row < 3; ++row) {
192 for (
int col = 0; col < 3; ++col) {
193 result(row, col) = lhs(row, col) - rhs(row, col);
199 return lhs.m_storage - rhs.m_storage;
209 if (std::is_constant_evaluated()) {
212 for (
int row = 0; row <
rows(); ++row) {
213 for (
int col = 0; col <
cols(); ++col) {
214 result(col, row) = (*this)(row, col);
220 return m_storage.transpose().eval();
230 requires(Rows != Eigen::Dynamic && Cols != Eigen::Dynamic)
232 if (std::is_constant_evaluated()) {
235 for (
int row = 0; row < Rows; ++row) {
236 for (
int col = 0; col < Cols; ++col) {
238 result(row, row) = 1.0;
240 result(row, col) = 0.0;
247 return Eigen::Matrix<Scalar, Rows, Cols>::Identity();
259 template <
int RhsRows,
int RhsCols>
260 requires(Rows == 1 || Cols == 1) && (RhsRows == 1 || RhsCols == 1) &&
261 (Rows * Cols == RhsRows * RhsCols)
263 if (std::is_constant_evaluated()) {
272 return m_storage.dot(rhs.
storage());
281 constexpr Scalar
norm()
const {
282 if (std::is_constant_evaluated()) {
285 for (
int row = 0; row < Rows; ++row) {
286 for (
int col = 0; col < Cols; ++col) {
287 sum += (*this)(row, col) * (*
this)(row, col);
293 return m_storage.norm();
304 requires(Rows == 3 && Cols == 1)
306 return Eigen::Vector3d{{(*this)(1) * rhs(2) - rhs(1) * (*this)(2),
307 rhs(0) * (*this)(2) - (*
this)(0) * rhs(2),
308 (*this)(0) * rhs(1) - rhs(0) * (*this)(1)}};
317 requires(Rows == 2 && Cols == 2)
321 Scalar a = (*this)(0, 0);
322 Scalar b = (*this)(0, 1);
323 Scalar c = (*this)(1, 0);
324 Scalar d = (*this)(1, 1);
325 return a * d - b * c;
334 requires(Rows == 3 && Cols == 3)
339 Scalar a = (*this)(0, 0);
340 Scalar b = (*this)(0, 1);
341 Scalar c = (*this)(0, 2);
342 Scalar d = (*this)(1, 0);
343 Scalar e = (*this)(1, 1);
344 Scalar f = (*this)(1, 2);
345 Scalar g = (*this)(2, 0);
346 Scalar h = (*this)(2, 1);
347 Scalar i = (*this)(2, 2);
348 return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i -
357 constexpr const Eigen::Matrix<Scalar, Rows, Cols>&
storage()
const {
364 constexpr operator Eigen::Matrix<Scalar, Rows, Cols>()
const {
369 Eigen::Matrix<Scalar, Rows, Cols> m_storage;
372template <
typename Derived>
373 requires std::derived_from<Derived, Eigen::MatrixBase<Derived>>
375 ->
ct_matrix<
typename Derived::Scalar, Derived::RowsAtCompileTime,
376 Derived::ColsAtCompileTime>;
378template <
typename Scalar,
int Rows>
381template <
typename Scalar,
int Cols>
Compile-time wrapper for Eigen::Matrix.
Definition ct_matrix.h:26
constexpr ct_matrix(std::initializer_list< std::initializer_list< Scalar > > list)
Constructs a scalar VariableMatrix from a nested list of Variables.
Definition ct_matrix.h:35
constexpr Scalar dot(const ct_matrix< Scalar, RhsRows, RhsCols > &rhs) const
Constexpr version of Eigen's vector dot member function.
Definition ct_matrix.h:262
friend constexpr ct_matrix< Scalar, Rows, Cols > operator*(Scalar lhs, const ct_matrix< Scalar, Rows, Cols > &rhs)
Constexpr version of Eigen's scalar multiplication operator.
Definition ct_matrix.h:105
friend constexpr ct_matrix< Scalar, Rows, Cols > operator+(const ct_matrix< Scalar, Rows, Cols > &lhs, const ct_matrix< Scalar, Rows, Cols > &rhs)
Constexpr version of Eigen's matrix addition operator.
Definition ct_matrix.h:160
static constexpr ct_matrix< Scalar, Rows, Cols > Identity()
Constexpr version of Eigen's identity function.
Definition ct_matrix.h:229
friend constexpr ct_matrix< Scalar, Rows, Cols2 > operator*(const ct_matrix< Scalar, Rows, Cols > &lhs, const ct_matrix< Scalar, Rows, Cols2 > &rhs)
Constexpr version of Eigen's matrix multiplication operator.
Definition ct_matrix.h:131
constexpr Scalar determinant() const
Constexpr version of Eigen's 2x2 matrix determinant member function.
Definition ct_matrix.h:316
constexpr Scalar & operator()(int index)
Returns reference to matrix element.
Definition ct_matrix.h:92
constexpr Scalar & operator()(int row, int col)
Returns reference to matrix element.
Definition ct_matrix.h:74
constexpr ct_matrix()=default
constexpr const Scalar & operator()(int row, int col) const
Returns reference to matrix element.
Definition ct_matrix.h:64
friend constexpr ct_matrix< Scalar, Rows, Cols > operator-(const ct_matrix< Scalar, Rows, Cols > &lhs, const ct_matrix< Scalar, Rows, Cols > &rhs)
Constexpr version of Eigen's matrix subtraction operator.
Definition ct_matrix.h:185
constexpr int cols() const
Returns number of columns.
Definition ct_matrix.h:56
constexpr const Scalar & operator()(int index) const
Returns reference to matrix element.
Definition ct_matrix.h:81
constexpr Scalar norm() const
Constexpr version of Eigen's norm member function.
Definition ct_matrix.h:281
constexpr int rows() const
Returns number of rows.
Definition ct_matrix.h:49
constexpr ct_matrix< Scalar, 3, 1 > cross(const ct_matrix< Scalar, 3, 1 > &rhs)
Constexpr version of Eigen's 3D vector cross member function.
Definition ct_matrix.h:303
constexpr ct_matrix< Scalar, Cols, Rows > transpose() const
Constexpr version of Eigen's transpose member function.
Definition ct_matrix.h:208
constexpr ct_matrix(const Derived &mat)
Definition ct_matrix.h:42
constexpr Scalar determinant() const
Constexpr version of Eigen's 3x3 matrix determinant member function.
Definition ct_matrix.h:333
constexpr const Eigen::Matrix< Scalar, Rows, Cols > & storage() const
Returns the internal Eigen matrix.
Definition ct_matrix.h:357
Definition ct_matrix.h:17
ct_matrix(const Derived &) -> ct_matrix< typename Derived::Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime >
constexpr return_t< T > sqrt(const T x) noexcept
Compile-time square-root function.
Definition sqrt.hpp:109