2009-02-06 09:01:50 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2009-02-06 09:01:50 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
|
|
|
|
// Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-02-06 09:01:50 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-02-06 09:01:50 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_REVERSE_H
|
|
|
|
|
#define EIGEN_REVERSE_H
|
|
|
|
|
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
namespace Eigen {
|
2012-04-15 11:06:28 +01:00
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
|
|
|
|
|
2009-02-06 09:01:50 +00:00
|
|
|
template<typename MatrixType, int Direction>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<Reverse<MatrixType, Direction> >
|
|
|
|
|
: traits<MatrixType>
|
2009-02-06 09:01:50 +00:00
|
|
|
{
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename traits<MatrixType>::StorageKind StorageKind;
|
|
|
|
|
typedef typename traits<MatrixType>::XprKind XprKind;
|
2015-06-19 17:56:39 +02:00
|
|
|
typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
|
2022-01-10 20:53:29 +00:00
|
|
|
typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNested_;
|
2009-02-06 09:01:50 +00:00
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
|
2022-01-10 20:53:29 +00:00
|
|
|
Flags = MatrixTypeNested_::Flags & (RowMajorBit | LvalueBit)
|
2009-02-06 09:01:50 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-07 12:01:39 +02:00
|
|
|
template<typename PacketType, bool ReversePacket> struct reverse_packet_cond
|
2009-02-06 12:40:38 +00:00
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
static inline PacketType run(const PacketType& x) { return preverse(x); }
|
2009-02-06 12:40:38 +00:00
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
|
2015-08-07 12:01:39 +02:00
|
|
|
template<typename PacketType> struct reverse_packet_cond<PacketType,false>
|
2009-02-06 12:40:38 +00:00
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
static inline PacketType run(const PacketType& x) { return x; }
|
2009-02-06 12:40:38 +00:00
|
|
|
};
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
} // end namespace internal
|
2010-10-25 10:15:22 -04:00
|
|
|
|
2016-01-01 21:45:06 +01:00
|
|
|
/** \class Reverse
|
|
|
|
|
* \ingroup Core_Module
|
|
|
|
|
*
|
|
|
|
|
* \brief Expression of the reverse of a vector or matrix
|
|
|
|
|
*
|
|
|
|
|
* \tparam MatrixType the type of the object of which we are taking the reverse
|
|
|
|
|
* \tparam Direction defines the direction of the reverse operation, can be Vertical, Horizontal, or BothDirections
|
|
|
|
|
*
|
|
|
|
|
* This class represents an expression of the reverse of a vector.
|
|
|
|
|
* It is the return type of MatrixBase::reverse() and VectorwiseOp::reverse()
|
|
|
|
|
* and most of the time this is the only way it is used.
|
|
|
|
|
*
|
|
|
|
|
* \sa MatrixBase::reverse(), VectorwiseOp::reverse()
|
|
|
|
|
*/
|
2009-02-06 09:01:50 +00:00
|
|
|
template<typename MatrixType, int Direction> class Reverse
|
2010-10-25 10:15:22 -04:00
|
|
|
: public internal::dense_xpr_base< Reverse<MatrixType, Direction> >::type
|
2009-02-06 09:01:50 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::dense_xpr_base<Reverse>::type Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
|
2014-06-20 15:39:38 +02:00
|
|
|
typedef typename internal::remove_all<MatrixType>::type NestedExpression;
|
2010-02-26 21:29:04 -05:00
|
|
|
using Base::IsRowMajor;
|
2009-02-06 09:01:50 +00:00
|
|
|
|
2009-02-06 12:40:38 +00:00
|
|
|
protected:
|
|
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
PacketSize = internal::packet_traits<Scalar>::size,
|
2009-02-06 12:40:38 +00:00
|
|
|
IsColMajor = !IsRowMajor,
|
|
|
|
|
ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
|
|
|
|
|
ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
|
|
|
|
|
OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
|
|
|
|
|
OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
|
|
|
|
|
ReversePacket = (Direction == BothDirections)
|
|
|
|
|
|| ((Direction == Vertical) && IsColMajor)
|
|
|
|
|
|| ((Direction == Horizontal) && IsRowMajor)
|
|
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef internal::reverse_packet_cond<PacketScalar,ReversePacket> reverse_packet;
|
2009-02-06 12:40:38 +00:00
|
|
|
public:
|
|
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) { }
|
2009-02-06 09:01:50 +00:00
|
|
|
|
|
|
|
|
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
|
|
|
|
|
inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
|
|
|
|
|
inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
|
2009-02-06 09:01:50 +00:00
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC inline Index innerStride() const
|
2010-07-16 22:26:07 +02:00
|
|
|
{
|
|
|
|
|
return -m_matrix.innerStride();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC const typename internal::remove_all<typename MatrixType::Nested>::type&
|
2021-03-08 12:39:11 -05:00
|
|
|
nestedExpression() const
|
2011-04-22 22:36:45 +01:00
|
|
|
{
|
|
|
|
|
return m_matrix;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-06 09:01:50 +00:00
|
|
|
protected:
|
2012-02-03 23:18:26 +01:00
|
|
|
typename MatrixType::Nested m_matrix;
|
2009-02-06 09:01:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \returns an expression of the reverse of *this.
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_reverse.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_reverse.out
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2017-02-28 16:42:00 -08:00
|
|
|
EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ReverseReturnType
|
2009-12-10 22:00:35 +01:00
|
|
|
DenseBase<Derived>::reverse()
|
2009-02-06 09:01:50 +00:00
|
|
|
{
|
2014-09-23 14:28:23 +02:00
|
|
|
return ReverseReturnType(derived());
|
2009-02-06 09:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-22 12:29:18 +02:00
|
|
|
|
|
|
|
|
//reverse const overload moved DenseBase.h due to a CUDA compiler bug
|
2009-02-06 09:01:50 +00:00
|
|
|
|
|
|
|
|
/** This is the "in place" version of reverse: it reverses \c *this.
|
|
|
|
|
*
|
|
|
|
|
* In most cases it is probably better to simply use the reversed expression
|
|
|
|
|
* of a matrix. However, when reversing the matrix data itself is really needed,
|
|
|
|
|
* then this "in-place" version is probably the right choice because it provides
|
2015-03-31 21:35:53 +02:00
|
|
|
* the following additional benefits:
|
2009-02-06 09:01:50 +00:00
|
|
|
* - less error prone: doing the same operation with .reverse() requires special care:
|
|
|
|
|
* \code m = m.reverse().eval(); \endcode
|
2015-03-31 21:35:53 +02:00
|
|
|
* - this API enables reverse operations without the need for a temporary
|
2009-02-06 09:01:50 +00:00
|
|
|
* - it allows future optimizations (cache friendliness, etc.)
|
|
|
|
|
*
|
2015-03-31 21:35:53 +02:00
|
|
|
* \sa VectorwiseOp::reverseInPlace(), reverse() */
|
2009-02-06 09:01:50 +00:00
|
|
|
template<typename Derived>
|
2017-02-28 16:42:00 -08:00
|
|
|
EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace()
|
2009-02-06 09:01:50 +00:00
|
|
|
{
|
2015-03-31 20:17:10 +02:00
|
|
|
if(cols()>rows())
|
|
|
|
|
{
|
|
|
|
|
Index half = cols()/2;
|
|
|
|
|
leftCols(half).swap(rightCols(half).reverse());
|
|
|
|
|
if((cols()%2)==1)
|
|
|
|
|
{
|
|
|
|
|
Index half2 = rows()/2;
|
|
|
|
|
col(half).head(half2).swap(col(half).tail(half2).reverse());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Index half = rows()/2;
|
|
|
|
|
topRows(half).swap(bottomRows(half).reverse());
|
|
|
|
|
if((rows()%2)==1)
|
|
|
|
|
{
|
|
|
|
|
Index half2 = cols()/2;
|
|
|
|
|
row(half).head(half2).swap(row(half).tail(half2).reverse());
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-06 09:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-31 21:35:53 +02:00
|
|
|
namespace internal {
|
2021-03-08 12:39:11 -05:00
|
|
|
|
2015-03-31 21:35:53 +02:00
|
|
|
template<int Direction>
|
|
|
|
|
struct vectorwise_reverse_inplace_impl;
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct vectorwise_reverse_inplace_impl<Vertical>
|
|
|
|
|
{
|
|
|
|
|
template<typename ExpressionType>
|
|
|
|
|
static void run(ExpressionType &xpr)
|
|
|
|
|
{
|
2019-02-21 22:44:40 +01:00
|
|
|
const int HalfAtCompileTime = ExpressionType::RowsAtCompileTime==Dynamic?Dynamic:ExpressionType::RowsAtCompileTime/2;
|
2015-03-31 21:35:53 +02:00
|
|
|
Index half = xpr.rows()/2;
|
2019-02-21 22:44:40 +01:00
|
|
|
xpr.topRows(fix<HalfAtCompileTime>(half))
|
|
|
|
|
.swap(xpr.bottomRows(fix<HalfAtCompileTime>(half)).colwise().reverse());
|
2015-03-31 21:35:53 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct vectorwise_reverse_inplace_impl<Horizontal>
|
|
|
|
|
{
|
|
|
|
|
template<typename ExpressionType>
|
|
|
|
|
static void run(ExpressionType &xpr)
|
|
|
|
|
{
|
2019-02-21 22:44:40 +01:00
|
|
|
const int HalfAtCompileTime = ExpressionType::ColsAtCompileTime==Dynamic?Dynamic:ExpressionType::ColsAtCompileTime/2;
|
2015-03-31 21:35:53 +02:00
|
|
|
Index half = xpr.cols()/2;
|
2019-02-21 22:44:40 +01:00
|
|
|
xpr.leftCols(fix<HalfAtCompileTime>(half))
|
|
|
|
|
.swap(xpr.rightCols(fix<HalfAtCompileTime>(half)).rowwise().reverse());
|
2015-03-31 21:35:53 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
/** This is the "in place" version of VectorwiseOp::reverse: it reverses each column or row of \c *this.
|
|
|
|
|
*
|
|
|
|
|
* In most cases it is probably better to simply use the reversed expression
|
|
|
|
|
* of a matrix. However, when reversing the matrix data itself is really needed,
|
|
|
|
|
* then this "in-place" version is probably the right choice because it provides
|
|
|
|
|
* the following additional benefits:
|
|
|
|
|
* - less error prone: doing the same operation with .reverse() requires special care:
|
|
|
|
|
* \code m = m.reverse().eval(); \endcode
|
|
|
|
|
* - this API enables reverse operations without the need for a temporary
|
|
|
|
|
*
|
|
|
|
|
* \sa DenseBase::reverseInPlace(), reverse() */
|
|
|
|
|
template<typename ExpressionType, int Direction>
|
2017-02-28 16:42:00 -08:00
|
|
|
EIGEN_DEVICE_FUNC void VectorwiseOp<ExpressionType,Direction>::reverseInPlace()
|
2015-03-31 21:35:53 +02:00
|
|
|
{
|
2019-01-17 18:27:49 +01:00
|
|
|
internal::vectorwise_reverse_inplace_impl<Direction>::run(m_matrix);
|
2015-03-31 21:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
2009-02-06 09:01:50 +00:00
|
|
|
|
|
|
|
|
#endif // EIGEN_REVERSE_H
|