Welcome to the KDE Community Forums, the official forum board for KDE.
You are currently viewing the forums as an unregistered user. Registration allows you to post and discuss topics, receive private messages, vote on ideas, subscribe to topics and many such great features. Registration is a simple process and completely free. So register now and be a part of the community!

How to sort the Vector?



yesint

Registered Member

Posts: 3

Karma: 0

How to sort the Vector?

Post 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() ?

Moderator User avatar

ggael

Moderator

Posts: 205

Karma: 0

Gender:  Male

France

RE: How to sort the Vector?

Post Sun Apr 19, 2009 8:26 am

no, and yes:
std::sort(v.data(), v.data()+v.size());
ggael, proud to be a member of KDE forums since 2008-Dec.



n2k

Registered Member

Posts: 13

Karma: 0

Re: How to sort the Vector?

Post 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!

Moderator User avatar

bjacob

Moderator

Posts: 293

Karma: 0

France

Re: How to sort the Vector?

Post 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: 13

Karma: 0

Re: How to sort the Vector?

Post Sun Oct 25, 2009 11:16 pm

Great, it works! thank you.



n2k

Registered Member

Posts: 13

Karma: 0

Re: How to sort the Vector?

Post 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!

Moderator User avatar

bjacob

Moderator

Posts: 293

Karma: 0

France

Re: How to sort the Vector?

Post 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!

Moderator User avatar

bjacob

Moderator

Posts: 293

Karma: 0

France

Re: How to sort the Vector?

Post 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: 13

Karma: 0

Re: How to sort the Vector?

Post 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.

Moderator User avatar

bjacob

Moderator

Posts: 293

Karma: 0

France

Re: How to sort the Vector?

Post 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!

Next »

Who is online

Users browsing this forum: No registered users and 4 guests