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
00044 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
00045 struct ei_traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
00046 {
00047 typedef typename ei_traits<ThenMatrixType>::Scalar Scalar;
00048 typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
00049 typedef typename ThenMatrixType::Nested ThenMatrixNested;
00050 typedef typename ElseMatrixType::Nested ElseMatrixNested;
00051 enum {
00052 RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
00053 ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
00054 MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
00055 MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
00056 Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & HereditaryBits,
00057 CoeffReadCost = ei_traits<typename ei_cleantype<ConditionMatrixNested>::type>::CoeffReadCost
00058 + EIGEN_ENUM_MAX(ei_traits<typename ei_cleantype<ThenMatrixNested>::type>::CoeffReadCost,
00059 ei_traits<typename ei_cleantype<ElseMatrixNested>::type>::CoeffReadCost)
00060 };
00061 };
00062
00063 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
00064 class Select : ei_no_assignment_operator,
00065 public MatrixBase<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
00066 {
00067 public:
00068
00069 EIGEN_GENERIC_PUBLIC_INTERFACE(Select)
00070
00071 Select(const ConditionMatrixType& conditionMatrix,
00072 const ThenMatrixType& thenMatrix,
00073 const ElseMatrixType& elseMatrix)
00074 : m_condition(conditionMatrix), m_then(thenMatrix), m_else(elseMatrix)
00075 {
00076 ei_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
00077 ei_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
00078 }
00079
00080 int rows() const { return m_condition.rows(); }
00081 int cols() const { return m_condition.cols(); }
00082
00083 const Scalar coeff(int i, int j) const
00084 {
00085 if (m_condition.coeff(i,j))
00086 return m_then.coeff(i,j);
00087 else
00088 return m_else.coeff(i,j);
00089 }
00090
00091 const Scalar coeff(int i) const
00092 {
00093 if (m_condition.coeff(i))
00094 return m_then.coeff(i);
00095 else
00096 return m_else.coeff(i);
00097 }
00098
00099 protected:
00100 const typename ConditionMatrixType::Nested m_condition;
00101 const typename ThenMatrixType::Nested m_then;
00102 const typename ElseMatrixType::Nested m_else;
00103 };
00104
00105
00116 template<typename Derived>
00117 template<typename ThenDerived,typename ElseDerived>
00118 inline const Select<Derived,ThenDerived,ElseDerived>
00119 MatrixBase<Derived>::select(const MatrixBase<ThenDerived>& thenMatrix,
00120 const MatrixBase<ElseDerived>& elseMatrix) const
00121 {
00122 return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
00123 }
00124
00132 template<typename Derived>
00133 template<typename ThenDerived>
00134 inline const Select<Derived,ThenDerived, NestByValue<typename ThenDerived::ConstantReturnType> >
00135 MatrixBase<Derived>::select(const MatrixBase<ThenDerived>& thenMatrix,
00136 typename ThenDerived::Scalar elseScalar) const
00137 {
00138 return Select<Derived,ThenDerived,NestByValue<typename ThenDerived::ConstantReturnType> >(
00139 derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
00140 }
00141
00149 template<typename Derived>
00150 template<typename ElseDerived>
00151 inline const Select<Derived, NestByValue<typename ElseDerived::ConstantReturnType>, ElseDerived >
00152 MatrixBase<Derived>::select(typename ElseDerived::Scalar thenScalar,
00153 const MatrixBase<ElseDerived>& elseMatrix) const
00154 {
00155 return Select<Derived,NestByValue<typename ElseDerived::ConstantReturnType>,ElseDerived>(
00156 derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
00157 }
00158
00159 #endif // EIGEN_SELECT_H