WZhao
WZhao

Reputation: 323

Why numba raise a type error in numpy linspace

I am using numba 0.34.0 and numpy 1.13.1. A small example is shown as following:

import numpy as np    
from numba import jit
@jit(nopython=True)
def initial(veh_size):
    t = np.linspace(0, (100 - 1) * 30, 100, dtype=np.int32)
    t0 = np.linspace(0, (veh_size - 1) * 30, veh_size, dtype=np.int32)
    return t0

initial(100)

Both the line with t and t0 have the same error message.

The error message:

numba.errors.InternalError: 
[1] During: resolving callee type: Function(<function linspace at 0x000001F977678C80>)
[2] During: typing of call at ***/test1.py (6)

Upvotes: 2

Views: 3117

Answers (1)

MSeifert
MSeifert

Reputation: 152637

Because the numba version of np.linspace accepts no dtype argument (source: numba 0.34 documentation):

2.7.3.3. Other functions

The following top-level functions are supported:

  • [...]

  • numpy.linspace() (only the 3-argument form)

  • [...]

You need to use astype to convert it inside a nopython-numba function:

import numpy as np    
from numba import jit
@jit(nopython=True)
def initial(veh_size):
    t = np.linspace(0, (100 - 1) * 30, 100).astype(np.int32)
    t0 = np.linspace(0, (veh_size - 1) * 30, veh_size).astype(np.int32)
    return t0

initial(100)

Or just don't use np.linspace in nopython-numba functions and pass it in as argument. That avoids a temporary array and I doubt that numbas np.linspace is faster than NumPys.

Upvotes: 8

Related Questions