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

[SOLVED] Trying to customize MatrixBase

Tags: None
(comma "," separated)
rikrd
Registered Member
Posts
5
Karma
0
Hi,

I'm trying to customize MatrixBase to add a few methods I often used. I have followed the instructions:

http://eigen.tuxfamily.org/api/CustomizingEigen.html

And I generated the following MatrixBaseAddons.h:

Code: Select all
#ifndef MATRIXBASEADDONS_H
#define MATRIXBASEADDONS_H

#include "CwiseUnaryOps.h"
const CwiseUnaryOp, Derived > max(const Scalar s) { return derived().unaryExpr( CwiseMaxOp( s ) ); }


#endif // MATRIXBASEADDONS_H


and the following CwiseUnaryOps.h:

Code: Select all
#ifndef CWISEUNARYOPS_H
#define CWISEUNARYOPS_H

template
struct CwiseMaxOp {
  CwiseMaxOp(const Scalar& other) : m_other(other){}
  const Scalar operator()(const Scalar& x) const { return x
#include
#include

using namespace std;


int main() {
  Eigen::MatrixXf m = Eigen::MatrixXf::Random(4, 4);
  Eigen::MatrixXf n = Eigen::MatrixXf::Random(4, 4);
 
  cout << "-----------Test 1-------------" << endl;

  cout << "A random matrix: " << endl;

  cout << m << endl;
 
  cout << "The maxed matrix:" << endl;

  cout << m.max(0.4) << endl;

  cout << "-----------Test 2-------------" << endl;

  cout << "The first row: " << endl;

  cout << m.row(0) << endl;
 
  cout << "The maxed first row:" << endl;

  cout << m.row(0).max(0.4) << endl;

  cout << "-----------Test 3-------------" << endl;

  cout << "The first row, binary op: " << endl;

  cout << m.row(0) + n.row(0) << endl;
 
  cout << "The maxed first row, binary op:" << endl;

  cout << (m.row(0) + n.row(0)).max(0.4) << endl;

}


It's only the third test that doesn't work.

Thanks
ricard

Last edited by bjacob on Fri Jan 09, 2009 8:04 pm, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

RE: Trying to customize MatrixBase

Sat Jan 03, 2009 11:45 am
Hi,

by "doesn't work" I guess you meant that it does not compile because you forgot the const qualifier to function max:
Code: Select all
const CwiseUnaryOp, Derived > max(const Scalar s) const
{ return derived().unaryExpr( CwiseMaxOp( s ) ); }


Also, I would recommend you to define your own eigen header file (eg. MyEigen.h) like this:
Code: Select all
#ifndef MYEIGEN_H
#define MYEIGEN_H

template
struct CwiseMaxOp {
  CwiseMaxOp(const Scalar& other) : m_other(other){}
  const Scalar operator()(const Scalar& x) const { return x
#include

#endif


such that CwiseMaxOp is not nested in the class MatrixBase, and then, throughout your project you include "MyEigen.h" instead of ....
rikrd
Registered Member
Posts
5
Karma
0

RE: Trying to customize MatrixBase

Sat Jan 03, 2009 12:54 pm
Thanks!

That was exactly it. Maybe when I learn more C++ I will even be able to help, instead of always ask for help.

In my program I do have a core.h where I put the eigen related stuff, however I did this a few days ago and some things are still not done well. Thanks for the suggestion, will take care of it.

ricard


ggael wrote:Hi,

by "doesn't work" I guess you meant that it does not compile because you forgot the const qualifier to function max:
Code: Select all
const CwiseUnaryOp, Derived > max(const Scalar s) const
{ return derived().unaryExpr( CwiseMaxOp( s ) ); }


Also, I would recommend you to define your own eigen header file (eg. MyEigen.h) like this:
Code: Select all
#ifndef MYEIGEN_H
#define MYEIGEN_H

template
struct CwiseMaxOp {
  CwiseMaxOp(const Scalar& other) : m_other(other){}
  const Scalar operator()(const Scalar& x) const { return x
#include

#endif


such that CwiseMaxOp is not nested in the class MatrixBase, and then, throughout your project you include "MyEigen.h" instead of ....


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell