2011-07-26 09:17:18 +02:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
2014-09-01 15:00:19 +02:00
|
|
|
// Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2011-07-26 09:17:18 +02:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// 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/.
|
2011-07-26 09:17:18 +02:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_BASIC_PRECONDITIONERS_H
|
|
|
|
|
#define EIGEN_BASIC_PRECONDITIONERS_H
|
|
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
namespace Eigen {
|
2012-04-15 11:06:28 +01:00
|
|
|
|
2011-12-02 19:02:49 +01:00
|
|
|
/** \ingroup IterativeLinearSolvers_Module
|
|
|
|
|
* \brief A preconditioner based on the digonal entries
|
2011-07-26 09:17:18 +02:00
|
|
|
*
|
|
|
|
|
* This class allows to approximately solve for A.x = b problems assuming A is a diagonal matrix.
|
|
|
|
|
* In other words, this preconditioner neglects all off diagonal entries and, in Eigen's language, solves for:
|
2015-03-04 09:34:27 +01:00
|
|
|
\code
|
|
|
|
|
A.diagonal().asDiagonal() . x = b
|
|
|
|
|
\endcode
|
2011-07-26 09:17:18 +02:00
|
|
|
*
|
2021-08-04 22:41:52 +00:00
|
|
|
* \tparam Scalar_ the type of the scalar.
|
2011-07-26 09:17:18 +02:00
|
|
|
*
|
2015-10-08 10:50:39 +02:00
|
|
|
* \implsparsesolverconcept
|
|
|
|
|
*
|
2011-07-26 09:17:18 +02:00
|
|
|
* This preconditioner is suitable for both selfadjoint and general problems.
|
|
|
|
|
* The diagonal entries are pre-inverted and stored into a dense vector.
|
|
|
|
|
*
|
|
|
|
|
* \note A variant that has yet to be implemented would attempt to preserve the norm of each column.
|
|
|
|
|
*
|
2015-03-04 09:34:27 +01:00
|
|
|
* \sa class LeastSquareDiagonalPreconditioner, class ConjugateGradient
|
2011-07-26 09:17:18 +02:00
|
|
|
*/
|
2021-08-04 22:41:52 +00:00
|
|
|
template <typename Scalar_>
|
2011-07-26 09:17:18 +02:00
|
|
|
class DiagonalPreconditioner {
|
2021-08-04 22:41:52 +00:00
|
|
|
typedef Scalar_ Scalar;
|
2011-07-26 09:17:18 +02:00
|
|
|
typedef Matrix<Scalar, Dynamic, 1> Vector;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
public:
|
2014-12-04 22:48:53 +01:00
|
|
|
typedef typename Vector::StorageIndex StorageIndex;
|
2015-12-01 14:38:47 +01:00
|
|
|
enum { ColsAtCompileTime = Dynamic, MaxColsAtCompileTime = Dynamic };
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
DiagonalPreconditioner() : m_isInitialized(false) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-07-13 20:54:38 +02:00
|
|
|
template <typename MatType>
|
2014-09-23 14:28:23 +02:00
|
|
|
explicit DiagonalPreconditioner(const MatType& mat) : m_invdiag(mat.cols()) {
|
2011-07-26 09:17:18 +02:00
|
|
|
compute(mat);
|
2021-03-08 12:39:11 -05:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2025-04-22 00:58:47 +00:00
|
|
|
constexpr Index rows() const noexcept { return m_invdiag.size(); }
|
|
|
|
|
constexpr Index cols() const noexcept { return m_invdiag.size(); }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-07-13 20:54:38 +02:00
|
|
|
template <typename MatType>
|
|
|
|
|
DiagonalPreconditioner& analyzePattern(const MatType&) {
|
2011-07-26 09:17:18 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-07-13 20:54:38 +02:00
|
|
|
template <typename MatType>
|
|
|
|
|
DiagonalPreconditioner& factorize(const MatType& mat) {
|
2011-07-26 09:17:18 +02:00
|
|
|
m_invdiag.resize(mat.cols());
|
|
|
|
|
for (int j = 0; j < mat.outerSize(); ++j) {
|
2014-09-01 15:00:19 +02:00
|
|
|
typename MatType::InnerIterator it(mat, j);
|
|
|
|
|
while (it && it.index() != j) ++it;
|
|
|
|
|
if (it && it.index() == j && it.value() != Scalar(0))
|
|
|
|
|
m_invdiag(j) = Scalar(1) / it.value();
|
2023-11-29 11:12:48 +00:00
|
|
|
else
|
2014-09-01 15:00:19 +02:00
|
|
|
m_invdiag(j) = Scalar(1);
|
|
|
|
|
}
|
2011-07-26 09:17:18 +02:00
|
|
|
m_isInitialized = true;
|
2012-02-27 14:10:26 +01:00
|
|
|
return *this;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2012-07-13 20:54:38 +02:00
|
|
|
template <typename MatType>
|
2015-03-04 09:34:27 +01:00
|
|
|
DiagonalPreconditioner& compute(const MatType& mat) {
|
2012-02-27 14:10:26 +01:00
|
|
|
return factorize(mat);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-01 15:00:19 +02:00
|
|
|
/** \internal */
|
2011-07-26 09:17:18 +02:00
|
|
|
template <typename Rhs, typename Dest>
|
2014-09-01 15:00:19 +02:00
|
|
|
void _solve_impl(const Rhs& b, Dest& x) const {
|
2011-07-26 09:17:18 +02:00
|
|
|
x = m_invdiag.array() * b.array();
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
template <typename Rhs>
|
2014-09-01 15:00:19 +02:00
|
|
|
inline const Solve<DiagonalPreconditioner, Rhs> solve(const MatrixBase<Rhs>& b) const {
|
|
|
|
|
eigen_assert(m_isInitialized && "DiagonalPreconditioner is not initialized.");
|
|
|
|
|
eigen_assert(m_invdiag.size() == b.rows() &&
|
|
|
|
|
"DiagonalPreconditioner::solve(): invalid number of rows of the right hand side matrix b");
|
|
|
|
|
return Solve<DiagonalPreconditioner, Rhs>(*this, b.derived());
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-04 16:13:34 +02:00
|
|
|
ComputationInfo info() { return Success; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
protected:
|
|
|
|
|
Vector m_invdiag;
|
|
|
|
|
bool m_isInitialized;
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
/** \ingroup IterativeLinearSolvers_Module
|
2015-03-05 10:16:32 +01:00
|
|
|
* \brief Jacobi preconditioner for LeastSquaresConjugateGradient
|
2015-03-04 09:34:27 +01:00
|
|
|
*
|
|
|
|
|
* This class allows to approximately solve for A' A x = A' b problems assuming A' A is a diagonal matrix.
|
|
|
|
|
* In other words, this preconditioner neglects all off diagonal entries and, in Eigen's language, solves for:
|
|
|
|
|
\code
|
|
|
|
|
(A.adjoint() * A).diagonal().asDiagonal() * x = b
|
|
|
|
|
\endcode
|
|
|
|
|
*
|
2021-08-04 22:41:52 +00:00
|
|
|
* \tparam Scalar_ the type of the scalar.
|
2015-03-04 09:34:27 +01:00
|
|
|
*
|
2015-10-08 10:50:39 +02:00
|
|
|
* \implsparsesolverconcept
|
|
|
|
|
*
|
2015-03-04 09:34:27 +01:00
|
|
|
* The diagonal entries are pre-inverted and stored into a dense vector.
|
2021-03-08 12:39:11 -05:00
|
|
|
*
|
2015-03-05 10:16:32 +01:00
|
|
|
* \sa class LeastSquaresConjugateGradient, class DiagonalPreconditioner
|
2015-03-04 09:34:27 +01:00
|
|
|
*/
|
2021-08-04 22:41:52 +00:00
|
|
|
template <typename Scalar_>
|
|
|
|
|
class LeastSquareDiagonalPreconditioner : public DiagonalPreconditioner<Scalar_> {
|
|
|
|
|
typedef Scalar_ Scalar;
|
2015-03-04 09:34:27 +01:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2021-08-04 22:41:52 +00:00
|
|
|
typedef DiagonalPreconditioner<Scalar_> Base;
|
2015-03-04 09:34:27 +01:00
|
|
|
using Base::m_invdiag;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
public:
|
|
|
|
|
LeastSquareDiagonalPreconditioner() : Base() {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
template <typename MatType>
|
|
|
|
|
explicit LeastSquareDiagonalPreconditioner(const MatType& mat) : Base() {
|
|
|
|
|
compute(mat);
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
template <typename MatType>
|
|
|
|
|
LeastSquareDiagonalPreconditioner& analyzePattern(const MatType&) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
template <typename MatType>
|
|
|
|
|
LeastSquareDiagonalPreconditioner& factorize(const MatType& mat) {
|
|
|
|
|
// Compute the inverse squared-norm of each column of mat
|
|
|
|
|
m_invdiag.resize(mat.cols());
|
2017-06-08 15:06:27 +02:00
|
|
|
if (MatType::IsRowMajor) {
|
|
|
|
|
m_invdiag.setZero();
|
|
|
|
|
for (Index j = 0; j < mat.outerSize(); ++j) {
|
|
|
|
|
for (typename MatType::InnerIterator it(mat, j); it; ++it) m_invdiag(it.index()) += numext::abs2(it.value());
|
|
|
|
|
}
|
|
|
|
|
for (Index j = 0; j < mat.cols(); ++j)
|
2017-06-09 11:57:53 +02:00
|
|
|
if (numext::real(m_invdiag(j)) > RealScalar(0)) m_invdiag(j) = RealScalar(1) / numext::real(m_invdiag(j));
|
2017-06-08 15:06:27 +02:00
|
|
|
} else {
|
|
|
|
|
for (Index j = 0; j < mat.outerSize(); ++j) {
|
2017-10-17 21:51:27 +02:00
|
|
|
RealScalar sum = mat.col(j).squaredNorm();
|
2017-06-09 11:57:53 +02:00
|
|
|
if (sum > RealScalar(0))
|
2017-06-08 15:06:27 +02:00
|
|
|
m_invdiag(j) = RealScalar(1) / sum;
|
|
|
|
|
else
|
|
|
|
|
m_invdiag(j) = RealScalar(1);
|
2015-03-04 09:34:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Base::m_isInitialized = true;
|
|
|
|
|
return *this;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2021-03-08 12:39:11 -05:00
|
|
|
|
2015-03-04 09:34:27 +01:00
|
|
|
template <typename MatType>
|
|
|
|
|
LeastSquareDiagonalPreconditioner& compute(const MatType& mat) {
|
|
|
|
|
return factorize(mat);
|
|
|
|
|
}
|
2021-03-08 12:39:11 -05:00
|
|
|
|
2015-08-04 16:13:34 +02:00
|
|
|
ComputationInfo info() { return Success; }
|
2015-03-04 09:34:27 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
};
|
2011-07-26 09:17:18 +02:00
|
|
|
|
2011-12-02 19:02:49 +01:00
|
|
|
/** \ingroup IterativeLinearSolvers_Module
|
|
|
|
|
* \brief A naive preconditioner which approximates any matrix as the identity matrix
|
2011-07-26 09:17:18 +02:00
|
|
|
*
|
2015-10-08 10:50:39 +02:00
|
|
|
* \implsparsesolverconcept
|
|
|
|
|
*
|
2011-07-26 09:17:18 +02:00
|
|
|
* \sa class DiagonalPreconditioner
|
|
|
|
|
*/
|
|
|
|
|
class IdentityPreconditioner {
|
|
|
|
|
public:
|
|
|
|
|
IdentityPreconditioner() {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
template <typename MatrixType>
|
2014-09-23 14:28:23 +02:00
|
|
|
explicit IdentityPreconditioner(const MatrixType&) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-02-27 14:10:26 +01:00
|
|
|
template <typename MatrixType>
|
|
|
|
|
IdentityPreconditioner& analyzePattern(const MatrixType&) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-02-27 14:10:26 +01:00
|
|
|
template <typename MatrixType>
|
|
|
|
|
IdentityPreconditioner& factorize(const MatrixType&) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
template <typename MatrixType>
|
|
|
|
|
IdentityPreconditioner& compute(const MatrixType&) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
template <typename Rhs>
|
|
|
|
|
inline const Rhs& solve(const Rhs& b) const {
|
|
|
|
|
return b;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-08-04 16:13:34 +02:00
|
|
|
ComputationInfo info() { return Success; }
|
2011-07-26 09:17:18 +02:00
|
|
|
};
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2011-07-26 09:17:18 +02:00
|
|
|
#endif // EIGEN_BASIC_PRECONDITIONERS_H
|