File indexing completed on 2025-01-18 09:56:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_SELECT_H
0011 #define EIGEN_SELECT_H
0012
0013 namespace Eigen {
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 namespace internal {
0031 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
0032 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
0033 : traits<ThenMatrixType>
0034 {
0035 typedef typename traits<ThenMatrixType>::Scalar Scalar;
0036 typedef Dense StorageKind;
0037 typedef typename traits<ThenMatrixType>::XprKind XprKind;
0038 typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
0039 typedef typename ThenMatrixType::Nested ThenMatrixNested;
0040 typedef typename ElseMatrixType::Nested ElseMatrixNested;
0041 enum {
0042 RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
0043 ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
0044 MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
0045 MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
0046 Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & RowMajorBit
0047 };
0048 };
0049 }
0050
0051 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
0052 class Select : public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type,
0053 internal::no_assignment_operator
0054 {
0055 public:
0056
0057 typedef typename internal::dense_xpr_base<Select>::type Base;
0058 EIGEN_DENSE_PUBLIC_INTERFACE(Select)
0059
0060 inline EIGEN_DEVICE_FUNC
0061 Select(const ConditionMatrixType& a_conditionMatrix,
0062 const ThenMatrixType& a_thenMatrix,
0063 const ElseMatrixType& a_elseMatrix)
0064 : m_condition(a_conditionMatrix), m_then(a_thenMatrix), m_else(a_elseMatrix)
0065 {
0066 eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
0067 eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
0068 }
0069
0070 inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0071 Index rows() const EIGEN_NOEXCEPT { return m_condition.rows(); }
0072 inline EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0073 Index cols() const EIGEN_NOEXCEPT { return m_condition.cols(); }
0074
0075 inline EIGEN_DEVICE_FUNC
0076 const Scalar coeff(Index i, Index j) const
0077 {
0078 if (m_condition.coeff(i,j))
0079 return m_then.coeff(i,j);
0080 else
0081 return m_else.coeff(i,j);
0082 }
0083
0084 inline EIGEN_DEVICE_FUNC
0085 const Scalar coeff(Index i) const
0086 {
0087 if (m_condition.coeff(i))
0088 return m_then.coeff(i);
0089 else
0090 return m_else.coeff(i);
0091 }
0092
0093 inline EIGEN_DEVICE_FUNC const ConditionMatrixType& conditionMatrix() const
0094 {
0095 return m_condition;
0096 }
0097
0098 inline EIGEN_DEVICE_FUNC const ThenMatrixType& thenMatrix() const
0099 {
0100 return m_then;
0101 }
0102
0103 inline EIGEN_DEVICE_FUNC const ElseMatrixType& elseMatrix() const
0104 {
0105 return m_else;
0106 }
0107
0108 protected:
0109 typename ConditionMatrixType::Nested m_condition;
0110 typename ThenMatrixType::Nested m_then;
0111 typename ElseMatrixType::Nested m_else;
0112 };
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 template<typename Derived>
0124 template<typename ThenDerived,typename ElseDerived>
0125 inline EIGEN_DEVICE_FUNC const Select<Derived,ThenDerived,ElseDerived>
0126 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
0127 const DenseBase<ElseDerived>& elseMatrix) const
0128 {
0129 return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
0130 }
0131
0132
0133
0134
0135
0136
0137 template<typename Derived>
0138 template<typename ThenDerived>
0139 inline EIGEN_DEVICE_FUNC const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
0140 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
0141 const typename ThenDerived::Scalar& elseScalar) const
0142 {
0143 return Select<Derived,ThenDerived,typename ThenDerived::ConstantReturnType>(
0144 derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
0145 }
0146
0147
0148
0149
0150
0151
0152 template<typename Derived>
0153 template<typename ElseDerived>
0154 inline EIGEN_DEVICE_FUNC const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
0155 DenseBase<Derived>::select(const typename ElseDerived::Scalar& thenScalar,
0156 const DenseBase<ElseDerived>& elseMatrix) const
0157 {
0158 return Select<Derived,typename ElseDerived::ConstantReturnType,ElseDerived>(
0159 derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
0160 }
0161
0162 }
0163
0164 #endif