Inheriting from Matrix

Before inheriting from Matrix, be really, I mean REALLY, sure that using EIGEN_MATRIX_PLUGIN is not what you really want (see previous section). If you just need to add few members to Matrix, this is the way to go.

An example of when you actually need to inherit Matrix, is when you have several layers of heritage such as MyVerySpecificVector1, MyVerySpecificVector2 -> MyVector1 -> Matrix and MyVerySpecificVector3, MyVerySpecificVector4 -> MyVector2 -> Matrix.

In order for your object to work within the Eigen framework, you need to define a few members in your inherited class.

Here is a minimalistic example:

#include <Eigen/Core>
#include <iostream>
class MyVectorType : public Eigen::VectorXd
{
public:
MyVectorType(void):Eigen::VectorXd() {}
// This constructor allows you to construct MyVectorType from Eigen expressions
template<typename OtherDerived>
: Eigen::VectorXd(other)
{ }
// This method allows you to assign Eigen expressions to MyVectorType
template<typename OtherDerived>
{
this->Eigen::VectorXd::operator=(other);
return *this;
}
};
int main()
{
MyVectorType v = MyVectorType::Ones(4);
v(2) += 10;
v = 2 * v;
std::cout << v.transpose() << std::endl;
}

Output:

This is the kind of error you can get if you don't provide those methods

error: no match for ‘operator=’ in ‘v = Eigen::operator*(
const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1, 0, -0x000000001, 1> >::Scalar&, 
const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType&)
(((const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType&)
((const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType*)(& v))))’
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
main
int main(int argc, char **argv)
Definition: cmake/example_cmake_find_gtsam/main.cpp:63
MyVectorType
Definition: CustomizingEigen_Inheritance.cpp:4
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:55