Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
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 <[email protected]>
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
16namespace Eigen {
17
33namespace internal {
34template <typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
35struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> > : traits<ThenMatrixType> {
36 typedef typename traits<ThenMatrixType>::Scalar Scalar;
37 typedef Dense StorageKind;
38 typedef typename traits<ThenMatrixType>::XprKind XprKind;
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
52template <typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
53class Select : public internal::dense_xpr_base<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type,
54 internal::no_assignment_operator {
55 public:
56 typedef typename internal::dense_xpr_base<Select>::type Base;
57 EIGEN_DENSE_PUBLIC_INTERFACE(Select)
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
66 inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_condition.rows(); }
67 inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_condition.cols(); }
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
103template <typename Derived>
104template <typename ThenDerived, typename ElseDerived>
105inline EIGEN_DEVICE_FUNC CwiseTernaryOp<
106 internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar, typename DenseBase<ElseDerived>::Scalar,
108 ThenDerived, ElseDerived, Derived>
110 using Op = internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar,
112 return CwiseTernaryOp<Op, ThenDerived, ElseDerived, Derived>(thenMatrix.derived(), elseMatrix.derived(), derived(),
113 Op());
114}
120template <typename Derived>
121template <typename ThenDerived>
122inline EIGEN_DEVICE_FUNC CwiseTernaryOp<
123 internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar, typename DenseBase<ThenDerived>::Scalar,
125 ThenDerived, typename DenseBase<ThenDerived>::ConstantReturnType, Derived>
127 const typename DenseBase<ThenDerived>::Scalar& elseScalar) const {
128 using ElseConstantType = typename DenseBase<ThenDerived>::ConstantReturnType;
129 using Op = internal::scalar_boolean_select_op<typename DenseBase<ThenDerived>::Scalar,
132 thenMatrix.derived(), ElseConstantType(rows(), cols(), elseScalar), derived(), Op());
133}
139template <typename Derived>
140template <typename ElseDerived>
141inline EIGEN_DEVICE_FUNC CwiseTernaryOp<
142 internal::scalar_boolean_select_op<typename DenseBase<ElseDerived>::Scalar, typename DenseBase<ElseDerived>::Scalar,
144 typename DenseBase<ElseDerived>::ConstantReturnType, ElseDerived, Derived>
146 const DenseBase<ElseDerived>& elseMatrix) const {
147 using ThenConstantType = typename DenseBase<ElseDerived>::ConstantReturnType;
148 using Op = internal::scalar_boolean_select_op<typename DenseBase<ElseDerived>::Scalar,
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
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
internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:62
Derived & derived()
Definition EigenBase.h:49
Expression of a coefficient wise version of the C++ ternary operator ?:
Definition Select.h:54
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83