Blame include/ceres/jet.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: keir@google.com (Keir Mierle)
Packit ea1746
//
Packit ea1746
// A simple implementation of N-dimensional dual numbers, for automatically
Packit ea1746
// computing exact derivatives of functions.
Packit ea1746
//
Packit ea1746
// While a complete treatment of the mechanics of automatic differentation is
Packit ea1746
// beyond the scope of this header (see
Packit ea1746
// http://en.wikipedia.org/wiki/Automatic_differentiation for details), the
Packit ea1746
// basic idea is to extend normal arithmetic with an extra element, "e," often
Packit ea1746
// denoted with the greek symbol epsilon, such that e != 0 but e^2 = 0. Dual
Packit ea1746
// numbers are extensions of the real numbers analogous to complex numbers:
Packit ea1746
// whereas complex numbers augment the reals by introducing an imaginary unit i
Packit ea1746
// such that i^2 = -1, dual numbers introduce an "infinitesimal" unit e such
Packit ea1746
// that e^2 = 0. Dual numbers have two components: the "real" component and the
Packit ea1746
// "infinitesimal" component, generally written as x + y*e. Surprisingly, this
Packit ea1746
// leads to a convenient method for computing exact derivatives without needing
Packit ea1746
// to manipulate complicated symbolic expressions.
Packit ea1746
//
Packit ea1746
// For example, consider the function
Packit ea1746
//
Packit ea1746
//   f(x) = x^2 ,
Packit ea1746
//
Packit ea1746
// evaluated at 10. Using normal arithmetic, f(10) = 100, and df/dx(10) = 20.
Packit ea1746
// Next, augument 10 with an infinitesimal to get:
Packit ea1746
//
Packit ea1746
//   f(10 + e) = (10 + e)^2
Packit ea1746
//             = 100 + 2 * 10 * e + e^2
Packit ea1746
//             = 100 + 20 * e       -+-
Packit ea1746
//                     --            |
Packit ea1746
//                     |             +--- This is zero, since e^2 = 0
Packit ea1746
//                     |
Packit ea1746
//                     +----------------- This is df/dx!
Packit ea1746
//
Packit ea1746
// Note that the derivative of f with respect to x is simply the infinitesimal
Packit ea1746
// component of the value of f(x + e). So, in order to take the derivative of
Packit ea1746
// any function, it is only necessary to replace the numeric "object" used in
Packit ea1746
// the function with one extended with infinitesimals. The class Jet, defined in
Packit ea1746
// this header, is one such example of this, where substitution is done with
Packit ea1746
// templates.
Packit ea1746
//
Packit ea1746
// To handle derivatives of functions taking multiple arguments, different
Packit ea1746
// infinitesimals are used, one for each variable to take the derivative of. For
Packit ea1746
// example, consider a scalar function of two scalar parameters x and y:
Packit ea1746
//
Packit ea1746
//   f(x, y) = x^2 + x * y
Packit ea1746
//
Packit ea1746
// Following the technique above, to compute the derivatives df/dx and df/dy for
Packit ea1746
// f(1, 3) involves doing two evaluations of f, the first time replacing x with
Packit ea1746
// x + e, the second time replacing y with y + e.
Packit ea1746
//
Packit ea1746
// For df/dx:
Packit ea1746
//
Packit ea1746
//   f(1 + e, y) = (1 + e)^2 + (1 + e) * 3
Packit ea1746
//               = 1 + 2 * e + 3 + 3 * e
Packit ea1746
//               = 4 + 5 * e
Packit ea1746
//
Packit ea1746
//               --> df/dx = 5
Packit ea1746
//
Packit ea1746
// For df/dy:
Packit ea1746
//
Packit ea1746
//   f(1, 3 + e) = 1^2 + 1 * (3 + e)
Packit ea1746
//               = 1 + 3 + e
Packit ea1746
//               = 4 + e
Packit ea1746
//
Packit ea1746
//               --> df/dy = 1
Packit ea1746
//
Packit ea1746
// To take the gradient of f with the implementation of dual numbers ("jets") in
Packit ea1746
// this file, it is necessary to create a single jet type which has components
Packit ea1746
// for the derivative in x and y, and passing them to a templated version of f:
Packit ea1746
//
Packit ea1746
//   template<typename T>
Packit ea1746
//   T f(const T &x, const T &y) {
Packit ea1746
//     return x * x + x * y;
Packit ea1746
//   }
Packit ea1746
//
Packit ea1746
//   // The "2" means there should be 2 dual number components.
Packit ea1746
//   Jet<double, 2> x(0);  // Pick the 0th dual number for x.
Packit ea1746
//   Jet<double, 2> y(1);  // Pick the 1st dual number for y.
Packit ea1746
//   Jet<double, 2> z = f(x, y);
Packit ea1746
//
Packit ea1746
//   LOG(INFO) << "df/dx = " << z.v[0]
Packit ea1746
//             << "df/dy = " << z.v[1];
Packit ea1746
//
Packit ea1746
// Most users should not use Jet objects directly; a wrapper around Jet objects,
Packit ea1746
// which makes computing the derivative, gradient, or jacobian of templated
Packit ea1746
// functors simple, is in autodiff.h. Even autodiff.h should not be used
Packit ea1746
// directly; instead autodiff_cost_function.h is typically the file of interest.
Packit ea1746
//
Packit ea1746
// For the more mathematically inclined, this file implements first-order
Packit ea1746
// "jets". A 1st order jet is an element of the ring
Packit ea1746
//
Packit ea1746
//   T[N] = T[t_1, ..., t_N] / (t_1, ..., t_N)^2
Packit ea1746
//
Packit ea1746
// which essentially means that each jet consists of a "scalar" value 'a' from T
Packit ea1746
// and a 1st order perturbation vector 'v' of length N:
Packit ea1746
//
Packit ea1746
//   x = a + \sum_i v[i] t_i
Packit ea1746
//
Packit ea1746
// A shorthand is to write an element as x = a + u, where u is the pertubation.
Packit ea1746
// Then, the main point about the arithmetic of jets is that the product of
Packit ea1746
// perturbations is zero:
Packit ea1746
//
Packit ea1746
//   (a + u) * (b + v) = ab + av + bu + uv
Packit ea1746
//                     = ab + (av + bu) + 0
Packit ea1746
//
Packit ea1746
// which is what operator* implements below. Addition is simpler:
Packit ea1746
//
Packit ea1746
//   (a + u) + (b + v) = (a + b) + (u + v).
Packit ea1746
//
Packit ea1746
// The only remaining question is how to evaluate the function of a jet, for
Packit ea1746
// which we use the chain rule:
Packit ea1746
//
Packit ea1746
//   f(a + u) = f(a) + f'(a) u
Packit ea1746
//
Packit ea1746
// where f'(a) is the (scalar) derivative of f at a.
Packit ea1746
//
Packit ea1746
// By pushing these things through sufficiently and suitably templated
Packit ea1746
// functions, we can do automatic differentiation. Just be sure to turn on
Packit ea1746
// function inlining and common-subexpression elimination, or it will be very
Packit ea1746
// slow!
Packit ea1746
//
Packit ea1746
// WARNING: Most Ceres users should not directly include this file or know the
Packit ea1746
// details of how jets work. Instead the suggested method for automatic
Packit ea1746
// derivatives is to use autodiff_cost_function.h, which is a wrapper around
Packit ea1746
// both jets.h and autodiff.h to make taking derivatives of cost functions for
Packit ea1746
// use in Ceres easier.
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_JET_H_
Packit ea1746
#define CERES_PUBLIC_JET_H_
Packit ea1746
Packit ea1746
#include <cmath>
Packit ea1746
#include <iosfwd>
Packit ea1746
#include <iostream>  // NOLINT
Packit ea1746
#include <limits>
Packit ea1746
#include <string>
Packit ea1746
Packit ea1746
#include "Eigen/Core"
Packit ea1746
#include "ceres/fpclassify.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
template <typename T, int N>
Packit ea1746
struct Jet {
Packit ea1746
  enum { DIMENSION = N };
Packit ea1746
Packit ea1746
  // Default-construct "a" because otherwise this can lead to false errors about
Packit ea1746
  // uninitialized uses when other classes relying on default constructed T
Packit ea1746
  // (where T is a Jet<T, N>). This usually only happens in opt mode. Note that
Packit ea1746
  // the C++ standard mandates that e.g. default constructed doubles are
Packit ea1746
  // initialized to 0.0; see sections 8.5 of the C++03 standard.
Packit ea1746
  Jet() : a() {
Packit ea1746
    v.setZero();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Constructor from scalar: a + 0.
Packit ea1746
  explicit Jet(const T& value) {
Packit ea1746
    a = value;
Packit ea1746
    v.setZero();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Constructor from scalar plus variable: a + t_i.
Packit ea1746
  Jet(const T& value, int k) {
Packit ea1746
    a = value;
Packit ea1746
    v.setZero();
Packit ea1746
    v[k] = T(1.0);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Constructor from scalar and vector part
Packit ea1746
  // The use of Eigen::DenseBase allows Eigen expressions
Packit ea1746
  // to be passed in without being fully evaluated until
Packit ea1746
  // they are assigned to v
Packit ea1746
  template<typename Derived>
Packit ea1746
  EIGEN_STRONG_INLINE Jet(const T& a, const Eigen::DenseBase<Derived> &v)
Packit ea1746
      : a(a), v(v) {
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Compound operators
Packit ea1746
  Jet<T, N>& operator+=(const Jet<T, N> &y) {
Packit ea1746
    *this = *this + y;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator-=(const Jet<T, N> &y) {
Packit ea1746
    *this = *this - y;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator*=(const Jet<T, N> &y) {
Packit ea1746
    *this = *this * y;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator/=(const Jet<T, N> &y) {
Packit ea1746
    *this = *this / y;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Compound with scalar operators.
Packit ea1746
  Jet<T, N>& operator+=(const T& s) {
Packit ea1746
    *this = *this + s;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator-=(const T& s) {
Packit ea1746
    *this = *this - s;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator*=(const T& s) {
Packit ea1746
    *this = *this * s;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  Jet<T, N>& operator/=(const T& s) {
Packit ea1746
    *this = *this / s;
Packit ea1746
    return *this;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // The scalar part.
Packit ea1746
  T a;
Packit ea1746
Packit ea1746
  // The infinitesimal part.
Packit ea1746
  //
Packit ea1746
  // We allocate Jets on the stack and other places they might not be aligned
Packit ea1746
  // to X(=16 [SSE], 32 [AVX] etc)-byte boundaries, which would prevent the safe
Packit ea1746
  // use of vectorisation.  If we have C++11, we can specify the alignment.
Packit ea1746
  // However, the standard gives wide lattitude as to what alignments are valid,
Packit ea1746
  // and it might be that the maximum supported alignment *guaranteed* to be
Packit ea1746
  // supported is < 16, in which case we do not specify an alignment, as this
Packit ea1746
  // implies the host is not a modern x86 machine.  If using < C++11, we cannot
Packit ea1746
  // specify alignment.
Packit ea1746
#ifndef CERES_USE_CXX11
Packit ea1746
  // Without >= C++11, we cannot specify the alignment so fall back to safe,
Packit ea1746
  // unvectorised version.
Packit ea1746
  Eigen::Matrix<T, N, 1, Eigen::DontAlign> v;
Packit ea1746
#else
Packit ea1746
  // Enable vectorisation iff the maximum supported scalar alignment is >=
Packit ea1746
  // 16 bytes, as this is the minimum required by Eigen for any vectorisation.
Packit ea1746
  //
Packit ea1746
  // NOTE: It might be the case that we could get >= 16-byte alignment even if
Packit ea1746
  //       kMaxAlignBytes < 16.  However we can't guarantee that this
Packit ea1746
  //       would happen (and it should not for any modern x86 machine) and if it
Packit ea1746
  //       didn't, we could get misaligned Jets.
Packit ea1746
  static constexpr int kAlignOrNot =
Packit ea1746
      16 <= ::ceres::port_constants::kMaxAlignBytes
Packit ea1746
            ? Eigen::AutoAlign : Eigen::DontAlign;
Packit ea1746
#if defined(EIGEN_MAX_ALIGN_BYTES)
Packit ea1746
  // Eigen >= 3.3 supports AVX & FMA instructions that require 32-byte alignment
Packit ea1746
  // (greater for AVX512).  Rather than duplicating the detection logic, use
Packit ea1746
  // Eigen's macro for the alignment size.
Packit ea1746
  //
Packit ea1746
  // NOTE: EIGEN_MAX_ALIGN_BYTES can be > 16 (e.g. 32 for AVX), even though
Packit ea1746
  //       kMaxAlignBytes will max out at 16.  We are therefore relying on
Packit ea1746
  //       Eigen's detection logic to ensure that this does not result in
Packit ea1746
  //       misaligned Jets.
Packit ea1746
#define CERES_JET_ALIGN_BYTES EIGEN_MAX_ALIGN_BYTES
Packit ea1746
#else
Packit ea1746
  // Eigen < 3.3 only supported 16-byte alignment.
Packit ea1746
#define CERES_JET_ALIGN_BYTES 16
Packit ea1746
#endif
Packit ea1746
  // Default to the native alignment if 16-byte alignment is not guaranteed to
Packit ea1746
  // be supported.  We cannot use alignof(T) as if we do, GCC 4.8 complains that
Packit ea1746
  // the alignment 'is not an integer constant', although Clang accepts it.
Packit ea1746
  static constexpr size_t kAlignment = kAlignOrNot == Eigen::AutoAlign
Packit ea1746
            ? CERES_JET_ALIGN_BYTES : alignof(double);
Packit ea1746
#undef CERES_JET_ALIGN_BYTES
Packit ea1746
  alignas(kAlignment) Eigen::Matrix<T, N, 1, kAlignOrNot> v;
Packit ea1746
#endif
Packit ea1746
};
Packit ea1746
Packit ea1746
// Unary +
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> const& operator+(const Jet<T, N>& f) {
Packit ea1746
  return f;
Packit ea1746
}
Packit ea1746
Packit ea1746
// TODO(keir): Try adding __attribute__((always_inline)) to these functions to
Packit ea1746
// see if it causes a performance increase.
Packit ea1746
Packit ea1746
// Unary -
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator-(const Jet<T, N>&f) {
Packit ea1746
  return Jet<T, N>(-f.a, -f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary +
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator+(const Jet<T, N>& f,
Packit ea1746
                    const Jet<T, N>& g) {
Packit ea1746
  return Jet<T, N>(f.a + g.a, f.v + g.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary + with a scalar: x + s
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator+(const Jet<T, N>& f, T s) {
Packit ea1746
  return Jet<T, N>(f.a + s, f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary + with a scalar: s + x
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator+(T s, const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(f.a + s, f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary -
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator-(const Jet<T, N>& f,
Packit ea1746
                    const Jet<T, N>& g) {
Packit ea1746
  return Jet<T, N>(f.a - g.a, f.v - g.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary - with a scalar: x - s
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator-(const Jet<T, N>& f, T s) {
Packit ea1746
  return Jet<T, N>(f.a - s, f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary - with a scalar: s - x
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator-(T s, const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(s - f.a, -f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary *
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator*(const Jet<T, N>& f,
Packit ea1746
                    const Jet<T, N>& g) {
Packit ea1746
  return Jet<T, N>(f.a * g.a, f.a * g.v + f.v * g.a);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary * with a scalar: x * s
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator*(const Jet<T, N>& f, T s) {
Packit ea1746
  return Jet<T, N>(f.a * s, f.v * s);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary * with a scalar: s * x
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator*(T s, const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(f.a * s, f.v * s);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary /
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator/(const Jet<T, N>& f,
Packit ea1746
                    const Jet<T, N>& g) {
Packit ea1746
  // This uses:
Packit ea1746
  //
Packit ea1746
  //   a + u   (a + u)(b - v)   (a + u)(b - v)
Packit ea1746
  //   ----- = -------------- = --------------
Packit ea1746
  //   b + v   (b + v)(b - v)        b^2
Packit ea1746
  //
Packit ea1746
  // which holds because v*v = 0.
Packit ea1746
  const T g_a_inverse = T(1.0) / g.a;
Packit ea1746
  const T f_a_by_g_a = f.a * g_a_inverse;
Packit ea1746
  return Jet<T, N>(f.a * g_a_inverse, (f.v - f_a_by_g_a * g.v) * g_a_inverse);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary / with a scalar: s / x
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator/(T s, const Jet<T, N>& g) {
Packit ea1746
  const T minus_s_g_a_inverse2 = -s / (g.a * g.a);
Packit ea1746
  return Jet<T, N>(s / g.a, g.v * minus_s_g_a_inverse2);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary / with a scalar: x / s
Packit ea1746
template<typename T, int N> inline
Packit ea1746
Jet<T, N> operator/(const Jet<T, N>& f, T s) {
Packit ea1746
  const T s_inverse = T(1.0) / s;
Packit ea1746
  return Jet<T, N>(f.a * s_inverse, f.v * s_inverse);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Binary comparison operators for both scalars and jets.
Packit ea1746
#define CERES_DEFINE_JET_COMPARISON_OPERATOR(op) \
Packit ea1746
template<typename T, int N> inline \
Packit ea1746
bool operator op(const Jet<T, N>& f, const Jet<T, N>& g) { \
Packit ea1746
  return f.a op g.a; \
Packit ea1746
} \
Packit ea1746
template<typename T, int N> inline \
Packit ea1746
bool operator op(const T& s, const Jet<T, N>& g) { \
Packit ea1746
  return s op g.a; \
Packit ea1746
} \
Packit ea1746
template<typename T, int N> inline \
Packit ea1746
bool operator op(const Jet<T, N>& f, const T& s) { \
Packit ea1746
  return f.a op s; \
Packit ea1746
}
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( <  )  // NOLINT
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( <= )  // NOLINT
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( >  )  // NOLINT
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( >= )  // NOLINT
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( == )  // NOLINT
Packit ea1746
CERES_DEFINE_JET_COMPARISON_OPERATOR( != )  // NOLINT
Packit ea1746
#undef CERES_DEFINE_JET_COMPARISON_OPERATOR
Packit ea1746
Packit ea1746
// Pull some functions from namespace std.
Packit ea1746
//
Packit ea1746
// This is necessary because we want to use the same name (e.g. 'sqrt') for
Packit ea1746
// double-valued and Jet-valued functions, but we are not allowed to put
Packit ea1746
// Jet-valued functions inside namespace std.
Packit ea1746
//
Packit ea1746
// TODO(keir): Switch to "using".
Packit ea1746
inline double abs     (double x) { return std::abs(x);      }
Packit ea1746
inline double log     (double x) { return std::log(x);      }
Packit ea1746
inline double exp     (double x) { return std::exp(x);      }
Packit ea1746
inline double sqrt    (double x) { return std::sqrt(x);     }
Packit ea1746
inline double cos     (double x) { return std::cos(x);      }
Packit ea1746
inline double acos    (double x) { return std::acos(x);     }
Packit ea1746
inline double sin     (double x) { return std::sin(x);      }
Packit ea1746
inline double asin    (double x) { return std::asin(x);     }
Packit ea1746
inline double tan     (double x) { return std::tan(x);      }
Packit ea1746
inline double atan    (double x) { return std::atan(x);     }
Packit ea1746
inline double sinh    (double x) { return std::sinh(x);     }
Packit ea1746
inline double cosh    (double x) { return std::cosh(x);     }
Packit ea1746
inline double tanh    (double x) { return std::tanh(x);     }
Packit ea1746
inline double floor   (double x) { return std::floor(x);    }
Packit ea1746
inline double ceil    (double x) { return std::ceil(x);     }
Packit ea1746
inline double pow  (double x, double y) { return std::pow(x, y);   }
Packit ea1746
inline double atan2(double y, double x) { return std::atan2(y, x); }
Packit ea1746
Packit ea1746
// In general, f(a + h) ~= f(a) + f'(a) h, via the chain rule.
Packit ea1746
Packit ea1746
// abs(x + h) ~= x + h or -(x + h)
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> abs(const Jet<T, N>& f) {
Packit ea1746
  return f.a < T(0.0) ? -f : f;
Packit ea1746
}
Packit ea1746
Packit ea1746
// log(a + h) ~= log(a) + h / a
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> log(const Jet<T, N>& f) {
Packit ea1746
  const T a_inverse = T(1.0) / f.a;
Packit ea1746
  return Jet<T, N>(log(f.a), f.v * a_inverse);
Packit ea1746
}
Packit ea1746
Packit ea1746
// exp(a + h) ~= exp(a) + exp(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> exp(const Jet<T, N>& f) {
Packit ea1746
  const T tmp = exp(f.a);
Packit ea1746
  return Jet<T, N>(tmp, tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// sqrt(a + h) ~= sqrt(a) + h / (2 sqrt(a))
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> sqrt(const Jet<T, N>& f) {
Packit ea1746
  const T tmp = sqrt(f.a);
Packit ea1746
  const T two_a_inverse = T(1.0) / (T(2.0) * tmp);
Packit ea1746
  return Jet<T, N>(tmp, f.v * two_a_inverse);
Packit ea1746
}
Packit ea1746
Packit ea1746
// cos(a + h) ~= cos(a) - sin(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> cos(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(cos(f.a), - sin(f.a) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// acos(a + h) ~= acos(a) - 1 / sqrt(1 - a^2) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> acos(const Jet<T, N>& f) {
Packit ea1746
  const T tmp = - T(1.0) / sqrt(T(1.0) - f.a * f.a);
Packit ea1746
  return Jet<T, N>(acos(f.a), tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// sin(a + h) ~= sin(a) + cos(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> sin(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(sin(f.a), cos(f.a) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// asin(a + h) ~= asin(a) + 1 / sqrt(1 - a^2) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> asin(const Jet<T, N>& f) {
Packit ea1746
  const T tmp = T(1.0) / sqrt(T(1.0) - f.a * f.a);
Packit ea1746
  return Jet<T, N>(asin(f.a), tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// tan(a + h) ~= tan(a) + (1 + tan(a)^2) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> tan(const Jet<T, N>& f) {
Packit ea1746
  const T tan_a = tan(f.a);
Packit ea1746
  const T tmp = T(1.0) + tan_a * tan_a;
Packit ea1746
  return Jet<T, N>(tan_a, tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// atan(a + h) ~= atan(a) + 1 / (1 + a^2) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> atan(const Jet<T, N>& f) {
Packit ea1746
  const T tmp = T(1.0) / (T(1.0) + f.a * f.a);
Packit ea1746
  return Jet<T, N>(atan(f.a), tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// sinh(a + h) ~= sinh(a) + cosh(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> sinh(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(sinh(f.a), cosh(f.a) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// cosh(a + h) ~= cosh(a) + sinh(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> cosh(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(cosh(f.a), sinh(f.a) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// tanh(a + h) ~= tanh(a) + (1 - tanh(a)^2) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> tanh(const Jet<T, N>& f) {
Packit ea1746
  const T tanh_a = tanh(f.a);
Packit ea1746
  const T tmp = T(1.0) - tanh_a * tanh_a;
Packit ea1746
  return Jet<T, N>(tanh_a, tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// The floor function should be used with extreme care as this operation will
Packit ea1746
// result in a zero derivative which provides no information to the solver.
Packit ea1746
//
Packit ea1746
// floor(a + h) ~= floor(a) + 0
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> floor(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(floor(f.a));
Packit ea1746
}
Packit ea1746
Packit ea1746
// The ceil function should be used with extreme care as this operation will
Packit ea1746
// result in a zero derivative which provides no information to the solver.
Packit ea1746
//
Packit ea1746
// ceil(a + h) ~= ceil(a) + 0
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> ceil(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(ceil(f.a));
Packit ea1746
}
Packit ea1746
Packit ea1746
// Bessel functions of the first kind with integer order equal to 0, 1, n.
Packit ea1746
//
Packit ea1746
// Microsoft has deprecated the j[0,1,n]() POSIX Bessel functions in favour of
Packit ea1746
// _j[0,1,n]().  Where available on MSVC, use _j[0,1,n]() to avoid deprecated
Packit ea1746
// function errors in client code (the specific warning is suppressed when
Packit ea1746
// Ceres itself is built).
Packit ea1746
inline double BesselJ0(double x) {
Packit ea1746
#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
Packit ea1746
  return _j0(x);
Packit ea1746
#else
Packit ea1746
  return j0(x);
Packit ea1746
#endif
Packit ea1746
}
Packit ea1746
inline double BesselJ1(double x) {
Packit ea1746
#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
Packit ea1746
  return _j1(x);
Packit ea1746
#else
Packit ea1746
  return j1(x);
Packit ea1746
#endif
Packit ea1746
}
Packit ea1746
inline double BesselJn(int n, double x) {
Packit ea1746
#if defined(CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
Packit ea1746
  return _jn(n, x);
Packit ea1746
#else
Packit ea1746
  return jn(n, x);
Packit ea1746
#endif
Packit ea1746
}
Packit ea1746
Packit ea1746
// For the formulae of the derivatives of the Bessel functions see the book:
Packit ea1746
// Olver, Lozier, Boisvert, Clark, NIST Handbook of Mathematical Functions,
Packit ea1746
// Cambridge University Press 2010.
Packit ea1746
//
Packit ea1746
// Formulae are also available at http://dlmf.nist.gov
Packit ea1746
Packit ea1746
// See formula http://dlmf.nist.gov/10.6#E3
Packit ea1746
// j0(a + h) ~= j0(a) - j1(a) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> BesselJ0(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(BesselJ0(f.a),
Packit ea1746
                   -BesselJ1(f.a) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// See formula http://dlmf.nist.gov/10.6#E1
Packit ea1746
// j1(a + h) ~= j1(a) + 0.5 ( j0(a) - j2(a) ) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> BesselJ1(const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(BesselJ1(f.a),
Packit ea1746
                   T(0.5) * (BesselJ0(f.a) - BesselJn(2, f.a)) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// See formula http://dlmf.nist.gov/10.6#E1
Packit ea1746
// j_n(a + h) ~= j_n(a) + 0.5 ( j_{n-1}(a) - j_{n+1}(a) ) h
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> BesselJn(int n, const Jet<T, N>& f) {
Packit ea1746
  return Jet<T, N>(BesselJn(n, f.a),
Packit ea1746
                   T(0.5) * (BesselJn(n - 1, f.a) - BesselJn(n + 1, f.a)) * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Jet Classification. It is not clear what the appropriate semantics are for
Packit ea1746
// these classifications. This picks that IsFinite and isnormal are "all"
Packit ea1746
// operations, i.e. all elements of the jet must be finite for the jet itself
Packit ea1746
// to be finite (or normal). For IsNaN and IsInfinite, the answer is less
Packit ea1746
// clear. This takes a "any" approach for IsNaN and IsInfinite such that if any
Packit ea1746
// part of a jet is nan or inf, then the entire jet is nan or inf. This leads
Packit ea1746
// to strange situations like a jet can be both IsInfinite and IsNaN, but in
Packit ea1746
// practice the "any" semantics are the most useful for e.g. checking that
Packit ea1746
// derivatives are sane.
Packit ea1746
Packit ea1746
// The jet is finite if all parts of the jet are finite.
Packit ea1746
template <typename T, int N> inline
Packit ea1746
bool IsFinite(const Jet<T, N>& f) {
Packit ea1746
  if (!IsFinite(f.a)) {
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
  for (int i = 0; i < N; ++i) {
Packit ea1746
    if (!IsFinite(f.v[i])) {
Packit ea1746
      return false;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
// The jet is infinite if any part of the jet is infinite.
Packit ea1746
template <typename T, int N> inline
Packit ea1746
bool IsInfinite(const Jet<T, N>& f) {
Packit ea1746
  if (IsInfinite(f.a)) {
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
  for (int i = 0; i < N; i++) {
Packit ea1746
    if (IsInfinite(f.v[i])) {
Packit ea1746
      return true;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  return false;
Packit ea1746
}
Packit ea1746
Packit ea1746
// The jet is NaN if any part of the jet is NaN.
Packit ea1746
template <typename T, int N> inline
Packit ea1746
bool IsNaN(const Jet<T, N>& f) {
Packit ea1746
  if (IsNaN(f.a)) {
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
  for (int i = 0; i < N; ++i) {
Packit ea1746
    if (IsNaN(f.v[i])) {
Packit ea1746
      return true;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  return false;
Packit ea1746
}
Packit ea1746
Packit ea1746
// The jet is normal if all parts of the jet are normal.
Packit ea1746
template <typename T, int N> inline
Packit ea1746
bool IsNormal(const Jet<T, N>& f) {
Packit ea1746
  if (!IsNormal(f.a)) {
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
  for (int i = 0; i < N; ++i) {
Packit ea1746
    if (!IsNormal(f.v[i])) {
Packit ea1746
      return false;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
// atan2(b + db, a + da) ~= atan2(b, a) + (- b da + a db) / (a^2 + b^2)
Packit ea1746
//
Packit ea1746
// In words: the rate of change of theta is 1/r times the rate of
Packit ea1746
// change of (x, y) in the positive angular direction.
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> atan2(const Jet<T, N>& g, const Jet<T, N>& f) {
Packit ea1746
  // Note order of arguments:
Packit ea1746
  //
Packit ea1746
  //   f = a + da
Packit ea1746
  //   g = b + db
Packit ea1746
Packit ea1746
  T const tmp = T(1.0) / (f.a * f.a + g.a * g.a);
Packit ea1746
  return Jet<T, N>(atan2(g.a, f.a), tmp * (- g.a * f.v + f.a * g.v));
Packit ea1746
}
Packit ea1746
Packit ea1746
Packit ea1746
// pow -- base is a differentiable function, exponent is a constant.
Packit ea1746
// (a+da)^p ~= a^p + p*a^(p-1) da
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> pow(const Jet<T, N>& f, double g) {
Packit ea1746
  T const tmp = g * pow(f.a, g - T(1.0));
Packit ea1746
  return Jet<T, N>(pow(f.a, g), tmp * f.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// pow -- base is a constant, exponent is a differentiable function.
Packit ea1746
// We have various special cases, see the comment for pow(Jet, Jet) for
Packit ea1746
// analysis:
Packit ea1746
//
Packit ea1746
// 1. For f > 0 we have: (f)^(g + dg) ~= f^g + f^g log(f) dg
Packit ea1746
//
Packit ea1746
// 2. For f == 0 and g > 0 we have: (f)^(g + dg) ~= f^g
Packit ea1746
//
Packit ea1746
// 3. For f < 0 and integer g we have: (f)^(g + dg) ~= f^g but if dg
Packit ea1746
// != 0, the derivatives are not defined and we return NaN.
Packit ea1746
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> pow(double f, const Jet<T, N>& g) {
Packit ea1746
  if (f == 0 && g.a > 0) {
Packit ea1746
    // Handle case 2.
Packit ea1746
    return Jet<T, N>(T(0.0));
Packit ea1746
  }
Packit ea1746
  if (f < 0 && g.a == floor(g.a)) {
Packit ea1746
    // Handle case 3.
Packit ea1746
    Jet<T, N> ret(pow(f, g.a));
Packit ea1746
    for (int i = 0; i < N; i++) {
Packit ea1746
      if (g.v[i] != T(0.0)) {
Packit ea1746
        // Return a NaN when g.v != 0.
Packit ea1746
        ret.v[i] = std::numeric_limits<T>::quiet_NaN();
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
    return ret;
Packit ea1746
  }
Packit ea1746
  // Handle case 1.
Packit ea1746
  T const tmp = pow(f, g.a);
Packit ea1746
  return Jet<T, N>(tmp, log(f) * tmp * g.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// pow -- both base and exponent are differentiable functions. This has a
Packit ea1746
// variety of special cases that require careful handling.
Packit ea1746
//
Packit ea1746
// 1. For f > 0:
Packit ea1746
//    (f + df)^(g + dg) ~= f^g + f^(g - 1) * (g * df + f * log(f) * dg)
Packit ea1746
//    The numerical evaluation of f * log(f) for f > 0 is well behaved, even for
Packit ea1746
//    extremely small values (e.g. 1e-99).
Packit ea1746
//
Packit ea1746
// 2. For f == 0 and g > 1: (f + df)^(g + dg) ~= 0
Packit ea1746
//    This cases is needed because log(0) can not be evaluated in the f > 0
Packit ea1746
//    expression. However the function f*log(f) is well behaved around f == 0
Packit ea1746
//    and its limit as f-->0 is zero.
Packit ea1746
//
Packit ea1746
// 3. For f == 0 and g == 1: (f + df)^(g + dg) ~= 0 + df
Packit ea1746
//
Packit ea1746
// 4. For f == 0 and 0 < g < 1: The value is finite but the derivatives are not.
Packit ea1746
//
Packit ea1746
// 5. For f == 0 and g < 0: The value and derivatives of f^g are not finite.
Packit ea1746
//
Packit ea1746
// 6. For f == 0 and g == 0: The C standard incorrectly defines 0^0 to be 1
Packit ea1746
//    "because there are applications that can exploit this definition". We
Packit ea1746
//    (arbitrarily) decree that derivatives here will be nonfinite, since that
Packit ea1746
//    is consistent with the behavior for f == 0, g < 0 and 0 < g < 1.
Packit ea1746
//    Practically any definition could have been justified because mathematical
Packit ea1746
//    consistency has been lost at this point.
Packit ea1746
//
Packit ea1746
// 7. For f < 0, g integer, dg == 0: (f + df)^(g + dg) ~= f^g + g * f^(g - 1) df
Packit ea1746
//    This is equivalent to the case where f is a differentiable function and g
Packit ea1746
//    is a constant (to first order).
Packit ea1746
//
Packit ea1746
// 8. For f < 0, g integer, dg != 0: The value is finite but the derivatives are
Packit ea1746
//    not, because any change in the value of g moves us away from the point
Packit ea1746
//    with a real-valued answer into the region with complex-valued answers.
Packit ea1746
//
Packit ea1746
// 9. For f < 0, g noninteger: The value and derivatives of f^g are not finite.
Packit ea1746
Packit ea1746
template <typename T, int N> inline
Packit ea1746
Jet<T, N> pow(const Jet<T, N>& f, const Jet<T, N>& g) {
Packit ea1746
  if (f.a == 0 && g.a >= 1) {
Packit ea1746
    // Handle cases 2 and 3.
Packit ea1746
    if (g.a > 1) {
Packit ea1746
      return Jet<T, N>(T(0.0));
Packit ea1746
    }
Packit ea1746
    return f;
Packit ea1746
  }
Packit ea1746
  if (f.a < 0 && g.a == floor(g.a)) {
Packit ea1746
    // Handle cases 7 and 8.
Packit ea1746
    T const tmp = g.a * pow(f.a, g.a - T(1.0));
Packit ea1746
    Jet<T, N> ret(pow(f.a, g.a), tmp * f.v);
Packit ea1746
    for (int i = 0; i < N; i++) {
Packit ea1746
      if (g.v[i] != T(0.0)) {
Packit ea1746
        // Return a NaN when g.v != 0.
Packit ea1746
        ret.v[i] = std::numeric_limits<T>::quiet_NaN();
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
    return ret;
Packit ea1746
  }
Packit ea1746
  // Handle the remaining cases. For cases 4,5,6,9 we allow the log() function
Packit ea1746
  // to generate -HUGE_VAL or NaN, since those cases result in a nonfinite
Packit ea1746
  // derivative.
Packit ea1746
  T const tmp1 = pow(f.a, g.a);
Packit ea1746
  T const tmp2 = g.a * pow(f.a, g.a - T(1.0));
Packit ea1746
  T const tmp3 = tmp1 * log(f.a);
Packit ea1746
  return Jet<T, N>(tmp1, tmp2 * f.v + tmp3 * g.v);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Define the helper functions Eigen needs to embed Jet types.
Packit ea1746
//
Packit ea1746
// NOTE(keir): machine_epsilon() and precision() are missing, because they don't
Packit ea1746
// work with nested template types (e.g. where the scalar is itself templated).
Packit ea1746
// Among other things, this means that decompositions of Jet's does not work,
Packit ea1746
// for example
Packit ea1746
//
Packit ea1746
//   Matrix<Jet<T, N> ... > A, x, b;
Packit ea1746
//   ...
Packit ea1746
//   A.solve(b, &x)
Packit ea1746
//
Packit ea1746
// does not work and will fail with a strange compiler error.
Packit ea1746
//
Packit ea1746
// TODO(keir): This is an Eigen 2.0 limitation that is lifted in 3.0. When we
Packit ea1746
// switch to 3.0, also add the rest of the specialization functionality.
Packit ea1746
template<typename T, int N> inline const Jet<T, N>& ei_conj(const Jet<T, N>& x) { return x;              }  // NOLINT
Packit ea1746
template<typename T, int N> inline const Jet<T, N>& ei_real(const Jet<T, N>& x) { return x;              }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_imag(const Jet<T, N>&  ) { return Jet<T, N>(0.0); }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_abs (const Jet<T, N>& x) { return fabs(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_abs2(const Jet<T, N>& x) { return x * x;          }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_sqrt(const Jet<T, N>& x) { return sqrt(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_exp (const Jet<T, N>& x) { return exp(x);         }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_log (const Jet<T, N>& x) { return log(x);         }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_sin (const Jet<T, N>& x) { return sin(x);         }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_cos (const Jet<T, N>& x) { return cos(x);         }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_tan (const Jet<T, N>& x) { return tan(x);         }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_atan(const Jet<T, N>& x) { return atan(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_sinh(const Jet<T, N>& x) { return sinh(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_cosh(const Jet<T, N>& x) { return cosh(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_tanh(const Jet<T, N>& x) { return tanh(x);        }  // NOLINT
Packit ea1746
template<typename T, int N> inline       Jet<T, N>  ei_pow (const Jet<T, N>& x, Jet<T, N> y) { return pow(x, y); }  // NOLINT
Packit ea1746
Packit ea1746
// Note: This has to be in the ceres namespace for argument dependent lookup to
Packit ea1746
// function correctly. Otherwise statements like CHECK_LE(x, 2.0) fail with
Packit ea1746
// strange compile errors.
Packit ea1746
template <typename T, int N>
Packit ea1746
inline std::ostream &operator<<(std::ostream &s, const Jet<T, N>& z) {
Packit ea1746
  s << "[" << z.a << " ; ";
Packit ea1746
  for (int i = 0; i < N; ++i) {
Packit ea1746
    s << z.v[i];
Packit ea1746
    if (i != N - 1) {
Packit ea1746
      s << ", ";
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  s << "]";
Packit ea1746
  return s;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
namespace Eigen {
Packit ea1746
Packit ea1746
// Creating a specialization of NumTraits enables placing Jet objects inside
Packit ea1746
// Eigen arrays, getting all the goodness of Eigen combined with autodiff.
Packit ea1746
template<typename T, int N>
Packit ea1746
struct NumTraits<ceres::Jet<T, N> > {
Packit ea1746
  typedef ceres::Jet<T, N> Real;
Packit ea1746
  typedef ceres::Jet<T, N> NonInteger;
Packit ea1746
  typedef ceres::Jet<T, N> Nested;
Packit ea1746
  typedef ceres::Jet<T, N> Literal;
Packit ea1746
Packit ea1746
  static typename ceres::Jet<T, N> dummy_precision() {
Packit ea1746
    return ceres::Jet<T, N>(1e-12);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  static inline Real epsilon() {
Packit ea1746
    return Real(std::numeric_limits<T>::epsilon());
Packit ea1746
  }
Packit ea1746
Packit ea1746
  static inline int digits10() { return NumTraits<T>::digits10(); }
Packit ea1746
Packit ea1746
  enum {
Packit ea1746
    IsComplex = 0,
Packit ea1746
    IsInteger = 0,
Packit ea1746
    IsSigned,
Packit ea1746
    ReadCost = 1,
Packit ea1746
    AddCost = 1,
Packit ea1746
    // For Jet types, multiplication is more expensive than addition.
Packit ea1746
    MulCost = 3,
Packit ea1746
    HasFloatingPoint = 1,
Packit ea1746
    RequireInitialization = 1
Packit ea1746
  };
Packit ea1746
Packit ea1746
  template<bool Vectorized>
Packit ea1746
  struct Div {
Packit ea1746
    enum {
Packit ea1746
#if defined(EIGEN_VECTORIZE_AVX)
Packit ea1746
      AVX = true,
Packit ea1746
#else
Packit ea1746
      AVX = false,
Packit ea1746
#endif
Packit ea1746
Packit ea1746
      // Assuming that for Jets, division is as expensive as
Packit ea1746
      // multiplication.
Packit ea1746
      Cost = 3
Packit ea1746
    };
Packit ea1746
  };
Packit ea1746
};
Packit ea1746
Packit ea1746
#if EIGEN_VERSION_AT_LEAST(3, 3, 0)
Packit ea1746
// Specifying the return type of binary operations between Jets and scalar types
Packit ea1746
// allows you to perform matrix/array operations with Eigen matrices and arrays
Packit ea1746
// such as addition, subtraction, multiplication, and division where one Eigen
Packit ea1746
// matrix/array is of type Jet and the other is a scalar type. This improves
Packit ea1746
// performance by using the optimized scalar-to-Jet binary operations but
Packit ea1746
// is only available on Eigen versions >= 3.3
Packit ea1746
template <typename BinaryOp, typename T, int N>
Packit ea1746
struct ScalarBinaryOpTraits<ceres::Jet<T, N>, T, BinaryOp> {
Packit ea1746
  typedef ceres::Jet<T, N> ReturnType;
Packit ea1746
};
Packit ea1746
template <typename BinaryOp, typename T, int N>
Packit ea1746
struct ScalarBinaryOpTraits<T, ceres::Jet<T, N>, BinaryOp> {
Packit ea1746
  typedef ceres::Jet<T, N> ReturnType;
Packit ea1746
};
Packit ea1746
#endif  // EIGEN_VERSION_AT_LEAST(3, 3, 0)
Packit ea1746
Packit ea1746
}  // namespace Eigen
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_JET_H_