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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
33 namespace internal {
34 template <typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
35 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> > : traits<ThenMatrixType> {
37  typedef Dense StorageKind;
39  typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
40  typedef typename ThenMatrixType::Nested ThenMatrixNested;
41  typedef typename ElseMatrixType::Nested ElseMatrixNested;
42  enum {
43  RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
44  ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
45  MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
46  MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
47  Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & RowMajorBit
48  };
49 };
50 } // namespace internal
51 
52 template <typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
53 class Select : public internal::dense_xpr_base<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type,
55  public:
58 
59  inline EIGEN_DEVICE_FUNC Select(const ConditionMatrixType& a_conditionMatrix, const ThenMatrixType& a_thenMatrix,
60  const ElseMatrixType& a_elseMatrix)
61  : m_condition(a_conditionMatrix), m_then(a_thenMatrix), m_else(a_elseMatrix) {
62  eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
63  eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
64  }
65 
68 
69  inline EIGEN_DEVICE_FUNC const Scalar coeff(Index i, Index j) const {
70  if (m_condition.coeff(i, j))
71  return m_then.coeff(i, j);
72  else
73  return m_else.coeff(i, j);
74  }
75 
76  inline EIGEN_DEVICE_FUNC const Scalar coeff(Index i) const {
77  if (m_condition.coeff(i))
78  return m_then.coeff(i);
79  else
80  return m_else.coeff(i);
81  }
82 
83  inline EIGEN_DEVICE_FUNC const ConditionMatrixType& conditionMatrix() const { return m_condition; }
84 
85  inline EIGEN_DEVICE_FUNC const ThenMatrixType& thenMatrix() const { return m_then; }
86 
87  inline EIGEN_DEVICE_FUNC const ElseMatrixType& elseMatrix() const { return m_else; }
88 
89  protected:
90  typename ConditionMatrixType::Nested m_condition;
91  typename ThenMatrixType::Nested m_then;
92  typename ElseMatrixType::Nested m_else;
93 };
94 
103 template <typename Derived>
104 template <typename ThenDerived, typename ElseDerived>
107  typename DenseBase<Derived>::Scalar>,
108  ThenDerived, ElseDerived, Derived>
109 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix, const DenseBase<ElseDerived>& elseMatrix) const {
112  return CwiseTernaryOp<Op, ThenDerived, ElseDerived, Derived>(thenMatrix.derived(), elseMatrix.derived(), derived(),
113  Op());
114 }
120 template <typename Derived>
121 template <typename ThenDerived>
124  typename DenseBase<Derived>::Scalar>,
125  ThenDerived, typename DenseBase<ThenDerived>::ConstantReturnType, Derived>
127  const typename DenseBase<ThenDerived>::Scalar& elseScalar) const {
128  using ElseConstantType = typename DenseBase<ThenDerived>::ConstantReturnType;
132  thenMatrix.derived(), ElseConstantType(rows(), cols(), elseScalar), derived(), Op());
133 }
139 template <typename Derived>
140 template <typename ElseDerived>
143  typename DenseBase<Derived>::Scalar>,
144  typename DenseBase<ElseDerived>::ConstantReturnType, ElseDerived, Derived>
146  const DenseBase<ElseDerived>& elseMatrix) const {
147  using ThenConstantType = typename DenseBase<ElseDerived>::ConstantReturnType;
150  return CwiseTernaryOp<Op, ThenConstantType, ElseDerived, Derived>(ThenConstantType(rows(), cols(), thenScalar),
151  elseMatrix.derived(), derived(), Op());
152 }
153 
154 } // end namespace Eigen
155 
156 #endif // EIGEN_SELECT_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define eigen_assert(x)
Definition: Macros.h:910
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:64
Generic expression where a coefficient-wise ternary operator is applied to two expressions.
Definition: CwiseTernaryOp.h:86
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
EIGEN_DEVICE_FUNC 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
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
Expression of a coefficient wise version of the C++ ternary operator ?:
Definition: Select.h:54
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Select.h:67
ElseMatrixType::Nested m_else
Definition: Select.h:92
EIGEN_DEVICE_FUNC const ElseMatrixType & elseMatrix() const
Definition: Select.h:87
internal::dense_xpr_base< Select >::type Base
Definition: Select.h:56
EIGEN_DEVICE_FUNC const Scalar coeff(Index i, Index j) const
Definition: Select.h:69
ConditionMatrixType::Nested m_condition
Definition: Select.h:90
EIGEN_DEVICE_FUNC const Scalar coeff(Index i) const
Definition: Select.h:76
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Select.h:66
ThenMatrixType::Nested m_then
Definition: Select.h:91
EIGEN_DEVICE_FUNC const ThenMatrixType & thenMatrix() const
Definition: Select.h:85
EIGEN_DEVICE_FUNC const ConditionMatrixType & conditionMatrix() const
Definition: Select.h:83
Definition: XprHelper.h:134
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
Definition: XprHelper.h:558
Definition: TernaryFunctors.h:23
Definition: ForwardDeclarations.h:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2