Diagonal.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) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_DIAGONAL_H
27 #define EIGEN_DIAGONAL_H
28 
29 namespace Eigen {
30 
50 namespace internal {
51 template<typename MatrixType, int DiagIndex>
52 struct traits<Diagonal<MatrixType,DiagIndex> >
53  : traits<MatrixType>
54 {
55  typedef typename nested<MatrixType>::type MatrixTypeNested;
56  typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
57  typedef typename MatrixType::StorageKind StorageKind;
58  enum {
59  AbsDiagIndex = DiagIndex<0 ? -DiagIndex : DiagIndex, // only used if DiagIndex != Dynamic
60  // FIXME these computations are broken in the case where the matrix is rectangular and DiagIndex!=0
61  RowsAtCompileTime = (int(DiagIndex) == Dynamic || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic
62  : (EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime,
63  MatrixType::ColsAtCompileTime) - AbsDiagIndex),
64  ColsAtCompileTime = 1,
65  MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
66  : DiagIndex == Dynamic ? EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime,
67  MatrixType::MaxColsAtCompileTime)
68  : (EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime) - AbsDiagIndex),
69  MaxColsAtCompileTime = 1,
70  MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
71  Flags = (unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit,
72  CoeffReadCost = _MatrixTypeNested::CoeffReadCost,
73  MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
74  InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
75  OuterStrideAtCompileTime = 0
76  };
77 };
78 }
79 
80 template<typename MatrixType, int DiagIndex> class Diagonal
81  : public internal::dense_xpr_base< Diagonal<MatrixType,DiagIndex> >::type
82 {
83  public:
84 
85  typedef typename internal::dense_xpr_base<Diagonal>::type Base;
87 
88  inline Diagonal(MatrixType& matrix, Index index = DiagIndex) : m_matrix(matrix), m_index(index) {}
89 
91 
92  inline Index rows() const
93  { return m_index.value()<0 ? (std::min)(m_matrix.cols(),m_matrix.rows()+m_index.value()) : (std::min)(m_matrix.rows(),m_matrix.cols()-m_index.value()); }
94 
95  inline Index cols() const { return 1; }
96 
97  inline Index innerStride() const
98  {
99  return m_matrix.outerStride() + 1;
100  }
101 
102  inline Index outerStride() const
103  {
104  return 0;
105  }
106 
107  typedef typename internal::conditional<
108  internal::is_lvalue<MatrixType>::value,
109  Scalar,
110  const Scalar
112 
113  inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.const_cast_derived().coeffRef(rowOffset(), colOffset())); }
114  inline const Scalar* data() const { return &(m_matrix.const_cast_derived().coeffRef(rowOffset(), colOffset())); }
115 
116  inline Scalar& coeffRef(Index row, Index)
117  {
118  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
119  return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
120  }
121 
122  inline const Scalar& coeffRef(Index row, Index) const
123  {
124  return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
125  }
126 
127  inline CoeffReturnType coeff(Index row, Index) const
128  {
129  return m_matrix.coeff(row+rowOffset(), row+colOffset());
130  }
131 
132  inline Scalar& coeffRef(Index index)
133  {
134  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
135  return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
136  }
137 
138  inline const Scalar& coeffRef(Index index) const
139  {
140  return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
141  }
142 
143  inline CoeffReturnType coeff(Index index) const
144  {
145  return m_matrix.coeff(index+rowOffset(), index+colOffset());
146  }
147 
148  const typename internal::remove_all<typename MatrixType::Nested>::type&
149  nestedExpression() const
150  {
151  return m_matrix;
152  }
153 
154  int index() const
155  {
156  return m_index.value();
157  }
158 
159  protected:
160  typename MatrixType::Nested m_matrix;
161  const internal::variable_if_dynamic<Index, DiagIndex> m_index;
162 
163  private:
164  // some compilers may fail to optimize std::max etc in case of compile-time constants...
165  EIGEN_STRONG_INLINE Index absDiagIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
166  EIGEN_STRONG_INLINE Index rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); }
167  EIGEN_STRONG_INLINE Index colOffset() const { return m_index.value()>0 ? m_index.value() : 0; }
168  // triger a compile time error is someone try to call packet
169  template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
170  template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
171 };
172 
181 template<typename Derived>
182 inline typename MatrixBase<Derived>::DiagonalReturnType
184 {
185  return derived();
186 }
187 
189 template<typename Derived>
192 {
193  return ConstDiagonalReturnType(derived());
194 }
195 
207 template<typename Derived>
210 {
211  return typename DiagonalIndexReturnType<Dynamic>::Type(derived(), index);
212 }
213 
215 template<typename Derived>
218 {
219  return typename ConstDiagonalIndexReturnType<Dynamic>::Type(derived(), index);
220 }
221 
233 template<typename Derived>
234 template<int Index>
237 {
238  return derived();
239 }
240 
242 template<typename Derived>
243 template<int Index>
244 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index>::Type
245 MatrixBase<Derived>::diagonal() const
246 {
247  return derived();
248 }
249 
250 } // end namespace Eigen
251 
252 #endif // EIGEN_DIAGONAL_H