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

How to sort the Vector?

Tags: None
(comma "," separated)
yesint
Registered Member
Posts
7
Karma
0

How to sort the Vector?

Sun Apr 19, 2009 8:01 am
Dear All,
Is it possible to sort the values in VectorXd? (I didn't find any sort() method)
If not, then is it possible to use VectorXd as an STL container to use STL sort() ?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

RE: How to sort the Vector?

Sun Apr 19, 2009 8:26 am
no, and yes:
std::sort(v.data(), v.data()+v.size());
n2k
Registered Member
Posts
41
Karma
0

Re: How to sort the Vector?

Sun Oct 25, 2009 10:57 pm
Hi,

I would like to write a function to sort an Eigen::MatrixBase<Derived>. Something like:

Code: Select all
   template<typename Derived>
   void sort(Eigen::MatrixBase<Derived>& M){
      std::sort(M.data(), M.data()+M.size());
   }


but of course this is not working, because "data" is defined in Matrix and not MatrixBase (I guess). Is there a way to get around this?

This is probably a very basic question but I'm just starting to get into Eigen internal mechanisms :). Thanks for your help!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: How to sort the Vector?

Sun Oct 25, 2009 11:10 pm
When you have a MatrixBase object x, you can get the "derived" object as:
Code: Select all
x.derived()


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

Re: How to sort the Vector?

Sun Oct 25, 2009 11:16 pm
Great, it works! thank you.
n2k
Registered Member
Posts
41
Karma
0

Re: How to sort the Vector?

Sun Oct 25, 2009 11:23 pm
I actually have another question. Could you please post the "EIGEN_MATRIXBASE_PLUGIN" version of this simple function?

I would like to use your example as a template to modify many functions that I have written. This would also be useful in order to combine the "sort" function with colwise() and rowwise().

Thanks for your quick replies!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: How to sort the Vector?

Sun Oct 25, 2009 11:31 pm
What you're asking for is exactly to see the implementation of derived(). It is implemented inline inside the MatrixBase class definition: open Eigen/src/Core/MatrixBase.h and search for derived(). Also search for EIGEN_MATRIXBASE_PLUGIN, you'll see what this macro does: just add stuff inside MatrixBase!


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
bjacob
Registered Member
Posts
658
Karma
3

Re: How to sort the Vector?

Mon Oct 26, 2009 12:03 am
oh.
You meant the sort() function? sorry.

Here your go:
Code: Select all
void sort(){
  std::sort(derived().data(), derived().data()+size());
}


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

Re: How to sort the Vector?

Mon Oct 26, 2009 12:59 am
I see, now I understand how to modify MatrixBase using EIGEN_MATRIXBASE_PLUGIN.

Just for the record I'm going to write down what I did:

Code: Select all
// file main.cpp
#include "EigenMatrixBasePlugin.h"
#include <Eigen/Core>
int main (int argc, char * const argv[]) {
   Eigen::MatrixXd v;
   v.setRandom(3,3);
   std::cout<<"v="<<std::endl<<v<<std::endl;

   v.sort();
   std::cout<<"v sorted="<<std::endl<<v<<std::endl;
}

// file EigenMatrixBasePlugin.h
#define EIGEN_MATRIXBASE_PLUGIN "MatrixBaseAddons.h"

// file MatrixBaseAddons.h
inline void sort(){
   std::sort(derived().data(), derived().data()+size());
}


One things is not clear to me now: what if I would like to use sort with rowwise()/colwise()?

Thanks again.
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: How to sort the Vector?

Mon Oct 26, 2009 2:12 am
n2k wrote:Two things are not clear to me now:

1- I tried to write the implementation of sort() in a file called "sort.h" and then include that file in "MatrixBaseAddons.h" but that did not work right ( sort() becomes a regular function instead of a member function). Am I doing something wrong or is this the expected behavior?


Strange, i dont understand what's going on. It seems that this should work.

2- what if I would like to use sort with rowwise()/colwise()?


these functions return an object of type PartialRedux. See Eigen/src/Array/PartialRedux.h. (in the development branch it's renamed to VectorwiseOp). Unfortunately this class doesnt have a PLUGIN macro. You'll have to edit this file manually. If you want we can add a plugin macro in the next minor release. Then open a feature request on the issue tracker.


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

Re: How to sort the Vector?

Mon Oct 26, 2009 2:18 am
Hi,

1- I actually edited my previous post because in the meanwhile I solved the problem (I had two files with the same name)

2- ok, I will open a feature request.

Thank you.
suidong
Registered Member
Posts
2
Karma
0

Re: RE: How to sort the Vector?

Sun Jun 01, 2014 3:44 pm
ggael wrote:no, and yes:
std::sort(v.data(), v.data()+v.size());

Hi,
I'm want to sort the Eigen:: VectorXf using std:: sort , and I use your method "std::sort(v.data(), v.data()+v.size());", but I failed .
Code: Select all
    void  myFunc(VectorXf &x, MatrixXf &A, MatrixXf &B){
    VectorXf temp = A + B*x;
    std::sort(temp.data(),temp.data() + temp.col(),Compare);  // error here
}


error is reference to non-static member function must be called
std::sort(temp.data(),temp.data() + temp.size(),Compare);


How i get it work? Thank you
jitseniesen
Registered Member
Posts
204
Karma
2

Re: RE: How to sort the Vector?

Sun Jun 01, 2014 5:05 pm
suidong wrote:error is reference to non-static member function must be called
std::sort(temp.data(),temp.data() + temp.size(),Compare);

I guess this means that Compare is a non-static member function. As the error message says, you cannot pass such a function as an argument. Perhaps you can convert it to a static function, or a function outside the class. I think that this has nothing to do with Eigen, this is just how C++ works.
suidong
Registered Member
Posts
2
Karma
0

Re: RE: How to sort the Vector?

Mon Jun 02, 2014 4:15 am
jitseniesen wrote:
suidong wrote:error is reference to non-static member function must be called
std::sort(temp.data(),temp.data() + temp.size(),Compare);

I guess this means that Compare is a non-static member function. As the error message says, you cannot pass such a function as an argument. Perhaps you can convert it to a static function, or a function outside the class. I think that this has nothing to do with Eigen, this is just how C++ works.

I put the compare function outside the class, and it works . Thank you!


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]