Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
Select.h
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;
60  EIGEN_DENSE_PUBLIC_INTERFACE(Select)
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 
72  inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
73  Index rows() const EIGEN_NOEXCEPT { return m_condition.rows(); }
74  inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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 
116 
125 template<typename Derived>
126 template<typename ThenDerived,typename ElseDerived>
127 inline EIGEN_DEVICE_FUNC const Select<Derived,ThenDerived,ElseDerived>
129  const DenseBase<ElseDerived>& elseMatrix) const
130 {
131  return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
132 }
133 
139 template<typename Derived>
140 template<typename ThenDerived>
143  const typename ThenDerived::Scalar& elseScalar) const
144 {
146  derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
147 }
148 
154 template<typename Derived>
155 template<typename ElseDerived>
157 DenseBase<Derived>::select(const typename ElseDerived::Scalar& thenScalar,
158  const DenseBase<ElseDerived>& elseMatrix) const
159 {
161  derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
162 }
163 
164 } // end namespace Eigen
165 
166 #endif // EIGEN_SELECT_H
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
const Select< Derived, ThenDerived, ElseDerived > select(const DenseBase< ThenDerived > &thenMatrix, const DenseBase< ElseDerived > &elseMatrix) const
Definition: Select.h:128
Derived & derived()
Definition: EigenBase.h:48
Expression of a coefficient wise version of the C++ ternary operator ?:
Definition: Select.h:56
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59