00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_SELECT_H
00026 #define EIGEN_SELECT_H
00027
00043 namespace internal {
00044 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
00045 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
00046 : traits<ThenMatrixType>
00047 {
00048 typedef typename traits<ThenMatrixType>::Scalar Scalar;
00049 typedef Dense StorageKind;
00050 typedef typename traits<ThenMatrixType>::XprKind XprKind;
00051 typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
00052 typedef typename ThenMatrixType::Nested ThenMatrixNested;
00053 typedef typename ElseMatrixType::Nested ElseMatrixNested;
00054 enum {
00055 RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
00056 ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
00057 MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
00058 MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
00059 Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & HereditaryBits,
00060 CoeffReadCost = traits<typename remove_all<ConditionMatrixNested>::type>::CoeffReadCost
00061 + EIGEN_SIZE_MAX(traits<typename remove_all<ThenMatrixNested>::type>::CoeffReadCost,
00062 traits<typename remove_all<ElseMatrixNested>::type>::CoeffReadCost)
00063 };
00064 };
00065 }
00066
00067 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
00068 class Select : internal::no_assignment_operator,
00069 public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type
00070 {
00071 public:
00072
00073 typedef typename internal::dense_xpr_base<Select>::type Base;
00074 EIGEN_DENSE_PUBLIC_INTERFACE(Select)
00075
00076 Select(const ConditionMatrixType& conditionMatrix,
00077 const ThenMatrixType& thenMatrix,
00078 const ElseMatrixType& elseMatrix)
00079 : m_condition(conditionMatrix), m_then(thenMatrix), m_else(elseMatrix)
00080 {
00081 eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
00082 eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
00083 }
00084
00085 Index rows() const { return m_condition.rows(); }
00086 Index cols() const { return m_condition.cols(); }
00087
00088 const Scalar coeff(Index i, Index j) const
00089 {
00090 if (m_condition.coeff(i,j))
00091 return m_then.coeff(i,j);
00092 else
00093 return m_else.coeff(i,j);
00094 }
00095
00096 const Scalar coeff(Index i) const
00097 {
00098 if (m_condition.coeff(i))
00099 return m_then.coeff(i);
00100 else
00101 return m_else.coeff(i);
00102 }
00103
00104 protected:
00105 const typename ConditionMatrixType::Nested m_condition;
00106 const typename ThenMatrixType::Nested m_then;
00107 const typename ElseMatrixType::Nested m_else;
00108 };
00109
00110
00119 template<typename Derived>
00120 template<typename ThenDerived,typename ElseDerived>
00121 inline const Select<Derived,ThenDerived,ElseDerived>
00122 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
00123 const DenseBase<ElseDerived>& elseMatrix) const
00124 {
00125 return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
00126 }
00127
00133 template<typename Derived>
00134 template<typename ThenDerived>
00135 inline const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
00136 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix,
00137 typename ThenDerived::Scalar elseScalar) const
00138 {
00139 return Select<Derived,ThenDerived,typename ThenDerived::ConstantReturnType>(
00140 derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
00141 }
00142
00148 template<typename Derived>
00149 template<typename ElseDerived>
00150 inline const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
00151 DenseBase<Derived>::select(typename ElseDerived::Scalar thenScalar,
00152 const DenseBase<ElseDerived>& elseMatrix) const
00153 {
00154 return Select<Derived,typename ElseDerived::ConstantReturnType,ElseDerived>(
00155 derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
00156 }
00157
00158 #endif // EIGEN_SELECT_H