Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:17

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_SELECT_H
0011 #define EIGEN_SELECT_H
0012 
0013 namespace Eigen {
0014 
0015 /** \class Select
0016   * \ingroup Core_Module
0017   *
0018   * \brief Expression of a coefficient wise version of the C++ ternary operator ?:
0019   *
0020   * \param ConditionMatrixType the type of the \em condition expression which must be a boolean matrix
0021   * \param ThenMatrixType the type of the \em then expression
0022   * \param ElseMatrixType the type of the \em else expression
0023   *
0024   * This class represents an expression of a coefficient wise version of the C++ ternary operator ?:.
0025   * It is the return type of DenseBase::select() and most of the time this is the only way it is used.
0026   *
0027   * \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const
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 /** \returns a matrix where each coefficient (i,j) is equal to \a thenMatrix(i,j)
0116   * if \c *this(i,j), and \a elseMatrix(i,j) otherwise.
0117   *
0118   * Example: \include MatrixBase_select.cpp
0119   * Output: \verbinclude MatrixBase_select.out
0120   *
0121   * \sa class Select
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 /** Version of DenseBase::select(const DenseBase&, const DenseBase&) with
0133   * the \em else expression being a scalar value.
0134   *
0135   * \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const, class Select
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 /** Version of DenseBase::select(const DenseBase&, const DenseBase&) with
0148   * the \em then expression being a scalar value.
0149   *
0150   * \sa DenseBase::select(const DenseBase<ThenDerived>&, const DenseBase<ElseDerived>&) const, class Select
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 } // end namespace Eigen
0163 
0164 #endif // EIGEN_SELECT_H