This forum has been archived. All content is frozen. Please use KDE Discuss instead.

About fixed-size matrix in C++ multiple inheritance

Tags: None
(comma "," separated)
eigenlearner
Registered Member
Posts
17
Karma
0
According to Eigen documentation, using fixed-size matrices has efficiency benefits over dynamic size ones.

I have a situation in C++ multiple inheritance, say finite element method as an example.

Base class: CElement
Two derived classes: CTriangleElement, CBrickElement

Assume there are two data members in the base class CElement, _n, the number of nodes in the element, and a stiff matrix, say Km with size of(_n x_n). The _n data member will be initialized to the known actual number of nodes in the derived classes accordingly. The _n in CTriangleElement is 3 and 8 in CBrickElement.

In this scheme, how do I use the fixed sized Km in the derived classes which is declared in the based class?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I'm not sure to understand your precise issue, so I'll assume your classes are template classes, so you can do something like:

using Base::Km;

in your derived classes where Base is CElement<...>
eigenlearner
Registered Member
Posts
17
Karma
0
What I mean is that for the stiff matrix Km, declared at base class CElement, it will be Matrix3d for CTriangleElement and Matrix8d for CBrickElement as fixed matrices for efficiency reason. Is it possible to accomplish this?

ggael wrote:I'm not sure to understand your precise issue, so I'll assume your classes are template classes, so you can do something like:

using Base::Km;

in your derived classes where Base is CElement<...>
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
sure, to this end you have two options: add a template parameter to CElement representing the size of the matrix, or use the CRTP pattern which is more complex but more powerful to factorize code (this is what do all the time in Eigen):

Code: Select all

template<typename T> struct traits;

template<typename Derived> struct CElement {
  enum { Size = traits<Derived>::Size };
  typedef traits<Derived>::Scalar;
  Matrix<Scalar,Size,Size> Km;
  ...
};

template<typename Scalar> struct CTriangleElement;

template<typename _Scalar> struct traits<CTriangleElement<_Scalar> > {
  enum { Size = 3 };
  typedef _Scalar Scalar;
};

template<typename Scalar> struct CTriangleElement
{
 ...
}
eigenlearner
Registered Member
Posts
17
Karma
0
Thanks for your help. However I still have problems understanding your codes. Could you make a simple c++ program to demonstrate it?

Also , I would like to have CTriangleElement derived from CElement, say something looks like

class CTriangleElement:public CElement {
};



ggael wrote:sure, to this end you have two options: add a template parameter to CElement representing the size of the matrix, or use the CRTP pattern which is more complex but more powerful to factorize code (this is what do all the time in Eigen):

Code: Select all

template<typename T> struct traits;

template<typename Derived> struct CElement {
  enum { Size = traits<Derived>::Size };
  typedef traits<Derived>::Scalar;
  Matrix<Scalar,Size,Size> Km;
  ...
};

template<typename Scalar> struct CTriangleElement;

template<typename _Scalar> struct traits<CTriangleElement<_Scalar> > {
  enum { Size = 3 };
  typedef _Scalar Scalar;
};

template<typename Scalar> struct CTriangleElement
{
 ...
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I forgot something:

Code: Select all
template<typename Scalar> struct CTriangleElement
 : public CElement<CTriangleElement<Scalar> >
{
  typedef  CElement<CTriangleElement> Base;
  using Base::Km;
 ...
}


this probably makes more sense now!!
eigenlearner
Registered Member
Posts
17
Karma
0
sorry, I got it.

ggael wrote:I forgot something:

Code: Select all
template<typename Scalar> struct CTriangleElement
 : public CElement<CTriangleElement<Scalar> >
{
  typedef  CElement<CTriangleElement> Base;
  using Base::Km;
 ...
}


this probably makes more sense now!!


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]