[eigen] [PATCH] adding Reverse expression

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Here is a patch to add the Reverse _expression_ this is useful for flipping matrices, rows or columns.

There exist similar methods in other toolboxes.  See below the examples of use.

There are still some things to implement in the patch:
 - correct Cost values (I just have to understand correctly how to get these values)
 - allow vectorization by reimplementing the packet() and writePacket() methods (I still don't understand very well all this, but will learn it)
 - optimized reverseInPlace() by using swap (this one is easy and will do it next)

See the TODO comments in it.
There are docs and snippets.

As for use (not much of a matlab fan (so scipy is chosen instead :))):

mat.reverse()  // scpiy.flipud( scipy.fliplr( mat ) )
mat.colwise.reverse()  // scpiy.flipud( mat )
mat.rowwise.reverse()  // scpiy.fliplr( mat )

See the docs for inputs and outputs examples.

Happy 2.0!!!!

--
ricard
http://www.ricardmarxer.com
http://www.caligraft.com
Index: doc/snippets/PartialRedux_reverse.cpp
===================================================================
--- doc/snippets/PartialRedux_reverse.cpp	(revision 0)
+++ doc/snippets/PartialRedux_reverse.cpp	(revision 0)
@@ -0,0 +1,4 @@
+MatrixXi m = MatrixXi::Random(3,4);
+cout << "Here is the matrix m:" << endl << m << endl;
+cout << "Here is the rowwise reverse of m:" << endl << m.rowwise().reverse() << endl;
+cout << "Here is the colwise reverse of m:" << endl << m.colwise().reverse() << endl;
Index: doc/snippets/MatrixBase_reverse.cpp
===================================================================
--- doc/snippets/MatrixBase_reverse.cpp	(revision 0)
+++ doc/snippets/MatrixBase_reverse.cpp	(revision 0)
@@ -0,0 +1,8 @@
+MatrixXi m = MatrixXi::Random(3,4);
+cout << "Here is the matrix m:" << endl << m << endl;
+cout << "Here is the reverse of m:" << endl << m.reverse() << endl;
+cout << "Here is the coefficient (1,0) in the reverse of m:" << endl
+     << m.reverse()(1,0) << endl;
+cout << "Let us overwrite this coefficient with the value 4." << endl;
+m.reverse()(1,0) = 4;
+cout << "Now the matrix m is:" << endl << m << endl;
Index: Eigen/src/Core/MatrixBase.h
===================================================================
--- Eigen/src/Core/MatrixBase.h	(revision 920471)
+++ Eigen/src/Core/MatrixBase.h	(working copy)
@@ -359,8 +359,11 @@
     const Eigen::Transpose<Derived> transpose() const;
     void transposeInPlace();
     const AdjointReturnType adjoint() const;
+  
+    Eigen::Reverse<Derived> reverse();
+    const Eigen::Reverse<Derived> reverse() const;
+    void reverseInPlace();
 
-
     RowXpr row(int i);
     const RowXpr row(int i) const;
 
Index: Eigen/src/Core/util/ForwardDeclarations.h
===================================================================
--- Eigen/src/Core/util/ForwardDeclarations.h	(revision 920471)
+++ Eigen/src/Core/util/ForwardDeclarations.h	(working copy)
@@ -40,6 +40,7 @@
          int _DirectAccessStatus = ei_traits<MatrixType>::Flags&DirectAccessBit ? DirectAccessBit
                                  : ei_traits<MatrixType>::Flags&SparseBit> class Block;
 template<typename MatrixType> class Transpose;
+template<typename MatrixType> class Reverse;
 template<typename MatrixType> class Conjugate;
 template<typename NullaryOp, typename MatrixType>         class CwiseNullaryOp;
 template<typename UnaryOp,   typename MatrixType>         class CwiseUnaryOp;
Index: Eigen/src/Array/PartialRedux.h
===================================================================
--- Eigen/src/Array/PartialRedux.h	(revision 920471)
+++ Eigen/src/Array/PartialRedux.h	(working copy)
@@ -258,6 +258,34 @@
     const PartialReduxExpr<ExpressionType, ei_member_count<int>, Direction> count() const
     { return _expression(); }
 
+
+    /** \returns a matrix expression
+      * where each column (or row) are reversed.
+      *
+      * Example: \include PartialRedux_reverse.cpp
+      * Output: \verbinclude PartialRedux_reverse.out
+      *
+      * \sa MatrixBase::reverse() */
+    const ExpressionType reverse() const
+    {
+      const int rows = _expression().rows();
+      const int cols = _expression().cols();
+
+      ExpressionType mat(rows, cols);
+      
+      if(Direction==Vertical) {
+        for (int i = 0; i < cols; i++) {
+          mat.col(i) = _expression().col(i).reverse();
+        }
+      } else {
+        for (int i = 0; i < rows; i++) {
+          mat.row(i) = _expression().row(i).reverse();
+        }
+      }
+      
+      return mat;
+    }
+
     /** \returns a 3x3 matrix expression of the cross product
       * of each column or row of the referenced expression with the \a other vector.
       *
Index: Eigen/Core
===================================================================
--- Eigen/Core	(revision 920471)
+++ Eigen/Core	(working copy)
@@ -136,6 +136,7 @@
 #include "src/Core/Block.h"
 #include "src/Core/Minor.h"
 #include "src/Core/Transpose.h"
+#include "src/Core/Reverse.h"
 #include "src/Core/DiagonalMatrix.h"
 #include "src/Core/DiagonalCoeffs.h"
 #include "src/Core/Sum.h"


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/