Ceres solver2
数值微分和解析微分
数值微分 在某些情况下,很难定义一个模板类cost functor
,这种情况下可以用数值微分。例如 f(x)=10?x
struct CostFunctor {
bool operator()(const double* const x, double* residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
CostFunction* cost_function =
new NumericDiffCostFunction(
new NumericDiffCostFunctor)
problem.AddResidualBlock(cost_function, NULL, &x);
通常更推荐使用
automatic differentiation
,因为它更高效,而数值微分存在数值误差,可能导致收敛速度变慢。解析微分 在某些情况下,使用自动微分是不太可能的。自动微分依赖于链式求导法则,使用解析微分可以更高效。这种情况下,自己定义函数计算残差和雅可比矩阵更加合适。
同样以 f(x)=10?x 为例
class QuadraticCostFunction : public ceres::SizedCostFunction<1, 1> {
public:
virtual ~QuadraticCostFunction() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
const double x = parameters[0][0];
residuals[0] = 10 - x;
// Compute the Jacobian if asked for.
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = -1;
}
return true;
}
};
More About Derivatives 【Ceres solver2】除了以上方法外,ceres solver还有其他函数,参考
DynamicAutoDiffCostFunction
, CostFunctionToFunctor
, NumericDiffFunctor
和 ConditionedCostFunction
。推荐阅读
- 回归|金融数值方法之回归计算-python实践
- LeetCode|LeetCode 每日一题 [52] 表示数值的字符串
- C++-类型转换
- 【机器学习】数值分析(1)——|【机器学习】数值分析(1)—— 任意方程求根
- 基于云上 Arm 架构赋能数值天气预报
- JZ-053-表示数值的字符串
- 浮点值的上溢和下溢(其他数值类型相似)
- LeetCode 363. 矩形区域不超过 K 的最大数值和(DP+set二分查找)
- es6-数值的扩展
- 有限体积法(4)——一维扩散方程数值求解(第二类边界条件)