ceres学习笔记(一):三种求导方式

ceres的基本使用

全部来自于官网ceres官方教材链接
推荐一个博客,感觉写的不错Ceres Solver 官方教程学习笔记

使用ceres时CMakeLists写法

cmake_minimum_required(VERSION 3.0.2)
project(usetools)

find_package(Ceres  REQUIRED)

include_directories(
  ${CERES_INCLUDE_DIRS}
)

add_executable(ceres_learning src/ceres_learning.cpp)
target_link_libraries(ceres_learning ${CERES_LIBRARIES})

ceres三种求导方式基本写法

#include <iostream>
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::NumericDiffCostFunction;
using ceres::Problem;
using ceres::SizedCostFunction;
using ceres::Solve;
using ceres::Solver;
// A templated cost functor that implements the residual r = 10 -
// x. The method operator() is templated so that we can then use an
// automatic differentiation wrapper around it to generate its
// derivatives.

// 自动求导
struct CostFunctor
{
     template <typename T>
     bool operator()(const T* const x, T *residual) const
     {
          residual[0] = T(10.0) - x[0];
          return true;
     }
};
// 数值求导
struct NumericDiffCostFunctor
{
     bool operator()(const double * const x, double *residual) const
     {
          residual[0] = 10.0 - x[0];
          return true;
     }
};
// 解析求导
class QuadraticCostFunction : public ceres::SizedCostFunction<1, 1>
{
public:
     virtual ~QuadraticCostFunction() {}
     virtual bool Evaluate(double const *const *parameters,
                           double *residual,
                           double **jacobians) const
     {
          const double x = parameters[0][0];
          residual[0] = 10 - x;
          if (jacobians != nullptr && jacobians[0] != nullptr)
          {
               jacobians[0][0] = -1;
          }
          return true;
     }
};

int main(int argc, char **argv)
{
     google::InitGoogleLogging(argv[0]);
     // The variable to solve for with its initial value. It will be
     double initial_x = 5.0;
     double x = initial_x;
     Problem problem;
     // CostFunction *cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
     // CostFunction *cost_function = new NumericDiffCostFunction<NumericDiffCostFunctor, ceres::CENTRAL, 1, 1>(new NumericDiffCostFunctor);
     CostFunction *cost_function = new QuadraticCostFunction;
     problem.AddResidualBlock(cost_function, nullptr, &x);
     Solver::Options options;
     options.linear_solver_type = ceres::DENSE_QR;
     options.minimizer_progress_to_stdout = true;
     Solver::Summary summary;
     Solve(options, &problem, &summary);
     std::cout << summary.BriefReport() << '\n';
     std::cout << "x : " << initial_x << " -> " << x << '\n';
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值