Main MRPT website > C++ reference
MRPT logo

Diagonal.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) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
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_DIAGONAL_H
00026 #define EIGEN_DIAGONAL_H
00027 
00028 /** \class Diagonal
00029   * \ingroup Core_Module
00030   *
00031   * \brief Expression of a diagonal/subdiagonal/superdiagonal in a matrix
00032   *
00033   * \param MatrixType the type of the object in which we are taking a sub/main/super diagonal
00034   * \param DiagIndex the index of the sub/super diagonal. The default is 0 and it means the main diagonal.
00035   *              A positive value means a superdiagonal, a negative value means a subdiagonal.
00036   *              You can also use Dynamic so the index can be set at runtime.
00037   *
00038   * The matrix is not required to be square.
00039   *
00040   * This class represents an expression of the main diagonal, or any sub/super diagonal
00041   * of a square matrix. It is the return type of MatrixBase::diagonal() and MatrixBase::diagonal(Index) and most of the
00042   * time this is the only way it is used.
00043   *
00044   * \sa MatrixBase::diagonal(), MatrixBase::diagonal(Index)
00045   */
00046 
00047 namespace internal {
00048 template<typename MatrixType, int DiagIndex>
00049 struct traits<Diagonal<MatrixType,DiagIndex> >
00050  : traits<MatrixType>
00051 {
00052   typedef typename nested<MatrixType>::type MatrixTypeNested;
00053   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00054   typedef typename MatrixType::StorageKind StorageKind;
00055   enum {
00056     AbsDiagIndex = DiagIndex<0 ? -DiagIndex : DiagIndex, // only used if DiagIndex != Dynamic
00057     // FIXME these computations are broken in the case where the matrix is rectangular and DiagIndex!=0
00058     RowsAtCompileTime = (int(DiagIndex) == Dynamic || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic
00059                       : (EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime,
00060                                         MatrixType::ColsAtCompileTime) - AbsDiagIndex),
00061     ColsAtCompileTime = 1,
00062     MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
00063                          : DiagIndex == Dynamic ? EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime,
00064                                                                     MatrixType::MaxColsAtCompileTime)
00065                          : (EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime) - AbsDiagIndex),
00066     MaxColsAtCompileTime = 1,
00067     MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
00068     Flags = (unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit,
00069     CoeffReadCost = _MatrixTypeNested::CoeffReadCost,
00070     MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
00071     InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
00072     OuterStrideAtCompileTime = 0
00073   };
00074 };
00075 }
00076 
00077 template<typename MatrixType, int DiagIndex> class Diagonal
00078    : public internal::dense_xpr_base< Diagonal<MatrixType,DiagIndex> >::type
00079 {
00080   public:
00081 
00082     typedef typename internal::dense_xpr_base<Diagonal>::type Base;
00083     EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal)
00084 
00085     inline Diagonal(const MatrixType& matrix, Index index = DiagIndex) : m_matrix(matrix), m_index(index) {}
00086 
00087     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
00088 
00089     inline Index rows() const
00090     { 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()); }
00091 
00092     inline Index cols() const { return 1; }
00093 
00094     inline Index innerStride() const
00095     {
00096       return m_matrix.outerStride() + 1;
00097     }
00098 
00099     inline Index outerStride() const
00100     {
00101       return 0;
00102     }
00103 
00104     inline Scalar& coeffRef(Index row, Index)
00105     {
00106       return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
00107     }
00108 
00109     inline const Scalar& coeffRef(Index row, Index) const
00110     {
00111       return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
00112     }
00113 
00114     inline CoeffReturnType coeff(Index row, Index) const
00115     {
00116       return m_matrix.coeff(row+rowOffset(), row+colOffset());
00117     }
00118 
00119     inline Scalar& coeffRef(Index index)
00120     {
00121       return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
00122     }
00123 
00124     inline const Scalar& coeffRef(Index index) const
00125     {
00126       return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
00127     }
00128 
00129     inline CoeffReturnType coeff(Index index) const
00130     {
00131       return m_matrix.coeff(index+rowOffset(), index+colOffset());
00132     }
00133 
00134   protected:
00135     const typename MatrixType::Nested m_matrix;
00136     const internal::variable_if_dynamic<Index, DiagIndex> m_index;
00137 
00138   private:
00139     // some compilers may fail to optimize std::max etc in case of compile-time constants...
00140     EIGEN_STRONG_INLINE Index absDiagIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
00141     EIGEN_STRONG_INLINE Index rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); }
00142     EIGEN_STRONG_INLINE Index colOffset() const { return m_index.value()>0 ? m_index.value() : 0; }
00143     // triger a compile time error is someone try to call packet
00144     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
00145     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
00146 };
00147 
00148 /** \returns an expression of the main diagonal of the matrix \c *this
00149   *
00150   * \c *this is not required to be square.
00151   *
00152   * Example: \include MatrixBase_diagonal.cpp
00153   * Output: \verbinclude MatrixBase_diagonal.out
00154   *
00155   * \sa class Diagonal */
00156 template<typename Derived>
00157 inline typename MatrixBase<Derived>::DiagonalReturnType
00158 MatrixBase<Derived>::diagonal()
00159 {
00160   return derived();
00161 }
00162 
00163 /** This is the const version of diagonal(). */
00164 template<typename Derived>
00165 inline const typename MatrixBase<Derived>::ConstDiagonalReturnType
00166 MatrixBase<Derived>::diagonal() const
00167 {
00168   return derived();
00169 }
00170 
00171 /** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
00172   *
00173   * \c *this is not required to be square.
00174   *
00175   * The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
00176   * and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
00177   *
00178   * Example: \include MatrixBase_diagonal_int.cpp
00179   * Output: \verbinclude MatrixBase_diagonal_int.out
00180   *
00181   * \sa MatrixBase::diagonal(), class Diagonal */
00182 template<typename Derived>
00183 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Dynamic>::Type
00184 MatrixBase<Derived>::diagonal(Index index)
00185 {
00186   return typename DiagonalIndexReturnType<Dynamic>::Type(derived(), index);
00187 }
00188 
00189 /** This is the const version of diagonal(Index). */
00190 template<typename Derived>
00191 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Dynamic>::Type
00192 MatrixBase<Derived>::diagonal(Index index) const
00193 {
00194   return typename ConstDiagonalIndexReturnType<Dynamic>::Type(derived(), index);
00195 }
00196 
00197 /** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
00198   *
00199   * \c *this is not required to be square.
00200   *
00201   * The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
00202   * and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
00203   *
00204   * Example: \include MatrixBase_diagonal_template_int.cpp
00205   * Output: \verbinclude MatrixBase_diagonal_template_int.out
00206   *
00207   * \sa MatrixBase::diagonal(), class Diagonal */
00208 template<typename Derived>
00209 template<int Index>
00210 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Index>::Type
00211 MatrixBase<Derived>::diagonal()
00212 {
00213   return derived();
00214 }
00215 
00216 /** This is the const version of diagonal<int>(). */
00217 template<typename Derived>
00218 template<int Index>
00219 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index>::Type
00220 MatrixBase<Derived>::diagonal() const
00221 {
00222   return derived();
00223 }
00224 
00225 #endif // EIGEN_DIAGONAL_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011