12#include <Eigen/IterativeLinearSolvers>
13#include <Eigen/Sparse>
15class ConstrainedConjugateGradient;
33template<
typename MatrixType,
36 typename FilterMatrixType,
41 const FilterMatrixType &
filter,
44 typename Dest::RealScalar &tol_error)
48 using RealScalar =
typename Dest::RealScalar;
49 using Scalar =
typename Dest::Scalar;
50 using VectorType = Matrix<Scalar, Dynamic, 1>;
52 RealScalar tol = tol_error;
57 VectorType residual =
filter * (rhs - mat *
x);
59 RealScalar rhsNorm2 = (
filter * rhs).squaredNorm();
67 RealScalar threshold = tol * tol * rhsNorm2;
68 RealScalar residualNorm2 = residual.squaredNorm();
69 if (residualNorm2 < threshold) {
71 tol_error =
sqrt(residualNorm2 / rhsNorm2);
76 p =
filter * precond.solve(residual);
78 VectorType
z(n), tmp(n);
79 RealScalar absNew = numext::real(
82 while (i < maxIters) {
83 tmp.noalias() =
filter * (mat * p);
85 Scalar alpha = absNew / p.dot(tmp);
87 residual -= alpha * tmp;
89 residualNorm2 = residual.squaredNorm();
90 if (residualNorm2 < threshold) {
94 z = precond.solve(residual);
96 RealScalar absOld = absNew;
97 absNew = numext::real(residual.dot(
z));
100 RealScalar
beta = absNew / absOld;
105 tol_error =
sqrt(residualNorm2 / rhsNorm2);
112template<
typename MatrixType>
struct MatrixFilter {
113 MatrixFilter() : m_cmat(NULL) {}
115 MatrixFilter(
const MatrixType &cmat) : m_cmat(&cmat) {}
117 void setMatrix(
const MatrixType &cmat)
122 template<
typename VectorType>
void apply(VectorType v)
const
128 const MatrixType *m_cmat;
132template<
typename _MatrixType,
134 typename _FilterMatrixType = _MatrixType,
135 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar>>
139 template<
typename _MatrixType,
int _UpLo,
typename _FilterMatrixType,
typename _Preconditioner>
199template<
typename _MatrixType,
int _UpLo,
typename _FilterMatrixType,
typename _Preconditioner>
201 :
public IterativeSolverBase<
202 ConstrainedConjugateGradient<_MatrixType, _UpLo, _FilterMatrixType, _Preconditioner>> {
203 using Base = IterativeSolverBase<ConstrainedConjugateGradient>;
206 using Base::m_isInitialized;
207 using Base::m_iterations;
208 using Base::mp_matrix;
212 using Scalar =
typename MatrixType::Scalar;
213 using Index =
typename MatrixType::Index;
253 template<
typename Rhs,
typename Guess>
254 internal::solve_retval_with_guess<ConstrainedConjugateGradient, Rhs, Guess>
solveWithGuess(
255 const MatrixBase<Rhs> &
b,
const Guess &x0)
const
257 eigen_assert(m_isInitialized &&
"ConjugateGradient is not initialized.");
259 Base::rows() ==
b.rows() &&
260 "ConjugateGradient::solve(): invalid number of rows of the right hand side matrix b");
261 return internal::solve_retval_with_guess<ConstrainedConjugateGradient, Rhs, Guess>(
262 *
this,
b.derived(), x0);
268 m_iterations = Base::maxIterations();
269 m_error = Base::m_tolerance;
271 for (
int j = 0; j <
b.cols(); j++) {
272 m_iterations = Base::maxIterations();
273 m_error = Base::m_tolerance;
275 typename Dest::ColXpr xj(
x, j);
280 Base::m_preconditioner,
285 m_isInitialized =
true;
286 m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
290 template<
typename Rhs,
typename Dest>
void _solve(
const Rhs &
b, Dest &
x)
const
302template<
typename _MatrixType,
int _UpLo,
typename _Filter,
typename _Preconditioner,
typename Rhs>
305 : solve_retval_base<ConstrainedConjugateGradient<_MatrixType, _UpLo, _Filter, _Preconditioner>,
308 EIGEN_MAKE_SOLVE_HELPERS(
Dec, Rhs)
310 template<typename Dest>
void evalTo(Dest &dst)
const
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
A conjugate gradient solver for sparse self-adjoint problems with additional constraints.
ConstrainedConjugateGradient(const MatrixType &A)
FilterMatrixType & filter()
internal::solve_retval_with_guess< ConstrainedConjugateGradient, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
~ConstrainedConjugateGradient()=default
void _solve(const Rhs &b, Dest &x) const
const FilterMatrixType & filter() const
ConstrainedConjugateGradient()
FilterMatrixType m_filter
typename MatrixType::Scalar Scalar
void _solveWithGuess(const Rhs &b, Dest &x) const
_Preconditioner Preconditioner
typename MatrixType::RealScalar RealScalar
_FilterMatrixType FilterMatrixType
typename MatrixType::Index Index
ccl_device_inline float beta(const float x, const float y)
EIGEN_DONT_INLINE void constrained_conjugate_gradient(const MatrixType &mat, const Rhs &rhs, Dest &x, const FilterMatrixType &filter, const Preconditioner &precond, int &iters, typename Dest::RealScalar &tol_error)
void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
ATTR_WARN_UNUSED_RESULT const BMVert * v
ConstrainedConjugateGradient< _MatrixType, _UpLo, _Filter, _Preconditioner > Dec
void evalTo(Dest &dst) const
_FilterMatrixType FilterMatrixType
_Preconditioner Preconditioner