我有一个完美的python程序,我想在这个程序中使用Cython来实现一个函数。
原始功能是:
def NoFeedB(Ef, Ef2, Nf, u, tijd ):
dEdt= np.zeros_like(Ef, dtype=np.complex128)
dNdt= np.zeros_like(Nf)
Efcon = np.conjugate(Ef)
for j in xrange(nrModes):
for n in xrange(nrMoments):
dEdt = dEdt + 0.5 * CouplingE((j+1,n,0)) * dg * (1+ A*1j) * Nf[n] * Ef[j] * np.exp( 1j* (sweep.omega[j]-sweep.omega) *tijd)
for k in xrange(nrModes):
if n==0:
dNdt = dNdt + (g* CouplingN((j+1, k+1, 0 )) * Efcon[j] * Ef[k] * np.exp( 1j* (sweep.omega[k]-sweep.omega[j]) *tijd) )
dNdt = dNdt + ( dg*(1+A*1j) * CouplingN((j+1,k+1,n)) * Nf[n] * Efcon[j] * Ef[k] * np.exp( 1j* (sweep.omega[k]-sweep.omega[j]) *tijd) )
dEdt = dEdt - 0.5*(pd-g)*Ef
dNdt = sweep.Jn - Nf*ed - np.real(dNdt)
return dEdt, dNdt
看看这两个网站: http://docs.cython.org/src/tutorial/cython_tutorial.html#primes 和 http://docs.cython.org/0.14/src/tutorial/numpy.html我将代码更改为:
def NoFeedB( np.ndarray[np.complex128_t,ndim= nrModes] Ef, np.ndarray[np.complex128_t,ndim= nrModes] Ef2, np.ndarray[np.complex128_t,ndim= nrMoments]Nf, np.ndarray[np.float64_t,ndim= nrModes] u, float tijd ):
cdef np.ndarray[np.complex128_t,ndim= nrModes] dEdt = np.zeros(nrModes,dtype=np.complex128)
cdef np.ndarray[np.complex128_t,ndim= nrMoments] dNdt= np.zeros(Nf, float64)
cdef np.ndarray[np.complex128_t,ndim= nrModes] Efcon = np.conjugate(Ef)
cdef Py_ssize_t j,n,k
for j in xrange(nrModes):
for n in xrange(nrMoments):
dEdt = dEdt + 0.5 * CouplingE((j+1,n,0)) * dg * (1+ A*1j) * Nf[n] * Ef[j] * np.exp( 1j* (sweep.omega[j]-sweep.omega) *tijd)
for k in xrange(nrModes):
if n==0:
dNdt = dNdt + (g* CouplingN((j+1, k+1, 0 )) * Efcon[j] * Ef[k] * np.exp( 1j* (sweep.omega[k]-sweep.omega[j]) *tijd) )
dNdt = dNdt + ( dg*(1+A*1j) * CouplingN((j+1,k+1,n)) * Nf[n] * Efcon[j] * Ef[k] * np.exp( 1j* (sweep.omega[k]-sweep.omega[j]) *tijd) )
dEdt = dEdt - 0.5*(pd-g)*Ef
dNdt = sweep.Jn - Nf*ed - np.real(dNdt)
return dEdt, dNdt
现在我收到了这个错误:
TypeError: %d format: a number is required, not NoneType
你可以帮我吗?
编辑1:
完成错误消息:(我的functions.pyx文件中没有提到错误发生在哪一行)
python setup_functions.py build_ext --inplace
Compiling functions.pyx because it changed.
Cythonizing functions.pyx
Traceback (most recent call last):
File "setup_functions.py", line 6, in <module>
setup(ext_modules = cythonize("functions.pyx"))
File "/usr/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 798, in cythonize
cythonize_one(*args[1:])
File "/usr/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 898, in cythonize_one
result = compile([pyx_file], options)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Main.py", line 608, in compile
return compile_multiple(source, options)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Main.py", line 586, in compile_multiple
result = run_pipeline(source, options, context=context)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Main.py", line 433, in run_pipeline
err, enddata = Pipeline.run_pipeline(pipeline, source)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Pipeline.py", line 328, in run_pipeline
data = phase(data)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Pipeline.py", line 52, in generate_pyx_code_stage
module_node.process_implementation(options, result)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/ModuleNode.py", line 109, in process_implementation
self.generate_c_code(env, options, result)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/ModuleNode.py", line 326, in generate_c_code
self.body.generate_function_definitions(env, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 394, in generate_function_definitions
stat.generate_function_definitions(env, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 394, in generate_function_definitions
stat.generate_function_definitions(env, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 2861, in generate_function_definitions
FuncDefNode.generate_function_definitions(self, env, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 1798, in generate_function_definitions
self.generate_function_body(env, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 1567, in generate_function_body
self.body.generate_execution_code(code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 400, in generate_execution_code
stat.generate_execution_code(code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 4519, in generate_execution_code
self.generate_assignment_code(code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Nodes.py", line 4650, in generate_assignment_code
self.lhs.generate_assignment_code(self.rhs, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/ExprNodes.py", line 1984, in generate_assignment_code
self.generate_acquire_buffer(rhs, code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/ExprNodes.py", line 2059, in generate_acquire_buffer
pos=self.pos, code=code)
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Buffer.py", line 369, in put_assign_to_buffer
getbuffer = get_getbuffer_call(code, "%s", buffer_aux, buffer_type) # fill in object below
File "/usr/lib/python2.7/dist-packages/Cython/Compiler/Buffer.py", line 343, in get_getbuffer_call
"%(cast)d, __pyx_stack)" % locals())
TypeError: %d format: a number is required, not NoneType