7 #include <spdlog/spdlog.h>
12 CGMinimizer::CGMinimizer(
long num_vars) : m_Size(num_vars) {
35 real_t gMinv_norm = std::sqrt(zT_dot_r);
39 for(
long cg_iter = 1; cg_iter <= max_cg_iter; ++cg_iter) {
46 real_t alpha = zT_dot_r / dAd;
53 if (newQ <= 0 && Qdiff <= 0)
55 if (cg_iter * Qdiff >= cg_tol * newQ) {
61 spdlog::warn(
"quadratic approximation > 0 or increasing in {}th CG iteration. Old Q: {}, New Q: {}",
69 real_t beta = znewTrnew / zT_dot_r;
74 spdlog::warn(
"reached maximum number of CG steps ({}). Remaining error is {}", max_cg_iter, Q);
82 const int TEST_SIZE = 5;
84 minimizer.set_epsilon(0.001);
85 types::DenseColMajor<real_t> A = types::DenseColMajor<real_t>::Random(TEST_SIZE, TEST_SIZE);
86 A = (A*A.transpose()).eval();
90 minimizer.minimize([&](
const DenseRealVector& d, Eigen::Ref<DenseRealVector> out){
96 CHECK(sol.norm() == doctest::Approx(0.0));
TEST_CASE("conjugate gradient")
void declare_hyper_parameter(std::string name, U S::*pointer)
Approximately solve a linear equation Ax + b = 0.
DenseRealVector m_A_times_d
std::function< void(const DenseRealVector &, Eigen::Ref< DenseRealVector >)> MatrixVectorProductFn
DenseRealVector m_Residual
r_k from the CG algorithm
long minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M)
Solves Ax+b=0. returns the number of iterations.
void set_epsilon(double v)
Sets the value of the tolerance hyperparameter.
DenseRealVector m_S
s from the CG algorithm
double get_epsilon() const
Gets the value of the tolerance hyperparameter.
DenseRealVector m_Conjugate
p_k from the CG algorithm
long do_minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M)
Main namespace in which all types, classes, and functions are defined.
constexpr const long CG_MIN_ITER_BOUND
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
float real_t
The default type for floating point values.