Select.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
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


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:24