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.17.0
target x86_64-freebsd13.3
tgt config freebsd_13-clang_17
timestamp 2025-09-07 02:21:25 UTC (22:24 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_17_libc++
timestamp 2025-09-07 02:15:07 UTC (28:42 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_17
timestamp 2025-09-07 02:09:43 UTC (34:06 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-static_O3
timestamp 2025-09-06 23:40:36 UTC (03:03:14 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-O3
timestamp 2025-09-06 23:35:47 UTC (03:08:03 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18
timestamp 2025-09-06 23:28:52 UTC (03:14:58 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_17_libc++
timestamp 2025-09-06 13:16:24 UTC (13:27:26 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_17
timestamp 2025-09-06 13:13:52 UTC (13:29:58 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-static_O3
timestamp 2025-09-06 13:12:27 UTC (13:31:23 hours ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-O3
timestamp 2025-09-06 13:11:37 UTC (13:32:12 hours ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++
timestamp 2025-09-06 13:11:12 UTC (13:32:38 hours ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-static_O3
timestamp 2025-09-05 12:39:46 UTC (01 14:04:03 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-O3
timestamp 2025-09-05 12:37:59 UTC (01 14:05:50 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-ndebug_O3
timestamp 2025-09-05 12:37:20 UTC (01 14:06:30 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14
timestamp 2025-09-05 12:35:27 UTC (01 14:08:22 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-O3
timestamp 2025-09-04 13:17:20 UTC (02 13:26:29 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-static_O3
timestamp 2025-09-04 13:15:37 UTC (02 13:28:13 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-ndebug_O3
timestamp 2025-09-04 13:13:56 UTC (02 13:29:54 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14
timestamp 2025-09-04 13:13:33 UTC (02 13:30:16 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10-O2
timestamp 2025-09-04 13:12:11 UTC (02 13:31:38 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10-static_O2
timestamp 2025-09-04 13:12:06 UTC (02 13:31:43 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10
timestamp 2025-09-04 13:11:16 UTC (02 13:32:34 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_12-bindist
timestamp 2025-09-04 13:03:21 UTC (02 13:40:29 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_ubuntu_24.04-gcc_13-bindist
timestamp 2025-09-04 12:58:26 UTC (02 13:45:23 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64-static_O2
timestamp 2025-09-04 12:55:00 UTC (02 13:48:50 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64
timestamp 2025-09-04 12:54:46 UTC (02 13:49:04 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64-O2
timestamp 2025-09-04 12:54:15 UTC (02 13:49:35 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_17_msvc_msvc_17.10
timestamp 2025-09-04 12:51:57 UTC (02 13:51:53 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18-static_O3
timestamp 2025-09-04 12:43:07 UTC (02 14:00:42 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18-O3
timestamp 2025-09-04 12:42:34 UTC (02 14:01:15 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18
timestamp 2025-09-04 12:41:28 UTC (02 14:02:22 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_13.1
timestamp 2025-09-04 12:29:27 UTC (02 14:14:23 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_13
timestamp 2025-09-04 10:52:27 UTC (02 15:51:22 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_fedora_40-gcc_14-bindist
timestamp 2025-09-04 06:53:34 UTC (02 19:50:15 days ago)
result warning (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10-O2
timestamp 2025-09-04 05:11:01 UTC (02 21:32:48 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10-static_O2
timestamp 2025-09-04 05:09:47 UTC (02 21:34:02 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10
timestamp 2025-09-04 05:06:05 UTC (02 21:37:45 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8-O2
timestamp 2025-09-04 02:54:10 UTC (02 23:49:39 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8-static_O2
timestamp 2025-09-04 02:50:48 UTC (02 23:53:01 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8
timestamp 2025-09-04 02:48:11 UTC (02 23:55:38 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18-O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_fedora_39-gcc_13-bindist
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin22.5.0
tgt config macos_13-clang_15.0
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-static_O3
result unbuilt