least-squares-cpp/0.3.0+1

[brief]

A single header-only library for least squares optimization.

Cpp14 License CMake cppget

least-squares-cpp is a header-only C++ library for unconstrained non-linear least squares optimization using the Eigen3 library.

It provides convenient and configurable access to the following fitting algorithms:

Convergence and runtime behaviour can be parametrized using advanced newton step refinement operators such as line searching or trust region methods.

The following animation shows a pointcloud registration using Gauss Newton and Armijo Backtracking in five iterations without initial guess.

pointcloud_registration_armijo

Install

Simply copy the single header file into your project and add it to your include path, then you are good to go.

least-squares-cpp now also supports the build2 build system. You can simply add least-squares-cpp into the dependency list of your manifest file:

depends: least-squares-cpp ^0.3.0

You can then simply import least-squares-cpp in your build files.

import libs = least-squares-cpp%lib{least-squares-cpp}

If CMake is your preferred tooling you can install the library with the following command:

cd path/to/repo
cmake -B out/ -S .
cmake --install out/

You can then simply use the library in your CMake projects by finding the package:

find_package(lsqcpp REQUIRED)
add_executable(myproject main.cpp)
target_link_libraries(myproject lsqcpp::lsqcpp)

The library requires Eigen3 to be installed on your system. In Debian based systems you can simply type

apt-get install libeigen3-dev

Make sure Eigen3 can be found by your build system.

Usage

For examples on using different algorithms and stop criteria, please have a look at the examples/ directory.

There are three major steps to use least-squares-cpp:

#include <lsqcpp/lsqcpp.hpp>

// Implement an objective functor.
struct ParabolicError
{
    static constexpr bool ComputesJacobian = false;

    template<typename Scalar, int Inputs, int Outputs>
    void operator()(const Eigen::Matrix<Scalar, Inputs, 1> &xval,
                    Eigen::Matrix<Scalar, Outputs, 1> &fval) const
    {
        // omit calculation of jacobian, so finite differences will be used
        // to estimate jacobian numerically
        fval.resize(xval.size() / 2);
        for(lsqcpp::Index i = 0; i < fval.size(); ++i)
            fval(i) = xval(i*2) * xval(i*2) + xval(i*2+1) * xval(i*2+1);
    }
};

int main()
{
    // Create GaussNewton optimizer object with ParabolicError functor as objective.
    // There are GradientDescent, GaussNewton and LevenbergMarquardt available.
    //
    // You can specify a StepSize functor as template parameter.
    // There are ConstantStepSize, BarzilaiBorwein, ArmijoBacktracking
    // WolfeBacktracking available. (Default for GaussNewton is ArmijoBacktracking)
    //
    // You can additionally specify a Callback functor as template parameter.
    //
    // You can additionally specify a FiniteDifferences functor as template
    // parameter. There are Forward-, Backward- and CentralDifferences
    // available. (Default is CentralDifferences)
    //
    // For GaussNewton and LevenbergMarquardt you can additionally specify a
    // linear equation system solver.
    // There are DenseSVDSolver and DenseCholeskySolver available.
    lsqcpp::GaussNewtonX<double, ParabolicError, lsqcpp::ArmijoBacktracking> optimizer;

    // Set number of iterations as stop criterion.
    // Set it to 0 or negative for infinite iterations (default is 0).
    optimizer.setMaximumIterations(100);

    // Set the minimum length of the gradient.
    // The optimizer stops minimizing if the gradient length falls below this
    // value.
    // Set it to 0 or negative to disable this stop criterion (default is 1e-9).
    optimizer.setMinimumGradientLength(1e-6);

    // Set the minimum length of the step.
    // The optimizer stops minimizing if the step length falls below this
    // value.
    // Set it to 0 or negative to disable this stop criterion (default is 1e-9).
    optimizer.setMinimumStepLength(1e-6);

    // Set the minimum least squares error.
    // The optimizer stops minimizing if the error falls below this
    // value.
    // Set it to 0 or negative to disable this stop criterion (default is 0).
    optimizer.setMinimumError(0);

    // Set the parameters of the step refiner (Armijo Backtracking).
    optimizer.setRefinementParameters({0.8, 1e-4, 1e-10, 1.0, 0});

    // Turn verbosity on, so the optimizer prints status updates after each
    // iteration.
    optimizer.setVerbosity(4);

    // Set initial guess.
    Eigen::VectorXd initialGuess(4);
    initialGuess << 1, 2, 3, 4;

    // Start the optimization.
    auto result = optimizer.minimize(initialGuess);

    std::cout << "Done! Converged: " << (result.converged ? "true" : "false")
        << " Iterations: " << result.iterations << std::endl;

    // do something with final function value
    std::cout << "Final fval: " << result.fval.transpose() << std::endl;

    // do something with final x-value
    std::cout << "Final xval: " << result.xval.transpose() << std::endl;

    return 0;
}

Performance Considerations

The performance for solving an optimization problem with least-squares-cpp can be significantly influenced by the following factors:

References

[1] Jorge Nocedal, Stephen J. Wright, Numerical Optimization, Springer 1999

version 0.3.0+1
license MIT
repository https://pkg.cppget.org/1/alpha
download least-squares-cpp-0.3.0+1.tar.gz
sha256 d806366a5624346da638bcadc9549ca3c677313476a9af36ea3882cf76942471
project least-squares-cpp
url github.com/Rookfighter/least-squares-cpp

Depends (2)

Eigen ^3.4.0
catch2 ^2.13.6

Requires (1)

c++ >= 14

Reviews

fail 0
pass 1

Builds

toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-msvc_18-static_O2
timestamp 2026-11-05 12:16:36 UTC (01 09:41:20 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-msvc_18-O2
timestamp 2026-11-05 12:16:13 UTC (01 09:41:43 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-msvc_18
timestamp 2026-11-05 12:16:08 UTC (01 09:41:49 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-clang_21_msvc_18-O2
timestamp 2026-11-05 11:42:29 UTC (01 10:15:27 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-clang_21_msvc_18-static_O2
timestamp 2026-11-05 11:41:35 UTC (01 10:16:21 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.5
tgt config windows_10-clang_21_msvc_18
timestamp 2026-11-05 11:40:40 UTC (01 10:17:17 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_fedora_43-gcc_15-bindist
timestamp 2026-11-05 11:06:44 UTC (01 10:51:13 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17
timestamp 2026-11-04 10:41:33 UTC (02 11:16:23 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_ubuntu_26.04-gcc_15-bindist
timestamp 2026-11-02 12:42:55 UTC (04 09:15:02 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-w64-mingw32
tgt config windows_10-gcc_15.2_mingw_w64-static_O2
timestamp 2026-11-02 11:54:24 UTC (04 10:03:32 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-w64-mingw32
tgt config windows_10-gcc_15.2_mingw_w64-O2
timestamp 2026-11-02 11:53:27 UTC (04 10:04:29 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-w64-mingw32
tgt config windows_10-gcc_15.2_mingw_w64
timestamp 2026-11-02 11:53:26 UTC (04 10:04:30 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_fedora_44-gcc_16-bindist
timestamp 2026-10-29 12:10:26 UTC (08 09:47:31 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-static_O3
timestamp 2026-10-25 04:09:09 UTC (12 17:48:47 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-O3
timestamp 2026-10-25 04:07:33 UTC (12 17:50:23 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0
timestamp 2026-10-25 04:05:58 UTC (12 17:51:59 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin22.5.0
tgt config macos_13-clang_15.0
timestamp 2026-10-25 00:58:27 UTC (12 20:59:29 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22-static_O3
timestamp 2026-10-25 00:57:51 UTC (12 21:00:06 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22-O3
timestamp 2026-10-25 00:56:27 UTC (12 21:01:29 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22
timestamp 2026-10-25 00:53:30 UTC (12 21:04:26 days ago)
result success | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-static_O3
timestamp 2026-10-25 00:33:11 UTC (12 21:24:45 days ago)
result success | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-O3
timestamp 2026-10-25 00:31:54 UTC (12 21:26:02 days ago)
result success | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18
timestamp 2026-10-25 00:31:49 UTC (12 21:26:07 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_21
timestamp 2026-10-24 13:37:52 UTC (13 08:20:04 days ago)
result success | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-static_O3
timestamp 2026-10-24 12:51:08 UTC (13 09:06:48 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-O3
timestamp 2026-10-24 12:50:13 UTC (13 09:07:43 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew
timestamp 2026-10-24 12:49:11 UTC (13 09:08:45 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_16-static_O3
timestamp 2026-10-24 12:03:08 UTC (13 09:54:48 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_16-ndebug_O3
timestamp 2026-10-24 12:02:52 UTC (13 09:55:04 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_16-O3
timestamp 2026-10-24 12:01:20 UTC (13 09:56:37 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_16
timestamp 2026-10-24 12:00:42 UTC (13 09:57:14 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-static_O3
timestamp 2026-10-24 11:58:17 UTC (13 09:59:39 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-O3
timestamp 2026-10-24 11:56:57 UTC (13 10:00:59 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++
timestamp 2026-10-24 11:55:22 UTC (13 10:02:34 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-static_O3
timestamp 2026-10-24 11:36:00 UTC (13 10:21:56 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-ndebug_O3
timestamp 2026-10-24 11:34:50 UTC (13 10:23:06 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14
timestamp 2026-10-24 11:34:10 UTC (13 10:23:46 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-O3
timestamp 2026-10-24 11:34:10 UTC (13 10:23:46 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_14-bindist
timestamp 2026-10-24 11:32:08 UTC (13 10:25:48 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_21_libc++
timestamp 2026-10-24 11:29:28 UTC (13 10:28:28 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_ubuntu_24.04-gcc_13-bindist
timestamp 2026-10-24 11:27:13 UTC (13 10:30:43 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-freebsd15.0
tgt config freebsd_15-clang_19-static_O3
timestamp 2026-10-24 11:24:41 UTC (13 10:33:15 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-freebsd15.0
tgt config freebsd_15-clang_19-O3
timestamp 2026-10-24 11:24:13 UTC (13 10:33:43 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_15-O3
timestamp 2026-10-24 11:24:02 UTC (13 10:33:54 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-freebsd15.0
tgt config freebsd_15-clang_19
timestamp 2026-10-24 11:23:53 UTC (13 10:34:03 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-gcc_15
timestamp 2026-10-24 11:22:59 UTC (13 10:34:58 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18
timestamp 2026-10-24 11:13:55 UTC (13 10:44:02 days ago)
result warning (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22_libc++-static_O3
timestamp 2026-10-24 11:00:01 UTC (13 10:57:55 days ago)
result error (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22_libc++-O3
timestamp 2026-10-24 10:59:08 UTC (13 10:58:49 days ago)
result error (update) | log | rebuild
toolchain public-0.18.1
target x86_64-linux-gnu
tgt config linux_debian_13-clang_22_libc++
timestamp 2026-10-24 10:58:55 UTC (13 10:59:01 days ago)
result error (update) | log | rebuild
toolchain public-0.18.1
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_13
timestamp 2026-10-24 10:25:55 UTC (13 11:32:01 days ago)
result warning (update) | log | rebuild