No implementation of function for making an array

Hello.
I´m trying to make an array with numpy for to later insert tuples into indexes.
This is the relevant line: lis=np.zeros(int((n+1)*(n/2+1)), dtype=(np.int16,np.int16,np.int16))
And it gives out this error:
`TypingError: No implementation of function Function() found for signature:

zeros(int64, dtype=UniTuple(class(int16) x 3))

There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function ‘zeros’: File: numba\core\typing\npydecl.py: Line 504.
With argument(s): ‘(int64, dtype=UniTuple(class(int16) x 3))’:
No match.`
What other numpy function can I use to make an array like this one?

hi @ismaelbrowne, are you sure that line is valid numpy? I cannot get it running

np.zeros(10, dtype=(np.int16,np.int16,np.int16))

TypeError: Tuple must have size 2, but has size 3 

What type of array are you trying to build?

Luk

If you don’t jit the function, the first element in dtype should be the type of the elements in the tuple, and the second is the amount of those elements per tuple, so dtype=(np.int16,2) would mean that each tuple has 2 int16 elements inside. But when you jit the function, it requires, at least from how I saw it interpreted the function, to put the type as many times as elements will be in each tuple, so dtype=(np.int16,np.int16,np.int16) would mean that each tuple has 3 int16 elements inside. Also, I’m trying to build a 1d array of tuples which contain 3 elements each.

Sorry if I didn’t explain myself better, what I wanted to say is that dtype=(np.int16,np.int16,np.int16) is not valid Numpy, so it will never be valid in Numba.

Hi @ismaelbrowne

Numba won’t work with a 1d array of tuples, as I think that requires an object dtype? If you need a 1d array with 3 elements in each location perhaps consider using a structured dtype/record array. For example, in NumPy:

In [37]: np.zeros(4, dtype=[(f'f{x}', np.int16) for x in range(3)])
Out[37]: 
array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
      dtype=[('f0', '<i2'), ('f1', '<i2'), ('f2', '<i2')])

in Numba, the type will need to be specified outside of the jit compiled function due to limitations in the compiler. Example:

In [67]: dt=np.dtype([(f'f{x}', np.int16) for x in range(3)])

In [68]: dt
Out[68]: dtype([('f0', '<i2'), ('f1', '<i2'), ('f2', '<i2')])

In [69]: @njit
    ...: def foo():
    ...:     n = 4
    ...:     x = np.zeros(n, dtype=dt)
    ...:     for i in range(n):
    ...:         x[i].f0 = 1 + i
    ...:         x[i].f1 = 2 + i
    ...:         x[i].f2 = 3 + i
    ...:     return x
    ...: 

In [70]: foo()
Out[70]: 
array([(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)],
      dtype=[('f0', '<i2'), ('f1', '<i2'), ('f2', '<i2')])

hope this helps?

1 Like

So that was it, I needed to specify the type outside of the jitted function.
Thank you very much!

No problem, glad you have something that works.