mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-12 17:33:15 +08:00
Move inheritance from Eigen example in stand-alone file.
Also fix a small mistake (Vector3d instead of VectorXd).
This commit is contained in:
parent
3444f06f68
commit
8fdffdd573
@ -72,55 +72,32 @@ Then one can the following declaration in the config.h or whatever prerequisites
|
|||||||
|
|
||||||
\section InheritingFromMatrix Inheriting from Matrix
|
\section InheritingFromMatrix Inheriting from Matrix
|
||||||
|
|
||||||
Before inheriting from Matrix, be really, i mean REALLY sure that using
|
Before inheriting from Matrix, be really, I mean REALLY, sure that using
|
||||||
EIGEN_MATRIX_PLUGIN is not what you really want (see previous section).
|
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.
|
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
|
An example of when you actually need to inherit Matrix, is when you
|
||||||
several layers of heritage such as MyVerySpecificVector1,MyVerySpecificVector1 -> MyVector1 -> Matrix and.
|
have several layers of heritage such as
|
||||||
MyVerySpecificVector3,MyVerySpecificVector4 -> MyVector2 -> Matrix.
|
MyVerySpecificVector1, MyVerySpecificVector2 -> MyVector1 -> Matrix and
|
||||||
|
MyVerySpecificVector3, MyVerySpecificVector4 -> MyVector2 -> Matrix.
|
||||||
|
|
||||||
In order for your object to work within the %Eigen framework, you need to
|
In order for your object to work within the %Eigen framework, you need to
|
||||||
define a few members in your inherited class.
|
define a few members in your inherited class.
|
||||||
|
|
||||||
Here is a minimalistic example:\n
|
Here is a minimalistic example:
|
||||||
\code
|
|
||||||
class MyVectorType : public Eigen::VectorXd
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
MyVectorType(void):Eigen::VectorXd() {}
|
|
||||||
|
|
||||||
typedef Eigen::VectorXd Base;
|
\include CustomizingEigen_Inheritance.cpp
|
||||||
|
|
||||||
// This constructor allows you to construct MyVectorType from Eigen expressions
|
Output: \verbinclude CustomizingEigen_Inheritance.out
|
||||||
template<typename OtherDerived>
|
|
||||||
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other)
|
|
||||||
: Eigen::Vector3d(other)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// This method allows you to assign Eigen expressions to MyVectorType
|
|
||||||
template<typename OtherDerived>
|
|
||||||
MyVectorType & operator= (const Eigen::MatrixBase <OtherDerived>& other)
|
|
||||||
{
|
|
||||||
this->Base::operator=(other);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
\endcode
|
|
||||||
|
|
||||||
This is the kind of error you can get if you don't provide those methods
|
This is the kind of error you can get if you don't provide those methods
|
||||||
\code
|
\verbatim
|
||||||
error: no match for ‘operator=’ in ‘delta =
|
error: no match for ‘operator=’ in ‘v = Eigen::operator*(
|
||||||
(((Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000,
|
const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1, 0, -0x000000001, 1> >::Scalar&,
|
||||||
1> >*)(& delta)) + 8u)->Eigen::MatrixBase<Derived>::cwise [with Derived =
|
const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType&)
|
||||||
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000,
|
(((const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType&)
|
||||||
1>]().Eigen::Cwise<ExpressionType>::operator* [with OtherDerived =
|
((const Eigen::MatrixBase<Eigen::Matrix<double, -0x000000001, 1> >::StorageBaseType*)(& v))))’
|
||||||
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>, ExpressionType =
|
\endverbatim
|
||||||
Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>](((const
|
|
||||||
Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1, 2, 10000, 1>
|
|
||||||
>&)(((const Eigen::MatrixBase<Eigen::Matrix<std::complex<float>, 10000, 1,
|
|
||||||
>2, 10000, 1> >*)((const spectral1d*)where)) + 8u)))’
|
|
||||||
\endcode
|
|
||||||
|
|
||||||
\anchor user_defined_scalars \section CustomScalarType Using custom scalar types
|
\anchor user_defined_scalars \section CustomScalarType Using custom scalar types
|
||||||
|
|
||||||
|
@ -5,7 +5,8 @@ namespace Eigen {
|
|||||||
There are two uses for the \c template and \c typename keywords in C++. One of them is fairly well known
|
There are two uses for the \c template and \c typename keywords in C++. One of them is fairly well known
|
||||||
amongst programmers: to define templates. The other use is more obscure: to specify that an expression refers
|
amongst programmers: to define templates. The other use is more obscure: to specify that an expression refers
|
||||||
to a template function or a type. This regularly trips up programmers that use the %Eigen library, often
|
to a template function or a type. This regularly trips up programmers that use the %Eigen library, often
|
||||||
leading to error messages from the compiler that are difficult to understand.
|
leading to error messages from the compiler that are difficult to understand, such as "expected expression" or
|
||||||
|
"no match for operator<".
|
||||||
|
|
||||||
\eigenAutoToc
|
\eigenAutoToc
|
||||||
|
|
||||||
|
30
doc/examples/CustomizingEigen_Inheritance.cpp
Normal file
30
doc/examples/CustomizingEigen_Inheritance.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#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>
|
||||||
|
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other)
|
||||||
|
: Eigen::VectorXd(other)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
// This method allows you to assign Eigen expressions to MyVectorType
|
||||||
|
template<typename OtherDerived>
|
||||||
|
MyVectorType& operator=(const Eigen::MatrixBase <OtherDerived>& other)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user