2008-09-03 17:16:28 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-09-03 17:16:28 +00:00
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-09-03 17:16:28 +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/.
|
2008-09-03 17:16:28 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_SELECT_H
|
|
|
|
|
#define EIGEN_SELECT_H
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2010-06-19 23:00:22 +02:00
|
|
|
/** \class Select
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2008-09-03 17:16:28 +00:00
|
|
|
*
|
|
|
|
|
* \brief Expression of a coefficient wise version of the C++ ternary operator ?:
|
|
|
|
|
*
|
|
|
|
|
* \param ConditionMatrixType the type of the \em condition expression which must be a boolean matrix
|
|
|
|
|
* \param ThenMatrixType the type of the \em then expression
|
|
|
|
|
* \param ElseMatrixType the type of the \em else expression
|
|
|
|
|
*
|
|
|
|
|
* This class represents an expression of a coefficient wise version of the C++ ternary operator ?:.
|
2009-12-10 22:00:35 +01:00
|
|
|
* It is the return type of DenseBase::select() and most of the time this is the only way it is used.
|
2008-09-03 17:16:28 +00:00
|
|
|
*
|
2009-12-10 22:00:35 +01:00
|
|
|
* \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const
|
2008-09-03 17:16:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
2008-09-03 17:16:28 +00:00
|
|
|
template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
|
|
|
|
|
: traits<ThenMatrixType>
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename traits<ThenMatrixType>::Scalar Scalar;
|
2010-04-16 10:13:32 -04:00
|
|
|
typedef Dense StorageKind;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename traits<ThenMatrixType>::XprKind XprKind;
|
2009-01-12 15:55:56 +00:00
|
|
|
typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
|
|
|
|
|
typedef typename ThenMatrixType::Nested ThenMatrixNested;
|
|
|
|
|
typedef typename ElseMatrixType::Nested ElseMatrixNested;
|
2008-09-03 17:16:28 +00:00
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
|
2014-03-12 13:34:11 +01:00
|
|
|
Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & RowMajorBit
|
2008-09-03 17:16:28 +00:00
|
|
|
};
|
|
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
}
|
2008-09-03 17:16:28 +00:00
|
|
|
|
|
|
|
|
template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
|
2014-12-02 14:40:19 +01:00
|
|
|
class Select : public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type,
|
|
|
|
|
internal::no_assignment_operator
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::dense_xpr_base<Select>::type Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Select)
|
2008-09-03 17:16:28 +00:00
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC
|
2012-06-22 16:32:45 +02:00
|
|
|
Select(const ConditionMatrixType& a_conditionMatrix,
|
|
|
|
|
const ThenMatrixType& a_thenMatrix,
|
|
|
|
|
const ElseMatrixType& a_elseMatrix)
|
|
|
|
|
: m_condition(a_conditionMatrix), m_then(a_thenMatrix), m_else(a_elseMatrix)
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
|
|
|
|
|
eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
|
2008-09-03 17:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC Index rows() const { return m_condition.rows(); }
|
|
|
|
|
inline EIGEN_DEVICE_FUNC Index cols() const { return m_condition.cols(); }
|
2008-09-03 17:16:28 +00:00
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC
|
2010-05-30 16:00:58 -04:00
|
|
|
const Scalar coeff(Index i, Index j) const
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
|
|
|
|
if (m_condition.coeff(i,j))
|
|
|
|
|
return m_then.coeff(i,j);
|
|
|
|
|
else
|
|
|
|
|
return m_else.coeff(i,j);
|
|
|
|
|
}
|
2009-11-17 16:04:19 +01:00
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC
|
2010-05-30 16:00:58 -04:00
|
|
|
const Scalar coeff(Index i) const
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
|
|
|
|
if (m_condition.coeff(i))
|
|
|
|
|
return m_then.coeff(i);
|
|
|
|
|
else
|
|
|
|
|
return m_else.coeff(i);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC const ConditionMatrixType& conditionMatrix() const
|
2011-04-12 22:34:16 +01:00
|
|
|
{
|
|
|
|
|
return m_condition;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC const ThenMatrixType& thenMatrix() const
|
2011-04-12 22:34:16 +01:00
|
|
|
{
|
|
|
|
|
return m_then;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 20:16:16 +01:00
|
|
|
inline EIGEN_DEVICE_FUNC const ElseMatrixType& elseMatrix() const
|
2011-04-12 22:34:16 +01:00
|
|
|
{
|
|
|
|
|
return m_else;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 17:16:28 +00:00
|
|
|
protected:
|
2012-02-03 23:18:26 +01:00
|
|
|
typename ConditionMatrixType::Nested m_condition;
|
|
|
|
|
typename ThenMatrixType::Nested m_then;
|
|
|
|
|
typename ElseMatrixType::Nested m_else;
|
2008-09-03 17:16:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-06-19 23:00:22 +02:00
|
|
|
/** \returns a matrix where each coefficient (i,j) is equal to \a thenMatrix(i,j)
|
2008-09-03 17:16:28 +00:00
|
|
|
* if \c *this(i,j), and \a elseMatrix(i,j) otherwise.
|
|
|
|
|
*
|
2009-01-17 09:59:32 +00:00
|
|
|
* Example: \include MatrixBase_select.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_select.out
|
|
|
|
|
*
|
2008-09-03 17:16:28 +00:00
|
|
|
* \sa class Select
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename ThenDerived,typename ElseDerived>
|
2020-08-20 16:48:11 +02:00
|
|
|
inline EIGEN_DEVICE_FUNC const Select<Derived,ThenDerived,ElseDerived>
|
2009-12-10 22:00:35 +01:00
|
|
|
DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
|
|
|
|
|
const DenseBase<ElseDerived>& elseMatrix) const
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
|
|
|
|
return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-19 23:00:22 +02:00
|
|
|
/** Version of DenseBase::select(const DenseBase&, const DenseBase&) with
|
2008-09-03 17:16:28 +00:00
|
|
|
* the \em else expression being a scalar value.
|
|
|
|
|
*
|
2009-12-10 22:00:35 +01:00
|
|
|
* \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const, class Select
|
2008-09-03 17:16:28 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename ThenDerived>
|
2020-08-20 16:48:11 +02:00
|
|
|
inline EIGEN_DEVICE_FUNC const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
|
2009-12-10 22:00:35 +01:00
|
|
|
DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
|
2013-04-12 15:26:55 +02:00
|
|
|
const typename ThenDerived::Scalar& elseScalar) const
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
2009-12-01 09:49:15 +01:00
|
|
|
return Select<Derived,ThenDerived,typename ThenDerived::ConstantReturnType>(
|
2008-09-03 17:16:28 +00:00
|
|
|
derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-19 23:00:22 +02:00
|
|
|
/** Version of DenseBase::select(const DenseBase&, const DenseBase&) with
|
2008-09-03 17:16:28 +00:00
|
|
|
* the \em then expression being a scalar value.
|
|
|
|
|
*
|
2009-12-10 22:00:35 +01:00
|
|
|
* \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const, class Select
|
2008-09-03 17:16:28 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename ElseDerived>
|
2020-08-20 16:48:11 +02:00
|
|
|
inline EIGEN_DEVICE_FUNC const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
|
2013-04-12 15:26:55 +02:00
|
|
|
DenseBase<Derived>::select(const typename ElseDerived::Scalar& thenScalar,
|
|
|
|
|
const DenseBase<ElseDerived>& elseMatrix) const
|
2008-09-03 17:16:28 +00:00
|
|
|
{
|
2009-12-01 09:49:15 +01:00
|
|
|
return Select<Derived,typename ElseDerived::ConstantReturnType,ElseDerived>(
|
2008-09-03 17:16:28 +00:00
|
|
|
derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2008-09-03 17:16:28 +00:00
|
|
|
#endif // EIGEN_SELECT_H
|