Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is signature for a zero dimensional array in numba

numba.jit() allows entering a type signature but I can't figure out what the signature for a zero dimensional array is.

For example:

numba.jit('void(float32, float32[:])')

says the function return is void and the input arguments are float32 scalar and float32 1-D array.

But what is instead of a scalar I want to pass in a 0-dimensional array. What's the type signature? I tried the obvious float32[] but that didn't seem to work.

In case you are wondering how one gets a 0-D array in numpy you do it like this:

a = numpy.array(2)

which is different than

a = numpy.array([2])

the latter is a 1-D array.

like image 965
Alexander Hamilton Avatar asked Oct 17 '25 19:10

Alexander Hamilton


1 Answers

This is how you can do it using numba.types.Array:

import numba as nb
import numpy as np

#        |---------0d int array---------|
@nb.njit(nb.types.Array(nb.int64, 0, "C")())
def func():
    return np.array(2)

Here I used that the returned value will be a C-contiguous int64 array with 0 dimensions. Adjust these as needed.


In my experience there is rarely a use-case (see "Benefit and Limitations of Ahead-of-Time compilation") for explicitly typed functions in numba - except for compilation times or in case one needs to avoid numba using already inferred types when it should compile a new function. So, personally, I wouldn't use these signatures.

like image 65
MSeifert Avatar answered Oct 19 '25 10:10

MSeifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!