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

Sort a RowXpr or ColXpr in Eigen 2.0.15

Tags: None
(comma "," separated)
someguy
Registered Member
Posts
14
Karma
0
Hi,

How can I sort a row of a matrix? I tried used the function below, but it gets a lot of errors

Thanks

Code: Select all
// Compile with g++ -c -I./eigen test_eigen.cpp

#include <algorithm>
#include <Eigen/Core>
#include <Eigen/Array>

USING_PART_OF_NAMESPACE_EIGEN

typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> EigenMatrixDbl;


template <typename ScalarType, typename Derived>
void Sort(Eigen::MatrixBase<Derived> &xValues)
{
        std::sort(xValues.derived().data(), xValues.derived().data()+xValues.derived().size());
}

int main(int argc, char** argv)
{
        EigenMatrixDbl m;
        m << 1, 2, 3,
             4, 5, 6,
             7, 8, 9;

   EigenMatrixDbl::RowXpr first_row = m.row(0);
        Sort<double>(first_row);

        return 0;
}


In file included from /usr/include/c++/4.4/algorithm:62,
from test_eigen.cpp:1:
/usr/include/c++/4.4/bits/stl_algo.h: In function âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = const double*]â:
/usr/include/c++/4.4/bits/stl_algo.h:2178: instantiated from âvoid std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = const double*]â
/usr/include/c++/4.4/bits/stl_algo.h:5222: instantiated from âvoid std::sort(_RAIter, _RAIter) [with _RAIter = const double*]â
test_eigen.cpp:13: instantiated from âvoid Sort(Eigen::MatrixBase<OtherDerived>&) [with ScalarType = double, Derived = Eigen::Block<Eigen::Matrix<double, 10000, 10000, 2, 10000, 10000>, 1, 10000, 1, 32>]â
test_eigen.cpp:26: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2109: error: assignment of read-only location â* __firstâ
/usr/include/c++/4.4/bits/stl_algo.h: In function âvoid std::__unguarded_linear_insert(_RandomAccessIterator, _Tp) [with _RandomAccessIterator = const double*, _Tp = double]â:
/usr/include/c++/4.4/bits/stl_algo.h:2112: instantiated from âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = const double*]â
/usr/include/c++/4.4/bits/stl_algo.h:2178: instantiated from âvoid std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = const double*]â
/usr/include/c++/4.4/bits/stl_algo.h:5222: instantiated from âvoid std::sort(_RAIter, _RAIter) [with _RAIter = const double*]â
test_eigen.cpp:13: instantiated from âvoid Sort(Eigen::MatrixBase<OtherDerived>&) [with ScalarType = double, Derived = Eigen::Block<Eigen::Matrix<double, 10000, 10000, 2, 10000, 10000>, 1, 10000, 1, 32>]â
test_eigen.cpp:26: instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2069: error: assignment of read-only location â* __lastâ
...
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
in Eigen2 there is only a const version of the data() function, so instead you can use coeffRef:

std::sort(&xValues.coeffRef(0), &xValues.coeffRef(0)+xValues.size());
someguy
Registered Member
Posts
14
Karma
0
Thanks, but now the program segfaults!

Code: Select all
template <typename ScalarType, typename Derived>
void Sort(Eigen::MatrixBase<Derived> &xValues)
{
//        std::sort(xValues.derived().data(), xValues.derived().data()+xValues.derived().size());
        std::sort(&xValues.coeffRef(0), &xValues.coeffRef(0)+xValues.size());

}



Code: Select all
$ g++ -o test_eigen -I./eigen test_eigen.cpp && ./test_eigen

Segmentation fault
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Compile with -g3 and get a backtrace!


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Hauke
Registered Member
Posts
109
Karma
3
OS
Since you are passing pointers to std::sort, the memory block must be continuous and neighboring elements must have a distance of 1 in ptr arithmetic. This is not the case for a row when Eigen is configured to work with column major memory layout.

Or am I missing something?

- Hauke
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Yes Hauke is perfectly right.
someguy
Registered Member
Posts
14
Karma
0
ggael wrote:Yes Hauke is perfectly right.


OK, the question remains: how do I sort a RowXpr or ColXpr in Eigen 2.0.15?

Thanks


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft