Most of this is a giant mess.

What we learned from it, however, is that using numba for a jit in certain circumstances is not feasable with completely general code. ie: The numeric_ODE module as it stands, is supposed to take any ODE as an input and integrate it. However, because of scoping issues and the necessity to pass an anonymous function through a function call, numba was not able


In [ ]:
import numpy as np
import numba as nb  #uncomment for numba
import matplotlib.pyplot as plt
%matplotlib inline

def julia(c):
    #@np.vectorize  #comment for numba
    @nb.vectorize #uncomment for numba
    def j(z):
        for n in range(100):
            z = z**2 + c
            if abs(z) > 2:
                return n
        return 0
    return j

j = julia(0.345 + 0.45j)

@nb.jit  #uncomment for numba
def cplane(min=-1.5, max=1.5, points=3000):
    r = np.linspace(-1.5, 1.5, points)
    x, y = np.meshgrid(r,r)
    z = x + y * 1j
    return z

%time z = cplane()
%time jset = j(z)

plt.figure(1, (20,15))
plt.imshow(jset, cmap=plt.cm.bone)
plt.xticks([])
plt.yticks([])
plt.title("Julia Set : c = 0.345 + 0.45j")
plt.show()


CPU times: user 384 ms, sys: 288 ms, total: 672 ms
Wall time: 1.91 s
CPU times: user 1.77 s, sys: 28 ms, total: 1.8 s
Wall time: 3.6 s

In [1]:
import numpy as np
import numeric_ode

In [2]:
u_0 = (0,1)
a = 0
b = 10
delta_t = 0.0001
mass = 1
delta = 0.25
force = 0.4
omega = 1

In [3]:
n = int((b - a) / float(delta_t)) #Number of points in t-mesh
tvals = np.linspace(a, b, n)
u_prime = lambda u, t: np.array((u[1], 1 / mass * (-delta * u[1] + u[0] - (u[0]) ** 3 + force * np.cos(omega * t))))
%time u_p = numeric_ode.integrate(u_0, a, b, delta_t, u_prime, 'rk4') #With Numba


---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-3-ed406cc83f72> in <module>()
      2 tvals = np.linspace(a, b, n)
      3 u_prime = lambda u, t: np.array((u[1], 1 / mass * (-delta * u[1] + u[0] - (u[0]) ** 3 + force * np.cos(omega * t))))
----> 4 get_ipython().magic("time u_p = numeric_ode.integrate(u_0, a, b, delta_t, u_prime, 'rk4') #With Numba")

/projects/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

/projects/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-59> in time(self, line, cell, local_ns)

/projects/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/projects/anaconda3/lib/python3.5/site-packages/IPython/core/magics/execution.py in time(self, line, cell, local_ns)
   1178         else:
   1179             st = clock2()
-> 1180             exec(code, glob, local_ns)
   1181             end = clock2()
   1182             out = None

<timed exec> in <module>()

/projects/anaconda3/lib/python3.5/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    307                                 for i, err in failed_args))
    308                 e.patch_message(msg)
--> 309             raise e
    310 
    311     def inspect_llvm(self, signature=None):

/projects/anaconda3/lib/python3.5/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    284                 argtypes.append(self.typeof_pyval(a))
    285         try:
--> 286             return self.compile(tuple(argtypes))
    287         except errors.TypingError as e:
    288             # Intercept typing error that may be due to an argument

/projects/anaconda3/lib/python3.5/site-packages/numba/dispatcher.py in compile(self, sig)
    530 
    531             self._cache_misses[sig] += 1
--> 532             cres = self._compiler.compile(args, return_type)
    533             self.add_overload(cres)
    534             self._cache.save_overload(sig, cres)

/projects/anaconda3/lib/python3.5/site-packages/numba/dispatcher.py in compile(self, args, return_type)
     79                                       impl,
     80                                       args=args, return_type=return_type,
---> 81                                       flags=flags, locals=self.locals)
     82         # Check typing error if object mode is used
     83         if cres.typing_error is not None and not flags.enable_pyobject:

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
    682     pipeline = Pipeline(typingctx, targetctx, library,
    683                         args, return_type, flags, locals)
--> 684     return pipeline.compile_extra(func)
    685 
    686 

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in compile_extra(self, func)
    346         self.lifted = ()
    347         self.lifted_from = None
--> 348         return self._compile_bytecode()
    349 
    350     def compile_ir(self, func_ir, lifted=(), lifted_from=None):

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in _compile_bytecode(self)
    647         """
    648         assert self.func_ir is None
--> 649         return self._compile_core()
    650 
    651     def _compile_ir(self):

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in _compile_core(self)
    634 
    635         pm.finalize()
--> 636         res = pm.run(self.status)
    637         if res is not None:
    638             # Early pipeline completion

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in run(self, status)
    233                     # No more fallback pipelines?
    234                     if is_final_pipeline:
--> 235                         raise patched_exception
    236                     # Go to next fallback pipeline
    237                     else:

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in run(self, status)
    225                 try:
    226                     event(stage_name)
--> 227                     stage()
    228                 except _EarlyPipelineCompletion as e:
    229                     return e.result

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in stage_nopython_frontend(self)
    434                 self.args,
    435                 self.return_type,
--> 436                 self.locals)
    437 
    438         with self.fallback_context('Function "%s" has invalid return type'

/projects/anaconda3/lib/python3.5/site-packages/numba/compiler.py in type_inference_stage(typingctx, interp, args, return_type, locals)
    782             infer.seed_type(k, v)
    783 
--> 784         infer.build_constraint()
    785         infer.propagate()
    786         typemap, restype, calltypes = infer.unify()

/projects/anaconda3/lib/python3.5/site-packages/numba/typeinfer.py in build_constraint(self)
    715         for blk in utils.itervalues(self.blocks):
    716             for inst in blk.body:
--> 717                 self.constrain_statement(inst)
    718 
    719     def return_types_from_partial(self):

/projects/anaconda3/lib/python3.5/site-packages/numba/typeinfer.py in constrain_statement(self, inst)
    874     def constrain_statement(self, inst):
    875         if isinstance(inst, ir.Assign):
--> 876             self.typeof_assign(inst)
    877         elif isinstance(inst, ir.SetItem):
    878             self.typeof_setitem(inst)

/projects/anaconda3/lib/python3.5/site-packages/numba/typeinfer.py in typeof_assign(self, inst)
    932                                               src=value.name, loc=inst.loc))
    933         elif isinstance(value, (ir.Global, ir.FreeVar)):
--> 934             self.typeof_global(inst, inst.target, value)
    935         elif isinstance(value, ir.Arg):
    936             self.typeof_arg(inst, inst.target, value)

/projects/anaconda3/lib/python3.5/site-packages/numba/typeinfer.py in typeof_global(self, inst, target, gvar)
   1023     def typeof_global(self, inst, target, gvar):
   1024         try:
-> 1025             typ = self.resolve_value_type(inst, gvar.value)
   1026         except TypingError as e:
   1027             if (gvar.name == self.func_id.func_name

/projects/anaconda3/lib/python3.5/site-packages/numba/typeinfer.py in resolve_value_type(self, inst, val)
    951         except ValueError as e:
    952             msg = str(e)
--> 953         raise TypingError(msg, loc=inst.loc)
    954 
    955     def typeof_arg(self, inst, target, arg):

TypingError: Failed at nopython (nopython frontend)
Untyped global name 'euler_step': cannot determine Numba type of <class 'function'>
File "numeric_ode.py", line 189

This error may have been caused by the following argument(s):
- argument 4: cannot determine Numba type of <class 'function'>

In [3]:
n = int((b - a) / float(delta_t)) #Number of points in t-mesh
tvals = np.linspace(a, b, n)
u_prime = lambda u, t: np.array((u[1], 1 / mass * (-delta * u[1] + u[0] - (u[0]) ** 3 + force * np.cos(omega * t))))
%time u_p = numeric_ode.integrate(u_0, a, b, delta_t, u_prime, 'rk4') #Without Numba


CPU times: user 1.82 s, sys: 0 ns, total: 1.82 s
Wall time: 1.88 s

In [ ]: