我在python中有以下代码来计算Mandelbrot集。它工作正常,但当我尝试通过在函数def之前添加@jit
装饰器来使用JIT编译它时,它不再起作用。谁能告诉我为什么?如果你不批评我的Mandelbrot计算(我猜它可以被优化),我将不胜感激,并且让我知道为什么JIT不能使用这个函数。顺便说一句,代码在def之后缩进。当我在这里插入它时,它就不会出现这种情况。
def mandelbrot(xmin,xmax,ymin,ymax,width,height,maxiter):
points=[]
x=np.linspace(xmin,xmax,width)
y=np.linspace(ymin,ymax,height)
for ix,re in enumerate(x):
points.append([])
for iy,im in enumerate(y):
cx=re
cy=im
zx=0
zy=0
for n in range(maxiter):
if zx*zx+zy*zy>4.0:
iters=n
break
else:
oldzx=zx
oldzy=zy
zy = 2*oldzx*oldzy+cy
zx = oldzx*oldzx-oldzy*oldzy+cx
iters=n
points[ix].append(int(iters))
return points
我得到以下错误报告,最后一行是LoweringError
runfile('D:/ python programs / mandelbrot / mandelbrot.py',wdir ='D:/ python programs / mandelbrot')Traceback(最近一次调用最后一次):
文件“”,第1行,在runfile中('D:/ python程序/ mandelbrot / mandelbrot.py',wdir ='D:/ python programs / mandelbrot')
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,第786行,在runfile execfile(filename,namespace)中
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,第110行,execfile exec(compile(f.read(),filename,'exec'),namespace)
文件“D:/ python programs / mandelbrot / mandelbrot.py”,第41行,在mandelbrot_set = mandelbrot(-2.0,1.0,-1.5,1.5,500,500,50)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ dispatcher.py”,第368行,在_compile_for_args中引发e
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ dispatcher.py”,第325行,在_compile_for_args中返回self.compile(tuple(argtypes))
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ dispatcher.py”,第653行,编译cres = self._compiler.compile(args,return_type)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ dispatcher.py”,第83行,编译管道_class = self.pipeline_class)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第873行,在compile_extra中返回pipeline.compile_extra(func)
在compile_extra中的文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第367行返回self._compile_bytecode()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第804行,在_compile_bytecode中返回self._compile_core()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第791行,在_compile_core res = pm.run(self.status)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第253行,在运行中引发patched_exception
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第245行,在运行阶段()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第438行,在stage_objectmode_frontend中cres = self.frontend_looplift()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第428行,frontend_looplift lifted = tuple(loops),lifted_from = None)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第887行,在compile_ir中lifted_from = lifted_from)
在compile_ir中的文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第375行返回self._compile_ir()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第811行,在_compile_ir中返回self._compile_core()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第791行,在_compile_core res = pm.run(self.status)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第253行,在运行中引发patched_exception
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第245行,在运行阶段()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第652行,在stage_objectmode_backend self._backend(lowerfn,objectmode = True)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第628行,在_backend中降低= lowerfn()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第601行,在backend_object_mode self.flags中)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ compiler.py”,第1018行,在py_lowering_stage lower.lower()中
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ lowering.py”,第173行,位于较低的self.lower_normal_function(self.fndesc)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ lowering.py”,第214行,在lower_normal_function entry_block_tail = self.lower_function_body()
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ lowering.py”,第239行,在lower_function_body self.lower_block(块)中
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ lowering.py”,第254行,在lower_block self.lower_inst(inst)中
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ contextlib.py”,第130行,退出self.gen.throw(类型,值,追溯)
在new_error_context six.reraise中输入文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ errors.py”,第585行(type(newerr),newerr,tb)
文件“C:\ Users \ Matthew \ Anaconda3 \ lib \ site-packages \ numba \ six.py”,第659行,重新加价
LoweringError:iters
文件“mandelbrot.py”,第17行:def mandelbrot(xmin,xmax,ymin,ymax,width,height,maxiter):for ix,re in enumerate(x):points.append([])^
我解决了这个问题。似乎我需要在循环开始时将变量'iters'初始化为0。所以当我把@jit放在它之前时这是有效的。谁知道为什么?
def mandelbrot(xmin,xmax,ymin,ymax,width,height,maxiter):
points=[]
x=np.linspace(xmin,xmax,width)
y=np.linspace(ymin,ymax,height)
for ix,re in enumerate(x):
points.append([])
for iy,im in enumerate(y):
iters=0
cx=re
cy=im
zx=0
zy=0
for n in range(maxiter):
if zx*zx+zy*zy>4.0:
iters=n
break
else:
oldzx=zx
oldzy=zy
zy = 2*oldzx*oldzy+cy
zx = oldzx*oldzx-oldzy*oldzy+cx
iters=n
points[ix].append(iters)
return points