![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_SELECT_H 00011 #define EIGEN_SELECT_H 00012 00013 namespace Eigen { 00014 00030 namespace internal { 00031 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType> 00032 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> > 00033 : traits<ThenMatrixType> 00034 { 00035 typedef typename traits<ThenMatrixType>::Scalar Scalar; 00036 typedef Dense StorageKind; 00037 typedef typename traits<ThenMatrixType>::XprKind XprKind; 00038 typedef typename ConditionMatrixType::Nested ConditionMatrixNested; 00039 typedef typename ThenMatrixType::Nested ThenMatrixNested; 00040 typedef typename ElseMatrixType::Nested ElseMatrixNested; 00041 enum { 00042 RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime, 00043 ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime, 00044 MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime, 00045 MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime, 00046 Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & RowMajorBit 00047 }; 00048 }; 00049 } 00050 00051 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType> 00052 class Select : public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type, 00053 internal::no_assignment_operator 00054 { 00055 public: 00056 00057 typedef typename internal::dense_xpr_base<Select>::type Base; 00058 EIGEN_DENSE_PUBLIC_INTERFACE(Select) 00059 00060 inline EIGEN_DEVICE_FUNC 00061 Select(const ConditionMatrixType& a_conditionMatrix, 00062 const ThenMatrixType& a_thenMatrix, 00063 const ElseMatrixType& a_elseMatrix) 00064 : m_condition(a_conditionMatrix), m_then(a_thenMatrix), m_else(a_elseMatrix) 00065 { 00066 eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows()); 00067 eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols()); 00068 } 00069 00070 inline EIGEN_DEVICE_FUNC Index rows() const { return m_condition.rows(); } 00071 inline EIGEN_DEVICE_FUNC Index cols() const { return m_condition.cols(); } 00072 00073 inline EIGEN_DEVICE_FUNC 00074 const Scalar coeff(Index i, Index j) const 00075 { 00076 if (m_condition.coeff(i,j)) 00077 return m_then.coeff(i,j); 00078 else 00079 return m_else.coeff(i,j); 00080 } 00081 00082 inline EIGEN_DEVICE_FUNC 00083 const Scalar coeff(Index i) const 00084 { 00085 if (m_condition.coeff(i)) 00086 return m_then.coeff(i); 00087 else 00088 return m_else.coeff(i); 00089 } 00090 00091 inline EIGEN_DEVICE_FUNC const ConditionMatrixType& conditionMatrix() const 00092 { 00093 return m_condition; 00094 } 00095 00096 inline EIGEN_DEVICE_FUNC const ThenMatrixType& thenMatrix() const 00097 { 00098 return m_then; 00099 } 00100 00101 inline EIGEN_DEVICE_FUNC const ElseMatrixType& elseMatrix() const 00102 { 00103 return m_else; 00104 } 00105 00106 protected: 00107 typename ConditionMatrixType::Nested m_condition; 00108 typename ThenMatrixType::Nested m_then; 00109 typename ElseMatrixType::Nested m_else; 00110 }; 00111 00112 00121 template<typename Derived> 00122 template<typename ThenDerived,typename ElseDerived> 00123 inline const Select<Derived,ThenDerived,ElseDerived> 00124 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix, 00125 const DenseBase<ElseDerived>& elseMatrix) const 00126 { 00127 return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived()); 00128 } 00129 00135 template<typename Derived> 00136 template<typename ThenDerived> 00137 inline const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType> 00138 DenseBase<Derived>::select(const DenseBase<ThenDerived>& thenMatrix, 00139 const typename ThenDerived::Scalar& elseScalar) const 00140 { 00141 return Select<Derived,ThenDerived,typename ThenDerived::ConstantReturnType>( 00142 derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar)); 00143 } 00144 00150 template<typename Derived> 00151 template<typename ElseDerived> 00152 inline const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived > 00153 DenseBase<Derived>::select(const typename ElseDerived::Scalar& thenScalar, 00154 const DenseBase<ElseDerived>& elseMatrix) const 00155 { 00156 return Select<Derived,typename ElseDerived::ConstantReturnType,ElseDerived>( 00157 derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived()); 00158 } 00159 00160 } // end namespace Eigen 00161 00162 #endif // EIGEN_SELECT_H