00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_DIAGONALMATRIX_H
00026 #define EIGEN_DIAGONALMATRIX_H
00027
00042 template<typename CoeffsVectorType>
00043 struct ei_traits<DiagonalMatrix<CoeffsVectorType> >
00044 {
00045 typedef typename CoeffsVectorType::Scalar Scalar;
00046 typedef typename ei_nested<CoeffsVectorType>::type CoeffsVectorTypeNested;
00047 typedef typename ei_unref<CoeffsVectorTypeNested>::type _CoeffsVectorTypeNested;
00048 enum {
00049 RowsAtCompileTime = CoeffsVectorType::SizeAtCompileTime,
00050 ColsAtCompileTime = CoeffsVectorType::SizeAtCompileTime,
00051 MaxRowsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime,
00052 MaxColsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime,
00053 Flags = (_CoeffsVectorTypeNested::Flags & HereditaryBits) | Diagonal,
00054 CoeffReadCost = _CoeffsVectorTypeNested::CoeffReadCost
00055 };
00056 };
00057
00058 template<typename CoeffsVectorType>
00059 class DiagonalMatrix : ei_no_assignment_operator,
00060 public MatrixBase<DiagonalMatrix<CoeffsVectorType> >
00061 {
00062 public:
00063
00064 EIGEN_GENERIC_PUBLIC_INTERFACE(DiagonalMatrix)
00065 typedef CoeffsVectorType _CoeffsVectorType;
00066
00067
00068 template<typename OtherCoeffsVectorType>
00069 inline DiagonalMatrix(const DiagonalMatrix<OtherCoeffsVectorType>& other) : m_coeffs(other.diagonal())
00070 {
00071 EIGEN_STATIC_ASSERT_VECTOR_ONLY(CoeffsVectorType);
00072 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherCoeffsVectorType);
00073 ei_assert(m_coeffs.size() > 0);
00074 }
00075
00076 inline DiagonalMatrix(const CoeffsVectorType& coeffs) : m_coeffs(coeffs)
00077 {
00078 EIGEN_STATIC_ASSERT_VECTOR_ONLY(CoeffsVectorType);
00079 ei_assert(coeffs.size() > 0);
00080 }
00081
00082 inline int rows() const { return m_coeffs.size(); }
00083 inline int cols() const { return m_coeffs.size(); }
00084
00085 inline const Scalar coeff(int row, int col) const
00086 {
00087 return row == col ? m_coeffs.coeff(row) : static_cast<Scalar>(0);
00088 }
00089
00090 inline const CoeffsVectorType& diagonal() const { return m_coeffs; }
00091
00092 protected:
00093 const typename CoeffsVectorType::Nested m_coeffs;
00094 };
00095
00108 template<typename Derived>
00109 inline const DiagonalMatrix<Derived>
00110 MatrixBase<Derived>::asDiagonal() const
00111 {
00112 return derived();
00113 }
00114
00124 template<typename Derived>
00125 bool MatrixBase<Derived>::isDiagonal
00126 (RealScalar prec) const
00127 {
00128 if(cols() != rows()) return false;
00129 RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
00130 for(int j = 0; j < cols(); ++j)
00131 {
00132 RealScalar absOnDiagonal = ei_abs(coeff(j,j));
00133 if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
00134 }
00135 for(int j = 0; j < cols(); ++j)
00136 for(int i = 0; i < j; ++i)
00137 {
00138 if(!ei_isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
00139 if(!ei_isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
00140 }
00141 return true;
00142 }
00143
00144 #endif // EIGEN_DIAGONALMATRIX_H