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!
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?
11 posts • Page 1 of 2
• 1, 2
How to sort the Vector?
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() ?
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() ?
RE: How to sort the Vector?
no, and yes:
std::sort(v.data(), v.data()+v.size());
std::sort(v.data(), v.data()+v.size());
ggael, proud to be a member of KDE forums since 2008-Dec.
Re: How to sort the Vector?
Hi,
I would like to write a function to sort an Eigen::MatrixBase<Derived>. Something like:
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!
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!Re: How to sort the Vector?
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!
Have a serious interest in Eigen? Then join the mailing list!
Re: How to sort the Vector?
Great, it works! thank you.
Re: How to sort the Vector?
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!
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!
Re: How to sort the Vector?
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!
Have a serious interest in Eigen? Then join the mailing list!
Re: How to sort the Vector?
oh.
You meant the sort() function? sorry.
Here your go:
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!
Have a serious interest in Eigen? Then join the mailing list!
Re: How to sort the Vector?
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:
One things is not clear to me now: what if I would like to use sort with rowwise()/colwise()?
Thanks again.
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.
Re: How to sort the Vector?
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!
Have a serious interest in Eigen? Then join the mailing list!
11 posts • Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 6 guests

Search
FAQ
Policy
KDE.org
KDE.news
Planet KDE
More 