Я пытаюсь вычислить 1-мерное дискретное косинусное преобразование (Тип 2), я пытаюсь улучшить свою производительность с помощью Numba.У меня есть следующий код:
import numpy as np
import math
import numba
@numba.jit()
def epsilon(N:int, i: int) -> float:
if i == 0 or i == N:
return math.sqrt(2)/2
return 1.0
@numba.jit()
def dct2(a):
n = len(a)
y = np.empty([2*n])
y[:len(a)] = a
y[n:] = np.flip(a)
fft = np.fft.fft(y)
erg = np.empty([n])
factor = 1/math.sqrt(2*n)
for i in range(0,n):
erg[i] = factor*epsilon(n,i)*(math.cos(-i*2*math.pi/(4*n))*fft[i].real - math.sin(-i*2*math.pi/(4*n))*fft[i].imag)
return erg
Я думаю, что он не может скомпилировать цикл for, но я не знаю почему.Из того, что я понимаю из документации по numba, цикл должен быть в состоянии подняться.
Я получил следующие предупреждения:
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function empty>)
[2] During: typing of call at src/algos.py (32)
File "src/algos.py", line 32:
def dct2(a):
<source elided>
n = len(a)
y = np.empty([2*n])
^
@numba.jit()
src/algos.py:29: NumbaWarning: Function "dct2" failed type inference: cannot determine Numba type of <class 'numba.dispatcher.LiftedLoop'>
File "src/algos.py", line 39:
def dct2(a):
<source elided>
factor = 1/math.sqrt(2*n)
for i in range(0,n):
^
@numba.jit()
src/algos.py:29: NumbaWarning: Function "dct2" was compiled in object mode without forceobj=True, but has lifted loops.
@numba.jit()
src/algos.py:29: NumbaWarning: Function "dct2" failed type inference: Invalid use of Function(<built-in function empty>) with argument(s) of type(s): (list(int64))
* parameterized
Кто-нибудь знает, почему цикл выходит из строя и как я могуисправить это?