Registered Member
|
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 ! |
Registered Member
|
For this particular function, it turns out that we have exactly the same in Eigen, as static method:
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! |
Registered Member
|
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... |
Registered Member
|
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 ! |
Registered Member
|
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! |
Moderator
|
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 |
Registered Member
|
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.
|
Moderator
|
hm, you should not return a "matrix", what is matrix ? no, you should return an expression, just like .cwise()<= does. Try this:
this should really work. |
Registered Member
|
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 <=' |
Moderator
|
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:
|
Registered Member
|
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
in the EIGEN_MATRIXBASE_PLUGIN but I have already an error : (sorry it's in french, i can translate if someone doesn't understand)
And if I use my code
I don't understand this error |
Registered Member
|
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! |
Registered Member
|
Thank you It works !
(I will use this method until Eigen 3) |
Registered Member
|
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...
The error :
If you have an idea |
Registered Member
|
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! |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]