| // This file is part of Eigen, a lightweight C++ template library | |
| // for linear algebra. | |
| // | |
| // Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr> | |
| // | |
| // 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 <http://www.gnu.org/licenses/>. | |
| #ifndef EIGEN_SELFADJOINTMATRIX_H | |
| #define EIGEN_SELFADJOINTMATRIX_H | |
| /** \class SelfAdjointView | |
| * \nonstableyet | |
| * | |
| * \brief Expression of a selfadjoint matrix from a triangular part of a dense matrix | |
| * | |
| * \param MatrixType the type of the dense matrix storing the coefficients | |
| * \param TriangularPart can be either \c Lower or \c Upper | |
| * | |
| * This class is an expression of a sefladjoint matrix from a triangular part of a matrix | |
| * with given dense storage of the coefficients. It is the return type of MatrixBase::selfadjointView() | |
| * and most of the time this is the only way that it is used. | |
| * | |
| * \sa class TriangularBase, MatrixBase::selfAdjointView() | |
| */ | |
| template<typename MatrixType, unsigned int UpLo> | |
| struct ei_traits<SelfAdjointView<MatrixType, UpLo> > : ei_traits<MatrixType> | |
| { | |
| typedef typename ei_nested<MatrixType>::type MatrixTypeNested; | |
| typedef typename ei_unref<MatrixTypeNested>::type _MatrixTypeNested; | |
| typedef MatrixType ExpressionType; | |
| enum { | |
| Mode = UpLo | SelfAdjoint, | |
| Flags = _MatrixTypeNested::Flags & (HereditaryBits) | |
| & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)), // FIXME these flags should be preserved | |
| CoeffReadCost = _MatrixTypeNested::CoeffReadCost | |
| }; | |
| }; | |
| template <typename Lhs, int LhsMode, bool LhsIsVector, | |
| typename Rhs, int RhsMode, bool RhsIsVector> | |
| struct SelfadjointProductMatrix; | |
| // FIXME could also be called SelfAdjointWrapper to be consistent with DiagonalWrapper ?? | |
| template<typename MatrixType, unsigned int UpLo> class SelfAdjointView | |
| : public TriangularBase<SelfAdjointView<MatrixType, UpLo> > | |
| { | |
| public: | |
| typedef TriangularBase<SelfAdjointView> Base; | |
| typedef typename ei_traits<SelfAdjointView>::Scalar Scalar; | |
| enum { | |
| Mode = ei_traits<SelfAdjointView>::Mode | |
| }; | |
| typedef typename MatrixType::PlainObject PlainObject; | |
| inline SelfAdjointView(const MatrixType& matrix) : m_matrix(matrix) | |
| { ei_assert(ei_are_flags_consistent<Mode>::ret); } | |
| inline int rows() const { return m_matrix.rows(); } | |
| inline int cols() const { return m_matrix.cols(); } | |
| inline int outerStride() const { return m_matrix.outerStride(); } | |
| inline int innerStride() const { return m_matrix.innerStride(); } | |
| /** \sa MatrixBase::coeff() | |
| * \warning the coordinates must fit into the referenced triangular part | |
| */ | |
| inline Scalar coeff(int row, int col) const | |
| { | |
| Base::check_coordinates_internal(row, col); | |
| return m_matrix.coeff(row, col); | |
| } | |
| /** \sa MatrixBase::coeffRef() | |
| * \warning the coordinates must fit into the referenced triangular part | |
| */ | |
| inline Scalar& coeffRef(int row, int col) | |
| { | |
| Base::check_coordinates_internal(row, col); | |
| return m_matrix.const_cast_derived().coeffRef(row, col); | |
| } | |
| /** \internal */ | |
| const MatrixType& _expression() const { return m_matrix; } | |
| const MatrixType& nestedExpression() const { return m_matrix; } | |
| MatrixType& nestedExpression() { return const_cast<MatrixType&>(m_matrix); } | |
| /** Efficient self-adjoint matrix times vector/matrix product */ | |
| template<typename OtherDerived> | |
| SelfadjointProductMatrix<MatrixType,Mode,false,OtherDerived,0,OtherDerived::IsVectorAtCompileTime> | |
| operator*(const MatrixBase<OtherDerived>& rhs) const | |
| { | |
| return SelfadjointProductMatrix | |
| <MatrixType,Mode,false,OtherDerived,0,OtherDerived::IsVectorAtCompileTime> | |
| (m_matrix, rhs.derived()); | |
| } | |
| /** Efficient vector/matrix times self-adjoint matrix product */ | |
| template<typename OtherDerived> friend | |
| SelfadjointProductMatrix<OtherDerived,0,OtherDerived::IsVectorAtCompileTime,MatrixType,Mode,false> | |
| operator*(const MatrixBase<OtherDerived>& lhs, const SelfAdjointView& rhs) | |
| { | |
| return SelfadjointProductMatrix | |
| <OtherDerived,0,OtherDerived::IsVectorAtCompileTime,MatrixType,Mode,false> | |
| (lhs.derived(),rhs.m_matrix); | |
| } | |
| /** Perform a symmetric rank 2 update of the selfadjoint matrix \c *this: | |
| * \f$ this = this + \alpha ( u v^* + v u^*) \f$ | |
| * \returns a reference to \c *this | |
| * | |
| * The vectors \a u and \c v \b must be column vectors, however they can be | |
| * a adjoint expression without any overhead. Only the meaningful triangular | |
| * part of the matrix is updated, the rest is left unchanged. | |
| * | |
| * \sa rankUpdate(const MatrixBase<DerivedU>&, Scalar) | |
| */ | |
| template<typename DerivedU, typename DerivedV> | |
| SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const MatrixBase<DerivedV>& v, Scalar alpha = Scalar(1)); | |
| /** Perform a symmetric rank K update of the selfadjoint matrix \c *this: | |
| * \f$ this = this + \alpha ( u u^* ) \f$ where \a u is a vector or matrix. | |
| * | |
| * \returns a reference to \c *this | |
| * | |
| * Note that to perform \f$ this = this + \alpha ( u^* u ) \f$ you can simply | |
| * call this function with u.adjoint(). | |
| * | |
| * \sa rankUpdate(const MatrixBase<DerivedU>&, const MatrixBase<DerivedV>&, Scalar) | |
| */ | |
| template<typename DerivedU> | |
| SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, Scalar alpha = Scalar(1)); | |
| /////////// Cholesky module /////////// | |
| const LLT<PlainObject, UpLo> llt() const; | |
| const LDLT<PlainObject> ldlt() const; | |
| /////////// Eigenvalue module /////////// | |
| /** Real part of #Scalar */ | |
| typedef typename NumTraits<Scalar>::Real RealScalar; | |
| /** Return type of eigenvalues() */ | |
| typedef Matrix<RealScalar, ei_traits<MatrixType>::ColsAtCompileTime, 1> EigenvaluesReturnType; | |
| EigenvaluesReturnType eigenvalues() const; | |
| RealScalar operatorNorm() const; | |
| protected: | |
| const typename MatrixType::Nested m_matrix; | |
| }; | |
| // template<typename OtherDerived, typename MatrixType, unsigned int UpLo> | |
| // ei_selfadjoint_matrix_product_returntype<OtherDerived,SelfAdjointView<MatrixType,UpLo> > | |
| // operator*(const MatrixBase<OtherDerived>& lhs, const SelfAdjointView<MatrixType,UpLo>& rhs) | |
| // { | |
| // return ei_matrix_selfadjoint_product_returntype<OtherDerived,SelfAdjointView<MatrixType,UpLo> >(lhs.derived(),rhs); | |
| // } | |
| // selfadjoint to dense matrix | |
| template<typename Derived1, typename Derived2, int UnrollCount, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, (SelfAdjoint|Upper), UnrollCount, ClearOpposite> | |
| { | |
| enum { | |
| col = (UnrollCount-1) / Derived1::RowsAtCompileTime, | |
| row = (UnrollCount-1) % Derived1::RowsAtCompileTime | |
| }; | |
| inline static void run(Derived1 &dst, const Derived2 &src) | |
| { | |
| ei_triangular_assignment_selector<Derived1, Derived2, (SelfAdjoint|Upper), UnrollCount-1, ClearOpposite>::run(dst, src); | |
| if(row == col) | |
| dst.coeffRef(row, col) = ei_real(src.coeff(row, col)); | |
| else if(row < col) | |
| dst.coeffRef(col, row) = ei_conj(dst.coeffRef(row, col) = src.coeff(row, col)); | |
| } | |
| }; | |
| template<typename Derived1, typename Derived2, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, SelfAdjoint|Upper, 0, ClearOpposite> | |
| { | |
| inline static void run(Derived1 &, const Derived2 &) {} | |
| }; | |
| template<typename Derived1, typename Derived2, int UnrollCount, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, (SelfAdjoint|Lower), UnrollCount, ClearOpposite> | |
| { | |
| enum { | |
| col = (UnrollCount-1) / Derived1::RowsAtCompileTime, | |
| row = (UnrollCount-1) % Derived1::RowsAtCompileTime | |
| }; | |
| inline static void run(Derived1 &dst, const Derived2 &src) | |
| { | |
| ei_triangular_assignment_selector<Derived1, Derived2, (SelfAdjoint|Lower), UnrollCount-1, ClearOpposite>::run(dst, src); | |
| if(row == col) | |
| dst.coeffRef(row, col) = ei_real(src.coeff(row, col)); | |
| else if(row > col) | |
| dst.coeffRef(col, row) = ei_conj(dst.coeffRef(row, col) = src.coeff(row, col)); | |
| } | |
| }; | |
| template<typename Derived1, typename Derived2, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, SelfAdjoint|Lower, 0, ClearOpposite> | |
| { | |
| inline static void run(Derived1 &, const Derived2 &) {} | |
| }; | |
| template<typename Derived1, typename Derived2, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, SelfAdjoint|Upper, Dynamic, ClearOpposite> | |
| { | |
| inline static void run(Derived1 &dst, const Derived2 &src) | |
| { | |
| for(int j = 0; j < dst.cols(); ++j) | |
| { | |
| for(int i = 0; i < j; ++i) | |
| { | |
| dst.copyCoeff(i, j, src); | |
| dst.coeffRef(j,i) = ei_conj(dst.coeff(i,j)); | |
| } | |
| dst.copyCoeff(j, j, src); | |
| } | |
| } | |
| }; | |
| template<typename Derived1, typename Derived2, bool ClearOpposite> | |
| struct ei_triangular_assignment_selector<Derived1, Derived2, SelfAdjoint|Lower, Dynamic, ClearOpposite> | |
| { | |
| inline static void run(Derived1 &dst, const Derived2 &src) | |
| { | |
| for(int i = 0; i < dst.rows(); ++i) | |
| { | |
| for(int j = 0; j < i; ++j) | |
| { | |
| dst.copyCoeff(i, j, src); | |
| dst.coeffRef(j,i) = ei_conj(dst.coeff(i,j)); | |
| } | |
| dst.copyCoeff(i, i, src); | |
| } | |
| } | |
| }; | |
| /*************************************************************************** | |
| * Implementation of MatrixBase methods | |
| ***************************************************************************/ | |
| template<typename Derived> | |
| template<unsigned int UpLo> | |
| const SelfAdjointView<Derived, UpLo> MatrixBase<Derived>::selfadjointView() const | |
| { | |
| return derived(); | |
| } | |
| template<typename Derived> | |
| template<unsigned int UpLo> | |
| SelfAdjointView<Derived, UpLo> MatrixBase<Derived>::selfadjointView() | |
| { | |
| return derived(); | |
| } | |
| #endif // EIGEN_SELFADJOINTMATRIX_H |