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 
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(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       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00107       return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
00108     }
00109 
00110     inline const Scalar& coeffRef(Index row, Index) const
00111     {
00112       return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset());
00113     }
00114 
00115     inline CoeffReturnType coeff(Index row, Index) const
00116     {
00117       return m_matrix.coeff(row+rowOffset(), row+colOffset());
00118     }
00119 
00120     inline Scalar& coeffRef(Index index)
00121     {
00122       EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
00123       return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
00124     }
00125 
00126     inline const Scalar& coeffRef(Index index) const
00127     {
00128       return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset());
00129     }
00130 
00131     inline CoeffReturnType coeff(Index index) const
00132     {
00133       return m_matrix.coeff(index+rowOffset(), index+colOffset());
00134     }
00135 
00136   protected:
00137     const typename MatrixType::Nested m_matrix;
00138     const internal::variable_if_dynamic<Index, DiagIndex> m_index;
00139 
00140   private:
00141     // some compilers may fail to optimize std::max etc in case of compile-time constants...
00142     EIGEN_STRONG_INLINE Index absDiagIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
00143     EIGEN_STRONG_INLINE Index rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); }
00144     EIGEN_STRONG_INLINE Index colOffset() const { return m_index.value()>0 ? m_index.value() : 0; }
00145     // triger a compile time error is someone try to call packet
00146     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
00147     template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
00148 };
00149 
00158 template<typename Derived>
00159 inline typename MatrixBase<Derived>::DiagonalReturnType
00160 MatrixBase<Derived>::diagonal()
00161 {
00162   return derived();
00163 }
00164 
00166 template<typename Derived>
00167 inline const typename MatrixBase<Derived>::ConstDiagonalReturnType
00168 MatrixBase<Derived>::diagonal() const
00169 {
00170   return ConstDiagonalReturnType(derived());
00171 }
00172 
00184 template<typename Derived>
00185 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Dynamic>::Type
00186 MatrixBase<Derived>::diagonal(Index index)
00187 {
00188   return typename DiagonalIndexReturnType<Dynamic>::Type(derived(), index);
00189 }
00190 
00192 template<typename Derived>
00193 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Dynamic>::Type
00194 MatrixBase<Derived>::diagonal(Index index) const
00195 {
00196   return typename ConstDiagonalIndexReturnType<Dynamic>::Type(derived(), index);
00197 }
00198 
00210 template<typename Derived>
00211 template<int Index>
00212 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Index>::Type
00213 MatrixBase<Derived>::diagonal()
00214 {
00215   return derived();
00216 }
00217 
00219 template<typename Derived>
00220 template<int Index>
00221 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index>::Type
00222 MatrixBase<Derived>::diagonal() const
00223 {
00224   return derived();
00225 }
00226 
00227 #endif // EIGEN_DIAGONAL_H


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:37