我尝试计算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
有人知道循环为什么失败以及如何解决吗?