DiSMEC++
cg.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_CG_H
7 #define DISMEC_CG_H
8 
9 #include "matrix_types.h"
10 #include "config.h"
11 #include <functional>
12 #include "utils/hyperparams.h"
13 
14 namespace dismec::solvers {
15 
24  public:
27  using MatrixVectorProductFn = std::function<void(const DenseRealVector &, Eigen::Ref<DenseRealVector>)>;
28 
29  explicit CGMinimizer(long num_vars);
30 
32  long minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M);
33 
35  const DenseRealVector& get_solution() const { return m_S; }
36 
38  [[nodiscard]] double get_epsilon() const { return m_Epsilon; }
39 
41  void set_epsilon(double v) { m_Epsilon = v; }
42 
43  private:
44  long do_minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M);
45 
46  long m_Size;
48 
49  // vector caches to prevent allocations
54  };
55 }
56 
57 #endif //DISMEC_CG_H
Base class for all objects that have adjustable hyper-parameters.
Definition: hyperparams.h:83
Approximately solve a linear equation Ax + b = 0.
Definition: cg.h:23
DenseRealVector m_A_times_d
Definition: cg.h:50
std::function< void(const DenseRealVector &, Eigen::Ref< DenseRealVector >)> MatrixVectorProductFn
Definition: cg.h:27
DenseRealVector m_Residual
r_k from the CG algorithm
Definition: cg.h:52
const DenseRealVector & get_solution() const
returns the solution vector found by the last minimize call
Definition: cg.h:35
CGMinimizer(long num_vars)
Definition: cg.cpp:12
long minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M)
Solves Ax+b=0. returns the number of iterations.
Definition: cg.cpp:21
void set_epsilon(double v)
Sets the value of the tolerance hyperparameter.
Definition: cg.h:41
DenseRealVector m_S
s from the CG algorithm
Definition: cg.h:51
double get_epsilon() const
Gets the value of the tolerance hyperparameter.
Definition: cg.h:38
DenseRealVector m_Conjugate
p_k from the CG algorithm
Definition: cg.h:53
long do_minimize(const MatrixVectorProductFn &A, const DenseRealVector &b, const DenseRealVector &M)
Definition: cg.cpp:26
Defines configuration variables.
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40
constexpr const real_t CG_DEFAULT_EPSILON
The default value for the tolerance parameter of the conjugate gradient optimization.
Definition: config.h:25
float real_t
The default type for floating point values.
Definition: config.h:17