···11+Ceres Solver - A fast non-linear least squares minimizer
22+Copyright 2015 Google Inc. All rights reserved.
33+http://ceres-solver.org/
44+55+Redistribution and use in source and binary forms, with or without
66+modification, are permitted provided that the following conditions are met:
77+88+* Redistributions of source code must retain the above copyright notice,
99+ this list of conditions and the following disclaimer.
1010+* Redistributions in binary form must reproduce the above copyright notice,
1111+ this list of conditions and the following disclaimer in the documentation
1212+ and/or other materials provided with the distribution.
1313+* Neither the name of Google Inc. nor the names of its contributors may be
1414+ used to endorse or promote products derived from this software without
1515+ specific prior written permission.
1616+1717+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1818+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1919+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2020+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2121+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2222+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2323+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2424+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2525+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2626+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2727+POSSIBILITY OF SUCH DAMAGE.
+11
src/external/tinyceres/README.md
···11+<!--
22+Copyright 2022, Collabora, Ltd.
33+Authors:
44+Moses Turner <moses@collabora.com>
55+SPDX-License-Identifier: CC0-1.0
66+-->
77+88+tinyceres
99+============
1010+1111+tinyceres is a small template library for solving Nonlinear Least Squares problems, created from small subset of [ceres-solver](http://ceres-solver.org/) - mainly TinySolver and the files that TinySover includes. It was created for [Monado](https://monado.freedesktop.org/) for real-time optical hand tracking, and in order to avoid adding a submodule or another system dependency the code was simply copied into Monado's source tree. The source-controlled version can be found [here](https://gitlab.freedesktop.org/monado/utilities/hand-tracking-playground/tinyceres)
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2022 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: jodebo_beck@gmx.de (Johannes Beck)
3131+// sergiu.deitsch@gmail.com (Sergiu Deitsch)
3232+//
3333+// Algorithms to be used together with integer_sequence, like computing the sum
3434+// or the exclusive scan (sometimes called exclusive prefix sum) at compile
3535+// time.
3636+3737+#ifndef CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_
3838+#define CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_
3939+4040+#include <utility>
4141+4242+#include "tinyceres/jet_fwd.hpp"
4343+4444+namespace ceres::internal {
4545+4646+// Implementation of calculating an exclusive scan (exclusive prefix sum) of an
4747+// integer sequence. Exclusive means that the i-th input element is not included
4848+// in the i-th sum. Calculating the exclusive scan for an input array I results
4949+// in the following output R:
5050+//
5151+// R[0] = 0
5252+// R[1] = I[0];
5353+// R[2] = I[0] + I[1];
5454+// R[3] = I[0] + I[1] + I[2];
5555+// ...
5656+//
5757+// In C++17 std::exclusive_scan does the same operation at runtime (but
5858+// cannot be used to calculate the prefix sum at compile time). See
5959+// https://en.cppreference.com/w/cpp/algorithm/exclusive_scan for a more
6060+// detailed description.
6161+//
6262+// Example for integer_sequence<int, 1, 4, 3> (seq := integer_sequence):
6363+// T , Sum, Ns... , Rs...
6464+// ExclusiveScanImpl<int, 0, seq<int, 1, 4, 3>, seq<int >>
6565+// ExclusiveScanImpl<int, 1, seq<int, 4, 3>, seq<int, 0 >>
6666+// ExclusiveScanImpl<int, 5, seq<int, 3>, seq<int, 0, 1 >>
6767+// ExclusiveScanImpl<int, 8, seq<int >, seq<int, 0, 1, 5>>
6868+// ^^^^^^^^^^^^^^^^^
6969+// resulting sequence
7070+template <typename T, T Sum, typename SeqIn, typename SeqOut>
7171+struct ExclusiveScanImpl;
7272+7373+template <typename T, T Sum, T N, T... Ns, T... Rs>
7474+struct ExclusiveScanImpl<T,
7575+ Sum,
7676+ std::integer_sequence<T, N, Ns...>,
7777+ std::integer_sequence<T, Rs...>> {
7878+ using Type =
7979+ typename ExclusiveScanImpl<T,
8080+ Sum + N,
8181+ std::integer_sequence<T, Ns...>,
8282+ std::integer_sequence<T, Rs..., Sum>>::Type;
8383+};
8484+8585+// End of 'recursion'. The resulting type is SeqOut.
8686+template <typename T, T Sum, typename SeqOut>
8787+struct ExclusiveScanImpl<T, Sum, std::integer_sequence<T>, SeqOut> {
8888+ using Type = SeqOut;
8989+};
9090+9191+// Calculates the exclusive scan of the specified integer sequence. The last
9292+// element (the total) is not included in the resulting sequence so they have
9393+// same length. This means the exclusive scan of integer_sequence<int, 1, 2, 3>
9494+// will be integer_sequence<int, 0, 1, 3>.
9595+template <typename Seq>
9696+class ExclusiveScanT {
9797+ using T = typename Seq::value_type;
9898+9999+ public:
100100+ using Type =
101101+ typename ExclusiveScanImpl<T, T(0), Seq, std::integer_sequence<T>>::Type;
102102+};
103103+104104+// Helper to use exclusive scan without typename.
105105+template <typename Seq>
106106+using ExclusiveScan = typename ExclusiveScanT<Seq>::Type;
107107+108108+// Removes all elements from a integer sequence corresponding to specified
109109+// ValueToRemove.
110110+//
111111+// This type should not be used directly but instead RemoveValue.
112112+template <typename T, T ValueToRemove, typename... Sequence>
113113+struct RemoveValueImpl;
114114+115115+// Final filtered sequence
116116+template <typename T, T ValueToRemove, T... Values>
117117+struct RemoveValueImpl<T,
118118+ ValueToRemove,
119119+ std::integer_sequence<T, Values...>,
120120+ std::integer_sequence<T>> {
121121+ using type = std::integer_sequence<T, Values...>;
122122+};
123123+124124+// Found a matching value
125125+template <typename T, T ValueToRemove, T... Head, T... Tail>
126126+struct RemoveValueImpl<T,
127127+ ValueToRemove,
128128+ std::integer_sequence<T, Head...>,
129129+ std::integer_sequence<T, ValueToRemove, Tail...>>
130130+ : RemoveValueImpl<T,
131131+ ValueToRemove,
132132+ std::integer_sequence<T, Head...>,
133133+ std::integer_sequence<T, Tail...>> {};
134134+135135+// Move one element from the tail to the head
136136+template <typename T, T ValueToRemove, T... Head, T MiddleValue, T... Tail>
137137+struct RemoveValueImpl<T,
138138+ ValueToRemove,
139139+ std::integer_sequence<T, Head...>,
140140+ std::integer_sequence<T, MiddleValue, Tail...>>
141141+ : RemoveValueImpl<T,
142142+ ValueToRemove,
143143+ std::integer_sequence<T, Head..., MiddleValue>,
144144+ std::integer_sequence<T, Tail...>> {};
145145+146146+// Start recursion by splitting the integer sequence into two separate ones
147147+template <typename T, T ValueToRemove, T... Tail>
148148+struct RemoveValueImpl<T, ValueToRemove, std::integer_sequence<T, Tail...>>
149149+ : RemoveValueImpl<T,
150150+ ValueToRemove,
151151+ std::integer_sequence<T>,
152152+ std::integer_sequence<T, Tail...>> {};
153153+154154+// RemoveValue takes an integer Sequence of arbitrary type and removes all
155155+// elements matching ValueToRemove.
156156+//
157157+// In contrast to RemoveValueImpl, this implementation deduces the value type
158158+// eliminating the need to specify it explicitly.
159159+//
160160+// As an example, RemoveValue<std::integer_sequence<int, 1, 2, 3>, 4>::type will
161161+// not transform the type of the original sequence. However,
162162+// RemoveValue<std::integer_sequence<int, 0, 0, 2>, 2>::type will generate a new
163163+// sequence of type std::integer_sequence<int, 0, 0> by removing the value 2.
164164+template <typename Sequence, typename Sequence::value_type ValueToRemove>
165165+struct RemoveValue
166166+ : RemoveValueImpl<typename Sequence::value_type, ValueToRemove, Sequence> {
167167+};
168168+169169+// Convenience template alias for RemoveValue.
170170+template <typename Sequence, typename Sequence::value_type ValueToRemove>
171171+using RemoveValue_t = typename RemoveValue<Sequence, ValueToRemove>::type;
172172+173173+// Returns true if all elements of Values are equal to HeadValue.
174174+//
175175+// Returns true if Values is empty.
176176+template <typename T, T HeadValue, T... Values>
177177+inline constexpr bool AreAllEqual_v = ((HeadValue == Values) && ...);
178178+179179+// Predicate determining whether an integer sequence is either empty or all
180180+// values are equal.
181181+template <typename Sequence>
182182+struct IsEmptyOrAreAllEqual;
183183+184184+// Empty case.
185185+template <typename T>
186186+struct IsEmptyOrAreAllEqual<std::integer_sequence<T>> : std::true_type {};
187187+188188+// General case for sequences containing at least one value.
189189+template <typename T, T HeadValue, T... Values>
190190+struct IsEmptyOrAreAllEqual<std::integer_sequence<T, HeadValue, Values...>>
191191+ : std::integral_constant<bool, AreAllEqual_v<T, HeadValue, Values...>> {};
192192+193193+// Convenience variable template for IsEmptyOrAreAllEqual.
194194+template <class Sequence>
195195+inline constexpr bool IsEmptyOrAreAllEqual_v =
196196+ IsEmptyOrAreAllEqual<Sequence>::value;
197197+198198+} // namespace ceres::internal
199199+200200+#endif // CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2022 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: sergiu.deitsch@gmail.com (Sergiu Deitsch)
3131+//
3232+3333+#ifndef CERES_PUBLIC_INTERNAL_JET_TRAITS_H_
3434+#define CERES_PUBLIC_INTERNAL_JET_TRAITS_H_
3535+3636+#include <tuple>
3737+#include <type_traits>
3838+#include <utility>
3939+4040+#include "tinyceres/internal/integer_sequence_algorithm.hpp"
4141+#include "tinyceres/jet_fwd.hpp"
4242+4343+namespace ceres {
4444+namespace internal {
4545+4646+// Predicate that determines whether any of the Types is a Jet.
4747+template <typename... Types>
4848+struct AreAnyJet : std::false_type {};
4949+5050+template <typename T, typename... Types>
5151+struct AreAnyJet<T, Types...> : AreAnyJet<Types...> {};
5252+5353+template <typename T, int N, typename... Types>
5454+struct AreAnyJet<Jet<T, N>, Types...> : std::true_type {};
5555+5656+// Convenience variable template for AreAnyJet.
5757+template <typename... Types>
5858+inline constexpr bool AreAnyJet_v = AreAnyJet<Types...>::value;
5959+6060+// Extracts the underlying floating-point from a type T.
6161+template <typename T, typename E = void>
6262+struct UnderlyingScalar {
6363+ using type = T;
6464+};
6565+6666+template <typename T, int N>
6767+struct UnderlyingScalar<Jet<T, N>> : UnderlyingScalar<T> {};
6868+6969+// Convenience template alias for UnderlyingScalar type trait.
7070+template <typename T>
7171+using UnderlyingScalar_t = typename UnderlyingScalar<T>::type;
7272+7373+// Predicate determining whether all Types in the pack are the same.
7474+//
7575+// Specifically, the predicate applies std::is_same recursively to pairs of
7676+// Types in the pack.
7777+template <typename T1, typename... Types>
7878+inline constexpr bool AreAllSame_v = (std::is_same<T1, Types>::value && ...);
7979+8080+// Determines the rank of a type. This allows to ensure that types passed as
8181+// arguments are compatible to each other. The rank of Jet is determined by the
8282+// dimensions of the dual part. The rank of a scalar is always 0.
8383+// Non-specialized types default to a rank of -1.
8484+template <typename T, typename E = void>
8585+struct Rank : std::integral_constant<int, -1> {};
8686+8787+// The rank of a scalar is 0.
8888+template <typename T>
8989+struct Rank<T, std::enable_if_t<std::is_scalar<T>::value>>
9090+ : std::integral_constant<int, 0> {};
9191+9292+// The rank of a Jet is given by its dimensionality.
9393+template <typename T, int N>
9494+struct Rank<Jet<T, N>> : std::integral_constant<int, N> {};
9595+9696+// Convenience variable template for Rank.
9797+template <typename T>
9898+inline constexpr int Rank_v = Rank<T>::value;
9999+100100+// Constructs an integer sequence of ranks for each of the Types in the pack.
101101+template <typename... Types>
102102+using Ranks_t = std::integer_sequence<int, Rank_v<Types>...>;
103103+104104+// Returns the scalar part of a type. This overload acts as an identity.
105105+template <typename T>
106106+constexpr decltype(auto) AsScalar(T&& value) noexcept {
107107+ return std::forward<T>(value);
108108+}
109109+110110+// Recursively unwraps the scalar part of a Jet until a non-Jet scalar type is
111111+// encountered.
112112+template <typename T, int N>
113113+constexpr decltype(auto) AsScalar(const Jet<T, N>& value) noexcept(
114114+ noexcept(AsScalar(value.a))) {
115115+ return AsScalar(value.a);
116116+}
117117+118118+} // namespace internal
119119+120120+// Type trait ensuring at least one of the types is a Jet,
121121+// the underlying scalar types are the same and Jet dimensions match.
122122+//
123123+// The type trait can be further specialized if necessary.
124124+//
125125+// This trait is a candidate for a concept definition once C++20 features can
126126+// be used.
127127+template <typename... Types>
128128+// clang-format off
129129+struct CompatibleJetOperands : std::integral_constant
130130+<
131131+ bool,
132132+ // At least one of the types is a Jet
133133+ internal::AreAnyJet_v<Types...> &&
134134+ // The underlying floating-point types are exactly the same
135135+ internal::AreAllSame_v<internal::UnderlyingScalar_t<Types>...> &&
136136+ // Non-zero ranks of types are equal
137137+ internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
138138+>
139139+// clang-format on
140140+{};
141141+142142+// Single Jet operand is always compatible.
143143+template <typename T, int N>
144144+struct CompatibleJetOperands<Jet<T, N>> : std::true_type {};
145145+146146+// Single non-Jet operand is always incompatible.
147147+template <typename T>
148148+struct CompatibleJetOperands<T> : std::false_type {};
149149+150150+// Empty operands are always incompatible.
151151+template <>
152152+struct CompatibleJetOperands<> : std::false_type {};
153153+154154+// Convenience variable template ensuring at least one of the types is a Jet,
155155+// the underlying scalar types are the same and Jet dimensions match.
156156+//
157157+// This trait is a candidate for a concept definition once C++20 features can
158158+// be used.
159159+template <typename... Types>
160160+inline constexpr bool CompatibleJetOperands_v =
161161+ CompatibleJetOperands<Types...>::value;
162162+163163+// Type trait ensuring at least one of the types is a Jet,
164164+// the underlying scalar types are compatible among each other and Jet
165165+// dimensions match.
166166+//
167167+// The type trait can be further specialized if necessary.
168168+//
169169+// This trait is a candidate for a concept definition once C++20 features can
170170+// be used.
171171+template <typename... Types>
172172+// clang-format off
173173+struct PromotableJetOperands : std::integral_constant
174174+<
175175+ bool,
176176+ // Types can be compatible among each other
177177+ internal::AreAnyJet_v<Types...> &&
178178+ // Non-zero ranks of types are equal
179179+ internal::IsEmptyOrAreAllEqual_v<internal::RemoveValue_t<internal::Ranks_t<Types...>, 0>>
180180+>
181181+// clang-format on
182182+{};
183183+184184+// Convenience variable template ensuring at least one of the types is a Jet,
185185+// the underlying scalar types are compatible among each other and Jet
186186+// dimensions match.
187187+//
188188+// This trait is a candidate for a concept definition once C++20 features can
189189+// be used.
190190+template <typename... Types>
191191+inline constexpr bool PromotableJetOperands_v =
192192+ PromotableJetOperands<Types...>::value;
193193+194194+} // namespace ceres
195195+196196+#endif // CERES_PUBLIC_INTERNAL_JET_TRAITS_H_
+1343
src/external/tinyceres/include/tinyceres/jet.hpp
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2022 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: keir@google.com (Keir Mierle)
3131+//
3232+// A simple implementation of N-dimensional dual numbers, for automatically
3333+// computing exact derivatives of functions.
3434+//
3535+// While a complete treatment of the mechanics of automatic differentiation is
3636+// beyond the scope of this header (see
3737+// http://en.wikipedia.org/wiki/Automatic_differentiation for details), the
3838+// basic idea is to extend normal arithmetic with an extra element, "e," often
3939+// denoted with the greek symbol epsilon, such that e != 0 but e^2 = 0. Dual
4040+// numbers are extensions of the real numbers analogous to complex numbers:
4141+// whereas complex numbers augment the reals by introducing an imaginary unit i
4242+// such that i^2 = -1, dual numbers introduce an "infinitesimal" unit e such
4343+// that e^2 = 0. Dual numbers have two components: the "real" component and the
4444+// "infinitesimal" component, generally written as x + y*e. Surprisingly, this
4545+// leads to a convenient method for computing exact derivatives without needing
4646+// to manipulate complicated symbolic expressions.
4747+//
4848+// For example, consider the function
4949+//
5050+// f(x) = x^2 ,
5151+//
5252+// evaluated at 10. Using normal arithmetic, f(10) = 100, and df/dx(10) = 20.
5353+// Next, argument 10 with an infinitesimal to get:
5454+//
5555+// f(10 + e) = (10 + e)^2
5656+// = 100 + 2 * 10 * e + e^2
5757+// = 100 + 20 * e -+-
5858+// -- |
5959+// | +--- This is zero, since e^2 = 0
6060+// |
6161+// +----------------- This is df/dx!
6262+//
6363+// Note that the derivative of f with respect to x is simply the infinitesimal
6464+// component of the value of f(x + e). So, in order to take the derivative of
6565+// any function, it is only necessary to replace the numeric "object" used in
6666+// the function with one extended with infinitesimals. The class Jet, defined in
6767+// this header, is one such example of this, where substitution is done with
6868+// templates.
6969+//
7070+// To handle derivatives of functions taking multiple arguments, different
7171+// infinitesimals are used, one for each variable to take the derivative of. For
7272+// example, consider a scalar function of two scalar parameters x and y:
7373+//
7474+// f(x, y) = x^2 + x * y
7575+//
7676+// Following the technique above, to compute the derivatives df/dx and df/dy for
7777+// f(1, 3) involves doing two evaluations of f, the first time replacing x with
7878+// x + e, the second time replacing y with y + e.
7979+//
8080+// For df/dx:
8181+//
8282+// f(1 + e, y) = (1 + e)^2 + (1 + e) * 3
8383+// = 1 + 2 * e + 3 + 3 * e
8484+// = 4 + 5 * e
8585+//
8686+// --> df/dx = 5
8787+//
8888+// For df/dy:
8989+//
9090+// f(1, 3 + e) = 1^2 + 1 * (3 + e)
9191+// = 1 + 3 + e
9292+// = 4 + e
9393+//
9494+// --> df/dy = 1
9595+//
9696+// To take the gradient of f with the implementation of dual numbers ("jets") in
9797+// this file, it is necessary to create a single jet type which has components
9898+// for the derivative in x and y, and passing them to a templated version of f:
9999+//
100100+// template<typename T>
101101+// T f(const T &x, const T &y) {
102102+// return x * x + x * y;
103103+// }
104104+//
105105+// // The "2" means there should be 2 dual number components.
106106+// // It computes the partial derivative at x=10, y=20.
107107+// Jet<double, 2> x(10, 0); // Pick the 0th dual number for x.
108108+// Jet<double, 2> y(20, 1); // Pick the 1st dual number for y.
109109+// Jet<double, 2> z = f(x, y);
110110+//
111111+// LOG(INFO) << "df/dx = " << z.v[0]
112112+// << "df/dy = " << z.v[1];
113113+//
114114+// Most users should not use Jet objects directly; a wrapper around Jet objects,
115115+// which makes computing the derivative, gradient, or jacobian of templated
116116+// functors simple, is in autodiff.h. Even autodiff.h should not be used
117117+// directly; instead autodiff_cost_function.h is typically the file of interest.
118118+//
119119+// For the more mathematically inclined, this file implements first-order
120120+// "jets". A 1st order jet is an element of the ring
121121+//
122122+// T[N] = T[t_1, ..., t_N] / (t_1, ..., t_N)^2
123123+//
124124+// which essentially means that each jet consists of a "scalar" value 'a' from T
125125+// and a 1st order perturbation vector 'v' of length N:
126126+//
127127+// x = a + \sum_i v[i] t_i
128128+//
129129+// A shorthand is to write an element as x = a + u, where u is the perturbation.
130130+// Then, the main point about the arithmetic of jets is that the product of
131131+// perturbations is zero:
132132+//
133133+// (a + u) * (b + v) = ab + av + bu + uv
134134+// = ab + (av + bu) + 0
135135+//
136136+// which is what operator* implements below. Addition is simpler:
137137+//
138138+// (a + u) + (b + v) = (a + b) + (u + v).
139139+//
140140+// The only remaining question is how to evaluate the function of a jet, for
141141+// which we use the chain rule:
142142+//
143143+// f(a + u) = f(a) + f'(a) u
144144+//
145145+// where f'(a) is the (scalar) derivative of f at a.
146146+//
147147+// By pushing these things through sufficiently and suitably templated
148148+// functions, we can do automatic differentiation. Just be sure to turn on
149149+// function inlining and common-subexpression elimination, or it will be very
150150+// slow!
151151+//
152152+// WARNING: Most Ceres users should not directly include this file or know the
153153+// details of how jets work. Instead the suggested method for automatic
154154+// derivatives is to use autodiff_cost_function.h, which is a wrapper around
155155+// both jets.h and autodiff.h to make taking derivatives of cost functions for
156156+// use in Ceres easier.
157157+158158+#pragma once
159159+160160+#include <cmath>
161161+#include <complex>
162162+#include <iosfwd>
163163+#include <iostream> // NOLINT
164164+#include <limits>
165165+#include <numeric>
166166+#include <string>
167167+#include <type_traits>
168168+169169+#include "Eigen/Core"
170170+#include "tinyceres/internal/jet_traits.hpp"
171171+172172+// Taken from port.h
173173+#define CERES_PREVENT_MACRO_SUBSTITUTION // Yes, it's empty
174174+175175+#include "tinyceres/jet_fwd.hpp"
176176+177177+// Here we provide partial specializations of std::common_type for the Jet class
178178+// to allow determining a Jet type with a common underlying arithmetic type.
179179+// Such an arithmetic type can be either a scalar or an another Jet. An example
180180+// for a common type, say, between a float and a Jet<double, N> is a Jet<double,
181181+// N> (i.e., std::common_type_t<float, ceres::Jet<double, N>> and
182182+// ceres::Jet<double, N> refer to the same type.)
183183+//
184184+// The partial specialization are also used for determining compatible types by
185185+// means of SFINAE and thus allow such types to be expressed as operands of
186186+// logical comparison operators. Missing (partial) specialization of
187187+// std::common_type for a particular (custom) type will therefore disable the
188188+// use of comparison operators defined by Ceres.
189189+//
190190+// Since these partial specializations are used as SFINAE constraints, they
191191+// enable standard promotion rules between various scalar types and consequently
192192+// their use in comparison against a Jet without providing implicit
193193+// conversions from a scalar, such as an int, to a Jet (see the implementation
194194+// of logical comparison operators below).
195195+196196+template <typename T, int N, typename U>
197197+struct std::common_type<T, ceres::Jet<U, N>> {
198198+ using type = ceres::Jet<common_type_t<T, U>, N>;
199199+};
200200+201201+template <typename T, int N, typename U>
202202+struct std::common_type<ceres::Jet<T, N>, U> {
203203+ using type = ceres::Jet<common_type_t<T, U>, N>;
204204+};
205205+206206+template <typename T, int N, typename U>
207207+struct std::common_type<ceres::Jet<T, N>, ceres::Jet<U, N>> {
208208+ using type = ceres::Jet<common_type_t<T, U>, N>;
209209+};
210210+211211+namespace ceres {
212212+213213+template <typename T, int N>
214214+struct Jet {
215215+ enum { DIMENSION = N };
216216+ using Scalar = T;
217217+218218+ // Default-construct "a" because otherwise this can lead to false errors about
219219+ // uninitialized uses when other classes relying on default constructed T
220220+ // (where T is a Jet<T, N>). This usually only happens in opt mode. Note that
221221+ // the C++ standard mandates that e.g. default constructed doubles are
222222+ // initialized to 0.0; see sections 8.5 of the C++03 standard.
223223+ Jet() : a() { v.setConstant(Scalar()); }
224224+225225+ // Constructor from scalar: a + 0.
226226+ explicit Jet(const T& value) {
227227+ a = value;
228228+ v.setConstant(Scalar());
229229+ }
230230+231231+ // Constructor from scalar plus variable: a + t_i.
232232+ Jet(const T& value, int k) {
233233+ a = value;
234234+ v.setConstant(Scalar());
235235+ v[k] = T(1.0);
236236+ }
237237+238238+ // Constructor from scalar and vector part
239239+ // The use of Eigen::DenseBase allows Eigen expressions
240240+ // to be passed in without being fully evaluated until
241241+ // they are assigned to v
242242+ template <typename Derived>
243243+ EIGEN_STRONG_INLINE Jet(const T& a, const Eigen::DenseBase<Derived>& v)
244244+ : a(a), v(v) {}
245245+246246+ // Compound operators
247247+ Jet<T, N>& operator+=(const Jet<T, N>& y) {
248248+ *this = *this + y;
249249+ return *this;
250250+ }
251251+252252+ Jet<T, N>& operator-=(const Jet<T, N>& y) {
253253+ *this = *this - y;
254254+ return *this;
255255+ }
256256+257257+ Jet<T, N>& operator*=(const Jet<T, N>& y) {
258258+ *this = *this * y;
259259+ return *this;
260260+ }
261261+262262+ Jet<T, N>& operator/=(const Jet<T, N>& y) {
263263+ *this = *this / y;
264264+ return *this;
265265+ }
266266+267267+ // Compound with scalar operators.
268268+ Jet<T, N>& operator+=(const T& s) {
269269+ *this = *this + s;
270270+ return *this;
271271+ }
272272+273273+ Jet<T, N>& operator-=(const T& s) {
274274+ *this = *this - s;
275275+ return *this;
276276+ }
277277+278278+ Jet<T, N>& operator*=(const T& s) {
279279+ *this = *this * s;
280280+ return *this;
281281+ }
282282+283283+ Jet<T, N>& operator/=(const T& s) {
284284+ *this = *this / s;
285285+ return *this;
286286+ }
287287+288288+ // The scalar part.
289289+ T a;
290290+291291+ // The infinitesimal part.
292292+ Eigen::Matrix<T, N, 1> v;
293293+294294+ // This struct needs to have an Eigen aligned operator new as it contains
295295+ // fixed-size Eigen types.
296296+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW
297297+};
298298+299299+// Unary +
300300+template <typename T, int N>
301301+inline Jet<T, N> const& operator+(const Jet<T, N>& f) {
302302+ return f;
303303+}
304304+305305+// TODO(keir): Try adding __attribute__((always_inline)) to these functions to
306306+// see if it causes a performance increase.
307307+308308+// Unary -
309309+template <typename T, int N>
310310+inline Jet<T, N> operator-(const Jet<T, N>& f) {
311311+ return Jet<T, N>(-f.a, -f.v);
312312+}
313313+314314+// Binary +
315315+template <typename T, int N>
316316+inline Jet<T, N> operator+(const Jet<T, N>& f, const Jet<T, N>& g) {
317317+ return Jet<T, N>(f.a + g.a, f.v + g.v);
318318+}
319319+320320+// Binary + with a scalar: x + s
321321+template <typename T, int N>
322322+inline Jet<T, N> operator+(const Jet<T, N>& f, T s) {
323323+ return Jet<T, N>(f.a + s, f.v);
324324+}
325325+326326+// Binary + with a scalar: s + x
327327+template <typename T, int N>
328328+inline Jet<T, N> operator+(T s, const Jet<T, N>& f) {
329329+ return Jet<T, N>(f.a + s, f.v);
330330+}
331331+332332+// Binary -
333333+template <typename T, int N>
334334+inline Jet<T, N> operator-(const Jet<T, N>& f, const Jet<T, N>& g) {
335335+ return Jet<T, N>(f.a - g.a, f.v - g.v);
336336+}
337337+338338+// Binary - with a scalar: x - s
339339+template <typename T, int N>
340340+inline Jet<T, N> operator-(const Jet<T, N>& f, T s) {
341341+ return Jet<T, N>(f.a - s, f.v);
342342+}
343343+344344+// Binary - with a scalar: s - x
345345+template <typename T, int N>
346346+inline Jet<T, N> operator-(T s, const Jet<T, N>& f) {
347347+ return Jet<T, N>(s - f.a, -f.v);
348348+}
349349+350350+// Binary *
351351+template <typename T, int N>
352352+inline Jet<T, N> operator*(const Jet<T, N>& f, const Jet<T, N>& g) {
353353+ return Jet<T, N>(f.a * g.a, f.a * g.v + f.v * g.a);
354354+}
355355+356356+// Binary * with a scalar: x * s
357357+template <typename T, int N>
358358+inline Jet<T, N> operator*(const Jet<T, N>& f, T s) {
359359+ return Jet<T, N>(f.a * s, f.v * s);
360360+}
361361+362362+// Binary * with a scalar: s * x
363363+template <typename T, int N>
364364+inline Jet<T, N> operator*(T s, const Jet<T, N>& f) {
365365+ return Jet<T, N>(f.a * s, f.v * s);
366366+}
367367+368368+// Binary /
369369+template <typename T, int N>
370370+inline Jet<T, N> operator/(const Jet<T, N>& f, const Jet<T, N>& g) {
371371+ // This uses:
372372+ //
373373+ // a + u (a + u)(b - v) (a + u)(b - v)
374374+ // ----- = -------------- = --------------
375375+ // b + v (b + v)(b - v) b^2
376376+ //
377377+ // which holds because v*v = 0.
378378+ const T g_a_inverse = T(1.0) / g.a;
379379+ const T f_a_by_g_a = f.a * g_a_inverse;
380380+ return Jet<T, N>(f_a_by_g_a, (f.v - f_a_by_g_a * g.v) * g_a_inverse);
381381+}
382382+383383+// Binary / with a scalar: s / x
384384+template <typename T, int N>
385385+inline Jet<T, N> operator/(T s, const Jet<T, N>& g) {
386386+ const T minus_s_g_a_inverse2 = -s / (g.a * g.a);
387387+ return Jet<T, N>(s / g.a, g.v * minus_s_g_a_inverse2);
388388+}
389389+390390+// Binary / with a scalar: x / s
391391+template <typename T, int N>
392392+inline Jet<T, N> operator/(const Jet<T, N>& f, T s) {
393393+ const T s_inverse = T(1.0) / s;
394394+ return Jet<T, N>(f.a * s_inverse, f.v * s_inverse);
395395+}
396396+397397+// Binary comparison operators for both scalars and jets. At least one of the
398398+// operands must be a Jet. Promotable scalars (e.g., int, float, double etc.)
399399+// can appear on either side of the operator. std::common_type_t is used as an
400400+// SFINAE constraint to selectively enable compatible operand types. This allows
401401+// comparison, for instance, against int literals without implicit conversion.
402402+// In case the Jet arithmetic type is a Jet itself, a recursive expansion of Jet
403403+// value is performed.
404404+#define CERES_DEFINE_JET_COMPARISON_OPERATOR(op) \
405405+ template <typename Lhs, \
406406+ typename Rhs, \
407407+ std::enable_if_t<PromotableJetOperands_v<Lhs, Rhs>>* = nullptr> \
408408+ constexpr bool operator op(const Lhs& f, const Rhs& g) noexcept( \
409409+ noexcept(internal::AsScalar(f) op internal::AsScalar(g))) { \
410410+ using internal::AsScalar; \
411411+ return AsScalar(f) op AsScalar(g); \
412412+ }
413413+CERES_DEFINE_JET_COMPARISON_OPERATOR(<) // NOLINT
414414+CERES_DEFINE_JET_COMPARISON_OPERATOR(<=) // NOLINT
415415+CERES_DEFINE_JET_COMPARISON_OPERATOR(>) // NOLINT
416416+CERES_DEFINE_JET_COMPARISON_OPERATOR(>=) // NOLINT
417417+CERES_DEFINE_JET_COMPARISON_OPERATOR(==) // NOLINT
418418+CERES_DEFINE_JET_COMPARISON_OPERATOR(!=) // NOLINT
419419+#undef CERES_DEFINE_JET_COMPARISON_OPERATOR
420420+421421+// Pull some functions from namespace std.
422422+//
423423+// This is necessary because we want to use the same name (e.g. 'sqrt') for
424424+// double-valued and Jet-valued functions, but we are not allowed to put
425425+// Jet-valued functions inside namespace std.
426426+using std::abs;
427427+using std::acos;
428428+using std::asin;
429429+using std::atan;
430430+using std::atan2;
431431+using std::cbrt;
432432+using std::ceil;
433433+using std::copysign;
434434+using std::cos;
435435+using std::cosh;
436436+using std::erf;
437437+using std::erfc;
438438+using std::exp;
439439+using std::exp2;
440440+using std::expm1;
441441+using std::fdim;
442442+using std::floor;
443443+using std::fma;
444444+using std::fmax;
445445+using std::fmin;
446446+using std::fpclassify;
447447+using std::hypot;
448448+using std::isfinite;
449449+using std::isinf;
450450+using std::isnan;
451451+using std::isnormal;
452452+using std::log;
453453+using std::log10;
454454+using std::log1p;
455455+using std::log2;
456456+using std::norm;
457457+using std::pow;
458458+using std::signbit;
459459+using std::sin;
460460+using std::sinh;
461461+using std::sqrt;
462462+using std::tan;
463463+using std::tanh;
464464+465465+// MSVC (up to 1930) defines quiet comparison functions as template functions
466466+// which causes compilation errors due to ambiguity in the template parameter
467467+// type resolution for using declarations in the ceres namespace. Workaround the
468468+// issue by defining specific overload and bypass MSVC standard library
469469+// definitions.
470470+#if defined(_MSC_VER)
471471+inline bool isgreater(double lhs,
472472+ double rhs) noexcept(noexcept(std::isgreater(lhs, rhs))) {
473473+ return std::isgreater(lhs, rhs);
474474+}
475475+inline bool isless(double lhs,
476476+ double rhs) noexcept(noexcept(std::isless(lhs, rhs))) {
477477+ return std::isless(lhs, rhs);
478478+}
479479+inline bool islessequal(double lhs,
480480+ double rhs) noexcept(noexcept(std::islessequal(lhs,
481481+ rhs))) {
482482+ return std::islessequal(lhs, rhs);
483483+}
484484+inline bool isgreaterequal(double lhs, double rhs) noexcept(
485485+ noexcept(std::isgreaterequal(lhs, rhs))) {
486486+ return std::isgreaterequal(lhs, rhs);
487487+}
488488+inline bool islessgreater(double lhs, double rhs) noexcept(
489489+ noexcept(std::islessgreater(lhs, rhs))) {
490490+ return std::islessgreater(lhs, rhs);
491491+}
492492+inline bool isunordered(double lhs,
493493+ double rhs) noexcept(noexcept(std::isunordered(lhs,
494494+ rhs))) {
495495+ return std::isunordered(lhs, rhs);
496496+}
497497+#else
498498+using std::isgreater;
499499+using std::isgreaterequal;
500500+using std::isless;
501501+using std::islessequal;
502502+using std::islessgreater;
503503+using std::isunordered;
504504+#endif
505505+506506+#ifdef CERES_HAS_CPP20
507507+using std::lerp;
508508+using std::midpoint;
509509+#endif // defined(CERES_HAS_CPP20)
510510+511511+512512+// In general, f(a + h) ~= f(a) + f'(a) h, via the chain rule.
513513+514514+// abs(x + h) ~= abs(x) + sgn(x)h
515515+template <typename T, int N>
516516+inline Jet<T, N> abs(const Jet<T, N>& f) {
517517+ return Jet<T, N>(abs(f.a), copysign(T(1), f.a) * f.v);
518518+}
519519+520520+// copysign(a, b) composes a float with the magnitude of a and the sign of b.
521521+// Therefore, the function can be formally defined as
522522+//
523523+// copysign(a, b) = sgn(b)|a|
524524+//
525525+// where
526526+//
527527+// d/dx |x| = sgn(x)
528528+// d/dx sgn(x) = 2δ(x)
529529+//
530530+// sgn(x) being the signum function. Differentiating copysign(a, b) with respect
531531+// to a and b gives:
532532+//
533533+// d/da sgn(b)|a| = sgn(a) sgn(b)
534534+// d/db sgn(b)|a| = 2|a|δ(b)
535535+//
536536+// with the dual representation given by
537537+//
538538+// copysign(a + da, b + db) ~= sgn(b)|a| + (sgn(a)sgn(b) da + 2|a|δ(b) db)
539539+//
540540+// where δ(b) is the Dirac delta function.
541541+template <typename T, int N>
542542+inline Jet<T, N> copysign(const Jet<T, N>& f, const Jet<T, N> g) {
543543+ // The Dirac delta function δ(b) is undefined at b=0 (here it's
544544+ // infinite) and 0 everywhere else.
545545+ T d = fpclassify(g) == FP_ZERO ? std::numeric_limits<T>::infinity() : T(0);
546546+ T sa = copysign(T(1), f.a); // sgn(a)
547547+ T sb = copysign(T(1), g.a); // sgn(b)
548548+ // The second part of the infinitesimal is 2|a|δ(b) which is either infinity
549549+ // or 0 unless a or any of the values of the b infinitesimal are 0. In the
550550+ // latter case, the corresponding values become NaNs (multiplying 0 by
551551+ // infinity gives NaN). We drop the constant factor 2 since it does not change
552552+ // the result (its values will still be either 0, infinity or NaN).
553553+ return Jet<T, N>(copysign(f.a, g.a), sa * sb * f.v + abs(f.a) * d * g.v);
554554+}
555555+556556+// log(a + h) ~= log(a) + h / a
557557+template <typename T, int N>
558558+inline Jet<T, N> log(const Jet<T, N>& f) {
559559+ const T a_inverse = T(1.0) / f.a;
560560+ return Jet<T, N>(log(f.a), f.v * a_inverse);
561561+}
562562+563563+// log10(a + h) ~= log10(a) + h / (a log(10))
564564+template <typename T, int N>
565565+inline Jet<T, N> log10(const Jet<T, N>& f) {
566566+ // Most compilers will expand log(10) to a constant.
567567+ const T a_inverse = T(1.0) / (f.a * log(T(10.0)));
568568+ return Jet<T, N>(log10(f.a), f.v * a_inverse);
569569+}
570570+571571+// log1p(a + h) ~= log1p(a) + h / (1 + a)
572572+template <typename T, int N>
573573+inline Jet<T, N> log1p(const Jet<T, N>& f) {
574574+ const T a_inverse = T(1.0) / (T(1.0) + f.a);
575575+ return Jet<T, N>(log1p(f.a), f.v * a_inverse);
576576+}
577577+578578+// exp(a + h) ~= exp(a) + exp(a) h
579579+template <typename T, int N>
580580+inline Jet<T, N> exp(const Jet<T, N>& f) {
581581+ const T tmp = exp(f.a);
582582+ return Jet<T, N>(tmp, tmp * f.v);
583583+}
584584+585585+// expm1(a + h) ~= expm1(a) + exp(a) h
586586+template <typename T, int N>
587587+inline Jet<T, N> expm1(const Jet<T, N>& f) {
588588+ const T tmp = expm1(f.a);
589589+ const T expa = tmp + T(1.0); // exp(a) = expm1(a) + 1
590590+ return Jet<T, N>(tmp, expa * f.v);
591591+}
592592+593593+// sqrt(a + h) ~= sqrt(a) + h / (2 sqrt(a))
594594+template <typename T, int N>
595595+inline Jet<T, N> sqrt(const Jet<T, N>& f) {
596596+ const T tmp = sqrt(f.a);
597597+ const T two_a_inverse = T(1.0) / (T(2.0) * tmp);
598598+ return Jet<T, N>(tmp, f.v * two_a_inverse);
599599+}
600600+601601+// cos(a + h) ~= cos(a) - sin(a) h
602602+template <typename T, int N>
603603+inline Jet<T, N> cos(const Jet<T, N>& f) {
604604+ return Jet<T, N>(cos(f.a), -sin(f.a) * f.v);
605605+}
606606+607607+// acos(a + h) ~= acos(a) - 1 / sqrt(1 - a^2) h
608608+template <typename T, int N>
609609+inline Jet<T, N> acos(const Jet<T, N>& f) {
610610+ const T tmp = -T(1.0) / sqrt(T(1.0) - f.a * f.a);
611611+ return Jet<T, N>(acos(f.a), tmp * f.v);
612612+}
613613+614614+// sin(a + h) ~= sin(a) + cos(a) h
615615+template <typename T, int N>
616616+inline Jet<T, N> sin(const Jet<T, N>& f) {
617617+ return Jet<T, N>(sin(f.a), cos(f.a) * f.v);
618618+}
619619+620620+// asin(a + h) ~= asin(a) + 1 / sqrt(1 - a^2) h
621621+template <typename T, int N>
622622+inline Jet<T, N> asin(const Jet<T, N>& f) {
623623+ const T tmp = T(1.0) / sqrt(T(1.0) - f.a * f.a);
624624+ return Jet<T, N>(asin(f.a), tmp * f.v);
625625+}
626626+627627+// tan(a + h) ~= tan(a) + (1 + tan(a)^2) h
628628+template <typename T, int N>
629629+inline Jet<T, N> tan(const Jet<T, N>& f) {
630630+ const T tan_a = tan(f.a);
631631+ const T tmp = T(1.0) + tan_a * tan_a;
632632+ return Jet<T, N>(tan_a, tmp * f.v);
633633+}
634634+635635+// atan(a + h) ~= atan(a) + 1 / (1 + a^2) h
636636+template <typename T, int N>
637637+inline Jet<T, N> atan(const Jet<T, N>& f) {
638638+ const T tmp = T(1.0) / (T(1.0) + f.a * f.a);
639639+ return Jet<T, N>(atan(f.a), tmp * f.v);
640640+}
641641+642642+// sinh(a + h) ~= sinh(a) + cosh(a) h
643643+template <typename T, int N>
644644+inline Jet<T, N> sinh(const Jet<T, N>& f) {
645645+ return Jet<T, N>(sinh(f.a), cosh(f.a) * f.v);
646646+}
647647+648648+// cosh(a + h) ~= cosh(a) + sinh(a) h
649649+template <typename T, int N>
650650+inline Jet<T, N> cosh(const Jet<T, N>& f) {
651651+ return Jet<T, N>(cosh(f.a), sinh(f.a) * f.v);
652652+}
653653+654654+// tanh(a + h) ~= tanh(a) + (1 - tanh(a)^2) h
655655+template <typename T, int N>
656656+inline Jet<T, N> tanh(const Jet<T, N>& f) {
657657+ const T tanh_a = tanh(f.a);
658658+ const T tmp = T(1.0) - tanh_a * tanh_a;
659659+ return Jet<T, N>(tanh_a, tmp * f.v);
660660+}
661661+662662+// The floor function should be used with extreme care as this operation will
663663+// result in a zero derivative which provides no information to the solver.
664664+//
665665+// floor(a + h) ~= floor(a) + 0
666666+template <typename T, int N>
667667+inline Jet<T, N> floor(const Jet<T, N>& f) {
668668+ return Jet<T, N>(floor(f.a));
669669+}
670670+671671+// The ceil function should be used with extreme care as this operation will
672672+// result in a zero derivative which provides no information to the solver.
673673+//
674674+// ceil(a + h) ~= ceil(a) + 0
675675+template <typename T, int N>
676676+inline Jet<T, N> ceil(const Jet<T, N>& f) {
677677+ return Jet<T, N>(ceil(f.a));
678678+}
679679+680680+// Some new additions to C++11:
681681+682682+// cbrt(a + h) ~= cbrt(a) + h / (3 a ^ (2/3))
683683+template <typename T, int N>
684684+inline Jet<T, N> cbrt(const Jet<T, N>& f) {
685685+ const T derivative = T(1.0) / (T(3.0) * cbrt(f.a * f.a));
686686+ return Jet<T, N>(cbrt(f.a), f.v * derivative);
687687+}
688688+689689+// exp2(x + h) = 2^(x+h) ~= 2^x + h*2^x*log(2)
690690+template <typename T, int N>
691691+inline Jet<T, N> exp2(const Jet<T, N>& f) {
692692+ const T tmp = exp2(f.a);
693693+ const T derivative = tmp * log(T(2));
694694+ return Jet<T, N>(tmp, f.v * derivative);
695695+}
696696+697697+// log2(x + h) ~= log2(x) + h / (x * log(2))
698698+template <typename T, int N>
699699+inline Jet<T, N> log2(const Jet<T, N>& f) {
700700+ const T derivative = T(1.0) / (f.a * log(T(2)));
701701+ return Jet<T, N>(log2(f.a), f.v * derivative);
702702+}
703703+704704+// Like sqrt(x^2 + y^2),
705705+// but acts to prevent underflow/overflow for small/large x/y.
706706+// Note that the function is non-smooth at x=y=0,
707707+// so the derivative is undefined there.
708708+template <typename T, int N>
709709+inline Jet<T, N> hypot(const Jet<T, N>& x, const Jet<T, N>& y) {
710710+ // d/da sqrt(a) = 0.5 / sqrt(a)
711711+ // d/dx x^2 + y^2 = 2x
712712+ // So by the chain rule:
713713+ // d/dx sqrt(x^2 + y^2) = 0.5 / sqrt(x^2 + y^2) * 2x = x / sqrt(x^2 + y^2)
714714+ // d/dy sqrt(x^2 + y^2) = y / sqrt(x^2 + y^2)
715715+ const T tmp = hypot(x.a, y.a);
716716+ return Jet<T, N>(tmp, x.a / tmp * x.v + y.a / tmp * y.v);
717717+}
718718+719719+// Like sqrt(x^2 + y^2 + z^2),
720720+// but acts to prevent underflow/overflow for small/large x/y/z.
721721+// Note that the function is non-smooth at x=y=z=0,
722722+// so the derivative is undefined there.
723723+template <typename T, int N>
724724+inline Jet<T, N> hypot(const Jet<T, N>& x,
725725+ const Jet<T, N>& y,
726726+ const Jet<T, N>& z) {
727727+ // d/da sqrt(a) = 0.5 / sqrt(a)
728728+ // d/dx x^2 + y^2 + z^2 = 2x
729729+ // So by the chain rule:
730730+ // d/dx sqrt(x^2 + y^2 + z^2)
731731+ // = 0.5 / sqrt(x^2 + y^2 + z^2) * 2x
732732+ // = x / sqrt(x^2 + y^2 + z^2)
733733+ // d/dy sqrt(x^2 + y^2 + z^2) = y / sqrt(x^2 + y^2 + z^2)
734734+ // d/dz sqrt(x^2 + y^2 + z^2) = z / sqrt(x^2 + y^2 + z^2)
735735+ const T tmp = hypot(x.a, y.a, z.a);
736736+ return Jet<T, N>(tmp, x.a / tmp * x.v + y.a / tmp * y.v + z.a / tmp * z.v);
737737+}
738738+739739+// Like x * y + z but rounded only once.
740740+template <typename T, int N>
741741+inline Jet<T, N> fma(const Jet<T, N>& x,
742742+ const Jet<T, N>& y,
743743+ const Jet<T, N>& z) {
744744+ // d/dx fma(x, y, z) = y
745745+ // d/dy fma(x, y, z) = x
746746+ // d/dz fma(x, y, z) = 1
747747+ return Jet<T, N>(fma(x.a, y.a, z.a), y.a * x.v + x.a * y.v + z.v);
748748+}
749749+750750+// Returns the larger of the two arguments. NaNs are treated as missing data.
751751+//
752752+// NOTE: This function is NOT subject to any of the error conditions specified
753753+// in `math_errhandling`.
754754+template <typename Lhs,
755755+ typename Rhs,
756756+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
757757+inline decltype(auto) fmax(const Lhs& f, const Rhs& g) {
758758+ using J = std::common_type_t<Lhs, Rhs>;
759759+ return (isnan(g) || isgreater(f, g)) ? J{f} : J{g};
760760+}
761761+762762+// Returns the smaller of the two arguments. NaNs are treated as missing data.
763763+//
764764+// NOTE: This function is NOT subject to any of the error conditions specified
765765+// in `math_errhandling`.
766766+template <typename Lhs,
767767+ typename Rhs,
768768+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
769769+inline decltype(auto) fmin(const Lhs& f, const Rhs& g) {
770770+ using J = std::common_type_t<Lhs, Rhs>;
771771+ return (isnan(f) || isless(g, f)) ? J{g} : J{f};
772772+}
773773+774774+// Returns the positive difference (f - g) of two arguments and zero if f <= g.
775775+// If at least one argument is NaN, a NaN is return.
776776+//
777777+// NOTE At least one of the argument types must be a Jet, the other one can be a
778778+// scalar. In case both arguments are Jets, their dimensionality must match.
779779+template <typename Lhs,
780780+ typename Rhs,
781781+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
782782+inline decltype(auto) fdim(const Lhs& f, const Rhs& g) {
783783+ using J = std::common_type_t<Lhs, Rhs>;
784784+ if (isnan(f) || isnan(g)) {
785785+ return std::numeric_limits<J>::quiet_NaN();
786786+ }
787787+ return isgreater(f, g) ? J{f - g} : J{};
788788+}
789789+790790+// erf is defined as an integral that cannot be expressed analytically
791791+// however, the derivative is trivial to compute
792792+// erf(x + h) = erf(x) + h * 2*exp(-x^2)/sqrt(pi)
793793+template <typename T, int N>
794794+inline Jet<T, N> erf(const Jet<T, N>& x) {
795795+ // We evaluate the constant as follows:
796796+ // 2 / sqrt(pi) = 1 / sqrt(atan(1.))
797797+ // On POSIX systems it is defined as M_2_SQRTPI, but this is not
798798+ // portable and the type may not be T. The above expression
799799+ // evaluates to full precision with IEEE arithmetic and, since it's
800800+ // constant, the compiler can generate exactly the same code. gcc
801801+ // does so even at -O0.
802802+ return Jet<T, N>(erf(x.a), x.v * exp(-x.a * x.a) * (T(1) / sqrt(atan(T(1)))));
803803+}
804804+805805+// erfc(x) = 1-erf(x)
806806+// erfc(x + h) = erfc(x) + h * (-2*exp(-x^2)/sqrt(pi))
807807+template <typename T, int N>
808808+inline Jet<T, N> erfc(const Jet<T, N>& x) {
809809+ // See in erf() above for the evaluation of the constant in the derivative.
810810+ return Jet<T, N>(erfc(x.a),
811811+ -x.v * exp(-x.a * x.a) * (T(1) / sqrt(atan(T(1)))));
812812+}
813813+814814+// Bessel functions of the first kind with integer order equal to 0, 1, n.
815815+//
816816+// Microsoft has deprecated the j[0,1,n]() POSIX Bessel functions in favour of
817817+// _j[0,1,n](). Where available on MSVC, use _j[0,1,n]() to avoid deprecated
818818+// function errors in client code (the specific warning is suppressed when
819819+// Ceres itself is built).
820820+inline double BesselJ0(double x) {
821821+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
822822+ return _j0(x);
823823+#else
824824+ return j0(x);
825825+#endif
826826+}
827827+inline double BesselJ1(double x) {
828828+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
829829+ return _j1(x);
830830+#else
831831+ return j1(x);
832832+#endif
833833+}
834834+inline double BesselJn(int n, double x) {
835835+#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
836836+ return _jn(n, x);
837837+#else
838838+ return jn(n, x);
839839+#endif
840840+}
841841+842842+// For the formulae of the derivatives of the Bessel functions see the book:
843843+// Olver, Lozier, Boisvert, Clark, NIST Handbook of Mathematical Functions,
844844+// Cambridge University Press 2010.
845845+//
846846+// Formulae are also available at http://dlmf.nist.gov
847847+848848+// See formula http://dlmf.nist.gov/10.6#E3
849849+// j0(a + h) ~= j0(a) - j1(a) h
850850+template <typename T, int N>
851851+inline Jet<T, N> BesselJ0(const Jet<T, N>& f) {
852852+ return Jet<T, N>(BesselJ0(f.a), -BesselJ1(f.a) * f.v);
853853+}
854854+855855+// See formula http://dlmf.nist.gov/10.6#E1
856856+// j1(a + h) ~= j1(a) + 0.5 ( j0(a) - j2(a) ) h
857857+template <typename T, int N>
858858+inline Jet<T, N> BesselJ1(const Jet<T, N>& f) {
859859+ return Jet<T, N>(BesselJ1(f.a),
860860+ T(0.5) * (BesselJ0(f.a) - BesselJn(2, f.a)) * f.v);
861861+}
862862+863863+// See formula http://dlmf.nist.gov/10.6#E1
864864+// j_n(a + h) ~= j_n(a) + 0.5 ( j_{n-1}(a) - j_{n+1}(a) ) h
865865+template <typename T, int N>
866866+inline Jet<T, N> BesselJn(int n, const Jet<T, N>& f) {
867867+ return Jet<T, N>(
868868+ BesselJn(n, f.a),
869869+ T(0.5) * (BesselJn(n - 1, f.a) - BesselJn(n + 1, f.a)) * f.v);
870870+}
871871+872872+// Classification and comparison functionality referencing only the scalar part
873873+// of a Jet. To classify the derivatives (e.g., for sanity checks), the dual
874874+// part should be referenced explicitly. For instance, to check whether the
875875+// derivatives of a Jet 'f' are reasonable, one can use
876876+//
877877+// isfinite(f.v.array()).all()
878878+// !isnan(f.v.array()).any()
879879+//
880880+// etc., depending on the desired semantics.
881881+//
882882+// NOTE: Floating-point classification and comparison functions and operators
883883+// should be used with care as no derivatives can be propagated by such
884884+// functions directly but only by expressions resulting from corresponding
885885+// conditional statements. At the same time, conditional statements can possibly
886886+// introduce a discontinuity in the cost function making it impossible to
887887+// evaluate its derivative and thus the optimization problem intractable.
888888+889889+// Determines whether the scalar part of the Jet is finite.
890890+template <typename T, int N>
891891+inline bool isfinite(const Jet<T, N>& f) {
892892+ return isfinite(f.a);
893893+}
894894+895895+// Determines whether the scalar part of the Jet is infinite.
896896+template <typename T, int N>
897897+inline bool isinf(const Jet<T, N>& f) {
898898+ return isinf(f.a);
899899+}
900900+901901+// Determines whether the scalar part of the Jet is NaN.
902902+template <typename T, int N>
903903+inline bool isnan(const Jet<T, N>& f) {
904904+ return isnan(f.a);
905905+}
906906+907907+// Determines whether the scalar part of the Jet is neither zero, subnormal,
908908+// infinite, nor NaN.
909909+template <typename T, int N>
910910+inline bool isnormal(const Jet<T, N>& f) {
911911+ return isnormal(f.a);
912912+}
913913+914914+// Determines whether the scalar part of the Jet f is less than the scalar
915915+// part of g.
916916+//
917917+// NOTE: This function does NOT set any floating-point exceptions.
918918+template <typename Lhs,
919919+ typename Rhs,
920920+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
921921+inline bool isless(const Lhs& f, const Rhs& g) {
922922+ using internal::AsScalar;
923923+ return isless(AsScalar(f), AsScalar(g));
924924+}
925925+926926+// Determines whether the scalar part of the Jet f is greater than the scalar
927927+// part of g.
928928+//
929929+// NOTE: This function does NOT set any floating-point exceptions.
930930+template <typename Lhs,
931931+ typename Rhs,
932932+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
933933+inline bool isgreater(const Lhs& f, const Rhs& g) {
934934+ using internal::AsScalar;
935935+ return isgreater(AsScalar(f), AsScalar(g));
936936+}
937937+938938+// Determines whether the scalar part of the Jet f is less than or equal to the
939939+// scalar part of g.
940940+//
941941+// NOTE: This function does NOT set any floating-point exceptions.
942942+template <typename Lhs,
943943+ typename Rhs,
944944+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
945945+inline bool islessequal(const Lhs& f, const Rhs& g) {
946946+ using internal::AsScalar;
947947+ return islessequal(AsScalar(f), AsScalar(g));
948948+}
949949+950950+// Determines whether the scalar part of the Jet f is less than or greater than
951951+// (f < g || f > g) the scalar part of g.
952952+//
953953+// NOTE: This function does NOT set any floating-point exceptions.
954954+template <typename Lhs,
955955+ typename Rhs,
956956+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
957957+inline bool islessgreater(const Lhs& f, const Rhs& g) {
958958+ using internal::AsScalar;
959959+ return islessgreater(AsScalar(f), AsScalar(g));
960960+}
961961+962962+// Determines whether the scalar part of the Jet f is greater than or equal to
963963+// the scalar part of g.
964964+//
965965+// NOTE: This function does NOT set any floating-point exceptions.
966966+template <typename Lhs,
967967+ typename Rhs,
968968+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
969969+inline bool isgreaterequal(const Lhs& f, const Rhs& g) {
970970+ using internal::AsScalar;
971971+ return isgreaterequal(AsScalar(f), AsScalar(g));
972972+}
973973+974974+// Determines if either of the scalar parts of the arguments are NaN and
975975+// thus cannot be ordered with respect to each other.
976976+template <typename Lhs,
977977+ typename Rhs,
978978+ std::enable_if_t<CompatibleJetOperands_v<Lhs, Rhs>>* = nullptr>
979979+inline bool isunordered(const Lhs& f, const Rhs& g) {
980980+ using internal::AsScalar;
981981+ return isunordered(AsScalar(f), AsScalar(g));
982982+}
983983+984984+// Categorize scalar part as zero, subnormal, normal, infinite, NaN, or
985985+// implementation-defined.
986986+template <typename T, int N>
987987+inline int fpclassify(const Jet<T, N>& f) {
988988+ return fpclassify(f.a);
989989+}
990990+991991+// Determines whether the scalar part of the argument is negative.
992992+template <typename T, int N>
993993+inline bool signbit(const Jet<T, N>& f) {
994994+ return signbit(f.a);
995995+}
996996+997997+998998+999999+#ifdef CERES_HAS_CPP20
10001000+// Computes the linear interpolation a + t(b - a) between a and b at the value
10011001+// t. For arguments outside of the range 0 <= t <= 1, the values are
10021002+// extrapolated.
10031003+//
10041004+// Differentiating lerp(a, b, t) with respect to a, b, and t gives:
10051005+//
10061006+// d/da lerp(a, b, t) = 1 - t
10071007+// d/db lerp(a, b, t) = t
10081008+// d/dt lerp(a, b, t) = b - a
10091009+//
10101010+// with the dual representation given by
10111011+//
10121012+// lerp(a + da, b + db, t + dt)
10131013+// ~= lerp(a, b, t) + (1 - t) da + t db + (b - a) dt .
10141014+template <typename T, int N>
10151015+inline Jet<T, N> lerp(const Jet<T, N>& a,
10161016+ const Jet<T, N>& b,
10171017+ const Jet<T, N>& t) {
10181018+ return Jet<T, N>{lerp(a.a, b.a, t.a),
10191019+ (T(1) - t.a) * a.v + t.a * b.v + (b.a - a.a) * t.v};
10201020+}
10211021+10221022+// Computes the midpoint a + (b - a) / 2.
10231023+//
10241024+// Differentiating midpoint(a, b) with respect to a and b gives:
10251025+//
10261026+// d/da midpoint(a, b) = 1/2
10271027+// d/db midpoint(a, b) = 1/2
10281028+//
10291029+// with the dual representation given by
10301030+//
10311031+// midpoint(a + da, b + db) ~= midpoint(a, b) + (da + db) / 2 .
10321032+template <typename T, int N>
10331033+inline Jet<T, N> midpoint(const Jet<T, N>& a, const Jet<T, N>& b) {
10341034+ Jet<T, N> result{midpoint(a.a, b.a)};
10351035+ // To avoid overflow in the differential, compute
10361036+ // (da + db) / 2 using midpoint.
10371037+ for (int i = 0; i < N; ++i) {
10381038+ result.v[i] = midpoint(a.v[i], b.v[i]);
10391039+ }
10401040+ return result;
10411041+}
10421042+#endif // defined(CERES_HAS_CPP20)
10431043+10441044+// atan2(b + db, a + da) ~= atan2(b, a) + (- b da + a db) / (a^2 + b^2)
10451045+//
10461046+// In words: the rate of change of theta is 1/r times the rate of
10471047+// change of (x, y) in the positive angular direction.
10481048+template <typename T, int N>
10491049+inline Jet<T, N> atan2(const Jet<T, N>& g, const Jet<T, N>& f) {
10501050+ // Note order of arguments:
10511051+ //
10521052+ // f = a + da
10531053+ // g = b + db
10541054+10551055+ T const tmp = T(1.0) / (f.a * f.a + g.a * g.a);
10561056+ return Jet<T, N>(atan2(g.a, f.a), tmp * (-g.a * f.v + f.a * g.v));
10571057+}
10581058+10591059+// Computes the square x^2 of a real number x (not the Euclidean L^2 norm as
10601060+// the name might suggest).
10611061+//
10621062+// NOTE: While std::norm is primarily intended for computing the squared
10631063+// magnitude of a std::complex<> number, the current Jet implementation does not
10641064+// support mixing a scalar T in its real part and std::complex<T> and in the
10651065+// infinitesimal. Mixed Jet support is necessary for the type decay from
10661066+// std::complex<T> to T (the squared magnitude of a complex number is always
10671067+// real) performed by std::norm.
10681068+//
10691069+// norm(x + h) ~= norm(x) + 2x h
10701070+template <typename T, int N>
10711071+inline Jet<T, N> norm(const Jet<T, N>& f) {
10721072+ return Jet<T, N>(norm(f.a), T(2) * f.a * f.v);
10731073+}
10741074+10751075+// pow -- base is a differentiable function, exponent is a constant.
10761076+// (a+da)^p ~= a^p + p*a^(p-1) da
10771077+template <typename T, int N>
10781078+inline Jet<T, N> pow(const Jet<T, N>& f, double g) {
10791079+ T const tmp = g * pow(f.a, g - T(1.0));
10801080+ return Jet<T, N>(pow(f.a, g), tmp * f.v);
10811081+}
10821082+10831083+// pow -- base is a constant, exponent is a differentiable function.
10841084+// We have various special cases, see the comment for pow(Jet, Jet) for
10851085+// analysis:
10861086+//
10871087+// 1. For f > 0 we have: (f)^(g + dg) ~= f^g + f^g log(f) dg
10881088+//
10891089+// 2. For f == 0 and g > 0 we have: (f)^(g + dg) ~= f^g
10901090+//
10911091+// 3. For f < 0 and integer g we have: (f)^(g + dg) ~= f^g but if dg
10921092+// != 0, the derivatives are not defined and we return NaN.
10931093+10941094+template <typename T, int N>
10951095+inline Jet<T, N> pow(T f, const Jet<T, N>& g) {
10961096+ Jet<T, N> result;
10971097+10981098+ if (fpclassify(f) == FP_ZERO && g > 0) {
10991099+ // Handle case 2.
11001100+ result = Jet<T, N>(T(0.0));
11011101+ } else {
11021102+ if (f < 0 && g == floor(g.a)) { // Handle case 3.
11031103+ result = Jet<T, N>(pow(f, g.a));
11041104+ for (int i = 0; i < N; i++) {
11051105+ if (fpclassify(g.v[i]) != FP_ZERO) {
11061106+ // Return a NaN when g.v != 0.
11071107+ result.v[i] = std::numeric_limits<T>::quiet_NaN();
11081108+ }
11091109+ }
11101110+ } else {
11111111+ // Handle case 1.
11121112+ T const tmp = pow(f, g.a);
11131113+ result = Jet<T, N>(tmp, log(f) * tmp * g.v);
11141114+ }
11151115+ }
11161116+11171117+ return result;
11181118+}
11191119+11201120+// pow -- both base and exponent are differentiable functions. This has a
11211121+// variety of special cases that require careful handling.
11221122+//
11231123+// 1. For f > 0:
11241124+// (f + df)^(g + dg) ~= f^g + f^(g - 1) * (g * df + f * log(f) * dg)
11251125+// The numerical evaluation of f * log(f) for f > 0 is well behaved, even for
11261126+// extremely small values (e.g. 1e-99).
11271127+//
11281128+// 2. For f == 0 and g > 1: (f + df)^(g + dg) ~= 0
11291129+// This cases is needed because log(0) can not be evaluated in the f > 0
11301130+// expression. However the function f*log(f) is well behaved around f == 0
11311131+// and its limit as f-->0 is zero.
11321132+//
11331133+// 3. For f == 0 and g == 1: (f + df)^(g + dg) ~= 0 + df
11341134+//
11351135+// 4. For f == 0 and 0 < g < 1: The value is finite but the derivatives are not.
11361136+//
11371137+// 5. For f == 0 and g < 0: The value and derivatives of f^g are not finite.
11381138+//
11391139+// 6. For f == 0 and g == 0: The C standard incorrectly defines 0^0 to be 1
11401140+// "because there are applications that can exploit this definition". We
11411141+// (arbitrarily) decree that derivatives here will be nonfinite, since that
11421142+// is consistent with the behavior for f == 0, g < 0 and 0 < g < 1.
11431143+// Practically any definition could have been justified because mathematical
11441144+// consistency has been lost at this point.
11451145+//
11461146+// 7. For f < 0, g integer, dg == 0: (f + df)^(g + dg) ~= f^g + g * f^(g - 1) df
11471147+// This is equivalent to the case where f is a differentiable function and g
11481148+// is a constant (to first order).
11491149+//
11501150+// 8. For f < 0, g integer, dg != 0: The value is finite but the derivatives are
11511151+// not, because any change in the value of g moves us away from the point
11521152+// with a real-valued answer into the region with complex-valued answers.
11531153+//
11541154+// 9. For f < 0, g noninteger: The value and derivatives of f^g are not finite.
11551155+11561156+template <typename T, int N>
11571157+inline Jet<T, N> pow(const Jet<T, N>& f, const Jet<T, N>& g) {
11581158+ Jet<T, N> result;
11591159+11601160+ if (fpclassify(f) == FP_ZERO && g >= 1) {
11611161+ // Handle cases 2 and 3.
11621162+ if (g > 1) {
11631163+ result = Jet<T, N>(T(0.0));
11641164+ } else {
11651165+ result = f;
11661166+ }
11671167+11681168+ } else {
11691169+ if (f < 0 && g == floor(g.a)) {
11701170+ // Handle cases 7 and 8.
11711171+ T const tmp = g.a * pow(f.a, g.a - T(1.0));
11721172+ result = Jet<T, N>(pow(f.a, g.a), tmp * f.v);
11731173+ for (int i = 0; i < N; i++) {
11741174+ if (fpclassify(g.v[i]) != FP_ZERO) {
11751175+ // Return a NaN when g.v != 0.
11761176+ result.v[i] = T(std::numeric_limits<double>::quiet_NaN());
11771177+ }
11781178+ }
11791179+ } else {
11801180+ // Handle the remaining cases. For cases 4,5,6,9 we allow the log()
11811181+ // function to generate -HUGE_VAL or NaN, since those cases result in a
11821182+ // nonfinite derivative.
11831183+ T const tmp1 = pow(f.a, g.a);
11841184+ T const tmp2 = g.a * pow(f.a, g.a - T(1.0));
11851185+ T const tmp3 = tmp1 * log(f.a);
11861186+ result = Jet<T, N>(tmp1, tmp2 * f.v + tmp3 * g.v);
11871187+ }
11881188+ }
11891189+11901190+ return result;
11911191+}
11921192+11931193+// Note: This has to be in the ceres namespace for argument dependent lookup to
11941194+// function correctly. Otherwise statements like CHECK_LE(x, 2.0) fail with
11951195+// strange compile errors.
11961196+template <typename T, int N>
11971197+inline std::ostream& operator<<(std::ostream& s, const Jet<T, N>& z) {
11981198+ s << "[" << z.a << " ; ";
11991199+ for (int i = 0; i < N; ++i) {
12001200+ s << z.v[i];
12011201+ if (i != N - 1) {
12021202+ s << ", ";
12031203+ }
12041204+ }
12051205+ s << "]";
12061206+ return s;
12071207+}
12081208+} // namespace ceres
12091209+12101210+namespace std {
12111211+template <typename T, int N>
12121212+struct numeric_limits<ceres::Jet<T, N>> {
12131213+ static constexpr bool is_specialized = true;
12141214+ static constexpr bool is_signed = std::numeric_limits<T>::is_signed;
12151215+ static constexpr bool is_integer = std::numeric_limits<T>::is_integer;
12161216+ static constexpr bool is_exact = std::numeric_limits<T>::is_exact;
12171217+ static constexpr bool has_infinity = std::numeric_limits<T>::has_infinity;
12181218+ static constexpr bool has_quiet_NaN = std::numeric_limits<T>::has_quiet_NaN;
12191219+ static constexpr bool has_signaling_NaN =
12201220+ std::numeric_limits<T>::has_signaling_NaN;
12211221+ static constexpr bool is_iec559 = std::numeric_limits<T>::is_iec559;
12221222+ static constexpr bool is_bounded = std::numeric_limits<T>::is_bounded;
12231223+ static constexpr bool is_modulo = std::numeric_limits<T>::is_modulo;
12241224+12251225+ static constexpr std::float_denorm_style has_denorm =
12261226+ std::numeric_limits<T>::has_denorm;
12271227+ static constexpr std::float_round_style round_style =
12281228+ std::numeric_limits<T>::round_style;
12291229+12301230+ static constexpr int digits = std::numeric_limits<T>::digits;
12311231+ static constexpr int digits10 = std::numeric_limits<T>::digits10;
12321232+ static constexpr int max_digits10 = std::numeric_limits<T>::max_digits10;
12331233+ static constexpr int radix = std::numeric_limits<T>::radix;
12341234+ static constexpr int min_exponent = std::numeric_limits<T>::min_exponent;
12351235+ static constexpr int min_exponent10 = std::numeric_limits<T>::max_exponent10;
12361236+ static constexpr int max_exponent = std::numeric_limits<T>::max_exponent;
12371237+ static constexpr int max_exponent10 = std::numeric_limits<T>::max_exponent10;
12381238+ static constexpr bool traps = std::numeric_limits<T>::traps;
12391239+ static constexpr bool tinyness_before =
12401240+ std::numeric_limits<T>::tinyness_before;
12411241+12421242+ static constexpr ceres::Jet<T, N> min
12431243+ CERES_PREVENT_MACRO_SUBSTITUTION() noexcept {
12441244+ return ceres::Jet<T, N>((std::numeric_limits<T>::min)());
12451245+ }
12461246+ static constexpr ceres::Jet<T, N> lowest() noexcept {
12471247+ return ceres::Jet<T, N>(std::numeric_limits<T>::lowest());
12481248+ }
12491249+ static constexpr ceres::Jet<T, N> epsilon() noexcept {
12501250+ return ceres::Jet<T, N>(std::numeric_limits<T>::epsilon());
12511251+ }
12521252+ static constexpr ceres::Jet<T, N> round_error() noexcept {
12531253+ return ceres::Jet<T, N>(std::numeric_limits<T>::round_error());
12541254+ }
12551255+ static constexpr ceres::Jet<T, N> infinity() noexcept {
12561256+ return ceres::Jet<T, N>(std::numeric_limits<T>::infinity());
12571257+ }
12581258+ static constexpr ceres::Jet<T, N> quiet_NaN() noexcept {
12591259+ return ceres::Jet<T, N>(std::numeric_limits<T>::quiet_NaN());
12601260+ }
12611261+ static constexpr ceres::Jet<T, N> signaling_NaN() noexcept {
12621262+ return ceres::Jet<T, N>(std::numeric_limits<T>::signaling_NaN());
12631263+ }
12641264+ static constexpr ceres::Jet<T, N> denorm_min() noexcept {
12651265+ return ceres::Jet<T, N>(std::numeric_limits<T>::denorm_min());
12661266+ }
12671267+12681268+ static constexpr ceres::Jet<T, N> max
12691269+ CERES_PREVENT_MACRO_SUBSTITUTION() noexcept {
12701270+ return ceres::Jet<T, N>((std::numeric_limits<T>::max)());
12711271+ }
12721272+};
12731273+12741274+} // namespace std
12751275+12761276+namespace Eigen {
12771277+12781278+// Creating a specialization of NumTraits enables placing Jet objects inside
12791279+// Eigen arrays, getting all the goodness of Eigen combined with autodiff.
12801280+template <typename T, int N>
12811281+struct NumTraits<ceres::Jet<T, N>> {
12821282+ using Real = ceres::Jet<T, N>;
12831283+ using NonInteger = ceres::Jet<T, N>;
12841284+ using Nested = ceres::Jet<T, N>;
12851285+ using Literal = ceres::Jet<T, N>;
12861286+12871287+ static typename ceres::Jet<T, N> dummy_precision() {
12881288+ return ceres::Jet<T, N>(1e-12);
12891289+ }
12901290+12911291+ static inline Real epsilon() {
12921292+ return Real(std::numeric_limits<T>::epsilon());
12931293+ }
12941294+12951295+ static inline int digits10() { return NumTraits<T>::digits10(); }
12961296+12971297+ enum {
12981298+ IsComplex = 0,
12991299+ IsInteger = 0,
13001300+ IsSigned,
13011301+ ReadCost = 1,
13021302+ AddCost = 1,
13031303+ // For Jet types, multiplication is more expensive than addition.
13041304+ MulCost = 3,
13051305+ HasFloatingPoint = 1,
13061306+ RequireInitialization = 1
13071307+ };
13081308+13091309+ template <bool Vectorized>
13101310+ struct Div {
13111311+ enum {
13121312+#if defined(EIGEN_VECTORIZE_AVX)
13131313+ AVX = true,
13141314+#else
13151315+ AVX = false,
13161316+#endif
13171317+13181318+ // Assuming that for Jets, division is as expensive as
13191319+ // multiplication.
13201320+ Cost = 3
13211321+ };
13221322+ };
13231323+13241324+ static inline Real highest() { return Real((std::numeric_limits<T>::max)()); }
13251325+ static inline Real lowest() { return Real(-(std::numeric_limits<T>::max)()); }
13261326+};
13271327+13281328+// Specifying the return type of binary operations between Jets and scalar types
13291329+// allows you to perform matrix/array operations with Eigen matrices and arrays
13301330+// such as addition, subtraction, multiplication, and division where one Eigen
13311331+// matrix/array is of type Jet and the other is a scalar type. This improves
13321332+// performance by using the optimized scalar-to-Jet binary operations but
13331333+// is only available on Eigen versions >= 3.3
13341334+template <typename BinaryOp, typename T, int N>
13351335+struct ScalarBinaryOpTraits<ceres::Jet<T, N>, T, BinaryOp> {
13361336+ using ReturnType = ceres::Jet<T, N>;
13371337+};
13381338+template <typename BinaryOp, typename T, int N>
13391339+struct ScalarBinaryOpTraits<T, ceres::Jet<T, N>, BinaryOp> {
13401340+ using ReturnType = ceres::Jet<T, N>;
13411341+};
13421342+13431343+} // namespace Eigen
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2022 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: sergiu.deitsch@gmail.com (Sergiu Deitsch)
3131+//
3232+3333+#pragma once
3434+3535+namespace ceres {
3636+3737+// Jet forward declaration necessary for the following partial specialization of
3838+// std::common_type and type traits.
3939+template <typename T, int N>
4040+struct Jet;
4141+4242+} // namespace ceres
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2021 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: mierle@gmail.com (Keir Mierle)
3131+//
3232+// WARNING WARNING WARNING
3333+// WARNING WARNING WARNING Tiny solver is experimental and will change.
3434+// WARNING WARNING WARNING
3535+//
3636+// A tiny least squares solver using Levenberg-Marquardt, intended for solving
3737+// small dense problems with low latency and low overhead. The implementation
3838+// takes care to do all allocation up front, so that no memory is allocated
3939+// during solving. This is especially useful when solving many similar problems;
4040+// for example, inverse pixel distortion for every pixel on a grid.
4141+//
4242+// Note: This code has no dependencies beyond Eigen, including on other parts of
4343+// Ceres, so it is possible to take this file alone and put it in another
4444+// project without the rest of Ceres.
4545+//
4646+// Algorithm based off of:
4747+//
4848+// [1] K. Madsen, H. Nielsen, O. Tingleoff.
4949+// Methods for Non-linear Least Squares Problems.
5050+// http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf
5151+5252+#ifndef CERES_PUBLIC_TINY_SOLVER_H_
5353+#define CERES_PUBLIC_TINY_SOLVER_H_
5454+5555+#include <cassert>
5656+#include <cmath>
5757+5858+#include "Eigen/Dense"
5959+6060+namespace ceres {
6161+6262+// To use tiny solver, create a class or struct that allows computing the cost
6363+// function (described below). This is similar to a ceres::CostFunction, but is
6464+// different to enable statically allocating all memory for the solver
6565+// (specifically, enum sizes). Key parts are the Scalar typedef, the enums to
6666+// describe problem sizes (needed to remove all heap allocations), and the
6767+// operator() overload to evaluate the cost and (optionally) jacobians.
6868+//
6969+// struct TinySolverCostFunctionTraits {
7070+// typedef double Scalar;
7171+// enum {
7272+// NUM_RESIDUALS = <int> OR Eigen::Dynamic,
7373+// NUM_PARAMETERS = <int> OR Eigen::Dynamic,
7474+// };
7575+// bool operator()(const double* parameters,
7676+// double* residuals,
7777+// double* jacobian) const;
7878+//
7979+// int NumResiduals() const; -- Needed if NUM_RESIDUALS == Eigen::Dynamic.
8080+// int NumParameters() const; -- Needed if NUM_PARAMETERS == Eigen::Dynamic.
8181+// };
8282+//
8383+// For operator(), the size of the objects is:
8484+//
8585+// double* parameters -- NUM_PARAMETERS or NumParameters()
8686+// double* residuals -- NUM_RESIDUALS or NumResiduals()
8787+// double* jacobian -- NUM_RESIDUALS * NUM_PARAMETERS in column-major format
8888+// (Eigen's default); or nullptr if no jacobian
8989+// requested.
9090+//
9191+// An example (fully statically sized):
9292+//
9393+// struct MyCostFunctionExample {
9494+// typedef double Scalar;
9595+// enum {
9696+// NUM_RESIDUALS = 2,
9797+// NUM_PARAMETERS = 3,
9898+// };
9999+// bool operator()(const double* parameters,
100100+// double* residuals,
101101+// double* jacobian) const {
102102+// residuals[0] = x + 2*y + 4*z;
103103+// residuals[1] = y * z;
104104+// if (jacobian) {
105105+// jacobian[0 * 2 + 0] = 1; // First column (x).
106106+// jacobian[0 * 2 + 1] = 0;
107107+//
108108+// jacobian[1 * 2 + 0] = 2; // Second column (y).
109109+// jacobian[1 * 2 + 1] = z;
110110+//
111111+// jacobian[2 * 2 + 0] = 4; // Third column (z).
112112+// jacobian[2 * 2 + 1] = y;
113113+// }
114114+// return true;
115115+// }
116116+// };
117117+//
118118+// The solver supports either statically or dynamically sized cost
119119+// functions. If the number of residuals is dynamic then the Function
120120+// must define:
121121+//
122122+// int NumResiduals() const;
123123+//
124124+// If the number of parameters is dynamic then the Function must
125125+// define:
126126+//
127127+// int NumParameters() const;
128128+//
129129+template <typename Function,
130130+ typename LinearSolver =
131131+ Eigen::LDLT<Eigen::Matrix<typename Function::Scalar, //
132132+ Function::NUM_PARAMETERS, //
133133+ Function::NUM_PARAMETERS>>>
134134+class TinySolver {
135135+ public:
136136+ // This class needs to have an Eigen aligned operator new as it contains
137137+ // fixed-size Eigen types.
138138+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW
139139+140140+ enum {
141141+ NUM_RESIDUALS = Function::NUM_RESIDUALS,
142142+ NUM_PARAMETERS = Function::NUM_PARAMETERS
143143+ };
144144+ using Scalar = typename Function::Scalar;
145145+ using Parameters = typename Eigen::Matrix<Scalar, NUM_PARAMETERS, 1>;
146146+147147+ enum Status {
148148+ // max_norm |J'(x) * f(x)| < gradient_tolerance
149149+ GRADIENT_TOO_SMALL,
150150+ // ||dx|| <= parameter_tolerance * (||x|| + parameter_tolerance)
151151+ RELATIVE_STEP_SIZE_TOO_SMALL,
152152+ // cost_threshold > ||f(x)||^2 / 2
153153+ COST_TOO_SMALL,
154154+ // num_iterations >= max_num_iterations
155155+ HIT_MAX_ITERATIONS,
156156+ // (new_cost - old_cost) < function_tolerance * old_cost
157157+ COST_CHANGE_TOO_SMALL,
158158+159159+ // TODO(sameeragarwal): Deal with numerical failures.
160160+ };
161161+162162+ struct Options {
163163+ int max_num_iterations = 50;
164164+165165+ // max_norm |J'(x) * f(x)| < gradient_tolerance
166166+ Scalar gradient_tolerance = 1e-10;
167167+168168+ // ||dx|| <= parameter_tolerance * (||x|| + parameter_tolerance)
169169+ Scalar parameter_tolerance = 1e-8;
170170+171171+ // (new_cost - old_cost) < function_tolerance * old_cost
172172+ Scalar function_tolerance = 1e-6;
173173+174174+ // cost_threshold > ||f(x)||^2 / 2
175175+ Scalar cost_threshold = std::numeric_limits<Scalar>::epsilon();
176176+177177+ Scalar initial_trust_region_radius = 1e4;
178178+ };
179179+180180+ struct Summary {
181181+ // 1/2 ||f(x_0)||^2
182182+ Scalar initial_cost = -1;
183183+ // 1/2 ||f(x)||^2
184184+ Scalar final_cost = -1;
185185+ // max_norm(J'f(x))
186186+ Scalar gradient_max_norm = -1;
187187+ int iterations = -1;
188188+ Status status = HIT_MAX_ITERATIONS;
189189+ };
190190+191191+ bool Update(const Function& function, const Parameters& x) {
192192+ if (!function(x.data(), residuals_.data(), jacobian_.data())) {
193193+ return false;
194194+ }
195195+196196+ residuals_ = -residuals_;
197197+198198+ // On the first iteration, compute a diagonal (Jacobi) scaling
199199+ // matrix, which we store as a vector.
200200+ if (summary.iterations == 0) {
201201+ // jacobi_scaling = 1 / (1 + diagonal(J'J))
202202+ //
203203+ // 1 is added to the denominator to regularize small diagonal
204204+ // entries.
205205+ jacobi_scaling_ = 1.0 / (1.0 + jacobian_.colwise().norm().array());
206206+ }
207207+208208+ // This explicitly computes the normal equations, which is numerically
209209+ // unstable. Nevertheless, it is often good enough and is fast.
210210+ //
211211+ // TODO(sameeragarwal): Refactor this to allow for DenseQR
212212+ // factorization.
213213+ jacobian_ = jacobian_ * jacobi_scaling_.asDiagonal();
214214+ jtj_ = jacobian_.transpose() * jacobian_;
215215+ g_ = jacobian_.transpose() * residuals_;
216216+ summary.gradient_max_norm = g_.array().abs().maxCoeff();
217217+ cost_ = residuals_.squaredNorm() / 2;
218218+ return true;
219219+ }
220220+221221+ const Summary& Solve(const Function& function, Parameters* x_and_min) {
222222+ Initialize<NUM_RESIDUALS, NUM_PARAMETERS>(function);
223223+ assert(x_and_min);
224224+ Parameters& x = *x_and_min;
225225+ summary = Summary();
226226+ summary.iterations = 0;
227227+228228+ // TODO(sameeragarwal): Deal with failure here.
229229+ Update(function, x);
230230+ summary.initial_cost = cost_;
231231+ summary.final_cost = cost_;
232232+233233+ if (summary.gradient_max_norm < options.gradient_tolerance) {
234234+ summary.status = GRADIENT_TOO_SMALL;
235235+ return summary;
236236+ }
237237+238238+ if (cost_ < options.cost_threshold) {
239239+ summary.status = COST_TOO_SMALL;
240240+ return summary;
241241+ }
242242+243243+ Scalar u = 1.0 / options.initial_trust_region_radius;
244244+ Scalar v = 2;
245245+246246+ for (summary.iterations = 1;
247247+ summary.iterations < options.max_num_iterations;
248248+ summary.iterations++) {
249249+ jtj_regularized_ = jtj_;
250250+ const Scalar min_diagonal = 1e-6;
251251+ const Scalar max_diagonal = 1e32;
252252+ for (int i = 0; i < lm_diagonal_.rows(); ++i) {
253253+ lm_diagonal_[i] = std::sqrt(
254254+ u * (std::min)((std::max)(jtj_(i, i), min_diagonal), max_diagonal));
255255+ jtj_regularized_(i, i) += lm_diagonal_[i] * lm_diagonal_[i];
256256+ }
257257+258258+ // TODO(sameeragarwal): Check for failure and deal with it.
259259+ linear_solver_.compute(jtj_regularized_);
260260+ lm_step_ = linear_solver_.solve(g_);
261261+ dx_ = jacobi_scaling_.asDiagonal() * lm_step_;
262262+263263+ // Adding parameter_tolerance to x.norm() ensures that this
264264+ // works if x is near zero.
265265+ const Scalar parameter_tolerance =
266266+ options.parameter_tolerance *
267267+ (x.norm() + options.parameter_tolerance);
268268+ if (dx_.norm() < parameter_tolerance) {
269269+ summary.status = RELATIVE_STEP_SIZE_TOO_SMALL;
270270+ break;
271271+ }
272272+ x_new_ = x + dx_;
273273+274274+ // TODO(keir): Add proper handling of errors from user eval of cost
275275+ // functions.
276276+ function(&x_new_[0], &f_x_new_[0], nullptr);
277277+278278+ const Scalar cost_change = (2 * cost_ - f_x_new_.squaredNorm());
279279+ // TODO(sameeragarwal): Better more numerically stable evaluation.
280280+ const Scalar model_cost_change = lm_step_.dot(2 * g_ - jtj_ * lm_step_);
281281+282282+ // rho is the ratio of the actual reduction in error to the reduction
283283+ // in error that would be obtained if the problem was linear. See [1]
284284+ // for details.
285285+ Scalar rho(cost_change / model_cost_change);
286286+ if (rho > 0) {
287287+ // Accept the Levenberg-Marquardt step because the linear
288288+ // model fits well.
289289+ x = x_new_;
290290+291291+ if (std::abs(cost_change) < options.function_tolerance) {
292292+ cost_ = f_x_new_.squaredNorm() / 2;
293293+ summary.status = COST_CHANGE_TOO_SMALL;
294294+ break;
295295+ }
296296+297297+ // TODO(sameeragarwal): Deal with failure.
298298+ Update(function, x);
299299+ if (summary.gradient_max_norm < options.gradient_tolerance) {
300300+ summary.status = GRADIENT_TOO_SMALL;
301301+ break;
302302+ }
303303+304304+ if (cost_ < options.cost_threshold) {
305305+ summary.status = COST_TOO_SMALL;
306306+ break;
307307+ }
308308+309309+ Scalar tmp = Scalar(2 * rho - 1);
310310+ u = u * (std::max)(Scalar(1 / 3.), Scalar(1) - tmp * tmp * tmp);
311311+ v = 2;
312312+313313+ } else {
314314+ // Reject the update because either the normal equations failed to solve
315315+ // or the local linear model was not good (rho < 0).
316316+317317+ // Additionally if the cost change is too small, then terminate.
318318+ if (std::abs(cost_change) < options.function_tolerance) {
319319+ // Terminate
320320+ summary.status = COST_CHANGE_TOO_SMALL;
321321+ break;
322322+ }
323323+324324+ // Reduce the size of the trust region.
325325+ u *= v;
326326+ v *= 2;
327327+ }
328328+ }
329329+330330+ summary.final_cost = cost_;
331331+ return summary;
332332+ }
333333+334334+ Options options;
335335+ Summary summary;
336336+337337+ private:
338338+ // Preallocate everything, including temporary storage needed for solving the
339339+ // linear system. This allows reusing the intermediate storage across solves.
340340+ LinearSolver linear_solver_;
341341+ Scalar cost_;
342342+ Parameters dx_, x_new_, g_, jacobi_scaling_, lm_diagonal_, lm_step_;
343343+ Eigen::Matrix<Scalar, NUM_RESIDUALS, 1> residuals_, f_x_new_;
344344+ Eigen::Matrix<Scalar, NUM_RESIDUALS, NUM_PARAMETERS> jacobian_;
345345+ Eigen::Matrix<Scalar, NUM_PARAMETERS, NUM_PARAMETERS> jtj_, jtj_regularized_;
346346+347347+ // The following definitions are needed for template metaprogramming.
348348+ template <bool Condition, typename T>
349349+ struct enable_if;
350350+351351+ template <typename T>
352352+ struct enable_if<true, T> {
353353+ using type = T;
354354+ };
355355+356356+ // The number of parameters and residuals are dynamically sized.
357357+ template <int R, int P>
358358+ typename enable_if<(R == Eigen::Dynamic && P == Eigen::Dynamic), void>::type
359359+ Initialize(const Function& function) {
360360+ Initialize(function.NumResiduals(), function.NumParameters());
361361+ }
362362+363363+ // The number of parameters is dynamically sized and the number of
364364+ // residuals is statically sized.
365365+ template <int R, int P>
366366+ typename enable_if<(R == Eigen::Dynamic && P != Eigen::Dynamic), void>::type
367367+ Initialize(const Function& function) {
368368+ Initialize(function.NumResiduals(), P);
369369+ }
370370+371371+ // The number of parameters is statically sized and the number of
372372+ // residuals is dynamically sized.
373373+ template <int R, int P>
374374+ typename enable_if<(R != Eigen::Dynamic && P == Eigen::Dynamic), void>::type
375375+ Initialize(const Function& function) {
376376+ Initialize(R, function.NumParameters());
377377+ }
378378+379379+ // The number of parameters and residuals are statically sized.
380380+ template <int R, int P>
381381+ typename enable_if<(R != Eigen::Dynamic && P != Eigen::Dynamic), void>::type
382382+ Initialize(const Function& /* function */) {}
383383+384384+ void Initialize(int num_residuals, int num_parameters) {
385385+ dx_.resize(num_parameters);
386386+ x_new_.resize(num_parameters);
387387+ g_.resize(num_parameters);
388388+ jacobi_scaling_.resize(num_parameters);
389389+ lm_diagonal_.resize(num_parameters);
390390+ lm_step_.resize(num_parameters);
391391+ residuals_.resize(num_residuals);
392392+ f_x_new_.resize(num_residuals);
393393+ jacobian_.resize(num_residuals, num_parameters);
394394+ jtj_.resize(num_parameters, num_parameters);
395395+ jtj_regularized_.resize(num_parameters, num_parameters);
396396+ }
397397+};
398398+399399+} // namespace ceres
400400+401401+#endif // CERES_PUBLIC_TINY_SOLVER_H_
···11+// SPDX-License-Identifier: BSD-3-Clause
22+// Ceres Solver - A fast non-linear least squares minimizer
33+// Copyright 2019 Google Inc. All rights reserved.
44+// http://ceres-solver.org/
55+//
66+// Redistribution and use in source and binary forms, with or without
77+// modification, are permitted provided that the following conditions are met:
88+//
99+// * Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// * Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the documentation
1313+// and/or other materials provided with the distribution.
1414+// * Neither the name of Google Inc. nor the names of its contributors may be
1515+// used to endorse or promote products derived from this software without
1616+// specific prior written permission.
1717+//
1818+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2121+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2222+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2323+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2424+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2525+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2626+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828+// POSSIBILITY OF SUCH DAMAGE.
2929+//
3030+// Author: mierle@gmail.com (Keir Mierle)
3131+//
3232+// WARNING WARNING WARNING
3333+// WARNING WARNING WARNING Tiny solver is experimental and will change.
3434+// WARNING WARNING WARNING
3535+3636+#ifndef CERES_PUBLIC_TINY_SOLVER_AUTODIFF_FUNCTION_H_
3737+#define CERES_PUBLIC_TINY_SOLVER_AUTODIFF_FUNCTION_H_
3838+3939+#include <memory>
4040+#include <type_traits>
4141+4242+#include "Eigen/Core"
4343+#include "tinyceres/jet.hpp"
4444+4545+//!@todo Really?
4646+const double kImpossibleValue = 1e302;
4747+4848+namespace ceres {
4949+5050+// An adapter around autodiff-style CostFunctors to enable easier use of
5151+// TinySolver. See the example below showing how to use it:
5252+//
5353+// // Example for cost functor with static residual size.
5454+// // Same as an autodiff cost functor, but taking only 1 parameter.
5555+// struct MyFunctor {
5656+// template<typename T>
5757+// bool operator()(const T* const parameters, T* residuals) const {
5858+// const T& x = parameters[0];
5959+// const T& y = parameters[1];
6060+// const T& z = parameters[2];
6161+// residuals[0] = x + 2.*y + 4.*z;
6262+// residuals[1] = y * z;
6363+// return true;
6464+// }
6565+// };
6666+//
6767+// typedef TinySolverAutoDiffFunction<MyFunctor, 2, 3>
6868+// AutoDiffFunction;
6969+//
7070+// MyFunctor my_functor;
7171+// AutoDiffFunction f(my_functor);
7272+//
7373+// Vec3 x = ...;
7474+// TinySolver<AutoDiffFunction> solver;
7575+// solver.Solve(f, &x);
7676+//
7777+// // Example for cost functor with dynamic residual size.
7878+// // NumResiduals() supplies dynamic size of residuals.
7979+// // Same functionality as in tiny_solver.h but with autodiff.
8080+// struct MyFunctorWithDynamicResiduals {
8181+// int NumResiduals() const {
8282+// return 2;
8383+// }
8484+//
8585+// template<typename T>
8686+// bool operator()(const T* const parameters, T* residuals) const {
8787+// const T& x = parameters[0];
8888+// const T& y = parameters[1];
8989+// const T& z = parameters[2];
9090+// residuals[0] = x + static_cast<T>(2.)*y + static_cast<T>(4.)*z;
9191+// residuals[1] = y * z;
9292+// return true;
9393+// }
9494+// };
9595+//
9696+// typedef TinySolverAutoDiffFunction<MyFunctorWithDynamicResiduals,
9797+// Eigen::Dynamic,
9898+// 3>
9999+// AutoDiffFunctionWithDynamicResiduals;
100100+//
101101+// MyFunctorWithDynamicResiduals my_functor_dyn;
102102+// AutoDiffFunctionWithDynamicResiduals f(my_functor_dyn);
103103+//
104104+// Vec3 x = ...;
105105+// TinySolver<AutoDiffFunctionWithDynamicResiduals> solver;
106106+// solver.Solve(f, &x);
107107+//
108108+// WARNING: The cost function adapter is not thread safe.
109109+template <typename CostFunctor,
110110+ int kNumResiduals,
111111+ int kNumParameters,
112112+ typename T = double>
113113+class TinySolverAutoDiffFunction {
114114+ public:
115115+ // This class needs to have an Eigen aligned operator new as it contains
116116+ // as a member a Jet type, which itself has a fixed-size Eigen type as member.
117117+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW
118118+119119+ explicit TinySolverAutoDiffFunction(const CostFunctor& cost_functor)
120120+ : cost_functor_(cost_functor) {
121121+ Initialize<kNumResiduals>(cost_functor);
122122+ }
123123+124124+ using Scalar = T;
125125+ enum {
126126+ NUM_PARAMETERS = kNumParameters,
127127+ NUM_RESIDUALS = kNumResiduals,
128128+ };
129129+130130+ // This is similar to AutoDifferentiate(), but since there is only one
131131+ // parameter block it is easier to inline to avoid overhead.
132132+ bool operator()(const T* parameters, T* residuals, T* jacobian) const {
133133+ if (jacobian == nullptr) {
134134+ // No jacobian requested, so just directly call the cost function with
135135+ // doubles, skipping jets and derivatives.
136136+ return cost_functor_(parameters, residuals);
137137+ }
138138+ // Initialize the input jets with passed parameters.
139139+ for (int i = 0; i < kNumParameters; ++i) {
140140+ jet_parameters_[i].a = parameters[i]; // Scalar part.
141141+ jet_parameters_[i].v.setZero(); // Derivative part.
142142+ jet_parameters_[i].v[i] = T(1.0);
143143+ }
144144+145145+ // Initialize the output jets such that we can detect user errors.
146146+ for (int i = 0; i < num_residuals_; ++i) {
147147+ jet_residuals_[i].a = kImpossibleValue;
148148+ jet_residuals_[i].v.setConstant(kImpossibleValue);
149149+ }
150150+151151+ // Execute the cost function, but with jets to find the derivative.
152152+ if (!cost_functor_(jet_parameters_, jet_residuals_.data())) {
153153+ return false;
154154+ }
155155+156156+ // Copy the jacobian out of the derivative part of the residual jets.
157157+ Eigen::Map<Eigen::Matrix<T, kNumResiduals, kNumParameters>> jacobian_matrix(
158158+ jacobian, num_residuals_, kNumParameters);
159159+ for (int r = 0; r < num_residuals_; ++r) {
160160+ residuals[r] = jet_residuals_[r].a;
161161+ // Note that while this looks like a fast vectorized write, in practice it
162162+ // unfortunately thrashes the cache since the writes to the column-major
163163+ // jacobian are strided (e.g. rows are non-contiguous).
164164+ jacobian_matrix.row(r) = jet_residuals_[r].v;
165165+ }
166166+ return true;
167167+ }
168168+169169+ int NumResiduals() const {
170170+ return num_residuals_; // Set by Initialize.
171171+ }
172172+173173+ private:
174174+ const CostFunctor& cost_functor_;
175175+176176+ // The number of residuals at runtime.
177177+ // This will be overridden if NUM_RESIDUALS == Eigen::Dynamic.
178178+ int num_residuals_ = kNumResiduals;
179179+180180+ // To evaluate the cost function with jets, temporary storage is needed. These
181181+ // are the buffers that are used during evaluation; parameters for the input,
182182+ // and jet_residuals_ are where the final cost and derivatives end up.
183183+ //
184184+ // Since this buffer is used for evaluation, the adapter is not thread safe.
185185+ using JetType = Jet<T, kNumParameters>;
186186+ mutable JetType jet_parameters_[kNumParameters];
187187+ // Eigen::Matrix serves as static or dynamic container.
188188+ mutable Eigen::Matrix<JetType, kNumResiduals, 1> jet_residuals_;
189189+190190+ // The number of residuals is dynamically sized and the number of
191191+ // parameters is statically sized.
192192+ template <int R>
193193+ typename std::enable_if<(R == Eigen::Dynamic), void>::type Initialize(
194194+ const CostFunctor& function) {
195195+ jet_residuals_.resize(function.NumResiduals());
196196+ num_residuals_ = function.NumResiduals();
197197+ }
198198+199199+ // The number of parameters and residuals are statically sized.
200200+ template <int R>
201201+ typename std::enable_if<(R != Eigen::Dynamic), void>::type Initialize(
202202+ const CostFunctor& /* function */) {
203203+ num_residuals_ = kNumResiduals;
204204+ }
205205+};
206206+207207+} // namespace ceres
208208+209209+#endif // CERES_PUBLIC_TINY_SOLVER_AUTODIFF_FUNCTION_H_