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

dealing with row/col expressions, inheriting from Matrix

Tags: None
(comma "," separated)
mlohry
Registered Member
Posts
8
Karma
0
I have some column vectors that are tightly coupled to other column vectors, so a logical layout may be like this:

Code: Select all
/*
[ a0 b0 c0 d0
  a1 b1 c1 d1
  a2 b2 c2 d2 ]
 */
constexpr unsigned int nRows = 3;
constexpr unsigned int nCols = 4;
struct MyMatrix : Eigen::Matrix<double,nRows,nCols> {
 ...
};
MyMatrix m;


m.col(0), m.col(1) etc will be regularly used as vectors in algebraic expressions. But I also have many functions that require whole rows.

  • How do I write a member function that returns a column by name, e.g. instead of m.col(1) I'd like to call m.b() and return the same thing. What is the appropriate return type for these calls (or is there a simple C++11 decltype thing to do this)? Can that be done without any overhead?
  • When passing whole rows to functions, what is the appropriate argument type?
  • When passing whole rows to functions, I'd like to be able to access individual elements by name, e.g. for
    Code: Select all
    r = m.row(1);

    I'd like to be able to access r.a, r.b, etc corresponding to m(1,0), m(1,2), etc. as if the row was a struct {double a,b,c,d}. Is this possible? (An alternative is to use a #define INDEXA 0, and access r[INDEXA], but I'd like to access it by name.)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
For m.b() you can return a "MatrixType::ColXpr"

For the second question, the best is to template your function to make it take a const MatrixBase<Derived>& object. If you don't want to use templates, then you can use the Ref class (http://eigen.tuxfamily.org/dox/classEigen_1_1Ref.html). Don't miss this page for more details: http://eigen.tuxfamily.org/dox/TopicFun ... Types.html

To name elements, you can use our plugin mechanism to add member functions to the MatrixBase (or DenseBase) class. This way you will be able to add members as m.a(), m.b(), etc. See this page: http://eigen.tuxfamily.org/dox/TopicCus ... Eigen.html
mlohry
Registered Member
Posts
8
Karma
0
Thanks very much, I should've read that section of the documentation more carefully. I'll work on using the plugin mechanism to try and come up with something that works for me.

As written, do you know if those plugins will have 0 abstraction penalty? e.g. compared to having

Code: Select all
#define IDXA 0
#define IDXB 2

MatExpr[IDXA] = ...
/* instead of */
MatExpr.a() = ...
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
The generated code will be exactly the same.


Bookmarks



Who is online

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