22 template<
typename Scalar>
23 class ProjectedGaussSeidelSolver
28 void setA(Eigen::SparseMatrix<Scalar>* A) { this->m_A = A; }
33 void setMaxIterations(
const unsigned int maxIterations) { this->m_maxIterations = maxIterations; }
38 void setRelaxation(
const Scalar relaxation) { this->m_relaxation = relaxation; }
44 void setEpsilon(
const Scalar epsilon) { this->m_epsilon = epsilon; }
51 Eigen::Matrix<Scalar, -1, 1>& solve(
const Eigen::Matrix<Scalar, -1, 1>& b,
const Eigen::Matrix<Scalar, -1, 2>& cu)
53 const Eigen::SparseMatrix<Scalar>& A = *m_A;
56 m_x = Eigen::Matrix<Scalar, -1, 1>(b.rows());
63 Eigen::Matrix<Scalar, -1, 1> xOld;
64 for (
unsigned int i = 0; i < m_maxIterations; i++)
67 for (Eigen::Index r = 0; r < A.rows(); r++)
72 for (Eigen::Index c = 0; c < r; c++)
74 delta += A.coeff(r, c) * m_x[c];
76 for (Eigen::Index c = r + 1; c < A.cols(); c++)
78 delta += A.coeff(r, c) * m_x[c];
83 delta = (b[r] - delta) / A.coeff(r, r);
85 m_x(r) += m_relaxation * (delta - m_x(r));
87 m_x(r) = std::min(cu(r, 1), std::max(cu(r, 0), m_x(r)));
91 m_conv = (m_x - xOld).norm();
93 if (m_conv < m_epsilon)
105 unsigned int m_maxIterations = 3;
106 Scalar m_relaxation =
static_cast<Scalar
>(0.1);
107 Scalar m_epsilon = 1.0e-4;
109 Eigen::Matrix<Scalar, -1, 1> m_x;
110 Eigen::SparseMatrix<Scalar>* m_A =
nullptr;
void setRelaxation(const Scalar relaxation)
Similar to step size can be used to avoid overshooting the solution.
void setMaxIterations(const unsigned int maxIterations)
Set the maximum number of iterations.
void setEpsilon(const Scalar epsilon)
Stops when energy=(x_i+1-x_i).norm() < epsilon, when the solution isn't changing anymore.
const double getEnergy() const
Energy is defined as energy=(x_i+1-x_i).norm()