Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Multivariate Newton and Halley

Synopsis
#include <boost/math/tools/multivariate_roots.hpp>
namespace boost { namespace math { namespace tools {

template <class F, class Container>
Container multivariate_newton(F f, Container x, int digits);

template <class F, class Container>
Container multivariate_newton(F f, Container x, int digits, std::uintmax_t& max_iter);

template <class F, class Container>
Container multivariate_halley(F f, Container x, int digits);

template <class F, class Container>
Container multivariate_halley(F f, Container x, int digits, std::uintmax_t& max_iter);

}}} // namespaces boost::math::tools
Description

These functions find a root of a system of n equations in n unknowns, F(x) = 0 with x in ℝn, starting from the guess x. They are the multivariate analogues of newton_raphson_iterate and halley_iterate of root-finding with derivatives.

xi ← xi + ai2 / (ai + bi/2).

This is the abstract Padé approximant of order (1, 1) to the inverse of F, with the vector operations taken componentwise: when n = 1 it is Halley's method. One LU factorization of J serves both linear solves, and the method needs F″ only along the direction a, so the n3 entries of the second derivative tensor are never formed.

Parameters

F f

A callable that takes a Container x and returns a pair or tuple. For Newton it holds F(x) and the Jacobian J(x), Jij = ∂/F/i/∂/x/j. For Halley it holds a third element, a callable d2 with d2(v) = F″(x)[v, v], the second derivative of t ↦ F(x + t v) at t = 0; Newton ignores a third element, so one callable serves both. F(x) and d2(v) may be any containers with size() and operator[]. J may be a container of rows (std::vector<std::vector<Real>>, std::array<std::array<Real, N>, N>, ...), or a row-major array of n2 entries (std::vector<Real>, std::array<Real, N*N>, ...).

Container x

The initial guess, a std::vector<Real>, std::array<Real, N>, or similar random access container of a floating point type, which may be a multiprecision type.

int digits

The desired number of binary digits of precision.

uintmax_t& max_iter

An optional maximum number of iterations; on exit, the number of iterations performed. It is the number of factorizations of J, and does not count the line search's evaluations of f.

The iteration stops when the Newton step a satisfies ‖/a/‖∞ ≤ 21−digits ‖/x/‖∞ (the step is then taken), when F(x) = 0 exactly, or when max_iter iterations have been performed. It also stops when steps below √ε‖/x/‖∞ no longer shrink at all, since they are then rounding noise; at a multiple root, where the steps shrink by a constant factor, the iteration goes on. As for the scalar functions, the tolerance is relative to x, so a root at the origin is approached until F underflows to zero.

Globalization

Far from a root a full Newton step can increase ‖F‖, so both functions minimize ½‖F‖2 along their steps. Newton backtracks along a until the Armijo condition ½‖F(x + t a)‖2 < (1 − 2×10−4 t)½‖F(x)‖2 holds, choosing each new t from the quadratic through ½‖F‖2 and its slope at t = 0, kept in [t/10, t/2]. Near a singular J the Newton step can be almost orthogonal to the gradient g = JTF of ½‖F‖2, and be cut back so far that ‖F‖ barely decreases. As in Powell's dogleg method, once t a is shorter than the Cauchy step −(‖/g/‖2/‖/Jg/‖2)g, the least of ‖F + J p/‖ along −/g, the search backtracks along the Cauchy step p instead, with ½‖F(x)‖2 + 10−4t gTp on the right of the Armijo condition. Halley tries its full step first, and if that does not decrease ‖F‖ it takes the backtracking Newton step instead. A trial point where F is not finite is treated as an increase, so f may return infinities or NaNs outside its domain.

The componentwise division can blow up where ai + bi/2 is near zero. Cuyt and Rall take 0/0 as 0 (the Halley component is zero when the Newton component is). In addition, where q = 1 + bi/(2/a/i) < 1/2, so that the Halley component points against the Newton component or is more than twice as long, the Newton component ai is used in its place. Near a root b = O(‖/a/‖2) and q → 1, so this does not affect the cubic convergence.

Errors

An evaluation_error is raised through the default policy (and so is thrown by default) when

A domain error is raised when x is empty.

When no step decreases ‖F‖ but the Newton step is below ε1/3‖/x/‖∞, x is returned without an error: at a local minimum of ‖F‖ that is not a root, J is singular and the Newton step large or undefined, whereas a step this small is the rounding noise of an accurate x, about cond(J)ε‖/x/‖, for cond(J) up to ε−2/3 (1010 in double precision).

With a policy that does not throw, every component of the result is the value returned by the error handler, NaN by default.

Supplying the derivatives

F″(x)[v, v] is the second derivative of the univariate function t ↦ F(x + t v) at t = 0, so a single second order forward-mode pass of autodiff in the one variable t computes it, for any n. The example multivariate_roots_ellipse.cpp finds the point of an ellipse closest to a point q: at the closest point p, p is on the ellipse and p − q is along the normal:

// At the closest point p, p is on the ellipse and p - q is along the normal (x/a^2, y/b^2).
struct ellipse_closest_point
{
   double a;
   double b;
   std::array<double, 2> q;

   template <class T>
   std::array<T, 2> operator()(std::array<T, 2> const& p) const
   {
      return {(p[0] - q[0]) * p[1] / (b * b) - (p[1] - q[1]) * p[0] / (a * a),
              p[0] * p[0] / (a * a) + p[1] * p[1] / (b * b) - 1};
   }
};

The Jacobian is built one column at a time by first order passes along the coordinate directions, and d2 is a second order pass along v, computed only when the solver asks for it:

using boost::math::differentiation::make_fvar;

// F(x + t v) with its first Order derivatives in t at t = 0: F, J v and F''[v, v]. One forward pass in the single
// variable t, however many variables F has.
template <int Order, class System, std::size_t N>
auto along(System const& F, std::array<double, N> const& x, std::array<double, N> const& v)
{
   auto const t = make_fvar<double, Order>(0);
   std::array<boost::math::differentiation::autodiff_fvar<double, Order>, N> p;
   for (std::size_t k = 0; k < N; ++k)
      p[k] = x[k] + v[k] * t;
   return F(p);
}

// (F, J, d2) for multivariate_halley: J a column at a time, d2(v) = F''[v, v] when the solver asks for it.
template <class System, std::size_t N>
auto with_derivatives(System const& F, std::array<double, N> const& x)
{
   std::array<double, N> value{};
   std::array<std::array<double, N>, N> J{};
   for (std::size_t j = 0; j < N; ++j)
   {
      std::array<double, N> e{};
      e[j] = 1;
      auto const Fj = along<1>(F, x, e);
      for (std::size_t i = 0; i < N; ++i)
      {
         value[i] = Fj[i].derivative(0);
         J[i][j] = Fj[i].derivative(1);
      }
   }
   auto d2 = [F, x](std::array<double, N> const& v) {
      auto const Fv = along<2>(F, x, v);
      std::array<double, N> w;
      for (std::size_t i = 0; i < N; ++i)
         w[i] = Fv[i].derivative(2);
      return w;
   };
   return std::make_tuple(value, J, d2);
}
ellipse_closest_point F{a, b, q};
auto f = [&F](std::array<double, 2> const& p) { return with_derivatives(F, p); };
// Start from the point of the ellipse at the angle of q, stretched to the ellipse's shape:
double const theta = std::atan2(a * q[1], b * q[0]);
std::array<double, 2> const p0{a * std::cos(theta), b * std::sin(theta)};
std::uintmax_t halley_iterations = 50;
auto p = multivariate_halley(f, p0, std::numeric_limits<double>::digits, halley_iterations);

The closure d2 may capture whatever it needs from the evaluation at x; for the Rosenbrock gradient system ∇((1 − x)2 + 100(y − x2)2) = 0, written out by hand,

auto f = [](std::vector<double> const& x) {
   double u = x[0], w = x[1];
   std::vector<double> F{-2 * (1 - u) - 400 * u * (w - u * u), 200 * (w - u * u)};
   std::vector<std::vector<double>> J{{2 - 400 * (w - u * u) + 800 * u * u, -400 * u}, {-400 * u, 200.0}};
   auto d2 = [u](std::vector<double> const& v) {
      return std::vector<double>{-800 * v[0] * v[1] + 2400 * u * v[0] * v[0], -400 * v[0] * v[0]};
   };
   return std::make_tuple(F, J, d2);
};
std::uintmax_t iterations = 1000;
auto x = boost::math::tools::multivariate_halley(f, std::vector<double>{-1.2, 1.0}, 53, iterations);
Performance and caveats

A Halley iteration costs one more triangular solve and one evaluation of d2 than a Newton iteration, and saves about a third of the iterations near the root; which is faster overall depends on the cost of f and d2 relative to the O(n3) factorization. Far from the root Halley's steps follow curved valleys better: from the classic start (−1.2, 1), Newton with the line search needs 253 iterations on the Rosenbrock gradient system, as its straight steps creep along the valley, and Halley needs 68.

Newton's method is invariant under affine changes of the variables x ↦ Ax; the multivariate Halley method is not, because its update divides componentwise. It is invariant under scaling of the individual variables, and, like Newton, under any invertible linear change of the equations F ↦ BF.

The LU factorization is dense; for large sparse systems, solve with a sparse factorization in a loop of your own. The line search guarantees that ‖F‖ decreases, not that the root found is the one nearest the guess.

References

PrevUpHomeNext