Eigen  3.3.3
Diagonal.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_DIAGONAL_H
00012 #define EIGEN_DIAGONAL_H
00013 
00014 namespace Eigen { 
00015 
00035 namespace internal {
00036 template<typename MatrixType, int DiagIndex>
00037 struct traits<Diagonal<MatrixType,DiagIndex> >
00038  : traits<MatrixType>
00039 {
00040   typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
00041   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00042   typedef typename MatrixType::StorageKind StorageKind;
00043   enum {
00044     RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic
00045                       : (EIGEN_PLAIN_ENUM_MIN(MatrixType::RowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
00046                                               MatrixType::ColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
00047     ColsAtCompileTime = 1,
00048     MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
00049                          : DiagIndex == DynamicIndex ? EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime,
00050                                                                               MatrixType::MaxColsAtCompileTime)
00051                          : (EIGEN_PLAIN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
00052                                                  MatrixType::MaxColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
00053     MaxColsAtCompileTime = 1,
00054     MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
00055     Flags = (unsigned int)_MatrixTypeNested::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
00056     MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
00057     InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
00058     OuterStrideAtCompileTime = 0
00059   };
00060 };
00061 }
00062 
00063 template<typename MatrixType, int _DiagIndex> class Diagonal
00064    : public internal::dense_xpr_base< Diagonal<MatrixType,_DiagIndex> >::type
00065 {
00066   public:
00067 
00068     enum { DiagIndex = _DiagIndex };
00069     typedef typename internal::dense_xpr_base<Diagonal>::type Base;
00070     EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal)
00071 
00072     EIGEN_DEVICE_FUNC
00073     explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex) : m_matrix(matrix), m_index(a_index) {}
00074 
00075     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
00076 
00077     EIGEN_DEVICE_FUNC
00078     inline Index rows() const
00079     {
00080       return m_index.value()<0 ? numext::mini<Index>(m_matrix.cols(),m_matrix.rows()+m_index.value())
00081                                : numext::mini<Index>(m_matrix.rows(),m_matrix.cols()-m_index.value());
00082     }
00083 
00084     EIGEN_DEVICE_FUNC
00085     inline Index cols() const { return 1; }
00086 
00087     EIGEN_DEVICE_FUNC
00088     inline Index innerStride() const
00089     {
00090       return m_matrix.outerStride() + 1;
00091     }
00092 
00093     EIGEN_DEVICE_FUNC
00094     inline Index outerStride() const
00095     {
00096       return 0;
00097     }
00098 
00099     typedef typename internal::conditional<
00100                        internal::is_lvalue<MatrixType>::value,
00101                        Scalar,
00102                        const Scalar
00103                      >::type ScalarWithConstIfNotLvalue;
00104 
00105     EIGEN_DEVICE_FUNC
00106     inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
00107     EIGEN_DEVICE_FUNC
00108     inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
00109 
00110     EIGEN_DEVICE_FUNC
00111     inline Scalar& coeffRef(Index row, Index)
00112     {
00113       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00114       return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
00115     }
00116 
00117     EIGEN_DEVICE_FUNC
00118     inline const Scalar& coeffRef(Index row, Index) const
00119     {
00120       return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
00121     }
00122 
00123     EIGEN_DEVICE_FUNC
00124     inline CoeffReturnType coeff(Index row, Index) const
00125     {
00126       return m_matrix.coeff(row+rowOffset(), row+colOffset());
00127     }
00128 
00129     EIGEN_DEVICE_FUNC
00130     inline Scalar& coeffRef(Index idx)
00131     {
00132       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00133       return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
00134     }
00135 
00136     EIGEN_DEVICE_FUNC
00137     inline const Scalar& coeffRef(Index idx) const
00138     {
00139       return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
00140     }
00141 
00142     EIGEN_DEVICE_FUNC
00143     inline CoeffReturnType coeff(Index idx) const
00144     {
00145       return m_matrix.coeff(idx+rowOffset(), idx+colOffset());
00146     }
00147 
00148     EIGEN_DEVICE_FUNC
00149     inline const typename internal::remove_all<typename MatrixType::Nested>::type& 
00150     nestedExpression() const 
00151     {
00152       return m_matrix;
00153     }
00154 
00155     EIGEN_DEVICE_FUNC
00156     inline Index index() const
00157     {
00158       return m_index.value();
00159     }
00160 
00161   protected:
00162     typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
00163     const internal::variable_if_dynamicindex<Index, DiagIndex> m_index;
00164 
00165   private:
00166     // some compilers may fail to optimize std::max etc in case of compile-time constants...
00167     EIGEN_DEVICE_FUNC
00168     EIGEN_STRONG_INLINE Index absDiagIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
00169     EIGEN_DEVICE_FUNC
00170     EIGEN_STRONG_INLINE Index rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); }
00171     EIGEN_DEVICE_FUNC
00172     EIGEN_STRONG_INLINE Index colOffset() const { return m_index.value()>0 ? m_index.value() : 0; }
00173     // trigger a compile-time error if someone try to call packet
00174     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
00175     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
00176 };
00177 
00186 template<typename Derived>
00187 inline typename MatrixBase<Derived>::DiagonalReturnType
00188 MatrixBase<Derived>::diagonal()
00189 {
00190   return DiagonalReturnType(derived());
00191 }
00192 
00194 template<typename Derived>
00195 inline typename MatrixBase<Derived>::ConstDiagonalReturnType
00196 MatrixBase<Derived>::diagonal() const
00197 {
00198   return ConstDiagonalReturnType(derived());
00199 }
00200 
00212 template<typename Derived>
00213 inline typename MatrixBase<Derived>::DiagonalDynamicIndexReturnType
00214 MatrixBase<Derived>::diagonal(Index index)
00215 {
00216   return DiagonalDynamicIndexReturnType(derived(), index);
00217 }
00218 
00220 template<typename Derived>
00221 inline typename MatrixBase<Derived>::ConstDiagonalDynamicIndexReturnType
00222 MatrixBase<Derived>::diagonal(Index index) const
00223 {
00224   return ConstDiagonalDynamicIndexReturnType(derived(), index);
00225 }
00226 
00238 template<typename Derived>
00239 template<int Index_>
00240 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Index_>::Type
00241 MatrixBase<Derived>::diagonal()
00242 {
00243   return typename DiagonalIndexReturnType<Index_>::Type(derived());
00244 }
00245 
00247 template<typename Derived>
00248 template<int Index_>
00249 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index_>::Type
00250 MatrixBase<Derived>::diagonal() const
00251 {
00252   return typename ConstDiagonalIndexReturnType<Index_>::Type(derived());
00253 }
00254 
00255 } // end namespace Eigen
00256 
00257 #endif // EIGEN_DIAGONAL_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends