Divide Jets directly

Dividing a Jet multiplies by the reciprocal of the divisor and thus
rounds twice. The quotient of Jets then differs from the same quotient
of doubles, so 49 / 49 yields 0.99999999999999989 instead of 1 and a
functor evaluated with Jets disagrees with its evaluation with doubles.
Callers cannot opt out of this rounding, whereas the reciprocal form
stays available by writing it explicitly. Divide the scalar part and
the derivatives directly, accepting 1.3 to 4 times slower division.

Change-Id: I932b55cba1714ed192ab2645961feaf20c4216a6
diff --git a/docs/source/version_history.rst b/docs/source/version_history.rst
index 4166943..aa7c945 100644
--- a/docs/source/version_history.rst
+++ b/docs/source/version_history.rst
@@ -91,6 +91,11 @@
    a deprecation warning; link against ``Ceres::ceres`` instead. (Sergiu
    Deitsch)
 #. Support for 32-bit MinGW has been removed. (Sergiu Deitsch)
+#. Dividing a ``Jet`` divides its scalar part and derivatives directly instead
+   of multiplying them by the reciprocal of the divisor, so that the quotient
+   rounds like scalar division. Division is slower as a result. For a scalar
+   divisor ``s``, multiplying by its reciprocal, as in ``x * (1.0 / s)``,
+   trades this accuracy for the previous speed. (Sergiu Deitsch)
 
 
 Bug Fixes & Minor Changes
diff --git a/include/ceres/jet.h b/include/ceres/jet.h
index 5090b92..7b36e36 100644
--- a/include/ceres/jet.h
+++ b/include/ceres/jet.h
@@ -260,9 +260,10 @@
   }
 
   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 = (v - a * y.v) * y_a_inverse;
+    // Copy the divisor since y may alias *this.
+    const T y_a = y.a;
+    a /= y_a;
+    v = (v - a * y.v) / y_a;
     return *this;
   }
 
@@ -284,9 +285,10 @@
   }
 
   EIGEN_STRONG_INLINE Jet<T, N>& operator/=(const T& s) {
-    const T s_inverse = T(1.0) / s;
-    a *= s_inverse;
-    v *= s_inverse;
+    // Copy the divisor since s may alias a.
+    const T divisor = s;
+    a /= divisor;
+    v /= divisor;
     return *this;
   }
 
@@ -374,16 +376,11 @@
 template <typename T, int N>
 EIGEN_STRONG_INLINE Jet<T, N> operator/(const Jet<T, N>& f,
                                         const Jet<T, N>& g) {
-  // This uses:
-  //
-  //   a + u   (a + u)(b - v)   (a + u)(b - v)
-  //   ----- = -------------- = --------------
-  //   b + v   (b + v)(b - v)        b^2
-  //
-  // which holds because v*v = 0.
-  const T g_a_inverse = T(1.0) / g.a;
-  const T f_a_by_g_a = f.a * g_a_inverse;
-  return Jet<T, N>(f_a_by_g_a, (f.v - f_a_by_g_a * g.v) * g_a_inverse);
+  // Dividing directly instead of multiplying by the reciprocal of y keeps the
+  // scalar part identical to the scalar quotient. The derivatives use
+  // (1 / y) dx − (x / y²) dy = (dx − (x / y) dy) / y.
+  const T quotient = f.a / g.a;
+  return Jet<T, N>(quotient, (f.v - quotient * g.v) / g.a);
 }
 
 // Binary / with a scalar: s / x
@@ -398,8 +395,7 @@
 // Binary / with a scalar: x / s
 template <typename T, int N>
 EIGEN_STRONG_INLINE Jet<T, N> operator/(const Jet<T, N>& f, T s) {
-  const T s_inverse = T(1.0) / s;
-  return Jet<T, N>(f.a * s_inverse, f.v * s_inverse);
+  return Jet<T, N>(f.a / s, f.v / s);
 }
 
 // Binary comparison operators for both scalars and jets. At least one of the
diff --git a/internal/ceres/jet_test.cc b/internal/ceres/jet_test.cc
index 108257f..6e41dc6 100644
--- a/internal/ceres/jet_test.cc
+++ b/internal/ceres/jet_test.cc
@@ -1020,6 +1020,33 @@
   }
 }
 
+TEST(Jet, DivisionRoundsLikeScalarDivision) {
+  // 49 * (1 / 49) rounds to 0.99999999999999989, while 49 / 49 is exactly 1.
+  constexpr double kOperand = 49.0;
+  const J numerator = MakeJet(kOperand, 1.0, 0.0);
+  const J quotient = MakeJet(kOperand / kOperand, 1.0 / kOperand, 0.0);
+
+  EXPECT_THAT(numerator / J(kOperand), JetEq(quotient));
+  EXPECT_THAT(numerator / kOperand, JetEq(quotient));
+  {
+    J c = numerator;
+    c /= J(kOperand);
+    EXPECT_THAT(c, JetEq(quotient));
+  }
+  {
+    J c = numerator;
+    c /= kOperand;
+    EXPECT_THAT(c, JetEq(quotient));
+  }
+}
+
+TEST(Jet, CompoundDivisionByOwnScalarPart) {
+  // The divisor aliases the scalar part that the division overwrites.
+  J x = MakeJet(4.0, 2.0, -6.0);
+  x /= x.a;
+  EXPECT_THAT(x, JetEq(MakeJet(1.0, 0.5, -1.5)));
+}
+
 TEST(Jet, CbrtExtremeMagnitudes) {
   // Verify cbrt avoids f.a * f.a overflow and underflow.
   {