24#ifndef VULPES_UTILS_EIGEN_HPP_
25#define VULPES_UTILS_EIGEN_HPP_
37template <Eigen::Index CrossId,
class ArgType>
43template <Eigen::Index CrossId,
class ArgType>
48 typedef typename ArgType::Scalar
Scalar;
62template <Eigen::Index CrossId,
typename ArgType>
64 : evaluator_base<EigenVulpes::SymMat<CrossId, ArgType>> {
66 typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type
85 }
else if (col == CrossId) {
110template <Eigen::Index CrossId,
class ArgType>
111class SymMat :
public Eigen::MatrixBase<SymMat<CrossId, ArgType>> {
115 ArgType::ColsAtCompileTime == 1,
116 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
119 typedef typename Eigen::internal::ref_selector<SymMat>::type
Nested;
125 typedef typename Eigen::internal::ref_selector<ArgType>::type
ArgTypeNested;
136template <Eigen::Index CrossId,
class ArgType>
148template <Eigen::Index RowId,
class ArgType>
154template <Eigen::Index RowId,
class ArgType>
173template <Eigen::Index RowId,
typename ArgType>
175 : evaluator_base<EigenVulpes::RowMat<RowId, ArgType>> {
177 typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type
216template <Eigen::Index RowId,
class ArgType>
217class RowMat :
public Eigen::MatrixBase<RowMat<RowId, ArgType>> {
221 ArgType::ColsAtCompileTime == 1,
222 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
225 typedef typename Eigen::internal::ref_selector<RowMat>::type
Nested;
231 typedef typename Eigen::internal::ref_selector<ArgType>::type
ArgTypeNested;
242template <Eigen::Index RowId,
class ArgType>
253template <Eigen::Index Idx,
typename Scalar_, Eigen::Index Size_>
259template <Eigen::Index Idx,
typename Scalar_, Eigen::Index Size_>
260struct traits<
EigenVulpes::OneComponentVector<Idx, Scalar_, Size_>> {
271 Flags = Eigen::ColMajor | Eigen::PacketAccessBit
280template <Eigen::Index Idx,
typename Scalar_, Eigen::Index Size_>
282 : evaluator_base<EigenVulpes::OneComponentVector<Idx, Scalar_, Size_>> {
293 EIGEN_STRONG_INLINE Scalar_
coeff(Index row, Index col = 0)
const {
294 EIGEN_UNUSED_VARIABLE(col);
295 return row == Idx ?
m_value : Scalar_(0);
316template <Eigen::Index Idx,
typename Scalar_, Eigen::Index Size_>
318 :
public Eigen::MatrixBase<OneComponentVector<Idx, Scalar_, Size_>> {
324 using Base = Eigen::MatrixBase<OneComponentVector<Idx, Scalar_, Size_>>;
326 typename Eigen::internal::ref_selector<OneComponentVector>::type;
334 EIGEN_UNUSED_VARIABLE(col);
354template <
typename Derived>
355auto toMap(Eigen::MatrixBase<Derived>& mat) {
356 return Eigen::Map<Eigen::Matrix<
357 typename Derived::Scalar, Derived::RowsAtCompileTime,
358 Derived::ColsAtCompileTime>>(
359 mat.derived().data(), mat.rows(), mat.cols());
368template <
typename Derived>
370 return Eigen::Map<
const Eigen::Matrix<
371 typename Derived::Scalar, Derived::RowsAtCompileTime,
372 Derived::ColsAtCompileTime>>(
373 mat.derived().data(), mat.rows(), mat.cols());
389 const Eigen::MatrixXd& mat1,
390 const Eigen::MatrixXd& mat2,
391 const double tolerance = 1e-12) {
392 if (mat1.rows() != mat2.rows() || mat1.cols() != mat2.cols()) {
393 std::cout <<
"Matrix sizes do not match: (" << mat1.rows() <<
","
394 << mat1.cols() <<
") vs (" << mat2.rows() <<
"," << mat2.cols()
399 double norm_diff = (mat1 - mat2).norm();
400 double norm_ref = mat2.norm();
402 bool result = norm_diff < tolerance * (norm_ref + 1.0);
405 std::cout <<
"Matrix comparison failed: ||diff||=" << norm_diff
406 <<
", ||ref||=" << norm_ref <<
", tolerance=" << tolerance
426 const Eigen::VectorXd& vec1,
427 const Eigen::VectorXd& vec2,
428 const double tolerance = 1e-12) {
429 if (vec1.size() != vec2.size()) {
430 std::cout <<
"Vector sizes do not match: " << vec1.size() <<
" vs "
431 << vec2.size() << std::endl;
435 double norm_diff = (vec1 - vec2).norm();
436 double norm_ref = vec2.norm();
438 bool result = norm_diff < tolerance * (norm_ref + 1.0);
441 std::cout <<
"Vector comparison failed: ||diff||=" << norm_diff
442 <<
", ||ref||=" << norm_ref <<
", tolerance=" << tolerance
Eigen expression for a vector with a single non-zero component.
Definition eigen.hpp:318
typename Eigen::internal::ref_selector< OneComponentVector >::type Nested
Definition eigen.hpp:325
Scalar value() const
Definition eigen.hpp:338
Scalar m_value
Definition eigen.hpp:341
Index rows() const
Definition eigen.hpp:330
Eigen::MatrixBase< OneComponentVector< Idx, Scalar_, Size_ > > Base
Definition eigen.hpp:324
Index cols() const
Definition eigen.hpp:331
Eigen::Index Index
Definition eigen.hpp:323
EIGEN_STATIC_ASSERT(Idx >=0 &&Idx< Size_, INDEX_OUT_OF_BOUNDS)
Scalar_ Scalar
Definition eigen.hpp:322
Scalar coeff(Index row, Index col=0) const
Definition eigen.hpp:333
OneComponentVector(Scalar value=Scalar(1))
Definition eigen.hpp:328
Eigen expression representing a vector inserted as one matrix row.
Definition eigen.hpp:217
Eigen::internal::ref_selector< RowMat >::type Nested
Definition eigen.hpp:225
ArgTypeNested m_arg
Definition eigen.hpp:232
Index rows() const
Definition eigen.hpp:228
Eigen::internal::ref_selector< ArgType >::type ArgTypeNested
Definition eigen.hpp:231
Index cols() const
Definition eigen.hpp:229
RowMat(const ArgType &arg)
Definition eigen.hpp:219
Eigen::Index Index
Definition eigen.hpp:227
Eigen expression representing a rank-1-derived symmetric matrix.
Definition eigen.hpp:111
ArgTypeNested m_arg
Definition eigen.hpp:126
Index rows() const
Definition eigen.hpp:122
Eigen::internal::ref_selector< SymMat >::type Nested
Definition eigen.hpp:119
Eigen::Index Index
Definition eigen.hpp:121
SymMat(const ArgType &arg)
Definition eigen.hpp:113
Index cols() const
Definition eigen.hpp:123
Eigen::internal::ref_selector< ArgType >::type ArgTypeNested
Definition eigen.hpp:125
bool compareEigenVectors(const Eigen::VectorXd &vec1, const Eigen::VectorXd &vec2, const double tolerance=1e-12)
Compare two dense vectors for approximate equality.
Definition eigen.hpp:425
SymMat< CrossId, ArgType > makeSymMat(const Eigen::MatrixBase< ArgType > &arg)
Build a SymMat expression from a column vector.
Definition eigen.hpp:137
auto toConstMap(const Eigen::MatrixBase< Derived > &mat)
View a dense Eigen matrix/vector as a read-only Eigen::Map.
Definition eigen.hpp:369
bool compareEigenMatrices(const Eigen::MatrixXd &mat1, const Eigen::MatrixXd &mat2, const double tolerance=1e-12)
Compare two dense matrices for approximate equality.
Definition eigen.hpp:388
auto toMap(Eigen::MatrixBase< Derived > &mat)
View a dense Eigen matrix/vector as a mutable Eigen::Map.
Definition eigen.hpp:355
RowMat< RowId, ArgType > makeRowMat(const Eigen::MatrixBase< ArgType > &arg)
Build a RowMat expression from a column vector.
Definition eigen.hpp:243
evaluator(const XprType &xpr)
Definition eigen.hpp:291
Scalar_ CoeffReturnType
Definition eigen.hpp:284
EigenVulpes::OneComponentVector< Idx, Scalar_, Size_ > XprType
Definition eigen.hpp:283
@ Flags
Definition eigen.hpp:288
@ CoeffReadCost
Definition eigen.hpp:287
EIGEN_STRONG_INLINE Scalar_ coeff(Index row, Index col=0) const
Definition eigen.hpp:293
Scalar_ m_value
Definition eigen.hpp:298
XprType::CoeffReturnType CoeffReturnType
Definition eigen.hpp:180
remove_all_t< ArgTypeNested > ArgTypeNestedCleaned
Definition eigen.hpp:179
evaluator< ArgTypeNestedCleaned > m_argImpl
Definition eigen.hpp:198
evaluator(const XprType &xpr)
Definition eigen.hpp:187
CoeffReturnType coeff(Index row, Index col) const
Definition eigen.hpp:190
const Index m_rows
Definition eigen.hpp:199
@ Flags
Definition eigen.hpp:184
@ CoeffReadCost
Definition eigen.hpp:183
nested_eval< ArgType, XprType::ColsAtCompileTime >::type ArgTypeNested
Definition eigen.hpp:178
EigenVulpes::RowMat< RowId, ArgType > XprType
Definition eigen.hpp:176
CoeffReturnType coeff(Index row, Index col) const
Definition eigen.hpp:79
evaluator< ArgTypeNestedCleaned > m_argImpl
Definition eigen.hpp:92
EigenVulpes::SymMat< CrossId, ArgType > XprType
Definition eigen.hpp:65
const Index m_rows
Definition eigen.hpp:93
XprType::CoeffReturnType CoeffReturnType
Definition eigen.hpp:69
remove_all_t< ArgTypeNested > ArgTypeNestedCleaned
Definition eigen.hpp:68
evaluator(const XprType &xpr)
Definition eigen.hpp:76
@ Flags
Definition eigen.hpp:73
@ CoeffReadCost
Definition eigen.hpp:72
nested_eval< ArgType, XprType::ColsAtCompileTime >::type ArgTypeNested
Definition eigen.hpp:67
@ MaxRowsAtCompileTime
Definition eigen.hpp:269
@ ColsAtCompileTime
Definition eigen.hpp:268
@ MaxColsAtCompileTime
Definition eigen.hpp:270
@ RowsAtCompileTime
Definition eigen.hpp:267
@ Flags
Definition eigen.hpp:271
Eigen::Index StorageIndex
Definition eigen.hpp:262
Scalar_ Scalar
Definition eigen.hpp:261
Eigen::MatrixXpr XprKind
Definition eigen.hpp:264
Eigen::Dense StorageKind
Definition eigen.hpp:263
@ MaxRowsAtCompileTime
Definition eigen.hpp:164
@ ColsAtCompileTime
Definition eigen.hpp:163
@ Flags
Definition eigen.hpp:161
@ MaxColsAtCompileTime
Definition eigen.hpp:165
@ RowsAtCompileTime
Definition eigen.hpp:162
Eigen::Dense StorageKind
Definition eigen.hpp:156
ArgType::Scalar Scalar
Definition eigen.hpp:159
ArgType::StorageIndex StorageIndex
Definition eigen.hpp:158
Eigen::MatrixXpr XprKind
Definition eigen.hpp:157
ArgType::StorageIndex StorageIndex
Definition eigen.hpp:47
ArgType::Scalar Scalar
Definition eigen.hpp:48
@ ColsAtCompileTime
Definition eigen.hpp:52
@ MaxColsAtCompileTime
Definition eigen.hpp:54
@ Flags
Definition eigen.hpp:50
@ MaxRowsAtCompileTime
Definition eigen.hpp:53
@ RowsAtCompileTime
Definition eigen.hpp:51
Eigen::MatrixXpr XprKind
Definition eigen.hpp:46
Eigen::Dense StorageKind
Definition eigen.hpp:45