From 9afd1324fd809631cd0762908629c30d72e61b40 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sun, 10 May 2009 16:24:39 +0000 Subject: [PATCH] constant Diagonal ---> DiagonalBits introduce ei_is_diagonal to check for it DiagonalCoeffs ---> Diagonal and allow Index to by Dynamic -> add MatrixBase::diagonal(int) with unittest and doc --- Eigen/Core | 2 +- Eigen/src/Cholesky/LDLT.h | 2 +- Eigen/src/Core/Diagonal.h | 187 ++++++++++++++++++ Eigen/src/Core/DiagonalCoeffs.h | 159 --------------- Eigen/src/Core/DiagonalMatrix.h | 10 +- Eigen/src/Core/DiagonalProduct.h | 8 +- Eigen/src/Core/MatrixBase.h | 18 +- Eigen/src/Core/Product.h | 2 +- Eigen/src/Core/Transpose.h | 2 +- Eigen/src/Core/util/Constants.h | 21 +- Eigen/src/Core/util/ForwardDeclarations.h | 2 +- Eigen/src/LU/LU.h | 4 +- Eigen/src/QR/HessenbergDecomposition.h | 6 +- Eigen/src/QR/Tridiagonalization.h | 6 +- Eigen/src/Sparse/SparseDiagonalProduct.h | 6 +- Eigen/src/Sparse/SparseMatrixBase.h | 4 +- Eigen/src/Sparse/SparseProduct.h | 2 +- doc/snippets/MatrixBase_diagonal_int.cpp | 4 +- .../MatrixBase_diagonal_template_int.cpp | 5 + test/submatrices.cpp | 9 +- 20 files changed, 256 insertions(+), 203 deletions(-) create mode 100644 Eigen/src/Core/Diagonal.h delete mode 100644 Eigen/src/Core/DiagonalCoeffs.h create mode 100644 doc/snippets/MatrixBase_diagonal_template_int.cpp diff --git a/Eigen/Core b/Eigen/Core index 9772ddcce..810daf81f 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -166,7 +166,7 @@ namespace Eigen { #include "src/Core/Minor.h" #include "src/Core/Transpose.h" #include "src/Core/DiagonalMatrix.h" -#include "src/Core/DiagonalCoeffs.h" +#include "src/Core/Diagonal.h" #include "src/Core/Redux.h" #include "src/Core/Visitor.h" #include "src/Core/Fuzzy.h" diff --git a/Eigen/src/Cholesky/LDLT.h b/Eigen/src/Cholesky/LDLT.h index 06f80fd87..5cd6f1bb1 100644 --- a/Eigen/src/Cholesky/LDLT.h +++ b/Eigen/src/Cholesky/LDLT.h @@ -83,7 +83,7 @@ template class LDLT } /** \returns the coefficients of the diagonal matrix D */ - inline DiagonalCoeffs vectorD(void) const { return m_matrix.diagonal(); } + inline Diagonal vectorD(void) const { return m_matrix.diagonal(); } /** \returns true if the matrix is positive (semidefinite) */ inline bool isPositive(void) const { return m_sign == 1; } diff --git a/Eigen/src/Core/Diagonal.h b/Eigen/src/Core/Diagonal.h new file mode 100644 index 000000000..90f5c4d89 --- /dev/null +++ b/Eigen/src/Core/Diagonal.h @@ -0,0 +1,187 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2007-2009 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_DIAGONAL_H +#define EIGEN_DIAGONAL_H + +/** \class Diagonal + * + * \brief Expression of a diagonal/subdiagonal/superdiagonal in a matrix + * + * \param MatrixType the type of the object in which we are taking a sub/main/super diagonal + * \param Index the index of the sub/super diagonal. The default is 0 and it means the main diagonal. + * A positive value means a superdiagonal, a negative value means a subdiagonal. + * You can also use Dynamic so the index can be set at runtime. + * + * The matrix is not required to be square. + * + * This class represents an expression of the main diagonal, or any sub/super diagonal + * of a square matrix. It is the return type of MatrixBase::diagonal() and MatrixBase::diagonal(int) and most of the + * time this is the only way it is used. + * + * \sa MatrixBase::diagonal(), MatrixBase::diagonal(int) + */ +template +struct ei_traits > +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename ei_nested::type MatrixTypeNested; + typedef typename ei_unref::type _MatrixTypeNested; + enum { + AbsIndex = Index<0 ? -Index : Index, // only used if Index != Dynamic + RowsAtCompileTime = (int(Index) == Dynamic || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic + : (EIGEN_ENUM_MIN(MatrixType::RowsAtCompileTime, + MatrixType::ColsAtCompileTime) - AbsIndex), + ColsAtCompileTime = 1, + MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic + : (EIGEN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime, + MatrixType::MaxColsAtCompileTime) - AbsIndex), + MaxColsAtCompileTime = 1, + Flags = (unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit), + CoeffReadCost = _MatrixTypeNested::CoeffReadCost + }; +}; + +template class Diagonal + : public MatrixBase > +{ + // some compilers may fail to optimize std::max etc in case of compile-time constants... + EIGEN_STRONG_INLINE int absIndex() const { return m_index.value()>0 ? m_index.value() : -m_index.value(); } + EIGEN_STRONG_INLINE int rowOffset() const { return m_index.value()>0 ? 0 : -m_index.value(); } + EIGEN_STRONG_INLINE int colOffset() const { return m_index.value()>0 ? m_index.value() : 0; } + + public: + + EIGEN_GENERIC_PUBLIC_INTERFACE(Diagonal) + + inline Diagonal(const MatrixType& matrix, int index = Index) : m_matrix(matrix), m_index(index) {} + + EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal) + + inline int rows() const{ return m_matrix.diagonalSize() - absIndex(); } + inline int cols() const { return 1; } + + inline Scalar& coeffRef(int row, int) + { + return m_matrix.const_cast_derived().coeffRef(row+rowOffset(), row+colOffset()); + } + + inline const Scalar coeff(int row, int) const + { + return m_matrix.coeff(row+rowOffset(), row+colOffset()); + } + + inline Scalar& coeffRef(int index) + { + return m_matrix.const_cast_derived().coeffRef(index+rowOffset(), index+colOffset()); + } + + inline const Scalar coeff(int index) const + { + return m_matrix.coeff(index+rowOffset(), index+colOffset()); + } + + protected: + const typename MatrixType::Nested m_matrix; + const ei_int_if_dynamic m_index; +}; + +/** \returns an expression of the main diagonal of the matrix \c *this + * + * \c *this is not required to be square. + * + * Example: \include MatrixBase_diagonal.cpp + * Output: \verbinclude MatrixBase_diagonal.out + * + * \sa class Diagonal */ +template +inline Diagonal +MatrixBase::diagonal() +{ + return Diagonal(derived()); +} + +/** This is the const version of diagonal(). */ +template +inline const Diagonal +MatrixBase::diagonal() const +{ + return Diagonal(derived()); +} + +/** \returns an expression of the \a Index-th sub or super diagonal of the matrix \c *this + * + * \c *this is not required to be square. + * + * The template parameter \a Index represent a super diagonal if \a Index > 0 + * and a sub diagonal otherwise. \a Index == 0 is equivalent to the main diagonal. + * + * Example: \include MatrixBase_diagonal_int.cpp + * Output: \verbinclude MatrixBase_diagonal_int.out + * + * \sa MatrixBase::diagonal(), class Diagonal */ +template +inline Diagonal +MatrixBase::diagonal(int index) +{ + return Diagonal(derived(), index); +} + +/** This is the const version of diagonal(int). */ +template +inline const Diagonal +MatrixBase::diagonal(int index) const +{ + return Diagonal(derived(), index); +} + +/** \returns an expression of the \a Index-th sub or super diagonal of the matrix \c *this + * + * \c *this is not required to be square. + * + * The template parameter \a Index represent a super diagonal if \a Index > 0 + * and a sub diagonal otherwise. \a Index == 0 is equivalent to the main diagonal. + * + * Example: \include MatrixBase_diagonal_template_int.cpp + * Output: \verbinclude MatrixBase_diagonal_template_int.out + * + * \sa MatrixBase::diagonal(), class Diagonal */ +template +template +inline Diagonal +MatrixBase::diagonal() +{ + return Diagonal(derived()); +} + +/** This is the const version of diagonal(). */ +template +template +inline const Diagonal +MatrixBase::diagonal() const +{ + return Diagonal(derived()); +} + +#endif // EIGEN_DIAGONAL_H diff --git a/Eigen/src/Core/DiagonalCoeffs.h b/Eigen/src/Core/DiagonalCoeffs.h deleted file mode 100644 index ba5c0da62..000000000 --- a/Eigen/src/Core/DiagonalCoeffs.h +++ /dev/null @@ -1,159 +0,0 @@ -// This file is part of Eigen, a lightweight C++ template library -// for linear algebra. Eigen itself is part of the KDE project. -// -// Copyright (C) 2006-2008 Benoit Jacob -// -// Eigen is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 3 of the License, or (at your option) any later version. -// -// Alternatively, you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of -// the License, or (at your option) any later version. -// -// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License and a copy of the GNU General Public License along with -// Eigen. If not, see . - -#ifndef EIGEN_DIAGONALCOEFFS_H -#define EIGEN_DIAGONALCOEFFS_H - -/** \class DiagonalCoeffs - * - * \brief Expression of a sub/main/super diagonal of a matrix - * - * \param MatrixType the type of the object in which we are taking s sub/main/super diagonal - * \param DiagId the index of the sub/super diagonal. The default is 0 and it means the main diagonal. - * - * The matrix is not required to be square. - * - * This class represents an expression of the main diagonal, or any sub/super diagonal - * of a square matrix. It is the return type of MatrixBase::diagonal() and most of the - * time this is the only way it is used. - * - * \sa MatrixBase::diagonal() - */ -template -struct ei_traits > -{ - typedef typename MatrixType::Scalar Scalar; - typedef typename ei_nested::type MatrixTypeNested; - typedef typename ei_unref::type _MatrixTypeNested; - enum { - AbsDiagId = DiagId<0 ? -DiagId : DiagId, - RowsAtCompileTime = int(MatrixType::SizeAtCompileTime) == Dynamic ? Dynamic - : (EIGEN_ENUM_MIN(MatrixType::RowsAtCompileTime, - MatrixType::ColsAtCompileTime) - AbsDiagId), - ColsAtCompileTime = 1, - MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic - : (EIGEN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime, - MatrixType::MaxColsAtCompileTime) - AbsDiagId), - MaxColsAtCompileTime = 1, - Flags = (unsigned int)_MatrixTypeNested::Flags & (HereditaryBits | LinearAccessBit), - CoeffReadCost = _MatrixTypeNested::CoeffReadCost - }; -}; - -template class DiagonalCoeffs - : public MatrixBase > -{ - enum { - AbsDiagId = DiagId<0 ? -DiagId : DiagId, - OffsetRow = DiagId<0 ? -DiagId : 0, - OffsetCol = DiagId<0 ? 0 : DiagId - }; - public: - - EIGEN_GENERIC_PUBLIC_INTERFACE(DiagonalCoeffs) - - inline DiagonalCoeffs(const MatrixType& matrix) : m_matrix(matrix) {} - - EIGEN_INHERIT_ASSIGNMENT_OPERATORS(DiagonalCoeffs) - - inline int rows() const { return std::min(m_matrix.rows(), m_matrix.cols()) - AbsDiagId; } - inline int cols() const { return 1; } - - inline Scalar& coeffRef(int row, int) - { - return m_matrix.const_cast_derived().coeffRef(row+OffsetRow, row+OffsetCol); - } - - inline const Scalar coeff(int row, int) const - { - return m_matrix.coeff(row+OffsetRow, row+OffsetCol); - } - - inline Scalar& coeffRef(int index) - { - return m_matrix.const_cast_derived().coeffRef(index+OffsetRow, index+OffsetCol); - } - - inline const Scalar coeff(int index) const - { - return m_matrix.coeff(index+OffsetRow, index+OffsetCol); - } - - protected: - - const typename MatrixType::Nested m_matrix; -}; - -/** \returns an expression of the main diagonal of the matrix \c *this - * - * \c *this is not required to be square. - * - * Example: \include MatrixBase_diagonal.cpp - * Output: \verbinclude MatrixBase_diagonal.out - * - * \sa class DiagonalCoeffs */ -template -inline DiagonalCoeffs -MatrixBase::diagonal() -{ - return DiagonalCoeffs(derived()); -} - -/** This is the const version of diagonal(). */ -template -inline const DiagonalCoeffs -MatrixBase::diagonal() const -{ - return DiagonalCoeffs(derived()); -} - -/** \returns an expression of the \a Id-th sub or super diagonal of the matrix \c *this - * - * \c *this is not required to be square. - * - * The template parameter \a Id represent a super diagonal if \a Id > 0 - * and a sub diagonal otherwise. \a Id == 0 is equivalent to the main diagonal. - * - * Example: \include MatrixBase_diagonal_int.cpp - * Output: \verbinclude MatrixBase_diagonal_int.out - * - * \sa MatrixBase::diagonal(), class DiagonalCoeffs */ -template -template -inline DiagonalCoeffs -MatrixBase::diagonal() -{ - return DiagonalCoeffs(derived()); -} - -/** This is the const version of diagonal(). */ -template -template -inline const DiagonalCoeffs -MatrixBase::diagonal() const -{ - return DiagonalCoeffs(derived()); -} - -#endif // EIGEN_DIAGONALCOEFFS_H diff --git a/Eigen/src/Core/DiagonalMatrix.h b/Eigen/src/Core/DiagonalMatrix.h index 57f92a968..596366edc 100644 --- a/Eigen/src/Core/DiagonalMatrix.h +++ b/Eigen/src/Core/DiagonalMatrix.h @@ -67,7 +67,7 @@ class DiagonalMatrixBase : ei_no_assignment_operator, template inline DiagonalMatrixBase(const MatrixBase& other) { - construct_from_expression + construct_from_expression::ret> ::run(derived(),other.derived()); } @@ -109,7 +109,7 @@ class DiagonalMatrixBase : ei_no_assignment_operator, template inline Derived& operator=(const MatrixBase& other) { - construct_from_expression + construct_from_expression::ret> ::run(derived(),other); return derived(); } @@ -149,7 +149,7 @@ template struct ei_traits > : ei_traits > { enum { - Flags = (ei_traits >::Flags & HereditaryBits) | Diagonal + Flags = (ei_traits >::Flags & HereditaryBits) | DiagonalBits }; }; @@ -209,7 +209,7 @@ class DiagonalMatrix template DiagonalMatrix& operator=(const MatrixBase& other) { - EIGEN_STATIC_ASSERT((OtherDerived::Flags&Diagonal)==Diagonal, THIS_METHOD_IS_ONLY_FOR_DIAGONAL_MATRIX); + EIGEN_STATIC_ASSERT(ei_is_diagonal::ret, THIS_METHOD_IS_ONLY_FOR_DIAGONAL_MATRIX); m_coeffs = other.diagonal(); return *this; } @@ -252,7 +252,7 @@ struct ei_traits > ColsAtCompileTime = CoeffsVectorType::SizeAtCompileTime, MaxRowsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime, MaxColsAtCompileTime = CoeffsVectorType::MaxSizeAtCompileTime, - Flags = (_CoeffsVectorTypeNested::Flags & HereditaryBits) | Diagonal, + Flags = (_CoeffsVectorTypeNested::Flags & HereditaryBits) | DiagonalBits, CoeffReadCost = _CoeffsVectorTypeNested::CoeffReadCost }; }; diff --git a/Eigen/src/Core/DiagonalProduct.h b/Eigen/src/Core/DiagonalProduct.h index 83ba96c3c..2dabe4612 100644 --- a/Eigen/src/Core/DiagonalProduct.h +++ b/Eigen/src/Core/DiagonalProduct.h @@ -30,7 +30,7 @@ * Unlike ei_nested, if the argument is a DiagonalMatrix and if it must be evaluated, * then it evaluated to a DiagonalMatrix having its own argument evaluated. */ -template struct ei_nested_diagonal : ei_nested {}; +template::ret> struct ei_nested_diagonal : ei_nested {}; template struct ei_nested_diagonal : ei_nested > {}; @@ -61,8 +61,8 @@ struct ei_traits > MaxRowsAtCompileTime = _LhsNested::MaxRowsAtCompileTime, MaxColsAtCompileTime = _RhsNested::MaxColsAtCompileTime, - LhsIsDiagonal = (_LhsNested::Flags&Diagonal)==Diagonal, - RhsIsDiagonal = (_RhsNested::Flags&Diagonal)==Diagonal, + LhsIsDiagonal = ei_is_diagonal<_LhsNested>::ret, + RhsIsDiagonal = ei_is_diagonal<_RhsNested>::ret, CanVectorizeRhs = (!RhsIsDiagonal) && (RhsFlags & RowMajorBit) && (RhsFlags & PacketAccessBit) && (ColsAtCompileTime % ei_packet_traits::size == 0), @@ -86,7 +86,7 @@ template class Product::_RhsNested _RhsNested; enum { - RhsIsDiagonal = (_RhsNested::Flags&Diagonal)==Diagonal + RhsIsDiagonal = ei_is_diagonal<_RhsNested>::ret }; public: diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 997ca24ed..13b66af73 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -167,9 +167,12 @@ template class MatrixBase inline int rows() const { return derived().rows(); } /** \returns the number of columns. \sa rows(), ColsAtCompileTime*/ inline int cols() const { return derived().cols(); } - /** \returns the number of coefficients, which is \a rows()*cols(). + /** \returns the number of coefficients, which is rows()*cols(). * \sa rows(), cols(), SizeAtCompileTime. */ inline int size() const { return rows() * cols(); } + /** \returns the size of the main diagonal, which is min(rows(),cols()). + * \sa rows(), cols(), SizeAtCompileTime. */ + inline int diagonalSize() const { return std::min(rows(),cols()); } /** \returns the number of nonzero coefficients which is in practice the number * of stored coefficients. */ inline int nonZeros() const { return derived().nonZeros(); } @@ -419,12 +422,15 @@ template class MatrixBase template typename BlockReturnType::SubVectorType segment(int start); template const typename BlockReturnType::SubVectorType segment(int start) const; - DiagonalCoeffs diagonal(); - const DiagonalCoeffs diagonal() const; - - template DiagonalCoeffs diagonal(); - template const DiagonalCoeffs diagonal() const; + Diagonal diagonal(); + const Diagonal diagonal() const; + template Diagonal diagonal(); + template const Diagonal diagonal() const; + + Diagonal diagonal(int index); + const Diagonal diagonal(int index) const; + template Part part(); template const Part part() const; diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index 305efc3dc..bc3ef0641 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -84,7 +84,7 @@ template struct ei_product_mode { enum{ - value = ((Rhs::Flags&Diagonal)==Diagonal) || ((Lhs::Flags&Diagonal)==Diagonal) + value = ei_is_diagonal::ret || ei_is_diagonal::ret ? DiagonalProduct : Lhs::MaxColsAtCompileTime == Dynamic && ( Lhs::MaxRowsAtCompileTime == Dynamic diff --git a/Eigen/src/Core/Transpose.h b/Eigen/src/Core/Transpose.h index edb87d0c3..5c9b7df11 100644 --- a/Eigen/src/Core/Transpose.h +++ b/Eigen/src/Core/Transpose.h @@ -138,7 +138,7 @@ template class Transpose * m = m.transpose().eval(); * \endcode * - * \sa transposeInPlace(), adjoint(), class DiagonalCoeffs */ + * \sa transposeInPlace(), adjoint() */ template inline Transpose MatrixBase::transpose() diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h index 87c0a0859..d6be2153b 100644 --- a/Eigen/src/Core/util/Constants.h +++ b/Eigen/src/Core/util/Constants.h @@ -140,7 +140,7 @@ const unsigned int LinearAccessBit = 0x10; * First, references to the coefficients must be available through coeffRef(int, int). This rules out read-only * expressions whose coefficients are computed on demand by coeff(int, int). Second, the memory layout of the * array of coefficients must be exactly the natural one suggested by rows(), cols(), stride(), and the RowMajorBit. - * This rules out expressions such as DiagonalCoeffs, whose coefficients, though referencable, do not have + * This rules out expressions such as Diagonal, whose coefficients, though referencable, do not have * such a regular memory layout. */ const unsigned int DirectAccessBit = 0x20; @@ -186,17 +186,24 @@ const unsigned int HereditaryBits = RowMajorBit | EvalBeforeAssigningBit | SparseBit; -// Possible values for the Mode parameter of part() and of extract() +// diagonal means both upper and lower triangular +const unsigned DiagonalBits = UpperTriangularBit | LowerTriangularBit; + +// Possible values for the Mode parameter of part() const unsigned int UpperTriangular = UpperTriangularBit; const unsigned int StrictlyUpperTriangular = UpperTriangularBit | ZeroDiagBit; const unsigned int LowerTriangular = LowerTriangularBit; const unsigned int StrictlyLowerTriangular = LowerTriangularBit | ZeroDiagBit; const unsigned int SelfAdjoint = SelfAdjointBit; - -// additional possible values for the Mode parameter of extract() const unsigned int UnitUpperTriangular = UpperTriangularBit | UnitDiagBit; const unsigned int UnitLowerTriangular = LowerTriangularBit | UnitDiagBit; -const unsigned int Diagonal = UpperTriangular | LowerTriangular; + +template struct ei_is_diagonal +{ + enum { + ret = ( (unsigned int)(T::Flags) & DiagonalBits ) == DiagonalBits + }; +}; enum { Aligned, Unaligned }; enum { ForceAligned, AsRequested }; @@ -227,10 +234,10 @@ enum { enum { ColMajor = 0, RowMajor = 0x1, // it is only a coincidence that this is equal to RowMajorBit -- don't rely on that - /** \internal Don't require alignment for the matrix itself (the array of coefficients, if dynamically allocated, may still be - requested to be aligned) */ /** \internal Align the matrix itself if it is vectorizable fixed-size */ AutoAlign = 0, + /** \internal Don't require alignment for the matrix itself (the array of coefficients, if dynamically allocated, may still be + requested to be aligned) */ DontAlign = 0x2 }; diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 59205ce2e..11fed05ec 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -48,7 +48,7 @@ template class Product; template class DiagonalMatrixBase; template class DiagonalMatrixWrapper; template class DiagonalMatrix; -template class DiagonalCoeffs; +template class Diagonal; template class Map; template class Part; template class Extract; diff --git a/Eigen/src/LU/LU.h b/Eigen/src/LU/LU.h index 759766412..32525ebc3 100644 --- a/Eigen/src/LU/LU.h +++ b/Eigen/src/LU/LU.h @@ -333,7 +333,7 @@ LU::LU(const MatrixType& matrix) m_p(matrix.rows()), m_q(matrix.cols()) { - const int size = matrix.diagonal().size(); + const int size = matrix.diagonalSize(); const int rows = matrix.rows(); const int cols = matrix.cols(); @@ -402,7 +402,7 @@ LU::LU(const MatrixType& matrix) template typename ei_traits::Scalar LU::determinant() const { - return Scalar(m_det_pq) * m_lu.diagonal().redux(ei_scalar_product_op()); + return Scalar(m_det_pq) * m_lu.diagonal().prod(); } template diff --git a/Eigen/src/QR/HessenbergDecomposition.h b/Eigen/src/QR/HessenbergDecomposition.h index 6d0ff794e..48dab1d62 100644 --- a/Eigen/src/QR/HessenbergDecomposition.h +++ b/Eigen/src/QR/HessenbergDecomposition.h @@ -58,10 +58,10 @@ template class HessenbergDecomposition typedef Matrix DiagonalType; typedef Matrix SubDiagonalType; - typedef typename NestByValue >::RealReturnType DiagonalReturnType; + typedef typename NestByValue >::RealReturnType DiagonalReturnType; - typedef typename NestByValue > > >::RealReturnType SubDiagonalReturnType; + typedef typename NestByValue >,0 > >::RealReturnType SubDiagonalReturnType; /** This constructor initializes a HessenbergDecomposition object for * further use with HessenbergDecomposition::compute() diff --git a/Eigen/src/QR/Tridiagonalization.h b/Eigen/src/QR/Tridiagonalization.h index 9996a02c8..d63803324 100644 --- a/Eigen/src/QR/Tridiagonalization.h +++ b/Eigen/src/QR/Tridiagonalization.h @@ -60,10 +60,10 @@ template class Tridiagonalization typedef Matrix DiagonalType; typedef Matrix SubDiagonalType; - typedef typename NestByValue >::RealReturnType DiagonalReturnType; + typedef typename NestByValue >::RealReturnType DiagonalReturnType; - typedef typename NestByValue > > >::RealReturnType SubDiagonalReturnType; + typedef typename NestByValue >,0 > >::RealReturnType SubDiagonalReturnType; /** This constructor initializes a Tridiagonalization object for * further use with Tridiagonalization::compute() diff --git a/Eigen/src/Sparse/SparseDiagonalProduct.h b/Eigen/src/Sparse/SparseDiagonalProduct.h index 932daf220..2daa4123c 100644 --- a/Eigen/src/Sparse/SparseDiagonalProduct.h +++ b/Eigen/src/Sparse/SparseDiagonalProduct.h @@ -42,7 +42,7 @@ struct ei_traits > : ei_traits::type _Lhs; typedef typename ei_cleantype::type _Rhs; enum { - SparseFlags = ((int(_Lhs::Flags)&Diagonal)==Diagonal) ? int(_Rhs::Flags) : int(_Lhs::Flags), + SparseFlags = ei_is_diagonal<_Lhs>::ret ? int(_Rhs::Flags) : int(_Lhs::Flags), Flags = SparseBit | (SparseFlags&RowMajorBit) }; }; @@ -58,9 +58,9 @@ class SparseDiagonalProduct : public SparseMatrixBase::_RhsNested _RhsNested; enum { - LhsMode = (_LhsNested::Flags&Diagonal)==Diagonal ? SDP_IsDiagonal + LhsMode = ei_is_diagonal<_LhsNested>::ret ? SDP_IsDiagonal : (_LhsNested::Flags&RowMajorBit) ? SDP_IsSparseRowMajor : SDP_IsSparseColMajor, - RhsMode = (_RhsNested::Flags&Diagonal)==Diagonal ? SDP_IsDiagonal + RhsMode = ei_is_diagonal<_RhsNested>::ret ? SDP_IsDiagonal : (_RhsNested::Flags&RowMajorBit) ? SDP_IsSparseRowMajor : SDP_IsSparseColMajor }; diff --git a/Eigen/src/Sparse/SparseMatrixBase.h b/Eigen/src/Sparse/SparseMatrixBase.h index 74a9a7feb..948424f90 100644 --- a/Eigen/src/Sparse/SparseMatrixBase.h +++ b/Eigen/src/Sparse/SparseMatrixBase.h @@ -384,8 +384,8 @@ template class SparseMatrixBase // template typename BlockReturnType::SubVectorType segment(int start); // template const typename BlockReturnType::SubVectorType segment(int start) const; -// DiagonalCoeffs diagonal(); -// const DiagonalCoeffs diagonal() const; +// Diagonal diagonal(); +// const Diagonal diagonal() const; // template Part part(); // template const Part part() const; diff --git a/Eigen/src/Sparse/SparseProduct.h b/Eigen/src/Sparse/SparseProduct.h index b9c77b6a3..083703cf2 100644 --- a/Eigen/src/Sparse/SparseProduct.h +++ b/Eigen/src/Sparse/SparseProduct.h @@ -29,7 +29,7 @@ template struct ei_sparse_product_mode { enum { - value = ((Lhs::Flags&Diagonal)==Diagonal || (Rhs::Flags&Diagonal)==Diagonal) + value = ei_is_diagonal::ret || ei_is_diagonal::ret ? DiagonalProduct : (Rhs::Flags&Lhs::Flags&SparseBit)==SparseBit ? SparseTimeSparseProduct diff --git a/doc/snippets/MatrixBase_diagonal_int.cpp b/doc/snippets/MatrixBase_diagonal_int.cpp index 0e73d1c16..7b66abf67 100644 --- a/doc/snippets/MatrixBase_diagonal_int.cpp +++ b/doc/snippets/MatrixBase_diagonal_int.cpp @@ -1,5 +1,5 @@ Matrix4i m = Matrix4i::Random(); cout << "Here is the matrix m:" << endl << m << endl; cout << "Here are the coefficients on the 1st super-diagonal and 2nd sub-diagonal of m:" << endl - << m.diagonal<1>().transpose() << endl - << m.diagonal<-2>().transpose() << endl; + << m.diagonal(1).transpose() << endl + << m.diagonal(-2).transpose() << endl; diff --git a/doc/snippets/MatrixBase_diagonal_template_int.cpp b/doc/snippets/MatrixBase_diagonal_template_int.cpp new file mode 100644 index 000000000..0e73d1c16 --- /dev/null +++ b/doc/snippets/MatrixBase_diagonal_template_int.cpp @@ -0,0 +1,5 @@ +Matrix4i m = Matrix4i::Random(); +cout << "Here is the matrix m:" << endl << m << endl; +cout << "Here are the coefficients on the 1st super-diagonal and 2nd sub-diagonal of m:" << endl + << m.diagonal<1>().transpose() << endl + << m.diagonal<-2>().transpose() << endl; diff --git a/test/submatrices.cpp b/test/submatrices.cpp index e34650842..596a8a20e 100644 --- a/test/submatrices.cpp +++ b/test/submatrices.cpp @@ -109,7 +109,6 @@ template void submatrices(const MatrixType& m) VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal()); m2.diagonal() = 2 * m1.diagonal(); m2.diagonal()[0] *= 3; - VERIFY_IS_APPROX(m2.diagonal()[0], static_cast(6) * m1.diagonal()[0]); const int BlockRows = EIGEN_ENUM_MIN(MatrixType::RowsAtCompileTime,2); const int BlockCols = EIGEN_ENUM_MIN(MatrixType::ColsAtCompileTime,5); @@ -152,6 +151,14 @@ template void submatrices(const MatrixType& m) m2.template diagonal() = 2 * m1.template diagonal(); m2.template diagonal()[0] *= 3; VERIFY_IS_APPROX(m2.template diagonal()[0], static_cast(6) * m1.template diagonal()[0]); + + m2.diagonal(N1) = 2 * m1.diagonal(N1); + m2.diagonal(N1)[0] *= 3; + VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast(6) * m1.diagonal(N1)[0]); + + m2.diagonal(N2) = 2 * m1.diagonal(N2); + m2.diagonal(N2)[0] *= 3; + VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast(6) * m1.diagonal(N2)[0]); } // stress some basic stuffs with block matrices