"""
Interface to the nlopt DIRECT implementation.
"""

# future imports
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

# by default export nothing.
__all__ = []

try:
    # try and import nlopt, and if not this package will not define or export
    # anything.
    import nlopt
    import numpy as np

    # exported symbols
    __all__ += ['solve_direct']

    def solve_direct(f, bounds, maximize=False):
        def objective(x, grad):
            """Objective function in the form required by nlopt."""
            if grad is None:
                fx = f(x[None], grad=False)
            else:
                fx, gx = f(x[None], grad=True)
                grad[:] = gx[0][:]
            return fx[0]

        bounds = np.array(bounds, ndmin=2)

        opt = nlopt.opt(nlopt.GN_DIRECT_L, bounds.shape[0])
        opt.set_lower_bounds(list(bounds[:, 0]))
        opt.set_upper_bounds(list(bounds[:, 1]))
        opt.set_ftol_rel(1e-6)

        if maximize:
            opt.set_max_objective(objective)
        else:
            opt.set_min_objective(objective)

        xmin = bounds[:, 0] + (bounds[:, 1] - bounds[:, 0]) / 2
        xmin = opt.optimize(xmin)
        fmin = opt.last_optimum_value()

        return xmin, fmin

except ImportError:
    pass

@@ -586,78 +586,17 @@
 grad
- is None:%0A                fx = f(x%5BNone%5D, grad=False)%0A            else
+.size %3E 0
 :%0A  
@@ -675,16 +675,78 @@
 x%5B0%5D%5B:%5D%0A
+            else:%0A                fx = f(x%5BNone%5D, grad=False)%0A
         

Fix bug with direct.