blob: 74ce1e9dd53ce8c7ba1167b29be062d35543c31c [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: keir@google.com (Keir Mierle)
30//
31// A simple implementation of N-dimensional dual numbers, for automatically
32// computing exact derivatives of functions.
33//
34// While a complete treatment of the mechanics of automatic differentation is
35// beyond the scope of this header (see
36// http://en.wikipedia.org/wiki/Automatic_differentiation for details), the
37// basic idea is to extend normal arithmetic with an extra element, "e," often
38// denoted with the greek symbol epsilon, such that e != 0 but e^2 = 0. Dual
39// numbers are extensions of the real numbers analogous to complex numbers:
40// whereas complex numbers augment the reals by introducing an imaginary unit i
41// such that i^2 = -1, dual numbers introduce an "infinitesimal" unit e such
42// that e^2 = 0. Dual numbers have two components: the "real" component and the
43// "infinitesimal" component, generally written as x + y*e. Surprisingly, this
44// leads to a convenient method for computing exact derivatives without needing
45// to manipulate complicated symbolic expressions.
46//
47// For example, consider the function
48//
49// f(x) = x^2 ,
50//
51// evaluated at 10. Using normal arithmetic, f(10) = 100, and df/dx(10) = 20.
52// Next, augument 10 with an infinitesimal to get:
53//
54// f(10 + e) = (10 + e)^2
55// = 100 + 2 * 10 * e + e^2
56// = 100 + 20 * e -+-
57// -- |
58// | +--- This is zero, since e^2 = 0
59// |
60// +----------------- This is df/dx!
61//
62// Note that the derivative of f with respect to x is simply the infinitesimal
63// component of the value of f(x + e). So, in order to take the derivative of
64// any function, it is only necessary to replace the numeric "object" used in
65// the function with one extended with infinitesimals. The class Jet, defined in
66// this header, is one such example of this, where substitution is done with
67// templates.
68//
69// To handle derivatives of functions taking multiple arguments, different
70// infinitesimals are used, one for each variable to take the derivative of. For
71// example, consider a scalar function of two scalar parameters x and y:
72//
73// f(x, y) = x^2 + x * y
74//
75// Following the technique above, to compute the derivatives df/dx and df/dy for
76// f(1, 3) involves doing two evaluations of f, the first time replacing x with
77// x + e, the second time replacing y with y + e.
78//
79// For df/dx:
80//
81// f(1 + e, y) = (1 + e)^2 + (1 + e) * 3
82// = 1 + 2 * e + 3 + 3 * e
83// = 4 + 5 * e
84//
85// --> df/dx = 5
86//
87// For df/dy:
88//
89// f(1, 3 + e) = 1^2 + 1 * (3 + e)
90// = 1 + 3 + e
91// = 4 + e
92//
93// --> df/dy = 1
94//
95// To take the gradient of f with the implementation of dual numbers ("jets") in
96// this file, it is necessary to create a single jet type which has components
97// for the derivative in x and y, and passing them to a templated version of f:
98//
99// template<typename T>
100// T f(const T &x, const T &y) {
101// return x * x + x * y;
102// }
103//
104// // The "2" means there should be 2 dual number components.
105// Jet<double, 2> x(0); // Pick the 0th dual number for x.
106// Jet<double, 2> y(1); // Pick the 1st dual number for y.
107// Jet<double, 2> z = f(x, y);
108//
Sameer Agarwal8c155d52013-11-08 08:04:44 -0800109// LOG(INFO) << "df/dx = " << z.a[0]
110// << "df/dy = " << z.a[1];
Keir Mierle8ebb0732012-04-30 23:09:08 -0700111//
112// Most users should not use Jet objects directly; a wrapper around Jet objects,
113// which makes computing the derivative, gradient, or jacobian of templated
114// functors simple, is in autodiff.h. Even autodiff.h should not be used
115// directly; instead autodiff_cost_function.h is typically the file of interest.
116//
117// For the more mathematically inclined, this file implements first-order
118// "jets". A 1st order jet is an element of the ring
119//
120// T[N] = T[t_1, ..., t_N] / (t_1, ..., t_N)^2
121//
122// which essentially means that each jet consists of a "scalar" value 'a' from T
123// and a 1st order perturbation vector 'v' of length N:
124//
125// x = a + \sum_i v[i] t_i
126//
127// A shorthand is to write an element as x = a + u, where u is the pertubation.
128// Then, the main point about the arithmetic of jets is that the product of
129// perturbations is zero:
130//
131// (a + u) * (b + v) = ab + av + bu + uv
132// = ab + (av + bu) + 0
133//
134// which is what operator* implements below. Addition is simpler:
135//
136// (a + u) + (b + v) = (a + b) + (u + v).
137//
138// The only remaining question is how to evaluate the function of a jet, for
139// which we use the chain rule:
140//
141// f(a + u) = f(a) + f'(a) u
142//
143// where f'(a) is the (scalar) derivative of f at a.
144//
145// By pushing these things through sufficiently and suitably templated
146// functions, we can do automatic differentiation. Just be sure to turn on
147// function inlining and common-subexpression elimination, or it will be very
148// slow!
149//
150// WARNING: Most Ceres users should not directly include this file or know the
151// details of how jets work. Instead the suggested method for automatic
152// derivatives is to use autodiff_cost_function.h, which is a wrapper around
153// both jets.h and autodiff.h to make taking derivatives of cost functions for
154// use in Ceres easier.
155
156#ifndef CERES_PUBLIC_JET_H_
157#define CERES_PUBLIC_JET_H_
158
159#include <cmath>
160#include <iosfwd>
161#include <iostream> // NOLINT
Sameer Agarwala0c282a2014-08-24 22:19:03 -0700162#include <limits>
Keir Mierle8ebb0732012-04-30 23:09:08 -0700163#include <string>
164
165#include "Eigen/Core"
Keir Mierle58ede272012-06-24 17:23:57 -0700166#include "ceres/fpclassify.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -0700167
168namespace ceres {
169
170template <typename T, int N>
171struct Jet {
172 enum { DIMENSION = N };
173
174 // Default-construct "a" because otherwise this can lead to false errors about
175 // uninitialized uses when other classes relying on default constructed T
176 // (where T is a Jet<T, N>). This usually only happens in opt mode. Note that
177 // the C++ standard mandates that e.g. default constructed doubles are
178 // initialized to 0.0; see sections 8.5 of the C++03 standard.
Keir Mierle8e68ff32012-08-14 14:40:42 -0700179 Jet() : a() {
180 v.setZero();
181 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700182
183 // Constructor from scalar: a + 0.
184 explicit Jet(const T& value) {
185 a = value;
186 v.setZero();
187 }
188
189 // Constructor from scalar plus variable: a + t_i.
190 Jet(const T& value, int k) {
191 a = value;
192 v.setZero();
193 v[k] = T(1.0);
194 }
195
Tim Langlois91087e82013-09-11 15:12:06 -0400196 // Constructor from scalar and vector part
197 // The use of Eigen::DenseBase allows Eigen expressions
198 // to be passed in without being fully evaluated until
199 // they are assigned to v
200 template<typename Derived>
Sameer Agarwala0c282a2014-08-24 22:19:03 -0700201 EIGEN_STRONG_INLINE Jet(const T& a, const Eigen::DenseBase<Derived> &v)
202 : a(a), v(v) {
Tim Langlois91087e82013-09-11 15:12:06 -0400203 }
204
Keir Mierle8ebb0732012-04-30 23:09:08 -0700205 // Compound operators
206 Jet<T, N>& operator+=(const Jet<T, N> &y) {
207 *this = *this + y;
208 return *this;
209 }
210
211 Jet<T, N>& operator-=(const Jet<T, N> &y) {
212 *this = *this - y;
213 return *this;
214 }
215
216 Jet<T, N>& operator*=(const Jet<T, N> &y) {
217 *this = *this * y;
218 return *this;
219 }
220
221 Jet<T, N>& operator/=(const Jet<T, N> &y) {
222 *this = *this / y;
223 return *this;
224 }
225
Sameer Agarwal45ccb512012-07-15 16:32:17 -0700226 // The scalar part.
227 T a;
Sameer Agarwaleb893402012-06-17 08:55:01 -0700228
Sameer Agarwal45ccb512012-07-15 16:32:17 -0700229 // The infinitesimal part.
Sameer Agarwaleb893402012-06-17 08:55:01 -0700230 //
Sameer Agarwal45ccb512012-07-15 16:32:17 -0700231 // Note the Eigen::DontAlign bit is needed here because this object
232 // gets allocated on the stack and as part of other arrays and
233 // structs. Forcing the right alignment there is the source of much
234 // pain and suffering. Even if that works, passing Jets around to
Sameer Agarwal1d7c4922012-07-16 20:40:25 -0700235 // functions by value has problems because the C++ ABI does not
Sameer Agarwal45ccb512012-07-15 16:32:17 -0700236 // guarantee alignment for function arguments.
237 //
238 // Setting the DontAlign bit prevents Eigen from using SSE for the
239 // various operations on Jets. This is a small performance penalty
240 // since the AutoDiff code will still expose much of the code as
241 // statically sized loops to the compiler. But given the subtle
242 // issues that arise due to alignment, especially when dealing with
243 // multiple platforms, it seems to be a trade off worth making.
244 Eigen::Matrix<T, N, 1, Eigen::DontAlign> v;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700245};
246
247// Unary +
248template<typename T, int N> inline
249Jet<T, N> const& operator+(const Jet<T, N>& f) {
250 return f;
251}
252
253// TODO(keir): Try adding __attribute__((always_inline)) to these functions to
254// see if it causes a performance increase.
255
256// Unary -
257template<typename T, int N> inline
258Jet<T, N> operator-(const Jet<T, N>&f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400259 return Jet<T, N>(-f.a, -f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700260}
261
262// Binary +
263template<typename T, int N> inline
264Jet<T, N> operator+(const Jet<T, N>& f,
265 const Jet<T, N>& g) {
Tim Langlois91087e82013-09-11 15:12:06 -0400266 return Jet<T, N>(f.a + g.a, f.v + g.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700267}
268
269// Binary + with a scalar: x + s
270template<typename T, int N> inline
271Jet<T, N> operator+(const Jet<T, N>& f, T s) {
Tim Langlois91087e82013-09-11 15:12:06 -0400272 return Jet<T, N>(f.a + s, f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700273}
274
275// Binary + with a scalar: s + x
276template<typename T, int N> inline
277Jet<T, N> operator+(T s, const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400278 return Jet<T, N>(f.a + s, f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700279}
280
281// Binary -
282template<typename T, int N> inline
283Jet<T, N> operator-(const Jet<T, N>& f,
284 const Jet<T, N>& g) {
Tim Langlois91087e82013-09-11 15:12:06 -0400285 return Jet<T, N>(f.a - g.a, f.v - g.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700286}
287
288// Binary - with a scalar: x - s
289template<typename T, int N> inline
290Jet<T, N> operator-(const Jet<T, N>& f, T s) {
Tim Langlois91087e82013-09-11 15:12:06 -0400291 return Jet<T, N>(f.a - s, f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700292}
293
294// Binary - with a scalar: s - x
295template<typename T, int N> inline
296Jet<T, N> operator-(T s, const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400297 return Jet<T, N>(s - f.a, -f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700298}
299
300// Binary *
301template<typename T, int N> inline
302Jet<T, N> operator*(const Jet<T, N>& f,
303 const Jet<T, N>& g) {
Tim Langlois91087e82013-09-11 15:12:06 -0400304 return Jet<T, N>(f.a * g.a, f.a * g.v + f.v * g.a);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700305}
306
307// Binary * with a scalar: x * s
308template<typename T, int N> inline
309Jet<T, N> operator*(const Jet<T, N>& f, T s) {
Tim Langlois91087e82013-09-11 15:12:06 -0400310 return Jet<T, N>(f.a * s, f.v * s);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700311}
312
313// Binary * with a scalar: s * x
314template<typename T, int N> inline
315Jet<T, N> operator*(T s, const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400316 return Jet<T, N>(f.a * s, f.v * s);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700317}
318
319// Binary /
320template<typename T, int N> inline
321Jet<T, N> operator/(const Jet<T, N>& f,
322 const Jet<T, N>& g) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700323 // This uses:
324 //
325 // a + u (a + u)(b - v) (a + u)(b - v)
326 // ----- = -------------- = --------------
327 // b + v (b + v)(b - v) b^2
328 //
329 // which holds because v*v = 0.
Sameer Agarwal487250e2013-04-05 14:20:37 -0700330 const T g_a_inverse = T(1.0) / g.a;
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700331 const T f_a_by_g_a = f.a * g_a_inverse;
Tim Langlois91087e82013-09-11 15:12:06 -0400332 return Jet<T, N>(f.a * g_a_inverse, (f.v - f_a_by_g_a * g.v) * g_a_inverse);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700333}
334
335// Binary / with a scalar: s / x
336template<typename T, int N> inline
337Jet<T, N> operator/(T s, const Jet<T, N>& g) {
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700338 const T minus_s_g_a_inverse2 = -s / (g.a * g.a);
Tim Langlois91087e82013-09-11 15:12:06 -0400339 return Jet<T, N>(s / g.a, g.v * minus_s_g_a_inverse2);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700340}
341
342// Binary / with a scalar: x / s
343template<typename T, int N> inline
344Jet<T, N> operator/(const Jet<T, N>& f, T s) {
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700345 const T s_inverse = 1.0 / s;
Tim Langlois91087e82013-09-11 15:12:06 -0400346 return Jet<T, N>(f.a * s_inverse, f.v * s_inverse);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700347}
348
349// Binary comparison operators for both scalars and jets.
350#define CERES_DEFINE_JET_COMPARISON_OPERATOR(op) \
351template<typename T, int N> inline \
352bool operator op(const Jet<T, N>& f, const Jet<T, N>& g) { \
353 return f.a op g.a; \
354} \
355template<typename T, int N> inline \
356bool operator op(const T& s, const Jet<T, N>& g) { \
357 return s op g.a; \
358} \
359template<typename T, int N> inline \
360bool operator op(const Jet<T, N>& f, const T& s) { \
361 return f.a op s; \
362}
363CERES_DEFINE_JET_COMPARISON_OPERATOR( < ) // NOLINT
364CERES_DEFINE_JET_COMPARISON_OPERATOR( <= ) // NOLINT
365CERES_DEFINE_JET_COMPARISON_OPERATOR( > ) // NOLINT
366CERES_DEFINE_JET_COMPARISON_OPERATOR( >= ) // NOLINT
367CERES_DEFINE_JET_COMPARISON_OPERATOR( == ) // NOLINT
368CERES_DEFINE_JET_COMPARISON_OPERATOR( != ) // NOLINT
369#undef CERES_DEFINE_JET_COMPARISON_OPERATOR
370
371// Pull some functions from namespace std.
372//
373// This is necessary because we want to use the same name (e.g. 'sqrt') for
374// double-valued and Jet-valued functions, but we are not allowed to put
375// Jet-valued functions inside namespace std.
376//
Keir Mierle8ebb0732012-04-30 23:09:08 -0700377// TODO(keir): Switch to "using".
378inline double abs (double x) { return std::abs(x); }
379inline double log (double x) { return std::log(x); }
380inline double exp (double x) { return std::exp(x); }
381inline double sqrt (double x) { return std::sqrt(x); }
382inline double cos (double x) { return std::cos(x); }
383inline double acos (double x) { return std::acos(x); }
384inline double sin (double x) { return std::sin(x); }
385inline double asin (double x) { return std::asin(x); }
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200386inline double tan (double x) { return std::tan(x); }
387inline double atan (double x) { return std::atan(x); }
388inline double sinh (double x) { return std::sinh(x); }
389inline double cosh (double x) { return std::cosh(x); }
390inline double tanh (double x) { return std::tanh(x); }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700391inline double pow (double x, double y) { return std::pow(x, y); }
392inline double atan2(double y, double x) { return std::atan2(y, x); }
393
394// In general, f(a + h) ~= f(a) + f'(a) h, via the chain rule.
395
396// abs(x + h) ~= x + h or -(x + h)
397template <typename T, int N> inline
398Jet<T, N> abs(const Jet<T, N>& f) {
399 return f.a < T(0.0) ? -f : f;
400}
401
402// log(a + h) ~= log(a) + h / a
403template <typename T, int N> inline
404Jet<T, N> log(const Jet<T, N>& f) {
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700405 const T a_inverse = T(1.0) / f.a;
Tim Langlois91087e82013-09-11 15:12:06 -0400406 return Jet<T, N>(log(f.a), f.v * a_inverse);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700407}
408
409// exp(a + h) ~= exp(a) + exp(a) h
410template <typename T, int N> inline
411Jet<T, N> exp(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400412 const T tmp = exp(f.a);
413 return Jet<T, N>(tmp, tmp * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700414}
415
416// sqrt(a + h) ~= sqrt(a) + h / (2 sqrt(a))
417template <typename T, int N> inline
418Jet<T, N> sqrt(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400419 const T tmp = sqrt(f.a);
420 const T two_a_inverse = T(1.0) / (T(2.0) * tmp);
421 return Jet<T, N>(tmp, f.v * two_a_inverse);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700422}
423
424// cos(a + h) ~= cos(a) - sin(a) h
425template <typename T, int N> inline
426Jet<T, N> cos(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400427 return Jet<T, N>(cos(f.a), - sin(f.a) * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700428}
429
430// acos(a + h) ~= acos(a) - 1 / sqrt(1 - a^2) h
431template <typename T, int N> inline
432Jet<T, N> acos(const Jet<T, N>& f) {
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700433 const T tmp = - T(1.0) / sqrt(T(1.0) - f.a * f.a);
Tim Langlois91087e82013-09-11 15:12:06 -0400434 return Jet<T, N>(acos(f.a), tmp * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700435}
436
437// sin(a + h) ~= sin(a) + cos(a) h
438template <typename T, int N> inline
439Jet<T, N> sin(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400440 return Jet<T, N>(sin(f.a), cos(f.a) * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700441}
442
443// asin(a + h) ~= asin(a) + 1 / sqrt(1 - a^2) h
444template <typename T, int N> inline
445Jet<T, N> asin(const Jet<T, N>& f) {
Sameer Agarwal25ac5482013-04-03 18:51:27 -0700446 const T tmp = T(1.0) / sqrt(T(1.0) - f.a * f.a);
Tim Langlois91087e82013-09-11 15:12:06 -0400447 return Jet<T, N>(asin(f.a), tmp * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700448}
449
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200450// tan(a + h) ~= tan(a) + (1 + tan(a)^2) h
451template <typename T, int N> inline
452Jet<T, N> tan(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400453 const T tan_a = tan(f.a);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200454 const T tmp = T(1.0) + tan_a * tan_a;
Tim Langlois91087e82013-09-11 15:12:06 -0400455 return Jet<T, N>(tan_a, tmp * f.v);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200456}
457
458// atan(a + h) ~= atan(a) + 1 / (1 + a^2) h
459template <typename T, int N> inline
460Jet<T, N> atan(const Jet<T, N>& f) {
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200461 const T tmp = T(1.0) / (T(1.0) + f.a * f.a);
Tim Langlois91087e82013-09-11 15:12:06 -0400462 return Jet<T, N>(atan(f.a), tmp * f.v);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200463}
464
465// sinh(a + h) ~= sinh(a) + cosh(a) h
466template <typename T, int N> inline
467Jet<T, N> sinh(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400468 return Jet<T, N>(sinh(f.a), cosh(f.a) * f.v);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200469}
470
471// cosh(a + h) ~= cosh(a) + sinh(a) h
472template <typename T, int N> inline
473Jet<T, N> cosh(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400474 return Jet<T, N>(cosh(f.a), sinh(f.a) * f.v);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200475}
476
477// tanh(a + h) ~= tanh(a) + (1 - tanh(a)^2) h
478template <typename T, int N> inline
479Jet<T, N> tanh(const Jet<T, N>& f) {
Tim Langlois91087e82013-09-11 15:12:06 -0400480 const T tanh_a = tanh(f.a);
Sameer Agarwalf0b071b2013-05-31 13:22:51 -0700481 const T tmp = T(1.0) - tanh_a * tanh_a;
Tim Langlois91087e82013-09-11 15:12:06 -0400482 return Jet<T, N>(tanh_a, tmp * f.v);
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200483}
484
Keir Mierle8ebb0732012-04-30 23:09:08 -0700485// Jet Classification. It is not clear what the appropriate semantics are for
Keir Mierle58ede272012-06-24 17:23:57 -0700486// these classifications. This picks that IsFinite and isnormal are "all"
487// operations, i.e. all elements of the jet must be finite for the jet itself
488// to be finite (or normal). For IsNaN and IsInfinite, the answer is less
489// clear. This takes a "any" approach for IsNaN and IsInfinite such that if any
490// part of a jet is nan or inf, then the entire jet is nan or inf. This leads
491// to strange situations like a jet can be both IsInfinite and IsNaN, but in
492// practice the "any" semantics are the most useful for e.g. checking that
493// derivatives are sane.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700494
495// The jet is finite if all parts of the jet are finite.
496template <typename T, int N> inline
Keir Mierle58ede272012-06-24 17:23:57 -0700497bool IsFinite(const Jet<T, N>& f) {
498 if (!IsFinite(f.a)) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700499 return false;
500 }
501 for (int i = 0; i < N; ++i) {
Keir Mierle58ede272012-06-24 17:23:57 -0700502 if (!IsFinite(f.v[i])) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700503 return false;
504 }
505 }
506 return true;
507}
508
509// The jet is infinite if any part of the jet is infinite.
510template <typename T, int N> inline
Keir Mierle58ede272012-06-24 17:23:57 -0700511bool IsInfinite(const Jet<T, N>& f) {
512 if (IsInfinite(f.a)) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700513 return true;
514 }
515 for (int i = 0; i < N; i++) {
Keir Mierle517e1962012-06-24 17:47:13 -0700516 if (IsInfinite(f.v[i])) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700517 return true;
518 }
519 }
520 return false;
521}
522
523// The jet is NaN if any part of the jet is NaN.
524template <typename T, int N> inline
Keir Mierle58ede272012-06-24 17:23:57 -0700525bool IsNaN(const Jet<T, N>& f) {
526 if (IsNaN(f.a)) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700527 return true;
528 }
529 for (int i = 0; i < N; ++i) {
Keir Mierle58ede272012-06-24 17:23:57 -0700530 if (IsNaN(f.v[i])) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700531 return true;
532 }
533 }
534 return false;
535}
536
537// The jet is normal if all parts of the jet are normal.
538template <typename T, int N> inline
Keir Mierle58ede272012-06-24 17:23:57 -0700539bool IsNormal(const Jet<T, N>& f) {
540 if (!IsNormal(f.a)) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700541 return false;
542 }
543 for (int i = 0; i < N; ++i) {
Keir Mierle58ede272012-06-24 17:23:57 -0700544 if (!IsNormal(f.v[i])) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700545 return false;
546 }
547 }
548 return true;
549}
550
551// atan2(b + db, a + da) ~= atan2(b, a) + (- b da + a db) / (a^2 + b^2)
552//
553// In words: the rate of change of theta is 1/r times the rate of
554// change of (x, y) in the positive angular direction.
555template <typename T, int N> inline
556Jet<T, N> atan2(const Jet<T, N>& g, const Jet<T, N>& f) {
557 // Note order of arguments:
558 //
559 // f = a + da
560 // g = b + db
561
Tim Langlois91087e82013-09-11 15:12:06 -0400562 T const tmp = T(1.0) / (f.a * f.a + g.a * g.a);
563 return Jet<T, N>(atan2(g.a, f.a), tmp * (- g.a * f.v + f.a * g.v));
Keir Mierle8ebb0732012-04-30 23:09:08 -0700564}
565
566
Sameer Agarwalecae1f02013-09-24 11:20:19 -0700567// pow -- base is a differentiable function, exponent is a constant.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700568// (a+da)^p ~= a^p + p*a^(p-1) da
569template <typename T, int N> inline
570Jet<T, N> pow(const Jet<T, N>& f, double g) {
Tim Langlois91087e82013-09-11 15:12:06 -0400571 T const tmp = g * pow(f.a, g - T(1.0));
572 return Jet<T, N>(pow(f.a, g), tmp * f.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700573}
574
575// pow -- base is a constant, exponent is a differentiable function.
576// (a)^(p+dp) ~= a^p + a^p log(a) dp
577template <typename T, int N> inline
578Jet<T, N> pow(double f, const Jet<T, N>& g) {
Sameer Agarwalecae1f02013-09-24 11:20:19 -0700579 T const tmp = pow(f, g.a);
580 return Jet<T, N>(tmp, log(f) * tmp * g.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700581}
582
583
584// pow -- both base and exponent are differentiable functions.
585// (a+da)^(b+db) ~= a^b + b * a^(b-1) da + a^b log(a) * db
586template <typename T, int N> inline
587Jet<T, N> pow(const Jet<T, N>& f, const Jet<T, N>& g) {
Tim Langlois91087e82013-09-11 15:12:06 -0400588 T const tmp1 = pow(f.a, g.a);
589 T const tmp2 = g.a * pow(f.a, g.a - T(1.0));
590 T const tmp3 = tmp1 * log(f.a);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700591
Tim Langlois91087e82013-09-11 15:12:06 -0400592 return Jet<T, N>(tmp1, tmp2 * f.v + tmp3 * g.v);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700593}
594
595// Define the helper functions Eigen needs to embed Jet types.
596//
597// NOTE(keir): machine_epsilon() and precision() are missing, because they don't
598// work with nested template types (e.g. where the scalar is itself templated).
599// Among other things, this means that decompositions of Jet's does not work,
600// for example
601//
602// Matrix<Jet<T, N> ... > A, x, b;
603// ...
604// A.solve(b, &x)
605//
606// does not work and will fail with a strange compiler error.
607//
608// TODO(keir): This is an Eigen 2.0 limitation that is lifted in 3.0. When we
609// switch to 3.0, also add the rest of the specialization functionality.
610template<typename T, int N> inline const Jet<T, N>& ei_conj(const Jet<T, N>& x) { return x; } // NOLINT
611template<typename T, int N> inline const Jet<T, N>& ei_real(const Jet<T, N>& x) { return x; } // NOLINT
612template<typename T, int N> inline Jet<T, N> ei_imag(const Jet<T, N>& ) { return Jet<T, N>(0.0); } // NOLINT
613template<typename T, int N> inline Jet<T, N> ei_abs (const Jet<T, N>& x) { return fabs(x); } // NOLINT
614template<typename T, int N> inline Jet<T, N> ei_abs2(const Jet<T, N>& x) { return x * x; } // NOLINT
615template<typename T, int N> inline Jet<T, N> ei_sqrt(const Jet<T, N>& x) { return sqrt(x); } // NOLINT
616template<typename T, int N> inline Jet<T, N> ei_exp (const Jet<T, N>& x) { return exp(x); } // NOLINT
617template<typename T, int N> inline Jet<T, N> ei_log (const Jet<T, N>& x) { return log(x); } // NOLINT
618template<typename T, int N> inline Jet<T, N> ei_sin (const Jet<T, N>& x) { return sin(x); } // NOLINT
619template<typename T, int N> inline Jet<T, N> ei_cos (const Jet<T, N>& x) { return cos(x); } // NOLINT
Johannes Schönbergera8d38d42013-05-30 00:11:44 +0200620template<typename T, int N> inline Jet<T, N> ei_tan (const Jet<T, N>& x) { return tan(x); } // NOLINT
621template<typename T, int N> inline Jet<T, N> ei_atan(const Jet<T, N>& x) { return atan(x); } // NOLINT
622template<typename T, int N> inline Jet<T, N> ei_sinh(const Jet<T, N>& x) { return sinh(x); } // NOLINT
623template<typename T, int N> inline Jet<T, N> ei_cosh(const Jet<T, N>& x) { return cosh(x); } // NOLINT
624template<typename T, int N> inline Jet<T, N> ei_tanh(const Jet<T, N>& x) { return tanh(x); } // NOLINT
Keir Mierle8ebb0732012-04-30 23:09:08 -0700625template<typename T, int N> inline Jet<T, N> ei_pow (const Jet<T, N>& x, Jet<T, N> y) { return pow(x, y); } // NOLINT
626
627// Note: This has to be in the ceres namespace for argument dependent lookup to
628// function correctly. Otherwise statements like CHECK_LE(x, 2.0) fail with
629// strange compile errors.
630template <typename T, int N>
631inline std::ostream &operator<<(std::ostream &s, const Jet<T, N>& z) {
632 return s << "[" << z.a << " ; " << z.v.transpose() << "]";
633}
634
635} // namespace ceres
636
637namespace Eigen {
638
639// Creating a specialization of NumTraits enables placing Jet objects inside
640// Eigen arrays, getting all the goodness of Eigen combined with autodiff.
641template<typename T, int N>
642struct NumTraits<ceres::Jet<T, N> > {
643 typedef ceres::Jet<T, N> Real;
644 typedef ceres::Jet<T, N> NonInteger;
645 typedef ceres::Jet<T, N> Nested;
646
Keir Mierleefe7ac62012-06-24 22:25:28 -0700647 static typename ceres::Jet<T, N> dummy_precision() {
648 return ceres::Jet<T, N>(1e-12);
649 }
650
Sameer Agarwala0c282a2014-08-24 22:19:03 -0700651 static inline Real epsilon() {
652 return Real(std::numeric_limits<T>::epsilon());
653 }
Filippo Basso91da3102014-01-19 14:35:23 +0100654
Keir Mierle8ebb0732012-04-30 23:09:08 -0700655 enum {
656 IsComplex = 0,
657 IsInteger = 0,
658 IsSigned,
659 ReadCost = 1,
660 AddCost = 1,
661 // For Jet types, multiplication is more expensive than addition.
662 MulCost = 3,
Sameer Agarwal11bf5ff2013-09-19 10:12:30 -0700663 HasFloatingPoint = 1,
664 RequireInitialization = 1
Keir Mierle8ebb0732012-04-30 23:09:08 -0700665 };
666};
667
668} // namespace Eigen
669
670#endif // CERES_PUBLIC_JET_H_