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),
74 #ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
75 inline size_t getMinIterations()
const {
return minIterations; }
76 inline size_t getMaxIterations()
const {
return maxIterations; }
77 inline size_t getReset()
const {
return reset; }
78 inline double getEpsilon()
const {
return epsilon_rel; }
79 inline double getEpsilon_rel()
const {
return epsilon_rel; }
80 inline double getEpsilon_abs()
const {
return epsilon_abs; }
82 inline void setMinIterations(
size_t value) { minIterations =
value; }
84 inline void setReset(
size_t value) { reset =
value; }
85 inline void setEpsilon(
double value) { epsilon_rel =
value; }
86 inline void setEpsilon_rel(
double value) { epsilon_rel =
value; }
87 inline void setEpsilon_abs(
double value) { epsilon_abs =
value; }
92 void print(std::ostream &
os)
const override;
94 static std::string blasTranslator(
const BLASKernel k) ;
95 static BLASKernel blasTranslator(
const std::string &
s) ;
108 template<
class S,
class V>
112 V estimate, residual, direction, q1, q2;
113 estimate = residual = direction = q1 = q2 =
initial;
115 system.residual(estimate, q1);
116 system.leftPrecondition(q1, residual);
117 system.rightPrecondition(residual, direction);
119 double currentGamma = system.dot(residual, residual), prevGamma,
alpha,
beta;
124 const double threshold =
132 <<
", ||r0||^2 = " << currentGamma
133 <<
", threshold = " << threshold << std::endl;
136 for ( k = 1 ; k <= iMaxIterations && (currentGamma > threshold || k <= iMinIterations) ; k++ ) {
138 if ( k % iReset == 0 ) {
139 system.residual(estimate, q1);
140 system.leftPrecondition(q1, residual);
141 system.rightPrecondition(residual, direction);
142 currentGamma = system.dot(residual, residual);
144 system.multiply(direction, q1);
145 alpha = currentGamma / system.dot(direction, q1);
146 system.axpy(
alpha, direction, estimate);
147 system.leftPrecondition(q1, q2);
148 system.axpy(-
alpha, q2, residual);
149 prevGamma = currentGamma;
150 currentGamma = system.dot(residual, residual);
151 beta = currentGamma / prevGamma;
152 system.rightPrecondition(residual, q1);
153 system.scal(
beta, direction);
154 system.axpy(1.0, q1, direction);
157 std::cout <<
"[PCG] k = " << k
158 <<
", alpha = " <<
alpha
159 <<
", beta = " <<
beta
160 <<
", ||r||^2 = " << currentGamma
166 std::cout <<
"[PCG] iterations = " << k
167 <<
", ||r||^2 = " << currentGamma