Select.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SELECT_H
26 #define EIGEN_SELECT_H
27 
28 namespace Eigen {
29 
45 namespace internal {
46 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
47 struct traits<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
48  : traits<ThenMatrixType>
49 {
50  typedef typename traits<ThenMatrixType>::Scalar Scalar;
51  typedef Dense StorageKind;
52  typedef typename traits<ThenMatrixType>::XprKind XprKind;
53  typedef typename ConditionMatrixType::Nested ConditionMatrixNested;
54  typedef typename ThenMatrixType::Nested ThenMatrixNested;
55  typedef typename ElseMatrixType::Nested ElseMatrixNested;
56  enum {
57  RowsAtCompileTime = ConditionMatrixType::RowsAtCompileTime,
58  ColsAtCompileTime = ConditionMatrixType::ColsAtCompileTime,
59  MaxRowsAtCompileTime = ConditionMatrixType::MaxRowsAtCompileTime,
60  MaxColsAtCompileTime = ConditionMatrixType::MaxColsAtCompileTime,
61  Flags = (unsigned int)ThenMatrixType::Flags & ElseMatrixType::Flags & HereditaryBits,
62  CoeffReadCost = traits<typename remove_all<ConditionMatrixNested>::type>::CoeffReadCost
63  + EIGEN_SIZE_MAX(traits<typename remove_all<ThenMatrixNested>::type>::CoeffReadCost,
64  traits<typename remove_all<ElseMatrixNested>::type>::CoeffReadCost)
65  };
66 };
67 }
68 
69 template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
70 class Select : internal::no_assignment_operator,
71  public internal::dense_xpr_base< Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >::type
72 {
73  public:
74 
75  typedef typename internal::dense_xpr_base<Select>::type Base;
77 
78  Select(const ConditionMatrixType& conditionMatrix,
79  const ThenMatrixType& thenMatrix,
80  const ElseMatrixType& elseMatrix)
81  : m_condition(conditionMatrix), m_then(thenMatrix), m_else(elseMatrix)
82  {
83  eigen_assert(m_condition.rows() == m_then.rows() && m_condition.rows() == m_else.rows());
84  eigen_assert(m_condition.cols() == m_then.cols() && m_condition.cols() == m_else.cols());
85  }
86 
87  Index rows() const { return m_condition.rows(); }
88  Index cols() const { return m_condition.cols(); }
89 
90  const Scalar coeff(Index i, Index j) const
91  {
92  if (m_condition.coeff(i,j))
93  return m_then.coeff(i,j);
94  else
95  return m_else.coeff(i,j);
96  }
97 
98  const Scalar coeff(Index i) const
99  {
100  if (m_condition.coeff(i))
101  return m_then.coeff(i);
102  else
103  return m_else.coeff(i);
104  }
105 
106  const ConditionMatrixType& conditionMatrix() const
107  {
108  return m_condition;
109  }
110 
111  const ThenMatrixType& thenMatrix() const
112  {
113  return m_then;
114  }
115 
116  const ElseMatrixType& elseMatrix() const
117  {
118  return m_else;
119  }
120 
121  protected:
122  typename ConditionMatrixType::Nested m_condition;
123  typename ThenMatrixType::Nested m_then;
124  typename ElseMatrixType::Nested m_else;
125 };
126 
127 
136 template<typename Derived>
137 template<typename ThenDerived,typename ElseDerived>
140  const DenseBase<ElseDerived>& elseMatrix) const
141 {
142  return Select<Derived,ThenDerived,ElseDerived>(derived(), thenMatrix.derived(), elseMatrix.derived());
143 }
144 
150 template<typename Derived>
151 template<typename ThenDerived>
154  typename ThenDerived::Scalar elseScalar) const
155 {
157  derived(), thenMatrix.derived(), ThenDerived::Constant(rows(),cols(),elseScalar));
158 }
159 
165 template<typename Derived>
166 template<typename ElseDerived>
168 DenseBase<Derived>::select(typename ElseDerived::Scalar thenScalar,
169  const DenseBase<ElseDerived>& elseMatrix) const
170 {
172  derived(), ElseDerived::Constant(rows(),cols(),thenScalar), elseMatrix.derived());
173 }
174 
175 } // end namespace Eigen
176 
177 #endif // EIGEN_SELECT_H