Fix Jet numeric traits and unary plus

Unary plus returns a reference that dangles for a temporary operand.
numeric_limits reports max_exponent10 as min_exponent10 and lacks
has_denorm_loss, and NumTraits fixes dummy_precision at 1e-12 whatever
the scalar type. Return by value and mirror the limits and precision of
the scalar type. Drop noalias from coefficient-wise compound
assignments, rename constructor parameters that shadow the members, and
share the NaN derivatives of pow for an integer exponent.

Change-Id: Ica19e6b1c30e5baf786712d8b6d981d922907906
diff --git a/include/ceres/jet.h b/include/ceres/jet.h
index d2ded9b..2219023 100644
--- a/include/ceres/jet.h
+++ b/include/ceres/jet.h
@@ -236,24 +236,25 @@
   // to be passed in without being fully evaluated until
   // they are assigned to v
   template <typename Derived>
-  EIGEN_STRONG_INLINE Jet(const T& a, const Eigen::DenseBase<Derived>& v)
-      : a(a), v(v) {}
+  EIGEN_STRONG_INLINE Jet(const T& value,
+                          const Eigen::DenseBase<Derived>& infinitesimal)
+      : a(value), v(infinitesimal) {}
 
   // Compound operators
   EIGEN_STRONG_INLINE Jet<T, N>& operator+=(const Jet<T, N>& y) {
     a += y.a;
-    v.noalias() += y.v;
+    v += y.v;
     return *this;
   }
 
   EIGEN_STRONG_INLINE Jet<T, N>& operator-=(const Jet<T, N>& y) {
     a -= y.a;
-    v.noalias() -= y.v;
+    v -= y.v;
     return *this;
   }
 
   EIGEN_STRONG_INLINE Jet<T, N>& operator*=(const Jet<T, N>& y) {
-    v.noalias() = a * y.v + v * y.a;
+    v = a * y.v + v * y.a;
     a *= y.a;
     return *this;
   }
@@ -261,7 +262,7 @@
   EIGEN_STRONG_INLINE Jet<T, N>& operator/=(const Jet<T, N>& y) {
     const T y_a_inverse = T(1.0) / y.a;
     a *= y_a_inverse;
-    v.noalias() = (v - a * y.v) * y_a_inverse;
+    v = (v - a * y.v) * y_a_inverse;
     return *this;
   }
 
@@ -302,7 +303,7 @@
 
 // Unary +
 template <typename T, int N>
-EIGEN_STRONG_INLINE Jet<T, N> const& operator+(const Jet<T, N>& f) {
+EIGEN_STRONG_INLINE Jet<T, N> operator+(const Jet<T, N>& f) {
   return f;
 }
 
@@ -1292,6 +1293,23 @@
   return Jet<T, N>(norm(f.a), (T(2.0) * f.a) * f.v);
 }
 
+namespace internal {
+
+// Replaces each derivative of result by NaN where the corresponding derivative
+// of the integer exponent g is nonzero, since any change of g leaves the real
+// numbers for a negative base.
+template <typename T, int N>
+EIGEN_STRONG_INLINE void SetNaNWhereExponentVaries(Jet<T, N>& result,
+                                                   const Jet<T, N>& g) {
+  for (int i = 0; i < N; ++i) {
+    if (fpclassify(g.v[i]) != FP_ZERO) {
+      result.v[i] = std::numeric_limits<T>::quiet_NaN();
+    }
+  }
+}
+
+}  // namespace internal
+
 // pow -- base is a differentiable function, exponent is a constant.
 // (a+da)^p ~= a^p + p*a^(p-1) da
 template <typename T, int N>
@@ -1321,12 +1339,7 @@
   } else {
     if (f < 0 && g == floor(g.a)) {  // Handle case 3.
       result = Jet<T, N>(pow(f, g.a));
-      for (int i = 0; i < N; i++) {
-        if (fpclassify(g.v[i]) != FP_ZERO) {
-          // Return a NaN when g.v != 0.
-          result.v[i] = std::numeric_limits<T>::quiet_NaN();
-        }
-      }
+      internal::SetNaNWhereExponentVaries(result, g);
     } else {
       // Handle case 1.
       T const tmp = pow(f, g.a);
@@ -1390,12 +1403,7 @@
       // Handle cases 7 and 8.
       T const tmp = g.a * pow(f.a, g.a - T(1.0));
       result = Jet<T, N>(pow(f.a, g.a), tmp * f.v);
-      for (int i = 0; i < N; i++) {
-        if (fpclassify(g.v[i]) != FP_ZERO) {
-          // Return a NaN when g.v != 0.
-          result.v[i] = T(std::numeric_limits<double>::quiet_NaN());
-        }
-      }
+      internal::SetNaNWhereExponentVaries(result, g);
     } else {
       // Handle the remaining cases. For cases 4,5,6,9 we allow the log()
       // function to generate -HUGE_VAL or NaN, since those cases result in a
@@ -1442,12 +1450,13 @@
   static constexpr bool is_bounded = std::numeric_limits<T>::is_bounded;
   static constexpr bool is_modulo = std::numeric_limits<T>::is_modulo;
 
-  // has_denorm (and has_denorm_loss, not defined for Jet) has been deprecated
-  // in C++23. However, without an intent to remove the declaration. Disable
-  // deprecation warnings temporarily just for the corresponding symbols.
+  // C++23 deprecates has_denorm and has_denorm_loss without removing them.
+  // Disable deprecation warnings just for the corresponding symbols.
   CERES_DISABLE_DEPRECATED_WARNING
   static constexpr std::float_denorm_style has_denorm =
       std::numeric_limits<T>::has_denorm;
+  static constexpr bool has_denorm_loss =
+      std::numeric_limits<T>::has_denorm_loss;
   CERES_RESTORE_DEPRECATED_WARNING
   static constexpr std::float_round_style round_style =
       std::numeric_limits<T>::round_style;
@@ -1457,7 +1466,7 @@
   static constexpr int max_digits10 = std::numeric_limits<T>::max_digits10;
   static constexpr int radix = std::numeric_limits<T>::radix;
   static constexpr int min_exponent = std::numeric_limits<T>::min_exponent;
-  static constexpr int min_exponent10 = std::numeric_limits<T>::max_exponent10;
+  static constexpr int min_exponent10 = std::numeric_limits<T>::min_exponent10;
   static constexpr int max_exponent = std::numeric_limits<T>::max_exponent;
   static constexpr int max_exponent10 = std::numeric_limits<T>::max_exponent10;
   static constexpr bool traps = std::numeric_limits<T>::traps;
@@ -1510,7 +1519,7 @@
   using Literal = ceres::Jet<T, N>;
 
   static typename ceres::Jet<T, N> dummy_precision() {
-    return ceres::Jet<T, N>(1e-12);
+    return ceres::Jet<T, N>(NumTraits<T>::dummy_precision());
   }
 
   static inline Real epsilon() {
diff --git a/internal/ceres/jet_test.cc b/internal/ceres/jet_test.cc
index 12851f2..dea4b70 100644
--- a/internal/ceres/jet_test.cc
+++ b/internal/ceres/jet_test.cc
@@ -34,7 +34,10 @@
 #include <algorithm>
 #include <cfenv>
 #include <cmath>
+#include <limits>
 #include <string>
+#include <type_traits>
+#include <utility>
 
 #include "absl/strings/str_format.h"
 #include "absl/types/span.h"
@@ -86,6 +89,13 @@
   return RelativelyNear(expected, kTolerance);
 }
 
+// Matches a Jet whose parts are each equal to those of expected.
+::testing::Matcher<J> JetEq(const J& expected) {
+  return ::testing::AllOf(
+      ::testing::Field("a", &J::a, ::testing::Eq(expected.a)),
+      ::testing::Field("v", &J::v, MatrixEq(expected.v)));
+}
+
 // Describes the operands and the result of pow for tracing failures.
 template <typename Base, typename Exponent>
 std::string DescribePow(const Base& a, const Exponent& b, const J& c) {
@@ -996,6 +1006,31 @@
   }
 }
 
+static_assert(std::is_same_v<decltype(+std::declval<const J&>()), J>,
+              "Unary plus must return a Jet by value, since a reference "
+              "dangles when the operand is a temporary.");
+
+static_assert(std::numeric_limits<J>::min_exponent10 ==
+                  std::numeric_limits<double>::min_exponent10,
+              "numeric_limits<Jet<T, N>> must mirror numeric_limits<T>.");
+
+CERES_DISABLE_DEPRECATED_WARNING
+static_assert(std::numeric_limits<J>::has_denorm_loss ==
+                  std::numeric_limits<double>::has_denorm_loss,
+              "numeric_limits<Jet<T, N>> must mirror numeric_limits<T>.");
+CERES_RESTORE_DEPRECATED_WARNING
+
+TEST(Jet, DummyPrecisionFollowsScalar) {
+  using JetFloat = Jet<float, 1>;
+  EXPECT_THAT(Eigen::NumTraits<JetFloat>::dummy_precision(),
+              ::testing::Field(
+                  "a",
+                  &JetFloat::a,
+                  ::testing::Eq(Eigen::NumTraits<float>::dummy_precision())));
+  EXPECT_THAT(Eigen::NumTraits<J>::dummy_precision(),
+              JetEq(J(Eigen::NumTraits<double>::dummy_precision())));
+}
+
 TEST(Jet, JetsInEigenMatrices) {
   J x = MakeJet(2.3, -2.7, 1e-3);
   J y = MakeJet(1.7, 0.5, 1e+2);