msicam
msicam

Reputation: 41

Is it possible to convert an int to an array while using njit(numba)?

I wondering if it is possible to convert an int to an array whilst using njit in numba? Sorry if this is a simple question, I am new to the library and looking to learn more about it.

I have tried the following:

import numpy as np 
from numba import njit 

    @njit
    def top(f):
        if f =="TRUE":
             t = 0
             for i in range(10):
                  t = t+1
             t = np.array(t)
             return t
    
    top("TRUE") 

but get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numba/dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/usr/lib/python3/dist-packages/numba/dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "/usr/lib/python3/dist-packages/numba/six.py", line 668, in reraise
    raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot unify int64 and array(int64, 0d, C) for 't', defined at <stdin> (4)

File "<stdin>", line 4:
<source missing, REPL/exec in use?>

[1] During: typing of assignment at <stdin> (7)

File "<stdin>", line 7:
<source missing, REPL/exec in use?>

Upvotes: 1

Views: 104

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

You probably should not change the type of your variable within a numba function (similar as why you would not be able to do so in a C function). This seems to work:

@njit
def top(f):
    if f =="TRUE":
        t = 0
        for i in range(10):
            t = t+1
        return np.array([t])

Upvotes: 2

Related Questions