Add fmax/fmin overloads for scalars Comparison operators are defined for Jet-scalar and scalar-Jet. Add corresponding overloads for fmax and fmix so users don't have to convert to Jet only to access these functions, plus it can potentially save an unnecessary conversion. Change-Id: I20cc2d5874935e019d4ecb2d6a8c2ae631c8a4d3
diff --git a/include/ceres/jet.h b/include/ceres/jet.h index 50aac1b..99cff79 100644 --- a/include/ceres/jet.h +++ b/include/ceres/jet.h
@@ -687,11 +687,27 @@ inline Jet<T, N> fmax(const Jet<T, N>& x, const Jet<T, N>& y) { return x < y ? y : x; } +template <typename T, int N> +inline Jet<T, N> fmax(const Jet<T, N>& x, const T& y) { + return x < y ? Jet<T, N>{y} : x; +} +template <typename T, int N> +inline Jet<T, N> fmax(const T& x, const Jet<T, N>& y) { + return x < y ? y : Jet<T, N>{x}; +} template <typename T, int N> inline Jet<T, N> fmin(const Jet<T, N>& x, const Jet<T, N>& y) { return y < x ? y : x; } +template <typename T, int N> +inline Jet<T, N> fmin(const Jet<T, N>& x, const T& y) { + return y < x ? Jet<T, N>{y} : x; +} +template <typename T, int N> +inline Jet<T, N> fmin(const T& x, const Jet<T, N>& y) { + return y < x ? y : Jet<T, N>{x}; +} // erf is defined as an integral that cannot be expressed analytically // however, the derivative is trivial to compute
diff --git a/internal/ceres/jet_test.cc b/internal/ceres/jet_test.cc index c65f68d..00a1227 100644 --- a/internal/ceres/jet_test.cc +++ b/internal/ceres/jet_test.cc
@@ -736,12 +736,62 @@ VL << "z = " << z; ExpectJetsClose(x, z); } + { + J z = fmax(y, x); + VL << "z = " << z; + ExpectJetsClose(x, z); + } + { + J z = fmax(x, y.a); + VL << "z = " << z; + ExpectJetsClose(x, z); + } + { + J z = fmax(y, x.a); + VL << "z = " << z; + ExpectJetsClose(J{x.a}, z); + } + { + J z = fmax(x.a, y); + VL << "z = " << z; + ExpectJetsClose(J{x.a}, z); + } + { + J z = fmax(y.a, x); + VL << "z = " << z; + ExpectJetsClose(x, z); + } { J z = fmin(x, y); VL << "z = " << z; ExpectJetsClose(y, z); } + { + J z = fmin(y, x); + VL << "z = " << z; + ExpectJetsClose(y, z); + } + { + J z = fmin(x, y.a); + VL << "z = " << z; + ExpectJetsClose(J{y.a}, z); + } + { + J z = fmin(y, x.a); + VL << "z = " << z; + ExpectJetsClose(y, z); + } + { + J z = fmin(x.a, y); + VL << "z = " << z; + ExpectJetsClose(y, z); + } + { + J z = fmin(y.a, x); + VL << "z = " << z; + ExpectJetsClose(J{y.a}, z); + } } TEST(Jet, JetsInEigenMatrices) {