Source code for mmf.solve.solver_utils

r"""Solver Utilities.

This module provides some utilities used by various solvers.
"""
from __future__ import division

__all__ = ['line_search', 'IterationError', 'NoDescentError']

import math

import numpy as np
import scipy as sp

import scipy.optimize

from mmf.solve.solver_interface import IterationError, Mesg

_FINFO = np.finfo(float)
_TINY = _FINFO.tiny
_MESGS = Mesg()

[docs]class NoDescentError(IterationError): """No downhill step could be found."""
def exact_line_search(g, g0, g1=None, mesgs=_MESGS, max_steps=500, rel_tol=1e-5): r"""Return `(l, g(l))` where `g(l)` is minimized.""" l = sp.optimize.brent(g, brack=(0, 1), tol=rel_tol, maxiter=max_steps) return (l, g(l))