$treeview $search $mathjax
Eigen
3.2.5
$projectbrief
|
$projectbrief
|
$searchbox |
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 nested<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 & (HereditaryBits | LinearAccessBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit, 00056 CoeffReadCost = _MatrixTypeNested::CoeffReadCost, 00057 MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret, 00058 InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1, 00059 OuterStrideAtCompileTime = 0 00060 }; 00061 }; 00062 } 00063 00064 template<typename MatrixType, int _DiagIndex> class Diagonal 00065 : public internal::dense_xpr_base< Diagonal<MatrixType,_DiagIndex> >::type 00066 { 00067 public: 00068 00069 enum { DiagIndex = _DiagIndex }; 00070 typedef typename internal::dense_xpr_base<Diagonal>::type Base; 00071 EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal) 00072 00073 inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex) : m_matrix(matrix), m_index(a_index) {} 00074 00075 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal) 00076 00077 inline Index rows() const 00078 { return m_index.value()<0 ? (std::min<Index>)(m_matrix.cols(),m_matrix.rows()+m_index.value()) : (std::min<Index>)(m_matrix.rows(),m_matrix.cols()-m_index.value()); } 00079 00080 inline Index cols() const { return 1; } 00081 00082 inline Index innerStride() const 00083 { 00084 return m_matrix.outerStride() + 1; 00085 } 00086 00087 inline Index outerStride() const 00088 { 00089 return 0; 00090 } 00091 00092 typedef typename internal::conditional< 00093 internal::is_lvalue<MatrixType>::value, 00094 Scalar, 00095 const Scalar 00096 >::type ScalarWithConstIfNotLvalue; 00097 00098 inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.const_cast_derived().coeffRef(rowOffset(), colOffset())); } 00099 inline const Scalar* data() const { return &(m_matrix.const_cast_derived().coeffRef(rowOffset(), colOffset())); } 00100 00101 inline Scalar& coeffRef(Index row, Index) 00102 { 00103 EIGEN_STATIC_ASSERT_LVALUE(MatrixType) 00104 return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset()); 00105 } 00106 00107 inline const Scalar& coeffRef(Index row, Index) const 00108 { 00109 return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset()); 00110 } 00111 00112 inline CoeffReturnType coeff(Index row, Index) const 00113 { 00114 return m_matrix.coeff(row+rowOffset(), row+colOffset()); 00115 } 00116 00117 inline Scalar& coeffRef(Index idx) 00118 { 00119 EIGEN_STATIC_ASSERT_LVALUE(MatrixType) 00120 return m_matrix.const_cast_derived().coeffRef(idx+rowOffset(), idx+colOffset()); 00121 } 00122 00123 inline const Scalar& coeffRef(Index idx) const 00124 { 00125 return m_matrix.const_cast_derived().coeffRef(idx+rowOffset(), idx+colOffset()); 00126 } 00127 00128 inline CoeffReturnType coeff(Index idx) const 00129 { 00130 return m_matrix.coeff(idx+rowOffset(), idx+colOffset()); 00131 } 00132 00133 const typename internal::remove_all<typename MatrixType::Nested>::type& 00134 nestedExpression() const 00135 { 00136 return m_matrix; 00137 } 00138 00139 int index() const 00140 { 00141 return m_index.value(); 00142 } 00143 00144 protected: 00145 typename MatrixType::Nested m_matrix; 00146 const internal::variable_if_dynamicindex<Index, DiagIndex> m_index; 00147 00148 private: 00149 // some compilers may fail to optimize std::max etc in case of compile-time constants... 00150 EIGEN_STRONG_INLINE Index absDiagIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); } 00151 EIGEN_STRONG_INLINE Index rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); } 00152 EIGEN_STRONG_INLINE Index colOffset() const { return m_index.value()>0 ? m_index.value() : 0; } 00153 // triger a compile time error is someone try to call packet 00154 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const; 00155 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const; 00156 }; 00157 00166 template<typename Derived> 00167 inline typename MatrixBase<Derived>::DiagonalReturnType 00168 MatrixBase<Derived>::diagonal() 00169 { 00170 return derived(); 00171 } 00172 00174 template<typename Derived> 00175 inline typename MatrixBase<Derived>::ConstDiagonalReturnType 00176 MatrixBase<Derived>::diagonal() const 00177 { 00178 return ConstDiagonalReturnType(derived()); 00179 } 00180 00192 template<typename Derived> 00193 inline typename MatrixBase<Derived>::DiagonalDynamicIndexReturnType 00194 MatrixBase<Derived>::diagonal(Index index) 00195 { 00196 return DiagonalDynamicIndexReturnType(derived(), index); 00197 } 00198 00200 template<typename Derived> 00201 inline typename MatrixBase<Derived>::ConstDiagonalDynamicIndexReturnType 00202 MatrixBase<Derived>::diagonal(Index index) const 00203 { 00204 return ConstDiagonalDynamicIndexReturnType(derived(), index); 00205 } 00206 00218 template<typename Derived> 00219 template<int Index> 00220 inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Index>::Type 00221 MatrixBase<Derived>::diagonal() 00222 { 00223 return derived(); 00224 } 00225 00227 template<typename Derived> 00228 template<int Index> 00229 inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index>::Type 00230 MatrixBase<Derived>::diagonal() const 00231 { 00232 return derived(); 00233 } 00234 00235 } // end namespace Eigen 00236 00237 #endif // EIGEN_DIAGONAL_H