Skip to content

Instantly share code, notes, and snippets.

@cc7768
Last active August 29, 2015 14:04
Show Gist options
  • Save cc7768/30315f3d291ee8c8472a to your computer and use it in GitHub Desktop.
Save cc7768/30315f3d291ee8c8472a to your computer and use it in GitHub Desktop.
Testing numba for optimization.
import numpy as np
from scipy.optimize import bisect
from numba import f8, jit, njit
def min_me(x):
return x**4 - 2*x**2 - x - 3.
num_min_me = jit(min_me)
def python_bisect(f, a, b, tol, mxiter):
"""
Beautiful Docstring
"""
# Evaluate functions at a and b
fa = f(a)
fb = f(b)
if abs(fa) < tol:
return a
elif abs(fb) < tol:
return b
c = (a+b)/2.
fc = f(c)
for iters in range(mxiter):
if abs(fc)<tol:
break
if fa*fc < 0:
b = c
fb = fc
else:
a = c
fa = fc
c = (a+b)/2.
fc = f(c)
if abs(f(c)) < tol:
conv = True
else:
conv = False
return c
jit_bisect = jit(f8(f8, f8))(python_bisect)
bisect(min_me, -5., 50.)
python_bisect(min_me, -5., 50.)
jit_bisect(min_me, -5., 50.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment