| // This file is part of Eigen, a lightweight C++ template library |
| // for linear algebra. |
| // |
| // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr> |
| // |
| // This Source Code Form is subject to the terms of the Mozilla |
| // Public License v. 2.0. If a copy of the MPL was not distributed |
| // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| // SPDX-License-Identifier: MPL-2.0 |
| |
| #ifndef EIGEN_SELFADJOINTMATRIX_H |
| #define EIGEN_SELFADJOINTMATRIX_H |
| |
| // IWYU pragma: private |
| #include "./InternalHeaderCheck.h" |
| |
| namespace Eigen { |
| |
| /** \class SelfAdjointView |
| * \ingroup Core_Module |
| * |
| * |
| * \brief Expression of a selfadjoint matrix from a triangular part of a dense matrix |
| * |
| * \tparam MatrixType the type of the dense matrix storing the coefficients |
| * \tparam TriangularPart can be either \c #Lower or \c #Upper |
| * |
| * This class is an expression of a selfadjoint 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() |
| */ |
| |
| namespace internal { |
| template <typename MatrixType, unsigned int UpLo> |
| struct traits<SelfAdjointView<MatrixType, UpLo> > : traits<MatrixType> { |
| using MatrixTypeNested = typename ref_selector<MatrixType>::non_const_type; |
| using MatrixTypeNestedCleaned = remove_all_t<MatrixTypeNested>; |
| using ExpressionType = MatrixType; |
| using FullMatrixType = typename MatrixType::PlainObject; |
| enum { |
| Mode = UpLo | SelfAdjoint, |
| FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0, |
| Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) & |
| (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved |
| }; |
| }; |
| |
| } // namespace internal |
| |
| template <typename MatrixType_, unsigned int UpLo> |
| class SelfAdjointView : public TriangularBase<SelfAdjointView<MatrixType_, UpLo> > { |
| public: |
| EIGEN_STATIC_ASSERT(UpLo == Lower || UpLo == Upper, SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY) |
| |
| using MatrixType = MatrixType_; |
| using Base = TriangularBase<SelfAdjointView>; |
| using MatrixTypeNested = typename internal::traits<SelfAdjointView>::MatrixTypeNested; |
| using MatrixTypeNestedCleaned = typename internal::traits<SelfAdjointView>::MatrixTypeNestedCleaned; |
| using NestedExpression = MatrixTypeNestedCleaned; |
| |
| /** \brief The type of coefficients in this matrix */ |
| using Scalar = typename internal::traits<SelfAdjointView>::Scalar; |
| /** Real part of #Scalar */ |
| using RealScalar = typename NumTraits<Scalar>::Real; |
| using StorageIndex = typename MatrixType::StorageIndex; |
| |
| enum { |
| Mode = internal::traits<SelfAdjointView>::Mode, |
| Flags = internal::traits<SelfAdjointView>::Flags, |
| TransposeMode = ((int(Mode) & int(Upper)) ? Lower : 0) | ((int(Mode) & int(Lower)) ? Upper : 0) |
| }; |
| using PlainObject = typename MatrixType::PlainObject; |
| |
| EIGEN_DEVICE_FUNC explicit inline SelfAdjointView(MatrixType& matrix) : m_matrix(matrix) {} |
| using Base::operator*; |
| EIGEN_DEFAULT_COPY_CONSTRUCTOR(SelfAdjointView) |
| |
| /** Assigns a matrix expression to the referenced triangular part of the selfadjoint matrix. */ |
| template <typename OtherDerived> |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const MatrixBase<OtherDerived>& other) { |
| m_matrix.template triangularView<UpLo>() = other; |
| return *this; |
| } |
| |
| /** Assigns a triangular or selfadjoint expression without materializing a dense temporary. */ |
| template <typename OtherDerived> |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const TriangularBase<OtherDerived>& other) { |
| other.evalToLazy(m_matrix); |
| return *this; |
| } |
| |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator=(const SelfAdjointView& other) { |
| return *this = static_cast<const Base&>(other); |
| } |
| |
| /** \sa MatrixBase::operator+=() */ |
| template <typename OtherDerived> |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator+=(const DenseBase<OtherDerived>& other) { |
| m_matrix.template triangularView<UpLo>() += other; |
| return *this; |
| } |
| |
| /** \sa MatrixBase::operator-=() */ |
| template <typename OtherDerived> |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator-=(const DenseBase<OtherDerived>& other) { |
| m_matrix.template triangularView<UpLo>() -= other; |
| return *this; |
| } |
| |
| /** \sa MatrixBase::operator*=() */ |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator*=(const Scalar& other) { |
| eigen_assert(numext::imag(other) == typename NumTraits<Scalar>::Real(0) && |
| "SelfAdjointView in-place scaling requires a real scalar; " |
| "scaling only the stored triangle by a non-real scalar would " |
| "leave conj(other) on the unstored half."); |
| m_matrix.template triangularView<UpLo>() *= other; |
| return *this; |
| } |
| |
| /** \sa DenseBase::operator/=() */ |
| EIGEN_DEVICE_FUNC SelfAdjointView& operator/=(const Scalar& other) { |
| eigen_assert(numext::imag(other) == typename NumTraits<Scalar>::Real(0) && |
| "SelfAdjointView in-place division requires a real scalar; " |
| "dividing only the stored triangle by a non-real scalar would " |
| "leave conj(other) on the unstored half."); |
| m_matrix.template triangularView<UpLo>() /= other; |
| return *this; |
| } |
| |
| /** \internal */ |
| EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& _expression() const noexcept { return m_matrix; } |
| |
| EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& nestedExpression() const noexcept { return m_matrix; } |
| EIGEN_DEVICE_FUNC constexpr MatrixTypeNestedCleaned& nestedExpression() noexcept { return m_matrix; } |
| |
| EIGEN_DEVICE_FUNC const SelfAdjointView< |
| const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(MatrixType, Scalar, internal::scalar_product_op), UpLo> |
| operator*(const Scalar& s) const { |
| return (nestedExpression() * s).template selfadjointView<UpLo>(); |
| } |
| |
| friend EIGEN_DEVICE_FUNC const SelfAdjointView< |
| const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, MatrixType, internal::scalar_product_op), UpLo> |
| operator*(const Scalar& s, const SelfAdjointView& mat) { |
| return (s * mat.nestedExpression()).template selfadjointView<UpLo>(); |
| } |
| |
| /** Perform a symmetric rank 2 update of the selfadjoint matrix \c *this: |
| * \f$ this = this + \alpha u v^* + conj(\alpha) 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 |
| * an 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> |
| EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const MatrixBase<DerivedV>& v, |
| const 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> |
| EIGEN_DEVICE_FUNC SelfAdjointView& rankUpdate(const MatrixBase<DerivedU>& u, const Scalar& alpha = Scalar(1)); |
| |
| /** \returns an expression of a triangular view extracted from the current selfadjoint view of a given triangular part |
| * |
| * The parameter \a TriMode can have the following values: \c #Upper, \c #StrictlyUpper, \c #UnitUpper, |
| * \c #Lower, \c #StrictlyLower, \c #UnitLower. |
| * |
| * If \c TriMode references the same triangular part than \c *this, then this method simply return a \c TriangularView |
| * of the nested expression, otherwise, the nested expression is first transposed, thus returning a \c |
| * TriangularView<Transpose<MatrixType>> object. |
| * |
| * \sa MatrixBase::triangularView(), class TriangularView |
| */ |
| template <unsigned int TriMode> |
| EIGEN_DEVICE_FUNC |
| std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), TriangularView<MatrixType, TriMode>, |
| TriangularView<typename MatrixType::AdjointReturnType, TriMode> > |
| triangularView() const { |
| std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), MatrixType&, |
| typename MatrixType::ConstTransposeReturnType> |
| tmp1(m_matrix); |
| std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), MatrixType&, |
| typename MatrixType::AdjointReturnType> |
| tmp2(tmp1); |
| return std::conditional_t<(TriMode & (Upper | Lower)) == (UpLo & (Upper | Lower)), |
| TriangularView<MatrixType, TriMode>, |
| TriangularView<typename MatrixType::AdjointReturnType, TriMode> >(tmp2); |
| } |
| |
| /** \returns a const expression of the main diagonal of the matrix \c *this |
| * |
| * This method simply returns the diagonal of the nested expression, thus by-passing the SelfAdjointView decorator. |
| * |
| * \sa MatrixBase::diagonal(), class Diagonal */ |
| EIGEN_DEVICE_FUNC typename MatrixType::ConstDiagonalReturnType diagonal() const { |
| return typename MatrixType::ConstDiagonalReturnType(m_matrix); |
| } |
| |
| /** \returns the matrix 1-norm (maximum absolute column sum) of the implicit |
| * full self-adjoint matrix, reading only the stored triangle. For Hermitian |
| * (complex) scalars the unstored entries are conjugates of stored ones, and |
| * since |conj(x)| = |x| the result matches the L1 norm of the full matrix. |
| */ |
| EIGEN_DEVICE_FUNC RealScalar l1Norm() const { |
| #ifdef EIGEN_GPU_COMPILE_PHASE |
| // The panel accumulator below is per-thread local storage on a device, so it would cost every |
| // kernel instantiating this kPanelSize scalars of stack and the registers to address them. |
| return l1NormPerColumn(); |
| #else |
| // For a self-adjoint matrix |a_ij| = |a_ji|, so the stored triangle of a row-major matrix is |
| // the transposed, column-major, complementary one and yields the same norm read the fast way. |
| EIGEN_IF_CONSTEXPR (bool(MatrixType::IsRowMajor)) { |
| return l1NormColumnwise<TransposeMode>(m_matrix.transpose()); |
| } else { |
| return l1NormColumnwise<UpLo>(m_matrix); |
| } |
| #endif |
| } |
| |
| private: |
| // Reading the mirrored term of column j as a row of the stored triangle costs a stride-n |
| // traversal of a column-major matrix. Instead accumulate column sums a panel at a time: |
| // |a_ij| from a column left of the panel is added to the sum of column i, which walks that |
| // column. Only the panel's diagonal block keeps the row traversal, where it is cache resident, |
| // and a panel-sized accumulator stays a stack object. |
| template <int Mode, typename Mat> |
| EIGEN_DEVICE_FUNC static RealScalar l1NormColumnwise(const Mat& m) { |
| static constexpr int kPanelSize = 64; |
| RealScalar norm = RealScalar(0); |
| const Index n = m.rows(); |
| Matrix<RealScalar, kPanelSize, 1> sums; |
| for (Index p = 0; p < n; p += kPanelSize) { |
| const Index len = numext::mini(Index(kPanelSize), n - p); |
| EIGEN_IF_CONSTEXPR (Mode == Lower) { |
| for (Index j = 0; j < len; ++j) |
| sums.coeffRef(j) = |
| m.col(p + j).tail(n - p - j).template lpNorm<1>() + m.row(p + j).segment(p, j).template lpNorm<1>(); |
| for (Index j = 0; j < p; ++j) sums.head(len) += m.col(j).segment(p, len).cwiseAbs(); |
| } else { |
| for (Index j = 0; j < len; ++j) |
| sums.coeffRef(j) = m.col(p + j).head(p + j + 1).template lpNorm<1>() + |
| m.row(p + j).segment(p + j + 1, len - j - 1).template lpNorm<1>(); |
| for (Index j = p + len; j < n; ++j) sums.head(len) += m.col(j).segment(p, len).cwiseAbs(); |
| } |
| norm = numext::maxi(norm, sums.head(len).maxCoeff()); |
| } |
| return norm; |
| } |
| |
| // Workspace-free form, one column sum at a time; the mirrored term is read as a row. |
| EIGEN_DEVICE_FUNC RealScalar l1NormPerColumn() const { |
| RealScalar norm = RealScalar(0); |
| const Index n = m_matrix.rows(); |
| for (Index col = 0; col < n; ++col) { |
| RealScalar abs_col_sum; |
| EIGEN_IF_CONSTEXPR (UpLo == Lower) { |
| abs_col_sum = |
| m_matrix.col(col).tail(n - col).template lpNorm<1>() + m_matrix.row(col).head(col).template lpNorm<1>(); |
| } else { |
| abs_col_sum = |
| m_matrix.col(col).head(col).template lpNorm<1>() + m_matrix.row(col).tail(n - col).template lpNorm<1>(); |
| } |
| norm = numext::maxi(norm, abs_col_sum); |
| } |
| return norm; |
| } |
| |
| public: |
| /////////// Cholesky module /////////// |
| |
| LLT<PlainObject, UpLo> llt() const; |
| LDLT<PlainObject, UpLo> ldlt() const; |
| BunchKaufman<PlainObject, UpLo> bunchKaufman() const; |
| |
| /////////// Eigenvalue module /////////// |
| |
| /** Return type of eigenvalues() */ |
| using EigenvaluesReturnType = Matrix<RealScalar, internal::traits<MatrixType>::ColsAtCompileTime, 1>; |
| |
| EIGEN_DEVICE_FUNC EigenvaluesReturnType eigenvalues() const; |
| EIGEN_DEVICE_FUNC RealScalar operatorNorm() const; |
| |
| protected: |
| MatrixTypeNested m_matrix; |
| }; |
| |
| // selfadjoint to dense matrix |
| |
| namespace internal { |
| |
| // TODO currently a selfadjoint expression has the form SelfAdjointView<.,.> |
| // in the future selfadjoint-ness should be defined by the expression traits |
| // such that Transpose<SelfAdjointView<.,.> > is valid. (currently TriangularBase::transpose() is overloaded to |
| // make it work) |
| template <typename MatrixType, unsigned int Mode> |
| struct evaluator_traits<SelfAdjointView<MatrixType, Mode> > { |
| using Kind = typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind; |
| using Shape = SelfAdjointShape; |
| }; |
| |
| template <int UpLo, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor, |
| int Version> |
| class triangular_dense_assignment_kernel<UpLo, SelfAdjoint, SetOpposite, DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, |
| Version> |
| : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> { |
| protected: |
| using Base = generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>; |
| using DstXprType = typename Base::DstXprType; |
| using SrcXprType = typename Base::SrcXprType; |
| using Base::m_dst; |
| using Base::m_functor; |
| using Base::m_src; |
| |
| public: |
| using DstEvaluatorType = typename Base::DstEvaluatorType; |
| using SrcEvaluatorType = typename Base::SrcEvaluatorType; |
| using Scalar = typename Base::Scalar; |
| using AssignmentTraits = typename Base::AssignmentTraits; |
| |
| EIGEN_DEVICE_FUNC triangular_dense_assignment_kernel(DstEvaluatorType& dst, const SrcEvaluatorType& src, |
| const Functor& func, DstXprType& dstExpr) |
| : Base(dst, src, func, dstExpr) {} |
| |
| EIGEN_DEVICE_FUNC void assignCoeff(Index row, Index col) { |
| eigen_internal_assert(row != col); |
| Scalar tmp = m_src.coeff(row, col); |
| m_functor.assignCoeff(m_dst.coeffRef(row, col), tmp); |
| m_functor.assignCoeff(m_dst.coeffRef(col, row), numext::conj(tmp)); |
| } |
| |
| // Override to ensure the SelfAdjoint assignCoeff (which mirrors conjugates) is called, |
| // not the base class version (which is a plain copy). |
| EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void assignCoeffByOuterInner(Index outer, Index inner) { |
| Index row = Base::rowIndexByOuterInner(outer, inner); |
| Index col = Base::colIndexByOuterInner(outer, inner); |
| assignCoeff(row, col); |
| } |
| |
| EIGEN_DEVICE_FUNC void assignDiagonalCoeff(Index id) { Base::assignCoeff(id, id); } |
| |
| EIGEN_DEVICE_FUNC void assignOppositeCoeff(Index, Index) { eigen_internal_assert(false && "should never be called"); } |
| }; |
| |
| } // end namespace internal |
| |
| /*************************************************************************** |
| * Implementation of MatrixBase methods |
| ***************************************************************************/ |
| |
| /** This is the const version of MatrixBase::selfadjointView() */ |
| template <typename Derived> |
| template <unsigned int UpLo> |
| EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type |
| MatrixBase<Derived>::selfadjointView() const { |
| return typename ConstSelfAdjointViewReturnType<UpLo>::Type(derived()); |
| } |
| |
| /** \returns an expression of a symmetric/self-adjoint view extracted from the upper or lower triangular part of the |
| * current matrix |
| * |
| * The parameter \a UpLo can be either \c #Upper or \c #Lower |
| * |
| * Example: \include MatrixBase_selfadjointView.cpp |
| * Output: \verbinclude MatrixBase_selfadjointView.out |
| * |
| * \sa class SelfAdjointView |
| */ |
| template <typename Derived> |
| template <unsigned int UpLo> |
| EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type |
| MatrixBase<Derived>::selfadjointView() { |
| return typename SelfAdjointViewReturnType<UpLo>::Type(derived()); |
| } |
| |
| } // end namespace Eigen |
| |
| #endif // EIGEN_SELFADJOINTMATRIX_H |