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

replace an old matrix library

Tags: None
(comma "," separated)
Zmatt
Registered Member
Posts
27
Karma
0
OS

replace an old matrix library

Sun Jun 27, 2010 10:22 am
Hello,

I discovered Eigen a few days ago, and it seems to be a great matrix library. So I'm trying to use Eigen instead of an old library developped internaly. The problem is that i want to preserve the compatibilty with the programs written before.
I read the page http://eigen.tuxfamily.org/dox/CustomizingEigen.html but I don't understand how I can create fonctions using Eigen.
For instance I had a fonction Ones to create a matrix of 1. the prototype was friend matrix & Ones(int nr, int nc), and I can use it with
matrix A; A = Ones(5,5);
So I'm looking for a pratical solution :)

Thank you in advance for any help !
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: replace an old matrix library

Sun Jun 27, 2010 12:38 pm
For this particular function, it turns out that we have exactly the same in Eigen, as static method:
Code: Select all
MatrixXf a = MatrixXf::Ones(5,5);

Writing such a function in an efficient way is nontrivial, since for efficiency you want it to return an expression object, not a plain matrix. See Eigen's source code for the definition of Ones returning a CwiseNullaryOp.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Sun Jun 27, 2010 1:42 pm
Thanks for your quick answer :)

But I'm looking for a more general solution for writing functions that return a matrix. For instance I had a function that delete a row, function that return matrix with a gaussian (or gamma, beta, etc. ) distribution. And I don't know how I can write such a fonction in an "addon" file extending the MatrixBase class :S

I can write a new class with a MatrixXd as parameter but it will be unefficient...
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Mon Jun 28, 2010 12:18 pm
I think I have found how i can do that with functions outside the class ! :)

But I have one more question : is it possible to enable a "cwise" mode that is to say can cwise be implicit to make for instance m <= n instead of m.cwise() <= n ?

Thanks !
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: replace an old matrix library

Mon Jun 28, 2010 12:29 pm
If you want cwise to be implicit, you want eigen3 (devel branch)'s Array class.

There is nothing like that in eigen2, unfortunately.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: replace an old matrix library

Mon Jun 28, 2010 12:33 pm
for that you need to implement all the operators you want <= in the MatrixBase addons file.

In the devel branch (Eigen3, first beta on Wednesday), we have Array objects which are just like Matrix and vectors but specialized for coeff-wise operations. To see the incompatibilities between Eigen 2 and 3 see this page:

http://eigen.tuxfamily.org/dox-devel/Ei ... igen3.html
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Mon Jun 28, 2010 12:48 pm
But I'm not sure I can use the third version, because my program needs to be stable :S Perhaps I need to wait for the beta ?

I try to put this code in my addon file but it doesn't work (I had no hope but I tried ^^), the problem is that the operator <= is already surcharged.
Code: Select all
matrix operator<=(const matrix &m,const matrix &n)
{
   matrix z = (m.cwise() <= n);
   return z;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: replace an old matrix library

Mon Jun 28, 2010 1:00 pm
hm, you should not return a "matrix", what is matrix ? no, you should return an expression, just like .cwise()<= does. Try this:


Code: Select all
template<typename OtherDerived>
inline const EIGEN_CWISE_BINOP_RETURN_TYPE(std::less_equal)
operator<=(const MatrixBase<OtherDerived> &other) const
{
  return EIGEN_CWISE_BINOP_RETURN_TYPE(std::less_equal)(derived(), other.derived());
}


this should really work.
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Mon Jun 28, 2010 1:19 pm
Yes, sorry matrix was something specific to my program (to preserve the compatibility) : #define matrix MatrixXd

I tried our code in a EIGEN_CWISE_PLUGIN but I have an error :
error C2535: 'const Eigen::CwiseBinaryOp<std::less_equal<ei_traits<T>::Scalar>,ExpressionType,OtherDerived> Eigen::Cwise<Derived>::operator <=(const Eigen::MatrixBase<OtherDerived> &) const' : fonction membre déjà définie ou déclarée (function already declared)
src/Core/Cwise.h(141) : voir la déclaration de 'Eigen::Cwise<Derived>::operator <='
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: replace an old matrix library

Mon Jun 28, 2010 1:41 pm
replace

EIGEN_CWISE_PLUGIN

by

EIGEN_MATRIXBASE_PLUGIN

Also you should not have to declare these functions using an explicit matrix type to have a source compatibility. With the above addons, the following will compile fine:

matrix a, b;
a<=b


Also, Re-reading your example, it should also work if you declare it in one of your header files, but *not* as a Eigen addons. Eigen's addons are inserted into their respective class definition. Inside Eigen, we have:

Code: Select all
class MatrixBase {
//...
#ifdef EIGEN_MATRIXBASE_PLUGIN
#include EIGEN_MATRIXBASE_PLUGIN
#endif

};
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Mon Jun 28, 2010 2:45 pm
Thanks :) Yes I knew that the addons are really include in the class. (Actually it's pratical and good to know)
But for my previous problem I have still a problem :S

I have place the code
Code: Select all
(line 115) template<typename OtherDerived>
inline const EIGEN_CWISE_BINOP_RETURN_TYPE(std::less_equal)
operator<=(const MatrixBase<OtherDerived> &other) const
{
  return EIGEN_CWISE_BINOP_RETURN_TYPE(std::less_equal)(derived(), other.derived());
}

in the EIGEN_MATRIXBASE_PLUGIN but I have already an error : (sorry it's in french, i can translate if someone doesn't understand)
error C4430: spécificateur de type manquant - int est pris en compte par défaut. Remarque : C++ ne prend pas en charge int par défaut
1> c:\documents and settings\bureau\eigen\eigen\src/Core/MatrixBase.h(643) : voir la référence à l'instanciation de la classe modèle 'Eigen::MatrixBase<Derived>' en cours de compilation
1>c:\documents and settings\bureau\eigen\eigen\testeigen\extension.h(117) : error C2143: erreur de syntaxe : absence de ';' avant '<='
.... etc. .... (45 errors....)


And if I use my code
Code: Select all
matrix operator<=(const matrix &m,const matrix &n)
{
   matrix z = (m.cwise() <= n);
   return z;
}
in an "normal" header file I have the error :
error C2039: 'YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY' : n'est pas membre de 'Eigen::ei_static_assert<condition>'

I don't understand this error
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: replace an old matrix library

Mon Jun 28, 2010 3:05 pm
The second error that you're getting, YOU_MIXED_DIFFERENT_NUMERIC_TYPES, is because when you do:

matrix z = (m.cwise() <= n);

You are assigning the *boolean* matrix expression (m.cwise() <= n) to the matrix z. The best fix is to change your operator to return a boolean matrix. (See how Eigen does). If however you prefer to return the same matrix type, you need to cast to your scalar type:

matrix z = (m.cwise() <= n).template cast<Scalar>();


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Mon Jun 28, 2010 3:14 pm
Thank you ;D It works !

(I will use this method until Eigen 3)
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: replace an old matrix library

Thu Jul 01, 2010 12:00 pm
Hi,

I have a new question :) I'm trying to add a new function in order to do new affectations to a matrix. For example :
A = 1.0; and all coefficients of A will be 1.

So I write a fonction that seems to be ok but when I want to use it, this function is not used and I have an error...
Code: Select all
EIGEN_STRONG_INLINE Derived& operator=(const Scalar &v)
{
   for (int i = 0; i < rows(); i++)
      for(int j = 0; i < cols(); j++)
         derived()(i,j) = v;
   return (*this);
}


The error :
error C2664: 'Eigen::Matrix<_Scalar,_Rows,_Cols> &Eigen::Matrix<_Scalar,_Rows,_Cols>::operator =(const Eigen::Matrix<_Scalar,_Rows,_Cols> &)' : impossible to convert the first parameter 'double' in 'const Eigen::Matrix<_Scalar,_Rows,_Cols> &'

If you have an idea :)
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: replace an old matrix library

Thu Jul 01, 2010 2:17 pm
Here 1.0 is a double constant, so, since you get this error, I guess the Scalar type of your matrix is not double. If you have a matrix of floats, try A = 1.0f, your operator should be used.

Also, look again at your second for statement, you have a bug in there (i versus j).


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]