JimBoy
JimBoy

Reputation: 597

Simple sum-function in Python with numba doesn't compute

I'm trying to learn Python and Numba and I can't figure out why the following code doesn't compute in IPython/Jupyter:

from numba import *

sample_array = np.arange(10000.0)

@jit('float64(float64, float64)')
def sum(x, y):
    return x + y

sum(sample_array, sample_array)

TypeError Traceback (most recent call last) in () ----> 1 sum(sample_array, sample_array)

C:\Users***\AppData\Local\Continuum\Anaconda\lib\site-packages\numba\dispatcher.pyc in _explain_matching_error(self, *args, **kws) 201 msg = ("No matching definition for argument type(s) %s" 202 % ', '.join(map(str, args))) --> 203 raise TypeError(msg) 204 205 def repr(self):

TypeError: No matching definition for argument type(s) array(float64, 1d, C), array(float64, 1d, C)

Upvotes: 7

Views: 8838

Answers (2)

luky
luky

Reputation: 2370

In my case it was because i was passing 2d array (matrix) but it was expecting 1d array (vector)

Upvotes: 0

JoshAdel
JoshAdel

Reputation: 68692

You are passing in arrays, but your jit signature expects scalar floats. Try the following instead:

@jit('float64[:](float64[:], float64[:])')
def sum(x, y):
    return x + y

My recommendation is to see if you can get away with not specifying types and just use the bare @jit decorator, which will do type inference at runtime and you can more flexibly handle inputs. For example:

@jit(nopython=True)
def sum(x, y):
    return x + y

In [13]: sum(1,2)
Out[13]: 3

In [14]: sum(np.arange(5),np.arange(5))
Out[14]: array([0, 2, 4, 6, 8])

My experience is that adding the types rarely gives any sort of performance benefit.

Upvotes: 6

Related Questions