Select.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SELECT_H
11 #define EIGEN_SELECT_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
32 namespace internal {
33 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
34 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
35  : traits<ThenMatrixType>
36 {
37  typedef typename traits<ThenMatrixType>::Scalar Scalar;
38  typedef Dense StorageKind;
39  typedef typename traits<ThenMatrixType>::XprKind XprKind;
40  typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
41  typedef typename ThenMatrixType::Nested ThenMatrixNested;
42  typedef typename ElseMatrixType::Nested ElseMatrixNested;
43  enum {
44  RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
45  ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
46  MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
47  MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
48  Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & RowMajorBit
49  };
50 };
51 }
52 
53 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
54 class Select : public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type,
55  internal::no_assignment_operator
56 {
57  public:
58 
59  typedef typename internal::dense_xpr_base<Select>::type Base;
61 
62  inline EIGEN_DEVICE_FUNC
63  Select(const ConditionMatrixType& a_conditionMatrix,
64  const ThenMatrixType& a_thenMatrix,
65  const ElseMatrixType& a_elseMatrix)
66  : m_condition(a_conditionMatrix), m_then(a_thenMatrix), m_else(a_elseMatrix)
67  {
68  eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
69  eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
70  }
71 
73  Index rows() const EIGEN_NOEXCEPT { return m_condition.rows(); }
75  Index cols() const EIGEN_NOEXCEPT { return m_condition.cols(); }
76 
77  inline EIGEN_DEVICE_FUNC
78  const Scalar coeff(Index i, Index j) const
79  {
80  if (m_condition.coeff(i,j))
81  return m_then.coeff(i,j);
82  else
83  return m_else.coeff(i,j);
84  }
85 
86  inline EIGEN_DEVICE_FUNC
87  const Scalar coeff(Index i) const
88  {
89  if (m_condition.coeff(i))
90  return m_then.coeff(i);
91  else
92  return m_else.coeff(i);
93  }
94 
95  inline EIGEN_DEVICE_FUNC const ConditionMatrixType& conditionMatrix() const
96  {
97  return m_condition;
98  }
99 
100  inline EIGEN_DEVICE_FUNC const ThenMatrixType& thenMatrix() const
101  {
102  return m_then;
103  }
104 
105  inline EIGEN_DEVICE_FUNC const ElseMatrixType& elseMatrix() const
106  {
107  return m_else;
108  }
109 
110  protected:
111  typename ConditionMatrixType::Nested m_condition;
112  typename ThenMatrixType::Nested m_then;
113  typename ElseMatrixType::Nested m_else;
114 };
115 
124 template <typename Derived>
125 template <typename ThenDerived, typename ElseDerived>
127  internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar,
129  typename DenseBase<Derived>::Scalar>,
130  ThenDerived, ElseDerived, Derived>
132  const DenseBase<ElseDerived>& elseMatrix) const {
133  using Op = internal::scalar_boolean_select_op<
137  thenMatrix.derived(), elseMatrix.derived(), derived(), Op());
138 }
144 template <typename Derived>
145 template <typename ThenDerived>
147  internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar,
149  typename DenseBase<Derived>::Scalar>,
150  ThenDerived, typename DenseBase<ThenDerived>::ConstantReturnType, Derived>
152  const DenseBase<ThenDerived>& thenMatrix,
153  const typename DenseBase<ThenDerived>::Scalar& elseScalar) const {
154  using ElseConstantType =
156  using Op = internal::scalar_boolean_select_op<
160  thenMatrix.derived(), ElseConstantType(rows(), cols(), elseScalar),
161  derived(), Op());
162 }
168 template <typename Derived>
169 template <typename ElseDerived>
171  internal::scalar_boolean_select_op<typename DenseBase<ElseDerived>::Scalar,
173  typename DenseBase<Derived>::Scalar>,
174  typename DenseBase<ElseDerived>::ConstantReturnType, ElseDerived,
175  Derived>
177  const typename DenseBase<ElseDerived>::Scalar& thenScalar,
178  const DenseBase<ElseDerived>& elseMatrix) const {
179  using ThenConstantType =
181  using Op = internal::scalar_boolean_select_op<
185  ThenConstantType(rows(), cols(), thenScalar), elseMatrix.derived(),
186  derived(), Op());
187 }
188 
189 } // end namespace Eigen
190 
191 #endif // EIGEN_SELECT_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
Generic expression where a coefficient-wise ternary operator is applied to two expressions.
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
CwiseTernaryOp< internal::scalar_boolean_select_op< typename DenseBase< ThenDerived >::Scalar, typename DenseBase< ElseDerived >::Scalar, Scalar >, ThenDerived, ElseDerived, Derived > select(const DenseBase< ThenDerived > &thenMatrix, const DenseBase< ElseDerived > &elseMatrix) const
Expression of a coefficient wise version of the C++ ternary operator ?:
Definition: Select.h:56
ElseMatrixType::Nested m_else
Definition: Select.h:113
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Select.h:75
const ElseMatrixType & elseMatrix() const
Definition: Select.h:105
const ThenMatrixType & thenMatrix() const
Definition: Select.h:100
const ConditionMatrixType & conditionMatrix() const
Definition: Select.h:95
internal::dense_xpr_base< Select >::type Base
Definition: Select.h:59
ConditionMatrixType::Nested m_condition
Definition: Select.h:111
ThenMatrixType::Nested m_then
Definition: Select.h:112
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Select.h:73
Select(const ConditionMatrixType &a_conditionMatrix, const ThenMatrixType &a_thenMatrix, const ElseMatrixType &a_elseMatrix)
Definition: Select.h:63
const Scalar coeff(Index i, Index j) const
Definition: Select.h:78
const Scalar coeff(Index i) const
Definition: Select.h:87
const unsigned int RowMajorBit
Definition: Constants.h:68
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
std::ptrdiff_t j