32 typedef std::shared_ptr<ConjugateGradientParameters>
shared_ptr;
54 size_t reset,
double epsilon_rel,
56 : minIterations(minIterations),
57 maxIterations(maxIterations),
59 epsilon_rel(epsilon_rel),
60 epsilon_abs(epsilon_abs),
65 minIterations(
p.minIterations),
66 maxIterations(
p.maxIterations),
68 epsilon_rel(
p.epsilon_rel),
69 epsilon_abs(
p.epsilon_abs),
72 #ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
73 inline size_t getMinIterations()
const {
return minIterations; }
74 inline size_t getMaxIterations()
const {
return maxIterations; }
75 inline size_t getReset()
const {
return reset; }
76 inline double getEpsilon()
const {
return epsilon_rel; }
77 inline double getEpsilon_rel()
const {
return epsilon_rel; }
78 inline double getEpsilon_abs()
const {
return epsilon_abs; }
80 inline void setMinIterations(
size_t value) { minIterations =
value; }
82 inline void setReset(
size_t value) { reset =
value; }
83 inline void setEpsilon(
double value) { epsilon_rel =
value; }
84 inline void setEpsilon_rel(
double value) { epsilon_rel =
value; }
85 inline void setEpsilon_abs(
double value) { epsilon_abs =
value; }
90 void print(std::ostream &
os)
const override;
92 static std::string blasTranslator(
const BLASKernel k) ;
93 static BLASKernel blasTranslator(
const std::string &
s) ;
106 template<
class S,
class V>
110 V estimate, residual, direction, q1, q2;
111 estimate = residual = direction = q1 = q2 =
initial;
113 system.residual(estimate, q1);
114 system.leftPrecondition(q1, residual);
115 system.rightPrecondition(residual, direction);
117 double currentGamma = system.dot(residual, residual), prevGamma,
alpha,
beta;
122 const double threshold =
130 <<
", ||r0||^2 = " << currentGamma
131 <<
", threshold = " << threshold << std::endl;
134 for ( k = 1 ; k <= iMaxIterations && (currentGamma > threshold || k <= iMinIterations) ; k++ ) {
136 if ( k % iReset == 0 ) {
137 system.residual(estimate, q1);
138 system.leftPrecondition(q1, residual);
139 system.rightPrecondition(residual, direction);
140 currentGamma = system.dot(residual, residual);
142 system.multiply(direction, q1);
143 alpha = currentGamma / system.dot(direction, q1);
144 system.axpy(
alpha, direction, estimate);
145 system.leftPrecondition(q1, q2);
146 system.axpy(-
alpha, q2, residual);
147 prevGamma = currentGamma;
148 currentGamma = system.dot(residual, residual);
149 beta = currentGamma / prevGamma;
150 system.rightPrecondition(residual, q1);
151 system.scal(
beta, direction);
152 system.axpy(1.0, q1, direction);
155 std::cout <<
"[PCG] k = " << k
156 <<
", alpha = " <<
alpha
157 <<
", beta = " <<
beta
158 <<
", ||r||^2 = " << currentGamma
164 std::cout <<
"[PCG] iterations = " << k
165 <<
", ||r||^2 = " << currentGamma